Skip to content

Commit

Permalink
Installer update - Copy mods and Uninstall actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nefaro committed Feb 1, 2022
1 parent ff688b8 commit d6910b3
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 85 deletions.
140 changes: 129 additions & 11 deletions Installer/InstallerCore/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ namespace InstallerCore
{
public abstract class Action
{
public abstract void Execute();
private static readonly Logger _log = Logger.GetLogger;

public void Execute()
{
_log.WriteLine(this.ToString());
this.ExecuteImpl();
}

protected abstract void ExecuteImpl();
}

public class InstallModKit : Action
Expand All @@ -20,7 +28,7 @@ public InstallModKit(string catalogPath, string outputPath, string backupPath, s
Patch = patch;
}

public override void Execute()
protected override void ExecuteImpl()
{
// apply patch. do this to a temp file because the patch itself might be referring to the file being replaced! (e.g. Gnomoria.exe)
string tmpOutputPath = OutputPath + ".tmp";
Expand All @@ -36,7 +44,7 @@ public override void Execute()

public override string ToString()
{
return $"Install {PatchVersion} to {OutputPath} using {Patch}";
return $"$$ Install {PatchVersion} to {OutputPath} using {Patch}";
}

public string CatalogPath { get; }
Expand All @@ -58,7 +66,7 @@ public InstallStandalone(string catalogPath, string outputPath, string vanillaMd
Patch = patch;
}

public override void Execute()
protected override void ExecuteImpl()
{
// apply patch
Patch.Install(OutputPath);
Expand All @@ -71,7 +79,7 @@ public override void Execute()

public override string ToString()
{
return $"Install {PatchVersion} to {OutputPath} using {Patch}";
return $"$$ Install {PatchVersion} to {OutputPath} using {Patch}";
}

public string CatalogPath { get; }
Expand All @@ -92,7 +100,7 @@ public UninstallModKit(string catalogPath, string outputPath, string backupPath,
PatchVersion = patchVersion;
}

public override void Execute()
protected override void ExecuteImpl()
{
// restore backup
File.Replace(BackupPath, OutputPath, destinationBackupFileName: null);
Expand All @@ -105,7 +113,7 @@ public override void Execute()

public override string ToString()
{
return $"Un-mod {PatchVersion} ({OutputPath})";
return $"$$ Un-mod {PatchVersion} ({OutputPath})";
}

public string CatalogPath { get; }
Expand All @@ -125,7 +133,7 @@ public UninstallStandalone(string catalogPath, string outputPath, string vanilla
PatchVersion = patchVersion;
}

public override void Execute()
protected override void ExecuteImpl()
{
// delete patched executable
File.Delete(OutputPath);
Expand All @@ -139,7 +147,7 @@ public override void Execute()

public override string ToString()
{
return $"Uninstall {PatchVersion} ({OutputPath})";
return $"$$ Uninstall {PatchVersion} ({OutputPath})";
}

public string CatalogPath { get; }
Expand All @@ -157,7 +165,7 @@ public InstallModLoader(string modLoaderPath, string outputPath)
BackupPath = outputPath + ".bak";
}

public override void Execute()
protected override void ExecuteImpl()
{
if ( File.Exists(ModloaderPath) )
{
Expand All @@ -180,7 +188,117 @@ public override void Execute()

public override string ToString()
{
return $"Install Mod Loader to {OutputPath}";
return $"$$ Install Mod Loader to {OutputPath}";
}

public string ModloaderPath { get; }
public string OutputPath { get; }
public string BackupPath { get; }
}

public class CopyModsAction : Action
{
public CopyModsAction(string modsPath, string outputPath)
{
ModsPath = modsPath;
OutputPath = outputPath;
BackupPath = outputPath + ".bak";
}

protected override void ExecuteImpl()
{
if (Directory.Exists(ModsPath))
{
if (Directory.Exists(OutputPath))
{
// Do backup of existing
CopyFilesRecursively(OutputPath, BackupPath);
}
// copy mods from given path to output path
CopyFilesRecursively(ModsPath, OutputPath);
// Unblock the files from the target
FileUnblocker.UnblockPath(OutputPath);
}
}

private void CopyFilesRecursively(string sourcePath, string targetPath)
{
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}

foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}

public override string ToString()
{
return $"$$ Copying Mods to {OutputPath}";
}

public string ModsPath { get; }
public string OutputPath { get; }
public string BackupPath { get; }
}

public class DeleteModsAction : Action
{
public DeleteModsAction(string modsPath, string outputPath)
{
ModsPath = modsPath;
OutputPath = outputPath;
BackupPath = outputPath + ".bak";
}

protected override void ExecuteImpl()
{
if (Directory.Exists(BackupPath))
{
Directory.Delete(BackupPath, true);
}
if (Directory.Exists(OutputPath))
{
Directory.Delete(OutputPath, true);
}
}

public override string ToString()
{
return $"$$ Cleaning up Mods from {OutputPath} and {BackupPath}";
}

public string ModsPath { get; }
public string OutputPath { get; }
public string BackupPath { get; }
}

public class UninstallModLoader : Action
{
public UninstallModLoader(string modLoaderPath, string outputPath)
{
ModloaderPath = modLoaderPath;
OutputPath = outputPath;
BackupPath = outputPath + ".bak";
}

protected override void ExecuteImpl()
{
if (File.Exists(BackupPath))
{
File.Delete(BackupPath);
}
if (File.Exists(OutputPath))
{
File.Delete(OutputPath);
}
}

public override string ToString()
{
return $"$$ Uninstall Mod Loader from {OutputPath} and {BackupPath}";
}

public string ModloaderPath { get; }
Expand Down
46 changes: 46 additions & 0 deletions Installer/InstallerCore/FileUnblocker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace InstallerCore
{
public class FileUnblocker
{
private static readonly Logger _log = Logger.GetLogger;

[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFile(string name);

public static bool UnblockFile(string fileName)
{
return DeleteFile(fileName + ":Zone.Identifier");
}

public static void UnblockPath(string path)
{
string[] files = System.IO.Directory.GetFiles(path);
string[] dirs = System.IO.Directory.GetDirectories(path);

foreach (string file in files)
{
_log.WriteLine($"Unblocking file: {file}");
bool result = UnblockFile(file);
if ( !result )
{
int error = Marshal.GetLastWin32Error();

_log.WriteLine($"Failed to unblock file: {file} with error {error}");
}
}

foreach (string dir in dirs)
{
UnblockPath(dir);
}
}
}
}
16 changes: 15 additions & 1 deletion Installer/InstallerCore/InstallerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class InstallerCore
{
private static readonly Logger _log = Logger.GetLogger;
private static readonly string _modLoaderFile = "GnollModLoader.dll";
private static readonly string _modsDirectory = "Gnoll Mods";


public static ScanResult ScanGameInstall(string installDir, GamePatchDatabase gameDb)
{
Expand All @@ -40,6 +42,8 @@ public static ScanResult ScanGameInstall(string installDir, GamePatchDatabase ga
string backupExePath = Path.Combine(installDir, "Gnomoria.orig.exe");
string modLoaderTargetPath = Path.Combine(installDir, _modLoaderFile);
string modLoaderSourcePath = Path.Combine(gameDb.PatchFolder, _modLoaderFile);
string modsSourcePath = Path.Combine(gameDb.PatchFolder, _modsDirectory);
string modsTargetPath = Path.Combine(installDir, _modsDirectory);

// Load installation catalog (json file)

Expand Down Expand Up @@ -101,6 +105,7 @@ public static ScanResult ScanGameInstall(string installDir, GamePatchDatabase ga
if ( File.Exists(modLoaderSourcePath) )
{
actions.Add(new InstallModLoader(modLoaderSourcePath, modLoaderTargetPath));
actions.Add(new UninstallModLoader(modLoaderSourcePath, modLoaderTargetPath));
}
else
{
Expand Down Expand Up @@ -132,6 +137,7 @@ public static ScanResult ScanGameInstall(string installDir, GamePatchDatabase ga
if (File.Exists(modLoaderSourcePath))
{
actions.Add(new InstallModLoader(modLoaderSourcePath, modLoaderTargetPath));
actions.Add(new UninstallModLoader(modLoaderSourcePath, modLoaderTargetPath));
}
else
{
Expand All @@ -150,7 +156,15 @@ public static ScanResult ScanGameInstall(string installDir, GamePatchDatabase ga
_log.WriteLine($"Game stand-alone modded => propose UninstallStandalone");
actions.Add(new UninstallStandalone(catalogPath, standalonePath, vanillaGameMd5, standalone));
}

if ( Directory.Exists(modsSourcePath) )
{
actions.Add(new CopyModsAction(modsSourcePath, modsTargetPath));
actions.Add(new DeleteModsAction(modsSourcePath, modsTargetPath));
}
else
{
_log.WriteLine($"Warning: Mods not included in {modsSourcePath}");
}
return new ScanResult(moddedVersion, gameVersion.Name, actions.ToArray(), patchAvailable: (installable != null),
standalone, (installable!=null? installable.PatchVersion: null));
}
Expand Down
1 change: 1 addition & 0 deletions Installer/InstallerCore/InstallerCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Action.cs" />
<Compile Include="FileUnblocker.cs" />
<Compile Include="GamePatchDatabase.cs" />
<Compile Include="Installable.cs" />
<Compile Include="InstallDb.cs" />
Expand Down
Loading

0 comments on commit d6910b3

Please sign in to comment.