Browse Source

Fixed backup restoration in some cases

refactor
Anairkoen Schno 6 years ago
parent
commit
f22d6a1349
3 changed files with 117 additions and 65 deletions
  1. +2
    -2
      IPA/PatchContext.cs
  2. +1
    -0
      IPA/Patcher/BackupManager.cs
  3. +114
    -63
      IPA/Program.cs

+ 2
- 2
IPA/PatchContext.cs View File

@ -39,7 +39,7 @@ namespace IPA
}; };
context.ProjectRoot = new FileInfo(context.Executable).Directory.FullName; context.ProjectRoot = new FileInfo(context.Executable).Directory.FullName;
context.IPARoot = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "IPA"); context.IPARoot = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "IPA");
context.IPA = Assembly.GetExecutingAssembly().Location ?? Path.Combine(context.ProjectRoot, "IPA.exe");
context.IPA = Assembly.GetExecutingAssembly()?.Location ?? Path.Combine(context.ProjectRoot, "IPA.exe");
context.DataPathSrc = Path.Combine(context.IPARoot, "Data"); context.DataPathSrc = Path.Combine(context.IPARoot, "Data");
context.LibsPathSrc = Path.Combine(context.IPARoot, "Libs"); context.LibsPathSrc = Path.Combine(context.IPARoot, "Libs");
context.PluginsFolder = Path.Combine(context.ProjectRoot, "Plugins"); context.PluginsFolder = Path.Combine(context.ProjectRoot, "Plugins");
@ -49,7 +49,7 @@ namespace IPA
context.ManagedPath = Path.Combine(context.DataPathDst, "Managed"); context.ManagedPath = Path.Combine(context.DataPathDst, "Managed");
context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll"); context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll");
context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll"); context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll");
context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName);
context.BackupPath = Path.Combine(context.IPARoot, "Backups", context.ProjectName);
string shortcutName = $"{context.ProjectName} (Patch & Launch)"; string shortcutName = $"{context.ProjectName} (Patch & Launch)";
context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk"; context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk";


+ 1
- 0
IPA/Patcher/BackupManager.cs View File

