goat3d

view exporters/maxgoat_stub/src/stub.cc @ 61:fdece14403ff

max gui
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 31 Mar 2014 09:41:47 +0300
parents d317eb4f83da
children 3751aabbc5b3
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 result = ex->DoExport(name, eiface, iface);
147 delete ex;
149 if((shutdown = (PluginShutdownFunc)GetProcAddress(dll, "LibShutdown"))) {
150 shutdown();
151 }
153 done:
154 delete [] dll_path;
155 if(dll) {
156 FreeLibrary(dll);
157 }
158 return result;
159 }
161 int GoatExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
162 BOOL non_interactive, DWORD opt)
163 {
164 return do_export(0, name, eiface, iface, non_interactive, opt);
165 }
168 int GoatAnimExporterStub::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
169 BOOL non_interactive, DWORD opt)
170 {
171 return do_export(1, name, eiface, iface, non_interactive, opt);
172 }
174 // ------------------------------------------
176 class GoatClassDesc : public ClassDesc2 {
177 public:
178 int IsPublic() { return TRUE; }
179 void *Create(BOOL loading = FALSE) { return new GoatExporterStub; }
180 const TCHAR *ClassName() { return L"GoatExporterStub"; }
181 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
182 Class_ID ClassID() { return Class_ID(0x2e4e6311, 0x2b154d91); }
183 const TCHAR *Category() { return L"Mutant Stargoat"; }
185 const TCHAR *InternalName() { return L"GoatExporterStub"; }
186 HINSTANCE HInstance() { return hinst; }
187 };
189 class GoatAnimClassDesc : public ClassDesc2 {
190 public:
191 int IsPublic() { return TRUE; }
192 void *Create(BOOL loading = FALSE) { return new GoatAnimExporterStub; }
193 const TCHAR *ClassName() { return L"GoatAnimExporterStub"; }
194 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
195 Class_ID ClassID() { return Class_ID(0x75054666, 0x45487285); }
196 const TCHAR *Category() { return L"Mutant Stargoat"; }
198 const TCHAR *InternalName() { return L"GoatAnimExporterStub"; }
199 HINSTANCE HInstance() { return hinst; }
200 };
203 static GoatClassDesc class_desc;
204 static GoatAnimClassDesc anim_class_desc;
206 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
207 {
208 if(reason == DLL_PROCESS_ATTACH) {
209 hinst = inst_handle;
210 DisableThreadLibraryCalls(hinst);
211 }
212 return TRUE;
213 }
215 extern "C" {
217 __declspec(dllexport) const TCHAR *LibDescription()
218 {
219 return L"Goat3D exporter stub";
220 }
222 __declspec(dllexport) int LibNumberClasses()
223 {
224 return 2;
225 }
227 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
228 {
229 switch(i) {
230 case 0:
231 return &class_desc;
232 case 1:
233 return &anim_class_desc;
234 default:
235 break;
236 }
237 return 0;
238 }
240 __declspec(dllexport) ULONG LibVersion()
241 {
242 return Get3DSMAXVersion();
243 }
245 __declspec(dllexport) int LibInitialize()
246 {
247 static char path[1024];
249 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
250 strcat(path, "/testexpstub.log");
252 if((logfile = fopen(path, "w"))) {
253 setvbuf(logfile, 0, _IONBF, 0);
254 }
255 return TRUE;
256 }
258 __declspec(dllexport) int LibShutdown()
259 {
260 if(logfile) {
261 fclose(logfile);
262 logfile = 0;
263 }
264 return TRUE;
265 }
267 } // extern "C"