Compare commits
5 Commits
effef64add
...
0.4.5
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ca6f97db1 | |||
|
34817ee111
|
|||
| 9eef8a77b6 | |||
|
de31851153
|
|||
|
3543b87561
|
11
Changelog.md
11
Changelog.md
@@ -1,5 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
[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]
|
||||
|
||||
- Added Stationpedia docs back after removing all harmony patches from the mod
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<Name>Slang</Name>
|
||||
<Author>JoeDiertay</Author>
|
||||
<Version>0.4.4</Version>
|
||||
<Version>0.4.5</Version>
|
||||
<Description>
|
||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using StationeersIC10Editor;
|
||||
using StationeersIC10Editor.IC10;
|
||||
|
||||
public class SlangFormatter : ICodeFormatter
|
||||
{
|
||||
@@ -14,8 +13,25 @@ public class SlangFormatter : ICodeFormatter
|
||||
private CancellationTokenSource? _lspCancellationToken;
|
||||
private object _tokenLock = new();
|
||||
|
||||
protected Editor? Ic10Editor = null;
|
||||
private IC10CodeFormatter iC10CodeFormatter = new IC10CodeFormatter();
|
||||
protected Editor? __Ic10Editor = null;
|
||||
|
||||
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 List<SourceMapEntry> ic10SourceMap = new();
|
||||
|
||||
@@ -80,7 +96,7 @@ public class SlangFormatter : ICodeFormatter
|
||||
{
|
||||
if (!Marshal.CompileFromString(RawText, out var compilationResult, out var sourceMap))
|
||||
{
|
||||
return "Compilation Error";
|
||||
return "# Compilation Error";
|
||||
}
|
||||
|
||||
return compilationResult + $"\n{EncodeSource(RawText, SLANG_SRC)}";
|
||||
@@ -118,6 +134,10 @@ public class SlangFormatter : ICodeFormatter
|
||||
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()
|
||||
{
|
||||
CancellationToken token;
|
||||
@@ -133,6 +153,11 @@ public class SlangFormatter : ICodeFormatter
|
||||
_ = 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)
|
||||
{
|
||||
try
|
||||
@@ -165,8 +190,7 @@ public class SlangFormatter : ICodeFormatter
|
||||
return;
|
||||
}
|
||||
|
||||
var (compilationSuccess, compiled, sourceMap) = await Task.Run(
|
||||
() =>
|
||||
var (compilationSuccess, compiled, sourceMap) = await Task.Run(() =>
|
||||
{
|
||||
var successful = Marshal.CompileFromString(
|
||||
inputSrc,
|
||||
@@ -174,15 +198,13 @@ public class SlangFormatter : ICodeFormatter
|
||||
out var sourceMap
|
||||
);
|
||||
return (successful, compiled, sourceMap);
|
||||
},
|
||||
cancellationToken
|
||||
);
|
||||
});
|
||||
|
||||
if (compilationSuccess)
|
||||
{
|
||||
ic10CompilationResult = compiled;
|
||||
ic10SourceMap = sourceMap;
|
||||
UpdateIc10Formatter();
|
||||
UpdateIc10Content(Ic10Editor);
|
||||
}
|
||||
}
|
||||
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()
|
||||
{
|
||||
var tab = Editor.ParentTab;
|
||||
if (Ic10Editor == null)
|
||||
{
|
||||
iC10CodeFormatter = new IC10CodeFormatter();
|
||||
Ic10Editor = new Editor(Editor.KeyHandler);
|
||||
Ic10Editor.IsReadOnly = true;
|
||||
iC10CodeFormatter.Editor = Ic10Editor;
|
||||
}
|
||||
|
||||
if (tab.Editors.Count < 2)
|
||||
{
|
||||
tab.AddEditor(Ic10Editor);
|
||||
}
|
||||
|
||||
// Bail if our backing field is null. We don't want to set the field in this function. It
|
||||
// runs way too much and we might not even have source code to use.
|
||||
if (__Ic10Editor == null)
|
||||
return;
|
||||
var caretPos = Editor.CaretPos.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
|
||||
);
|
||||
|
||||
// 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);
|
||||
|
||||
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)
|
||||
{
|
||||
HashSet<uint> linesToRefresh;
|
||||
|
||||
@@ -67,7 +67,9 @@ public static class Marshal
|
||||
try
|
||||
{
|
||||
_libraryHandle = LoadLibrary(ExtractNativeLibrary(Ffi.RustLib));
|
||||
L.Debug("Rust DLL loaded successfully. Enjoy native speed compilations!");
|
||||
CodeFormatters.RegisterFormatter("Slang", typeof(SlangFormatter), true);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -91,8 +93,13 @@ public static class Marshal
|
||||
|
||||
try
|
||||
{
|
||||
FreeLibrary(_libraryHandle);
|
||||
CodeFormatters.RegisterFormatter("Slang", typeof(PlainTextFormatter), true);
|
||||
if (!FreeLibrary(_libraryHandle))
|
||||
{
|
||||
L.Warning("Unable to free Rust library");
|
||||
}
|
||||
_libraryHandle = IntPtr.Zero;
|
||||
L.Debug("Rust DLL library freed");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -191,9 +198,9 @@ public static class Marshal
|
||||
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
using (Stream stream = assembly.GetManifestResourceStream(libName))
|
||||
using (Stream resourceStream = assembly.GetManifestResourceStream(libName))
|
||||
{
|
||||
if (stream == null)
|
||||
if (resourceStream == null)
|
||||
{
|
||||
L.Error(
|
||||
$"{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 "";
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create))
|
||||
{
|
||||
stream.CopyTo(fileStream);
|
||||
resourceStream.CopyTo(fileStream);
|
||||
}
|
||||
return destinationPath;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
L.Warning($"Could not overwrite {libName} (it might be in use): {e.Message}");
|
||||
return "";
|
||||
// If we fail here, the file is likely locked.
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using BepInEx;
|
||||
using HarmonyLib;
|
||||
|
||||
@@ -40,43 +39,32 @@ namespace Slang
|
||||
{
|
||||
public const string PluginGuid = "com.biddydev.slang";
|
||||
public const string PluginName = "Slang";
|
||||
public const string PluginVersion = "0.4.4";
|
||||
public const string PluginVersion = "0.4.5";
|
||||
|
||||
private Harmony? _harmony;
|
||||
private static Harmony? _harmony;
|
||||
|
||||
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 bool IsSlangSource(ref string input)
|
||||
{
|
||||
return SlangSourceCheck.IsMatch(input);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
public void Awake()
|
||||
{
|
||||
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,
|
||||
// as the compiler itself has failed to load.
|
||||
if (!Marshal.Init())
|
||||
{
|
||||
L.Error("Marshal failed to init");
|
||||
return;
|
||||
}
|
||||
|
||||
this._harmony.PatchAll();
|
||||
_harmony.PatchAll();
|
||||
L.Debug("Ran Harmony patches");
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
Marshal.Destroy();
|
||||
_harmony?.UnpatchSelf();
|
||||
L.Debug("Cleaned up Harmony patches");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
2
rust_compiler/Cargo.lock
generated
2
rust_compiler/Cargo.lock
generated
@@ -930,7 +930,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
||||
|
||||
[[package]]
|
||||
name = "slang"
|
||||
version = "0.4.3"
|
||||
version = "0.4.5"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "slang"
|
||||
version = "0.4.3"
|
||||
version = "0.4.5"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
|
||||
Reference in New Issue
Block a user