goat3d

annotate exporters/maxgoat_stub/src/stub.cc @ 60:0c3576325480

moving the exporter along slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Mar 2014 08:53:33 +0300
parents d317eb4f83da
children 3751aabbc5b3
rev   line source
nuclear@58 1 #include <stdio.h>
nuclear@58 2 #include <string.h>
nuclear@58 3 #include <stdlib.h>
nuclear@58 4 #include <errno.h>
nuclear@58 5 #include <map>
nuclear@58 6 #include <windows.h>
nuclear@58 7 #include <shlobj.h>
nuclear@58 8 #include "max.h"
nuclear@58 9 #include "impexp.h" // SceneExport
nuclear@58 10 #include "iparamb2.h" // ClassDesc2
nuclear@58 11 #include "plugapi.h"
nuclear@58 12 #include "IGame.h"
nuclear@58 13 #include "IGameExport.h"
nuclear@58 14 #include "IConversionmanager.h"
nuclear@58 15
nuclear@58 16
nuclear@58 17 #pragma comment (lib, "core.lib")
nuclear@58 18 #pragma comment (lib, "geom.lib")
nuclear@58 19 #pragma comment (lib, "gfx.lib")
nuclear@58 20 #pragma comment (lib, "mesh.lib")
nuclear@58 21 #pragma comment (lib, "maxutil.lib")
nuclear@58 22 #pragma comment (lib, "maxscrpt.lib")
nuclear@58 23 #pragma comment (lib, "paramblk2.lib")
nuclear@58 24 #pragma comment (lib, "msxml2.lib")
nuclear@58 25 #pragma comment (lib, "igame.lib")
nuclear@58 26 #pragma comment (lib, "comctl32.lib")
nuclear@58 27
nuclear@58 28
nuclear@58 29 #define VER_MAJOR 1
nuclear@58 30 #define VER_MINOR 0
nuclear@58 31 #define VERSION(major, minor) \
nuclear@58 32 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
nuclear@58 33
nuclear@58 34 typedef int (*PluginInitFunc)();
nuclear@58 35 typedef int (*PluginShutdownFunc)();
nuclear@58 36 typedef ClassDesc *(*PluginClassDescFunc)(int);
nuclear@58 37
nuclear@58 38 static FILE *logfile;
nuclear@58 39 static HINSTANCE hinst;
nuclear@60 40 static const wchar_t *copyright_str = L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
nuclear@58 41
nuclear@58 42 class GoatExporterStub : public SceneExport {
nuclear@58 43 public:
nuclear@58 44 IGameScene *igame;
nuclear@58 45
nuclear@60 46 int ExtCount() { return 1; }
nuclear@60 47 const TCHAR *Ext(int n) { return L"goatsce"; }
nuclear@60 48 const TCHAR *LongDesc() { return L"Goat3D Scene file"; }
nuclear@60 49 const TCHAR *ShortDesc() { return L"Goat3D Scene"; }
nuclear@60 50 const TCHAR *AuthorName() { return L"John Tsiombikas"; }
nuclear@60 51 const TCHAR *CopyrightMessage() { return copyright_str; }
nuclear@60 52 const TCHAR *OtherMessage1() { return L"foo1"; }
nuclear@60 53 const TCHAR *OtherMessage2() { return L"foo2"; }
nuclear@60 54 unsigned int Version() { return VERSION(VER_MAJOR, VER_MINOR); }
nuclear@60 55 void ShowAbout(HWND win) { MessageBoxA(win, "Goat3D exporter", "About this plugin", 0); }
nuclear@58 56
nuclear@58 57 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
nuclear@58 58 };
nuclear@58 59
nuclear@60 60 class GoatAnimExporterStub : public SceneExport {
nuclear@60 61 public:
nuclear@60 62 IGameScene *igame;
nuclear@58 63
nuclear@60 64 int ExtCount() { return 1; }
nuclear@60 65 const TCHAR *Ext(int n) { return L"goatanm"; }
nuclear@60 66 const TCHAR *LongDesc() { return L"Goat3D Animation file"; }
nuclear@60 67 const TCHAR *ShortDesc() { return L"Goat3D Animation"; }
nuclear@60 68 const TCHAR *AuthorName() { return L"John Tsiombikas"; }
nuclear@60 69 const TCHAR *CopyrightMessage() { return copyright_str; }
nuclear@60 70 const TCHAR *OtherMessage1() { return L"bar1"; }
nuclear@60 71 const TCHAR *OtherMessage2() { return L"bar2"; }
nuclear@60 72 unsigned int Version() { return VERSION(VER_MAJOR, VER_MINOR); }
nuclear@60 73 void ShowAbout(HWND win) { MessageBoxA(win, "Goat3D anim exporter", "About this plugin", 0); }
nuclear@58 74
nuclear@60 75 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
nuclear@60 76 };
nuclear@58 77
nuclear@58 78 static const char *find_dll_dir()
nuclear@58 79 {
nuclear@58 80 static char path[MAX_PATH];
nuclear@58 81
nuclear@58 82 HMODULE dll;
nuclear@58 83 if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
nuclear@58 84 (LPCSTR)find_dll_dir, &dll)) {
nuclear@58 85 return 0;
nuclear@58 86 }
nuclear@58 87 GetModuleFileNameA(dll, path, sizeof path);
nuclear@58 88
nuclear@58 89 char *last_slash = strrchr(path, '\\');
nuclear@58 90 if(last_slash && last_slash[1]) {
nuclear@58 91 *last_slash = 0;
nuclear@58 92 }
nuclear@58 93
nuclear@58 94 return path;
nuclear@58 95 }
nuclear@58 96
nuclear@60 97 static int do_export(int which, const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL non_int, DWORD opt)
nuclear@58 98 {
nuclear@58 99 const char *dll_fname = "maxgoat.dll";
nuclear@58 100 char *dll_path;
nuclear@58 101 HMODULE dll = 0;
nuclear@58 102 PluginInitFunc init;
nuclear@58 103 PluginShutdownFunc shutdown;
nuclear@58 104 PluginClassDescFunc get_class_desc;
nuclear@58 105 ClassDesc *desc;
nuclear@58 106 SceneExport *ex;
nuclear@58 107 int result = IMPEXP_FAIL;
nuclear@58 108
nuclear@58 109 const char *plugdir = find_dll_dir();
nuclear@58 110 if(plugdir) {
nuclear@58 111 dll_path = new char[strlen(dll_fname) + strlen(plugdir) + 2];
nuclear@58 112 sprintf(dll_path, "%s\\%s", plugdir, dll_fname);
nuclear@58 113 } else {
nuclear@58 114 dll_path = new char[strlen(dll_fname) + 1];
nuclear@58 115 strcpy(dll_path, dll_fname);
nuclear@58 116 }
nuclear@58 117
nuclear@58 118 if(!(dll = LoadLibraryA(dll_path))) {
nuclear@58 119 fprintf(logfile, "failed to load exporter: %s\n", dll_path);
nuclear@58 120 goto done;
nuclear@58 121 }
nuclear@58 122
nuclear@58 123 if(!(get_class_desc = (PluginClassDescFunc)GetProcAddress(dll, "LibClassDesc"))) {
nuclear@58 124 fprintf(logfile, "maxgoat.dll is invalid (no LibClassDesc function)\n");
nuclear@58 125 goto done;
nuclear@58 126 }
nuclear@58 127
nuclear@58 128 // first initialize the library
nuclear@58 129 if((init = (PluginInitFunc)GetProcAddress(dll, "LibInitialize"))) {
nuclear@58 130 if(!init()) {
nuclear@58 131 fprintf(logfile, "exporter initialization failed!\n");
nuclear@58 132 goto done;
nuclear@58 133 }
nuclear@58 134 }
nuclear@58 135
nuclear@60 136 if(!(desc = get_class_desc(which))) {
nuclear@58 137 fprintf(logfile, "failed to get the class descriptor\n");
nuclear@58 138 goto done;
nuclear@58 139 }
nuclear@58 140
nuclear@58 141 if(!(ex = (SceneExport*)desc->Create())) {
nuclear@58 142 fprintf(logfile, "failed to create exporter class instance\n");
nuclear@58 143 goto done;
nuclear@58 144 }
nuclear@58 145
nuclear@58 146 result = ex->DoExport(name, eiface, iface);
nuclear@58 147 delete ex;
nuclear@58 148
nuclear@58 149 if((shutdown = (PluginShutdownFunc)GetProcAddress(dll, "LibShutdown"))) {
nuclear@58 150 shutdown();
nuclear@58 151 }
nuclear@58 152
nuclear@58 153 done:
nuclear@58 154 delete [] dll_path;
nuclear@58 155 if(dll) {
nuclear@58 156 FreeLibrary(dll);
nuclear@58 157 }
nuclear@58 158 return result;
nuclear@58 159 }
nuclear@58 160
nuclear@60 161 int GoatExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
nuclear@60 162 BOOL non_interactive, DWORD opt)
nuclear@60 163 {
nuclear@60 164 return do_export(0, name, eiface, iface, non_interactive, opt);
nuclear@60 165 }
nuclear@60 166
nuclear@60 167
nuclear@60 168 int GoatAnimExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
nuclear@60 169 BOOL non_interactive, DWORD opt)
nuclear@60 170 {
nuclear@60 171 return do_export(1, name, eiface, iface, non_interactive, opt);
nuclear@60 172 }
nuclear@58 173
nuclear@58 174 // ------------------------------------------
nuclear@58 175
nuclear@58 176 class GoatClassDesc : public ClassDesc2 {
nuclear@58 177 public:
nuclear@58 178 int IsPublic() { return TRUE; }
nuclear@58 179 void *Create(BOOL loading = FALSE) { return new GoatExporterStub; }
nuclear@58 180 const TCHAR *ClassName() { return L"GoatExporterStub"; }
nuclear@58 181 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
nuclear@58 182 Class_ID ClassID() { return Class_ID(0x2e4e6311, 0x2b154d91); }
nuclear@58 183 const TCHAR *Category() { return L"Mutant Stargoat"; }
nuclear@58 184
nuclear@58 185 const TCHAR *InternalName() { return L"GoatExporterStub"; }
nuclear@58 186 HINSTANCE HInstance() { return hinst; }
nuclear@58 187 };
nuclear@58 188
nuclear@60 189 class GoatAnimClassDesc : public ClassDesc2 {
nuclear@60 190 public:
nuclear@60 191 int IsPublic() { return TRUE; }
nuclear@60 192 void *Create(BOOL loading = FALSE) { return new GoatAnimExporterStub; }
nuclear@60 193 const TCHAR *ClassName() { return L"GoatAnimExporterStub"; }
nuclear@60 194 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
nuclear@60 195 Class_ID ClassID() { return Class_ID(0x75054666, 0x45487285); }
nuclear@60 196 const TCHAR *Category() { return L"Mutant Stargoat"; }
nuclear@60 197
nuclear@60 198 const TCHAR *InternalName() { return L"GoatAnimExporterStub"; }
nuclear@60 199 HINSTANCE HInstance() { return hinst; }
nuclear@60 200 };
nuclear@60 201
nuclear@60 202
nuclear@58 203 static GoatClassDesc class_desc;
nuclear@60 204 static GoatAnimClassDesc anim_class_desc;
nuclear@58 205
nuclear@58 206 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
nuclear@58 207 {
nuclear@58 208 if(reason == DLL_PROCESS_ATTACH) {
nuclear@58 209 hinst = inst_handle;
nuclear@58 210 DisableThreadLibraryCalls(hinst);
nuclear@58 211 }
nuclear@58 212 return TRUE;
nuclear@58 213 }
nuclear@58 214
nuclear@58 215 extern "C" {
nuclear@58 216
nuclear@58 217 __declspec(dllexport) const TCHAR *LibDescription()
nuclear@58 218 {
nuclear@58 219 return L"Goat3D exporter stub";
nuclear@58 220 }
nuclear@58 221
nuclear@58 222 __declspec(dllexport) int LibNumberClasses()
nuclear@58 223 {
nuclear@60 224 return 2;
nuclear@58 225 }
nuclear@58 226
nuclear@58 227 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
nuclear@58 228 {
nuclear@60 229 switch(i) {
nuclear@60 230 case 0:
nuclear@60 231 return &class_desc;
nuclear@60 232 case 1:
nuclear@60 233 return &anim_class_desc;
nuclear@60 234 default:
nuclear@60 235 break;
nuclear@60 236 }
nuclear@60 237 return 0;
nuclear@58 238 }
nuclear@58 239
nuclear@58 240 __declspec(dllexport) ULONG LibVersion()
nuclear@58 241 {
nuclear@58 242 return Get3DSMAXVersion();
nuclear@58 243 }
nuclear@58 244
nuclear@58 245 __declspec(dllexport) int LibInitialize()
nuclear@58 246 {
nuclear@58 247 static char path[1024];
nuclear@58 248
nuclear@58 249 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
nuclear@58 250 strcat(path, "/testexpstub.log");
nuclear@58 251
nuclear@58 252 if((logfile = fopen(path, "w"))) {
nuclear@58 253 setvbuf(logfile, 0, _IONBF, 0);
nuclear@58 254 }
nuclear@58 255 return TRUE;
nuclear@58 256 }
nuclear@58 257
nuclear@58 258 __declspec(dllexport) int LibShutdown()
nuclear@58 259 {
nuclear@58 260 if(logfile) {
nuclear@58 261 fclose(logfile);
nuclear@58 262 logfile = 0;
nuclear@58 263 }
nuclear@58 264 return TRUE;
nuclear@58 265 }
nuclear@58 266
nuclear@58 267 } // extern "C"