Skip to content

Commit c4c6c63

Browse files
committed
Add automatic c binding updater
asardll.c, asardll.h, and interface-lib.h are now generated from their respective .in files and from interface-lib.cpp
1 parent 24860f8 commit c4c6c63

File tree

8 files changed

+725
-258
lines changed

8 files changed

+725
-258
lines changed

src/asar-dll-bindings/c/asardll.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// note for asar devs: autogenerated by update_c_bind.py, don't edit this
2+
// directly! either edit asardll.c.in or interface-lib.cpp.
13
#include <stdbool.h>
24
#include <stddef.h>
35

@@ -108,42 +110,51 @@ inline static bool setfunction(void* target, FARPROC fn)
108110

109111
#include "asardll.h"
110112

111-
#undef asarfunc
112-
#undef ASAR_DLL_H_INCLUDED
113-
114-
#define asarfunc
115-
#include "asardll.h"
116-
117113
static void * asardll=NULL;
118114

119115
static bool (*asar_i_init)(void);
120116
static void(*asar_i_close)(void);
117+
int (*asar_version)(void);
118+
int (*asar_apiversion)(void);
119+
bool (*asar_reset)(void);
120+
bool (*asar_patch)(const struct patchparams *params);
121+
int (*asar_maxromsize)(void);
122+
const struct errordata * (*asar_geterrors)(int * count);
123+
const struct errordata * (*asar_getwarnings)(int * count);
124+
const char * const * (*asar_getprints)(int * count);
125+
const struct labeldata * (*asar_getalllabels)(int * count);
126+
int (*asar_getlabelval)(const char * name);
127+
const char * (*asar_getdefine)(const char * name);
128+
const char * (*asar_resolvedefines)(const char * data);
129+
const struct definedata * (*asar_getalldefines)(int * count);
130+
double (*asar_math)(const char * math_, const char ** error);
131+
const struct writtenblockdata * (*asar_getwrittenblocks)(int * count);
132+
enum mappertype (*asar_getmapper)(void);
133+
const char * (*asar_getsymbolsfile)(const char* type);
121134

122135
#define require(b) if (!(b)) { asardll=NULL; return false; }
123-
#define loadi(name) loadraw("asar_"#name, asar_i_##name)
124-
#define load(name) loadraw("asar_"#name, asar_##name)
125136

