goat3d

view exporters/maxgoat_stub/src/stub.cc @ 58:d317eb4f83da

- made everything compile properly on windows again - removed libanim/libvmath, we'll use them as external dependencies - added new maxgoat_stub 3dsmax plugin project. Gets loaded as a max plugin and loads the actual maxgoat (and later maxgoat_anim) exporters on demand, to allow reloading the actual exporters without having to restart 3dsmax (which takes AGES).
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Mar 2014 03:19:55 +0200
parents
children 0c3576325480
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;
41 class GoatExporterStub : public SceneExport {
42 private:
44 public:
45 IGameScene *igame;
47 int ExtCount();
48 const TCHAR *Ext(int n);
49 const TCHAR *LongDesc();
50 const TCHAR *ShortDesc();
51 const TCHAR *AuthorName();
52 const TCHAR *CopyrightMessage();
53 const TCHAR *OtherMessage1();
54 const TCHAR *OtherMessage2();
55 unsigned int Version();
56 void ShowAbout(HWND win);
58 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
59 };
62 int GoatExporterStub::ExtCount()
63 {
64 return 1;
65 }
67 const TCHAR *GoatExporterStub::Ext(int n)
68 {
69 return L"xml";
70 }
72 const TCHAR *GoatExporterStub::LongDesc()
73 {
74 return L"Goat3D scene file";
75 }
77 const TCHAR *GoatExporterStub::ShortDesc()
78 {
79 return L"Goat3D";
80 }
82 const TCHAR *GoatExporterStub::AuthorName()
83 {
84 return L"John Tsiombikas";
85 }
87 const TCHAR *GoatExporterStub::CopyrightMessage()
88 {
89 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
90 }
92 const TCHAR *GoatExporterStub::OtherMessage1()
93 {
94 return L"other1";
95 }
97 const TCHAR *GoatExporterStub::OtherMessage2()
98 {
99 return L"other2";
100 }
102 unsigned int GoatExporterStub::Version()
103 {
104 return VERSION(VER_MAJOR, VER_MINOR);
105 }
107 void GoatExporterStub::ShowAbout(HWND win)
108 {
109 MessageBoxA(win, "Goat3D exporter stub", "About this plugin", 0);
110 }
112 static const char *find_dll_dir()
113 {
114 static char path[MAX_PATH];
116 HMODULE dll;
117 if(!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
118 (LPCSTR)find_dll_dir, &dll)) {
119 return 0;
120 }
121 GetModuleFileNameA(dll, path, sizeof path);
123 char *last_slash = strrchr(path, '\\');
124 if(last_slash && last_slash[1]) {
125 *last_slash = 0;
126 }
128 return path;
129 }
131 /* TODO: open a dialog, let the user select goat3d or goat3danim, then load the correct dll
132 */
133 int GoatExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
134 BOOL non_interactive, DWORD opt)
135 {
136 const char *dll_fname = "maxgoat.dll";
137 char *dll_path;
138 HMODULE dll = 0;
139 PluginInitFunc init;
140 PluginShutdownFunc shutdown;
141 PluginClassDescFunc get_class_desc;
142 ClassDesc *desc;
143 SceneExport *ex;
144 int result = IMPEXP_FAIL;
146 const char *plugdir = find_dll_dir();
147 if(plugdir) {
148 dll_path = new char[strlen(dll_fname) + strlen(plugdir) + 2];
149 sprintf(dll_path, "%s\\%s", plugdir, dll_fname);
150 } else {
151 dll_path = new char[strlen(dll_fname) + 1];
152 strcpy(dll_path, dll_fname);
153 }
155 if(!(dll = LoadLibraryA(dll_path))) {
156 fprintf(logfile, "failed to load exporter: %s\n", dll_path);
157 goto done;
158 }
160 if(!(get_class_desc = (PluginClassDescFunc)GetProcAddress(dll, "LibClassDesc"))) {
161 fprintf(logfile, "maxgoat.dll is invalid (no LibClassDesc function)\n");
162 goto done;
163 }
165 // first initialize the library
166 if((init = (PluginInitFunc)GetProcAddress(dll, "LibInitialize"))) {
167 if(!init()) {
168 fprintf(logfile, "exporter initialization failed!\n");
169 goto done;
170 }
171 }
173 // TODO: pass 1 for anim
174 if(!(desc = get_class_desc(0))) {
175 fprintf(logfile, "failed to get the class descriptor\n");
176 goto done;
177 }
179 if(!(ex = (SceneExport*)desc->Create())) {
180 fprintf(logfile, "failed to create exporter class instance\n");
181 goto done;
182 }
184 result = ex->DoExport(name, eiface, iface);
185 delete ex;
187 if((shutdown = (PluginShutdownFunc)GetProcAddress(dll, "LibShutdown"))) {
188 shutdown();
189 }
191 done:
192 delete [] dll_path;
193 if(dll) {
194 FreeLibrary(dll);
195 }
196 return result;
197 }
200 // ------------------------------------------
202 class GoatClassDesc : public ClassDesc2 {
203 public:
204 int IsPublic() { return TRUE; }
205 void *Create(BOOL loading = FALSE) { return new GoatExporterStub; }
206 const TCHAR *ClassName() { return L"GoatExporterStub"; }
207 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
208 Class_ID ClassID() { return Class_ID(0x2e4e6311, 0x2b154d91); }
209 const TCHAR *Category() { return L"Mutant Stargoat"; }
211 const TCHAR *InternalName() { return L"GoatExporterStub"; }
212 HINSTANCE HInstance() { return hinst; }
213 };
215 static GoatClassDesc class_desc;
217 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
218 {
219 if(reason == DLL_PROCESS_ATTACH) {
220 hinst = inst_handle;
221 DisableThreadLibraryCalls(hinst);
222 }
223 return TRUE;
224 }
226 extern "C" {
228 __declspec(dllexport) const TCHAR *LibDescription()
229 {
230 return L"Goat3D exporter stub";
231 }
233 __declspec(dllexport) int LibNumberClasses()
234 {
235 return 1;
236 }
238 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
239 {
240 return i == 0 ? &class_desc : 0;
241 }
243 __declspec(dllexport) ULONG LibVersion()
244 {
245 return Get3DSMAXVersion();
246 }
248 __declspec(dllexport) int LibInitialize()
249 {
250 static char path[1024];
252 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
253 strcat(path, "/testexpstub.log");
255 if((logfile = fopen(path, "w"))) {
256 setvbuf(logfile, 0, _IONBF, 0);
257 }
258 return TRUE;
259 }
261 __declspec(dllexport) int LibShutdown()
262 {
263 if(logfile) {
264 fclose(logfile);
265 logfile = 0;
266 }
267 return TRUE;
268 }
270 } // extern "C"