Debian Codegraph @

dokidoki-support/minlua.c

raw symbols ◀ ▶ SCIP
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 #include <errno.h>
7
8 #include <lua.h>
9 #include <lauxlib.h>
10 #include <lualib.h>
11
12 #include "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_LOADER(module_name, loader) \
24 int loader(lua_State *L); \
25 lua_pushcfunction(L, loader); \
26 lua_setfield(L, -2, module_name)
27
28 void init_preloaders(lua_State *L)
29 {
30 lua_getglobal(L, "package");
31 lua_getfield(L, -1, "preload");
32
33 // add your custom loaders here, they look like this:
34 REGISTER_LOADER("collision.native", luaopen_collision_native);
35 REGISTER_LOADER("gl", luaopen_gl);
36 REGISTER_LOADER("glfw", luaopen_glfw);
37 REGISTER_LOADER("glu", luaopen_glu);
38 REGISTER_LOADER("log", luaopen_log);
39 REGISTER_LOADER("memarray", luaopen_memarray);
40 REGISTER_LOADER("mixer", luaopen_mixer);
41 REGISTER_LOADER("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_LOADERS
46 #include EXTRA_LOADERS
47 #endif
48
49 lua_pop(L, 2);
50 }
51
52 //// working directory handling ///////////////////////////////////////////////
53
54 #if defined(DOKIDOKI_LINUX)
55 int get_exe_path(char *buffer, int size)
56 {
57 int ret = readlink("/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_LINUX)
121 int switch_to_game_directory()
122 {
123 log_message("finding the game's directory");
124 int size = 256;
125 char *buffer = NULL;
126 while(!buffer)
127 {
128 buffer = (char *)malloc(size);
129 int ret = get_exe_path(buffer, size);
130
131 // error
132 if(ret <= 0)
133 {
134 free(buffer);
135 buffer = NULL;
136 log_message("failed locating the executable");
137 return 0;
138 }
139 // buffer too small
140 else if(ret == size)
141 {
142 free(buffer);
143 buffer = NULL;
144 size *= 2;
145 }
146 }
147
148 char *dir = dirname(buffer);
149
150 int success = chdir(dir);
151 if(success == 0)
152 log_messagef("changed directory to '%s'", dir);
153 else
154 log_messagef("failed to change directory to '%s'", dir);
155
156 free(buffer);
157 return success;
158 }
159 #endif
160
161 //// main program /////////////////////////////////////////////////////////////
162
163 int main(int argc, char ** argv)
164 {
165 int i;
166
167 int use_logfile = 1;
168 for(i = 0; i < argc; i++)
169 if(strcmp(argv[i], "--stderr") == 0)
170 use_logfile = 0;
171 log_init(use_logfile);
172
•173 // switch_to_game_directory();
174 char *dir = "/usr/share/pax-britannica";
175 log_messagef("changing directory to '%s'", dir);
176 int success = chdir(dir);
177 if(success == -1)
178 perror("chdir()");
179
180 // load lua and libraries
181 lua_State *L = lua_open();
182 luaL_openlibs(L);
183 init_preloaders(L);
184
185 // load arguments (and pre-arguments) into a global 'arg' table
186 lua_newtable(L);
187 for(i = 0; i < argc; i++)
188 {
189 lua_pushstring(L, argv[i]);
190 lua_rawseti(L, -2, i);
191 }
192 lua_setglobal(L, "arg");
193
194 // open app, with error reporting
195 lua_getglobal(L, "debug");
196 lua_getfield(L, -1, "traceback");
197
198 int error = luaL_dofile(L, "init.lua");
199
200 if(!error)
201 {
202 // load command-line arguments as function arguments
203 lua_checkstack(L, argc - 1);
204 for(i = 1; i <= argc - 1; i++)
205 lua_pushstring(L, argv[i]);
206 error = lua_pcall(L, argc - 1, 0, -2); // run the result
207 }
208
209 if(error)
210 log_messagef("fatal error: %s", lua_tostring(L, -1));
211 else
212 log_message("exiting normally");
213
214 lua_close(L);
215 return error;
216 }
217