diff --git a/src/love.cpp b/src/love.cpp index 40213b404..00e8f36c5 100644 --- a/src/love.cpp +++ b/src/love.cpp @@ -172,7 +172,13 @@ static int l_print_sdl_log(lua_State *L) } #endif -static int runlove(int argc, char **argv) +enum DoneAction +{ + DONE_QUIT, + DONE_RESTART, +}; + +static DoneAction runlove(int argc, char **argv, int &retval) { #ifdef LOVE_LEGENDARY_APP_ARGV_HACK int hack_argc = 0; @@ -186,7 +192,8 @@ static int runlove(int argc, char **argv) if (argc > 1 && strcmp(argv[1], "--version") == 0) { printf("LOVE %s (%s)\n", love_version(), love_codename()); - return 0; + retval = 0; + return DONE_QUIT; } // Create the virtual machine. @@ -247,7 +254,13 @@ static int runlove(int argc, char **argv) // Call the returned boot function. lua_call(L, 0, 1); - int retval = 0; + retval = 0; + DoneAction done = DONE_QUIT; + + // if love.boot() returns "restart", we'll start up again after closing this + // Lua state. + if (lua_type(L, -1) == LUA_TSTRING && strcmp(lua_tostring(L, -1), "restart") == 0) + done = DONE_RESTART; if (lua_isnumber(L, -1)) retval = (int) lua_tonumber(L, -1); @@ -262,13 +275,11 @@ static int runlove(int argc, char **argv) } #endif // LOVE_LEGENDARY_APP_ARGV_HACK - return retval; + return done; } int main(int argc, char **argv) { - int retval = 0; - if (strcmp(LOVE_VERSION_STRING, love_version()) != 0) { printf("Version mismatch detected!\nLOVE binary is version %s\n" @@ -276,15 +287,20 @@ int main(int argc, char **argv) return 1; } -#ifdef LOVE_IOS - // on iOS we should never programmatically exit the app, so we'll just - // "restart" when that is attempted. Games which use threads might cause - // some issues if the threads aren't cleaned up properly... - while (true) -#endif + int retval = 0; + DoneAction done = DONE_QUIT; + + do { - retval = runlove(argc, argv); - } + done = runlove(argc, argv, retval); + +#ifdef LOVE_IOS + // on iOS we should never programmatically exit the app, so we'll just + // "restart" when that is attempted. Games which use threads might cause + // some issues if the threads aren't cleaned up properly... + done = DONE_RESTART; +#endif + } while (done != DONE_QUIT); #ifdef LOVE_ANDROID SDL_Quit(); diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index ff42232f0..552827e1d 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -679,5 +679,5 @@ return function() local result, retval = xpcall(love.run, deferErrhand) if not result then return 1 end - return tonumber(retval) or 0 + return retval == nil and 0 or retval end diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index 45c5fe150..90d13adf8 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -1234,8 +1234,9 @@ const unsigned char boot_lua[] = 0x72, 0x75, 0x6e, 0x2c, 0x20, 0x64, 0x65, 0x66, 0x65, 0x72, 0x45, 0x72, 0x72, 0x68, 0x61, 0x6e, 0x64, 0x29, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x31, 0x20, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x74, 0x6f, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x28, 0x72, - 0x65, 0x74, 0x76, 0x61, 0x6c, 0x29, 0x20, 0x6f, 0x72, 0x20, 0x30, 0x0a, + 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x76, 0x61, 0x6c, 0x20, 0x3d, 0x3d, 0x20, + 0x6e, 0x69, 0x6c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x30, 0x20, 0x6f, 0x72, 0x20, 0x72, 0x65, 0x74, 0x76, 0x61, + 0x6c, 0x0a, 0x65, 0x6e, 0x64, 0x0a, }; // [boot.lua] } // love