Skip to content
Merged
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
9 changes: 0 additions & 9 deletions src/CLI/CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,4 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<!-- 7zip -->
<ItemGroup>
<PackageReference Include="7z.Libs" Version="23.1.0" />
</ItemGroup>
<Target Name="Organize7zLibs" AfterTargets="Build">
<Move SourceFiles="$(OutDir)\x64\7z.dll" DestinationFiles="$(OutDir)\7z64.dll" />
<RemoveDir Directories="$(OutDir)\x86;$(OutDir)\x64" />
</Target>
</Project>
12 changes: 12 additions & 0 deletions src/CLI/ConsoleEventLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Core.Utils;
using Core;

namespace AMS2CM.CLI;

internal class ConsoleEventLogger : BaseEventLogger
{
public override void ProgressUpdate(IPercent? value)
{
}
protected override void LogMessage(string message) => Console.WriteLine(message);
}
6 changes: 3 additions & 3 deletions src/CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Core;
using AMS2CM.CLI;
using Core;

try
{
var config = Config.Load(args);
var modManager = Init.CreateModManager(config);
modManager.Logs += Console.WriteLine;
modManager.InstallEnabledMods();
modManager.InstallEnabledMods(new ConsoleEventLogger());
}
catch (Exception e)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Core/Backup/IBackupStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Core.Backup;

public interface IBackupStrategy
{
public void PerformBackup(string fullPath);
public void RestoreBackup(string fullPath);
public void DeleteBackup(string fullPath);
public bool IsBackupFile(string fullPath);
}
52 changes: 52 additions & 0 deletions src/Core/Backup/MoveFileBackupStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.IO.Abstractions;

namespace Core.Backup;

public class MoveFileBackupStrategy
{

private readonly IFileSystem fs;
private readonly Func<string, string> generateBackupFilePath;

public MoveFileBackupStrategy(IFileSystem fs, Func<string, string> generateBackupFilePath)
{
this.fs = fs;
this.generateBackupFilePath = generateBackupFilePath;
}

public void PerformBackup(string fullPath)
{
if (!fs.File.Exists(fullPath))
{
return;
}

var backupFilePath = generateBackupFilePath(fullPath);
if (fs.File.Exists(backupFilePath))
{
fs.File.Delete(fullPath);
}
else
{
fs.File.Move(fullPath, backupFilePath);
}
}

public void RestoreBackup(string fullPath)
{
var backupFilePath = generateBackupFilePath(fullPath);
if (fs.File.Exists(backupFilePath))
{
fs.File.Move(backupFilePath, fullPath);
}
}

public void DeleteBackup(string fullPath)
{
var backupFilePath = generateBackupFilePath(fullPath);
if (fs.File.Exists(backupFilePath))
{
fs.File.Delete(backupFilePath);
}
}
}
16 changes: 16 additions & 0 deletions src/Core/Backup/SuffixBackupStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO.Abstractions;

namespace Core.Backup;

public class SuffixBackupStrategy : MoveFileBackupStrategy, IBackupStrategy
{
public const string BackupSuffix = ".orig";

public SuffixBackupStrategy() :
base(new FileSystem(), _ => $"{_}{BackupSuffix}")
{
}

public bool IsBackupFile(string fullPath) =>
fullPath.EndsWith(BackupSuffix);
}
59 changes: 59 additions & 0 deletions src/Core/BaseEventLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Core.Utils;

namespace Core;

/// <summary>
/// This class is here because of the CLI. Move it into the GUI once the CLI
/// can be decommissioned or into a shared module that handles localisation.
/// </summary>
public abstract class BaseEventLogger : IModManager.IEventHandler
{
public abstract void ProgressUpdate(IPercent? progress);
protected abstract void LogMessage(string message);

public void InstallNoMods() =>
LogMessage($"No mod archives to install");
public void InstallStart() =>
LogMessage("Installing mods:");
public void InstallCurrent(string packageName) =>
LogMessage($"- {packageName}");
public void InstallEnd()
{
}

public void PostProcessingNotRequired() =>
LogMessage("Post-processing not required");
public void PostProcessingStart() =>
LogMessage("Post-processing:");
public void ExtractingBootfiles(string? packageName) =>
LogMessage($"Extracting bootfiles from {packageName ?? "game"}");
public void ExtractingBootfilesErrorMultiple(IReadOnlyCollection<string> bootfilesPackageNames)
{
LogMessage("Multiple bootfiles found:");
foreach (var packageName in bootfilesPackageNames)
{
LogMessage($"- {packageName}");
}
}
public void PostProcessingVehicles() =>
LogMessage("- Appending crd file entries");
public void PostProcessingTracks() =>
LogMessage("- Appending trd file entries");
public void PostProcessingDrivelines() =>
LogMessage("- Appending driveline records");
public void PostProcessingEnd()
{
}

public void UninstallNoMods() =>
LogMessage("No previously installed mods found. Skipping uninstall phase.");
public void UninstallStart() =>
LogMessage($"Uninstalling mods:");
public void UninstallCurrent(string packageName) =>
LogMessage($"- {packageName}");
public void UninstallSkipModified(string filePath) =>
LogMessage($" Skipping modified file {filePath}");
public void UninstallEnd()
{
}
}
10 changes: 10 additions & 0 deletions src/Core/BootfilesManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Core;

