-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathLDtkDefinitionObjectsCache.cs
More file actions
295 lines (251 loc) · 10.8 KB
/
LDtkDefinitionObjectsCache.cs
File metadata and controls
295 lines (251 loc) · 10.8 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
namespace LDtkUnity
{
/// <summary>
/// Holds onto all scriptable objects and attempts to link everything by their uids.
///
/// Rules for defining definition objects:
/// - Have all schema fields exist except for deprecated ones.
///
/// - Do a #region for the internal values only to the LDtk editor.
/// All others will do the [field: SerializeField] with a { get; private set; }
///
/// - All uid refs should translate into the appropriate scriptable object by relaying in a second pass over all def objects.
/// - All strings/ints/floats that might represent a color or Vector2 can be turned into the appropriate structs.
/// </summary>
internal class LDtkDefinitionObjectsCache
{
private readonly LDtkDebugInstance _logger;
private Dictionary<int, LDtkDefinitionObject> _defsDict;
internal List<LDtkDefinitionObject> Defs;
//key: tilesetuid, value: rectangle to sprite ref
private Dictionary<int, Dictionary<Rect,Sprite>> _allSprites;
private Dictionary<int, LDtkArtifactAssetsTileset> _tilesetArtifacts;
internal LDtkDefinitionObjectsCache(LDtkDebugInstance logger)
{
_logger = logger;
}
public void InitializeFromProject(Definitions defs, Dictionary<int, LDtkArtifactAssetsTileset> tilesets)
{
LDtkProfiler.BeginSample("InitializeTilesets");
InitializeTilesets(tilesets);
LDtkProfiler.EndSample();
LDtkProfiler.BeginSample("GenerateObjects");
GenerateObjects(defs);
LDtkProfiler.EndSample();
LDtkProfiler.BeginSample("PopulateObjects");
PopulateObjects(defs);
LDtkProfiler.EndSample();
LDtkProfiler.BeginSample("SetObjectNames");
SetObjectNames();
LDtkProfiler.EndSample();
LDtkProfiler.BeginSample("CacheDictToListViaProject");
CacheDictToListViaProject();
LDtkProfiler.EndSample();
}
public void InitializeFromLevel(List<LDtkDefinitionObject> defs, Dictionary<int, LDtkArtifactAssetsTileset> tilesets)
{
Profiler.BeginSample("InitializeTilesets");
InitializeTilesets(tilesets);
Profiler.EndSample();
Defs = defs;
Profiler.BeginSample("CacheListToDictViaLevel");
CacheListToDictViaLevel();
Profiler.EndSample();
}
private void InitializeTilesets(Dictionary<int, LDtkArtifactAssetsTileset> tilesets)
{
tilesets ??= new Dictionary<int, LDtkArtifactAssetsTileset>();
_tilesetArtifacts = new Dictionary<int, LDtkArtifactAssetsTileset>(tilesets.Count);
_allSprites = new Dictionary<int, Dictionary<Rect, Sprite>>(tilesets.Count);
foreach (var pair in tilesets)
{
_tilesetArtifacts[pair.Key] = pair.Value;
Dictionary<Rect, Sprite> dict;
if (pair.Value != null)
{
LDtkProfiler.BeginSample("AllSpritesToConvertedDict");
dict = pair.Value.AllSpritesToConvertedDict();
LDtkProfiler.EndSample();
}
else
{
dict = new Dictionary<Rect, Sprite>();
}
_allSprites.Add(pair.Key, dict);
}
}
private void GenerateObjects(Definitions defs)
{
_defsDict = new Dictionary<int, LDtkDefinitionObject>(defs.Entities.Length + defs.Enums.Length + defs.ExternalEnums.Length + defs.Layers.Length + defs.LevelFields.Length + defs.Tilesets.Length);
foreach (EntityDefinition def in defs.Entities)
{
_defsDict.Add(def.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectEntity>());
foreach (FieldDefinition field in def.FieldDefs)
{
_defsDict.Add(field.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectField>());
}
}
AddEnums(defs.Enums);
AddEnums(defs.ExternalEnums);
void AddEnums(EnumDefinition[] enums)
{
foreach (EnumDefinition def in enums)
{
_defsDict.Add(def.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectEnum>());
}
}
foreach (LayerDefinition def in defs.Layers)
{
_defsDict.Add(def.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectLayer>());
foreach (AutoLayerRuleGroup group in def.AutoRuleGroups)
{
_defsDict.Add(group.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectAutoLayerRuleGroup>());
foreach (AutoLayerRuleDefinition rule in group.Rules)
{
_defsDict.Add(rule.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectAutoLayerRule>());
}
}
}
foreach (FieldDefinition def in defs.LevelFields)
{
_defsDict.Add(def.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectField>());
}
foreach (TilesetDefinition def in defs.Tilesets)
{
_defsDict.Add(def.Uid, ScriptableObject.CreateInstance<LDtkDefinitionObjectTileset>());
}
}
private void PopulateObjects(Definitions defs)
{
foreach (EntityDefinition def in defs.Entities)
{
if (_defsDict[def.Uid] is LDtkDefinitionObjectEntity obj)
{
obj.Populate(this, def);
}
}
AddEnums(defs.Enums);
AddEnums(defs.ExternalEnums);
void AddEnums(EnumDefinition[] enumDefs)
{
foreach (EnumDefinition def in enumDefs)
{
if (_defsDict[def.Uid] is LDtkDefinitionObjectEnum obj)
{
obj.Populate(this, def);
}
}
}
foreach (LayerDefinition def in defs.Layers)
{
if (_defsDict[def.Uid] is LDtkDefinitionObjectLayer obj)
{
obj.Populate(this, def);
}
}
foreach (FieldDefinition def in defs.LevelFields)
{
if (_defsDict[def.Uid] is LDtkDefinitionObjectField obj)
{
obj.Populate(this, def);
}
}
foreach (TilesetDefinition def in defs.Tilesets)
{
if (_defsDict[def.Uid] is LDtkDefinitionObjectTileset obj)
{
obj.Populate(this, def);
}
}
}
private void SetObjectNames()
{
foreach (var obj in _defsDict.Values)
{
obj.SetAssetName();
}
}
public T GetObject<T>(int? uid) where T : ScriptableObject
{
return uid == null ? null : GetObject<T>(uid.Value);
}
public T GetObject<T>(int uid) where T : ScriptableObject
{
if (!_defsDict.TryGetValue(uid, out var obj))
{
_logger.LogError($"Failed to get a \"{typeof(T).Name}\" of uid: {uid}. This is likely from a broken json structure");
return null;
}
if (obj is T t)
{
return t;
}
_logger.LogError($"Failed to get a \"{typeof(T).Name}\" of uid {uid} due to a type mismatch with {obj.GetType().Name}");
return null;
}
/// <summary>
/// Could return null sprite if the tile would only have clear pixels.
/// <seealso cref="LDtkArtifactAssetsTileset.FindAdditionalSpriteForRect"/>
/// </summary>
public Sprite GetSpriteForTilesetRectangle(TilesetRectangle rectangle)
{
if (rectangle == null)
{
return null;
}
if (!_allSprites.TryGetValue(rectangle.TilesetUid, out var sprites))
{
//todo while awaiting this fix, just safely return null if a definition is not found https://github.com/deepnight/ldtk/issues/1107
TilesetDefinition tilesetDef = rectangle.Tileset;
if (tilesetDef == null)
{
//_logger.LogError($"Problem getting sprite for TilesetRectangle def uid {rectangle.TilesetUid}: No definition exists?");
return null;
}
_logger.LogError($"Problem getting sprite for TilesetRectangle def uid \"{tilesetDef.Identifier}\": Couldn't get the dictionary for the tileset uid");
return null;
}
if (sprites.IsNullOrEmpty())
{
//this means there were no sprites; a previous issue is the actual problem to not have any.
return null;
}
if (!sprites.TryGetValue(rectangle.UnityRect, out var sprite))
{
_logger.LogError($"Problem getting sprite for TilesetRectangle def uid {rectangle.TilesetUid}: Couldn't get the sprite from the dictionary for the Rect {rectangle.UnityRect} out of {sprites.Count} possible rects");
return null;
}
return sprite;
}
internal LDtkArtifactAssetsTileset GetTilesetArtifacts(int uid)
{
if (_tilesetArtifacts != null && _tilesetArtifacts.TryGetValue(uid, out var artifacts))
{
return artifacts;
}
return null;
}
private void CacheDictToListViaProject()
{
Defs = new List<LDtkDefinitionObject>(_defsDict.Count);
foreach (LDtkDefinitionObject def in _defsDict.Values)
{
Defs.Add(def);
}
}
private void CacheListToDictViaLevel()
{
_defsDict = new Dictionary<int, LDtkDefinitionObject>(Defs.Count);
foreach (LDtkDefinitionObject def in Defs)
{
if (def is ILDtkUid uid)
{
_defsDict.Add(uid.Uid, def);
}
}
}
}
}