initial integration with ic10editor mod
This commit is contained in:
@@ -6,7 +6,12 @@ namespace Slang
|
||||
{
|
||||
public static unsafe class SlangExtensions
|
||||
{
|
||||
// 1. Convert the Rust Byte Vector (Vec_uint8_t) to a C# String
|
||||
/**
|
||||
* <summary>
|
||||
* This is a helper method to convert a Rust struct for a string pointer
|
||||
* into a C# style string.
|
||||
* </summary>
|
||||
*/
|
||||
public static string AsString(this Vec_uint8_t vec)
|
||||
{
|
||||
if (vec.ptr == null || vec.len == UIntPtr.Zero)
|
||||
@@ -20,16 +25,28 @@ namespace Slang
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* <summary>This will free a Rust string struct. Because this is a pointer to a struct, this memory
|
||||
* is managed by Rust, therefor it must be freed by Rust
|
||||
* </summary>
|
||||
*/
|
||||
public static void Drop(this Vec_uint8_t vec)
|
||||
{
|
||||
Ffi.free_string(vec);
|
||||
}
|
||||
|
||||
// 2. Convert Rust Token Vector to C# List
|
||||
/**
|
||||
* <summary>This helper converts a Rust vec to a C# List. This handles freeing the
|
||||
* Rust allocation after the List is created, there is no need to Drop this memory.
|
||||
* </summary>
|
||||
*/
|
||||
public static Line AsList(this Vec_FfiToken_t vec)
|
||||
{
|
||||
L.Info("Converting output into a C# List.");
|
||||
var list = new Line();
|
||||
L.Info("Created new `Line`.");
|
||||
list.Capacity = (int)vec.len;
|
||||
L.Info("Changed `Capacity` to be returned Vec's len");
|
||||
|
||||
var currentPtr = vec.ptr;
|
||||
|
||||
@@ -40,7 +57,6 @@ namespace Slang
|
||||
FfiToken_t token = currentPtr[i];
|
||||
|
||||
var newToken = new Token(token.text.AsString(), token.column);
|
||||
newToken.Error = token.error.AsString();
|
||||
|
||||
list.Add(newToken);
|
||||
}
|
||||
|
||||
@@ -23,10 +23,42 @@ public unsafe partial class Ffi {
|
||||
#if IOS
|
||||
private const string RustLib = "slang.framework/slang";
|
||||
#else
|
||||
private const string RustLib = "slang";
|
||||
public const string RustLib = "slang_compiler.dll";
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <c>&'lt [T]</c> but with a guaranteed <c>#[repr(C)]</c> layout.
|
||||
///
|
||||
/// # C layout (for some given type T)
|
||||
///
|
||||
/// ```c
|
||||
/// typedef struct {
|
||||
/// // Cannot be NULL
|
||||
/// T * ptr;
|
||||
/// size_t len;
|
||||
/// } slice_T;
|
||||
/// ```
|
||||
///
|
||||
/// # Nullable pointer?
|
||||
///
|
||||
/// If you want to support the above typedef, but where the <c>ptr</c> field is
|
||||
/// allowed to be <c>NULL</c> (with the contents of <c>len</c> then being undefined)
|
||||
/// use the <c>Option< slice_ptr<_> ></c> type.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
||||
public unsafe struct slice_ref_uint16_t {
|
||||
/// <summary>
|
||||
/// Pointer to the first element (if any).
|
||||
/// </summary>
|
||||
public UInt16 /*const*/ * ptr;
|
||||
|
||||
/// <summary>
|
||||
/// Element count
|
||||
/// </summary>
|
||||
public UIntPtr len;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
|
||||
/// </summary>
|
||||
@@ -40,9 +72,15 @@ public unsafe struct Vec_uint8_t {
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
/// <summary>
|
||||
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
||||
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
||||
/// This should result in the ability to compile many times without triggering frame drops
|
||||
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
||||
/// </summary>
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
Vec_uint8_t compile_from_string (
|
||||
byte /*const*/ * input);
|
||||
slice_ref_uint16_t input);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 104)]
|
||||
@@ -83,9 +121,15 @@ public unsafe partial class Ffi {
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
/// <summary>
|
||||
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
||||
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
||||
/// This should result in the ability to tokenize many times without triggering frame drops
|
||||
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
||||
/// </summary>
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
Vec_FfiToken_t tokenize_line (
|
||||
byte /*const*/ * input);
|
||||
slice_ref_uint16_t input);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,29 +1,63 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using StationeersIC10Editor;
|
||||
|
||||
namespace Slang
|
||||
{
|
||||
public static class Marshal
|
||||
{
|
||||
public static unsafe Line TokenizeLine(string input)
|
||||
public static unsafe Line TokenizeLine(string source)
|
||||
{
|
||||
if (String.IsNullOrEmpty(input))
|
||||
if (String.IsNullOrEmpty(source))
|
||||
{
|
||||
return new Line();
|
||||
}
|
||||
|
||||
// Make sure the string is a null terminated string
|
||||
if (input[input.Length - 1] != '\0')
|
||||
L.Info("Input string not empty");
|
||||
|
||||
fixed (char* ptrString = source)
|
||||
{
|
||||
input += '\0';
|
||||
L.Info("In `fixed` block.");
|
||||
var input = new slice_ref_uint16_t
|
||||
{
|
||||
ptr = (ushort*)ptrString,
|
||||
len = (UIntPtr)source.Length,
|
||||
};
|
||||
L.Info("Calling tokenize_line");
|
||||
return Ffi.tokenize_line(input).AsList();
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe bool CompileFromString(string inputString, out string compiledString)
|
||||
{
|
||||
if (String.IsNullOrEmpty(inputString))
|
||||
{
|
||||
compiledString = String.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
var strBytes = Encoding.UTF8.GetBytes(input);
|
||||
|
||||
fixed (byte* ptrString = strBytes)
|
||||
fixed (char* ptrString = inputString)
|
||||
{
|
||||
return Ffi.tokenize_line(ptrString).AsList();
|
||||
var input = new slice_ref_uint16_t
|
||||
{
|
||||
ptr = (ushort*)ptrString,
|
||||
len = (UIntPtr)inputString.Length,
|
||||
};
|
||||
|
||||
var result = Ffi.compile_from_string(input);
|
||||
try
|
||||
{
|
||||
if ((ulong)result.len < 1)
|
||||
{
|
||||
compiledString = String.Empty;
|
||||
return false;
|
||||
}
|
||||
compiledString = result.AsString();
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
result.Drop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace Slang
|
||||
}
|
||||
|
||||
[BepInPlugin(PluginGuid, PluginName, "0.1.0")]
|
||||
[BepInDependency(StationeersIC10Editor.IC10EditorPlugin.PluginGuid)]
|
||||
public class SlangPlugin : BaseUnityPlugin
|
||||
{
|
||||
public const string PluginGuid = "com.biddydev.slang";
|
||||
@@ -60,26 +61,15 @@ namespace Slang
|
||||
|
||||
public static unsafe string Compile(string source)
|
||||
{
|
||||
if (string.IsNullOrEmpty(source))
|
||||
return "";
|
||||
|
||||
// Add a null terminator char at the end of the source string (turns into a CStr)
|
||||
source += "\0";
|
||||
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(source);
|
||||
|
||||
// don't move my memory around, C#!
|
||||
fixed (byte* pBytes = bytes)
|
||||
string compiled;
|
||||
if (Marshal.CompileFromString(source, out compiled))
|
||||
{
|
||||
var compiled = Ffi.compile_from_string(pBytes);
|
||||
try
|
||||
{
|
||||
return compiled.AsString();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Ffi.free_string(compiled);
|
||||
}
|
||||
// TODO: handle saving the original source code
|
||||
return compiled;
|
||||
}
|
||||
else
|
||||
{
|
||||
return compiled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,13 +89,16 @@ namespace Slang
|
||||
private void Awake()
|
||||
{
|
||||
L.SetLogger(Logger);
|
||||
ExtractNativeDll("slang.dll");
|
||||
var harmony = new Harmony(PluginGuid);
|
||||
harmony.PatchAll();
|
||||
CodeFormatters.RegisterFormatter("slang", () => new SlangFormatter(), true);
|
||||
|
||||
if (ExtractNativeDll(Ffi.RustLib))
|
||||
{
|
||||
var harmony = new Harmony(PluginGuid);
|
||||
harmony.PatchAll();
|
||||
CodeFormatters.RegisterFormatter("slang", () => new SlangFormatter(), true);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExtractNativeDll(string fileName)
|
||||
private bool ExtractNativeDll(string fileName)
|
||||
{
|
||||
string destinationPath = Path.Combine(Path.GetDirectoryName(Info.Location), fileName);
|
||||
|
||||
@@ -116,9 +109,9 @@ namespace Slang
|
||||
if (stream == null)
|
||||
{
|
||||
L.Error(
|
||||
"slang.dll compiler not found. This means it was not embedded in the mod. Please contact the mod author!"
|
||||
$"{Ffi.RustLib} not found. This means it was not embedded in the mod. Please contact the mod author!"
|
||||
);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
@@ -127,10 +120,12 @@ namespace Slang
|
||||
{
|
||||
stream.CopyTo(fileStream);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
L.Warning($"Could not overwrite {fileName} (it might be in use): {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="../rust_compiler/target/x86_64-pc-windows-gnu/release/slang.dll">
|
||||
<LogicalName>slang.compiler.dll</LogicalName>
|
||||
<LogicalName>slang_compiler.dll</LogicalName>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user