forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRCXStorage.cs
140 lines (129 loc) · 3.29 KB
/
VRCXStorage.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
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace VRCX
{
public class VRCXStorage
{
public static readonly VRCXStorage Instance;
private static readonly ReaderWriterLockSlim m_Lock = new ReaderWriterLockSlim();
private static Dictionary<string, string> m_Storage = new Dictionary<string, string>();
private static string m_JsonPath = Path.Combine(Program.AppDataDirectory, "VRCX.json");
private static bool m_Dirty;
static VRCXStorage()
{
Instance = new VRCXStorage();
}
public static void Load()
{
m_Lock.EnterWriteLock();
try
{
JsonSerializer.Deserialize(m_JsonPath, ref m_Storage);
m_Dirty = false;
}
finally
{
m_Lock.ExitWriteLock();
}
}
public static void Save()
{
m_Lock.EnterReadLock();
try
{
if (m_Dirty)
{
JsonSerializer.Serialize(m_JsonPath, m_Storage);
m_Dirty = false;
}
}
finally
{
m_Lock.ExitReadLock();
}
}
public void Flush()
{
Save();
}
public void Clear()
{
m_Lock.EnterWriteLock();
try
{
if (m_Storage.Count > 0)
{
m_Storage.Clear();
m_Dirty = true;
}
}
finally
{
m_Lock.ExitWriteLock();
}
}
public bool Remove(string key)
{
m_Lock.EnterWriteLock();
try
{
var result = m_Storage.Remove(key);
if (result)
{
m_Dirty = true;
}
return result;
}
finally
{
m_Lock.ExitWriteLock();
}
}
public string Get(string key)
{
m_Lock.EnterReadLock();
try
{
return m_Storage.TryGetValue(key, out string value)
? value
: string.Empty;
}
finally
{
m_Lock.ExitReadLock();
}
}
public void Set(string key, string value)
{
m_Lock.EnterWriteLock();
try
{
m_Storage[key] = value;
m_Dirty = true;
}
finally
{
m_Lock.ExitWriteLock();
}
}
public string GetAll()
{
m_Lock.EnterReadLock();
try
{
return System.Text.Json.JsonSerializer.Serialize(m_Storage);
}
finally
{
m_Lock.ExitReadLock();
}
}
}
}