From 1b01a0636c874e423cac7d47055cbce7b10e9715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guilloux?= Date: Thu, 17 Jul 2025 13:30:33 +0200 Subject: [PATCH] Fix #14362, 4b677e8256: Don't crash old scripts doing silly instantiation (#14394) --- bin/ai/compat_14.nut | 8 ++++++++ bin/game/compat_14.nut | 8 ++++++++ src/script/script_instance.cpp | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/bin/ai/compat_14.nut b/bin/ai/compat_14.nut index 012a8d070c..721278c24e 100644 --- a/bin/ai/compat_14.nut +++ b/bin/ai/compat_14.nut @@ -36,3 +36,11 @@ AITown.FoundTown <- function(tile, size, city, layout, name) { return AITown.Fou AIVehicle.SetNameCompat14 <- AIVehicle.SetName; AIVehicle.SetName <- function(id, name) { return AIVehicle.SetNameCompat14(id, AICompat14.Text(name)); } + +AIObject.constructorCompat14 <- AIObject.constructor; +foreach(name, object in CompatScriptRootTable) { + if (type(object) != "class") continue; + if (!object.rawin("constructor")) continue; + if (object.constructor != AIObject.constructorCompat14) continue; + object.constructor <- function() : (name) { AILog.Error("'" + name + "' is not instantiable"); } +} diff --git a/bin/game/compat_14.nut b/bin/game/compat_14.nut index 757e2496f4..652f6724ca 100644 --- a/bin/game/compat_14.nut +++ b/bin/game/compat_14.nut @@ -81,3 +81,11 @@ GSTown.FoundTown <- function(tile, size, city, layout, name) { return GSTown.Fou GSVehicle.SetNameCompat14 <- GSVehicle.SetName; GSVehicle.SetName <- function(id, name) { return GSVehicle.SetNameCompat14(id, GSCompat14.Text(name)); } + +GSObject.constructorCompat14 <- GSObject.constructor; +foreach(name, object in CompatScriptRootTable) { + if (type(object) != "class") continue; + if (!object.rawin("constructor")) continue; + if (object.constructor != GSObject.constructorCompat14) continue; + object.constructor <- function() : (name) { GSLog.Error("'" + name + "' is not instantiable"); } +} diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 6d493e4438..e321d515b8 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -130,6 +130,13 @@ bool ScriptInstance::LoadCompatibilityScripts(Subdirectory dir, std::spanapi_version)); + HSQUIRRELVM vm = this->engine->GetVM(); + sq_pushroottable(vm); + sq_pushstring(vm, "CompatScriptRootTable"); + sq_pushroottable(vm); + sq_newslot(vm, -3, SQFalse); + sq_pop(vm, 1); + /* Downgrade the API till we are the same version as the script. The last * entry in the list is always the current version, so skip that one. */ for (auto it = std::rbegin(api_versions) + 1; it != std::rend(api_versions); ++it) { @@ -138,6 +145,11 @@ bool ScriptInstance::LoadCompatibilityScripts(Subdirectory dir, std::spanapi_version) break; } + sq_pushroottable(vm); + sq_pushstring(vm, "CompatScriptRootTable"); + sq_deleteslot(vm, -2, SQFalse); + sq_pop(vm, 1); + return true; }