Debian Codegraph @

dokidoki-support/minlua.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #include <libgen.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #include <lua.h>
8 #include <lauxlib.h>
9 #include <lualib.h>
10
11 #include "log.h"
12
13 //// preloaders ///////////////////////////////////////////////////////////////
14 // to statically link in binary lua modules:
15 //
16 // - use the right linker options (or just include the .a in the link line)
17 // - add them to init_preloaders right below
18 //
19 // You don't need to manually declare them or include anything, the
20 // REGISTER_LOADER macro handles that.
21
22 #define REGISTER_LOADER(module_name, loader) \
23 int loader(lua_State *L); \
24 lua_pushcfunction(L, loader); \
25 lua_setfield(L, -2, module_name)
26
27 void init_preloaders(lua_State *L)
28 {
29 lua_getglobal(L, "package");
30 lua_getfield(L, -1, "preload");
31
32 // add your custom loaders here, they look like this:
33 REGISTER_LOADER("collision.native", luaopen_collision_native);
34 REGISTER_LOADER("gl", luaopen_gl);
35 REGISTER_LOADER("glfw", luaopen_glfw);
36 REGISTER_LOADER("glu", luaopen_glu);
37 REGISTER_LOADER("log", luaopen_log);
38 REGISTER_LOADER("memarray", luaopen_memarray);
39 REGISTER_LOADER("mixer", luaopen_mixer);
40 REGISTER_LOADER("stb_image", luaopen_stb_image);
41
42 // each night I pray that I might one day happen upon a less disgusting way
43 // of doing this
44 #ifdef EXTRA_LOADERS
45 #include EXTRA_LOADERS
46 #endif
47
48 lua_pop(L, 2);
49 }
50
51 //// working directory handling ///////////////////////////////////////////////
52
53 #if defined(DOKIDOKI_LINUX)
54 int get_exe_path(char *buffer, int size)
55 {
56 int ret = readlink("/proc/self/exe", buffer, size);
57 if(0 >= ret && ret < size)
58 buffer[ret] = 0;
59 return ret;
60 }
61 #elif defined(DOKIDOKI_MINGW)
62 #include <windows.h>
63 int get_exe_path(char *buffer, int size)
64 {
65 return GetModuleFileName(NULL, buffer, size);
66 }
67 #elif defined(DOKIDOKI_MACOSX)
68 #include <CFBundle.h>
69 #include <CFURL.h>
70 int switch_to_game_directory()
71 {
72 log_message("finding the bundle directory");
73
74 CFBundleRef bundle = CFBundleGetMainBundle();
75 CFURLRef url = NULL;
76 if(bundle)
77 url = CFBundleCopyBundleURL(bundle);
78 if(!url)
79 {
80 log_message("no bundle found");
81 return 0;
82 }
83
84 CFStringRef str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
85 CFRelease(url);
86
87 int size = CFStringGetLength(str) + 1;
88 char *dir = (char *)malloc(size);
89
90 int success = CFStringGetCString(str, dir, size, kCFURLPOSIXPathStyle);
91 CFRelease(str);
92
93 if(success)
94 {
95 int success = chdir(dir);
96 if(success == 0)
97 log_messagef("changed directory to '%s'", dir);
98 else
99 log_messagef("failed changing directory to '%s'", dir);
100
101 // there must be a better way to tell if we're bundled :/
102 if(chdir("Contents/Resources") == 0)
103 log_message("running as an app bundle, changed to resources directory");
104 else
105 log_message("running unbundled");
106 }
107
108 free(dir);
109 return success;
110 }
111 #else
112 int switch_to_game_directory()
113 {
114 log_message("automatic game path detection not supported on this platform");
115 return 0;
116 }
117 #endif
118
119 #if defined(DOKIDOKI_MINGW) || defined(DOKIDOKI_LINUX)
120 int switch_to_game_directory()
121 {
122 log_message("finding the game's directory");
123 int size = 256;
124 char *buffer = NULL;
125 while(!buffer)
126 {
127 buffer = (char *)malloc(size);
128 int ret = get_exe_path(buffer, size);
129
130 // error
131 if(ret <= 0)
132 {
133 free(buffer);
134 buffer = NULL;
135 log_message("failed locating the executable");
136 return 0;
137 }
138 // buffer too small
139 else if(ret == size)
140 {
141 free(buffer);
142 buffer = NULL;
143 size *= 2;
144 }
145 }
146
147 char *dir = dirname(buffer);
148
149 int success = chdir(dir);
150 if(success == 0)
151 log_messagef("changed directory to '%s'", dir);
152 else
153 log_messagef("failed to change directory to '%s'", dir);
154
155 free(buffer);
156 return success;
157 }
158 #endif
159
160 //// main program /////////////////////////////////////////////////////////////
161
162 int main(int argc, char ** argv)
163 {
164 int i;
165
166 int use_logfile = 1;
167 for(i = 0; i < argc; i++)
168 if(strcmp(argv[i], "--stderr") == 0)
169 use_logfile = 0;
170 log_init(use_logfile);
171
172 switch_to_game_directory();
173
174 // load lua and libraries
175 lua_State *L = lua_open();
176 luaL_openlibs(L);
177 init_preloaders(L);
178
179 // load arguments (and pre-arguments) into a global 'arg' table
180 lua_newtable(L);
181 for(i = 0; i < argc; i++)
182 {
183 lua_pushstring(L, argv[i]);
184 lua_rawseti(L, -2, i);
185 }
186 lua_setglobal(L, "arg");
187
188 // open app, with error reporting
189 lua_getglobal(L, "debug");
190 lua_getfield(L, -1, "traceback");
191
192 int error = luaL_dofile(L, "init.lua");
193
194 if(!error)
195 {
196 // load command-line arguments as function arguments
197 lua_checkstack(L, argc - 1);
198 for(i = 1; i <= argc - 1; i++)
199 lua_pushstring(L, argv[i]);
200 error = lua_pcall(L, argc - 1, 0, -2); // run the result
201 }
202
203 if(error)
204 log_messagef("fatal error: %s", lua_tostring(L, -1));
205 else
206 log_message("exiting normally");
207
208 lua_close(L);
209 return error;
210 }
211