-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.cs
More file actions
279 lines (214 loc) · 9.02 KB
/
Copy pathManager.cs
File metadata and controls
279 lines (214 loc) · 9.02 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MapStreaming
{
public static class Manager
{
private static ConcurrentDictionary<Vector3Int, (int Index, bool Open, string Path)> _chunks;
private static Config _config;
private static Vector3Int _focus;
private static readonly object _lock = new();
private static ConcurrentDictionary<string, Vector3Int> _reverse;
static Manager()
{
Cleanup();
if (Config.verbose) Debug.Log("Registering callbacks for MapStreaming.Manager.");
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
#if UNITY_EDITOR
EditorSceneManager.sceneOpened -= OnSceneOpened;
EditorSceneManager.sceneOpened += OnSceneOpened;
EditorSceneManager.sceneClosed -= OnSceneClosed;
EditorSceneManager.sceneClosed += OnSceneClosed;
#endif
Application.quitting -= Cleanup;
Application.quitting += Cleanup;
Refresh();
}
public static Config Config
{
get
{
if (_config is not null) return _config;
if (!Application.isEditor)
{
if (Resources.FindObjectsOfTypeAll<Config>() is { Length: > 0 } resources)
{
if (resources.Length > 1) Debug.LogWarning("Found more than one config file.");
return _config = resources[0];
}
Debug.LogWarning("No config file found.");
}
var created = ScriptableObject.CreateInstance<Config>();
#if UNITY_EDITOR
var path = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(created))
.Replace("Config.cs", "Config.asset");
if (AssetDatabase.LoadAssetAtPath<Config>(path) is { } existing) return _config = existing;
Debug.Log("Looking for MapStreaming.Config outside of default location.");
if (AssetDatabase.FindAssetGUIDs($"t:{typeof(Config)}") is { Length: > 0 } guids)
{
if (guids.Length > 1) Debug.LogWarning("Found more than one config file.");
if (AssetDatabase.LoadAssetByGUID<Config>(guids[0]) is { } found) return _config = found;
}
AssetDatabase.CreateAsset(created, path);
#endif
return created;
}
}
public static Transform Tracker { get; set; }
private static void Cleanup()
{
if (Config.verbose) Debug.Log("Cleaning callbacks for MapStreaming.Manager.");
SceneManager.sceneUnloaded -= OnSceneUnloaded;
SceneManager.sceneLoaded -= OnSceneLoaded;
#if UNITY_EDITOR
EditorSceneManager.sceneOpened -= OnSceneOpened;
EditorSceneManager.sceneClosed -= OnSceneClosed;
#endif
}
public static string FilePath(Vector3Int location, string kind, string extension)
{
var result = Config.path;
result = result.Replace("{location}", "x={x} y={y} z={z}");
result = result.Replace("{x}", $"{(short)location.x:X4}");
result = result.Replace("{y}", $"{(short)location.y:X4}");
result = result.Replace("{z}", $"{(short)location.z:X4}");
result = result.Replace("{extension}", extension);
result = result.Replace("{kind}", kind);
return result;
}
#if UNITY_EDITOR
private static void OnSceneClosed(Scene scene)
{
TrackSceneChange(scene, false);
}
#endif
private static void OnSceneLoaded(Scene scene, LoadSceneMode _)
{
TrackSceneChange(scene, true);
}
#if UNITY_EDITOR
private static void OnSceneOpened(Scene scene, OpenSceneMode _)
{
TrackSceneChange(scene, true);
}
#endif
private static void OnSceneUnloaded(Scene scene)
{
TrackSceneChange(scene, false);
}
private static void Refresh()
{
if (Config.verbose) Debug.Log("Refreshing and reloading all MapStreaming.Manager data.");
lock (_lock)
{
#if UNITY_EDITOR
var missing = Config.Chunks.Where(chunk => !AssetDatabase.AssetPathExists(chunk.Key)).ToList();
foreach (var chunk in missing) Config.Remove(chunk.Key);
EditorBuildSettings.scenes =
EditorBuildSettings.scenes.ToList().FindAll(item => item.enabled).ToArray();
#endif
_chunks ??= new ConcurrentDictionary<Vector3Int, (int, bool, string)>();
_chunks.Clear();
_reverse ??= new ConcurrentDictionary<string, Vector3Int>();
_reverse.Clear();
for (var index = 0; index < SceneManager.sceneCountInBuildSettings; index++)
{
var path = SceneUtility.GetScenePathByBuildIndex(index);
if (Config.GetLocation(path) is not { } location) continue;
_chunks[location] = (index, true, path);
_reverse[path] = location;
}
}
if (Config.verbose) Debug.Log("Refresh complete.");
}
private static void TrackSceneChange(Scene scene, bool open)
{
lock (_lock)
{
if (!_reverse.TryGetValue(scene.path, out var location)) return;
if (!_chunks.TryGetValue(location, out var chunk)) return;
_chunks[location] = (chunk.Index, open, chunk.Path);
}
}
public static void Add(Vector3Int location)
{
if (Application.isPlaying) return;
Refresh();
#if UNITY_EDITOR
lock (_lock)
{
if (Config.GetPath(location) is not null)
{
Debug.Log($"Chunk location x={location.x} y={location.y} z={location.z} already exists.");
return;
}
}
var chunk = new GameObject("Chunk");
var chunkComponent = chunk.AddComponent<Chunk>();
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
var scenePath = FilePath(location, "Scene", "unity");
SceneManager.MoveGameObjectToScene(chunk, scene);
EditorSceneManager.SaveScene(scene, scenePath);
if (new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes) is { } buildScenes)
if (buildScenes.All(item => item.path != scenePath))
EditorBuildSettings.scenes =
buildScenes.Append(new EditorBuildSettingsScene(scenePath, true)).ToArray();
Config.Add(scenePath, location);
chunkComponent.location = location;
chunkComponent.Setup();
EditorSceneManager.SaveScene(scene);
#else
Debug.LogError("Unable to change chunk data while playing.");
#endif
}
public static void Focus(Vector3Int location)
{
#if UNITY_EDITOR
var lastActiveScene = SceneView.lastActiveSceneView;
lastActiveScene.rotation = Quaternion.Euler(0, 0, 0);
lastActiveScene.pivot = location * Config.size;
lastActiveScene.rotation = Quaternion.Euler(90, 0, 0);
#endif
}
public static void Remove(Vector3Int location)
{
if (Application.isPlaying)
{
Debug.LogError("Unable to change chunk data while playing.");
return;
}
lock (_lock)
{
if (Config.GetPath(location) is { } path)
{
if (new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes) is { } buildScenes)
{
for (var i = 0; i < buildScenes.Count; i++)
if (buildScenes[i].path == path)
{
if (SceneManager.sceneCount == 1)
{
Debug.LogError("Unable to remove chunk when it's the only open scene.");
return;
}
#if UNITY_EDITOR
EditorSceneManager.CloseScene(SceneManager.GetSceneByBuildIndex(i), true);
#endif
break;
}
EditorBuildSettings.scenes = buildScenes.Where(s => s.path != path).ToArray();
}
Config.Remove(location);
}
}
}
}
}