Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions IPA.Tests/ProgramTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace IPA.Tests
Expand All @@ -26,7 +22,7 @@ public class ProgramTest

// Not-flat -> Not-Flat
[InlineData("native/x86/from.dll", "native/x86/to.dll", "native", false, new string[] { "native/x86/to.dll" })]
[InlineData("native/x86_64/from.dll", "native/x86_64/to.dll", "native", false, new string[] { "native/x86_64/to.dll" })]
[InlineData("native/x86_64/from.dll", "native/x86_64/to.dll", "native", false, new string[] { "native/x86_64/to.dll" })]

public void CopiesCorrectly(string from, string to, string nativeFolder, bool isFlat, string[] expected)
{
Expand Down
4 changes: 0 additions & 4 deletions IPA.Tests/ShortcutTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace IPA.Tests
Expand Down
11 changes: 4 additions & 7 deletions IPA/PatchContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace IPA
{
Expand Down Expand Up @@ -37,7 +34,7 @@ private PatchContext() { }
public static PatchContext Create(String[] args)
{
var context = new PatchContext();

context.Args = args;
context.Executable = args[0];
context.ProjectRoot = new FileInfo(context.Executable).Directory.FullName;
Expand All @@ -49,7 +46,7 @@ public static PatchContext Create(String[] args)
context.ProjectName = Path.GetFileNameWithoutExtension(context.Executable);
context.DataPathDst = Path.Combine(context.ProjectRoot, context.ProjectName + "_Data");
context.ManagedPath = Path.Combine(context.DataPathDst, "Managed");
context.EngineFile = DetermineEngineFile(context.ManagedPath, "UnityEngine.CoreModule.dll", "UnityEngine.dll");
context.EngineFile = DetermineEngineFile(context.ManagedPath, "UnityEngine.CoreModule.dll", "UnityEngine.dll");
context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-Csharp.dll");
context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName);
context.ShortcutPath = Path.Combine(context.ProjectRoot, $"{context.ProjectName} (Patch & Launch)") + ".lnk";
Expand All @@ -61,10 +58,10 @@ public static PatchContext Create(String[] args)

private static string DetermineEngineFile(string basePath, params string[] candidates)
{
foreach(var candidate in candidates)
foreach (var candidate in candidates)
{
var fullPath = Path.Combine(basePath, candidate);
if(File.Exists(fullPath))
if (File.Exists(fullPath))
{
return fullPath;
}
Expand Down
10 changes: 3 additions & 7 deletions IPA/Patcher/BackupManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IPA.Patcher
{
Expand All @@ -22,11 +18,11 @@ public static bool HasBackup(PatchContext context)
{
return FindLatestBackup(context) != null;
}

public static bool Restore(PatchContext context)
{
var backup = FindLatestBackup(context);
if(backup != null)
if (backup != null)
{
backup.Restore();
backup.Delete();
Expand Down
28 changes: 15 additions & 13 deletions IPA/Patcher/BackupUnit.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;

namespace IPA.Patcher
{
Expand All @@ -30,13 +27,14 @@ private BackupUnit(PatchContext context, string name)
_Context = context;
_BackupPath = new DirectoryInfo(Path.Combine(_Context.BackupPath, Name));
}

public static BackupUnit FromDirectory(DirectoryInfo directory, PatchContext context)
{
var unit = new BackupUnit(context, directory.Name);

// Parse directory
foreach(var file in directory.GetFiles("*", SearchOption.AllDirectories)) {
foreach (var file in directory.GetFiles("*", SearchOption.AllDirectories))
{
var relativePath = file.FullName.Substring(directory.FullName.Length + 1);
unit._Files.Add(relativePath);
}
Expand All @@ -60,16 +58,16 @@ internal void Delete()
/// <param name="path"></param>
public void Add(FileInfo file)
{
if(!file.FullName.StartsWith(_Context.ProjectRoot))
if (!file.FullName.StartsWith(_Context.ProjectRoot))
{
Console.Error.WriteLine("Invalid file path for backup! {0}", file);
return;
}

var relativePath = file.FullName.Substring(_Context.ProjectRoot.Length + 1);
var backupPath = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
if(_Files.Contains(relativePath))

if (_Files.Contains(relativePath))
{
Console.WriteLine("Skipping backup of {0}", relativePath);
return;
Expand All @@ -81,7 +79,8 @@ public void Add(FileInfo file)
if (file.Exists)
{
file.CopyTo(backupPath.FullName);
} else
}
else
{
// Make empty file
backupPath.Create().Close();
Expand All @@ -96,7 +95,7 @@ public void Add(FileInfo file)
/// </summary>
public void Restore()
{
foreach(var relativePath in _Files)
foreach (var relativePath in _Files)
{
Console.WriteLine("Restoring {0}", relativePath);
// Original version
Expand All @@ -110,15 +109,18 @@ public void Restore()
Console.WriteLine(" {0} => {1}", backupFile.FullName, target.FullName);
target.Directory.Create();
backupFile.CopyTo(target.FullName, true);
} else
}
else
{
Console.WriteLine(" x {0}", target.FullName);
if(target.Exists)
if (target.Exists)
{
target.Delete();
}
}
} else {
}
else
{
Console.Error.WriteLine("Backup not found!");
}
}
Expand Down
25 changes: 13 additions & 12 deletions IPA/Patcher/Patcher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil;
using Mono.Cecil.Cil;

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

Expand Down Expand Up @@ -36,7 +35,7 @@ private void LoadModules()
{
AssemblyResolver = resolver,
};

_Module = ModuleDefinition.ReadModule(_File.FullName, parameters);
}

Expand All @@ -46,7 +45,8 @@ public bool IsPatched
{
foreach (var @ref in _Module.AssemblyReferences)
{
if (@ref.Name == "IllusionInjector") return true;
if (@ref.Name == "IllusionInjector")
return true;
}
return false;
}
Expand All @@ -61,18 +61,19 @@ public void Patch()

_Module.AssemblyReferences.Add(nameReference);
int patched = 0;
foreach(var type in FindEntryTypes())
foreach (var type in FindEntryTypes())
{
if(PatchType(type, injector))
if (PatchType(type, injector))
{
patched++;
}
}
if(patched > 0)

if (patched > 0)
{
_Module.Write(_File.FullName);
} else
}
else
{
throw new Exception("Could not find any entry type!");
}
Expand Down
27 changes: 15 additions & 12 deletions IPA/Patcher/Virtualizer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil;

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

Expand Down Expand Up @@ -38,7 +36,7 @@ private void LoadModules()

_Module = ModuleDefinition.ReadModule(_File.FullName, parameters);
}

/// <summary>
///
/// </summary>
Expand All @@ -55,17 +53,20 @@ public void Virtualize()

private void VirtualizeType(TypeDefinition type)
{
if(type.IsSealed)
if (type.IsSealed)
{
// Unseal
type.IsSealed = false;
}

if (type.IsInterface) return;
if (type.IsAbstract) return;
if (type.IsInterface)
return;
if (type.IsAbstract)
return;

// These two don't seem to work.
if (type.Name == "SceneControl" || type.Name == "ConfigUI") return;
if (type.Name == "SceneControl" || type.Name == "ConfigUI")
return;

Console.WriteLine("Virtualizing {0}", type.Name);
// Take care of sub types
Expand Down Expand Up @@ -97,7 +98,8 @@ private void VirtualizeType(TypeDefinition type)

foreach (var field in type.Fields)
{
if (field.IsPrivate) field.IsFamily = true;
if (field.IsPrivate)
field.IsFamily = true;
}
}

Expand All @@ -106,7 +108,8 @@ public bool IsVirtualized
get
{
var awakeMethods = _Module.GetTypes().SelectMany(t => t.Methods.Where(m => m.Name == "Awake"));
if (awakeMethods.Count() == 0) return false;
if (awakeMethods.Count() == 0)
return false;

return ((float)awakeMethods.Count(m => m.IsVirtual) / awakeMethods.Count()) > 0.5f;
}
Expand Down
Loading