forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateCache.cs
More file actions
127 lines (106 loc) · 4.26 KB
/
Copy pathStateCache.cs
File metadata and controls
127 lines (106 loc) · 4.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Text;
using UnityEditor.Utils;
using UnityEngine;
namespace UnityEditor
{
class StateCache<T>
{
string m_CacheFolder;
Dictionary<string, T> m_Cache = new Dictionary<string, T>();
public StateCache(string cacheFolder)
{
if (string.IsNullOrEmpty(cacheFolder))
throw new ArgumentException("cacheFolder cannot be null or empty string", cacheFolder);
if (cacheFolder.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
{
throw new ArgumentException("Cache folder path has invalid path characters: '" + cacheFolder + "'");
}
cacheFolder = cacheFolder.ConvertSeparatorsToUnity();
if (!cacheFolder.EndsWith("/"))
{
Debug.LogError("The cache folder path should end with a forward slash: '/'. Path: " + cacheFolder + ". Fixed up.");
cacheFolder += "/";
}
if (cacheFolder.StartsWith("/"))
{
Debug.LogError("The cache folder path should not start with a forward slash: '/'. Path: " + cacheFolder + ". Fixed up."); // since on OSX a leading '/' means the root directory
cacheFolder = cacheFolder.TrimStart(new[] { '/' });
}
m_CacheFolder = cacheFolder;
}
public string cacheFolderPath { get { return m_CacheFolder; } }
public void SetState(string key, T obj)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be null or empty string", key);
if (obj == null)
throw new ArgumentNullException("obj");
string json = JsonUtility.ToJson(obj);
var filePath = GetFilePathForKey(key);
try
{
string directory = System.IO.Path.GetDirectoryName(filePath);
System.IO.Directory.CreateDirectory(directory);
System.IO.File.WriteAllText(filePath, json, Encoding.UTF8); // Persist state
}
catch (Exception e)
{
Debug.LogError(string.Format("Error saving file {0}. Error: {1}", filePath, e.ToString()));
}
m_Cache[key] = obj;
}
public T GetState(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be null or empty string", key);
T obj;
if (m_Cache.TryGetValue(key, out obj))
return obj;
string filePath = GetFilePathForKey(key);
if (System.IO.File.Exists(filePath))
{
string jsonString = null;
try
{
jsonString = System.IO.File.ReadAllText(filePath, Encoding.UTF8);
}
catch (Exception e)
{
Debug.LogError(string.Format("Error loading file {0}. Error: {1}", filePath, e.ToString()));
return default(T);
}
try
{
obj = JsonUtility.FromJson<T>(jsonString);
}
catch (ArgumentException exception)
{
Debug.LogError(string.Format("Invalid file content for {0}. Removing file. Error: {1}", filePath, exception.ToString()));
RemoveState(key);
return default(T);
}
m_Cache[key] = obj;
return obj;
}
return default(T);
}
public void RemoveState(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be null or empty string", key);
m_Cache.Remove(key);
string filePath = GetFilePathForKey(key);
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
}
public string GetFilePathForKey(string key)
{
return m_CacheFolder + key + ".json";
}
}
}