-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDCLPlayerPrefs.cs
More file actions
164 lines (130 loc) · 5.26 KB
/
Copy pathDCLPlayerPrefs.cs
File metadata and controls
164 lines (130 loc) · 5.26 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
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
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Multiplayer.PlayMode;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace DCL.Prefs
{
/// <summary>
/// A wrapper based on UnityEngine.PlayerPrefs for storing preferences / settings.
/// </summary>
public static class DCLPlayerPrefs
{
private const string VECTOR2_KEY_FORMAT = "{0}_{1}";
private static IDCLPrefs dclPrefs;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Initialize()
{
IReadOnlyList<string> playmodeTags = CurrentPlayer.Tags;
Initialize(playmodeTags.Contains("PrefsInMemory"));
}
public static void SetString(string key, string value, bool save = false)
{
dclPrefs.SetString(key, value);
if (save)
Save();
}
public static void SetInt(string key, int value, bool save = false)
{
dclPrefs.SetInt(key, value);
if (save)
Save();
}
public static void SetVector2Int(string key, Vector2Int value, bool save = false)
{
dclPrefs.SetInt(string.Format(VECTOR2_KEY_FORMAT, "X", key), value.x);
dclPrefs.SetInt(string.Format(VECTOR2_KEY_FORMAT, "Y", key), value.y);
if(save)
Save();
}
public static void SetFloat(string key, float value, bool save = false)
{
dclPrefs.SetFloat(key, value);
if (save)
Save();
}
public static string GetString(string key, string defaultValue = "") =>
dclPrefs.GetString(key, defaultValue);
public static int GetInt(string key, int defaultValue = 0) =>
dclPrefs.GetInt(key, defaultValue);
public static float GetFloat(string key, float defaultValue = 0f) =>
dclPrefs.GetFloat(key, defaultValue);
public static Vector2Int GetVector2Int(string key, Vector2Int defaultValue)
{
int x = dclPrefs.GetInt(string.Format(VECTOR2_KEY_FORMAT, "X", key), defaultValue.x);
int y = dclPrefs.GetInt(string.Format(VECTOR2_KEY_FORMAT, "Y", key), defaultValue.y);
return new Vector2Int(x, y);
}
public static bool HasKey(string key) =>
dclPrefs.HasKey(key);
public static bool HasVectorKey(string key) =>
dclPrefs.HasKey(string.Format(VECTOR2_KEY_FORMAT, "X", key));
public static void DeleteKey(string key, bool save = false)
{
dclPrefs.DeleteKey(key);
if (save)
Save();
}
public static void DeleteVector2Key(string key)
{
dclPrefs.DeleteKey(string.Format(VECTOR2_KEY_FORMAT, "X", key));
dclPrefs.DeleteKey(string.Format(VECTOR2_KEY_FORMAT, "Y", key));
}
public static void SetBool(string key, bool value, bool save = false)
{
dclPrefs.SetBool(key, value);
if (save)
Save();
}
public static bool GetBool(string key, bool defaultValue = false) =>
dclPrefs.GetBool(key, defaultValue);
public static void DeleteAll() =>
dclPrefs.DeleteAll();
public static void Save() =>
dclPrefs.Save();
/// <summary>
/// Synchronous save that blocks until data is written to disk.
/// Use in OnApplicationQuit or other shutdown paths where async save may not complete.
/// </summary>
public static void SaveSync() =>
dclPrefs.SaveSync();
private static void Initialize(bool inMemory)
{
if (dclPrefs != null)
throw new InvalidOperationException("DCLPrefs already initialized.");
dclPrefs = inMemory ? new InMemoryDCLPlayerPrefs() : new FileDCLPlayerPrefs();
// ExitUtils lives in Utility which already depends on DCL.Prefs (via PersistentSetting), so subscribe directly here
Application.quitting += OnQuitting;
}
private static void OnQuitting()
{
Application.quitting -= OnQuitting;
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
(dclPrefs as IDisposable)?.Dispose();
dclPrefs = null;
Debug.Log($"[ExitUtils] [DCLPlayerPrefs] cleanup took {stopwatch.ElapsedMilliseconds}ms");
}
#if UNITY_EDITOR
[MenuItem("Edit/Clear All DCLPlayerPrefs", priority = 280)]
private static void ClearDCLPlayerPrefs()
{
string[] files = Directory.GetFiles(Application.persistentDataPath, "userdata_*");
foreach (string file in files)
File.Delete(file);
}
[MenuItem("Edit/Clear All DCLPlayerPrefs", validate = true)]
private static bool ValidateClearDCLPlayerPrefs() =>
!Application.isPlaying;
[MenuItem("Decentraland/PlayerPrefs/Reset Nearby Voice Intro Tip")]
private static void ResetNearbyVoiceIntroTip()
{
DeleteKey(DCLPrefKeys.NEARBY_VOICE_TIP_DISMISSED, save: true);
Debug.Log("Nearby Voice Intro Tip has been reset.");
}
#endif
}
}