This repository has been archived by the owner on Feb 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
126 lines (104 loc) · 5.09 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx;
using HarmonyLib;
using FailBetter.Core;
using Sunless.Game.ApplicationProviders;
namespace AllShipsAllSlots;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
internal sealed class Plugin : BaseUnityPlugin
{
public static Plugin Instance;
private static Assembly Assembly { get; } = Assembly.GetExecutingAssembly();
private const string CONFIG_FILENAME = "AllShipsAllSlots_config.ini";
private static bool unlockForward = true;
private static bool unlockAFT = true;
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
Logger.LogInfo("\n _ _ _____ _ _ _ _ _____ _ _ \n /\\ | | |/ ____| | (_) /\\ | | |/ ____| | | | \n / \\ | | | (___ | |__ _ _ __ ___ / \\ | | | (___ | | ___ | |_ ___ \n / /\\ \\ | | |\\___ \\| '_ \\| | '_ \\/ __| / /\\ \\ | | |\\___ \\| |/ _ \\| __/ __|\n / ____ \\| | |____) | | | | | |_) \\__ \\/ ____ \\| | |____) | | (_) | |_\\__ \\\n /_/ \\_\\_|_|_____/|_| |_|_| .__/|___/_/ \\_\\_|_|_____/|_|\\___/ \\__|___/\n | | \n |_| \n");
Instance = this;
LoadConfig(); // Load config
// Initialize Harmony
Harmony.CreateAndPatchAll(typeof(ShipyardProviderPatch));
}
private void LoadConfig(bool loadDefault = false)
{
string[] lines;
if (File.Exists(CONFIG_FILENAME) && !loadDefault)
{
lines = File.ReadAllLines(CONFIG_FILENAME);
}
else
{
Logger.LogWarning("Config not found or corrupt, using default values.");
string file = ReadTextResource(GetEmbeddedPath() + CONFIG_FILENAME); // Get the default config from the embedded resources
lines = file.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // Split the file into lines
}
var optionsDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
try
{
foreach (var line in lines)
{
if (line.Contains('=')) // Check if the line contains an '=' character so it's a valid config line
{
// Remove all spaces from the line and split it at the first occurrence of '=' into two parts
string[] keyValue = line.Replace(" ", "").Split(['='], 2);
optionsDict[keyValue[0]] = keyValue[1]; // Add the key and value to the dictionary
}
}
unlockForward = bool.Parse(optionsDict["unlockforward"]);
unlockAFT = bool.Parse(optionsDict["unlockaft"]);
Logger.LogInfo($"Config loaded - Forward slots: {unlockForward}, AFT slots: {unlockAFT}");
}
catch (Exception)
{
LoadConfig( /*loadDefault =*/ true); // Load config with default values
}
}
public static string GetEmbeddedPath(string folderName = "") // Get the path of embedded resources
{
string projectName = Assembly.GetExecutingAssembly().GetName().Name;
string fullPath = $"{projectName}.{folderName}";
return fullPath;
}
public static string ReadTextResource(string fullResourceName)
{
using (Stream stream = Assembly.GetManifestResourceStream(fullResourceName))
{
if (stream == null)
{
Instance.Logger.LogWarning("Tried to get resource that doesn't exist: " + fullResourceName);
return null; // Return null if the embedded resource doesn't exist
}
using var reader = new StreamReader(stream);
return reader.ReadToEnd(); // Read and return the embedded resource
}
}
[HarmonyPatch(typeof(ShipyardProvider))]
private static class ShipyardProviderPatch
{
[HarmonyPrefix]
[HarmonyPatch("UpdateCombatSlots")]
private static bool PrefixStart(ShipyardProvider __instance, Quality newShip)
{
// Whether the ship has the slot originally
bool hasForward = WellKnownQualityProvider.ShipsWithForwardSlots.Contains(newShip);
bool hasAft = WellKnownQualityProvider.ShipsWithAftSlots.Contains(newShip);
// Whether it should have it based on the config
int shouldHaveForward = unlockForward ? 1 : 0;
int shouldHaveAft = unlockAFT ? 1 : 0;
__instance.CurrentCharacter.AcquireQualityAtExplicitLevel(
WellKnownQualityProvider.Forward,
hasForward ? 1 : shouldHaveForward);
__instance.CurrentCharacter.AcquireQualityAtExplicitLevel(
WellKnownQualityProvider.Aft,
hasAft ? 1 : shouldHaveAft);
return false; // Don't run the original function
}
}
}