From 76ac17ff92b78217890342976dae35a473506640 Mon Sep 17 00:00:00 2001 From: Ed Kolis <edkolis@gmail.com> Date: Sat, 21 Dec 2024 14:01:04 -0500 Subject: [PATCH] 334 building a vehicle crashes the game on turn processing (#342) * Fix typo * Fix threading crash in PythonScriptEngine * Don't serialize space vehicles' sectors, that's saved in the star system or something and it was crashing the game with a null reference exception in serialization --- .../Modding/Scripts/PythonScriptEngine.cs | 22 ++++++++++++++----- FrEee.Root/Configuration.cs | 2 +- FrEee.Vehicles/Types/SpaceVehicle.cs | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/FrEee.Core.Domain/Modding/Scripts/PythonScriptEngine.cs b/FrEee.Core.Domain/Modding/Scripts/PythonScriptEngine.cs index 6b58c75b..ecde69f2 100644 --- a/FrEee.Core.Domain/Modding/Scripts/PythonScriptEngine.cs +++ b/FrEee.Core.Domain/Modding/Scripts/PythonScriptEngine.cs @@ -10,6 +10,7 @@ using System.Reflection; using FrEee.Objects.GameState; using FrEee.Objects.Space; +using System.Threading; namespace FrEee.Modding.Scripts; @@ -18,6 +19,9 @@ namespace FrEee.Modding.Scripts; /// </summary> public class PythonScriptEngine : MarshalByRefObject { + private static readonly Lock codeScriptLock = new(); + private static readonly Lock compiledScriptLock = new(); + /// <summary> /// Creates the IronPython scripting engine /// </summary> @@ -372,9 +376,12 @@ public static void UpdateScope(IDictionary<string, object> variables) /// <returns>The script.</returns> private static PythonScript GetCodeScript(ScriptCode source) { - if (!codeScripts.ContainsKey(source)) - codeScripts.Add(source, new PythonScript(source.ModuleName, source.Code, source.ExternalScripts)); - return codeScripts[source]; + lock (codeScriptLock) + { + if (!codeScripts.ContainsKey(source)) + codeScripts.Add(source, new PythonScript(source.ModuleName, source.Code, source.ExternalScripts)); + return codeScripts[source]; + } } /// <summary> @@ -384,9 +391,12 @@ private static PythonScript GetCodeScript(ScriptCode source) /// <returns>The compiled code.</returns> private static CompiledCode GetCompiledScript(PythonScript source) { - if (!compiledScripts.ContainsKey(source)) - compiledScripts.Add(source, Compile(source)); - return compiledScripts[source]; + lock (compiledScriptLock) + { + if (!compiledScripts.ContainsKey(source)) + compiledScripts.Add(source, Compile(source)); + return compiledScripts[source]; + } } private class CompiledCodeWithVariables : IEquatable<CompiledCodeWithVariables> diff --git a/FrEee.Root/Configuration.cs b/FrEee.Root/Configuration.cs index 9046afb9..846cc5f2 100644 --- a/FrEee.Root/Configuration.cs +++ b/FrEee.Root/Configuration.cs @@ -31,7 +31,7 @@ public static void ConfigureDI() { // TODO: load dependencies from configuration file in mod data so we can really modularize this thing! - // reset in case DI as already running (e.g. unit test suite) + // reset in case DI was already running (e.g. unit test suite) DI.Reset(); // processes diff --git a/FrEee.Vehicles/Types/SpaceVehicle.cs b/FrEee.Vehicles/Types/SpaceVehicle.cs index 9a481e75..2ea8b1ee 100644 --- a/FrEee.Vehicles/Types/SpaceVehicle.cs +++ b/FrEee.Vehicles/Types/SpaceVehicle.cs @@ -159,7 +159,7 @@ public long PopulationStorageFree get { return 0; } } - [DoNotCopy(false)] + [DoNotSerialize(false)] public override Sector Sector { get