Start bindings and interface implementation for the IC10Editor mod

This commit is contained in:
2025-11-28 01:14:54 -07:00
parent 2e7dc4fd91
commit 611f0f6f71
6 changed files with 79 additions and 15 deletions

View File

@@ -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";
/// <summary>Takes raw `Slang` source code and compiles it into IC10</summary>
[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)
/// <summary>Take original slang source code and copies it to a file
/// for use in restoring later.
/// </summary>
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}");
}
}
}