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
8 changes: 4 additions & 4 deletions OpenKh.Tests.ModsManager/ModsServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void Dispose()
{
try
{
DeleteDir(Path.Combine(ConfigurationService.ModsGamePath, "test"));
DeleteDir(Path.Combine(ConfigurationService.InstalledModsPath, "test"));
}
catch (Exception e)
{
output.WriteLine(e.Message);
}
try
{
DeleteDir(Path.Combine(ConfigurationService.ModCollectionsPath, "test"));
DeleteDir(Path.Combine(ConfigurationService.InstalledCollectionsPath, "test"));
}
catch (Exception e)
{
Expand All @@ -45,7 +45,7 @@ public void Dispose()

private void AddMod()
{
var modDir = ConfigurationService.ModsGamePath;
var modDir = ConfigurationService.InstalledModsPath;
Directory.CreateDirectory(Path.Combine(modDir, "test/test"));
using (File.Create(Path.Combine(modDir, "test/test/mod.yml"))) { }
}
Expand All @@ -57,7 +57,7 @@ private void EnableMod()

private void AddCollectionMod()
{
var modDir = ConfigurationService.ModCollectionsPath;
var modDir = ConfigurationService.InstalledCollectionsPath;
Directory.CreateDirectory(Path.Combine(modDir, "test/test"));
using (File.Create(Path.Combine(modDir, "test/test/mod.yml")))
{ }
Expand Down
93 changes: 73 additions & 20 deletions OpenKh.Tools.ModsManager/Services/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ namespace OpenKh.Tools.ModsManager.Services
{
public static class ConfigurationService
{
private class LegacyConfigMigration
{
private static readonly IDeserializer _legacyvaluedeserializer =
new DeserializerBuilder()
.IgnoreFields()
.IgnoreUnmatchedProperties()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
public string ModCollectionPath { get; internal set; }
public string ModCollectionsPath { get; internal set; }
public string GameModPath { get; internal set; }
public string GameDataPath { get; internal set; }

public static LegacyConfigMigration Open(string fileName)
{
if (!File.Exists(fileName))
return new LegacyConfigMigration();

using var reader = new StreamReader(fileName);
return _legacyvaluedeserializer.Deserialize<LegacyConfigMigration>(reader);
}
}

private class Config
{
private static readonly IDeserializer _deserializer =
Expand All @@ -26,10 +49,10 @@ private class Config
.Build();

public int WizardVersionNumber { get; set; }
public string ModCollectionPath { get; internal set; }
public string ModCollectionsPath { get; internal set; }
public string GameModPath { get; internal set; }
public string GameDataPath { get; internal set; }
public string ExtractedGameDataPath { get; internal set; }
public string InstalledModsPath { get; internal set; }
public string InstalledCollectionsPath { get; internal set; }
public string CompiledModPath { get; internal set; }
Comment thread
shananas marked this conversation as resolved.
public int GameEdition { get; internal set; } = 1;
public string IsoLocationKH2 { get; internal set; }
public string IsoLocationKH1 { get; internal set; }
Expand Down Expand Up @@ -86,6 +109,7 @@ public static Config Open(string fileName)
private static string EnabledCollectionModsPathBBS = Path.Combine(StoragePath, "collection-mods-BBS.json");
private static string EnabledCollectionModsPathRECOM = Path.Combine(StoragePath, "collection-mods-ReCoM.json");
private static string EnabledCollectionModsPathKH3D = Path.Combine(StoragePath, "collection-mods-KH3D.json");
private static readonly LegacyConfigMigration _legacyconfig = LegacyConfigMigration.Open(ConfigPath);
private static readonly Config _config = Config.Open(ConfigPath);
public static string PresetPath = Path.Combine(StoragePath, "presets");
private static readonly HashSet<string> _supportedGames = new HashSet<string>()
Expand All @@ -108,9 +132,19 @@ public class YamlGenPref

static ConfigurationService()
{
string modsPath = Path.GetFullPath(Path.Combine(ModsGamePath, ".."));
if (!Directory.Exists(Path.Combine(modsPath, "collections")))
Directory.CreateDirectory(Path.Combine(modsPath, "collections"));
if (!string.IsNullOrEmpty(_legacyconfig.GameDataPath) && string.IsNullOrEmpty(_config.ExtractedGameDataPath))
_config.ExtractedGameDataPath = _legacyconfig.GameDataPath;
if (!string.IsNullOrEmpty(_legacyconfig.ModCollectionPath) && string.IsNullOrEmpty(_config.InstalledModsPath))
_config.InstalledModsPath = _legacyconfig.ModCollectionPath;
if (!string.IsNullOrEmpty(_legacyconfig.ModCollectionsPath) && string.IsNullOrEmpty(_config.InstalledCollectionsPath))
_config.InstalledCollectionsPath = _legacyconfig.ModCollectionsPath;
if (!string.IsNullOrEmpty(_legacyconfig.GameModPath) && string.IsNullOrEmpty(_config.CompiledModPath))
_config.CompiledModPath = _legacyconfig.GameModPath;


string modsPath = Path.GetFullPath(Path.Combine(InstalledModsPath, ".."));
if (!Directory.Exists(Path.Combine(InstalledCollectionsPath)))
Directory.CreateDirectory(InstalledCollectionsPath);
if (!Directory.Exists(Path.Combine(modsPath, "kh2")))
Directory.CreateDirectory(Path.Combine(modsPath, "kh2"));
if (!Directory.Exists(Path.Combine(modsPath, "kh1")))
Expand Down Expand Up @@ -250,43 +284,62 @@ public static int WizardVersionNumber
_config.Save(ConfigPath);
}
}

public static string ModsGamePath
public static string GameDataLocation
{
get => Path.Combine(_config.ModCollectionPath ?? Path.GetFullPath(Path.Combine(StoragePath, "mods")), LaunchGame);
get => _config.ExtractedGameDataPath ?? Path.GetFullPath(Path.Combine(StoragePath, "data"));
set
{
_config.ModCollectionPath = value;
_config.ExtractedGameDataPath = value;
_config.Save(ConfigPath);
}
}

public static string ModCollectionsPath
public static string InstalledModsPath
{
get => _config.ModCollectionsPath ?? Path.GetFullPath(Path.Combine(StoragePath, "mods", "collections"));
get
{
if (!string.IsNullOrEmpty(_config.InstalledModsPath) && _config.InstalledModsPath != _config.CompiledModPath)
return Path.Combine(_config.InstalledModsPath, LaunchGame);
else
return Path.Combine(_config.InstalledModsPath ?? Path.GetFullPath(StoragePath), "mods", LaunchGame);
}
set
{
_config.ModCollectionsPath = value;
_config.InstalledModsPath = value;
_config.Save(ConfigPath);
}
}

public static string GameModPath
public static string InstalledCollectionsPath
{
get => Path.Combine(_config.GameModPath ?? Path.GetFullPath(Path.Combine(StoragePath, "mod")), LaunchGame);
get
{
if (string.IsNullOrEmpty(_config.InstalledCollectionsPath))
return Path.GetFullPath(Path.Combine(StoragePath, "mods", "collections"));
else if (_config.InstalledCollectionsPath != _config.InstalledModsPath)
return _config.InstalledCollectionsPath;
else
return Path.Combine(_config.InstalledCollectionsPath, "collections");
}
set
{
_config.GameModPath = value;
_config.InstalledCollectionsPath = value;
_config.Save(ConfigPath);
}
}

public static string GameDataLocation
public static string CompiledModPath
{
get => _config.GameDataPath ?? Path.GetFullPath(Path.Combine(StoragePath, "data"));
get
{
if (!string.IsNullOrEmpty(_config.CompiledModPath) && _config.CompiledModPath != _config.InstalledModsPath)
return Path.Combine(_config.CompiledModPath, LaunchGame);
else
return Path.Combine(_config.CompiledModPath ?? Path.GetFullPath(StoragePath), "mod", LaunchGame);
}
set
{
_config.GameDataPath = value;
_config.CompiledModPath = value;
_config.Save(ConfigPath);
}
}
Expand Down
20 changes: 10 additions & 10 deletions OpenKh.Tools.ModsManager/Services/ModsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static IEnumerable<string> UnorderedMods
{
get
{
var modsPath = ConfigurationService.ModsGamePath;
var modsPath = ConfigurationService.InstalledModsPath;
foreach (var dir in Directory.GetDirectories(modsPath))
{
var authorName = Path.GetFileName(dir);
Expand All @@ -76,7 +76,7 @@ public static IEnumerable<string> UnorderedMods
if (File.Exists(Path.Combine(dir, ModMetadata)))
yield return authorName;
}
var collectionsPath = ConfigurationService.ModCollectionsPath;
var collectionsPath = ConfigurationService.InstalledCollectionsPath;
foreach (var dir in Directory.GetDirectories(collectionsPath))
{
var authorName = Path.GetFileName(dir);
Expand Down Expand Up @@ -447,11 +447,11 @@ public static void InstallModFromLua(string fileName)
}

public static string GetModPath(string repositoryName) =>
Path.Combine(ConfigurationService.ModsGamePath, repositoryName);
Path.Combine(ConfigurationService.InstalledModsPath, repositoryName);
public static string GetModsGamePath(string repositoryName, string game) =>
Path.Combine(ConfigurationService.ModsGamePath, "..", game, repositoryName);
Path.Combine(ConfigurationService.InstalledModsPath, "..", game, repositoryName);
public static string GetCollectionPath(string repositoryName) =>
Path.Combine(ConfigurationService.ModCollectionsPath, repositoryName);
Path.Combine(ConfigurationService.InstalledCollectionsPath, repositoryName);

public static IEnumerable<ModModel> GetMods(IEnumerable<string> modNames)
{
Expand Down Expand Up @@ -528,19 +528,19 @@ public static Task Update(string modName,

public static Task<bool> RunPatcherAsync(bool fastMode) => Task.Run(() => Handle(() =>
{
if (Directory.Exists(ConfigurationService.GameModPath))
if (Directory.Exists(ConfigurationService.CompiledModPath))
{
try
{
Directory.Delete(ConfigurationService.GameModPath, true);
Directory.Delete(ConfigurationService.CompiledModPath, true);
}
catch (Exception ex)
{
Log.Warn("Unable to fully clean the mod directory:\n{0}", ex.Message);
}
}

Directory.CreateDirectory(ConfigurationService.GameModPath);
Directory.CreateDirectory(ConfigurationService.CompiledModPath);

var patcherProcessor = new PatcherProcessor();
var modsList = GetMods(EnabledMods).ToList();
Expand All @@ -557,7 +557,7 @@ public static Task<bool> RunPatcherAsync(bool fastMode) => Task.Run(() => Handle

patcherProcessor.Patch(
Path.Combine(ConfigurationService.GameDataLocation, ConfigurationService.LaunchGame),
ConfigurationService.GameModPath,
ConfigurationService.CompiledModPath,
mod.Metadata,
mod.Path,
ConfigurationService.GameEdition,
Expand All @@ -569,7 +569,7 @@ public static Task<bool> RunPatcherAsync(bool fastMode) => Task.Run(() => Handle
enabledOptionalAssets);
}

using var packageMapWriter = new StreamWriter(Path.Combine(ConfigurationService.GameModPath, "patch-package-map.txt"));
using var packageMapWriter = new StreamWriter(Path.Combine(ConfigurationService.CompiledModPath, "patch-package-map.txt"));
foreach (var entry in packageMap)
packageMapWriter.WriteLine(entry.Key + " $$$$ " + entry.Value);
packageMapWriter.Flush();
Expand Down
4 changes: 2 additions & 2 deletions OpenKh.Tools.ModsManager/Services/OperationDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static int GetFinalNamePath(string fileName, out string finalFileName)
return -1; // Error Code -1: denied
}

basePath = ConfigurationService.GameModPath;
basePath = ConfigurationService.CompiledModPath;
finalFileName = Path.Combine(basePath, fileName);
if (File.Exists(finalFileName))
{
Expand Down Expand Up @@ -126,7 +126,7 @@ private static int GetFinalNamePath(string fileName, out string finalFileName)
.Replace($"/{region}/", $"/{fallback}/")
.Replace($".a.{region}", $".a.{fallback}")
.Replace($".apdx", $".a.{fallback}");
basePath = ConfigurationService.GameModPath;
basePath = ConfigurationService.CompiledModPath;
finalFileName = Path.Combine(basePath, temptativeRegionalFallbackFileName);
if (File.Exists(finalFileName))
{
Expand Down
Loading
Loading