Skip to content

Commit

Permalink
deal with missing backup file and already injected backup file when d…
Browse files Browse the repository at this point in the history
…oing restore operation
  • Loading branch information
janxious committed Aug 14, 2018
1 parent 4a62b13 commit 48d0264
Showing 1 changed file with 69 additions and 19 deletions.
88 changes: 69 additions & 19 deletions BattleTechModLoaderInjector/BTMLInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ private static int Main(string[] args)
{
if (!injected)
{
// TODO: detect backup corruption
Backup(gameDllPath, gameDllBackupPath);
Inject(gameDllPath, injectDllPath);
}
Expand All @@ -135,22 +134,29 @@ private static int Main(string[] args)
return returnCode;
}
}
catch (BackupFileNotFound e)
{
SayException(e);
SayHowToRecoverMissingBackup(e.BackupFileName);
returnCode = 3;
return returnCode;
}
catch (BackupFileInjected e)
{
SayException(e);
SayHowToRecoverInjectedBackup(e.BackupFileName);
returnCode = 4;
return returnCode;
}
catch (Exception e)
{
SayException(e);
}

// if we got here something went badly
returnCode = 1;
return returnCode;
}

private static void SayHowToRecover()
{
WriteLine("You will need the original assembly file beside the injector (named as assembly-csharp.dll.orig) for /restore to work.");
WriteLine("You may need to reinstall or use Steam's file verification function if you have no other backup.");
}

private static void SayInjectedStatus(bool injected)
{
if (injected) {
Expand All @@ -172,6 +178,9 @@ private static void Restore(string filePath, string backupFilePath)
{
throw new BackupFileNotFound();
}
if (IsInjected(backupFilePath)) {
throw new BackupFileInjected();
}
File.Copy(backupFilePath, filePath, true);
WriteLine($"{Path.GetFileName(backupFilePath)} restored to {Path.GetFileName(filePath)}");
}
Expand Down Expand Up @@ -236,6 +245,10 @@ private static void Inject(string hookFilePath, string injectFilePath)
}
}

private static bool IsInjected(string dllPath)
{
return IsInjected(dllPath, out _);
}
private static bool IsInjected(string dllPath, out bool isCurrentInjection)
{
isCurrentInjection = false;
Expand Down Expand Up @@ -290,17 +303,6 @@ private static bool IsHookInstalled(MethodDefinition methodDefinition, out bool
return false;
}

public class BackupFileNotFound : FileNotFoundException
{
public BackupFileNotFound(string backupFileName = "Assembly-CSharp.dll.orig") :
base(FormulateMessage(backupFileName)) {}

private static string FormulateMessage(string backupFileName)
{
return $"The backup file \"{backupFileName}\" could not be found.";
}
}

private static void SayHelp(OptionSet p)
{
WriteLine("Usage: BattleTechModLoaderInjector.exe [OPTIONS]+");
Expand Down Expand Up @@ -337,6 +339,20 @@ private static void SayHeader()
WriteLine("----------------------------");
}

private static void SayHowToRecoverMissingBackup(string backupFileName)
{
WriteLine("----------------------------");
WriteLine($"The backup game assembly file must be in the directory with the injector for /restore to work. The backup file should be named \"{backupFileName}\".");
WriteLine("You may need to reinstall or use Steam/GOG's file verification function if you have no other backup.");
}

private static void SayHowToRecoverInjectedBackup(string backupFileName)
{
WriteLine("----------------------------");
WriteLine($"The backup game assembly file named \"{backupFileName}\" was already BTML injected. Something has gone wrong.");
WriteLine("You may need to reinstall or use Steam/GOG's file verification function if you have no other backup.");
}

private static void SayWasNotInjected()
{
WriteLine($"{GameDllFileName} was not previously injected.");
Expand Down Expand Up @@ -384,4 +400,38 @@ private static void PromptForKey(bool requireKeyPress)
ReadKey();
}
}

public class BackupFileInjected : Exception
{
private string backupFileName;
public string BackupFileName => backupFileName;

public BackupFileInjected(string backupFileName = "Assembly-CSharp.dll.orig") :
base(FormulateMessage(backupFileName))
{
this.backupFileName = backupFileName;
}

private static string FormulateMessage(string backupFileName)
{
return $"The backup file \"{backupFileName}\" was BTML-injected.";
}
}

public class BackupFileNotFound : FileNotFoundException
{
private string backupFileName;
public string BackupFileName => backupFileName;

public BackupFileNotFound(string backupFileName = "Assembly-CSharp.dll.orig") :
base(FormulateMessage(backupFileName))
{
this.backupFileName = backupFileName;
}

private static string FormulateMessage(string backupFileName)
{
return $"The backup file \"{backupFileName}\" could not be found.";
}
}
}

0 comments on commit 48d0264

Please sign in to comment.