126137
static bool asar_init_shared(void)
127138
{
128-
loadi(init);
129-
loadi(close);
130-
load(version);
131-
load(apiversion);
132-
load(reset);
133-
load(patch);
134-
load(maxromsize);
135-
load(geterrors);
136-
load(getwarnings);
137-
load(getprints);
138-
load(getalllabels);
139-
load(getlabelval);
140-
load(getdefine);
141-
load(getalldefines);
142-
load(resolvedefines);
143-
load(math);
144-
load(getwrittenblocks);
145-
load(getmapper);
146-
load(getsymbolsfile);
139+
loadraw("asar_init", asar_i_init);
140+
loadraw("asar_close", asar_i_close);
141+
loadraw("asar_version", asar_version);
142+
loadraw("asar_apiversion", asar_apiversion);
143+
loadraw("asar_reset", asar_reset);
144+
loadraw("asar_patch", asar_patch);
145+
loadraw("asar_maxromsize", asar_maxromsize);
146+
loadraw("asar_geterrors", asar_geterrors);
147+
loadraw("asar_getwarnings", asar_getwarnings);
148+
loadraw("asar_getprints", asar_getprints);
149+
loadraw("asar_getalllabels", asar_getalllabels);
150+
loadraw("asar_getlabelval", asar_getlabelval);
151+
loadraw("asar_getdefine", asar_getdefine);
152+
loadraw("asar_resolvedefines", asar_resolvedefines);
153+
loadraw("asar_getalldefines", asar_getalldefines);
154+
loadraw("asar_math", asar_math);
155+
loadraw("asar_getwrittenblocks", asar_getwrittenblocks);
156+
loadraw("asar_getmapper", asar_getmapper);
157+
loadraw("asar_getsymbolsfile", asar_getsymbolsfile);
147158
if (asar_apiversion() < expectedapiversion || (asar_apiversion() / 100) > (expectedapiversion / 100)) return false;
148159
require(asar_i_init());
149160
return true;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <stdbool.h>
2+
#include <stddef.h>
3+
4+
#if defined(_WIN32)
5+
# if defined(_MSC_VER)
6+
# pragma warning(push)
7+
# pragma warning(disable : 4255)
8+
# pragma warning(disable : 4668)
9+
# endif
10+
11+
# include <windows.h>
12+
# include <stdio.h>
13+
14+
# if defined(_MSC_VER)
15+
# pragma warning(pop)
16+
# endif
17+
18+
inline static void * getlib(void)
19+
{
20+
void * ret_val = LoadLibraryW(L"asar.dll");
21+
22+
if (ret_val == NULL)
23+
{
24+
// TODO: Add a better method of error checking? This won't do much for people who are using
25+
// Asar with a GUI application, they probably won't see this error output.
26+
char buf[1024];
27+
sprintf(buf, "Failed to load Asar DLL! HRESULT: 0x%08x\n", (unsigned int)HRESULT_FROM_WIN32(GetLastError()));
28+
printf("%s", buf);
29+
OutputDebugStringA(buf);
30+
}
31+
32+
return ret_val;
33+
}
34+
35+
inline static void * getlibfrompath(const char * path)
36+
{
37+
int required_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
38+
if (required_size <= 0) return NULL;
39+
40+
wchar_t* path_buf = (wchar_t*)malloc((size_t)required_size * sizeof(wchar_t));
41+
if (path_buf == NULL) return NULL;
42+
43+
int converted_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, path_buf, required_size);
44+
45+
if (converted_size == 0)
46+
{
47+
return NULL;
48+
}
49+
50+
void * ret_val = LoadLibraryW(path_buf);
51+
free(path_buf);
52+
53+
if (ret_val == NULL)
54+
{
55+
// TODO: Add a better method of error checking? This won't do much for people who are using
56+
// Asar with a GUI application, they probably won't see this error output.
57+
char buf[1024];
58+
sprintf(buf, "Failed to load Asar DLL! HRESULT: 0x%08x\n", (unsigned int)HRESULT_FROM_WIN32(GetLastError()));
59+
printf("%s", buf);
60+
OutputDebugStringA(buf);
61+
}
62+
63+
return ret_val;
64+
}
65+
66+
inline static bool setfunction(void* target, FARPROC fn)
67+
{
68+
memcpy(target, &fn, sizeof(fn));
69+
return fn;
70+
}
71+
# define loadraw(name, target) require(setfunction(&target, GetProcAddress((HINSTANCE)asardll, name)))
72+
# define closelib(var) FreeLibrary((HINSTANCE)var)
73+
#else
74+
# include <dlfcn.h>
75+
# include <stdio.h>
76+
77+
# ifdef __APPLE__
78+
# define EXTENSION ".dylib"
79+
# else
80+
# define EXTENSION ".so"
81+
# endif
82+
83+
inline static void * getlib(void)
84+
{
85+
const char * names[]={"./libasar"EXTENSION, "libasar", NULL};
86+
for (int i=0;names[i];i++)
87+
{
88+
void * rval=dlopen(names[i], RTLD_LAZY);
89+
const char*e=dlerror();
90+
if(e)puts(e);
91+
if (rval) return rval;
92+
}
93+
return NULL;
94+
}
95+
96+
inline static void * getlibfrompath(const char * path)
97+
{
98+
void * rval = dlopen(path, RTLD_LAZY);
99+
const char*e = dlerror();
100+
if (e)puts(e);
101+
if (rval) return rval;
102+
return NULL;
103+
}
104+
105+
# define loadraw(name, target) *(void **)(&target)=dlsym(asardll, name); require(target)
106+
# define closelib(var) dlclose(var)
107+
#endif
108+
109+
#include "asardll.h"
110+
111+
static void * asardll=NULL;
112+
113+
static bool (*asar_i_init)(void);
114+
static void(*asar_i_close)(void);
115+
$FUNCTIONPROTOS$
116+
117+
#define require(b) if (!(b)) { asardll=NULL; return false; }
118+
119+
static bool asar_init_shared(void)
120+
{
121+
loadraw("asar_init", asar_i_init);
122+
loadraw("asar_close", asar_i_close);
123+
$FUNCTIONLOADS$
124+
if (asar_apiversion() < expectedapiversion || (asar_apiversion() / 100) > (expectedapiversion / 100)) return false;
125+
require(asar_i_init());
126+
return true;
127+
}
128+
129+
bool asar_init(void)
130+
{
131+
if (asardll) return true;
132+
asardll=getlib();
133+
require(asardll);
134+
if (!asar_init_shared()) return false;
135+
return true;
136+
}
137+
138+
bool asar_init_with_dll_path(const char * dllpath)
139+
{
140+
if (asardll) return true;
141+
asardll = getlibfrompath(dllpath);
142+
require(asardll);
143+
if (!asar_init_shared()) return false;
144+
return true;
145+
}
146+
147+
void asar_close(void)
148+
{
149+
if (!asardll) return;
150+
asar_i_close();
151+
closelib(asardll);
152+
asardll=NULL;
153+
}

0 commit comments

Comments
 (0)