ported PrtView plugin
[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 const char* mapname_get()
97 {
98   return Map_Name(g_map);
99 }
100
101 class RadiantCoreAPI
102 {
103   _QERFuncTable_1 m_radiantcore;
104 public:
105   typedef _QERFuncTable_1 Type;
106   STRING_CONSTANT(Name, "*");
107
108   RadiantCoreAPI()
109   {
110     m_radiantcore.getEnginePath = &EnginePath_get;
111     m_radiantcore.getAppPath = &AppPath_get;
112     m_radiantcore.getGameToolsPath = &GameToolsPath_get;
113     m_radiantcore.getSettingsPath = &SettingsPath_get;
114
115     m_radiantcore.getGameName = &gamename_get;
116     m_radiantcore.getGameMode = &gamemode_get;
117
118     m_radiantcore.getMapName = &mapname_get;
119
120     m_radiantcore.getGameDescriptionKeyValue = &GameDescription_getKeyValue;
121     m_radiantcore.getRequiredGameDescriptionKeyValue = &GameDescription_getRequiredKeyValue;
122
123     m_radiantcore.attachGameToolsPathObserver = Radiant_attachGameToolsPathObserver;
124     m_radiantcore.detachGameToolsPathObserver = Radiant_detachGameToolsPathObserver;
125     m_radiantcore.attachEnginePathObserver = Radiant_attachEnginePathObserver;
126     m_radiantcore.detachEnginePathObserver = Radiant_detachEnginePathObserver;
127     m_radiantcore.attachGameNameObserver = Radiant_attachGameNameObserver;
128     m_radiantcore.detachGameNameObserver = Radiant_detachGameNameObserver;
129     m_radiantcore.attachGameModeObserver = Radiant_attachGameModeObserver;
130     m_radiantcore.detachGameModeObserver = Radiant_detachGameModeObserver;
131
132     m_radiantcore.m_pfnMessageBox = &gtk_MessageBox;
133     m_radiantcore.m_pfnFileDialog = &file_dialog;
134     m_radiantcore.m_pfnColorDialog = &color_dialog;
135     m_radiantcore.m_pfnDirDialog = &dir_dialog;
136     m_radiantcore.m_pfnNewImage = &new_plugin_image;
137   }
138   _QERFuncTable_1* getTable()
139   {
140     return &m_radiantcore;
141   }
142 };
143
144 typedef SingletonModule<RadiantCoreAPI> RadiantCoreModule;
145 typedef Static<RadiantCoreModule> StaticRadiantCoreModule;
146 StaticRegisterModule staticRegisterRadiantCore(StaticRadiantCoreModule::instance());
147
148
149 class RadiantDependencies :
150   public GlobalRadiantModuleRef,
151   public GlobalFileSystemModuleRef,
152   public GlobalEntityModuleRef,
153   public GlobalShadersModuleRef,
154   public GlobalBrushModuleRef,
155   public GlobalSceneGraphModuleRef,
156   public GlobalShaderCacheModuleRef,
157   public GlobalFiletypesModuleRef,
158   public GlobalSelectionModuleRef,
159   public GlobalReferenceModuleRef,
160   public GlobalOpenGLModuleRef,
161   public GlobalEntityClassManagerModuleRef,
162   public GlobalUndoModuleRef,
163   public GlobalScripLibModuleRef,
164   public GlobalNamespaceModuleRef
165 {
166   ImageModulesRef m_image_modules;
167   MapModulesRef m_map_modules;
168   ToolbarModulesRef m_toolbar_modules;
169   PluginModulesRef m_plugin_modules;
170
171 public:
172   RadiantDependencies() :
173     GlobalEntityModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entities")),
174     GlobalShadersModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("shaders")),
175     GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes")),
176     GlobalEntityClassManagerModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclass")),
177     m_image_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("texturetypes")),
178     m_map_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("maptypes")),
179     m_toolbar_modules("*"),
180     m_plugin_modules("*")
181   {
182   }
183
184   ImageModules& getImageModules()
185   {
186     return m_image_modules.get();
187   }
188   MapModules& getMapModules()
189   {
190     return m_map_modules.get();
191   }
192   ToolbarModules& getToolbarModules()
193   {
194     return m_toolbar_modules.get();
195   }
196   PluginModules& getPluginModules()
197   {
198     return m_plugin_modules.get();
199   }
200 };
201
202 class Radiant : public TypeSystemRef
203 {
204 public:
205   Radiant()
206   {
207     Preferences_Init();
208
209     GlobalFiletypes().addType("sound", "wav", filetype_t("PCM sound files", "*.wav"));
210
211     Selection_construct();
212     HomePaths_Construct();
213     VFS_Construct();
214     Grid_construct();
215     MultiMon_Construct();
216     MRU_Construct();
217     Pointfile_Construct();
218     GLWindow_Construct();
219     BuildMenu_Construct();
220     Map_Construct();
221     EntityList_Construct();
222     MainFrame_Construct();
223     GroupDialog_Construct();
224     SurfaceInspector_Construct();
225     PatchInspector_Construct();
226     CamWnd_Construct();
227     XYWindow_Construct();
228     BuildMonitor_Construct();
229     TextureBrowser_Construct();
230     Entity_Construct();
231     Autosave_Construct();
232     EntityInspector_construct();
233     FindTextureDialog_Construct();
234     NullModel_construct();
235     MapRoot_construct();
236
237     EnginePath_verify();
238     EnginePath_Realise();
239   }
240   ~Radiant()
241   {
242     EnginePath_Unrealise();
243
244     MapRoot_destroy();
245     NullModel_destroy();
246     FindTextureDialog_Destroy();
247     EntityInspector_destroy();
248     Autosave_Destroy();
249     Entity_Destroy();
250     TextureBrowser_Destroy();
251     BuildMonitor_Destroy();
252     XYWindow_Destroy();
253     CamWnd_Destroy();
254     PatchInspector_Destroy();
255     SurfaceInspector_Destroy();
256     GroupDialog_Destroy();
257     MainFrame_Destroy();
258     EntityList_Destroy();
259     Map_Destroy();
260     BuildMenu_Destroy();
261     GLWindow_Destroy();
262     Pointfile_Destroy();
263     MRU_Destroy();
264     MultiMon_Destroy();
265     Grid_destroy();
266     VFS_Destroy();
267     HomePaths_Destroy();
268     Selection_destroy();
269   }
270 };
271
272 namespace
273 {
274   bool g_RadiantInitialised = false;
275   RadiantDependencies* g_RadiantDependencies;
276   Radiant* g_Radiant;
277 }
278
279
280
281 bool Radiant_Construct(ModuleServer& server)
282 {
283   GlobalModuleServer::instance().set(server);
284   StaticModuleRegistryList().instance().registerModules();
285
286   g_RadiantDependencies = new RadiantDependencies();
287
288   g_RadiantInitialised = !server.getError();
289
290   if(g_RadiantInitialised)
291   {
292     g_Radiant = new Radiant;
293   }
294
295   return g_RadiantInitialised;
296 }
297 void Radiant_Destroy()
298 {
299   if(g_RadiantInitialised)
300   {
301     delete g_Radiant;
302   }
303
304   delete g_RadiantDependencies;
305 }
306
307 ImageModules& Radiant_getImageModules()
308 {
309   return g_RadiantDependencies->getImageModules();
310 }
311 MapModules& Radiant_getMapModules()
312 {
313   return g_RadiantDependencies->getMapModules();
314 }
315 ToolbarModules& Radiant_getToolbarModules()
316 {
317   return g_RadiantDependencies->getToolbarModules();
318 }
319 PluginModules& Radiant_getPluginModules()
320 {
321   return g_RadiantDependencies->getPluginModules();
322 }
323
324