Browse Source

- Added colour logging to the logger

- Rewrote all logging to use the logger
- IPA should look for `Beat Saber.exe` if loaded without having the application dropped on it
pull/46/head
artman41 6 years ago
parent
commit
6f1a8893fa
11 changed files with 63 additions and 43 deletions
  1. +1
    -1
      IPA.Tests/ProgramTest.cs
  2. +1
    -1
      IPA/PatchContext.cs
  3. +10
    -4
      IPA/Program.cs
  4. +7
    -4
      IllusionInjector/CompositePlugin.cs
  5. +3
    -0
      IllusionInjector/IllusionInjector.csproj
  6. +0
    -6
      IllusionInjector/PluginComponent.cs
  7. +11
    -10
      IllusionInjector/PluginManager.cs
  8. +4
    -0
      IllusionPlugin/IllusionPlugin.csproj
  9. +26
    -17
      IllusionPlugin/Logger.cs
  10. BIN
      Libs/UnityEngine.CoreModule.dll
  11. BIN
      Libs/UnityEngine.dll

+ 1
- 1
IPA.Tests/ProgramTest.cs View File

@ -30,7 +30,7 @@ namespace IPA.Tests
public void CopiesCorrectly(string from, string to, string nativeFolder, bool isFlat, string[] expected)
{
var outcome = Program.NativePluginInterceptor(new FileInfo(from), new FileInfo(to), new DirectoryInfo(nativeFolder), isFlat).Select(f => f.FullName).ToList();
var outcome = Program.NativePluginInterceptor(new FileInfo(from), new FileInfo(to), new DirectoryInfo(nativeFolder), isFlat, Program.Architecture.Unknown).Select(f => f.FullName).ToList();
var expectedPaths = expected.Select(e => new FileInfo(e)).Select(f => f.FullName).ToList();
Assert.Equal(expectedPaths, outcome);


+ 1
- 1
IPA/PatchContext.cs View File

@ -52,7 +52,7 @@ namespace IPA
context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll");
context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll");
context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName);
string shortcutName = string.Format("{0} (Patch & Launch)", context.ProjectName);
string shortcutName = $"{context.ProjectName} (Patch & Launch)";
context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk";
Directory.CreateDirectory(context.BackupPath);


+ 10
- 4
IPA/Program.cs View File

@ -21,16 +21,22 @@ namespace IPA
Unknown
}
static void Main(string[] args)
{
static void Main(string[] args) {
PatchContext context;
Console.WriteLine($"{args[0]}");
if(args.Length < 1 || !args[0].EndsWith(".exe"))
{
Fail("Drag an (executable) file on the exe!");
//Fail("Drag an (executable) file on the exe!");
context = PatchContext.Create(new [] {"./Beat Saber.exe"});
}
else {
context = PatchContext.Create(args);
}
try
{
var context = PatchContext.Create(args);
bool isRevert = args.Contains("--revert") || Keyboard.IsKeyDown(Keys.LMenu);
// Sanitizing
Validate(context);


+ 7
- 4
IllusionInjector/CompositePlugin.cs View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using Logger = IllusionPlugin.Logger;
namespace IllusionInjector {
public class CompositePlugin : IPlugin {
@ -11,6 +12,8 @@ namespace IllusionInjector {
private delegate void CompositeCall(IPlugin plugin);
private Logger debugLogger => PluginManager.debugLogger;
public CompositePlugin(IEnumerable<IPlugin> plugins) {
this.plugins = plugins;
}
@ -29,7 +32,7 @@ namespace IllusionInjector {
plugin.OnSceneLoaded(scene, sceneMode);
}
catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
}
}
}
@ -40,7 +43,7 @@ namespace IllusionInjector {
plugin.OnSceneUnloaded(scene);
}
catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
}
}
}
@ -51,7 +54,7 @@ namespace IllusionInjector {
plugin.OnActiveSceneChanged(prevScene, nextScene);
}
catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
}
}
}
@ -63,7 +66,7 @@ namespace IllusionInjector {
callback(plugin);
}
catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
}
}
}


+ 3
- 0
IllusionInjector/IllusionInjector.csproj View File

@ -39,6 +39,9 @@
<HintPath>..\Libs\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bootstrapper.cs" />


+ 0
- 6
IllusionInjector/PluginComponent.cs View File

