Caelum
CaelumPlugin.cpp
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "CaelumPrecompiled.h"
22 #include "CaelumPlugin.h"
23 
24 // ms_Singleton was renamed to msSingleton in ogre 1.8
25 #if OGRE_VERSION_MINOR < 8
26  #define SINGLETON_MEMBER ms_Singleton
27 #else
28  #define SINGLETON_MEMBER msSingleton
29 #endif
30 
31 #if OGRE_VERSION_MINOR >= 9
32  namespace Ogre
33  {
34  template<> Caelum::CaelumPlugin* Ogre::Singleton<Caelum::CaelumPlugin>::SINGLETON_MEMBER = 0;
35  }
36 #else
37  template<> Caelum::CaelumPlugin* Ogre::Singleton<Caelum::CaelumPlugin>::SINGLETON_MEMBER = 0;
38 #endif
39 
40 namespace Caelum
41 {
42  CaelumPlugin* CaelumPlugin::getSingletonPtr () {
43  return SINGLETON_MEMBER;
44  }
45 
46  CaelumPlugin& CaelumPlugin::getSingleton () {
47  assert (SINGLETON_MEMBER);
48  return *SINGLETON_MEMBER;
49  }
50 
51  extern "C" void CAELUM_EXPORT dllStartPlugin () {
52  assert (CaelumPlugin::getSingletonPtr () == 0);
53  CaelumPlugin* plugin = new CaelumPlugin();
54  assert (CaelumPlugin::getSingletonPtr () == plugin);
55  Ogre::Root::getSingleton ().installPlugin (CaelumPlugin::getSingletonPtr ());
56  }
57 
58  extern "C" void CAELUM_EXPORT dllStopPlugin () {
59  assert (CaelumPlugin::getSingletonPtr () != 0);
60  Ogre::Root::getSingleton ().uninstallPlugin (CaelumPlugin::getSingletonPtr ());
61  delete CaelumPlugin::getSingletonPtr ();
62  assert (CaelumPlugin::getSingletonPtr () == 0);
63  }
64 
65 #if CAELUM_SCRIPT_SUPPORT
66  CaelumPlugin::CaelumPlugin(): mScriptTranslatorManager(&mTypeDescriptorData)
67 #else
68  CaelumPlugin::CaelumPlugin()
69 #endif
70  {
71  mIsInstalled = false;
72  }
73 
74  CaelumPlugin::~CaelumPlugin() {
75  }
76 
77  const Ogre::String CaelumPlugin::CAELUM_PLUGIN_NAME = "Caelum";
78 
79  const Ogre::String& CaelumPlugin::getName () const {
80  return CAELUM_PLUGIN_NAME;
81  }
82 
83  void CaelumPlugin::install ()
84  {
85  assert(!mIsInstalled && "Already installed");
86 
87  Ogre::LogManager::getSingleton ().logMessage("Caelum plugin version " +
88  Ogre::StringConverter::toString (CAELUM_VERSION_MAIN) + "." +
89  Ogre::StringConverter::toString (CAELUM_VERSION_SEC) + "." +
90  Ogre::StringConverter::toString (CAELUM_VERSION_TER) + " "
91  "installed");
92 
93 #if CAELUM_SCRIPT_SUPPORT
94  Ogre::ScriptCompilerManager::getSingleton ().addTranslatorManager (
95  getScriptTranslatorManager ());
96  Ogre::ResourceGroupManager::getSingleton()._registerResourceManager (
97  getPropScriptResourceManager ()->getResourceType (),
98  getPropScriptResourceManager ());
99 
100  getScriptTranslatorManager()->_setPropScriptResourceManager (
101  &mPropScriptResourceManager);
102 #endif // CAELUM_SCRIPT_SUPPORT
103 
104  mIsInstalled = true;
105  }
106 
107  void CaelumPlugin::initialise () {
108  }
109 
110  void CaelumPlugin::shutdown () {
111  }
112 
113  void CaelumPlugin::uninstall ()
114  {
115  assert(mIsInstalled && "Not installed");
116 
117 #if CAELUM_SCRIPT_SUPPORT
118  getScriptTranslatorManager()->_setPropScriptResourceManager (0);
119 
120  Ogre::ResourceGroupManager::getSingleton ()._unregisterResourceManager (
121  getPropScriptResourceManager ()->getResourceType ());
122  Ogre::ScriptCompilerManager::getSingleton ().removeTranslatorManager (
123  getScriptTranslatorManager ());
124 #endif // CAELUM_SCRIPT_SUPPORT
125 
126  Ogre::LogManager::getSingleton ().logMessage("Caelum plugin uninstalled");
127 
128  mIsInstalled = false;
129  }
130 
131 #if CAELUM_SCRIPT_SUPPORT
132  void CaelumPlugin::loadCaelumSystemFromScript (
133  CaelumSystem* sys,
134  const Ogre::String& objectName,
135  const Ogre::String& groupName)
136  {
137  assert (sys);
138  assert (this->isInstalled () && "Must install CaelumPlugin before loading scripts");
139 
140  // Fetch raw resource ptr. Attempt to support explicit resource groups currently in Ogre trunk.
141 #if OGRE_VERSION >= 0x00010900
142  Ogre::ResourcePtr res = getPropScriptResourceManager ()->createOrRetrieve (objectName, groupName).first;
143 #elif OGRE_VERSION >= 0x00010700
144  Ogre::ResourcePtr res = getPropScriptResourceManager ()->getByName (objectName, groupName);
145 #else
146  Ogre::ResourcePtr res = getPropScriptResourceManager ()->getByName (objectName);
147 #endif
148 
149  // Check a PropScriptResource was found.
150  PropScriptResource* propRes = static_cast<PropScriptResource*> (res.get ());
151  if (!propRes) {
152  OGRE_EXCEPT (Ogre::Exception::ERR_ITEM_NOT_FOUND,
153  "Could not find caelum_sky_system " + objectName,
154  "CaelumPlugin::loadCaelumSystemFromScript");
155  }
156 
157  // Fetch the resource stream. Look in the actual group of the resource!
158  const Ogre::String& scriptFileName = propRes->getOrigin();
159  const Ogre::String& scriptFileGroup = propRes->getGroup();
160  Ogre::DataStreamPtr streamPtr = Ogre::ResourceGroupManager::getSingleton ().openResource (
161  scriptFileName, scriptFileGroup, false);
162 
163  // Feed it into the compiler.
164  this->getScriptTranslatorManager()->getCaelumSystemTranslator()->setTranslationTarget (sys, objectName);
165  Ogre::ScriptCompilerManager::getSingleton ().parseScript (streamPtr, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
166  bool found = this->getScriptTranslatorManager()->getCaelumSystemTranslator()->foundTranslationTarget ();
167 
168  // This shouldn't normally happen.
169  if (!found) {
170  OGRE_EXCEPT (Ogre::Exception::ERR_ITEM_NOT_FOUND,
171  "Could not find caelum_sky_system " + objectName + " in file " + scriptFileName + " on reparsing. "
172  "Perhaps information in PropScriptResourceManager is out of date?",
173  "CaelumPlugin::loadCaelumSystemFromScript");
174  }
175  this->getScriptTranslatorManager()->getCaelumSystemTranslator()->clearTranslationTarget ();
176  }
177 #endif // CAELUM_SCRIPT_SUPPORT
178 }
This is the "root class" of caelum.
Definition: CaelumSystem.h:113
Implement an Ogre::Plugin for Caelum.
Definition: CaelumPlugin.h:37
Caelum namespace.
Definition: Astronomy.cpp:24