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
134 lines (112 loc) · 4.49 KB
/
Copy pathStateCache.cs
File metadata and controls
134 lines (112 loc) · 4.49 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
// 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<Hash128, T> m_Cache = new Dictionary<Hash128, T>();
public string cacheFolderPath { get { return m_CacheFolder; } }
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 void SetState(Hash128 key, T obj)
{
ThrowIfInvalid(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));
}
m_Cache[key] = obj;
}
public T GetState(Hash128 key, T defaultValue = default(T))
{
ThrowIfInvalid(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));
return defaultValue;
}
try
{
obj = JsonUtility.FromJson<T>(jsonString);
}
catch (ArgumentException exception)
{
Debug.LogError(string.Format("Invalid file content for {0}. Removing file. Error: {1}", filePath, exception));
RemoveState(key);
return defaultValue;
}
m_Cache[key] = obj;
return obj;
}
return defaultValue;
}
public void RemoveState(Hash128 key)
{
ThrowIfInvalid(key);
m_Cache.Remove(key);
string filePath = GetFilePathForKey(key);
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
}
void ThrowIfInvalid(Hash128 key)
{
if (!key.isValid)
throw new ArgumentException("Hash128 key is invalid: " + key.ToString());
}
public string GetFilePathForKey(Hash128 key)
{
// Hashed folder structure to ensure we scale with large amounts of state files.
// See: https://medium.com/eonian-technologies/file-name-hashing-creating-a-hashed-directory-structure-eabb03aa4091
string hexKey = key.ToString();
string hexFolder = hexKey.Substring(0, 2) + "/";
return m_CacheFolder + hexFolder + hexKey + ".json";
}
}
}