Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
</packageSources>
</configuration>
76 changes: 33 additions & 43 deletions ScriptLoader/ScriptLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,57 @@
using System.Security.Cryptography;
using System.Text;
using BepInEx;
using BepInEx.IL2CPP;
using BepInEx.Logging;

namespace ScriptLoader
{
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.2.4")]
public class ScriptLoader : BaseUnityPlugin
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.3.0")]
public class ScriptLoader : BasePlugin
{
private readonly string scriptsPath = Path.Combine(Paths.GameRootPath, "scripts");
private Dictionary<string, ScriptInfo> availableScripts = new Dictionary<string, ScriptInfo>();
private FileSystemWatcher fileSystemWatcher;
private Assembly lastCompilationAssembly;
private string lastCompilationHash;
private LoggerTextWriter loggerTextWriter;
private bool shouldRecompile;

private void Awake()
private LogTextWriter LogTextWriter;
public override bool Unload()
{
fileSystemWatcher.EnableRaisingEvents = false;
fileSystemWatcher.Dispose();
return base.Unload();
}
public override void Load()
{
DontDestroyOnLoad(this);
loggerTextWriter = new LoggerTextWriter(Logger);
LogTextWriter = new LogTextWriter(Log);
CompileScripts();

fileSystemWatcher = new FileSystemWatcher(scriptsPath);
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
fileSystemWatcher.Filter = "*.cs";
fileSystemWatcher.Changed += (sender, args) =>
{
Logger.LogInfo($"File {Path.GetFileName(args.Name)} changed. Recompiling...");
shouldRecompile = true;
Log.LogInfo("File " + Path.GetFileName(args.Name) + " changed. Recompiling...");
CompileScripts();
};
fileSystemWatcher.Deleted += (sender, args) =>
{
Logger.LogInfo($"File {Path.GetFileName(args.Name)} removed. Recompiling...");
shouldRecompile = true;
Log.LogInfo("File " + Path.GetFileName(args.Name) + " removed. Recompiling...");
CompileScripts();
};
fileSystemWatcher.Created += (sender, args) =>
{
Logger.LogInfo($"File {Path.GetFileName(args.Name)} created. Recompiling...");
shouldRecompile = true;
Log.LogInfo("File " + Path.GetFileName(args.Name) + " created. Recompiling...");
CompileScripts();
};
fileSystemWatcher.Renamed += (sender, args) =>
{
Logger.LogInfo($"File {Path.GetFileName(args.Name)} renamed. Recompiling...");
shouldRecompile = true;
Log.LogInfo("File " + Path.GetFileName(args.Name) + " renamed. Recompiling...");
CompileScripts();
};
fileSystemWatcher.EnableRaisingEvents = true;
}

private void OnDestroy()
{
fileSystemWatcher.EnableRaisingEvents = false;
fileSystemWatcher.Dispose();
}

private void Update()
{
if (!shouldRecompile)
return;
CompileScripts();
shouldRecompile = false;
}

private void CompileScripts()
{
if (!Directory.Exists(scriptsPath))
Expand Down Expand Up @@ -97,7 +87,7 @@ bool UsesHarmonyWrapper(string scriptFile)
var text = File.ReadAllText(scriptFile);
if (text.Contains("HarmonyWrapper") || text.Contains("BepInEx.Harmony"))
{
Logger.LogError($"Skipping loading `{scriptFile}` because it references outdated HarmonyWrapper and BepInEx.Harmony. To fix this, refer to github.com/denikson/BepInEx.ScriptLoader#upgrading-to-1240");
Log.LogError("Skipping loading `" + scriptFile + "` because it references outdated HarmonyWrapper and BepInEx.Harmony. To fix this, refer to github.com/denikson/BepInEx.ScriptLoader#upgrading-to-1240");
return true;
}
return false;
Expand All @@ -106,8 +96,8 @@ bool UsesHarmonyWrapper(string scriptFile)
var ignores = new HashSet<string>(File.ReadAllLines(ignoresPath).Select(s => s.Trim()));
var scriptsToCompile = files.Where(f => !UsesHarmonyWrapper(f) && IsValidProcess(f) && !ignores.Contains(Path.GetFileName(f))).ToList();

Logger.LogInfo(
$"Found {files.Length} scripts to compile, skipping {files.Length - scriptsToCompile.Count} scripts because of `scriptignores` or process filters");
Log.LogInfo(
"Found " + files.Length + " scripts to compile, skipping " + (files.Length - scriptsToCompile.Count) + " scripts because of `scriptignores` or process filters");

var md5 = MD5.Create();
var scriptDict = new Dictionary<string, byte[]>();
Expand All @@ -123,7 +113,7 @@ bool UsesHarmonyWrapper(string scriptFile)

if (hash == lastCompilationHash)
{
Logger.LogInfo("No changes detected! Skipping compilation!");
Log.LogInfo("No changes detected! Skipping compilation!");
return;
}

Expand All @@ -134,11 +124,11 @@ bool UsesHarmonyWrapper(string scriptFile)
Assembly.LoadFile(infoReference);
}

var ass = MonoCompiler.Compile(scriptDict, loggerTextWriter);
var ass = MonoCompiler.Compile(scriptDict, LogTextWriter);

if (ass == null)
{
Logger.LogError("Skipping loading scripts because of errors above.");
Log.LogError("Skipping loading scripts because of errors above.");
return;
}

Expand All @@ -151,7 +141,7 @@ bool UsesHarmonyWrapper(string scriptFile)
if (method == null)
continue;

Logger.Log(LogLevel.Info, $"Unloading {type.Name}");
Log.Log(LogLevel.Info, "Unloading " + type.Name);
method.Invoke(null, new object[0]);
}

Expand All @@ -166,20 +156,20 @@ bool UsesHarmonyWrapper(string scriptFile)
if (method == null)
continue;

Logger.Log(LogLevel.Info, $"Running {type.Name}");
Log.Log(LogLevel.Info, "Running " + type.Name);
method.Invoke(null, new object[0]);
}
}
}

