[Scummvm-git-logs] scummvm master -> 95d26488aac2210c0964b4b1fd2a14ac48604238

sev- noreply at scummvm.org
Thu Feb 15 22:52:17 UTC 2024


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
95d26488aa CHAMBER: remove setjmp/longjmp for restart and SCR_4D_PriorityCommand.


Commit: 95d26488aac2210c0964b4b1fd2a14ac48604238
    https://github.com/scummvm/scummvm/commit/95d26488aac2210c0964b4b1fd2a14ac48604238
Author: kartiksharmakk (77577353+kartiksharmakk at users.noreply.github.com)
Date: 2024-02-15T23:52:13+01:00

Commit Message:
CHAMBER: remove setjmp/longjmp for restart and SCR_4D_PriorityCommand.

Changed paths:
    engines/chamber/chamber.cpp
    engines/chamber/chamber.h
    engines/chamber/kult.cpp
    engines/chamber/savegame.cpp
    engines/chamber/script.cpp


diff --git a/engines/chamber/chamber.cpp b/engines/chamber/chamber.cpp
index 266657611b3..7de8b19412a 100644
--- a/engines/chamber/chamber.cpp
+++ b/engines/chamber/chamber.cpp
@@ -48,6 +48,9 @@ ChamberEngine::ChamberEngine(OSystem *syst, const ADGameDescription *desc)
 	_rnd = new Common::RandomSource("chamber");
 
 	_shouldQuit = false;
+	_shouldRestart = false;
+	_prioritycommand_1 = false;
+	_prioritycommand_2 = false;
 	_pxiData = NULL;
 
 	_speakerHandle = NULL;
diff --git a/engines/chamber/chamber.h b/engines/chamber/chamber.h
index ea55d194afd..3a8e7830c4b 100644
--- a/engines/chamber/chamber.h
+++ b/engines/chamber/chamber.h
@@ -21,6 +21,7 @@
 
 #ifndef CHAMBER_H
 #define CHAMBER_H
+#define RUNCOMMAND_RESTART 1337
 
 #include "common/random.h"
 #include "common/serializer.h"
@@ -48,6 +49,8 @@ public:
 	Common::Language getLanguage() const;
 
 	Common::Error run() override;
+	Common::Error init();
+	Common::Error execute();
 	bool hasFeature(EngineFeature f) const override;
 	bool canLoadGameStateCurrently(Common::U32String *msg = nullptr) override { return true; }
 	bool canSaveGameStateCurrently(Common::U32String *msg = nullptr) override { return true; }
@@ -62,6 +65,9 @@ public:
 
 public:
 	bool _shouldQuit;
+	bool _shouldRestart;
+	bool _prioritycommand_1;
+	bool _prioritycommand_2;
 
 	byte *_pxiData;
 
diff --git a/engines/chamber/kult.cpp b/engines/chamber/kult.cpp
index 27592d410af..ae755874b11 100644
--- a/engines/chamber/kult.cpp
+++ b/engines/chamber/kult.cpp
@@ -19,10 +19,6 @@
  *
  */
 
-#define FORBIDDEN_SYMBOL_EXCEPTION_setjmp
-
-#include <setjmp.h>
-
 #include "common/error.h"
 #include "common/system.h"
 #include "engines/util.h"
@@ -167,6 +163,8 @@ process:
 			updateUndrawCursor(target);
 			refreshSpritesData();
 			runCommand();
+			if (g_vm->_shouldRestart)
+				return;
 			blitSpritesToBackBuffer();
 			processInput();
 			drawSpots(target);
@@ -175,7 +173,9 @@ process:
 
 			updateUndrawCursor(target);
 			refreshSpritesData();
