-
Notifications
You must be signed in to change notification settings - Fork 4
/
SaveHandler.cs
153 lines (139 loc) · 5.34 KB
/
SaveHandler.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.IO;
using HarmonyLib;
using Newtonsoft.Json;
using UnityEngine;
using System.Collections.Generic;
using BepInEx.Logging;
using LunacidAP.Data;
using static LunacidAP.Data.LunacidGifts;
namespace LunacidAP
{
public class SaveHandler
{
private static ManualLogSource _log;
public SaveHandler(ManualLogSource log)
{
_log = log;
Harmony.CreateAndPatchAll(typeof(SaveHandler));
}
[HarmonyPatch(typeof(Save), "SAVE_FILE")]
[HarmonyPrefix]
private static void SaveFile_SaveArchipelagoData(int Save_Slot, Vector3 POS, CONTROL CON)
{
try
{
_log.LogInfo($"Trying to save to {Save_Slot}");
//SaveData(Save_Slot);
}
catch
{
_log.LogError("Could not save data!");
}
}
[HarmonyPatch(typeof(Save), "LOAD_FILE")]
[HarmonyPostfix]
private static void Load_LoadArchipelagoData(int Save_Slot)
{
try
{
//ReadSave(Save_Slot);
}
catch (Exception ex)
{
_log.LogError($"Failure in {nameof(Load_LoadArchipelagoData)}");
_log.LogError($"Reason: {ex.Message}");
}
}
public static void SaveData(int Save_Slot)
{
var dir = Application.absoluteURL + "ArchSaves/";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var savePath = Path.Combine(dir, $"Save{Save_Slot}.json");
_log.LogInfo($"Saving to {savePath}...");
if (!File.Exists(savePath))
{
}
var newAPSaveData = new APSaveData()
{
SlotName = ConnectionData.SlotName,
HostName = ConnectionData.HostName,
Port = ConnectionData.Port,
Password = ConnectionData.Password,
Symbols = ConnectionData.Index,
DeathLink = ConnectionData.DeathLink,
CheatCount = ConnectionData.CheatedCount,
ObtainedItems = ConnectionData.ReceivedItems,
CheckedLocations = ConnectionData.CompletedLocations,
CommunionHints = ConnectionData.CommunionHints,
Elements = ConnectionData.Elements,
Entrances = ConnectionData.Entrances,
ScoutedLocations = ConnectionData.ScoutedLocations,
EnteredScenes = ConnectionData.EnteredScenes,
ReceivedGifts = ConnectionData.ReceivedGifts,
};
if (ArchipelagoClient.AP.Authenticated && (ConnectionData.Seed == 0))
{
newAPSaveData.Seed = ArchipelagoClient.AP.SlotData.Seed;
}
string json = JsonConvert.SerializeObject(newAPSaveData);
File.WriteAllText(savePath, json);
_log.LogInfo("Save complete!");
}
public static void ReadSave(int Save_Slot)
{
try
{
if (ArchipelagoClient.IsInGame)
{
return; // Don't keep spam loading in situations it isn't relevant; causes data loss.
}
_log.LogInfo($"Reading save {Save_Slot}");
var dir = Application.absoluteURL + "ArchSaves/";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var savePath = Path.Combine(dir, $"Save{Save_Slot}.json");
if (File.Exists(savePath))
{
using StreamReader reader = new StreamReader(savePath);
string text = reader.ReadToEnd();
var loadedSave = JsonConvert.DeserializeObject<APSaveData>(text);
ConnectionData.WriteConnectionData(loadedSave.HostName, loadedSave.Port, loadedSave.SlotName, loadedSave.Password,
loadedSave.Seed, loadedSave.Symbols, loadedSave.DeathLink, loadedSave.CheatCount, loadedSave.ObtainedItems, loadedSave.CheckedLocations,
loadedSave.CommunionHints, loadedSave.Elements, loadedSave.Entrances, loadedSave.ScoutedLocations, loadedSave.EnteredScenes, loadedSave.ReceivedGifts);
return;
}
_log.LogError("SAVE not found");
}
catch (Exception ex)
{
_log.LogError($"Failed to parse json for save {Save_Slot}");
_log.LogError($"{ex}");
}
}
}
internal class APSaveData
{
public string SlotName;
public string HostName;
public int Port;
public string Password;
public int Seed;
public int Symbols;
public bool DeathLink;
public int CheatCount;
public List<ReceivedItem> ObtainedItems;
public List<long> CheckedLocations;
public Dictionary<string, CommunionHint.HintData> CommunionHints;
public Dictionary<string, string> Elements;
public Dictionary<string, string> Entrances;
public SortedDictionary<long, ArchipelagoItem> ScoutedLocations;
public List<string> EnteredScenes;
public List<ReceivedGift> ReceivedGifts;
}
}