| 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 <stdio.h> |
| 23 | #include "picomodel.h" |
| 24 | typedef unsigned char byte; |
| 25 | #include <stdlib.h> |
| 26 | #include <algorithm> |
| 27 | #include <list> |
| 28 | |
| 29 | #include "iscenegraph.h" |
| 30 | #include "irender.h" |
| 31 | #include "iselection.h" |
| 32 | #include "iimage.h" |
| 33 | #include "imodel.h" |
| 34 | #include "igl.h" |
| 35 | #include "ifilesystem.h" |
| 36 | #include "iundo.h" |
| 37 | #include "ifiletypes.h" |
| 38 | |
| 39 | #include "modulesystem/singletonmodule.h" |
| 40 | #include "stream/textstream.h" |
| 41 | #include "string/string.h" |
| 42 | #include "stream/stringstream.h" |
| 43 | #include "typesystem.h" |
| 44 | #include "cmdlib.h" |
| 45 | |
| 46 | #include "model.h" |
| 47 | |
| 48 | void PicoPrintFunc( int level, const char *str ){ |
| 49 | if ( str == 0 ) { |
| 50 | return; |
| 51 | } |
| 52 | switch ( level ) |
| 53 | { |
| 54 | case PICO_NORMAL: |
| 55 | globalOutputStream() << str << "\n"; |
| 56 | break; |
| 57 | |
| 58 | case PICO_VERBOSE: |
| 59 | //globalOutputStream() << "PICO_VERBOSE: " << str << "\n"; |
| 60 | break; |
| 61 | |
| 62 | case PICO_WARNING: |
| 63 | globalErrorStream() << "PICO_WARNING: " << str << "\n"; |
| 64 | break; |
| 65 | |
| 66 | case PICO_ERROR: |
| 67 | globalErrorStream() << "PICO_ERROR: " << str << "\n"; |
| 68 | break; |
| 69 | |
| 70 | case PICO_FATAL: |
| 71 | globalErrorStream() << "PICO_FATAL: " << str << "\n"; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void PicoLoadFileFunc( const char *name, byte **buffer, int *bufSize ){ |
| 77 | *bufSize = vfsLoadFile( name, (void**) buffer ); |
| 78 | } |
| 79 | |
| 80 | void PicoFreeFileFunc( void* file ){ |
| 81 | vfsFreeFile( file ); |
| 82 | } |
| 83 | |
| 84 | static CopiedString s_modelSaveRoot; |
| 85 | |
| 86 | void PicoSetModelSaveRoot( const char *modelVfsPath ){ |
| 87 | if ( modelVfsPath != NULL ) { |
| 88 | const char *root = GlobalFileSystem().findFile( modelVfsPath ); |
| 89 | if ( root != NULL && root[0] != '\0' ) { |
| 90 | s_modelSaveRoot = root; |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | s_modelSaveRoot = ""; |
| 95 | } |
| 96 | |
| 97 | int PicoSaveFileFunc( const char *name, const unsigned char *buffer, int bufSize ){ |
| 98 | /* resolve VFS-relative name to absolute filesystem path */ |
| 99 | const char *root = NULL; |
| 100 | |
| 101 | /* prefer the root where the current model was loaded from */ |
| 102 | if ( !string_empty( s_modelSaveRoot.c_str() ) ) { |
| 103 | root = s_modelSaveRoot.c_str(); |
| 104 | } |
| 105 | |
| 106 | if ( root == NULL || root[0] == '\0' ) { |
| 107 | root = GlobalFileSystem().findFile( name ); |
| 108 | } |
| 109 | |
| 110 | if ( root == NULL || root[0] == '\0' ) { |
| 111 | root = GlobalFileSystem().findFile( "" ); |
| 112 | if ( root == NULL || root[0] == '\0' ) { |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* construct full path */ |
| 118 | StringOutputStream fullPath( 256 ); |
| 119 | fullPath << root << name; |
| 120 | |
| 121 | FILE *f = fopen( fullPath.c_str(), "rb" ); |
| 122 | if ( f != NULL ) { |
| 123 | /* file already exists, skip writing */ |
| 124 | fclose( f ); |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | /* create intermediate directories if needed */ |
| 129 | { |
| 130 | StringOutputStream dirPath( 256 ); |
| 131 | dirPath << fullPath.c_str(); |
| 132 | char *dir = const_cast<char*>( dirPath.c_str() ); |
| 133 | for ( char *p = dir + 1; *p != '\0'; ++p ) { |
| 134 | if ( *p == '/' ) { |
| 135 | *p = '\0'; |
| 136 | Q_mkdir( dir ); |
| 137 | *p = '/'; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | f = fopen( fullPath.c_str(), "wb" ); |
| 143 | if ( f == NULL ) { |
| 144 | globalErrorStream() << "Failed to save file: " << fullPath.c_str() << "\n"; |
| 145 | return 0; |
| 146 | } |
| 147 | fwrite( buffer, 1, bufSize, f ); |
| 148 | fclose( f ); |
| 149 | globalOutputStream() << "Save file: " << fullPath.c_str() << "\n"; |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | void pico_initialise(){ |
| 154 | PicoInit(); |
| 155 | PicoSetMallocFunc( malloc ); |
| 156 | PicoSetFreeFunc( free ); |
| 157 | PicoSetPrintFunc( PicoPrintFunc ); |
| 158 | PicoSetLoadFileFunc( PicoLoadFileFunc ); |
| 159 | PicoSetFreeFileFunc( PicoFreeFileFunc ); |
| 160 | PicoSetSaveFileFunc( PicoSaveFileFunc ); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | class PicoModelLoader : public ModelLoader |
| 165 | { |
| 166 | const picoModule_t* m_module; |
| 167 | public: |
| 168 | PicoModelLoader( const picoModule_t* module ) : m_module( module ){ |
| 169 | } |
| 170 | scene::Node& loadModel( ArchiveFile& file ){ |
| 171 | return loadPicoModel( m_module, file ); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | class ModelPicoDependencies : |
| 176 | public GlobalFileSystemModuleRef, |
| 177 | public GlobalOpenGLModuleRef, |
| 178 | public GlobalUndoModuleRef, |
| 179 | public GlobalSceneGraphModuleRef, |
| 180 | public GlobalShaderCacheModuleRef, |
| 181 | public GlobalSelectionModuleRef, |
| 182 | public GlobalFiletypesModuleRef |
| 183 | { |
| 184 | }; |
| 185 | |
| 186 | class ModelPicoAPI : public TypeSystemRef |
| 187 | { |
| 188 | PicoModelLoader m_modelLoader; |
| 189 | public: |
| 190 | typedef ModelLoader Type; |
| 191 | |
| 192 | ModelPicoAPI( const char* extension, const picoModule_t* module ) : |
| 193 | m_modelLoader( module ){ |
| 194 | StringOutputStream filter( 128 ); |
| 195 | filter << "*." << extension; |
| 196 | GlobalFiletypesModule::getTable().addType( Type::Name(), extension, filetype_t( module->displayName, filter.c_str() ) ); |
| 197 | } |
| 198 | ModelLoader* getTable(){ |
| 199 | return &m_modelLoader; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | class PicoModelAPIConstructor |
| 204 | { |
| 205 | CopiedString m_extension; |
| 206 | const picoModule_t* m_module; |
| 207 | public: |
| 208 | PicoModelAPIConstructor( const char* extension, const picoModule_t* module ) : |
| 209 | m_extension( extension ), m_module( module ){ |
| 210 | } |
| 211 | const char* getName(){ |
| 212 | return m_extension.c_str(); |
| 213 | } |
| 214 | ModelPicoAPI* constructAPI( ModelPicoDependencies& dependencies ){ |
| 215 | return new ModelPicoAPI( m_extension.c_str(), m_module ); |
| 216 | } |
| 217 | void destroyAPI( ModelPicoAPI* api ){ |
| 218 | delete api; |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | typedef SingletonModule<ModelPicoAPI, ModelPicoDependencies, PicoModelAPIConstructor> PicoModelModule; |
| 224 | typedef std::list<PicoModelModule> PicoModelModules; |
| 225 | PicoModelModules g_PicoModelModules; |
| 226 | |
| 227 | |
| 228 | extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){ |
| 229 | initialiseModule( server ); |
| 230 | |
| 231 | pico_initialise(); |
| 232 | |
| 233 | const picoModule_t** modules = PicoModuleList( 0 ); |
| 234 | while ( *modules != 0 ) |
| 235 | { |
| 236 | const picoModule_t* module = *modules++; |
| 237 | if ( module->canload && module->load ) { |
| 238 | for ( char*const* ext = module->defaultExts; *ext != 0; ++ext ) |
| 239 | { |
| 240 | g_PicoModelModules.push_back( PicoModelModule( PicoModelAPIConstructor( *ext, module ) ) ); |
| 241 | g_PicoModelModules.back().selfRegister(); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |