123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- /*
- Copyright (C) 2001-2006, William Joseph.
- All Rights Reserved.
- This file is part of GtkRadiant.
- GtkRadiant is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- GtkRadiant is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GtkRadiant; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include "skincache.h"
- #include "ifilesystem.h"
- #include "iscriplib.h"
- #include "iarchive.h"
- #include "modelskin.h"
- #include <map>
- #include "stream/stringstream.h"
- #include "generic/callback.h"
- #include "container/cache.h"
- #include "container/hashfunc.h"
- #include "os/path.h"
- #include "moduleobservers.h"
- #include "modulesystem/singletonmodule.h"
- #include "stringio.h"
- void parseShaderName(CopiedString& name, const char* token)
- {
- StringOutputStream cleaned(256);
- cleaned << PathCleaned(token);
- name = cleaned.c_str();
- }
- class Doom3ModelSkin
- {
- typedef std::map<CopiedString, CopiedString> Remaps;
- Remaps m_remaps;
- public:
- bool parseTokens(Tokeniser& tokeniser)
- {
- RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "{"));
- tokeniser.nextLine();
- for(;;)
- {
- const char* token = tokeniser.getToken();
- if(token == 0)
- {
- return false;
- }
- if(string_equal(token, "}"))
- {
- tokeniser.nextLine();
- return true;
- }
- else if(string_equal(token, "model"))
- {
- //const char* model =
- tokeniser.getToken();
- }
- else
- {
- CopiedString from, to;
- parseShaderName(from, token);
- tokeniser.nextLine(); // hack to handle badly formed skins
- parseShaderName(to, tokeniser.getToken());
- if(!string_equal(from.c_str(), to.c_str()))
- {
- m_remaps.insert(Remaps::value_type(from, to));
- }
- }
- tokeniser.nextLine();
- }
- }
- const char* getRemap(const char* name) const
- {
- Remaps::const_iterator i = m_remaps.find(name);
- if(i != m_remaps.end())
- {
- return (*i).second.c_str();
- }
- return "";
- }
- void forEachRemap(const SkinRemapCallback& callback) const
- {
- for(Remaps::const_iterator i = m_remaps.begin(); i != m_remaps.end(); ++i)
- {
- callback(SkinRemap((*i).first.c_str(), (*i).second.c_str()));
- }
- }
- };
- class GlobalSkins
- {
- public:
- typedef std::map<CopiedString, Doom3ModelSkin> SkinMap;
- SkinMap m_skins;
- Doom3ModelSkin g_nullSkin;
- Doom3ModelSkin& getSkin(const char* name)
- {
- SkinMap::iterator i = m_skins.find(name);
- if(i != m_skins.end())
- {
- return (*i).second;
- }
-
- return g_nullSkin;
- }
- bool parseTokens(Tokeniser& tokeniser)
- {
- tokeniser.nextLine();
- for(;;)
- {
- const char* token = tokeniser.getToken();
- if(token == 0)
- {
- // end of token stream
- return true;
- }
- if(!string_equal(token, "skin"))
- {
- Tokeniser_unexpectedError(tokeniser, token, "skin");
- return false;
- }
- const char* other = tokeniser.getToken();
- if(other == 0)
- {
- Tokeniser_unexpectedError(tokeniser, token, "#string");
- return false;
- }
- CopiedString name;
- parseShaderName(name, other);
- Doom3ModelSkin& skin = m_skins[name];
- RETURN_FALSE_IF_FAIL(skin.parseTokens(tokeniser));
- }
- }
- void parseFile(const char* name)
- {
- StringOutputStream relativeName(64);
- relativeName << "skins/" << name;
- ArchiveTextFile* file = GlobalFileSystem().openTextFile(relativeName.c_str());
- if(file != 0)
- {
- globalOutputStream() << "parsing skins from " << makeQuoted(name) << "\n";
- {
- Tokeniser& tokeniser = GlobalScriptLibrary().m_pfnNewSimpleTokeniser(file->getInputStream());
- parseTokens(tokeniser);
- tokeniser.release();
- }
- file->release();
- }
- else
- {
- globalErrorStream() << "failed to open " << makeQuoted(name) << "\n";
- }
- }
- typedef MemberCaller1<GlobalSkins, const char*, &GlobalSkins::parseFile> ParseFileCaller;
- void construct()
- {
- GlobalFileSystem().forEachFile("skins/", "skin", ParseFileCaller(*this));
- }
- void destroy()
- {
- m_skins.clear();
- }
- void realise()
- {
- construct();
- }
- void unrealise()
- {
- destroy();
- }
- };
- GlobalSkins g_skins;
- class Doom3ModelSkinCacheElement : public ModelSkin
- {
- ModuleObservers m_observers;
- Doom3ModelSkin* m_skin;
- public:
- Doom3ModelSkinCacheElement() : m_skin(0)
- {
- }
- void attach(ModuleObserver& observer)
- {
- m_observers.attach(observer);
- if(realised())
- {
- observer.realise();
- }
- }
- void detach(ModuleObserver& observer)
- {
- if(realised())
- {
- observer.unrealise();
- }
- m_observers.detach(observer);
- }
- bool realised() const
- {
- return m_skin != 0;
- }
- void realise(const char* name)
- {
- ASSERT_MESSAGE(!realised(), "Doom3ModelSkinCacheElement::realise: already realised");
- m_skin = &g_skins.getSkin(name);
- m_observers.realise();
- }
- void unrealise()
- {
- ASSERT_MESSAGE(realised(), "Doom3ModelSkinCacheElement::unrealise: not realised");
- m_observers.unrealise();
- m_skin = 0;
- }
- const char* getRemap(const char* name) const
- {
- ASSERT_MESSAGE(realised(), "Doom3ModelSkinCacheElement::getRemap: not realised");
- return m_skin->getRemap(name);
- }
- void forEachRemap(const SkinRemapCallback& callback) const
- {
- ASSERT_MESSAGE(realised(), "Doom3ModelSkinCacheElement::forEachRemap: not realised");
- m_skin->forEachRemap(callback);
- }
- };
- class Doom3ModelSkinCache : public ModelSkinCache, public ModuleObserver
- {
- class CreateDoom3ModelSkin
- {
- Doom3ModelSkinCache& m_cache;
- public:
- explicit CreateDoom3ModelSkin(Doom3ModelSkinCache& cache)
- : m_cache(cache)
- {
- }
- Doom3ModelSkinCacheElement* construct(const CopiedString& name)
- {
- Doom3ModelSkinCacheElement* skin = new Doom3ModelSkinCacheElement;
- if(m_cache.realised())
- {
- skin->realise(name.c_str());
- }
- return skin;
- }
- void destroy(Doom3ModelSkinCacheElement* skin)
- {
- if(m_cache.realised())
- {
- skin->unrealise();
- }
- delete skin;
- }
- };
- typedef HashedCache<CopiedString, Doom3ModelSkinCacheElement, HashString, std::equal_to<CopiedString>, CreateDoom3ModelSkin> Cache;
- Cache m_cache;
- bool m_realised;
- public:
- typedef ModelSkinCache Type;
- STRING_CONSTANT(Name, "*");
- ModelSkinCache* getTable()
- {
- return this;
- }
- Doom3ModelSkinCache() : m_cache(CreateDoom3ModelSkin(*this)), m_realised(false)
- {
- GlobalFileSystem().attach(*this);
- }
- ~Doom3ModelSkinCache()
- {
- GlobalFileSystem().detach(*this);
- }
- ModelSkin& capture(const char* name)
- {
- return *m_cache.capture(name);
- }
- void release(const char* name)
- {
- m_cache.release(name);
- }
- bool realised() const
- {
- return m_realised;
- }
- void realise()
- {
- g_skins.realise();
- m_realised = true;
- for(Cache::iterator i = m_cache.begin(); i != m_cache.end(); ++i)
- {
- (*i).value->realise((*i).key.c_str());
- }
- }
- void unrealise()
- {
- m_realised = false;
- for(Cache::iterator i = m_cache.begin(); i != m_cache.end(); ++i)
- {
- (*i).value->unrealise();
- }
- g_skins.unrealise();
- }
- };
- class Doom3ModelSkinCacheDependencies : public GlobalFileSystemModuleRef, public GlobalScripLibModuleRef
- {
- };
- typedef SingletonModule<Doom3ModelSkinCache, Doom3ModelSkinCacheDependencies> Doom3ModelSkinCacheModule;
- Doom3ModelSkinCacheModule g_Doom3ModelSkinCacheModule;
- void Doom3ModelSkinCacheModule_selfRegister(ModuleServer& server)
- {
- g_Doom3ModelSkinCacheModule.selfRegister();
- }
|