-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
85 lines (74 loc) · 3.04 KB
/
Copy pathPlugin.cs
File metadata and controls
85 lines (74 loc) · 3.04 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx;
namespace SeaCull;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
private const string CONFIG_FILENAME = "SeaCull.ini";
public static Dictionary<string, object> ConfigOptions = new();
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
LoadConfig();
}
private void Start()
{
SeaCuller.Initialize();
}
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(new[] { '=' }, 2);
optionsDict[keyValue[0]] = keyValue[1]; // Add the key and value to the dictionary
}
}
ConfigOptions["CullTiles"] = bool.Parse(optionsDict["CullTiles"]);
ConfigOptions["CullDistance"] = float.Parse(optionsDict["CullDistance"]);
ConfigOptions["ZeeAnimationTargetFPS"] = int.Parse(optionsDict["ZeeAnimationTargetFPS"]);
}
catch (Exception)
{
LoadConfig( /*loadDefault =*/ true); // Load config with default values
}
}
private static string GetEmbeddedPath(string folderName = "") // Get the path of embedded resources
{
string projectName = Assembly.GetExecutingAssembly().GetName().Name;
string fullPath = $"{projectName}.{folderName}";
return fullPath;
}
private string ReadTextResource(string fullResourceName)
{
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fullResourceName);
if (stream == null)
{
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
}
}