forked from zaphim12/RL2Archipelago
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
87 lines (74 loc) · 2.74 KB
/
Copy pathPlugin.cs
File metadata and controls
87 lines (74 loc) · 2.74 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
86
87
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
namespace RL2Archipelago;
[BepInPlugin(PluginInfo.GUID, PluginInfo.Name, PluginInfo.Version)]
public class Plugin : BaseUnityPlugin
{
public static Plugin Instance { get; private set; }
// Static accessor so every file can write Plugin.Log.LogInfo(...) without grabbing Instance.
public static ManualLogSource Log { get; private set; }
public static APConnectionData ConnectionData { get; private set; }
private static readonly string ConnectionDataFilePath = Path.Combine(
Paths.ConfigPath, "RL2Archipelago", "connection-data.json");
private void Awake()
{
Instance = this;
Log = Logger;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
LoadConnectionData();
Log.LogInfo($"{PluginInfo.Name} v{PluginInfo.Version} loaded.");
}
// ── Connection data ──────────────────────────────────────────────────────────────
/// <summary>
/// Loads AP connection data from disk, if it exists. This is used to pre-populate
/// the connection UI with the last-connected server and slot.
/// </summary>
public static void LoadConnectionData()
{
try
{
if (File.Exists(ConnectionDataFilePath))
{
var json = File.ReadAllText(ConnectionDataFilePath);
ConnectionData = JsonConvert.DeserializeObject<APConnectionData>(json);
Log.LogInfo("Connection data loaded.");
}
else
{
Log.LogDebug("No existing AP connection data found.");
}
}
catch (Exception ex)
{
Log.LogError($"Failed to load connection data: {ex.Message}");
}
}
public static void WriteConnectionData()
{
if (ConnectionData == null) return;
try
{
Directory.CreateDirectory(Path.GetDirectoryName(ConnectionDataFilePath));
var json = JsonConvert.SerializeObject(ConnectionData, Formatting.Indented);
File.WriteAllText(ConnectionDataFilePath, json);
Log.LogDebug("Connection data written.");
}
catch (Exception ex)
{
Log.LogError($"Failed to write connection data: {ex.Message}");
}
}
/// <summary>
/// Creates a fresh APConnectionData and persists it to disk.
/// </summary>
public static void InitNewConnectionData(APConnectionData connData)
{
ConnectionData = connData;
WriteConnectionData();
}
}