goat3d
diff exporters/maxgoat_stub/src/stub.cc @ 64:99715321ad6d
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Apr 2014 08:53:42 +0300 |
parents | d317eb4f83da |
children | 3751aabbc5b3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/exporters/maxgoat_stub/src/stub.cc Thu Apr 17 08:53:42 2014 +0300 1.3 @@ -0,0 +1,267 @@ 1.4 +#include <stdio.h> 1.5 +#include <string.h> 1.6 +#include <stdlib.h> 1.7 +#include <errno.h> 1.8 +#include <map> 1.9 +#include <windows.h> 1.10 +#include <shlobj.h> 1.11 +#include "max.h" 1.12 +#include "impexp.h" // SceneExport 1.13 +#include "iparamb2.h" // ClassDesc2 1.14 +#include "plugapi.h" 1.15 +#include "IGame.h" 1.16 +#include "IGameExport.h" 1.17 +#include "IConversionmanager.h" 1.18 + 1.19 + 1.20 +#pragma comment (lib, "core.lib") 1.21 +#pragma comment (lib, "geom.lib") 1.22 +#pragma comment (lib, "gfx.lib") 1.23 +#pragma comment (lib, "mesh.lib") 1.24 +#pragma comment (lib, "maxutil.lib") 1.25 +#pragma comment (lib, "maxscrpt.lib") 1.26 +#pragma comment (lib, "paramblk2.lib") 1.27 +#pragma comment (lib, "msxml2.lib") 1.28 +#pragma comment (lib, "igame.lib") 1.29 +#pragma comment (lib, "comctl32.lib") 1.30 + 1.31 + 1.32 +#define VER_MAJOR 1 1.33 +#define VER_MINOR 0 1.34 +#define VERSION(major, minor) \ 1.35 + ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor))) 1.36 + 1.37 +typedef int (*PluginInitFunc)(); 1.38 +typedef int (*PluginShutdownFunc)(); 1.39 +typedef ClassDesc *(*PluginClassDescFunc)(int); 1.40 + 1.41 +static FILE *logfile; 1.42 +static HINSTANCE hinst; 1.43 +static const wchar_t *copyright_str = L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details."; 1.44 + 1.45 +class GoatExporterStub : public SceneExport { 1.46 +public: 1.47 + IGameScene *igame; 1.48 + 1.49 + int ExtCount() { return 1; } 1.50 + const TCHAR *Ext(int n) { return L"goatsce"; } 1.51 + const TCHAR *LongDesc() { return L"Goat3D Scene file"; } 1.52 + const TCHAR *ShortDesc() { return L"Goat3D Scene"; } 1.53 + const TCHAR *AuthorName() { return L"John Tsiombikas"; } 1.54 + const TCHAR *CopyrightMessage() { return copyright_str; } 1.55 + const TCHAR *OtherMessage1() { return L"foo1"; } 1.56 + const TCHAR *OtherMessage2() { return L"foo2"; } 1.57 + unsigned int Version() { return VERSION(VER_MAJOR, VER_MINOR); } 1.58 + void ShowAbout(HWND win) { MessageBoxA(win, "Goat3D exporter", "About this plugin", 0); } 1.59 + 1.60 + int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0); 1.61 +}; 1.62 + 1.63 +class GoatAnimExporterStub : public SceneExport { 1.64 +public: 1.65 + IGameScene *igame; 1.66 + 1.67 + int ExtCount() { return 1; } 1.68 + const TCHAR *Ext(int n) { return L"goatanm"; } 1.69 + const TCHAR *LongDesc() { return L"Goat3D Animation file"; } 1.70 + const TCHAR *ShortDesc() { return L"Goat3D Animation"; } 1.71 + const TCHAR *AuthorName() { return L"John Tsiombikas"; } 1.72 + const TCHAR *CopyrightMessage() { return copyright_str; } 1.73 + const TCHAR *OtherMessage1() { return L"bar1"; } 1.74 + const TCHAR *OtherMessage2() { return L"bar2"; } 1.75 + unsigned int Version() { return VERSION(VER_MAJOR, VER_MINOR); } 1.76 + void ShowAbout(HWND win) { MessageBoxA(win, "Goat3D anim exporter", "About this plugin", 0); } 1.77 + 1.78 + int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0); 1.79 +}; 1.80 + 1.81 +static const char *find_dll_dir() 1.82 +{ 1.83 + static char path[MAX_PATH]; 1.84 + 1.85 + HMODULE dll; 1.86 + if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, 1.87 + (LPCSTR)find_dll_dir, &dll)) { 1.88 + return 0; 1.89 + } 1.90 + GetModuleFileNameA(dll, path, sizeof path); 1.91 + 1.92 + char *last_slash = strrchr(path, '\\'); 1.93 + if(last_slash && last_slash[1]) { 1.94 + *last_slash = 0; 1.95 + } 1.96 + 1.97 + return path; 1.98 +} 1.99 + 1.100 +static int do_export(int which, const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL non_int, DWORD opt) 1.101 +{ 1.102 + const char *dll_fname = "maxgoat.dll"; 1.103 + char *dll_path; 1.104 + HMODULE dll = 0; 1.105 + PluginInitFunc init; 1.106 + PluginShutdownFunc shutdown; 1.107 + PluginClassDescFunc get_class_desc; 1.108 + ClassDesc *desc; 1.109 + SceneExport *ex; 1.110 + int result = IMPEXP_FAIL; 1.111 + 1.112 + const char *plugdir = find_dll_dir(); 1.113 + if(plugdir) { 1.114 + dll_path = new char[strlen(dll_fname) + strlen(plugdir) + 2]; 1.115 + sprintf(dll_path, "%s\\%s", plugdir, dll_fname); 1.116 + } else { 1.117 + dll_path = new char[strlen(dll_fname) + 1]; 1.118 + strcpy(dll_path, dll_fname); 1.119 + } 1.120 + 1.121 + if(!(dll = LoadLibraryA(dll_path))) { 1.122 + fprintf(logfile, "failed to load exporter: %s\n", dll_path); 1.123 + goto done; 1.124 + } 1.125 + 1.126 + if(!(get_class_desc = (PluginClassDescFunc)GetProcAddress(dll, "LibClassDesc"))) { 1.127 + fprintf(logfile, "maxgoat.dll is invalid (no LibClassDesc function)\n"); 1.128 + goto done; 1.129 + } 1.130 + 1.131 + // first initialize the library 1.132 + if((init = (PluginInitFunc)GetProcAddress(dll, "LibInitialize"))) { 1.133 + if(!init()) { 1.134 + fprintf(logfile, "exporter initialization failed!\n"); 1.135 + goto done; 1.136 + } 1.137 + } 1.138 + 1.139 + if(!(desc = get_class_desc(which))) { 1.140 + fprintf(logfile, "failed to get the class descriptor\n"); 1.141 + goto done; 1.142 + } 1.143 + 1.144 + if(!(ex = (SceneExport*)desc->Create())) { 1.145 + fprintf(logfile, "failed to create exporter class instance\n"); 1.146 + goto done; 1.147 + } 1.148 + 1.149 + result = ex->DoExport(name, eiface, iface); 1.150 + delete ex; 1.151 + 1.152 + if((shutdown = (PluginShutdownFunc)GetProcAddress(dll, "LibShutdown"))) { 1.153 + shutdown(); 1.154 + } 1.155 + 1.156 +done: 1.157 + delete [] dll_path; 1.158 + if(dll) { 1.159 + FreeLibrary(dll); 1.160 + } 1.161 + return result; 1.162 +} 1.163 + 1.164 +int GoatExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, 1.165 + BOOL non_interactive, DWORD opt) 1.166 +{ 1.167 + return do_export(0, name, eiface, iface, non_interactive, opt); 1.168 +} 1.169 + 1.170 + 1.171 +int GoatAnimExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, 1.172 + BOOL non_interactive, DWORD opt) 1.173 +{ 1.174 + return do_export(1, name, eiface, iface, non_interactive, opt); 1.175 +} 1.176 + 1.177 +// ------------------------------------------ 1.178 + 1.179 +class GoatClassDesc : public ClassDesc2 { 1.180 +public: 1.181 + int IsPublic() { return TRUE; } 1.182 + void *Create(BOOL loading = FALSE) { return new GoatExporterStub; } 1.183 + const TCHAR *ClassName() { return L"GoatExporterStub"; } 1.184 + SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; } 1.185 + Class_ID ClassID() { return Class_ID(0x2e4e6311, 0x2b154d91); } 1.186 + const TCHAR *Category() { return L"Mutant Stargoat"; } 1.187 + 1.188 + const TCHAR *InternalName() { return L"GoatExporterStub"; } 1.189 + HINSTANCE HInstance() { return hinst; } 1.190 +}; 1.191 + 1.192 +class GoatAnimClassDesc : public ClassDesc2 { 1.193 +public: 1.194 + int IsPublic() { return TRUE; } 1.195 + void *Create(BOOL loading = FALSE) { return new GoatAnimExporterStub; } 1.196 + const TCHAR *ClassName() { return L"GoatAnimExporterStub"; } 1.197 + SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; } 1.198 + Class_ID ClassID() { return Class_ID(0x75054666, 0x45487285); } 1.199 + const TCHAR *Category() { return L"Mutant Stargoat"; } 1.200 + 1.201 + const TCHAR *InternalName() { return L"GoatAnimExporterStub"; } 1.202 + HINSTANCE HInstance() { return hinst; } 1.203 +}; 1.204 + 1.205 + 1.206 +static GoatClassDesc class_desc; 1.207 +static GoatAnimClassDesc anim_class_desc; 1.208 + 1.209 +BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved) 1.210 +{ 1.211 + if(reason == DLL_PROCESS_ATTACH) { 1.212 + hinst = inst_handle; 1.213 + DisableThreadLibraryCalls(hinst); 1.214 + } 1.215 + return TRUE; 1.216 +} 1.217 + 1.218 +extern "C" { 1.219 + 1.220 +__declspec(dllexport) const TCHAR *LibDescription() 1.221 +{ 1.222 + return L"Goat3D exporter stub"; 1.223 +} 1.224 + 1.225 +__declspec(dllexport) int LibNumberClasses() 1.226 +{ 1.227 + return 2; 1.228 +} 1.229 + 1.230 +__declspec(dllexport) ClassDesc *LibClassDesc(int i) 1.231 +{ 1.232 + switch(i) { 1.233 + case 0: 1.234 + return &class_desc; 1.235 + case 1: 1.236 + return &anim_class_desc; 1.237 + default: 1.238 + break; 1.239 + } 1.240 + return 0; 1.241 +} 1.242 + 1.243 +__declspec(dllexport) ULONG LibVersion() 1.244 +{ 1.245 + return Get3DSMAXVersion(); 1.246 +} 1.247 + 1.248 +__declspec(dllexport) int LibInitialize() 1.249 +{ 1.250 + static char path[1024]; 1.251 + 1.252 + SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path); 1.253 + strcat(path, "/testexpstub.log"); 1.254 + 1.255 + if((logfile = fopen(path, "w"))) { 1.256 + setvbuf(logfile, 0, _IONBF, 0); 1.257 + } 1.258 + return TRUE; 1.259 +} 1.260 + 1.261 +__declspec(dllexport) int LibShutdown() 1.262 +{ 1.263 + if(logfile) { 1.264 + fclose(logfile); 1.265 + logfile = 0; 1.266 + } 1.267 + return TRUE; 1.268 +} 1.269 + 1.270 +} // extern "C" 1.271 \ No newline at end of file