]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - plugins/entity/skincache.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / entity / skincache.cpp
index b6a3e139bf870a61ff6620eede826fc8545bd4e7..5486aad56b9c27f4a06d973271fa5e4a830fa630 100644 (file)
@@ -1,23 +1,23 @@
 /*
-Copyright (C) 2001-2006, William Joseph.
-All Rights Reserved.
+   Copyright (C) 2001-2006, William Joseph.
+   All Rights Reserved.
 
-This file is part of GtkRadiant.
+   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 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.
+   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
-*/
+   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"
 
@@ -37,312 +37,299 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "modulesystem/singletonmodule.h"
 #include "stringio.h"
 
-void parseShaderName(CopiedString& name, const char* token)
+void parseShaderName(CopiedString &name, const char *token)
 {
-  StringOutputStream cleaned(256);
-  cleaned << PathCleaned(token);
-  name = cleaned.c_str();
+    StringOutputStream cleaned(256);
+    cleaned << PathCleaned(token);
+    name = cleaned.c_str();
 }
 
-class Doom3ModelSkin
-{
-  typedef std::map<CopiedString, CopiedString> Remaps;
-  Remaps m_remaps;
+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(;;)
+    bool parseTokens(Tokeniser &tokeniser)
     {
-      const char* token = tokeniser.getToken();
-      if(token == 0)
-      {
-        return false;
-      }
-      if(string_equal(token, "}"))
-      {
+        RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "{"));
         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));
+        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();
         }
-      }
-      tokeniser.nextLine();
     }
-  }
-  const char* getRemap(const char* name) const
-  {
-    Remaps::const_iterator i = m_remaps.find(name);
-    if(i != m_remaps.end())
+
+    const char *getRemap(const char *name) const
     {
-      return (*i).second.c_str();
+        Remaps::const_iterator i = m_remaps.find(name);
+        if (i != m_remaps.end()) {
+            return (*i).second.c_str();
+        }
+        return "";
     }
-    return "";
-  }
-  void forEachRemap(const SkinRemapCallback& callback) const
-  {
-    for(Remaps::const_iterator i = m_remaps.begin(); i != m_remaps.end(); ++i)
+
+    void forEachRemap(const SkinRemapCallback &callback) const
     {
-      callback(SkinRemap((*i).first.c_str(), (*i).second.c_str()));
+        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
-{
+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())
+    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)
     {
-      return (*i).second;
+        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));
+        }
     }
-    
-    return g_nullSkin;
-  }
-
-  bool parseTokens(Tokeniser& tokeniser)
-  {
-    tokeniser.nextLine();
-    for(;;)
+
+    void parseFile(const char *name)
     {
-      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));
+        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";
+        }
     }
-  }
-
-  void parseFile(const char* name)
-  {
-    StringOutputStream relativeName(64);
-    relativeName << "skins/" << name;
-    ArchiveTextFile* file = GlobalFileSystem().openTextFile(relativeName.c_str());
-    if(file != 0)
+
+    typedef MemberCaller<GlobalSkins, void(const char *), &GlobalSkins::parseFile> ParseFileCaller;
+
+    void construct()
     {
-      globalOutputStream() << "parsing skins from " << makeQuoted(name) << "\n";
-      {
-        Tokeniser& tokeniser = GlobalScriptLibrary().m_pfnNewSimpleTokeniser(file->getInputStream());
-        parseTokens(tokeniser);
-        tokeniser.release();
-      }
-      file->release();
+        GlobalFileSystem().forEachFile("skins/", "skin", ParseFileCaller(*this));
     }
-    else
+
+    void destroy()
     {
-      globalErrorStream() << "failed to open " << makeQuoted(name) << "\n";
+        m_skins.clear();
+    }
+
+    void realise()
+    {
+        construct();
+    }
+
+    void unrealise()
+    {
+        destroy();
     }
-  }
-
-  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;
+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())
+    Doom3ModelSkinCacheElement() : m_skin(0)
     {
-      observer.realise();
     }
-  }
-  void detach(ModuleObserver& observer)
-  {
-    if(realised())
+
+    void attach(ModuleObserver &observer)
     {
-      observer.unrealise();
+        m_observers.attach(observer);
+        if (realised()) {
+            observer.realise();
+        }
     }
-    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)
+    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;
     }
-    Doom3ModelSkinCacheElement* construct(const CopiedString& name)
+
+    const char *getRemap(const char *name) const
     {
-      Doom3ModelSkinCacheElement* skin = new Doom3ModelSkinCacheElement;
-      if(m_cache.realised())
-      {
-        skin->realise(name.c_str());
-      }
-      return skin;
+        ASSERT_MESSAGE(realised(), "Doom3ModelSkinCacheElement::getRemap: not realised");
+        return m_skin->getRemap(name);
     }
-    void destroy(Doom3ModelSkinCacheElement* skin)
+
+    void forEachRemap(const SkinRemapCallback &callback) const
     {
-      if(m_cache.realised())
-      {
-        skin->unrealise();
-      }
-      delete skin;
+        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;
+        }
 
-  typedef HashedCache<CopiedString, Doom3ModelSkinCacheElement, HashString, std::equal_to<CopiedString>, CreateDoom3ModelSkin> Cache;
-  Cache m_cache;
-  bool m_realised;
+        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)
+    typedef ModelSkinCache Type;
+
+    STRING_CONSTANT(Name, "*");
+
+    ModelSkinCache *getTable()
     {
-      (*i).value->realise((*i).key.c_str());
+        return this;
     }
-  }
-  void unrealise()
-  {
-    m_realised = false;
-    for(Cache::iterator i = m_cache.begin(); i != m_cache.end(); ++i)
+
+    Doom3ModelSkinCache() : m_cache(CreateDoom3ModelSkin(*this)), m_realised(false)
+    {
+        GlobalFileSystem().attach(*this);
+    }
+
+    ~Doom3ModelSkinCache()
     {
-      (*i).value->unrealise();
+        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();
     }
-    g_skins.unrealise();
-  }
 };
 
-class Doom3ModelSkinCacheDependencies : public GlobalFileSystemModuleRef, public GlobalScripLibModuleRef
-{
+class Doom3ModelSkinCacheDependencies : public GlobalFileSystemModuleRef, public GlobalScripLibModuleRef {
 };
 
 typedef SingletonModule<Doom3ModelSkinCache, Doom3ModelSkinCacheDependencies> Doom3ModelSkinCacheModule;
 
 Doom3ModelSkinCacheModule g_Doom3ModelSkinCacheModule;
 
-void Doom3ModelSkinCacheModule_selfRegister(ModuleServerserver)
+void Doom3ModelSkinCacheModule_selfRegister(ModuleServer &server)
 {
-  g_Doom3ModelSkinCacheModule.selfRegister();
+    g_Doom3ModelSkinCacheModule.selfRegister();
 }
-