Skip to content

Commit

Permalink
Merge pull request #1 from Slaynash/master
Browse files Browse the repository at this point in the history
update fork
  • Loading branch information
Bluscream authored Jun 16, 2019
2 parents a5dfe9d + ca5bbbe commit 1674911
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 79 deletions.
6 changes: 5 additions & 1 deletion Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using UnityEngine;
using Windows;

namespace VRCModLoader
{
Expand All @@ -14,10 +15,13 @@ class Bootstrapper : MonoBehaviour

void Awake()
{
VRCModLogger.Init();
VRCModLogger.Log("[VRCModLoader] Logger Initialised");
if (Environment.CommandLine.Contains("--verbose") || ModPrefs.GetBool("vrctools", "enabledebugconsole", false))
{
VRCModLogger.consoleEnabled = true;
Windows.GuiConsole.CreateConsole();
GuiConsole.CreateConsole();
VRCModLogger.Log("[VRCModLoader] Bootstrapper created");
}
if (Environment.CommandLine.Contains("--nomodloader"))
{
Expand Down
85 changes: 25 additions & 60 deletions ConsoleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,29 @@ namespace Windows
{

class GuiConsole
{
public static void CreateConsole()
{
if (hasConsole)
return;
if (oldOut == IntPtr.Zero)
oldOut = GetStdHandle( -11 );
if (! AllocConsole())
throw new Exception("AllocConsole() failed");
conOut = CreateFile( "CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero );
if (! SetStdHandle(-11, conOut))
throw new Exception("SetStdHandle() failed");
StreamToConsole();
hasConsole = true;
}
public static void ReleaseConsole()
{
if (! hasConsole)
return;
if (! CloseHandle(conOut))
throw new Exception("CloseHandle() failed");
conOut = IntPtr.Zero;
if (! FreeConsole())
throw new Exception("FreeConsole() failed");
if (! SetStdHandle(-11, oldOut))
throw new Exception("SetStdHandle() failed");
StreamToConsole();
hasConsole = false;
}
private static void StreamToConsole()
{
Stream cstm = Console.OpenStandardOutput();
StreamWriter cstw = new StreamWriter( cstm, Encoding.Default );
cstw.AutoFlush = true;
Console.SetOut( cstw );
Console.SetError( cstw );
}
private static bool hasConsole = false;
private static IntPtr conOut;
private static IntPtr oldOut;
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError=false)]
private static extern bool FreeConsole();
[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr GetStdHandle( int nStdHandle );
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr CreateFile(
string fileName,
int desiredAccess,
int shareMode,
IntPtr securityAttributes,
int creationDisposition,
int flagsAndAttributes,
IntPtr templateFile );
[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]
private static extern bool CloseHandle(IntPtr handle);
}
{
[DllImport("kernel32.dll")]
private static extern int AllocConsole();

[DllImport("user32.dll")]
[return: MarshalAs(2)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

private static void ShowConsole()
{
SetForegroundWindow(GetConsoleWindow());
}

internal static void CreateConsole()
{
AllocConsole();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
Console.SetIn(new StreamReader(Console.OpenStandardInput()));
Console.Clear();
ShowConsole();
}
}
}
9 changes: 8 additions & 1 deletion Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public static void Inject()

private static void Bootstrapper_Destroyed()
{
ModComponent.Create();
try
{
ModComponent.Create();
}
catch(Exception e)
{
VRCModLogger.LogError(e.ToString());
}
}
}
}
1 change: 1 addition & 0 deletions ModComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void OnApplicationQuit()
void OnLevelWasLoaded(int level)
{
VRCModLogger.Log("[ModComponent] OnLevelWasLoaded called (" + level + ")");
transform.SetAsLastSibling();
if (level == 0) StartCoroutine(VRCToolsUpdater.UpdateAndRebootIfRequired());
if (mods != null) mods.OnLevelWasLoaded(level);
freshlyLoaded = true;
Expand Down
14 changes: 12 additions & 2 deletions ModManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -103,7 +103,17 @@ private static void LoadMods()
{
string newPath = tmpmodDirectory + s.Substring(modDirectory.Length);
VRCModLogger.Log("Copying " + s + " to " + newPath);
File.Copy(s, newPath);
try {
File.Copy(s, newPath);
} catch (System.UnauthorizedAccessException ex) {
System.Threading.Mutex m = new System.Threading.Mutex(false, "VRChat");
if (m.WaitOne(1, false) == true)
{
VRCModLogger.LogError(ex.ToString());
return;
}
VRCModLogger.Log($"Unable to copy \"{s}\" to temporary directory because the game is already running, trying to continue...");
}
LoadModsFromFile(newPath, exeName);
}

Expand Down
20 changes: 6 additions & 14 deletions VRCModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
<Reference Include="Blake2Sharp">
<HintPath>..\..\..\..\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\Blake2Sharp.dll</HintPath>
</Reference>
<Reference Include="Copie de Assembly-CSharp">
<HintPath>..\..\..\..\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\Copie de Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="DotZLib">
<HintPath>..\..\..\..\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\DotZLib.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -96,7 +93,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\jeux\steam\SteamApps\common\VRChat\VRChat_Data\Managed\UnityEngine.UI.dll</HintPath>
Expand All @@ -116,11 +116,7 @@
<Compile Include="ConsoleWindow.cs" />
<Compile Include="IniFile.cs" />
<Compile Include="Injector.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Resources.Designer.cs" />
<Compile Include="VRCMod.cs" />
<Compile Include="ModPrefs.cs" />
<Compile Include="ModComponent.cs" />
Expand All @@ -132,12 +128,8 @@
<Compile Include="VRCModLogger.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="Resources\VRLoader.dll" />
</ItemGroup>
Expand Down
36 changes: 35 additions & 1 deletion VRCModLogger.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
using System;
using System.IO;
using UnityEngine;

namespace VRCModLoader
{
public class VRCModLogger
{
internal static bool consoleEnabled = false;
private static StreamWriter log;

internal static void Init()
{
FileStream fileStream = null;
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo;

string logFilePath = Path.Combine(Environment.CurrentDirectory, "Logs");
logFilePath = logFilePath + "/VRCModLoader_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff") + ".log";
logFileInfo = new FileInfo(logFilePath);
logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();
if (!logFileInfo.Exists)
{
fileStream = logFileInfo.Create();
}
else
{
fileStream = new FileStream(logFilePath, FileMode.Append);
}
log = new StreamWriter(fileStream);
log.AutoFlush = true;
}

internal static void Stop()
{
log.Close();
}

public static void Log(string s)
{
if(consoleEnabled) Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] [VRCMod] " + s);
Debug.Log("[VRCMod] " + s);
if(log != null) log.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + s);
}

public static void Log(string s, params object[] args)
{
if (consoleEnabled) Console.WriteLine("[VRCMod] " + s, args);
Debug.LogFormat("[VRCMod] " + s, args);
Debug.LogFormat("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] [VRCMod] " + string.Format(s, args));
if (log != null) log.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + string.Format(s, args));
}

public static void LogError(string s)
{
if (consoleEnabled) Console.WriteLine("[VRCMod] [Error] " + s);
Debug.Log("[VRCMod] [Error] " + s);
if (log != null) log.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] [Error] " + s);
}

public static void LogError(string s, params object[] args)
{
if (consoleEnabled) Console.WriteLine("[VRCMod] [Error] " + s, args);
Debug.LogFormat("[VRCMod] [Error] " + s, args);
if (log != null) log.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] [Error] " + string.Format(s, args));
}
}
}

0 comments on commit 1674911

Please sign in to comment.