Skip to content

Commit

Permalink
Only create directory if needed and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
harbingerofme committed Feb 27, 2021
1 parent a455b1c commit a328d66
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions HookGenPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public static class HookGenPatcher

private const string CONFIG_FILE_NAME = "HookGenPatcher.cfg";

private static readonly ConfigFile Config =
new(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true);
private static readonly ConfigFile Config = new ConfigFile(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true);

private const char EntrySeparator = ',';

Expand All @@ -32,25 +31,31 @@ public static void Initialize()
var assemblyNames = AssemblyNamesToHookGenPatch.Value.Split(EntrySeparator);

var mmhookFolder = Path.Combine(Paths.PluginPath, "MMHOOK");
Directory.CreateDirectory(mmhookFolder);


foreach (var customAssemblyName in assemblyNames)
{
var mmhookFileName = "MMHOOK_" + customAssemblyName;

string pathIn = Path.Combine(Paths.ManagedPath, customAssemblyName);
string pathOut = Path.Combine(mmhookFolder, mmhookFileName);
bool shouldCreateDirectory = true;

foreach (string mmhookFile in Directory.EnumerateFiles(Paths.PluginPath, mmhookFileName, SearchOption.AllDirectories))
{
if (Path.GetFileName(mmhookFile).Equals(mmhookFileName))
{
pathOut = mmhookFile;
Logger.LogInfo("Previous MMHOOK location found. Using that location to save instead.");
shouldCreateDirectory = false;
break;
}
}

if(shouldCreateDirectory)
{
Directory.CreateDirectory(mmhookFolder);
}
var size = new FileInfo(pathIn).Length;

if (File.Exists(pathOut))
Expand Down Expand Up @@ -95,7 +100,7 @@ public static void Initialize()
using (ModuleDefinition mOut = gen.OutputModule)
{
gen.Generate();
mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, Mono.Cecil.TypeAttributes.AutoClass));
mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, TypeAttributes.AutoClass));
mOut.Write(pathOut);
}

Expand Down

0 comments on commit a328d66

Please sign in to comment.