Skip to content

Commit

Permalink
334 building a vehicle crashes the game on turn processing (#342)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ekolis authored Dec 21, 2024
1 parent 2dafa56 commit 76ac17f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
22 changes: 16 additions & 6 deletions FrEee.Core.Domain/Modding/Scripts/PythonScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using FrEee.Objects.GameState;
using FrEee.Objects.Space;
using System.Threading;

namespace FrEee.Modding.Scripts;

Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion FrEee.Root/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion FrEee.Vehicles/Types/SpaceVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public long PopulationStorageFree
get { return 0; }
}

[DoNotCopy(false)]
[DoNotSerialize(false)]
public override Sector Sector
{
get
Expand Down

0 comments on commit 76ac17f

Please sign in to comment.