forked from weedeej/S1Property_Upgrades
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModSaveManager.cs
More file actions
120 lines (111 loc) · 4.42 KB
/
ModSaveManager.cs
File metadata and controls
120 lines (111 loc) · 4.42 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
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
using MelonLoader;
using MelonLoader.Utils;
using Newtonsoft.Json;
using ScheduleOne.Persistence;
using ScheduleOne.Property;
using UnityEngine;
namespace PropertyUpgrades
{
public class ExtraLoadingDock
{
[JsonProperty("Position"), JsonConverter(typeof(Vector3Converter))]
public Vector3 Position { get; set; }
[JsonProperty("Rotation"), JsonConverter(typeof(Vector3Converter))]
public Vector3 Rotation { get; set; }
}
public class PropertyData
{
[JsonProperty("EmployeeCapacity")]
public int EmployeeCapacity { get; set; }
[JsonProperty("MixTimePerItemReduction")]
public int MixTimePerItemReduction { get; set; }
[JsonProperty("ExtraGrowSpeedMultiplier")]
public float ExtraGrowSpeedMultiplier { get; set; }
[JsonProperty("ExtraLoadingDocks")]
public ExtraLoadingDock[] ExtraLoadingDocks { get; set; }
}
public class ModSaveManager
{
private string saveFilePath = null;
private string tempSaveFilePath = null;
public Dictionary<string, PropertyData> saveData = new Dictionary<string, PropertyData>();
public ModSaveManager()
{
string saveDirectory = LoadManager.Instance.LoadedGameFolderPath;
int separatorIndex = saveDirectory.IndexOf("Saves\\") + 5;
string idSlotPath = saveDirectory.Substring(separatorIndex);
string[] idSlotArr = idSlotPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
this.saveFilePath = Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades", $"{idSlotArr[0]}_{idSlotArr[1]}.json");
this.tempSaveFilePath = Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades", $"_temp_{idSlotArr[0]}_{idSlotArr[1]}.json");
// Initialize the save manager
if (!Directory.Exists(Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades")))
{
Directory.CreateDirectory(Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades"));
}
}
public void Save()
{
try
{
string json = JsonConvert.SerializeObject(this.saveData, Formatting.Indented);
File.WriteAllText(this.saveFilePath, json);
}
catch (Exception ex)
{
MelonLogger.Error($"Failed to save data: {ex.Message}");
}
}
public void SaveTemp()
{
try
{
string json = JsonConvert.SerializeObject(this.saveData, Formatting.Indented);
File.WriteAllText(this.tempSaveFilePath, json);
}
catch (Exception ex)
{
MelonLogger.Error($"Failed to save data: {ex.Message}");
}
}
public ModSaveManager Load()
{
if (!File.Exists(this.saveFilePath))
{
return this;
}
string json = File.ReadAllText(this.saveFilePath);
this.saveData = JsonConvert.DeserializeObject<Dictionary<string, PropertyData>>(json);
return this;
}
public ModSaveManager LoadTemp()
{
if (!File.Exists(this.tempSaveFilePath) && !File.Exists(this.saveFilePath))
{
return this;
}
string json = File.ReadAllText(File.Exists(this.tempSaveFilePath) ? this.tempSaveFilePath : this.saveFilePath);
this.saveData = JsonConvert.DeserializeObject<Dictionary<string, PropertyData>>(json);
return this;
}
public static void ClearTemp()
{
if (!Directory.Exists(Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades")))
{
Directory.CreateDirectory(Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades"));
}
// Delete all temp files
string[] tempFiles = Directory.GetFiles(Path.Combine(MelonEnvironment.UserDataDirectory, "Property Upgrades"), "_temp_*.json");
foreach (string file in tempFiles)
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
MelonLogger.Error($"Failed to delete temp file: {ex.Message}");
}
}
}
}
}