Skip to content

Commit 3e88303

Browse files
committed
Reimport config JSON only on change
Fix LDtkProject apply issue with config. I need to change LDtkConfigData.WriteJson to return the output path and an out bool isChanged.
1 parent aac64a5 commit 3e88303

2 files changed

Lines changed: 64 additions & 55 deletions

File tree

Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ internal sealed class LDtkProjectImporter : LDtkJsonImporter<LDtkProjectFile>
3131
public const string CREATE_LEVEL_BOUNDS_TRIGGER = nameof(_createLevelBoundsTrigger);
3232
public const string USE_PARALLAX = nameof(_useParallax);
3333
public const string SCALE_ENTITIES = nameof(_scaleEntities);
34-
34+
3535
public const string INTGRID = nameof(_intGridValues);
3636
public const string ENTITIES = nameof(_entities);
37-
37+
3838
public const string ENUM_GENERATE = nameof(_enumGenerate);
3939
public const string ENUM_PATH = nameof(_enumPath);
4040
public const string ENUM_NAMESPACE = nameof(_enumNamespace);
41-
42-
public const string USE_LAYER_SORTING_ORDERS = nameof(_useLayerCustomSortingOrders);
43-
public const string LAYER_SORTING_ORDERS = nameof(_layerCustomSortingOrders);
44-
41+
42+
public const string USE_LAYER_SORTING_ORDERS = nameof(_useLayerCustomSortingOrders);
43+
public const string LAYER_SORTING_ORDERS = nameof(_layerCustomSortingOrders);
44+
4545
/// <summary>
4646
/// This is cached into the meta file upon an import. Could be null if the import was a failure. Invisible to the inspector.
4747
/// </summary>
@@ -56,19 +56,19 @@ internal sealed class LDtkProjectImporter : LDtkJsonImporter<LDtkProjectFile>
5656
[SerializeField] private bool _createLevelBoundsTrigger = false;
5757
[SerializeField] private bool _useParallax = true;
5858
[SerializeField] private bool _scaleEntities = true;
59-
59+
6060
[SerializeField] private LDtkAssetIntGridValue[] _intGridValues = Array.Empty<LDtkAssetIntGridValue>();
61-
61+
6262
[SerializeField] private LDtkAssetEntity[] _entities = Array.Empty<LDtkAssetEntity>();
63-
63+
6464
[SerializeField] private bool _enumGenerate = false;
6565
[SerializeField] private string _enumPath = null;
6666
[SerializeField] private string _enumNamespace = string.Empty;
6767

6868
[SerializeField] private bool _useLayerCustomSortingOrders = false;
69-
[SerializeField] private LDtkLayerCustomSortingOrder[] _layerCustomSortingOrders = Array.Empty<LDtkLayerCustomSortingOrder>();
69+
[SerializeField] private LDtkLayerCustomSortingOrder[] _layerCustomSortingOrders = Array.Empty<LDtkLayerCustomSortingOrder>();
70+
7071

71-
7272
public LDtkProjectFile JsonFile => _jsonFile;
7373
public bool IntGridValueColorsVisible => _intGridValueColorsVisible;
7474
public int PixelsPerUnit => _pixelsPerUnit;
@@ -123,38 +123,38 @@ protected override void Import()
123123
FailImport();
124124
return;
125125
}
126-
126+
127127
if (IsVersionOutdated())
128128
{
129129
BufferEditorCache();
130130
FailImport();
131131
return;
132132
}
133-
133+
134134
LDtkProfiler.BeginSample("CreateJsonAsset");
135135
CreateJsonAsset();
136136
LDtkProfiler.EndSample();
137-
137+
138138
if (!TryGetJson(out LdtkJson json))
139139
{
140140
Logger.LogError("Json deserialization error. Not importing.");
141141
BufferEditorCache();
142142
FailImport();
143143
return;
144144
}
145-
145+
146146
LDtkProfiler.BeginSample("CacheSchemaDefs");
147147
CacheSchemaDefs(json);
148148
LDtkProfiler.EndSample();
149149

150150
LDtkProfiler.BeginSample("CreateArtifactAsset");
151151
CreateArtifactAsset(json);
152152
LDtkProfiler.EndSample();
153-
153+
154154
LDtkProfiler.BeginSample("MakeDefObjects");
155155
MakeDefObjects(json);
156156
LDtkProfiler.EndSample();
157-
157+
158158
//if for whatever reason (or backwards compatibility), if the ppu is -1 in any capacity
159159
LDtkProfiler.BeginSample("SetPixelsPerUnit");
160160
LDtkPpuInitializer ppu = new LDtkPpuInitializer(_pixelsPerUnit, assetPath, assetPath);
@@ -164,27 +164,27 @@ protected override void Import()
164164
EditorUtility.SetDirty(this);
165165
}
166166
LDtkProfiler.EndSample();
167-
167+
168168
LDtkProfiler.BeginSample("CreateTableOfContents");
169169
TryCreateTableOfContents(json);
170170
LDtkProfiler.EndSample();
171171

