111 lines
2.9 KiB
C#
111 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using BepInEx;
|
|
using HarmonyLib;
|
|
using LaunchPadBooster;
|
|
|
|
namespace Slang
|
|
{
|
|
class L
|
|
{
|
|
private static BepInEx.Logging.ManualLogSource? _logger;
|
|
|
|
public static void SetLogger(BepInEx.Logging.ManualLogSource logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public static void Debug(string message)
|
|
{
|
|
_logger?.LogDebug(message);
|
|
}
|
|
|
|
public static void Info(string message)
|
|
{
|
|
_logger?.LogInfo(message);
|
|
}
|
|
|
|
public static void Error(string message)
|
|
{
|
|
_logger?.LogError(message);
|
|
}
|
|
|
|
public static void Warning(string message)
|
|
{
|
|
_logger?.LogWarning(message);
|
|
}
|
|
}
|
|
|
|
[BepInPlugin(PluginGuid, PluginName, "0.1.0")]
|
|
[BepInDependency(StationeersIC10Editor.IC10EditorPlugin.PluginGuid)]
|
|
public class SlangPlugin : BaseUnityPlugin
|
|
{
|
|
public const string PluginGuid = "com.biddydev.slang";
|
|
public const string PluginName = "Slang";
|
|
|
|
public static Mod MOD = new Mod(PluginName, "0.1.0");
|
|
|
|
private 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Encodes the original slang source code as base64 and uses gzip to compress it, returning the resulting string.
|
|
/// </summary>
|
|
public static string EncodeSource(string source)
|
|
{
|
|
if (string.IsNullOrEmpty(source))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
byte[] bytes = Encoding.UTF8.GetBytes(source);
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
|
|
{
|
|
gzipStream.Write(bytes, 0, bytes.Length);
|
|
}
|
|
return Convert.ToBase64String(memoryStream.ToArray());
|
|
}
|
|
}
|
|
|
|
public static bool IsSlangSource(ref string input)
|
|
{
|
|
return SlangSourceCheck.IsMatch(input);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
L.SetLogger(Logger);
|
|
this._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())
|
|
{
|
|
return;
|
|
}
|
|
|
|
this._harmony.PatchAll();
|
|
}
|
|
}
|
|
}
|