Bladeren bron

love.event.quit("restart") now causes LÖVE to restart the Lua state after shutting it down.

Note that this isn't perfect – it won't kill running threads which aren't cleaned up by the Lua code when the main Lua state is shut down.
Alex Szpakowski 9 jaren geleden
bovenliggende
commit
e803878445
3 gewijzigde bestanden met toevoegingen van 33 en 16 verwijderingen
  1. 29 13
      src/love.cpp
  2. 1 1
      src/scripts/boot.lua
  3. 3 2
      src/scripts/boot.lua.h

+ 29 - 13
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;
 	}
 
+	int retval = 0;
+	DoneAction done = DONE_QUIT;
+
+	do
+	{
+		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...
-	while (true)
+		// 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
-	{
-		retval = runlove(argc, argv);
-	}
+	} while (done != DONE_QUIT);
 
 #ifdef LOVE_ANDROID
 	SDL_Quit();

+ 1 - 1
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

+ 3 - 2
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