From 611f0f6f714e91ccbf4ae6d0c4b51bba80758870 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Fri, 28 Nov 2025 01:14:54 -0700 Subject: [PATCH] Start bindings and interface implementation for the IC10Editor mod --- .gitignore | 1 + csharp_mod/Patches.cs | 5 +++ csharp_mod/SlangFormatter.cs | 12 +++++++ csharp_mod/SlangPlugin.cs | 48 +++++++++++++++++-------- csharp_mod/StationpediaDocumentation.cs | 22 ++++++++++++ csharp_mod/stationeersSlang.csproj | 6 +++- 6 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 csharp_mod/SlangFormatter.cs create mode 100644 csharp_mod/StationpediaDocumentation.cs diff --git a/.gitignore b/.gitignore index 8764920..5e30f94 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target release bin obj +ref diff --git a/csharp_mod/Patches.cs b/csharp_mod/Patches.cs index 3ea5f7f..78d067f 100644 --- a/csharp_mod/Patches.cs +++ b/csharp_mod/Patches.cs @@ -1,3 +1,4 @@ +using System; using Assets.Scripts.Objects.Motherboards; using HarmonyLib; @@ -29,6 +30,10 @@ namespace Slang return; } + var newUuid = Guid.NewGuid().ToString(); + + SlangPlugin.CopySourceToFile(result); + // Set the result to be the compiled source so the rest of the function can continue as normal result = compiled; } diff --git a/csharp_mod/SlangFormatter.cs b/csharp_mod/SlangFormatter.cs new file mode 100644 index 0000000..fd96cba --- /dev/null +++ b/csharp_mod/SlangFormatter.cs @@ -0,0 +1,12 @@ +using StationeersIC10Editor; + +namespace Slang +{ + public class SlangFormatter : ICodeFormatter + { + public override Line ParseLine(string line) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/csharp_mod/SlangPlugin.cs b/csharp_mod/SlangPlugin.cs index 9038960..0225cfa 100644 --- a/csharp_mod/SlangPlugin.cs +++ b/csharp_mod/SlangPlugin.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; using BepInEx; using HarmonyLib; @@ -9,7 +10,7 @@ namespace Slang { class L { - private static BepInEx.Logging.ManualLogSource _logger; + private static BepInEx.Logging.ManualLogSource? _logger; public static void SetLogger(BepInEx.Logging.ManualLogSource logger) { @@ -18,34 +19,32 @@ namespace Slang public static void Debug(string message) { - _logger.LogDebug(message); + _logger?.LogDebug(message); } public static void Info(string message) { - _logger.LogInfo(message); + _logger?.LogInfo(message); } public static void Error(string message) { - _logger.LogError(message); + _logger?.LogError(message); } public static void Warning(string message) { - _logger.LogWarning(message); + _logger?.LogWarning(message); } } [BepInPlugin(PluginGuid, PluginName, "0.1.0")] public class SlangPlugin : BaseUnityPlugin { - public const string PluginGuid = "com.dbidwell94.slang"; + public const string PluginGuid = "com.biddydev.slang"; public const string PluginName = "Slang"; - const string RUST_DLL_NAME = "slang.dll"; - - private readonly string[] SLANG_KEYWORDS = { "let ", "fn " }; + const string RUST_DLL_NAME = "slang.compiler.dll"; /// Takes raw `Slang` source code and compiles it into IC10 [DllImport(RUST_DLL_NAME, CallingConvention = CallingConvention.Cdecl)] @@ -55,6 +54,21 @@ namespace Slang [DllImport(RUST_DLL_NAME, CallingConvention = CallingConvention.Cdecl)] private static extern void free_slang_string(IntPtr ptr); + private static Regex? _slangSourceCheck = null; + + private static Regex SlangSourceCheck + { + get + { + if (_slangSourceCheck is null) + { + _slangSourceCheck = new Regex(@"[;{}()]|\b(let|fn|device)\b|\/\/"); + } + + return _slangSourceCheck; + } + } + public static string Compile(string source) { if (string.IsNullOrEmpty(source)) @@ -71,11 +85,19 @@ namespace Slang } } - public static bool IsSlangSource(ref string input) + /// Take original slang source code and copies it to a file + /// for use in restoring later. + /// + public static bool CopySourceToFile(string source) { return true; } + public static bool IsSlangSource(ref string input) + { + return SlangSourceCheck.IsMatch(input); + } + private void Awake() { L.SetLogger(Logger); @@ -94,7 +116,7 @@ namespace Slang { if (stream == null) { - Logger.LogError( + L.Error( $"{RUST_DLL_NAME} compiler not found. This means it was not embedded in the mod. Please contact the mod author!" ); return; @@ -109,9 +131,7 @@ namespace Slang } catch (IOException e) { - Logger.LogWarning( - $"Could not overwrite {fileName} (it might be in use): {e.Message}" - ); + L.Warning($"Could not overwrite {fileName} (it might be in use): {e.Message}"); } } } diff --git a/csharp_mod/StationpediaDocumentation.cs b/csharp_mod/StationpediaDocumentation.cs new file mode 100644 index 0000000..e03814a --- /dev/null +++ b/csharp_mod/StationpediaDocumentation.cs @@ -0,0 +1,22 @@ +using Assets.Scripts.UI; + +namespace Slang +{ + public static class SlangDocs + { + public static StationpediaPage[] Pages + { + get + { + return + [ + new StationpediaPage( + "slang-init", + "Slang", + "Slang is a new high level language built specifically for Stationeers" + ), + ]; + } + } + } +} diff --git a/csharp_mod/stationeersSlang.csproj b/csharp_mod/stationeersSlang.csproj index 20ec76f..2810128 100644 --- a/csharp_mod/stationeersSlang.csproj +++ b/csharp_mod/stationeersSlang.csproj @@ -46,11 +46,15 @@ $(ManagedDir)/Assembly-CSharp-firstpass.dll False + + ./ref/IC10Editor.dll + False + - slang.dll + slang.compiler.dll