9 Commits

Author SHA1 Message Date
e272737ea2 Merge pull request 'Fixed const -> let bug' (#10) from declaration_const_as_let into master
All checks were successful
CI/CD Pipeline / test (push) Successful in 32s
CI/CD Pipeline / build (push) Successful in 1m43s
CI/CD Pipeline / release (push) Successful in 5s
Reviewed-on: #10
2025-12-29 02:44:50 -07:00
f679601818 Fixed const -> let bug
All checks were successful
CI/CD Pipeline / test (pull_request) Successful in 33s
CI/CD Pipeline / build (pull_request) Has been skipped
CI/CD Pipeline / release (pull_request) Has been skipped
2025-12-29 02:31:23 -07:00
3ca6f97db1 Merge pull request 'IC10Editor fix' (#9) from live-reload into master
All checks were successful
CI/CD Pipeline / test (push) Successful in 33s
CI/CD Pipeline / build (push) Successful in 1m41s
CI/CD Pipeline / release (push) Successful in 4s
Reviewed-on: #9
2025-12-27 22:26:50 -07:00
34817ee111 Merge branch 'master' of ssh://git.biddydev.com:2222/dbidwell/stationeers_lang into live-reload
All checks were successful
CI/CD Pipeline / test (pull_request) Successful in 32s
CI/CD Pipeline / build (pull_request) Has been skipped
CI/CD Pipeline / release (pull_request) Has been skipped
2025-12-27 22:19:09 -07:00
9eef8a77b6 Merge pull request 'Update About.xml docs for the workshop' (#8) from documentation into master
All checks were successful
CI/CD Pipeline / test (push) Successful in 34s
CI/CD Pipeline / build (push) Successful in 1m41s
CI/CD Pipeline / release (push) Has been skipped
Reviewed-on: #8
2025-12-27 22:19:00 -07:00
de31851153 Fixed IC10Editor implementation bug where IC10 code would not update after clicking 'Cancel' 2025-12-27 22:18:21 -07:00
3543b87561 wip 2025-12-27 16:03:36 -07:00
effef64add Update About.xml docs for the workshop
All checks were successful
CI/CD Pipeline / test (pull_request) Successful in 33s
CI/CD Pipeline / build (pull_request) Has been skipped
CI/CD Pipeline / release (pull_request) Has been skipped
2025-12-26 22:33:48 -07:00
794b27b8c6 Merge pull request 'documentation' (#7) from documentation into master
All checks were successful
CI/CD Pipeline / test (push) Successful in 33s
CI/CD Pipeline / build (push) Successful in 1m42s
CI/CD Pipeline / release (push) Has been skipped
Reviewed-on: #7
2025-12-26 22:02:46 -07:00
9 changed files with 217 additions and 76 deletions

View File

@@ -1,5 +1,21 @@
# Changelog # Changelog
[0.4.6]
- Fixed bug in compiler where you were unable to assign a `const` value to
a `let` variable
[0.4.5]
- Fixed issue where after clicking "Cancel" on the IC10 Editor, the side-by-side
IC10 output would no longer update with highlighting or code updates.
- Added ability to live-reload the mod while developing using the `ScriptEngine`
mod from BepInEx
- This required adding in cleanup code to cleanup references to the Rust DLL
before destroying the mod instance.
- Added BepInEx debug logging. This will ONLY show if you have debug logs
enabled in the BepInEx configuration file.
[0.4.4] [0.4.4]
- Added Stationpedia docs back after removing all harmony patches from the mod - Added Stationpedia docs back after removing all harmony patches from the mod

View File

@@ -2,11 +2,11 @@
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Slang</Name> <Name>Slang</Name>
<Author>JoeDiertay</Author> <Author>JoeDiertay</Author>
<Version>0.4.4</Version> <Version>0.4.6</Version>
<Description> <Description>
[h1]Slang: High-Level Programming for Stationeers[/h1] [h1]Slang: High-Level Programming for Stationeers[/h1]
Stop writing assembly. Start writing code. Iterate faster. Stop writing assembly. Start writing code.
Slang (Stationeers Language) brings modern programming to Stationeers. It allows you to write scripts using a familiar C-style syntax (variables, functions, if/else, loops) directly in the in-game editor. When you hit confirm, Slang compiles your code into IC10 instantly. Slang (Stationeers Language) brings modern programming to Stationeers. It allows you to write scripts using a familiar C-style syntax (variables, functions, if/else, loops) directly in the in-game editor. When you hit confirm, Slang compiles your code into IC10 instantly.
@@ -15,7 +15,7 @@ Slang (Stationeers Language) brings modern programming to Stationeers. It allows
[h2]Features[/h2] [h2]Features[/h2]
[list] [list]
[*] [b]In-Game Compilation:[/b] No external tools needed. Write Slang directly in the IC editor. [*] [b]In-Game Compilation:[/b] No external tools needed. Write Slang directly in the IC editor.
[*] [b]No More Register Juggling:[/b] Define variables with let (e.g., let temp = 300). The compiler manages r0-r15 for you. [*] [b]No More Register Juggling:[/b] Define variables with let (e.g., let temp = 200c). The compiler manages r0-r15 for you.
[*] [b]Control Flow:[/b] Write readable logic with if, else, while, loop, break, and continue. [*] [b]Control Flow:[/b] Write readable logic with if, else, while, loop, break, and continue.
[*] [b]Functions:[/b] Create reusable code blocks with arguments. [*] [b]Functions:[/b] Create reusable code blocks with arguments.
[*] [b]Smart Editor:[/b] Get real-time syntax highlighting and error checking (red text) as you type. [*] [b]Smart Editor:[/b] Get real-time syntax highlighting and error checking (red text) as you type.
@@ -53,14 +53,13 @@ loop {
[h2]Known Issues (Beta)[/h2] [h2]Known Issues (Beta)[/h2]
[list] [list]
[*] [b]Stack Access:[/b] Direct stack memory access is disabled to prevent conflicts with the compiler's internal memory management. A workaround is being planned. [*] [b]Stack Access:[/b] Direct stack memory access is disabled to prevent conflicts with the compiler's internal memory management. A workaround is being planned.
[*] [b]Documentation:[/b] In-game tooltips for syscalls (like load, set) are WIP. Check the "Slang" entry in the Stationpedia (F1) for help. [*] [b]Documentation:[/b] In-game tooltips for syscalls (like load, set) are WIP. Check the "Slang" entry in the Stationpedia (F1) for help, or checkout [url=https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md]The Docs[/url] for guides on how to get started.
[/list] [/list]
[h2]Planned Features[/h2] [h2]Planned Features[/h2]
[list] [list]
[*] Enhanced LSP features (Autocomplete, Go to Definition). [*] Enhanced LSP features (Autocomplete, Go to Definition).
[*] Full feature parity with all IC10 instructions. [*] Full feature parity with all IC10 instructions.
[*] Tutorials and beginner script examples.
[/list] [/list]
[h2]FAQ[/h2] [h2]FAQ[/h2]
@@ -70,10 +69,18 @@ A: The Slang compiler is built in Rust for performance and reliability. It is co
[b]Q: Is this compatible with my current save?[/b] [b]Q: Is this compatible with my current save?[/b]
A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As a matter of fact: if you wish to stop using Slang at any time, your compiled IC10 will still exist on the chip. However: Slang adds a comment at the bottom of your compiled IC10 which is a GZIP and base64 encoded version of your Slang source code. This might break line limits with Slang not installed. If you wish to no longer use Slang, I recommend you remove this comment from the source code after uninstalling Slang. A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As a matter of fact: if you wish to stop using Slang at any time, your compiled IC10 will still exist on the chip. However: Slang adds a comment at the bottom of your compiled IC10 which is a GZIP and base64 encoded version of your Slang source code. This might break line limits with Slang not installed. If you wish to no longer use Slang, I recommend you remove this comment from the source code after uninstalling Slang.
[b]Q: Does this modify the in-game scripting language[/b]
A: No! Slang compiles directly to IC10. Any valid Slang file will produce valid IC10. The goal of this mod is twofold:
[list]
[*] Allow experienced users to quickly iterate on their scripts to get back into base upgrades faster
[*] Allow newcomers who already know C-style languages (JS/C#/Java/Rust/C/etc) to see how it maps to IC10 so they can get to understand IC10 better
[/list]
[h2]Useful Links[/h2] [h2]Useful Links[/h2]
[url=https://github.com/dbidwell94/stationeers_lang]Source Code on GitHub[/url] [url=https://github.com/dbidwell94/stationeers_lang]Source Code on GitHub[/url]
[url=https://discord.gg/stationeers]Stationeers Official Discord[/url] [url=https://discord.gg/stationeers]Stationeers Official Discord[/url]
[url=https://discord.gg/M4sCfYMacs]Stationeers Modding Discord[/url] [url=https://discord.gg/M4sCfYMacs]Stationeers Modding Discord[/url]
[url=https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md]Getting Started Guide[/url]
</Description> </Description>
<ChangeLog xsi:nil="true" /> <ChangeLog xsi:nil="true" />
<WorkshopHandle>3619985558</WorkshopHandle> <WorkshopHandle>3619985558</WorkshopHandle>
@@ -116,5 +123,7 @@ A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As
See: https://github.com/StationeersLaunchPad/StationeersLaunchPad See: https://github.com/StationeersLaunchPad/StationeersLaunchPad
Source Code: https://github.com/dbidwell94/stationeers_lang Source Code: https://github.com/dbidwell94/stationeers_lang
Documentation: https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md
]]></InGameDescription> ]]></InGameDescription>
</ModMetadata> </ModMetadata>

View File

@@ -6,7 +6,6 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using StationeersIC10Editor; using StationeersIC10Editor;
using StationeersIC10Editor.IC10;
public class SlangFormatter : ICodeFormatter public class SlangFormatter : ICodeFormatter
{ {
@@ -14,8 +13,25 @@ public class SlangFormatter : ICodeFormatter
private CancellationTokenSource? _lspCancellationToken; private CancellationTokenSource? _lspCancellationToken;
private object _tokenLock = new(); private object _tokenLock = new();
protected Editor? Ic10Editor = null; protected Editor? __Ic10Editor = null;
private IC10CodeFormatter iC10CodeFormatter = new IC10CodeFormatter();
protected Editor Ic10Editor
{
get
{
if (__Ic10Editor == null)
{
var tab = Editor.ParentTab;
tab.ClearExtraEditors();
__Ic10Editor = new Editor(Editor.KeyHandler);
Ic10Editor.IsReadOnly = true;
tab.AddEditor(__Ic10Editor);
}
return __Ic10Editor;
}
}
private string ic10CompilationResult = ""; private string ic10CompilationResult = "";
private List<SourceMapEntry> ic10SourceMap = new(); private List<SourceMapEntry> ic10SourceMap = new();
@@ -80,7 +96,7 @@ public class SlangFormatter : ICodeFormatter
{ {
if (!Marshal.CompileFromString(RawText, out var compilationResult, out var sourceMap)) if (!Marshal.CompileFromString(RawText, out var compilationResult, out var sourceMap))
{ {
return "Compilation Error"; return "# Compilation Error";
} }
return compilationResult + $"\n{EncodeSource(RawText, SLANG_SRC)}"; return compilationResult + $"\n{EncodeSource(RawText, SLANG_SRC)}";
@@ -118,6 +134,10 @@ public class SlangFormatter : ICodeFormatter
return styledLine; return styledLine;
} }
/// <summary>
/// This handles calling the `HandleLsp` function by creating a new `CancellationToken` and
/// cancelling the current call if applicable.
/// </summary>
private void HandleCodeChanged() private void HandleCodeChanged()
{ {
CancellationToken token; CancellationToken token;
@@ -133,6 +153,11 @@ public class SlangFormatter : ICodeFormatter
_ = HandleLsp(inputSrc, token); _ = HandleLsp(inputSrc, token);
} }
/// <summary>
/// Takes a copy of the current source code and sends it to the Rust compiler in a background thread
/// to get diagnostic data. This also handles getting a compilation response of optimized IC10 for the
/// side-by-side IC10Editor to show with sourcemap highlighting.
/// </summary>
private async Task HandleLsp(string inputSrc, CancellationToken cancellationToken) private async Task HandleLsp(string inputSrc, CancellationToken cancellationToken)
{ {
try try
@@ -165,24 +190,21 @@ public class SlangFormatter : ICodeFormatter
return; return;
} }
var (compilationSuccess, compiled, sourceMap) = await Task.Run( var (compilationSuccess, compiled, sourceMap) = await Task.Run(() =>
() => {
{ var successful = Marshal.CompileFromString(
var successful = Marshal.CompileFromString( inputSrc,
inputSrc, out var compiled,
out var compiled, out var sourceMap
out var sourceMap );
); return (successful, compiled, sourceMap);
return (successful, compiled, sourceMap); });
},
cancellationToken
);
if (compilationSuccess) if (compilationSuccess)
{ {
ic10CompilationResult = compiled; ic10CompilationResult = compiled;
ic10SourceMap = sourceMap; ic10SourceMap = sourceMap;
UpdateIc10Formatter(); UpdateIc10Content(Ic10Editor);
} }
} }
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
@@ -192,22 +214,24 @@ public class SlangFormatter : ICodeFormatter
} }
} }
/// <summary>
/// Updates the underlying code in the IC10 Editor, after which will call `UpdateIc10Formatter` to
/// update highlighting of relavent fields.
/// </summary>
private void UpdateIc10Content(Editor editor)
{
editor.ResetCode(ic10CompilationResult);
UpdateIc10Formatter();
}
// This runs on the main thread. This function ONLY updates the highlighting of the IC10 code.
// If you need to update the code in the editor itself, you should use `UpdateIc10Content`.
private void UpdateIc10Formatter() private void UpdateIc10Formatter()
{ {
var tab = Editor.ParentTab; // Bail if our backing field is null. We don't want to set the field in this function. It
if (Ic10Editor == null) // runs way too much and we might not even have source code to use.
{ if (__Ic10Editor == null)
iC10CodeFormatter = new IC10CodeFormatter(); return;
Ic10Editor = new Editor(Editor.KeyHandler);
Ic10Editor.IsReadOnly = true;
iC10CodeFormatter.Editor = Ic10Editor;
}
if (tab.Editors.Count < 2)
{
tab.AddEditor(Ic10Editor);
}
var caretPos = Editor.CaretPos.Line; var caretPos = Editor.CaretPos.Line;
// get the slang sourceMap at the current editor line // get the slang sourceMap at the current editor line
@@ -215,10 +239,6 @@ public class SlangFormatter : ICodeFormatter
entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos
); );
// extract the current "context" of the ic10 compilation. The current Slang source line
// should be directly next to the compiled IC10 source line, and we should highlight the
// IC10 code that directly represents the Slang source
Ic10Editor.ResetCode(ic10CompilationResult); Ic10Editor.ResetCode(ic10CompilationResult);
if (lines.Count() < 1) if (lines.Count() < 1)
@@ -245,7 +265,11 @@ public class SlangFormatter : ICodeFormatter
}; };
} }
// This runs on the Main Thread /// <summary>
/// Takes diagnostics from the Rust FFI compiler and applies it as semantic tokens to the
/// source in this editor.
/// This runs on the Main Thread
/// </summary>
private void ApplyDiagnostics(Dictionary<uint, IGrouping<uint, Diagnostic>> dict) private void ApplyDiagnostics(Dictionary<uint, IGrouping<uint, Diagnostic>> dict)
{ {
HashSet<uint> linesToRefresh; HashSet<uint> linesToRefresh;

View File

@@ -67,7 +67,9 @@ public static class Marshal
try try
{ {
_libraryHandle = LoadLibrary(ExtractNativeLibrary(Ffi.RustLib)); _libraryHandle = LoadLibrary(ExtractNativeLibrary(Ffi.RustLib));
L.Debug("Rust DLL loaded successfully. Enjoy native speed compilations!");
CodeFormatters.RegisterFormatter("Slang", typeof(SlangFormatter), true); CodeFormatters.RegisterFormatter("Slang", typeof(SlangFormatter), true);
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@@ -91,8 +93,13 @@ public static class Marshal
try try
{ {
FreeLibrary(_libraryHandle); CodeFormatters.RegisterFormatter("Slang", typeof(PlainTextFormatter), true);
if (!FreeLibrary(_libraryHandle))
{
L.Warning("Unable to free Rust library");
}
_libraryHandle = IntPtr.Zero; _libraryHandle = IntPtr.Zero;
L.Debug("Rust DLL library freed");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@@ -191,9 +198,9 @@ public static class Marshal
Assembly assembly = Assembly.GetExecutingAssembly(); Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(libName)) using (Stream resourceStream = assembly.GetManifestResourceStream(libName))
{ {
if (stream == null) if (resourceStream == null)
{ {
L.Error( L.Error(
$"{libName} not found. This means it was not embedded in the mod. Please contact the mod author!" $"{libName} not found. This means it was not embedded in the mod. Please contact the mod author!"
@@ -201,18 +208,85 @@ public static class Marshal
return ""; return "";
} }
// Check if file exists and contents are identical to avoid overwriting locked files
if (File.Exists(destinationPath))
{
try
{
using (
FileStream fileStream = new FileStream(
destinationPath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite
)
)
{
if (resourceStream.Length == fileStream.Length)
{
if (StreamsContentsAreEqual(resourceStream, fileStream))
{
L.Debug(
$"DLL {libName} already exists and matches. Skipping extraction."
);
return destinationPath;
}
}
}
}
catch (IOException ex)
{
L.Warning(
$"Could not verify existing {libName}, attempting overwrite. {ex.Message}"
);
}
}
resourceStream.Position = 0;
// Attempt to overwrite if missing or different
try try
{ {
using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create)) using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create))
{ {
stream.CopyTo(fileStream); resourceStream.CopyTo(fileStream);
} }
return destinationPath; return destinationPath;
} }
catch (IOException e) catch (IOException e)
{ {
L.Warning($"Could not overwrite {libName} (it might be in use): {e.Message}"); // If we fail here, the file is likely locked.
return ""; // However, if we are here, it means the file is DIFFERENT or we couldn't read it.
// As a fallback for live-reload, we can try returning the path anyway
// assuming the existing locked file might still work.
L.Warning(
$"Could not overwrite {libName} (it might be in use): {e.Message}. Attempting to use existing file."
);
return destinationPath;
}
}
}
private static bool StreamsContentsAreEqual(Stream stream1, Stream stream2)
{
const int bufferSize = 4096;
byte[] buffer1 = new byte[bufferSize];
byte[] buffer2 = new byte[bufferSize];
while (true)
{
int count1 = stream1.Read(buffer1, 0, bufferSize);
int count2 = stream2.Read(buffer2, 0, bufferSize);
if (count1 != count2)
return false;
if (count1 == 0)
return true;
for (int i = 0; i < count1; i++)
{
if (buffer1[i] != buffer2[i])
return false;
} }
} }
} }

View File

@@ -1,4 +1,3 @@
using System.Text.RegularExpressions;
using BepInEx; using BepInEx;
using HarmonyLib; using HarmonyLib;
@@ -40,43 +39,32 @@ namespace Slang
{ {
public const string PluginGuid = "com.biddydev.slang"; public const string PluginGuid = "com.biddydev.slang";
public const string PluginName = "Slang"; public const string PluginName = "Slang";
public const string PluginVersion = "0.4.4"; public const string PluginVersion = "0.4.6";
private Harmony? _harmony; private static Harmony? _harmony;
private static Regex? _slangSourceCheck = null; public void Awake()
private static Regex SlangSourceCheck
{
get
{
if (_slangSourceCheck is null)
{
_slangSourceCheck = new Regex(@"[;{}()]|\b(let|fn|device)\b|\/\/");
}
return _slangSourceCheck;
}
}
public static bool IsSlangSource(ref string input)
{
return SlangSourceCheck.IsMatch(input);
}
private void Awake()
{ {
L.SetLogger(Logger); L.SetLogger(Logger);
this._harmony = new Harmony(PluginGuid); _harmony = new Harmony(PluginGuid);
// If we failed to load the compiler, bail from the rest of the patches. It won't matter, // If we failed to load the compiler, bail from the rest of the patches. It won't matter,
// as the compiler itself has failed to load. // as the compiler itself has failed to load.
if (!Marshal.Init()) if (!Marshal.Init())
{ {
L.Error("Marshal failed to init");
return; return;
} }
this._harmony.PatchAll(); _harmony.PatchAll();
L.Debug("Ran Harmony patches");
}
public void OnDestroy()
{
Marshal.Destroy();
_harmony?.UnpatchSelf();
L.Debug("Cleaned up Harmony patches");
} }
} }
} }

