Skip to content

Commit

Permalink
Add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicAglialoro committed Mar 20, 2023
1 parent 54ac025 commit 930dcd1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 23 deletions.
55 changes: 32 additions & 23 deletions SRXDModManager.Library/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
namespace SRXDModManager.Library;

public class ModManager {
private string gameDirectory;
private string pluginsDirectory;
public string GameDirectory { get; set; }

private GitHubClient gitHubClient;
private JsonSerializer serializer;
private SortedDictionary<string, Mod> loadedMods;

public ModManager(string gameDirectory) {
this.gameDirectory = gameDirectory;
pluginsDirectory = Path.Combine(gameDirectory, "BepInEx", "plugins");

GameDirectory = gameDirectory;
gitHubClient = new GitHubClient();
serializer = new JsonSerializer();
loadedMods = new SortedDictionary<string, Mod>();
Expand All @@ -41,10 +38,10 @@ public Result SetActiveBuild(ActiveBuild build) {
if (build == activeBuild)
return Result.Success();

string activePlayerPath = Path.Combine(gameDirectory, "UnityPlayer.dll");
string tempPlayerPath = Path.Combine(gameDirectory, "UnityPlayer.dll.tmp");
string il2CppPlayerPath = Path.Combine(gameDirectory, "UnityPlayer_IL2CPP.dll");
string monoPlayerPath = Path.Combine(gameDirectory, "UnityPlayer_Mono.dll");
string activePlayerPath = Path.Combine(GameDirectory, "UnityPlayer.dll");
string tempPlayerPath = Path.Combine(GameDirectory, "UnityPlayer.dll.tmp");
string il2CppPlayerPath = Path.Combine(GameDirectory, "UnityPlayer_IL2CPP.dll");
string monoPlayerPath = Path.Combine(GameDirectory, "UnityPlayer_Mono.dll");

File.Move(activePlayerPath, tempPlayerPath);

Expand All @@ -60,8 +57,8 @@ public Result SetActiveBuild(ActiveBuild build) {
break;
}
}
catch (IOException e) {
return Result.Failure(e.Message);
catch (IOException) {
return Result.Failure("An IO exception occurred");
}
finally {
if (File.Exists(tempPlayerPath))
Expand All @@ -75,8 +72,8 @@ public Result<ActiveBuild> GetActiveBuild() {
if (!VerifyGameDirectoryExists().Then(VerifyUnityPlayerExists).TryGetValue(out string failureMessage))
return Result<ActiveBuild>.Failure(failureMessage);

bool monoExists = File.Exists(Path.Combine(gameDirectory, "UnityPlayer_Mono.dll"));
bool il2CppExists = File.Exists(Path.Combine(gameDirectory, "UnityPlayer_IL2CPP.dll"));
bool monoExists = File.Exists(Path.Combine(GameDirectory, "UnityPlayer_Mono.dll"));
bool il2CppExists = File.Exists(Path.Combine(GameDirectory, "UnityPlayer_IL2CPP.dll"));

if (!il2CppExists && monoExists)
return Result<ActiveBuild>.Success(ActiveBuild.Il2Cpp);
Expand All @@ -88,10 +85,14 @@ public Result<ActiveBuild> GetActiveBuild() {
}

public Result<IReadOnlyList<Mod>> RefreshLoadedMods() {
string pluginsDirectory = Path.Combine(GameDirectory, "BepInEx", "plugins");

lock (loadedMods) {
loadedMods.Clear();

if (!VerifyGameDirectoryExists().Then(VerifyPluginsDirectoryExists).TryGetValue(out string failureMessage))
if (!VerifyGameDirectoryExists()
.Then(VerifyPluginsDirectoryExists)
.TryGetValue(out string failureMessage))
return Result<IReadOnlyList<Mod>>.Failure(failureMessage);

foreach (string directory in Directory.GetDirectories(pluginsDirectory)) {
Expand Down Expand Up @@ -136,21 +137,23 @@ public async Task<Result<Mod>> DownloadMod(string repository) {
}

private Result VerifyGameDirectoryExists() {
if (!Directory.Exists(gameDirectory))
return Result.Failure($"Could not find game directory {gameDirectory}");
if (!Directory.Exists(GameDirectory))
return Result.Failure($"Could not find game directory {GameDirectory}");

return Result.Success();
}

private Result VerifyPluginsDirectoryExists() {
string pluginsDirectory = Path.Combine(GameDirectory, "BepInEx", "plugins");

if (!Directory.Exists(pluginsDirectory))
return Result.Failure($"Could not find plugins directory {pluginsDirectory}. Ensure that BepInEx is installed");

return Result.Success();
}

private Result VerifyUnityPlayerExists() {
if (!File.Exists(Path.Combine(gameDirectory, "UnityPlayer.dll")))
if (!File.Exists(Path.Combine(GameDirectory, "UnityPlayer.dll")))
return Result.Failure("Could not find UnityPlayer.dll");

return Result.Success();
Expand Down Expand Up @@ -179,10 +182,13 @@ private async Task<Result<Mod>> PerformDownload(GitHubAsset zipAsset, Version ex
.TryGetValue(out var stream, out string failureMessage))
return Result<Mod>.Failure(failureMessage);

string tempDirectory = Path.Combine(Path.GetTempPath(), zipAsset.Name);
string pluginsDirectory = Path.Combine(GameDirectory, "BepInEx", "plugins");
string tempDirectory = Path.Combine(pluginsDirectory, zipAsset.Name, ".tmp");

if (!Directory.Exists(tempDirectory))
Directory.CreateDirectory(tempDirectory);
if (Directory.Exists(tempDirectory))
Directory.Delete(tempDirectory, true);

Directory.CreateDirectory(tempDirectory);

try {
using (var archive = new ZipArchive(stream)) {
Expand All @@ -192,7 +198,7 @@ private async Task<Result<Mod>> PerformDownload(GitHubAsset zipAsset, Version ex

if (!string.IsNullOrWhiteSpace(fileDirectory) && !Directory.Exists(fileDirectory))
Directory.CreateDirectory(fileDirectory);

using var entryStream = entry.Open();
using var fileStream = File.OpenWrite(path);

Expand All @@ -214,14 +220,17 @@ private async Task<Result<Mod>> PerformDownload(GitHubAsset zipAsset, Version ex
return Result<Mod>.Failure(failureMessage);

string directory = Path.Combine(pluginsDirectory, manifest.Name);

if (Directory.Exists(directory))
Directory.Delete(directory, true);

Directory.Move(tempDirectory, directory);

return Result<Mod>.Success(CreateModFromManifest(manifest, directory, version));
}
catch (IOException) {
return Result<Mod>.Failure("An IO exception occurred");
}
finally {
if (Directory.Exists(tempDirectory))
Directory.Delete(tempDirectory, true);
Expand Down
27 changes: 27 additions & 0 deletions SRXDModManager/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SRXDModManager.Library;

namespace SRXDModManager;
Expand Down Expand Up @@ -137,6 +140,30 @@ public void GetAllModInfo() {
}

public void RefreshMods() {
string configPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json");
string gameDirectory = null;

if (File.Exists(configPath)) {
try {
var config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(configPath));

gameDirectory = config.GameDirectory;
Console.WriteLine($"Using game directory {gameDirectory}");
}
catch (JsonException) {
Console.WriteLine("Failed to deserialize config.json");
}
}
else
Console.WriteLine("config.json not found");

if (gameDirectory == null) {
Console.WriteLine("Defaulting game directory to C:\\Program Files (x86)\\Steam\\steamapps\\common\\Spin Rhythm");
gameDirectory = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Spin Rhythm";
}

modManager.GameDirectory = gameDirectory;

if (!modManager.RefreshLoadedMods().TryGetValue(out var mods, out string failureMessage)) {
Console.WriteLine($"Failed to load mods: {failureMessage}");

Expand Down
8 changes: 8 additions & 0 deletions SRXDModManager/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Newtonsoft.Json;

namespace SRXDModManager;

public class Config {
[JsonProperty("gameDirectory")]
public string GameDirectory { get; set; } = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Spin Rhythm";
}
2 changes: 2 additions & 0 deletions SRXDModManager/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using SRXDModManager.Library;

Expand Down
4 changes: 4 additions & 0 deletions SRXDModManager/SRXDModManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
Expand All @@ -55,6 +58,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CommandLine.cs" />
<Compile Include="Config.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions SRXDModManager/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.CommandLine" version="2.0.0-beta4.22272.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
Expand Down

0 comments on commit 930dcd1

Please sign in to comment.