Unified the C# mod and the Rust compiler into a monorepo
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,2 +1,5 @@
|
||||
/target
|
||||
target
|
||||
*.ic10
|
||||
release
|
||||
bin
|
||||
obj
|
||||
|
||||
45
build.sh
45
build.sh
@@ -2,21 +2,46 @@
|
||||
|
||||
set -e
|
||||
|
||||
RUST_DIR="rust_compiler"
|
||||
CSHARP_DIR="csharp_mod"
|
||||
RELEASE_DIR="release"
|
||||
|
||||
export RUSTFLAGS="--remap-path-prefix=${PWD}=. --remap-path-prefix=${HOME}/.cargo=~/.cargo"
|
||||
|
||||
# -- Build for Native (Linux x86-64) --
|
||||
echo "Building native (Linux x86-64) executable..."
|
||||
cargo build --release --target=x86_64-unknown-linux-gnu
|
||||
echo "Native build complete."
|
||||
echo "--------------------"
|
||||
cd "$RUST_DIR"
|
||||
echo "Building native Rust binaries and libs"
|
||||
|
||||
# -- Build for Native (Linux x86-64) --
|
||||
cargo build --release --target=x86_64-unknown-linux-gnu
|
||||
|
||||
# -- Build for Windows (x86-64) --
|
||||
echo "Building Windows (x86-64) dll and executable..."
|
||||
cargo build --release --target=x86_64-pc-windows-gnu
|
||||
echo "Windows build successful."
|
||||
|
||||
cd ..
|
||||
echo "--------------------"
|
||||
|
||||
echo "All builds successful"
|
||||
echo "Linux executable at target/x86_64-unknown-linux-gnu/release/slang"
|
||||
echo "Windows .exe at target/x86_64-pc-windows-gnu/release/slang.exe"
|
||||
echo "Windows .dll at target/x86_64-pc-windows-gnu/release/slang.dll"
|
||||
echo "Building C# mod"
|
||||
echo "--------------------"
|
||||
|
||||
cd "$CSHARP_DIR"
|
||||
dotnet build -c Release
|
||||
|
||||
cd ..
|
||||
echo "--------------------"
|
||||
|
||||
echo "Copying Release files to output directory"
|
||||
echo "--------------------"
|
||||
|
||||
RUST_WIN_EXE="$RUST_DIR/target/x86_64-pc-windows-gnu/release/slang.exe"
|
||||
RUST_LINUX_BIN="$RUST_DIR/target/x86_64-unknown-linux-gnu/release/slang"
|
||||
CHARP_DLL="$CSHARP_DIR/bin/Release/net46/StationeersSlang.dll"
|
||||
|
||||
# Check if the release dir exists, if not: create it.
|
||||
if [[ ! -d "$RELEASE_DIR" ]]; then
|
||||
mkdir "$RELEASE_DIR"
|
||||
fi
|
||||
|
||||
cp "$RUST_WIN_EXE" "$RELEASE_DIR/slang.exe"
|
||||
cp "$RUST_LINUX_BIN" "$RELEASE_DIR/slang"
|
||||
cp "$CHARP_DLL" "$RELEASE_DIR/StationeersSlang.dll"
|
||||
|
||||
36
csharp_mod/Patches.cs
Normal file
36
csharp_mod/Patches.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Assets.Scripts.Objects.Motherboards;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace Slang
|
||||
{
|
||||
[HarmonyPatch]
|
||||
public static class SlangPatches
|
||||
{
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChipMotherboard),
|
||||
nameof(ProgrammableChipMotherboard.InputFinished)
|
||||
)]
|
||||
[HarmonyPrefix]
|
||||
public static void PGM_InputFinished(ref string result)
|
||||
{
|
||||
if (string.IsNullOrEmpty(result) || !SlangPlugin.IsSlangSource(ref result))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
L.Info("Detected Slang source, compiling...");
|
||||
|
||||
// Compile the Slang source into IC10
|
||||
string compiled = SlangPlugin.Compile(result);
|
||||
|
||||
// Ensure that the string is correct
|
||||
if (string.IsNullOrEmpty(compiled))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the result to be the compiled source so the rest of the function can continue as normal
|
||||
result = compiled;
|
||||
}
|
||||
}
|
||||
}
|
||||
119
csharp_mod/SlangPlugin.cs
Normal file
119
csharp_mod/SlangPlugin.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using BepInEx;
|
||||
using HarmonyLib;
|
||||
|
||||
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")]
|
||||
public class SlangPlugin : BaseUnityPlugin
|
||||
{
|
||||
public const string PluginGuid = "com.dbidwell94.slang";
|
||||
public const string PluginName = "Slang";
|
||||
|
||||
const string RUST_DLL_NAME = "slang.dll";
|
||||
|
||||
private readonly string[] SLANG_KEYWORDS = { "let ", "fn " };
|
||||
|
||||
/// <summary>Takes raw `Slang` source code and compiles it into IC10</summary>
|
||||
[DllImport(RUST_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern IntPtr compile_from_string(string input);
|
||||
|
||||
/// <summary>Frees memory that was allocated by the FFI call to `compile_from_string`</summary>
|
||||
[DllImport(RUST_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
|
||||
private static extern void free_slang_string(IntPtr ptr);
|
||||
|
||||
public static string Compile(string source)
|
||||
{
|
||||
if (string.IsNullOrEmpty(source))
|
||||
return "";
|
||||
|
||||
IntPtr ptr = compile_from_string(source);
|
||||
try
|
||||
{
|
||||
return Marshal.PtrToStringAnsi(ptr);
|
||||
}
|
||||
finally
|
||||
{
|
||||
free_slang_string(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsSlangSource(ref string input)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
L.SetLogger(Logger);
|
||||
ExtractNativeDll(RUST_DLL_NAME);
|
||||
var harmony = new Harmony("com.dbidwell94.slang");
|
||||
harmony.PatchAll();
|
||||
}
|
||||
|
||||
private void ExtractNativeDll(string fileName)
|
||||
{
|
||||
string destinationPath = Path.Combine(Path.GetDirectoryName(Info.Location), fileName);
|
||||
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
using (Stream stream = assembly.GetManifestResourceStream(fileName))
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
Logger.LogError(
|
||||
$"{RUST_DLL_NAME} compiler not found. This means it was not embedded in the mod. Please contact the mod author!"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create))
|
||||
{
|
||||
stream.CopyTo(fileStream);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Logger.LogWarning(
|
||||
$"Could not overwrite {fileName} (it might be in use): {e.Message}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
csharp_mod/stationeersSlang.csproj
Normal file
57
csharp_mod/stationeersSlang.csproj
Normal file
@@ -0,0 +1,57 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net46</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<AssemblyName>StationeersSlang</AssemblyName>
|
||||
<Description>Slang Compiler Bridge</Description>
|
||||
<Version>0.1.0</Version>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<GameDir>/home/dbidwell/.local/share/Steam/steamapps/common/Stationeers/</GameDir>
|
||||
<ManagedDir>$(GameDir)/rocketstation_Data/Managed</ManagedDir>
|
||||
<BepInExDir>$(GameDir)/BepInEx/core</BepInExDir>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="netstandard">
|
||||
<HintPath>$(ManagedDir)/netstandard.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="BepInEx">
|
||||
<HintPath>$(BepInExDir)/BepInEx.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="0Harmony">
|
||||
<HintPath>$(BepInExDir)/0Harmony.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
|
||||
<Reference Include="UnityEngine">
|
||||
<HintPath>$(ManagedDir)/UnityEngine.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.CoreModule">
|
||||
<HintPath>$(ManagedDir)/UnityEngine.CoreModule.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Assembly-CSharp">
|
||||
<HintPath>$(ManagedDir)/Assembly-CSharp.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Assembly-CSharp-firstpass">
|
||||
<HintPath>$(ManagedDir)/Assembly-CSharp-firstpass.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="../rust_compiler/target/x86_64-pc-windows-gnu/release/slang.dll">
|
||||
<LogicalName>slang.dll</LogicalName>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
0
Cargo.lock → rust_compiler/Cargo.lock
generated
0
Cargo.lock → rust_compiler/Cargo.lock
generated
@@ -4,6 +4,7 @@ mod test;
|
||||
pub mod sys_call;
|
||||
pub mod tree_node;
|
||||
|
||||
use crate::sys_call::System;
|
||||
use quick_error::quick_error;
|
||||
use std::io::SeekFrom;
|
||||
use sys_call::SysCall;
|
||||
@@ -13,8 +14,6 @@ use tokenizer::{
|
||||
};
|
||||
use tree_node::*;
|
||||
|
||||
use crate::sys_call::System;
|
||||
|
||||
#[macro_export]
|
||||
/// A macro to create a boxed value.
|
||||
macro_rules! boxed {
|
||||
Reference in New Issue
Block a user