goat3d

view exporters/maxgoat_stub/src/stub.cc @ 66:3751aabbc5b3

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