View File

@@ -930,7 +930,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
[[package]] [[package]]
name = "slang" name = "slang"
version = "0.4.3" version = "0.4.6"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "slang" name = "slang"
version = "0.4.3" version = "0.4.6"
edition = "2021" edition = "2021"
[workspace] [workspace]

View File

@@ -168,3 +168,28 @@ fn test_const_hash_expr() -> anyhow::Result<()> {
); );
Ok(()) Ok(())
} }
#[test]
fn test_declaration_is_const() -> anyhow::Result<()> {
let compiled = compile! {
debug
r#"
const MAX = 100;
let max = MAX;
"#
};
assert_eq!(
compiled,
indoc! {
"
j main
main:
move r8 100
"
}
);
Ok(())
}

View File

@@ -714,7 +714,12 @@ impl<'a> Compiler<'a> {
Operand::Register(VariableScope::TEMP_STACK_REGISTER) Operand::Register(VariableScope::TEMP_STACK_REGISTER)
} }
VariableLocation::Constant(_) | VariableLocation::Device(_) => unreachable!(), VariableLocation::Constant(Literal::Number(num)) => Operand::Number(num.into()),
VariableLocation::Constant(Literal::Boolean(b)) => {
Operand::Number(Number::from(b).into())
}
VariableLocation::Device(_)
| VariableLocation::Constant(Literal::String(_)) => unreachable!(),
}; };
self.emit_variable_assignment(&var_loc, src)?; self.emit_variable_assignment(&var_loc, src)?;
(var_loc, None) (var_loc, None)