-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiw.c
171 lines (146 loc) · 4.99 KB
/
aiw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "aiw.h"
#ifdef _WIN32
#include <windows.h>
#elif !defined(__EMSCRIPTEN__)
#include <unistd.h>
#include <dlfcn.h>
#endif
#include "../candle/systems/sauces.h"
C_ENUM aiReturn (*aiwGetMaterialString)(const C_STRUCT aiMaterial* pMat,
const char* pKey,
unsigned int type,
unsigned int index,
C_STRUCT aiString* pOut);
C_ENUM aiReturn (*aiwGetMaterialColor)(const C_STRUCT aiMaterial* pMat,
const char* pKey,
unsigned int type,
unsigned int index,
C_STRUCT aiColor4D* pOut);
unsigned int (*aiwGetMaterialTextureCount)(const C_STRUCT aiMaterial* pMat,
C_ENUM aiTextureType type);
C_ENUM aiReturn (*aiwGetMaterialTexture)(
const C_STRUCT aiMaterial* mat,
C_ENUM aiTextureType type,
unsigned int index,
C_STRUCT aiString* path,
C_ENUM aiTextureMapping* mapping /*= NULL*/,
unsigned int* uvindex /*= NULL*/,
ai_real* blend /*= NULL*/,
C_ENUM aiTextureOp* op /*= NULL*/,
C_ENUM aiTextureMapMode* mapmode /*= NULL*/,
unsigned int* flags /*= NULL*/);
const C_STRUCT aiScene* (*aiwImportFile)(
const char *pFile,
unsigned int pFlags);
const C_STRUCT aiScene* (*aiwImportFileFromMemory)(
const char *pBuffer,
unsigned int pLength,
unsigned int pFlags,
const char *pHint);
void (*aiwReleaseImport)(
const C_STRUCT aiScene *pScene);
void aiw_init(void)
{
#ifdef _WIN32
#define ailib(l) LoadLibrary(l)
#define aisym(v, type, l, s) v = (type)GetProcAddress(l, #s)
#define aiclose(l)
#elif defined(__EMSCRIPTEN__)
#define ailib(l)
#define aisym(v, type, l, s) v = (type)s
#define aiclose(l)
#else
#define ailib(l) dlopen(l, RTLD_NOW)
#define aisym(v, type, l, s) v = (type)dlsym(l, #s)
#define aiclose(l)
#endif
FILE *fp;
#ifdef _WIN32
HINSTANCE ailib;
char lib_filename[MAX_PATH];
strcpy(lib_filename, "assimp.dll");
fp = fopen(lib_filename, "r");
if (fp == NULL)
{
char temp_path[MAX_PATH];
HANDLE fd, file_map;
LPVOID address;
size_t bytes_num;
resource_t *sauce = c_sauces_get_sauce(c_sauces(&SYS), sauce_handle(lib_filename));
char *bytes = c_sauces_get_bytes(c_sauces(&SYS), sauce, &bytes_num);
GetTempPath(MAX_PATH, temp_path);
GetTempFileName(temp_path, TEXT("tempoai"), 0, lib_filename);
fd = CreateFile(lib_filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
file_map = CreateFileMapping(fd, NULL, PAGE_READWRITE, 0, bytes_num, NULL);
address = MapViewOfFile(file_map, FILE_MAP_WRITE, 0, 0, 0);
CopyMemory(address, bytes, bytes_num);
UnmapViewOfFile(address);
CloseHandle(file_map);
CloseHandle(fd);
}
ailib = ailib(lib_filename);
#elif !defined(__EMSCRIPTEN__)
void *ailib;
char lib_filename[PATH_MAX];
strcpy(lib_filename, "libassimp.so");
fp = fopen(lib_filename, "r");
if (fp == NULL)
{
char temp_name[] = "/tmp/XXXXXXX";
int fd = mkstemp(temp_name);
size_t bytes_num;
resource_t *sauce = c_sauces_get_sauce(c_sauces(&SYS), sauce_handle(lib_filename));
char *bytes = c_sauces_get_bytes(c_sauces(&SYS), sauce, &bytes_num);
if (write(fd, bytes, bytes_num) == -1)
{
printf("Failed to write to ai shared library temp file.\n");
exit(1);
}
strcpy(lib_filename, temp_name);
close(fd);
}
ailib = ailib(lib_filename);
if (!ailib) {
puts(dlerror());
exit(1);
}
#else
void *ailib = NULL;
#endif
aisym(aiwGetMaterialString, C_ENUM aiReturn (*)(const C_STRUCT aiMaterial* pMat,
const char* pKey,
unsigned int type,
unsigned int index,
C_STRUCT aiString* pOut),
ailib, aiGetMaterialString);
aisym(aiwGetMaterialColor, C_ENUM aiReturn (*)(const C_STRUCT aiMaterial* pMat,
const char* pKey,
unsigned int type,
unsigned int index,
C_STRUCT aiColor4D* pOut), ailib, aiGetMaterialColor);
aisym(aiwGetMaterialTextureCount, unsigned int (*)(const C_STRUCT aiMaterial* pMat,
C_ENUM aiTextureType type), ailib, aiGetMaterialTextureCount);
aisym(aiwGetMaterialTexture, C_ENUM aiReturn (*)(
const C_STRUCT aiMaterial* mat,
C_ENUM aiTextureType type,
unsigned int index,
C_STRUCT aiString* path,
C_ENUM aiTextureMapping* mapping,
unsigned int* uvindex,
ai_real* blend,
C_ENUM aiTextureOp* op,
C_ENUM aiTextureMapMode* mapmode,
unsigned int* flags), ailib,
aiGetMaterialTexture);
aisym(aiwImportFile, const C_STRUCT aiScene* (*)(const char *pFile,
unsigned int pFlags), ailib, aiImportFile);
aisym(aiwImportFileFromMemory, const C_STRUCT aiScene* (*)(const char *pBuffer,
unsigned int pLength,
unsigned int pFlags,
const char *pHint), ailib, aiImportFileFromMemory);
aisym(aiwReleaseImport, void (*)(const C_STRUCT aiScene *pScene), ailib, aiReleaseImport);
aiclose(ailib);
#undef ailib
#undef aisym
}