internal class LoggerTextWriter : TextWriter
internal class LogTextWriter : TextWriter
{
private readonly ManualLogSource logger;
private readonly ManualLogSource Log;
private readonly StringBuilder sb = new StringBuilder();

public LoggerTextWriter(ManualLogSource logger)
public LogTextWriter(ManualLogSource Log)
{
this.logger = logger;
this.Log = Log;
}

public override Encoding Encoding { get; } = Encoding.UTF8;
Expand All @@ -188,7 +178,7 @@ public override void Write(char value)
{
if (value == '\n')
{
logger.Log(LogLevel.Info, sb.ToString());
Log.Log(LogLevel.Info, sb.ToString());
sb.Length = 0;
return;
}
Expand Down
15 changes: 11 additions & 4 deletions ScriptLoader/ScriptLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ScriptLoader</RootNamespace>
<AssemblyName>ScriptLoader</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -22,6 +23,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,11 +32,16 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="BepInEx">
<HintPath>..\lib\BepInEx.dll</HintPath>
<Private>False</Private>
<Reference Include="BepInEx.Core, Version=6.0.0.530, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\BepInEx.Core.dll</HintPath>
</Reference>
<Reference Include="BepInEx.IL2CPP, Version=6.0.0.530, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\BepInEx.IL2CPP.dll</HintPath>
</Reference>
<Reference Include="mcs">
<HintPath>..\lib\mcs.dll</HintPath>
Expand Down
1 change: 0 additions & 1 deletion ScriptLoader/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="ILRepack.Lib.MSBuild.Task" version="2.0.18" targetFramework="net35" />
</packages>
Binary file added lib/BepInEx.Core.dll
Binary file not shown.
Binary file added lib/BepInEx.IL2CPP.dll
Binary file not shown.