Browse Source

Workaround file locking Write exceptions

tags/v4.0.0
NGnius (Graham) 3 years ago
parent
commit
ccaa7be436
6 changed files with 211 additions and 8 deletions
  1. +1
    -1
      IPA/IPA.csproj
  2. +152
    -0
      IPA/Patcher/FileUtil.cs
  3. +7
    -2
      IPA/Patcher/Patcher.cs
  4. +7
    -2
      IPA/Patcher/Virtualizer.cs
  5. +42
    -1
      IPA/Program.cs
  6. +2
    -2
      IllusionPlugin/IEnhancedPlugin.cs

+ 1
- 1
IPA/IPA.csproj View File

@@ -62,7 +62,7 @@
<Target Name="AfterBuild">
</Target>
-->
<Target Name="AfterBuild">
<Target Name="Custom1AfterBuild" AfterTargets="Build">
<Message Text="Packing..." Importance="normal" />
<ItemGroup>
<Launcher Include="$(SolutionDir)Launcher\$(OutDir)Launcher.exe" />


+ 152
- 0
IPA/Patcher/FileUtil.cs View File

@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace IPA.Patcher
{
static public class FileUtil
{
[StructLayout(LayoutKind.Sequential)]
struct RM_UNIQUE_PROCESS
{
public int dwProcessId;
public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
}

const int RmRebootReasonNone = 0;
const int CCH_RM_MAX_APP_NAME = 255;
const int CCH_RM_MAX_SVC_NAME = 63;

enum RM_APP_TYPE
{
RmUnknownApp = 0,
RmMainWindow = 1,
RmOtherWindow = 2,
RmService = 3,
RmExplorer = 4,
RmConsole = 5,
RmCritical = 1000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct RM_PROCESS_INFO
{
public RM_UNIQUE_PROCESS Process;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)]
public string strAppName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
public string strServiceShortName;

public RM_APP_TYPE ApplicationType;
public uint AppStatus;
public uint TSSessionId;
[MarshalAs(UnmanagedType.Bool)] public bool bRestartable;
}

[DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
static extern int RmRegisterResources(uint pSessionHandle,
UInt32 nFiles,
string[] rgsFilenames,
UInt32 nApplications,
[In] RM_UNIQUE_PROCESS[] rgApplications,
UInt32 nServices,
string[] rgsServiceNames);

[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)]
static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);

[DllImport("rstrtmgr.dll")]
static extern int RmEndSession(uint pSessionHandle);

[DllImport("rstrtmgr.dll")]
static extern int RmGetList(uint dwSessionHandle,
out uint pnProcInfoNeeded,
ref uint pnProcInfo,
[In, Out] RM_PROCESS_INFO[] rgAffectedApps,
ref uint lpdwRebootReasons);

/// <summary>
/// Find out what process(es) have a lock on the specified file.
/// </summary>
/// <param name="path">Path of the file.</param>
/// <returns>Processes locking the file</returns>
/// <remarks>See also:
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373661(v=vs.85).aspx
/// http://wyupdate.googlecode.com/svn-history/r401/trunk/frmFilesInUse.cs (no copyright in code at time of viewing)
///
/// </remarks>
static public List<Process> WhoIsLocking(string path)
{
uint handle;
string key = Guid.NewGuid().ToString();
List<Process> processes = new List<Process>();

int res = RmStartSession(out handle, 0, key);

if (res != 0)
throw new Exception("Could not begin restart session. Unable to determine file locker.");

try
{
const int ERROR_MORE_DATA = 234;
uint pnProcInfoNeeded = 0,
pnProcInfo = 0,
lpdwRebootReasons = RmRebootReasonNone;

string[] resources = new string[] {path}; // Just checking on one resource.

res = RmRegisterResources(handle, (uint) resources.Length, resources, 0, null, 0, null);

if (res != 0)
throw new Exception("Could not register resource.");

//Note: there's a race condition here -- the first call to RmGetList() returns
// the total number of process. However, when we call RmGetList() again to get
// the actual processes this number may have increased.
res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);

if (res == ERROR_MORE_DATA)
{
// Create an array to store the process results
RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
pnProcInfo = pnProcInfoNeeded;

// Get the list
res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);

if (res == 0)
{
processes = new List<Process>((int) pnProcInfo);

// Enumerate all of the results and add them to the
// list to be returned
for (int i = 0; i < pnProcInfo; i++)
{
try
{
processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
}
// catch the error -- in case the process is no longer running
catch (ArgumentException)
{
}
}
}
else
throw new Exception("Could not list processes locking resource.");
}
else if (res != 0)
throw new Exception("Could not list processes locking resource. Failed to get size of result.");
}
finally
{
RmEndSession(handle);
}