@ -11,6 +11,7 @@ namespace IPA.Patcher
{ {
public static BackupUnit FindLatestBackup(PatchContext context) public static BackupUnit FindLatestBackup(PatchContext context)
{ {
new DirectoryInfo(context.BackupPath).Create();
return new DirectoryInfo(context.BackupPath) return new DirectoryInfo(context.BackupPath)
.GetDirectories() .GetDirectories()
.OrderByDescending(p => p.Name) .OrderByDescending(p => p.Name)


+ 114
- 63
IPA/Program.cs View File

@ -11,9 +11,12 @@ using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
using IPA.ArgParsing; using IPA.ArgParsing;
namespace IPA {
public class Program {
public enum Architecture {
namespace IPA
{
public class Program
{
public enum Architecture
{
x86, x86,
x64, x64,
Unknown Unknown
@ -21,18 +24,18 @@ namespace IPA {
public static Version Version => Assembly.GetEntryAssembly().GetName().Version; public static Version Version => Assembly.GetEntryAssembly().GetName().Version;
public static ArgumentFlag ArgHelp = new ArgumentFlag("--help", "-h") { DocString = "prints this message" };
public static ArgumentFlag ArgWaitFor = new ArgumentFlag("--waitfor", "-w") { DocString = "waits for the specified PID to exit", ValueString = "PID" };
public static ArgumentFlag ArgForce = new ArgumentFlag("--force", "-f") { DocString = "forces the operation to go through" };
public static ArgumentFlag ArgRevert = new ArgumentFlag("--revert", "-r") { DocString = "reverts the IPA installation" };
public static ArgumentFlag ArgNoWait = new ArgumentFlag("--nowait", "-n") { DocString = "doesn't wait for user input after the operation" };
public static ArgumentFlag ArgStart = new ArgumentFlag("--start", "-s") { DocString = "uses value as arguments to start the game after the patch/unpatch", ValueString = "ARGUMENTS" };
public static ArgumentFlag ArgLaunch = new ArgumentFlag("--launch", "-l") { DocString = "uses positional parameters as arguments to start the game after patch/unpatch" };
public static ArgumentFlag ArgHelp = new ArgumentFlag("--help", "-h") { DocString = "prints this message" };
public static ArgumentFlag ArgWaitFor = new ArgumentFlag("--waitfor", "-w") { DocString = "waits for the specified PID to exit", ValueString = "PID" };
public static ArgumentFlag ArgForce = new ArgumentFlag("--force", "-f") { DocString = "forces the operation to go through" };
public static ArgumentFlag ArgRevert = new ArgumentFlag("--revert", "-r") { DocString = "reverts the IPA installation" };
public static ArgumentFlag ArgNoWait = new ArgumentFlag("--nowait", "-n") { DocString = "doesn't wait for user input after the operation" };
public static ArgumentFlag ArgStart = new ArgumentFlag("--start", "-s") { DocString = "uses value as arguments to start the game after the patch/unpatch", ValueString = "ARGUMENTS" };
public static ArgumentFlag ArgLaunch = new ArgumentFlag("--launch", "-l") { DocString = "uses positional parameters as arguments to start the game after patch/unpatch" };
static void Main(string[] args) static void Main(string[] args)
{ {
Arguments.CmdLine.Flags(ArgHelp, ArgWaitFor, ArgForce, ArgRevert, ArgNoWait, ArgStart, ArgLaunch).Process(); Arguments.CmdLine.Flags(ArgHelp, ArgWaitFor, ArgForce, ArgRevert, ArgNoWait, ArgStart, ArgLaunch).Process();
if (ArgHelp) if (ArgHelp)
{ {
Arguments.CmdLine.PrintHelp(); Arguments.CmdLine.PrintHelp();
@ -57,7 +60,7 @@ namespace IPA {
} }
PatchContext context; PatchContext context;
var argExeName = Arguments.CmdLine.PositionalArgs.FirstOrDefault(s => s.EndsWith(".exe")); var argExeName = Arguments.CmdLine.PositionalArgs.FirstOrDefault(s => s.EndsWith(".exe"));
if (argExeName == null) if (argExeName == null)
context = PatchContext.Create(new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles() context = PatchContext.Create(new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles()
@ -65,7 +68,7 @@ namespace IPA {
.FullName); .FullName);
else else
context = PatchContext.Create(argExeName); context = PatchContext.Create(argExeName);
// Sanitizing // Sanitizing
Validate(context); Validate(context);
@ -77,7 +80,8 @@ namespace IPA {
StartIfNeedBe(context); StartIfNeedBe(context);
} }
} }
catch (Exception e) {
catch (Exception e)
{
Fail(e.Message); Fail(e.Message);
} }
@ -95,32 +99,40 @@ namespace IPA {
} }
} }
private static void Validate(PatchContext c) {
if (!Directory.Exists(c.DataPathDst) || !File.Exists(c.EngineFile)) {
private static void Validate(PatchContext c)
{
if (!Directory.Exists(c.DataPathDst) || !File.Exists(c.EngineFile))
{
Fail("Game does not seem to be a Unity project. Could not find the libraries to patch."); Fail("Game does not seem to be a Unity project. Could not find the libraries to patch.");
Console.WriteLine($"DataPath: {c.DataPathDst}"); Console.WriteLine($"DataPath: {c.DataPathDst}");
Console.WriteLine($"EngineFile: {c.EngineFile}"); Console.WriteLine($"EngineFile: {c.EngineFile}");
} }
} }
private static void Install(PatchContext context) {
try {
private static void Install(PatchContext context)
{
try
{
var backup = new BackupUnit(context); var backup = new BackupUnit(context);
#region Patch Version Check #region Patch Version Check
var patchedModule = PatchedModule.Load(context.EngineFile); var patchedModule = PatchedModule.Load(context.EngineFile);
#if DEBUG
var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) >= 0; var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) >= 0;
#else
var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) > 0;
#endif
Console.WriteLine($"Current: {Version} Patched: {patchedModule.Data.Version}"); Console.WriteLine($"Current: {Version} Patched: {patchedModule.Data.Version}");
if (isCurrentNewer) {
if (isCurrentNewer)
{
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine( Console.WriteLine(
$"Preparing for update, {(patchedModule.Data.Version == null ? "UnPatched" : patchedModule.Data.Version.ToString())} => {Version}"); $"Preparing for update, {(patchedModule.Data.Version == null ? "UnPatched" : patchedModule.Data.Version.ToString())} => {Version}");
Console.WriteLine("--- Starting ---"); Console.WriteLine("--- Starting ---");
Revert(context); Revert(context);
Console.ResetColor(); Console.ResetColor();
#region File Copying #region File Copying
Console.ForegroundColor = ConsoleColor.Magenta; Console.ForegroundColor = ConsoleColor.Magenta;
@ -146,7 +158,8 @@ namespace IPA {
#endregion #endregion
} }
else {
else
{
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Files up to date @ Version {Version}!"); Console.WriteLine($"Files up to date @ Version {Version}!");
Console.ResetColor(); Console.ResetColor();
@ -156,7 +169,8 @@ namespace IPA {
#region Create Plugin Folder #region Create Plugin Folder
if (!Directory.Exists(context.PluginsFolder)) {
if (!Directory.Exists(context.PluginsFolder))
{
Console.ForegroundColor = ConsoleColor.DarkYellow; Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Creating plugins folder... "); Console.WriteLine("Creating plugins folder... ");
Directory.CreateDirectory(context.PluginsFolder); Directory.CreateDirectory(context.PluginsFolder);
@ -167,7 +181,8 @@ namespace IPA {
#region Patching #region Patching
if (!patchedModule.Data.IsPatched || isCurrentNewer) {
if (!patchedModule.Data.IsPatched || isCurrentNewer)
{
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Patching UnityEngine.dll with Version {Application.ProductVersion}... "); Console.WriteLine($"Patching UnityEngine.dll with Version {Application.ProductVersion}... ");
backup.Add(context.EngineFile); backup.Add(context.EngineFile);
@ -180,9 +195,11 @@ namespace IPA {
#region Virtualizing #region Virtualizing
if (File.Exists(context.AssemblyFile)) {
if (File.Exists(context.AssemblyFile))
{
var virtualizedModule = VirtualizedModule.Load(context.AssemblyFile); var virtualizedModule = VirtualizedModule.Load(context.AssemblyFile);
if (!virtualizedModule.IsVirtualized) {
if (!virtualizedModule.IsVirtualized)
{
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Virtualizing Assembly-Csharp.dll... "); Console.WriteLine("Virtualizing Assembly-Csharp.dll... ");
backup.Add(context.AssemblyFile); backup.Add(context.AssemblyFile);
@ -195,10 +212,10 @@ namespace IPA {
#endregion #endregion
#region Creating shortcut #region Creating shortcut
if(!File.Exists(context.ShortcutPath))
if (!File.Exists(context.ShortcutPath))
{ {
Console.ForegroundColor = ConsoleColor.DarkGreen; Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Creating shortcut to IPA ({0})... ", context.IPA);
Console.WriteLine("Creating shortcut to IPA ({0})... ", context.IPA);
try try
{ {
Shortcut.Create( Shortcut.Create(
@ -210,7 +227,8 @@ namespace IPA {
hotkey: "", hotkey: "",
iconPath: context.Executable iconPath: context.Executable
); );
} catch (Exception)
}
catch (Exception)
{ {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Failed to create shortcut, but game was patched!"); Console.Error.WriteLine("Failed to create shortcut, but game was patched!");
@ -227,19 +245,23 @@ namespace IPA {
Console.ResetColor(); Console.ResetColor();
} }
private static void Revert(PatchContext context) {
private static void Revert(PatchContext context)
{
Console.ForegroundColor = ConsoleColor.Cyan; Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("Restoring backup... "); Console.Write("Restoring backup... ");
if (BackupManager.Restore(context)) {
if (BackupManager.Restore(context))
{
Console.WriteLine("Done!"); Console.WriteLine("Done!");
} }
else {
else
{
Console.WriteLine("Already vanilla or you removed your backups!"); Console.WriteLine("Already vanilla or you removed your backups!");
} }
if (File.Exists(context.ShortcutPath)) {
if (File.Exists(context.ShortcutPath))
{
Console.WriteLine("Deleting shortcut..."); Console.WriteLine("Deleting shortcut...");
File.Delete(context.ShortcutPath); File.Delete(context.ShortcutPath);
} }
@ -250,7 +272,8 @@ namespace IPA {
Console.ResetColor(); Console.ResetColor();
} }
private static void StartIfNeedBe(PatchContext context) {
private static void StartIfNeedBe(PatchContext context)
{
if (ArgStart.HasValue) if (ArgStart.HasValue)
{ {
Process.Start(context.Executable, ArgStart.Value); Process.Start(context.Executable, ArgStart.Value);
@ -269,42 +292,51 @@ namespace IPA {
} }
public static IEnumerable<FileInfo> NativePluginInterceptor(FileInfo from, FileInfo to, public static IEnumerable<FileInfo> NativePluginInterceptor(FileInfo from, FileInfo to,
DirectoryInfo nativePluginFolder, bool isFlat, Architecture preferredArchitecture) {
if (to.FullName.StartsWith(nativePluginFolder.FullName)) {
DirectoryInfo nativePluginFolder, bool isFlat, Architecture preferredArchitecture)
{
if (to.FullName.StartsWith(nativePluginFolder.FullName))
{
var relevantBit = to.FullName.Substring(nativePluginFolder.FullName.Length + 1); var relevantBit = to.FullName.Substring(nativePluginFolder.FullName.Length + 1);
// Goes into the plugin folder! // Goes into the plugin folder!
bool isFileFlat = !relevantBit.StartsWith("x86"); bool isFileFlat = !relevantBit.StartsWith("x86");
if (isFlat && !isFileFlat) {
if (isFlat && !isFileFlat)
{
// Flatten structure // Flatten structure
bool is64Bit = relevantBit.StartsWith("x86_64"); bool is64Bit = relevantBit.StartsWith("x86_64");
if (!is64Bit && preferredArchitecture == Architecture.x86) {
if (!is64Bit && preferredArchitecture == Architecture.x86)
{
// 32 bit // 32 bit
yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, yield return new FileInfo(Path.Combine(nativePluginFolder.FullName,
relevantBit.Substring("x86".Length + 1))); relevantBit.Substring("x86".Length + 1)));
} }
else if (is64Bit && (preferredArchitecture == Architecture.x64 || else if (is64Bit && (preferredArchitecture == Architecture.x64 ||
preferredArchitecture == Architecture.Unknown)) {
preferredArchitecture == Architecture.Unknown))
{
// 64 bit // 64 bit
yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, yield return new FileInfo(Path.Combine(nativePluginFolder.FullName,
relevantBit.Substring("x86_64".Length + 1))); relevantBit.Substring("x86_64".Length + 1)));
} }
else {
else
{
// Throw away // Throw away
yield break; yield break;
} }
} }
else if (!isFlat && isFileFlat) {
else if (!isFlat && isFileFlat)
{
// Deepen structure // Deepen structure
yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86"), yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86"),
relevantBit)); relevantBit));
yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86_64"), yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86_64"),
relevantBit)); relevantBit));
} }
else {
else
{
yield return to; yield return to;
} }
} }
else {
else
{
yield return to; yield return to;
} }
} }
@ -317,20 +349,26 @@ namespace IPA {
Console.SetCursorPosition(0, tpos); Console.SetCursorPosition(0, tpos);
} }
private static IEnumerable<FileInfo> PassThroughInterceptor(FileInfo from, FileInfo to) {
private static IEnumerable<FileInfo> PassThroughInterceptor(FileInfo from, FileInfo to)
{
yield return to; yield return to;
} }
public static void CopyAll(DirectoryInfo source, DirectoryInfo target, bool aggressive, BackupUnit backup, public static void CopyAll(DirectoryInfo source, DirectoryInfo target, bool aggressive, BackupUnit backup,
Func<FileInfo, FileInfo, IEnumerable<FileInfo>> interceptor = null) {
if (interceptor == null) {
Func<FileInfo, FileInfo, IEnumerable<FileInfo>> interceptor = null)
{
if (interceptor == null)
{
interceptor = PassThroughInterceptor; interceptor = PassThroughInterceptor;
} }
// Copy each file into the new directory. // Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles()) {
foreach (var targetFile in interceptor(fi, new FileInfo(Path.Combine(target.FullName, fi.Name)))) {
if (!targetFile.Exists || targetFile.LastWriteTimeUtc < fi.LastWriteTimeUtc || aggressive) {
foreach (FileInfo fi in source.GetFiles())
{
foreach (var targetFile in interceptor(fi, new FileInfo(Path.Combine(target.FullName, fi.Name))))
{
if (!targetFile.Exists || targetFile.LastWriteTimeUtc < fi.LastWriteTimeUtc || aggressive)
{
targetFile.Directory.Create(); targetFile.Directory.Create();
Console.CursorTop--; Console.CursorTop--;
@ -343,14 +381,16 @@ namespace IPA {
} }
// Copy each subdirectory using recursion. // Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = new DirectoryInfo(Path.Combine(target.FullName, diSourceSubDir.Name)); DirectoryInfo nextTargetSubDir = new DirectoryInfo(Path.Combine(target.FullName, diSourceSubDir.Name));
CopyAll(diSourceSubDir, nextTargetSubDir, aggressive, backup, interceptor); CopyAll(diSourceSubDir, nextTargetSubDir, aggressive, backup, interceptor);
} }
} }
static void Fail(string message) {
static void Fail(string message)
{
Console.Error.WriteLine("ERROR: " + message); Console.Error.WriteLine("ERROR: " + message);
WaitForEnd(); WaitForEnd();
@ -358,7 +398,8 @@ namespace IPA {
Environment.Exit(1); Environment.Exit(1);
} }
public static string Args(params string[] args) {
public static string Args(params string[] args)
{
return string.Join(" ", args.Select(EncodeParameterArgument).ToArray()); return string.Join(" ", args.Select(EncodeParameterArgument).ToArray());
} }
@ -368,7 +409,8 @@ namespace IPA {
/// <param name="original">The value that should be received by the program</param> /// <param name="original">The value that should be received by the program</param>
/// <returns>The value which needs to be passed to the program for the original value /// <returns>The value which needs to be passed to the program for the original value
/// to come through</returns> /// to come through</returns>
public static string EncodeParameterArgument(string original) {
public static string EncodeParameterArgument(string original)
{
if (string.IsNullOrEmpty(original)) if (string.IsNullOrEmpty(original))
return original; return original;
string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0"); string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0");
@ -376,10 +418,13 @@ namespace IPA {
return value; return value;
} }
public static Architecture DetectArchitecture(string assembly) {
using (var reader = new BinaryReader(File.OpenRead(assembly))) {
public static Architecture DetectArchitecture(string assembly)
{
using (var reader = new BinaryReader(File.OpenRead(assembly)))
{
var header = reader.ReadUInt16(); var header = reader.ReadUInt16();
if (header == 0x5a4d) {
if (header == 0x5a4d)
{
reader.BaseStream.Seek(60, SeekOrigin.Begin); // this location contains the offset for the PE header reader.BaseStream.Seek(60, SeekOrigin.Begin); // this location contains the offset for the PE header
var peOffset = reader.ReadUInt32(); var peOffset = reader.ReadUInt32();
@ -395,16 +440,19 @@ namespace IPA {
else else
return Architecture.Unknown; return Architecture.Unknown;
} }
else {
else
{
// Not a supported binary // Not a supported binary
return Architecture.Unknown; return Architecture.Unknown;
} }
} }
} }
public abstract class Keyboard {
public abstract class Keyboard
{
[Flags] [Flags]
private enum KeyStates {
private enum KeyStates
{
None = 0, None = 0,
Down = 1, Down = 1,
Toggled = 2 Toggled = 2
@ -413,10 +461,11 @@ namespace IPA {
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern short GetKeyState(int keyCode); private static extern short GetKeyState(int keyCode);
private static KeyStates GetKeyState(Keys key) {
private static KeyStates GetKeyState(Keys key)
{
KeyStates state = KeyStates.None; KeyStates state = KeyStates.None;
short retVal = GetKeyState((int) key);
short retVal = GetKeyState((int)key);
//If the high-order bit is 1, the key is down //If the high-order bit is 1, the key is down
//otherwise, it is up. //otherwise, it is up.
@ -430,11 +479,13 @@ namespace IPA {
return state; return state;
} }
public static bool IsKeyDown(Keys key) {
public static bool IsKeyDown(Keys key)
{
return KeyStates.Down == (GetKeyState(key) & KeyStates.Down); return KeyStates.Down == (GetKeyState(key) & KeyStates.Down);
} }
public static bool IsKeyToggled(Keys key) {
public static bool IsKeyToggled(Keys key)
{
return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled); return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled);
} }
} }


Loading…
Cancel
Save