| 1 |
#cxx . . $ `<file>/dokidoki-support/minlua.c`/include <libgen.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <unistd.h> |
| •6 |
#include <errno.h> |
| 7 |
|
| 8 |
#include <lua.h> |
| 9 |
#include <lauxlib.h> |
| 10 |
#include <lualib.h> |
| 11 |
|
| 12 |
#include "log.h"cxx . . $ `<file>/dokidoki-support/log.h`/ |
| 13 |
|
| 14 |
//// preloaders /////////////////////////////////////////////////////////////// |
| 15 |
// to statically link in binary lua modules: |
| 16 |
// |
| 17 |
// - use the right linker options (or just include the .a in the link line) |
| 18 |
// - add them to init_preloaders right below |
| 19 |
// |
| 20 |
// You don't need to manually declare them or include anything, the |
| 21 |
// REGISTER_LOADER macro handles that. |
| 22 |
|
| 23 |
#define REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!(module_name, loader) \ |
| 24 |
int loader(lua_State *L); \ |
| 25 |
lua_pushcfunctioncxx . . $ `<external>/15848857005865937740/lua.h:260:9`!(L, loader); \ |
| 26 |
lua_setfield(L, -2, module_name) |
| 27 |
|
| 28 |
void init_preloaderscxx . . $ init_preloaders(f6fa9609f70d70d8).(lua_Statecxx . . $ lua_State# *L) |
| 29 |
{ |
| 30 |
lua_getglobalcxx . . $ `<external>/15848857005865937740/lua.h:277:9`!(L, "package"); |
| 31 |
lua_getfieldcxx . . $ lua_getfield(5a7f0bea59244d1d).(L, -1, "preload"); |
| 32 |
|
| 33 |
// add your custom loaders here, they look like this: |
| 34 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("collision.native", luaopen_collision_native); |
| 35 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("gl", luaopen_gl); |
| 36 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("glfw", luaopen_glfw); |
| 37 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("glu", luaopen_glu); |
| 38 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("log", luaopen_log); |
| 39 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("memarray", luaopen_memarray); |
| 40 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("mixer", luaopen_mixer); |
| 41 |
REGISTER_LOADERcxx . . $ `dokidoki-support/minlua.c:23:9`!("stb_image", luaopen_stb_image); |
| 42 |
|
| 43 |
// each night I pray that I might one day happen upon a less disgusting way |
| 44 |
// of doing this |
| 45 |
#ifdef EXTRA_LOADERScxx . . $ `<built-in>:417:9`! |
| 46 |
#include EXTRA_LOADERScxx . . $ `<built-in>:417:9`! |
| 47 |
#endif |
| 48 |
|
| 49 |
lua_popcxx . . $ `<external>/15848857005865937740/lua.h:254:9`!(L, 2); |
| 50 |
} |
| 51 |
|
| 52 |
//// working directory handling /////////////////////////////////////////////// |
| 53 |
|
| 54 |
#if defined(DOKIDOKI_LINUXcxx . . $ `<built-in>:416:9`!) |
| 55 |
int get_exe_pathcxx . . $ get_exe_path(6abd07ddb1a912f0).(char *buffer, int size) |
| 56 |
{ |
| 57 |
int ret = readlinkcxx . . $ readlink(373c9bd46766ee5).("/proc/self/exe", buffer, size); |
| 58 |
if(0 >= ret && ret < size) |
| 59 |
buffer[ret] = 0; |
| 60 |
return ret; |
| 61 |
} |
| 62 |
#elif defined(DOKIDOKI_MINGW) |
| 63 |
#include <windows.h> |
| 64 |
int get_exe_path(char *buffer, int size) |
| 65 |
{ |
| 66 |
return GetModuleFileName(NULL, buffer, size); |
| 67 |
} |
| 68 |
#elif defined(DOKIDOKI_MACOSX) |
| 69 |
#include <CFBundle.h> |
| 70 |
#include <CFURL.h> |
| 71 |
int switch_to_game_directory() |
| 72 |
{ |
| 73 |
log_message("finding the bundle directory"); |
| 74 |
|
| 75 |
CFBundleRef bundle = CFBundleGetMainBundle(); |
| 76 |
CFURLRef url = NULL; |
| 77 |
if(bundle) |
| 78 |
url = CFBundleCopyBundleURL(bundle); |
| 79 |
if(!url) |
| 80 |
{ |
| 81 |
log_message("no bundle found"); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
CFStringRef str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); |
| 86 |
CFRelease(url); |
| 87 |
|
| 88 |
int size = CFStringGetLength(str) + 1; |
| 89 |
char *dir = (char *)malloc(size); |
| 90 |
|
| 91 |
int success = CFStringGetCString(str, dir, size, kCFURLPOSIXPathStyle); |
| 92 |
CFRelease(str); |
| 93 |
|
| 94 |
if(success) |
| 95 |
{ |
| 96 |
int success = chdir(dir); |
| 97 |
if(success == 0) |
| 98 |
log_messagef("changed directory to '%s'", dir); |
| 99 |
else |
| 100 |
log_messagef("failed changing directory to '%s'", dir); |
| 101 |
|
| 102 |
// there must be a better way to tell if we're bundled :/ |
| 103 |
if(chdir("Contents/Resources") == 0) |
| 104 |
log_message("running as an app bundle, changed to resources directory"); |
| 105 |
else |
| 106 |
log_message("running unbundled"); |
| 107 |
} |
| 108 |
|
| 109 |
free(dir); |
| 110 |
return success; |
| 111 |
} |
| 112 |
#else |
| 113 |
int switch_to_game_directory() |
| 114 |
{ |
| 115 |
log_message("automatic game path detection not supported on this platform"); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
#endif |
| 119 |
|
| 120 |
#if defined(DOKIDOKI_MINGW) || defined(DOKIDOKI_LINUXcxx . . $ `<built-in>:416:9`!) |
| 121 |
int switch_to_game_directorycxx . . $ switch_to_game_directory(d1b40a211a1a4e4c).() |
| 122 |
{ |
| 123 |
log_messagecxx . . $ log_message(85c52e162fed56f9).("finding the game's directory"); |
| 124 |
int size = 256; |
| 125 |
char *buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 126 |
while(!buffer) |
| 127 |
{ |
| 128 |
buffer = (char *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size); |
| 129 |
int ret = get_exe_pathcxx . . $ get_exe_path(6abd07ddb1a912f0).(buffer, size); |
| 130 |
|
| 131 |
// error |
| 132 |
if(ret <= 0) |
| 133 |
{ |
| 134 |
freecxx . . $ free(5d22bdacc48458e8).(buffer); |
| 135 |
buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 136 |
log_messagecxx . . $ log_message(85c52e162fed56f9).("failed locating the executable"); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
// buffer too small |
| 140 |
else if(ret == size) |
| 141 |
{ |
| 142 |
freecxx . . $ free(5d22bdacc48458e8).(buffer); |
| 143 |
buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 144 |
size *= 2; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
char *dir = dirnamecxx . . $ dirname(7a9722d75b371431).(buffer); |
| 149 |
|
| 150 |
int success = chdircxx . . $ chdir(5c4306d8553b3696).(dir); |
| 151 |
if(success == 0) |
| 152 |
log_messagefcxx . . $ log_messagef(326aeb464d16410c).("changed directory to '%s'", dir); |
| 153 |
else |
| 154 |
log_messagefcxx . . $ log_messagef(326aeb464d16410c).("failed to change directory to '%s'", dir); |
| 155 |
|
| 156 |
freecxx . . $ free(5d22bdacc48458e8).(buffer); |
| 157 |
return success; |
| 158 |
} |
| 159 |
#endif |
| 160 |
|
| 161 |
//// main program ///////////////////////////////////////////////////////////// |
| 162 |
|
| 163 |
int maincxx . . $ main(afcd3168fc1188a4).(int argc, char ** argv) |
| 164 |
{ |
| 165 |
int i; |
| 166 |
|
| 167 |
int use_logfile = 1; |
| 168 |
for(i = 0; i < argc; i++) |
| 169 |
if(strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "--stderr") == 0) |
| 170 |
use_logfile = 0; |
| 171 |
log_initcxx . . $ log_init(d4f767463ce0a6b3).(use_logfile); |
| 172 |
|
| •173 |
// switch_to_game_directory(); |
| 174 |
char *dir = "/usr/share/pax-britannica"; |
| 175 |
log_messagefcxx . . $ log_messagef(326aeb464d16410c).("changing directory to '%s'", dir); |
| 176 |
int success = chdircxx . . $ chdir(5c4306d8553b3696).(dir); |
| 177 |
if(success == -1) |
| 178 |
perrorcxx . . $ perror(85c52e162fed56f9).("chdir()"); |
| 179 |
|
| 180 |
// load lua and libraries |
| 181 |
lua_Statecxx . . $ lua_State# *L = lua_opencxx . . $ `<external>/15848857005865937740/lua.h:287:9`!(); |
| 182 |
luaL_openlibscxx . . $ luaL_openlibs(f6fa9609f70d70d8).(L); |
| 183 |
init_preloaderscxx . . $ init_preloaders(f6fa9609f70d70d8).(L); |
| 184 |
|
| 185 |
// load arguments (and pre-arguments) into a global 'arg' table |
| 186 |
lua_newtablecxx . . $ `<external>/15848857005865937740/lua.h:256:9`!(L); |
| 187 |
for(i = 0; i < argc; i++) |
| 188 |
{ |
| 189 |
lua_pushstringcxx . . $ lua_pushstring(2c01a949aef328b).(L, argv[i]); |
| 190 |
lua_rawseticxx . . $ lua_rawseti(2c6648a7e86590d).(L, -2, i); |
| 191 |
} |
| 192 |
lua_setglobalcxx . . $ `<external>/15848857005865937740/lua.h:276:9`!(L, "arg"); |
| 193 |
|
| 194 |
// open app, with error reporting |
| 195 |
lua_getglobalcxx . . $ `<external>/15848857005865937740/lua.h:277:9`!(L, "debug"); |
| 196 |
lua_getfieldcxx . . $ lua_getfield(5a7f0bea59244d1d).(L, -1, "traceback"); |
| 197 |
|
| 198 |
int error = luaL_dofilecxx . . $ `<external>/18095755238171912061/lauxlib.h:111:9`!(L, "init.lua"); |
| 199 |
|
| 200 |
if(!error) |
| 201 |
{ |
| 202 |
// load command-line arguments as function arguments |
| 203 |
lua_checkstackcxx . . $ lua_checkstack(95e3aa3a3b4bb934).(L, argc - 1); |
| 204 |
for(i = 1; i <= argc - 1; i++) |
| 205 |
lua_pushstringcxx . . $ lua_pushstring(2c01a949aef328b).(L, argv[i]); |
| 206 |
error = lua_pcallcxx . . $ lua_pcall(4c68878629ad9751).(L, argc - 1, 0, -2); // run the result |
| 207 |
} |
| 208 |
|
| 209 |
if(error) |
| 210 |
log_messagefcxx . . $ log_messagef(326aeb464d16410c).("fatal error: %s", lua_tostringcxx . . $ `<external>/15848857005865937740/lua.h:279:9`!(L, -1)); |
| 211 |
else |
| 212 |
log_messagecxx . . $ log_message(85c52e162fed56f9).("exiting normally"); |
| 213 |
|
| 214 |
lua_closecxx . . $ lua_close(f6fa9609f70d70d8).(L); |
| 215 |
return error; |
| 216 |
} |
| 217 |
|