return processes;
}
}
}

+ 7
- 2
IPA/Patcher/Patcher.cs View File

@@ -8,7 +8,7 @@ using System.Text;

namespace IPA.Patcher
{
class PatchedModule
class PatchedModule : IDisposable
{
private static readonly string[] ENTRY_TYPES = { "Input", "Display" };

@@ -71,7 +71,7 @@ namespace IPA.Patcher
if(patched > 0)
{
_Module.Write(_File.FullName);
_Module.Write(_File.FullName+"2");
} else
{
throw new Exception("Could not find any entry type!");
@@ -95,5 +95,10 @@ namespace IPA.Patcher
{
return _Module.GetTypes().Where(m => ENTRY_TYPES.Contains(m.Name));
}

public void Dispose()
{
_Module?.Dispose();
}
}
}

+ 7
- 2
IPA/Patcher/Virtualizer.cs View File

@@ -7,7 +7,7 @@ using System.Text;

namespace IPA.Patcher
{
class VirtualizedModule
class VirtualizedModule : IDisposable
{
private const string ENTRY_TYPE = "Display";

@@ -50,7 +50,7 @@ namespace IPA.Patcher
VirtualizeType(type);
}

_Module.Write(_File.FullName);
_Module.Write(_File.FullName+"2");
}

private void VirtualizeType(TypeDefinition type)
@@ -111,5 +111,10 @@ namespace IPA.Patcher
return ((float)awakeMethods.Count(m => m.IsVirtual) / awakeMethods.Count()) > 0.5f;
}
}

public void Dispose()
{
_Module?.Dispose();
}
}
}

+ 42
- 1
IPA/Program.cs View File

@@ -91,9 +91,21 @@ namespace IPA
{
Console.Write("Patching UnityEngine.dll... ");
backup.Add(context.EngineFile);
patchedModule.Patch();
try
{
patchedModule.Patch();
}
catch (Exception)
{
Console.WriteLine($"Processes that are locking {context.EngineFile}:");
foreach (Process proc in FileUtil.WhoIsLocking(context.EngineFile))
{
Console.WriteLine(proc.ProcessName);
}
}
Console.WriteLine("Done!");
}
patchedModule.Dispose();

// Virtualizing
if (File.Exists(context.AssemblyFile))
@@ -106,6 +118,7 @@ namespace IPA
virtualizedModule.Virtualize();
Console.WriteLine("Done!");
}
virtualizedModule.Dispose();
}

// Creating shortcut
@@ -129,6 +142,34 @@ namespace IPA
Console.Error.WriteLine("Failed to create shortcut, but game was patched! Exception: " + e);
}
}
// copy back
try
{
Console.WriteLine("Copying patched files back to correct locations");
var engineFile = new FileInfo(context.EngineFile + "2");
engineFile.CopyTo(context.EngineFile, true);
engineFile.Delete();
var assemblyFile = new FileInfo(context.AssemblyFile + "2");
assemblyFile.CopyTo(context.AssemblyFile, true);
assemblyFile.Delete();
Console.WriteLine("Copied");
}
catch (Exception)
{
// Check stuff
Console.WriteLine($"Processes that are locking {context.EngineFile}:");
foreach (Process proc in FileUtil.WhoIsLocking(context.EngineFile))
{
Console.WriteLine(proc.ProcessName);
}

Console.WriteLine($"Processes that are locking {context.AssemblyFile}:");
foreach (Process proc in FileUtil.WhoIsLocking(context.AssemblyFile))
{
Console.WriteLine(proc.ProcessName);
}
Console.WriteLine("Copy Back failed");
}
}
catch (Exception e)
{


+ 2
- 2
IllusionPlugin/IEnhancedPlugin.cs View File

@@ -31,7 +31,7 @@ namespace IllusionPlugin

public void OnUpdate()
{
Debug.LogWarning("IEnhanced Test Warning OnUpdate");
}

public void OnFixedUpdate()
@@ -156,7 +156,7 @@ namespace IllusionPlugin

public void OnGUI()
{
Debug.LogWarning("IEnhancedPlugin Test Warning OnGUI");
}

public void OnJointBreak(float breakForce)


Loading…
Cancel
Save