-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
162 lines (122 loc) · 4.14 KB
/
Copy pathConfig.cs
File metadata and controls
162 lines (122 loc) · 4.14 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
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
namespace MapStreaming
{
public class Config : ScriptableObject
{
private static readonly object _lock = new();
#region Serialized Fields
[SerializeField] public int distance = 3;
[SerializeField] public string path = "Assets/Chunks/Chunk {location} {kind}.{extension}";
[SerializeField] public int size = 500;
[SerializeField] public bool verbose;
[SerializeField] private List<string> cache = new();
#endregion
public Dictionary<string, Vector3Int> Chunks
{
get
{
lock (_lock)
{
Dictionary<string, Vector3Int> result = new();
foreach (var item in cache)
{
var parts = item.Split(":");
if (parts.Length < 4) continue;
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);
var z = int.Parse(parts[2]);
var p = parts[3];
result[p] = new Vector3Int(x, y, z);
}
return result;
}
}
}
public void Add(string scenePath, Vector3Int location)
{
lock (_lock)
{
foreach (var item in cache)
{
var parts = item.Split(":");
if (parts.Length < 4) continue;
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);
var z = int.Parse(parts[2]);
if (x == location.x && y == location.y && z == location.z)
throw new Exception("Duplicate chunk location.");
if (parts[3] == scenePath) throw new Exception("Duplicate scene");
}
cache.Add($"{location.x}:{location.y}:{location.z}:{scenePath}");
Save();
}
}
public Vector3Int? GetLocation(string scenePath)
{
Chunks.TryGetValue(scenePath, out var result);
return result;
}
[CanBeNull]
public string GetPath(Vector3Int location)
{
lock (_lock)
{
foreach (var item in cache)
{
var parts = item.Split(":");
if (parts.Length < 4) continue;
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);
var z = int.Parse(parts[2]);
if (x != location.x || y != location.y || z != location.z) continue;
return parts[3];
}
}
return null;
}
public void Remove(string scenePath)
{
lock (_lock)
{
foreach (var item in cache)
{
var parts = item.Split(":");
if (parts.Length < 4 || parts[3] != scenePath) continue;
cache.Remove(item);
break;
}
}
Save();
}
public void Remove(Vector3Int location)
{
lock (_lock)
{
foreach (var item in cache)
{
var parts = item.Split(":");
if (parts.Length < 4) continue;
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);
var z = int.Parse(parts[2]);
if (x != location.x || y != location.y || z != location.z) continue;
cache.Remove(item);
break;
}
}
Save();
}
public void Save()
{
if (Application.isPlaying) return;
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
AssetDatabase.SaveAssetIfDirty(this);
#endif
}
}
}