int main(int argc, char ** argv) { int i; int use_logfile = 1; for(i = 0; i < argc; i++) if(strcmp(argv[i], "--stderr") == 0) use_logfile = 0; log_init(use_logfile); switch_to_game_directory(); // load lua and libraries lua_State *L = lua_open(); luaL_openlibs(L); init_preloaders(L); // load arguments (and pre-arguments) into a global 'arg' table lua_newtable(L); for(i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i); } lua_setglobal(L, "arg"); // open app, with error reporting lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); int error = luaL_dofile(L, "init.lua"); if(!error) { // load command-line arguments as function arguments lua_checkstack(L, argc - 1); for(i = 1; i <= argc - 1; i++) lua_pushstring(L, argv[i]); error = lua_pcall(L, argc - 1, 0, -2); // run the result } if(error) log_messagef("fatal error: %s", lua_tostring(L, -1)); else log_message("exiting normally"); lua_close(L); return error; }
int main(int argc, char ** argv) { // number of arguments to skip before the script's real arguments int skip_arg = 1; // init physfs if(!PHYSFS_init(argv[0])) { fprintf(stderr, "physfs init failed: %s", PHYSFS_getLastError()); return 1; } // get executable path const char *directory = PHYSFS_getBaseDir(); const char *executable = basename(argv[0]); char *path = malloc(strlen(directory) + strlen(executable) + 1); strcpy(path, directory); strcat(path, executable); // try to mount the executable as an archive, on failure try to mount the // first argument instead if(!PHYSFS_mount(path, "/", 0)) { skip_arg = skip_arg + 1; if(argc < 2 || !PHYSFS_mount(argv[1], "/", 0)) { fprintf(stderr, "no archive found in the executable nor in the " "first argument\n"); return 1; } } free(path); // load lua and libraries lua_State *L = lua_open(); luaL_openlibs(L); init_physfs_loader(L); init_preloaders(L); // load arguments (and pre-arguments) into a global 'arg' table lua_newtable(L); for(int i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - skip_arg + 1); } lua_setglobal(L, "arg"); // open app, with error reporting lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_pushcfunction(L, seed_loadfile); lua_pushstring(L, "init.lua"); int error = lua_pcall(L, 1, 1, 0); // load file if(!error) { // load command-line arguments as function arguments lua_checkstack(L, argc - skip_arg); for(int i = 1; i <= argc - skip_arg; i++) lua_pushstring(L, argv[i + skip_arg - 1]); error = lua_pcall(L, argc - skip_arg, 0, -2); // run the result } if(error) fprintf(stderr, "%s\n", lua_tostring(L, -1)); lua_close(L); PHYSFS_deinit(); return error; }