-			runCommandKeepSp();
+			uint16 restart = runCommandKeepSp();
+			if (restart == RUNCOMMAND_RESTART && g_vm->_shouldRestart)
+				return;
 			script_byte_vars.used_commands++;
 			if (script_byte_vars.dead_flag) {
 				if (--script_byte_vars.tries_left == 0)
@@ -194,13 +194,11 @@ void exitGame(void) {
 	uninitTimer();
 }
 
-jmp_buf restart_jmp;
-
 #ifdef DEBUG_ENDING
 extern theEnd(void);
 #endif
 
-Common::Error ChamberEngine::run() {
+Common::Error ChamberEngine::init() {
 	byte c;
 
 	// Initialize graphics using following:
@@ -309,10 +307,10 @@ Common::Error ChamberEngine::run() {
 		}
 	}
 
-	/*restart game from here*/
-//restart:;
-	setjmp(restart_jmp);
+	return Common::kNoError;
+}
 
+Common::Error ChamberEngine::execute() {
 	randomize();
 
 	/* Set start zone */
@@ -354,6 +352,8 @@ Common::Error ChamberEngine::run() {
 
 	/* Main game loop */
 	gameLoop(frontbuffer);
+	if (g_vm->_shouldRestart)
+		run();
 
 	/*TODO: the following code is never executed since gameLoop is infinite (or the whole game is restarted) */
 
@@ -364,5 +364,16 @@ Common::Error ChamberEngine::run() {
 
 	return Common::kNoError;
 }
+Common::Error ChamberEngine::run() {
+	if (!g_vm->_shouldRestart)
+		init();
+
+	do {
+		g_vm->_shouldRestart = false;
+		execute();
+	} while (g_vm->_shouldRestart);
+
+	return Common::kNoError;
+}
 
 } // End of namespace Chamber
diff --git a/engines/chamber/savegame.cpp b/engines/chamber/savegame.cpp
index 4dcd9390f09..699c5d37751 100644
--- a/engines/chamber/savegame.cpp
+++ b/engines/chamber/savegame.cpp
@@ -674,6 +674,7 @@ extern void askDisk2(void);
 
 void restartGame(void) {
 	warning("STUB: restartGame()");
+	g_vm->_shouldRestart = true;
 
 #if 0
 	int16 f;
@@ -692,7 +693,6 @@ void restartGame(void) {
 	script_byte_vars.cur_spot_flags = 0xFF;
 	script_byte_vars.load_flag = 2;
 
-	longjmp(restart_jmp, 1);
 #endif
 }
 
diff --git a/engines/chamber/script.cpp b/engines/chamber/script.cpp
index 01a63404ad2..97e4280eaa9 100644
--- a/engines/chamber/script.cpp
+++ b/engines/chamber/script.cpp
@@ -19,11 +19,6 @@
  *
  */
 
-#define FORBIDDEN_SYMBOL_EXCEPTION_setjmp
-#define FORBIDDEN_SYMBOL_EXCEPTION_longjmp
-
-#include <setjmp.h>
-
 #include "common/system.h"
 #include "common/debug.h"
 
@@ -55,8 +50,6 @@ char DEBUG_SCRIPT_LOG[] = "!script.log";
 
 namespace Chamber {
 
-jmp_buf script_jmp;
-
 byte rand_seed;
 uint16 the_command;
 uint16 script_res;
@@ -603,12 +596,7 @@ uint16 SCR_4D_PriorityCommand(void) {
 	the_command |= (*script_ptr++) << 8;
 	the_command |= 0xF000;
 
-	/*TODO: normally this should be called from the runCommand() itself,
-	because command Fxxx may be issued from the other places as well (maybe it's not the case)
-	But that would require some sort of synchronization to avoid infinite loop
-	So jump to top interepter's loop directly from here for now
-	*/
-	longjmp(script_jmp, 1);
+	g_vm->_prioritycommand_1 = true;
 
 	return ScriptRerun;
 }
@@ -1664,6 +1652,11 @@ uint16 SCR_3D_ActionsMenu(void) {
 		}
 
 		runCommand();
+		// For properly returning to RunScript() during the 2nd Call from SCR_4D_PriorityCommand() to runCommand()
+		if (g_vm->_prioritycommand_1) {
+			g_vm->_prioritycommand_2 = true;
+			break;
+		}
 
 		script_byte_vars.used_commands++;
 		if (script_byte_vars.bvar_43 == 0 && script_byte_vars.used_commands > script_byte_vars.check_used_commands) {
@@ -4343,6 +4336,12 @@ uint16 RunScript(byte *code) {
 
 		status = script_handlers[opcode]();
 
+		if (g_vm->_shouldRestart)
+			return status;
+
+		if (g_vm->_prioritycommand_1)
+			return status;
+
 		if (status != ScriptContinue || g_vm->_shouldQuit)
 			break;
 	}
@@ -4424,6 +4423,14 @@ again:;
 		}
 	}
 #endif
+	if (g_vm->_shouldRestart)
+		return runCommandKeepSp();
+
+	if (g_vm->_prioritycommand_1 && !(g_vm->_prioritycommand_2))
+		return res;
+
+	if (g_vm->_prioritycommand_1 && g_vm->_prioritycommand_2)
+		return runCommandKeepSp();
 
 	/*TODO: this is pretty hacky, original code manipulates the stack to discard old script invocation*/
 	if (res == ScriptRerun)
@@ -4434,7 +4441,10 @@ again:;
 
 uint16 runCommandKeepSp(void) {
 	/*keep_sp = sp;*/
-	setjmp(script_jmp);
+	g_vm->_prioritycommand_1 = false;
+	g_vm->_prioritycommand_2 = false;
+	if (g_vm->_shouldRestart)
+		return RUNCOMMAND_RESTART;
 	return runCommand();
 }
 




More information about the Scummvm-git-logs mailing list