172172
LDtkProfiler.BeginSample("MainBuild");
173173
MainBuild(json);
174174
LDtkProfiler.EndSample();
175-
175+
176176
LDtkProfiler.BeginSample("TryGenerateEnums");
177177
TryGenerateEnums(json);
178178
LDtkProfiler.EndSample();
179-
179+
180180
LDtkProfiler.BeginSample("CreateConfigurationFile");
181181
GenerateConfigurationFile(json);
182182
LDtkProfiler.EndSample();
183-
183+
184184
LDtkProfiler.BeginSample("BufferEditorCache");
185185
BufferEditorCache();
186186
LDtkProfiler.EndSample();
187-
187+
188188
LDtkProfiler.BeginSample("ReleaseDefs");
189189
ReleaseDefs();
190190
LDtkProfiler.EndSample();
@@ -197,11 +197,11 @@ private void MakeDefObjects(LdtkJson json)
197197
LDtkProfiler.BeginSample("InitializeFromProject");
198198
DefinitionObjects.InitializeFromProject(json.Defs, artifacts);
199199
LDtkProfiler.EndSample();
200-
200+
201201
LDtkProfiler.BeginSample("Set _definitions");
202202
_artifacts._definitions = DefinitionObjects.Defs;
203203
LDtkProfiler.EndSample();
204-
204+
205205
LDtkProfiler.BeginSample("AddAllObjectsToAsset");
206206
foreach (var obj in DefinitionObjects.Defs)
207207
{
@@ -242,18 +242,18 @@ private void TryCreateTableOfContents(LdtkJson json)
242242
{
243243
return;
244244
}
245-
245+
246246
Toc = ScriptableObject.CreateInstance<LDtkTableOfContents>();
247247
Toc.name += AssetName + "_Toc";
248248

249249
LDtkTocFieldFactory factory = new LDtkTocFieldFactory(json, this, this);
250-
250+
251251
LDtkProfiler.BeginSample("Toc_IndexEntitiesAndFieldsByIdentifiers");
252252
factory.IndexEntitiesAndFieldsByIdentifiers();
253253
LDtkProfiler.EndSample();
254254

255255
Toc.InitializeList(json);
256-
256+
257257
LDtkFieldParser.CacheRecentBuilder(null);
258258
LDtkProfiler.BeginSample("Toc_GenerateAndAddEntries");
259259
foreach (LdtkTableOfContentEntry tocEntry in json.Toc)
@@ -262,8 +262,8 @@ private void TryCreateTableOfContents(LdtkJson json)
262262
Toc.AddEntry(tocEntry, output.Definition, output.Fields);
263263
}
264264
LDtkProfiler.EndSample();
265-
266-
265+
266+
267267
ImportContext.AddObjectToAsset("toc", Toc, LDtkIconUtility.LoadListIcon());
268268
}
269269

