CLIPA/IPA/Program.cs

112 lines
4.1 KiB
C#
Raw Normal View History

2016-12-15 20:07:03 +00:00
using IPA.Patcher;
using System;
2016-12-14 21:45:50 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace IPA
{
class Program
{
static void Main(string[] args)
{
if(args.Length < 1 || !args[0].EndsWith(".exe"))
{
Fail("Drag an (executable) file on the exe!");
}
string launcherSrc = Path.Combine("IPA", "Launcher.exe");
string managedFolder = Path.Combine("IPA", "Managed");
2016-12-15 20:07:03 +00:00
string pluginsFolder = "Plugins";
string projectName = Path.GetFileNameWithoutExtension(args[0]);
2017-01-08 16:49:11 +00:00
string dataPath = Path.Combine(Path.Combine(Path.GetDirectoryName(args[0]), projectName + "_Data"), "Managed");
2016-12-15 20:07:03 +00:00
string engineFile = Path.Combine(dataPath, "UnityEngine.dll");
string assemblyFile = Path.Combine(dataPath, "Assembly-Csharp.dll");
2016-12-14 21:45:50 +00:00
// Sanitizing
if (!File.Exists(launcherSrc)) Fail("Couldn't find DLLs! Make sure you extracted all contents of the release archive.");
2016-12-15 20:07:03 +00:00
if(!Directory.Exists(dataPath) || !File.Exists(engineFile) || !File.Exists(assemblyFile))
{
Fail("Game does not seem to be a Unity project. Could not find the libraries to patch.");
}
2016-12-14 21:45:50 +00:00
try
{
2016-12-15 20:07:03 +00:00
// Copying
2017-01-08 16:49:11 +00:00
Console.Write("Updating files... ");
2016-12-14 21:45:50 +00:00
CopyAll(new DirectoryInfo(managedFolder), new DirectoryInfo(dataPath));
2017-01-08 16:49:11 +00:00
Console.WriteLine("Successfully updated files!");
2016-12-15 20:07:03 +00:00
2017-01-08 16:49:11 +00:00
if (!Directory.Exists(pluginsFolder))
2016-12-15 20:07:03 +00:00
{
2017-01-08 16:49:11 +00:00
Console.WriteLine("Creating plugins folder... ");
2016-12-15 20:07:03 +00:00
Directory.CreateDirectory(pluginsFolder);
}
// Patching
var patchedModule = PatchedModule.Load(engineFile);
if(!patchedModule.IsPatched)
{
2017-01-08 16:49:11 +00:00
Console.Write("Patching UnityEngine.dll... ");
2016-12-15 20:07:03 +00:00
BackupManager.MakeBackup(engineFile);
patchedModule.Patch();
2017-01-08 16:49:11 +00:00
Console.WriteLine("Done!");
2016-12-15 20:07:03 +00:00
}
// Virtualizing
var virtualizedModule = VirtualizedModule.Load(assemblyFile);
if(!virtualizedModule.IsVirtualized)
{
2017-01-08 16:49:11 +00:00
Console.Write("Virtualizing Assembly-Csharp.dll... ");
2016-12-15 20:07:03 +00:00
BackupManager.MakeBackup(assemblyFile);
virtualizedModule.Virtualize();
2017-01-08 16:49:11 +00:00
Console.WriteLine("Done!");
2016-12-15 20:07:03 +00:00
}
2016-12-14 21:45:50 +00:00
} catch(Exception e)
{
Fail("Oops! This should not have happened.\n\n" + e);
}
2017-01-08 16:49:11 +00:00
Console.WriteLine("Finished!");
2016-12-14 21:45:50 +00:00
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
2016-12-15 20:07:03 +00:00
string targetFile = Path.Combine(target.FullName, fi.Name);
if (!File.Exists(targetFile) || File.GetLastWriteTimeUtc(targetFile) < fi.LastWriteTimeUtc)
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
2016-12-14 21:45:50 +00:00
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
static void Fail(string message)
{
2016-12-15 20:07:03 +00:00
Console.Error.Write("ERROR: " + message);
if (!Environment.CommandLine.Contains("--nowait"))
{
2016-12-15 20:13:27 +00:00
Console.WriteLine("\n\n[Press any key to quit]");
Console.ReadKey();
2016-12-15 20:07:03 +00:00
}
2016-12-14 21:45:50 +00:00
Environment.Exit(1);
}
}
}