From d3974ad5906917aa25d543bd0b859d357d6afa65 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Thu, 11 Dec 2025 23:33:17 -0700 Subject: [PATCH] update changelog and version bump --- Changelog.md | 6 ++ ModData/About/About.xml | 2 +- csharp_mod/GlobalCode.cs | 5 +- csharp_mod/Marshal.cs | 26 ++++++-- csharp_mod/Patches.cs | 96 ++++++++++++++++++++++++++++-- csharp_mod/Plugin.cs | 2 +- csharp_mod/stationeersSlang.csproj | 2 +- rust_compiler/Cargo.lock | 2 +- rust_compiler/Cargo.toml | 2 +- 9 files changed, 128 insertions(+), 15 deletions(-) diff --git a/Changelog.md b/Changelog.md index d563491..46736d6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,11 @@ # Changelog +[0.2.4] + +- Groundwork laid to collect and track source maps +- IC Housing will now display the `Slang` source error line (if available) + instead of the `IC10` source error line + [0.2.3] - Fixed stack underflow with function invocations diff --git a/ModData/About/About.xml b/ModData/About/About.xml index ae84c45..ef16119 100644 --- a/ModData/About/About.xml +++ b/ModData/About/About.xml @@ -2,7 +2,7 @@ Slang JoeDiertay - 0.2.3 + 0.2.4 [h1]Slang: High-Level Programming for Stationeers[/h1] diff --git a/csharp_mod/GlobalCode.cs b/csharp_mod/GlobalCode.cs index 14f3c23..bd9c5b2 100644 --- a/csharp_mod/GlobalCode.cs +++ b/csharp_mod/GlobalCode.cs @@ -44,10 +44,12 @@ public static class GlobalCode public static bool GetSlangErrorLineFromICError( Guid reference, uint icErrorLine, - out uint slangSrc + out uint slangSrc, + out Range slangSpan ) { slangSrc = icErrorLine; + slangSpan = new Range { }; if (!sourceMaps.ContainsKey(reference)) { @@ -62,6 +64,7 @@ public static class GlobalCode } slangSrc = foundRange[0].StartLine; + slangSpan = foundRange[0]; return true; } diff --git a/csharp_mod/Marshal.cs b/csharp_mod/Marshal.cs index fdc12a6..d5fb834 100644 --- a/csharp_mod/Marshal.cs +++ b/csharp_mod/Marshal.cs @@ -10,10 +10,23 @@ using StationeersIC10Editor; public struct Range { - public uint StartCol; - public uint EndCol; - public uint StartLine; - public uint EndLine; + public uint StartCol = 0; + public uint EndCol = 0; + public uint StartLine = 0; + public uint EndLine = 0; + + public Range(uint startLine, uint startCol, uint endLine, uint endCol) + { + StartLine = startLine; + StartCol = startCol; + EndLine = endLine; + EndCol = endCol; + } + + public override string ToString() + { + return $"L{StartLine}C{StartCol} - L{EndLine}C{EndCol}"; + } } public struct Diagnostic @@ -27,6 +40,11 @@ public struct SourceMapEntry { public Range SlangSource; public uint Ic10Line; + + public override string ToString() + { + return $"IC10: {Ic10Line} Slang: `{SlangSource}`"; + } } public static class Marshal diff --git a/csharp_mod/Patches.cs b/csharp_mod/Patches.cs index ea5169f..12fb8a2 100644 --- a/csharp_mod/Patches.cs +++ b/csharp_mod/Patches.cs @@ -1,12 +1,34 @@ namespace Slang; using System; +using System.Runtime.CompilerServices; using Assets.Scripts.Objects; using Assets.Scripts.Objects.Electrical; using Assets.Scripts.Objects.Motherboards; using Assets.Scripts.UI; using HarmonyLib; +class LineErrorData +{ + public AsciiString SourceRef; + public uint IC10ErrorSource; + public string SlangErrorReference; + public Range SlangErrorSpan; + + public LineErrorData( + AsciiString sourceRef, + uint ic10ErrorSource, + string slangErrorRef, + Range slangErrorSpan + ) + { + this.SourceRef = sourceRef; + this.IC10ErrorSource = ic10ErrorSource; + this.SlangErrorReference = slangErrorRef; + this.SlangErrorSpan = slangErrorSpan; + } +} + [HarmonyPatch] public static class SlangPatches { @@ -14,6 +36,9 @@ public static class SlangPatches private static AsciiString? _motherboardCachedCode; private static Guid? _currentlyEditingGuid; + private static ConditionalWeakTable _errorReferenceTable = + new(); + [HarmonyPatch( typeof(ProgrammableChipMotherboard), nameof(ProgrammableChipMotherboard.InputFinished) @@ -151,27 +176,88 @@ public static class SlangPatches { if ( String.IsNullOrEmpty(__result) - || __result.LastIndexOf(GlobalCode.SLANG_REF) < 0 + || !uint.TryParse(__result.Trim(), out var ic10ErrorLineNumber) + ) + { + return; + } + + var sourceAscii = __instance.GetSourceCode(); + + if (_errorReferenceTable.TryGetValue(__instance, out var cache)) + { + if (cache.SourceRef.Equals(sourceAscii) && cache.IC10ErrorSource == ic10ErrorLineNumber) + { + __result = cache.SlangErrorReference; + return; + } + } + + var source = System.Text.Encoding.UTF8.GetString( + System.Text.Encoding.ASCII.GetBytes(__instance.GetSourceCode()) + ); + + var slangIndex = source.LastIndexOf(GlobalCode.SLANG_REF); + + if ( + slangIndex < 0 || !Guid.TryParse( - __result + source .Substring( - __result.LastIndexOf(GlobalCode.SLANG_REF) + GlobalCode.SLANG_REF.Length + source.LastIndexOf(GlobalCode.SLANG_REF) + GlobalCode.SLANG_REF.Length ) .Trim(), out var slangGuid ) - || !uint.TryParse(__result.Trim(), out var ic10ErrorLineNumber) || !GlobalCode.GetSlangErrorLineFromICError( slangGuid, ic10ErrorLineNumber, - out var slangErrorLineNumber + out var slangErrorLineNumber, + out var slangSpan ) ) { return; } + L.Warning($"IC error at: {__result} -- Slang source error line: {slangErrorLineNumber}"); __result = slangErrorLineNumber.ToString(); + _errorReferenceTable.Remove(__instance); + _errorReferenceTable.Add( + __instance, + new LineErrorData( + sourceAscii, + ic10ErrorLineNumber, + slangErrorLineNumber.ToString(), + slangSpan + ) + ); + } + + [HarmonyPatch( + typeof(ProgrammableChip), + nameof(ProgrammableChip.SetSourceCode), + new Type[] { typeof(string) } + )] + [HarmonyPostfix] + public static void pgc_SetSourceCode_string(ProgrammableChip __instance, string sourceCode) + { + _errorReferenceTable.Remove(__instance); + } + + [HarmonyPatch( + typeof(ProgrammableChip), + nameof(ProgrammableChip.SetSourceCode), + new Type[] { typeof(string), typeof(ICircuitHolder) } + )] + [HarmonyPostfix] + public static void pgc_SetSourceCode_string_parent( + ProgrammableChip __instance, + string sourceCode, + ICircuitHolder parent + ) + { + _errorReferenceTable.Remove(__instance); } [HarmonyPatch( diff --git a/csharp_mod/Plugin.cs b/csharp_mod/Plugin.cs index fa576fe..6f8266c 100644 --- a/csharp_mod/Plugin.cs +++ b/csharp_mod/Plugin.cs @@ -41,7 +41,7 @@ namespace Slang { public const string PluginGuid = "com.biddydev.slang"; public const string PluginName = "Slang"; - public const string PluginVersion = "0.1.1"; + public const string PluginVersion = "0.2.4"; public static Mod MOD = new Mod(PluginName, PluginVersion); diff --git a/csharp_mod/stationeersSlang.csproj b/csharp_mod/stationeersSlang.csproj index 445cd13..3564bc4 100644 --- a/csharp_mod/stationeersSlang.csproj +++ b/csharp_mod/stationeersSlang.csproj @@ -5,7 +5,7 @@ enable StationeersSlang Slang Compiler Bridge - 0.2.3 + 0.2.4 true latest diff --git a/rust_compiler/Cargo.lock b/rust_compiler/Cargo.lock index fe7f196..ca53d0a 100644 --- a/rust_compiler/Cargo.lock +++ b/rust_compiler/Cargo.lock @@ -910,7 +910,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "slang" -version = "0.2.3" +version = "0.2.4" dependencies = [ "anyhow", "clap", diff --git a/rust_compiler/Cargo.toml b/rust_compiler/Cargo.toml index 89fff96..fdf339b 100644 --- a/rust_compiler/Cargo.toml +++ b/rust_compiler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "slang" -version = "0.2.3" +version = "0.2.4" edition = "2021" [workspace]