Skip to content

Commit 8050166

Browse files
committed
Added support for testing if mod is enabled by searching for mod entry in userGameState.cgs config
1 parent 6fb110d commit 8050166

15 files changed

+399
-14
lines changed

Patch.API/Helpers/GameStateUtil.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.IO;
2+
3+
namespace Patch.API.Helpers {
4+
public static class GameStateUtil {
5+
internal static IGameState Util { get; set; }
6+
7+
public static bool? IsModEnabled(string workingPath) {
8+
return Util.IsModEnabled(workingPath);
9+
}
10+
}
11+
}

Patch.API/Helpers/IGameState.cs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Patch.API.Helpers {
2+
public interface IGameState {
3+
bool? IsModEnabled(string workingPath);
4+
}
5+
}

Patch.API/IPaths.cs

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public interface IPaths {
1919
/// Path to game's directory Mods folder
2020
/// </summary>
2121
string FilesModsPath { get; }
22+
23+
/// <summary>
24+
/// Path to user AppData folder.
25+
/// </summary>
26+
string AppDataPath { get; }
2227

2328
/// <summary>
2429
/// Path to user AppData mods folder.

Patch.API/Patch.API.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
</PropertyGroup>
3434
<ItemGroup>
3535
<Compile Include="AssemblyToPatch.cs" />
36+
<Compile Include="Helpers\GameStateUtil.cs" />
37+
<Compile Include="Helpers\IGameState.cs" />
3638
<Compile Include="ILogger.cs" />
3739
<Compile Include="IPatch.cs" />
3840
<Compile Include="IPaths.cs" />

Patch.API/Properties/AssemblyInfo.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
// General Information about an assembly is controlled through the following
@@ -12,6 +13,7 @@
1213
[assembly: AssemblyCopyright("Copyright © 2020")]
1314
[assembly: AssemblyTrademark("")]
1415
[assembly: AssemblyCulture("")]
16+
[assembly: InternalsVisibleTo("PatchLoader")]
1517

1618
// Setting ComVisible to false makes the types in this assembly not visible
1719
// to COM components. If you need to access a type in this assembly from
@@ -21,15 +23,5 @@
2123
// The following GUID is for the ID of the typelib if this project is exposed to COM
2224
[assembly: Guid("C5C7F3DD-F203-4510-8525-7EE6FB0E6124")]
2325

24-
// Version information for an assembly consists of the following four values:
25-
//
26-
// Major Version
27-
// Minor Version
28-
// Build Number
29-
// Revision
30-
//
31-
// You can specify all the values or you can default the Build and Revision Numbers
32-
// by using the '*' as shown below:
33-
// [assembly: AssemblyVersion("1.0.*")]
3426
[assembly: AssemblyVersion("2.0.0.0")]
3527
[assembly: AssemblyFileVersion("2.0.0.0")]

PatchLoader/EntryPoint.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4+
using Patch.API.Helpers;
5+
using PatchLoader.Helpers;
46
using Utils;
57
using Logger = Utils.Logger;
68

@@ -30,6 +32,10 @@ public static void Main(string[] args) {
3032
_logger.Info("******** Workshop mods loading disabled via --noWorkshop commandline argument. Processing workshop mods directories will be skipped ********");
3133
}
3234

35+
SettingsFile.Logger = new WithPrefixLogger(_logger, "PatchLoader.SettingsFile");
36+
SettingsFile.LocalAppDataPath = _paths.AppDataPath;
37+
GameStateUtil.Util = new GameState();
38+
3339
try {
3440
AppDomain.CurrentDomain.TypeResolve += AssemblyResolver;
3541
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver;

PatchLoader/Helpers/GameState.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.IO;
2+
using Patch.API.Helpers;
3+
4+
namespace PatchLoader.Helpers {
5+
internal class GameState: IGameState {
6+
public bool? IsModEnabled(string workingPath) {
7+
string name = Path.GetFileNameWithoutExtension(workingPath);
8+
string key = name + workingPath.GetHashCode().ToString() + ".enabled";
9+
SavedBool save = new SavedBool(key, "userGameState");
10+
return save.m_Exists ? save.value : (bool?) null;
11+
}
12+
}
13+
}

PatchLoader/Helpers/SavedBool.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace PatchLoader.Helpers {
2+
3+
internal class SavedBool : SavedValue {
4+
public readonly bool value;
5+
public SavedBool(string name, string fileName) : this(name, fileName, false) { }
6+
7+
public SavedBool(string name, string fileName, bool def) : base(name, fileName) {
8+
value = def;
9+
m_Exists = settingsFile.GetValue(name, ref value);
10+
}
11+
12+
public static implicit operator bool(SavedBool s) {
13+
return s.value;
14+
}
15+
16+
public override string ToString() {
17+
return value.ToString();
18+
}
19+
}
20+
}

PatchLoader/Helpers/SavedFloat.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace PatchLoader.Helpers {
2+
3+
internal class SavedFloat : SavedValue {
4+
public readonly float value;
5+
6+
public SavedFloat(string name, string fileName) : this(name, fileName, 0) { }
7+
8+
public SavedFloat(string name, string fileName, float def) : base(name, fileName) {
9+
value = def;
10+
m_Exists = settingsFile.GetValue(name, ref value);
11+
}
12+
13+
public static implicit operator float(SavedFloat s) {
14+
return s.value;
15+
}
16+
17+
public override string ToString() {
18+
return value.ToString();
19+
}
20+
}
21+
}

PatchLoader/Helpers/SavedInt.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace PatchLoader.Helpers {
2+
3+
internal class SavedInt : SavedValue {
4+
public readonly int value;
5+
6+
public SavedInt(string name, string fileName) : this(name, fileName, 0) { }
7+
8+
public SavedInt(string name, string fileName, int def) : base(name, fileName) {
9+
value = def;
10+
m_Exists = settingsFile.GetValue(name, ref value);
11+
}
12+
13+
public static implicit operator int(SavedInt s) {
14+
return s.value;
15+
}
16+
17+
public override string ToString() {
18+
return value.ToString();
19+
}
20+
}
21+
}

PatchLoader/Helpers/SavedString.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace PatchLoader.Helpers {
2+
3+
internal class SavedString : SavedValue
4+
{
5+
public readonly string value;
6+
7+
public SavedString(string name, string fileName) : this(name, fileName, "") { }
8+
9+
public SavedString(string name, string fileName, string def) : base(name, fileName) {
10+
value = def;
11+
m_Exists = settingsFile.GetValue(name, ref value);
12+
}
13+
14+
public static implicit operator string(SavedString s) {
15+
return s.value;
16+
}
17+
18+
public override string ToString() {
19+
return value.ToString();
20+
}
21+
}
22+
}

PatchLoader/Helpers/SavedValue.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace PatchLoader.Helpers {
2+
3+
internal abstract class SavedValue {
4+
public SavedValue(string name, string fileName) {
5+
this.name = name;
6+
this.m_FileName = fileName;
7+
}
8+
9+
protected string name { get; }
10+
11+
protected string m_FileName;
12+
13+
internal bool m_Exists;
14+
public bool exists => m_Exists;
15+
16+
private SettingsFile m_SettingsFile;
17+
internal SettingsFile settingsFile => m_SettingsFile ?? (m_SettingsFile = SettingsFile.GetOrLoad(m_FileName));
18+
19+
public ushort version => settingsFile.version;
20+
21+
public static bool operator ==(SavedValue x, SavedValue y) {
22+
if (object.ReferenceEquals(x, null)) {
23+
return object.ReferenceEquals(y, null);
24+
}
25+
return x.Equals(y);
26+
}
27+
28+
public static bool operator !=(SavedValue x, SavedValue y) {
29+
return !(x == y);
30+
}
31+
32+
public override bool Equals(object obj) {
33+
return obj != null && ((SavedValue)obj).name == name && ((SavedValue)obj).m_FileName == m_FileName;
34+
}
35+
36+
public bool Equals(SavedValue obj) {
37+
return !(obj == null) && obj.name == name && obj.m_FileName == m_FileName;
38+
}
39+
40+
public override int GetHashCode() {
41+
return name.GetHashCode() ^ m_FileName.GetHashCode();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)