@ -9,7 +9,6 @@ namespace IllusionInjector
public class PluginComponent : MonoBehaviour
{
private CompositePlugin plugins;
private bool freshlyLoaded = false;
private bool quitting = false;
public static PluginComponent Create()
@ -31,10 +30,6 @@ namespace IllusionInjector
void Update()
{
if (freshlyLoaded)
{
freshlyLoaded = false;
}
plugins.OnUpdate();
}
@ -70,7 +65,6 @@ namespace IllusionInjector
void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{
plugins.OnSceneLoaded(scene, sceneMode);
freshlyLoaded = true;
}
private void OnSceneUnloaded(Scene scene) {


+ 11
- 10
IllusionInjector/PluginManager.cs View File

@ -15,6 +15,8 @@ namespace IllusionInjector
{
private static List<IPlugin> _Plugins = null;
internal static readonly Logger debugLogger = new Logger("IllusionInjector");
/// <summary>
/// Gets the list of loaded plugins and loads them if necessary.
/// </summary>
@ -38,7 +40,7 @@ namespace IllusionInjector
// Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
// so we need to resort to P/Invoke
string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
Console.WriteLine(exeName);
debugLogger.Log(exeName);
_Plugins = new List<IPlugin>();
if (!Directory.Exists(pluginDirectory)) return;
@ -51,16 +53,15 @@ namespace IllusionInjector
// DEBUG
Console.WriteLine("Running on Unity {0}", UnityEngine.Application.unityVersion);
Console.WriteLine("-----------------------------");
Console.WriteLine("Loading plugins from {0} and found {1}", pluginDirectory, _Plugins.Count);
Console.WriteLine("-----------------------------");
debugLogger.Log($"Running on Unity {UnityEngine.Application.unityVersion}");
debugLogger.Log("-----------------------------");
debugLogger.Log($"Loading plugins from {pluginDirectory} and found {_Plugins.Count}");
debugLogger.Log("-----------------------------");
foreach (var plugin in _Plugins)
{
Console.WriteLine(" {0}: {1}", plugin.Name, plugin.Version);
debugLogger.Log($"{plugin.Name}: {plugin.Version}");
}
Console.WriteLine("-----------------------------");
debugLogger.Log("-----------------------------");
}
private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName)
@ -94,7 +95,7 @@ namespace IllusionInjector
}
catch (Exception e)
{
Console.WriteLine("[WARN] Could not load plugin {0} in {1}! {2}", t.FullName, Path.GetFileName(file), e);
debugLogger.Exception($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
}
}
}
@ -102,7 +103,7 @@ namespace IllusionInjector
}
catch (Exception e)
{
Console.WriteLine("[ERROR] Could not load {0}! {1}", Path.GetFileName(file), e);
debugLogger.Error($"Could not load {Path.GetFileName(file)}! {e}");
}
return plugins;


+ 4
- 0
IllusionPlugin/IllusionPlugin.csproj View File

@ -36,11 +36,15 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="IEnhancedPlugin.cs" />
<Compile Include="IniFile.cs" />
<Compile Include="IPlugin.cs" />
<Compile Include="Logger.cs" />
<Compile Include="ModPrefs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>


+ 26
- 17
IllusionPlugin/Logger.cs View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using IllusionPlugin;
@ -11,6 +12,8 @@ namespace IllusionPlugin {
private readonly Thread _watcherThread;
private bool _threadRunning;
private string ModName;
private logMessage oldLog;
struct logMessage {
@ -24,18 +27,18 @@ namespace IllusionPlugin {
}
enum WarningLevel {
Log, Error, Exception
Log, Error, Exception, Warning
}
Logger() {
public Logger(string modName = "Default") {
_logQueue = new Queue<logMessage>();
_logFile = GetPath("Default");
_logFile = GetPath(modName);
_watcherThread = new Thread(QueueWatcher) {IsBackground = true};
_threadRunning = true;
Start();
}
public Logger(IPlugin plugin) {
public Logger(IPlugin plugin) {
_logQueue = new Queue<logMessage>();
_logFile = GetPath(plugin);
_watcherThread = new Thread(QueueWatcher) {IsBackground = true};
@ -45,22 +48,26 @@ namespace IllusionPlugin {
public void Log(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Log));
_logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Log));
}
public void Error(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Error));
_logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Error));
}
public void Exception(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Exception));
_logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Exception));
}
public void Warning(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[WARNING @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Warning));
}
void QueueWatcher() {
_logFile.Create().Close();
SetConsoleColour(WarningLevel.Log);
while (_threadRunning) {
if (_logQueue.Count > 0) {
_watcherThread.IsBackground = false;
@ -70,8 +77,8 @@ namespace IllusionPlugin {
if (d.Message == oldLog.Message) return;
oldLog = d;
f.WriteLine(d.Message);
if(d.WarningLevel != oldLog.WarningLevel) SetConsoleColour(d.WarningLevel);
Console.WriteLine(d);
Console.ForegroundColor = GetConsoleColour(d.WarningLevel);
Console.WriteLine(d.Message);
}
}
@ -87,22 +94,24 @@ namespace IllusionPlugin {
_watcherThread.Join();
}
void SetConsoleColour(WarningLevel level) {
ConsoleColor GetConsoleColour(WarningLevel level) {
switch (level) {
case WarningLevel.Log:
Console.ForegroundColor = ConsoleColor.Green;
break;
return ConsoleColor.Green;
case WarningLevel.Error:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
return ConsoleColor.Yellow;
case WarningLevel.Exception:
Console.ForegroundColor = ConsoleColor.Red;
break;
return ConsoleColor.Red;
case WarningLevel.Warning:
return ConsoleColor.Blue;
default:
return ConsoleColor.Gray;
}
}
FileInfo GetPath(IPlugin plugin) => GetPath(plugin.Name);
FileInfo GetPath(string modName) {
ModName = modName;
var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}");
logsDir.Create();
return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt");


BIN
Libs/UnityEngine.CoreModule.dll View File


BIN
Libs/UnityEngine.dll View File


Loading…
Cancel
Save