3cf53115310f9a187fbe547c1f83c3e60d39fd5f
[xonotic/netradiant.git] / radiant / plugin.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant 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 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "plugin.h"
23
24 #include "debugging/debugging.h"
25
26 #include "qerplugin.h"
27 #include "ifilesystem.h"
28 #include "ishaders.h"
29 #include "ientity.h"
30 #include "ieclass.h"
31 #include "irender.h"
32 #include "iscenegraph.h"
33 #include "iselection.h"
34 #include "ifilter.h"
35 #include "iscriplib.h"
36 #include "igl.h"
37 #include "iundo.h"
38 #include "itextures.h"
39 #include "ireference.h"
40 #include "ifiletypes.h"
41 #include "preferencesystem.h"
42 #include "ibrush.h"
43 #include "ipatch.h"
44 #include "iimage.h"
45 #include "itoolbar.h"
46 #include "iplugin.h"
47 #include "imap.h"
48 #include "namespace.h"
49
50 #include "gtkutil/messagebox.h"
51 #include "gtkutil/filechooser.h"
52 #include "maplib.h"
53
54 #include "error.h"
55 #include "map.h"
56 #include "qe3.h"
57 #include "entityinspector.h"
58 #include "entitylist.h"
59 #include "points.h"
60 #include "gtkmisc.h"
61 #include "texwindow.h"
62 #include "mainframe.h"
63 #include "build.h"
64 #include "mru.h"
65 #include "multimon.h"
66 #include "surfacedialog.h"
67 #include "groupdialog.h"
68 #include "patchdialog.h"
69 #include "camwindow.h"
70 #include "watchbsp.h"
71 #include "xywindow.h"
72 #include "entity.h"
73 #include "select.h"
74 #include "preferences.h"
75 #include "autosave.h"
76 #include "plugintoolbar.h"
77 #include "findtexturedialog.h"
78 #include "nullmodel.h"
79 #include "grid.h"
80
81 #include "modulesystem/modulesmap.h"
82 #include "modulesystem/singletonmodule.h"
83
84 #include "generic/callback.h"
85
86 const char* GameDescription_getKeyValue(const char* key)
87 {
88   return g_pGameDescription->getKeyValue(key);
89 }
90
91 const char* GameDescription_getRequiredKeyValue(const char* key)
92 {
93   return g_pGameDescription->getRequiredKeyValue(key);
94 }
95
96 class RadiantCoreAPI
97 {
98   _QERFuncTable_1 m_radiantcore;
99 public:
100   typedef _QERFuncTable_1 Type;
101   STRING_CONSTANT(Name, "*");
102
103   RadiantCoreAPI()
104   {
105     m_radiantcore.getEnginePath = &EnginePath_get;
106     m_radiantcore.getAppPath = &AppPath_get;
107     m_radiantcore.getGameToolsPath = &GameToolsPath_get;
108     m_radiantcore.getSettingsPath = &SettingsPath_get;
109
110     m_radiantcore.getGameName = &gamename_get;
111     m_radiantcore.getGameMode = &gamemode_get;
112
113     m_radiantcore.getGameDescriptionKeyValue = &GameDescription_getKeyValue;
114     m_radiantcore.getRequiredGameDescriptionKeyValue = &GameDescription_getRequiredKeyValue;
115
116     m_radiantcore.attachGameToolsPathObserver = Radiant_attachGameToolsPathObserver;
117     m_radiantcore.detachGameToolsPathObserver = Radiant_detachGameToolsPathObserver;
118     m_radiantcore.attachEnginePathObserver = Radiant_attachEnginePathObserver;
119     m_radiantcore.detachEnginePathObserver = Radiant_detachEnginePathObserver;
120     m_radiantcore.attachGameNameObserver = Radiant_attachGameNameObserver;
121     m_radiantcore.detachGameNameObserver = Radiant_detachGameNameObserver;
122     m_radiantcore.attachGameModeObserver = Radiant_attachGameModeObserver;
123     m_radiantcore.detachGameModeObserver = Radiant_detachGameModeObserver;
124
125     m_radiantcore.m_pfnMessageBox = &gtk_MessageBox;
126     m_radiantcore.m_pfnFileDialog = &file_dialog;
127     m_radiantcore.m_pfnColorDialog = &color_dialog;
128     m_radiantcore.m_pfnDirDialog = &dir_dialog;
129     m_radiantcore.m_pfnNewImage = &new_plugin_image;
130   }
131   _QERFuncTable_1* getTable()
132   {
133     return &m_radiantcore;
134   }
135 };
136
137 typedef SingletonModule<RadiantCoreAPI> RadiantCoreModule;
138 typedef Static<RadiantCoreModule> StaticRadiantCoreModule;
139 StaticRegisterModule staticRegisterRadiantCore(StaticRadiantCoreModule::instance());
140
141
142 class RadiantDependencies :
143   public GlobalRadiantModuleRef,
144   public GlobalFileSystemModuleRef,
145   public GlobalEntityModuleRef,
146   public GlobalShadersModuleRef,
147   public GlobalBrushModuleRef,
148   public GlobalSceneGraphModuleRef,
149   public GlobalShaderCacheModuleRef,
150   public GlobalFiletypesModuleRef,
151   public GlobalSelectionModuleRef,
152   public GlobalReferenceModuleRef,
153   public GlobalOpenGLModuleRef,
154   public GlobalEntityClassManagerModuleRef,
155   public GlobalUndoModuleRef,
156   public GlobalScripLibModuleRef,
157   public GlobalNamespaceModuleRef
158 {
159   ImageModulesRef m_image_modules;
160   MapModulesRef m_map_modules;
161   ToolbarModulesRef m_toolbar_modules;
162   PluginModulesRef m_plugin_modules;
163
164 public:
165   RadiantDependencies() :
166     GlobalEntityModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entities")),
167     GlobalShadersModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("shaders")),
168     GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes")),
169     GlobalEntityClassManagerModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclass")),
170     m_image_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("texturetypes")),
171     m_map_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("maptypes")),
172     m_toolbar_modules("*"),
173     m_plugin_modules("*")
174   {
175   }
176
177   ImageModules& getImageModules()
178   {
179     return m_image_modules.get();
180   }
181   MapModules& getMapModules()
182   {
183     return m_map_modules.get();
184   }
185   ToolbarModules& getToolbarModules()
186   {
187     return m_toolbar_modules.get();
188   }
189   PluginModules& getPluginModules()
190   {
191     return m_plugin_modules.get();
192   }
193 };
194
195 class Radiant : public TypeSystemRef
196 {
197 public:
198   Radiant()
199   {
200     Preferences_Init();
201
202     GlobalFiletypes().addType("sound", "wav", filetype_t("PCM sound files", "*.wav"));
203
204     Selection_construct();
205     HomePaths_Construct();
206     VFS_Construct();
207     Grid_construct();
208     MultiMon_Construct();
209     MRU_Construct();
210     Pointfile_Construct();
211     GLWindow_Construct();
212     BuildMenu_Construct();
213     Map_Construct();
214     EntityList_Construct();
215     MainFrame_Construct();
216     GroupDialog_Construct();
217     SurfaceInspector_Construct();
218     PatchInspector_Construct();
219     CamWnd_Construct();
220     XYWindow_Construct();
221     BuildMonitor_Construct();
222     TextureBrowser_Construct();
223     Entity_Construct();
224     Autosave_Construct();
225     EntityInspector_construct();
226     FindTextureDialog_Construct();
227     NullModel_construct();
228     MapRoot_construct();
229
230     EnginePath_verify();
231     EnginePath_Realise();
232   }
233   ~Radiant()
234   {
235     EnginePath_Unrealise();
236
237     MapRoot_destroy();
238     NullModel_destroy();
239     FindTextureDialog_Destroy();
240     EntityInspector_destroy();
241     Autosave_Destroy();
242     Entity_Destroy();
243     TextureBrowser_Destroy();
244     BuildMonitor_Destroy();
245     XYWindow_Destroy();
246     CamWnd_Destroy();
247     PatchInspector_Destroy();
248     SurfaceInspector_Destroy();
249     GroupDialog_Destroy();
250     MainFrame_Destroy();
251     EntityList_Destroy();
252     Map_Destroy();
253     BuildMenu_Destroy();
254     GLWindow_Destroy();
255     Pointfile_Destroy();
256     MRU_Destroy();
257     MultiMon_Destroy();
258     Grid_destroy();
259     VFS_Destroy();
260     HomePaths_Destroy();
261     Selection_destroy();
262   }
263 };
264
265 namespace
266 {
267   bool g_RadiantInitialised = false;
268   RadiantDependencies* g_RadiantDependencies;
269   Radiant* g_Radiant;
270 }
271
272
273
274 bool Radiant_Construct(ModuleServer& server)
275 {
276   GlobalModuleServer::instance().set(server);
277   StaticModuleRegistryList().instance().registerModules();
278
279   g_RadiantDependencies = new RadiantDependencies();
280
281   g_RadiantInitialised = !server.getError();
282
283   if(g_RadiantInitialised)
284   {
285     g_Radiant = new Radiant;
286   }
287
288   return g_RadiantInitialised;
289 }
290 void Radiant_Destroy()
291 {
292   if(g_RadiantInitialised)
293   {
294     delete g_Radiant;
295   }
296
297   delete g_RadiantDependencies;
298 }
299
300 ImageModules& Radiant_getImageModules()
301 {
302   return g_RadiantDependencies->getImageModules();
303 }
304 MapModules& Radiant_getMapModules()
305 {
306   return g_RadiantDependencies->getMapModules();
307 }
308 ToolbarModules& Radiant_getToolbarModules()
309 {
310   return g_RadiantDependencies->getToolbarModules();
311 }
312 PluginModules& Radiant_getPluginModules()
313 {
314   return g_RadiantDependencies->getPluginModules();
315 }
316
317