From ccaa7be436ccc6f44d6408ed8941cc6ecb2e83d8 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 16 Aug 2020 11:45:08 -0400 Subject: [PATCH] Workaround file locking Write exceptions --- IPA/IPA.csproj | 2 +- IPA/Patcher/FileUtil.cs | 152 ++++++++++++++++++++++++++++++ IPA/Patcher/Patcher.cs | 9 +- IPA/Patcher/Virtualizer.cs | 9 +- IPA/Program.cs | 43 ++++++++- IllusionPlugin/IEnhancedPlugin.cs | 4 +- 6 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 IPA/Patcher/FileUtil.cs diff --git a/IPA/IPA.csproj b/IPA/IPA.csproj index 67fdc45..ae1838b 100644 --- a/IPA/IPA.csproj +++ b/IPA/IPA.csproj @@ -62,7 +62,7 @@ --> - + diff --git a/IPA/Patcher/FileUtil.cs b/IPA/Patcher/FileUtil.cs new file mode 100644 index 0000000..739247e --- /dev/null +++ b/IPA/Patcher/FileUtil.cs @@ -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); + + /// + /// Find out what process(es) have a lock on the specified file. + /// + /// Path of the file. + /// Processes locking the file + /// 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) + /// + /// + static public List WhoIsLocking(string path) + { + uint handle; + string key = Guid.NewGuid().ToString(); + List processes = new List(); + + 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((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; + } + } +} \ No newline at end of file diff --git a/IPA/Patcher/Patcher.cs b/IPA/Patcher/Patcher.cs index 0a091b2..3e5faab 100644 --- a/IPA/Patcher/Patcher.cs +++ b/IPA/Patcher/Patcher.cs @@ -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(); + } } } diff --git a/IPA/Patcher/Virtualizer.cs b/IPA/Patcher/Virtualizer.cs index ac131cb..408cc04 100644 --- a/IPA/Patcher/Virtualizer.cs +++ b/IPA/Patcher/Virtualizer.cs @@ -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(); + } } } diff --git a/IPA/Program.cs b/IPA/Program.cs index 06f0906..637bb45 100644 --- a/IPA/Program.cs +++ b/IPA/Program.cs @@ -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) { diff --git a/IllusionPlugin/IEnhancedPlugin.cs b/IllusionPlugin/IEnhancedPlugin.cs index 9bcb4c3..1d16701 100644 --- a/IllusionPlugin/IEnhancedPlugin.cs +++ b/IllusionPlugin/IEnhancedPlugin.cs @@ -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)