@@ -288,13 +288,20 @@ private void GenerateConfigurationFile(LdtkJson json)
288288
Entities = _entities,
289289
ScaleEntities = _scaleEntities,
290290
};
291-
config.WriteJson(assetPath);
292-
293-
//importing the asset if it doesn't exist due to the asset database not refreshing this automatically
294-
//NOTE: This is not allowed during the import of another asset because of determinism
295-
//AssetDatabase.ImportAsset(writePath);
291+
string writePath = config.WriteJson(assetPath, out bool isChanged);
292+
293+
if (isChanged)
294+
{
295+
EditorApplication.delayCall += () =>
296+
{
297+
if (File.Exists(writePath))
298+
{
299+
AssetDatabase.ImportAsset(writePath, ImportAssetOptions.ForceUpdate);
300+
}
301+
};
302+
}
296303
}
297-
304+
298305
private void BufferEditorCache()
299306
{
300307
EditorApplication.delayCall += () =>
@@ -309,11 +316,11 @@ private void MainBuild(LdtkJson json)
309316
var preAction = new LDtkAssetProcessorActionCache();
310317
LDtkAssetProcessorInvoker.AddPreProcessProject(preAction, json, AssetName);
311318
LDtkProfiler.EndSample();
312-
319+
313320
LDtkProfiler.BeginSample("RunPreprocessors");
314321
preAction.Process();
315322
LDtkProfiler.EndSample();
316-
323+
317324
LDtkProfiler.BeginSample("ImportProject");
318325
LDtkBuilderProjectFactory factory = new LDtkBuilderProjectFactory(this);
319326
factory.Import(json);
@@ -327,7 +334,7 @@ private void TryGenerateEnums(LdtkJson json)
327334
{
328335
return;
329336
}
330-
337+
331338
LDtkProjectImporterEnumGenerator enumGenerator = new LDtkProjectImporterEnumGenerator(json.Defs.Enums, ImportContext, _enumPath, _enumNamespace);
332339
enumGenerator.Generate();
333340
}
@@ -336,12 +343,12 @@ private void CreateArtifactAsset(LdtkJson json)
336343
{
337344
_artifacts = ScriptableObject.CreateInstance<LDtkArtifactAssets>();
338345
_artifacts.name = AssetName + "_Artifacts";
339-
346+
340347
LDtkProfiler.BeginSample("CreateAllBackgrounds");
341348
LDtkBackgroundSliceCreator bgMaker = new LDtkBackgroundSliceCreator(this);
342349
List<Sprite> allBackgrounds = bgMaker.CreateAllBackgrounds(json);
343350
LDtkProfiler.EndSample();
344-
351+
345352
_artifacts._backgrounds = new List<Sprite>(allBackgrounds);
346353
foreach (Sprite bg in allBackgrounds)
347354
{
@@ -354,7 +361,7 @@ private void CreateArtifactAsset(LdtkJson json)
354361
}
355362
ImportContext.AddObjectToAsset("artifacts", _artifacts, (Texture2D)LDtkIconUtility.GetUnityIcon("Image"));
356363
}
357-
364+
358365
public TileBase GetIntGridValueTile(string key) => GetSerializedImporterAsset(_intGridValues, key);
359366
public GameObject GetEntity(string key) => GetSerializedImporterAsset(_entities, key);
360367
private T GetSerializedImporterAsset<T>(IEnumerable<LDtkAsset<T>> input, string key) where T : Object //todo these should be indexed too for performance.
@@ -382,20 +389,20 @@ private void CreateArtifactAsset(LdtkJson json)
382389
{
383390
continue;
384391
}
385-
392+
386393
return (T)asset.Asset;
387394
}
388-
395+
389396
return default;
390397
}
391-
398+
392399
public void TryCacheArtifactsAsset(LDtkDebugInstance logger)
393400
{
394401
if (_artifacts != null)
395402
{
396403
return;
397404
}
398-
405+
399406
_artifacts = AssetDatabase.LoadAssetAtPath<LDtkArtifactAssets>(assetPath);
400407
if (_artifacts == null)
401408
{
@@ -420,11 +427,11 @@ public Sprite GetBackgroundArtifact(Level level)
420427
}
421428

422429
LDtkArtifactAssets ldtkArtifactAssets = GetArtifactAssets();
423-
string assetName = level.Identifier;
424-
430+
string assetName = level.Identifier;
431+
425432
Sprite asset = ldtkArtifactAssets.GetBackgroundSlow(assetName);
426433
if (asset != null)
427-
{
434+
{
428435
return asset;
429436
}
430437
Logger.LogError($"Tried retrieving a background from the importer's artifacts, but was null: \"{assetName}\"");

Assets/LDtkUnity/Editor/Utility/Artifacts/LDtkConfigData.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ internal struct LDtkConfigData
2222
public bool ScaleEntities;
2323
public LDtkAssetIntGridValue[] IntGridValues;
2424
public LDtkAssetEntity[] Entities;
25-
26-
internal string WriteJson(string projectAssetPath)
25+
26+
internal string WriteJson(string projectAssetPath, out bool isChanged)
2727
{
2828
string writePath = GetPath(projectAssetPath);
2929
string json = EditorJsonUtility.ToJson(this, true);
3030
byte[] byteArray = Encoding.UTF8.GetBytes(json);
31-
31+
isChanged = false;
32+
3233
LDtkPathUtility.TryCreateDirectoryForFile(writePath);
33-
34+
3435
//Only write if the contents are actually changed! Otherwise, it's been observed to pollute source control
3536
//It's not good practice to write files to disk during a scripted importer, but it works for now.
3637
if (File.Exists(writePath))
@@ -41,11 +42,12 @@ internal string WriteJson(string projectAssetPath)
4142
return writePath;
4243
}
4344
}
44-
45+
46+
isChanged = true;
4547
File.WriteAllBytes(writePath, byteArray);
4648
return writePath;
4749
}
48-
50+
4951
internal static LDtkConfigData ReadJson(string assetPath)
5052
{
5153
if (!File.Exists(assetPath))
@@ -60,7 +62,7 @@ internal static LDtkConfigData ReadJson(string assetPath)
6062
EditorJsonUtility.FromJsonOverwrite(json, data);
6163
return data;
6264
}
63-
65+
6466
internal static string GetPath(string projectAssetPath)
6567
{
6668
string dir = Path.GetDirectoryName(projectAssetPath);

0 commit comments

Comments
 (0)