-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSaveData.cs
More file actions
117 lines (94 loc) · 3.64 KB
/
SaveData.cs
File metadata and controls
117 lines (94 loc) · 3.64 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace BetterWeaponColourMenu
{
class SaveData
{
public static void LoadData()
{
path = Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf("\\")) + "\\saveData.json";
Debug.Log("Saving colour menu data to " + path);
if (!File.Exists(path))
{
File.Create(path).Close();
}
FileInfo fInfo = new FileInfo(path);
if (fInfo.Exists)
{
using (StreamReader jFile = fInfo.OpenText())
{
colourMenuBoolData = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(jFile.ReadToEnd());
if (colourMenuBoolData == null)
colourMenuBoolData = new Dictionary<string, Dictionary<string, string>>();
jFile.Close();
}
}
else
{
Debug.Log("Couldn't find a save file, making one now");
fInfo.Create();
}
//instance = UnityEngine.JsonUtility.FromJson<SaveData>(File.ReadAllText(path));
}
public static void SaveDataToFile()
{
path = Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf("\\")) + "\\saveData.json";
Debug.Log("Saving colour menu data to " + path);
FileInfo fInfo = new FileInfo(path);
File.WriteAllText(fInfo.FullName, JsonConvert.SerializeObject(colourMenuBoolData));
//if (!File.Exists(path))
//{
// File.Create(path).Close();
//}
}
public static void SetSaveValue(string dataKey, string weapon, object value, bool forceNotSave = false)
{
if (colourMenuBoolData == null) LoadData();
if (!colourMenuBoolData.ContainsKey(dataKey))
{
var dict = new Dictionary<string, string>();
dict.Add(weapon, value.ToString());
colourMenuBoolData.Add(dataKey, dict);
}
else if (!colourMenuBoolData[dataKey].ContainsKey(weapon))
{
colourMenuBoolData[dataKey].Add(weapon, value.ToString());
}
else
{
colourMenuBoolData[dataKey][weapon] = value.ToString();
}
if (!forceNotSave) SaveDataToFile();
}
public static string RetriveSaveValue(string dataKey, string weapon, string fallback = "MISSING DATA")
{
if (colourMenuBoolData == null) LoadData();
if (!colourMenuBoolData.ContainsKey(dataKey))
{
var dict = new Dictionary<string, string>();
dict.Add(weapon, fallback);
colourMenuBoolData.Add(dataKey, dict);
SaveDataToFile();
}
else if (!colourMenuBoolData[dataKey].ContainsKey(weapon))
{
colourMenuBoolData[dataKey].Add(weapon, fallback);
SaveDataToFile();
}
return colourMenuBoolData[dataKey][weapon];
}
//public static SaveData instance;
static Dictionary<string, Dictionary<string, string>> colourMenuBoolData;
//Dictionary<string, Dictionary<string, Color>> colourMenuColourData;
static string path = "";
}
}