plugin.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "plugin.h"
  18. #include <stdio.h>
  19. #include "picomodel.h"
  20. typedef unsigned char byte;
  21. #include <stdlib.h>
  22. #include <algorithm>
  23. #include <list>
  24. #include "iscenegraph.h"
  25. #include "irender.h"
  26. #include "iselection.h"
  27. #include "iimage.h"
  28. #include "imodel.h"
  29. #include "igl.h"
  30. #include "ifilesystem.h"
  31. #include "iundo.h"
  32. #include "ifiletypes.h"
  33. #include "modulesystem/singletonmodule.h"
  34. #include "stream/textstream.h"
  35. #include "string/string.h"
  36. #include "stream/stringstream.h"
  37. #include "typesystem.h"
  38. #include "model.h"
  39. void PicoPrintFunc( int level, const char *str )
  40. {
  41. if( str == 0 )
  42. return;
  43. switch( level )
  44. {
  45. case PICO_NORMAL:
  46. globalOutputStream() << str << "\n";
  47. break;
  48. case PICO_VERBOSE:
  49. //globalOutputStream() << "PICO_VERBOSE: " << str << "\n";
  50. break;
  51. case PICO_WARNING:
  52. globalErrorStream() << "PICO_WARNING: " << str << "\n";
  53. break;
  54. case PICO_ERROR:
  55. globalErrorStream() << "PICO_ERROR: " << str << "\n";
  56. break;
  57. case PICO_FATAL:
  58. globalErrorStream() << "PICO_FATAL: " << str << "\n";
  59. break;
  60. }
  61. }
  62. void PicoLoadFileFunc( char *name, byte **buffer, int *bufSize )
  63. {
  64. *bufSize = vfsLoadFile( (const char*) name, (void**) buffer);
  65. }
  66. void PicoFreeFileFunc( void* file )
  67. {
  68. vfsFreeFile(file);
  69. }
  70. void pico_initialise()
  71. {
  72. PicoInit();
  73. PicoSetMallocFunc( malloc );
  74. PicoSetFreeFunc( free );
  75. PicoSetPrintFunc( PicoPrintFunc );
  76. PicoSetLoadFileFunc( PicoLoadFileFunc );
  77. PicoSetFreeFileFunc( PicoFreeFileFunc );
  78. }
  79. class PicoModelLoader : public ModelLoader
  80. {
  81. const picoModule_t* m_module;
  82. public:
  83. PicoModelLoader(const picoModule_t* module) : m_module(module)
  84. {
  85. }
  86. scene::Node& loadModel(ArchiveFile& file)
  87. {
  88. return loadPicoModel(m_module, file);
  89. }
  90. };
  91. class ModelPicoDependencies :
  92. public GlobalFileSystemModuleRef,
  93. public GlobalOpenGLModuleRef,
  94. public GlobalUndoModuleRef,
  95. public GlobalSceneGraphModuleRef,
  96. public GlobalShaderCacheModuleRef,
  97. public GlobalSelectionModuleRef,
  98. public GlobalFiletypesModuleRef
  99. {
  100. };
  101. class ModelPicoAPI : public TypeSystemRef
  102. {
  103. PicoModelLoader m_modelLoader;
  104. public:
  105. typedef ModelLoader Type;
  106. ModelPicoAPI(const char* extension, const picoModule_t* module) :
  107. m_modelLoader(module)
  108. {
  109. StringOutputStream filter(128);
  110. filter << "*." << extension;
  111. GlobalFiletypesModule::getTable().addType(Type::Name(), extension, filetype_t(module->displayName, filter.c_str()));
  112. }
  113. ModelLoader* getTable()
  114. {
  115. return &m_modelLoader;
  116. }
  117. };
  118. class PicoModelAPIConstructor
  119. {
  120. CopiedString m_extension;
  121. const picoModule_t* m_module;
  122. public:
  123. PicoModelAPIConstructor(const char* extension, const picoModule_t* module) :
  124. m_extension(extension), m_module(module)
  125. {
  126. }
  127. const char* getName()
  128. {
  129. return m_extension.c_str();
  130. }
  131. ModelPicoAPI* constructAPI(ModelPicoDependencies& dependencies)
  132. {
  133. return new ModelPicoAPI(m_extension.c_str(), m_module);
  134. }
  135. void destroyAPI(ModelPicoAPI* api)
  136. {
  137. delete api;
  138. }
  139. };
  140. typedef SingletonModule<ModelPicoAPI, ModelPicoDependencies, PicoModelAPIConstructor> PicoModelModule;
  141. typedef std::list<PicoModelModule> PicoModelModules;
  142. PicoModelModules g_PicoModelModules;
  143. extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
  144. {
  145. GlobalErrorStream::instance().setOutputStream(server.getErrorStream());
  146. GlobalOutputStream::instance().setOutputStream(server.getOutputStream());
  147. GlobalDebugMessageHandler::instance().setHandler(server.getDebugMessageHandler());
  148. GlobalModuleServer::instance().set(server);
  149. pico_initialise();
  150. const picoModule_t** modules = PicoModuleList( 0 );
  151. while(*modules != 0)
  152. {
  153. const picoModule_t* module = *modules++;
  154. if(module->canload && module->load)
  155. {
  156. for(char*const* ext = module->defaultExts; *ext != 0; ++ext)
  157. {
  158. g_PicoModelModules.push_back(PicoModelModule(PicoModelAPIConstructor(*ext, module)));
  159. g_PicoModelModules.back().selfRegister();
  160. }
  161. }
  162. }
  163. }