-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
73 lines (59 loc) · 2.03 KB
/
Database.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
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
namespace Decal_Loader;
public class Database {
public static Database instance { get; private set; }
private readonly string configPath;
private readonly string configFile;
private Dictionary<string, string> data; // <uri, guid>
public static void Init(string configPath, string configFile) {
instance = new(configPath, configFile);
}
private Database(string configPath, string configFile) {
this.configPath = configPath;
this.configFile = configFile;
Load();
}
private void Load() {
try {
if (File.Exists(configFile)) {
var json = File.ReadAllText(configFile);
data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
}
} catch (Exception e) {
Debug.LogError(e.Message);
}
data ??= [];
}
public GUID? GetGuidForUri(string uri) {
uri = SanitizeUrl(uri);
if (data.TryGetValue(uri, out var value)) return GUID.Parse(value);
return null;
}
/**
* Removes the config path from the URI if present.
* Also replaces `\` with `/`.
*
* This ensures that any given file should compute to the same URI no matter where the user's app data folder is, or what OS the game is running on.
*/
private string SanitizeUrl(string uri) {
return uri.Replace(configPath, "").Replace(@"\", "/");
}
public void Add(string uri, GUID guid) {
uri = SanitizeUrl(uri);
data[uri] = guid.ToString();
Save();
}
private void Save() {
try {
var json = JsonConvert.SerializeObject(data, Formatting.Indented);
Directory.CreateDirectory(configPath);
File.WriteAllText(configFile, json);
} catch (Exception e) {
Debug.LogError(e.Message);
}
}
}