public class BootfilesManager
{
public const string BootfilesPrefix = "__bootfiles";

// TODO Make it an instance method when not called all over the place
internal static bool IsBootFiles(string packageName) =>
packageName.StartsWith(BootfilesPrefix);
}
2 changes: 1 addition & 1 deletion src/Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class GameConfig : Game.IConfig
public string ProcessName { get; set; } = "Undefined";
}

public class ModInstallConfig : ModFactory.IConfig
public class ModInstallConfig : ModInstaller.IConfig, InstallationFactory.IConfig
{
public IEnumerable<string> DirsAtRoot { get; set; } = Array.Empty<string>();
public IEnumerable<string> ExcludedFromInstall { get; set; } = Array.Empty<string>();
Expand Down
9 changes: 4 additions & 5 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
<PackageReference Include="Gameloop.Vdf" Version="0.6.2" />
<PackageReference Include="Gameloop.Vdf.JsonConverter" Version="0.2.1" />
<PackageReference Include="PCarsTools" Version="1.1.2.5" />
<PackageReference Include="Squid-Box.SevenZipSharp.Lite" Version="1.6.2.24" />
<PackageReference Include="Octokit" Version="11.0.1" />
</ItemGroup>

<ItemGroup>
<!-- FIXME enter the correct version once PR merges and released (0.1.6?) -->
<PackageReference Include="OpenSimTools.LibArchive.Net" Version="0.1.6-beta0001" />
<PackageReference Include="Octokit" Version="11.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="21.0.2" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions src/Core/IInstallationFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Core.Mods;

namespace Core;

public interface IInstallationFactory
{
IInstaller GeneratedBootfilesInstaller();
IInstaller ModInstaller(ModPackage modPackage);
}
9 changes: 0 additions & 9 deletions src/Core/IModFactory.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/Core/IModInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Core.Mods;
using Core.State;

namespace Core;
public interface IModInstaller
{
void InstallPackages(IReadOnlyCollection<ModPackage> packages, string installDir, Action<IInstallation> afterInstall, ModInstaller.IEventHandler eventHandler, CancellationToken cancellationToken);
void UninstallPackages(InternalInstallationState currentState, string installDir, Action<IInstallation> afterUninstall, ModInstaller.IEventHandler eventHandler, CancellationToken cancellationToken);
}
13 changes: 5 additions & 8 deletions src/Core/IModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

public interface IModManager
{
// Temporary until proper events are provided
public delegate void LogHandler(string logLine);
public delegate void ProgressHandler(double? progress);

public event LogHandler? Logs;
public event ProgressHandler? Progress;
public interface IEventHandler : ModInstaller.IEventHandler
{
}

List<ModState> FetchState();
string DisableMod(string packagePath);
string EnableMod(string packagePath);
ModState AddNewMod(string packagePath);
void DeleteMod(string packagePath);
void InstallEnabledMods(CancellationToken cancellationToken = default);
void UninstallAllMods(CancellationToken cancellationToken = default);
void InstallEnabledMods(IEventHandler eventHandler, CancellationToken cancellationToken = default);
void UninstallAllMods(IEventHandler eventHandler, CancellationToken cancellationToken = default);
}
5 changes: 1 addition & 4 deletions src/Core/ITempDir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

public interface ITempDir
{
string BasePath
{
get;
}
string BasePath { get; }
void Cleanup();
}
5 changes: 3 additions & 2 deletions src/Core/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public static IModManager CreateModManager(Config config)
var tempDir = new SubdirectoryTempDir(modsDir);
var statePersistence = new JsonFileStatePersistence(modsDir);
var modRepository = new ModRepository(modsDir);
var modFactory = new ModFactory(config.ModInstall, game);
var installationFactory = new InstallationFactory(game, tempDir, config.ModInstall);
var safeFileDelete = new WindowsRecyclingBin();
return new ModManager(game, modRepository, modFactory, statePersistence, safeFileDelete, tempDir);
var modInstaller = new ModInstaller(installationFactory, tempDir, config.ModInstall);
return new ModManager(game, modRepository, modInstaller, statePersistence, safeFileDelete, tempDir);
}
}
28 changes: 28 additions & 0 deletions src/Core/InstallationFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Core.Mods;
using Core.Games;

namespace Core;

public class InstallationFactory : IInstallationFactory
{
public interface IConfig : BaseInstaller.IConfig
{
}

private readonly IConfig config;
private readonly IGame game;
private readonly ITempDir tempDir;

public InstallationFactory(IGame game, ITempDir tempDir, IConfig config)
{
this.game = game;
this.tempDir = tempDir;
this.config = config;
}

public IInstaller ModInstaller(ModPackage modPackage) =>
new ModArchiveInstaller(modPackage.PackageName, modPackage.FsHash, tempDir, config, modPackage.FullPath);

public IInstaller GeneratedBootfilesInstaller() =>
new GeneratedBootfilesInstaller(tempDir, config, game);
}
26 changes: 0 additions & 26 deletions src/Core/ModFactory.cs

This file was deleted.

Loading