-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlueprintStore.cs
More file actions
393 lines (352 loc) · 15.5 KB
/
Copy pathBlueprintStore.cs
File metadata and controls
393 lines (352 loc) · 15.5 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
namespace Fieldwright;
/// <summary>
/// Lightweight blueprint summary for the library UI. Derived from the on-disk file
/// at scan time without keeping the whole schematic in memory.
/// </summary>
public class BlueprintEntry
{
public string Name = string.Empty;
public int SizeX, SizeY, SizeZ;
public int BlockCount;
public string AnchorFaceLabel = string.Empty;
public DateTime ModifiedAt;
public bool HasBackup;
}
/// <summary>
/// Read/write blueprint files in %APPDATA%/VintagestoryData/Blueprints/. Falls back to
/// reading bare BlockSchematic JSON files with an implicit (0,0,0) anchor for
/// compatibility with WorldEdit / BetterRuins schematics.
/// </summary>
public static class BlueprintStore
{
private const string Component = "store";
private const string BlueprintsFolderName = "Blueprints";
// Position bit-packing convention used by BlockSchematic.Indices.
// Matches PosBitMask = 0x3ff (10 bits per axis); same layout used by
// GhostMesh.cs and GhostMatchTracker.cs.
private const uint PosBitMask = 0x3ff;
/// <summary>
/// Build a BlockSchematic from a region without using BlockSchematic.AddArea.
/// The vanilla AddArea captures every entity in the region (mobs, dropped items,
/// falling blocks, projectiles) and calls Entity.OnStoreCollectibleMappings on each
/// to remap block/item references. Some entities (modded or vanilla) have null
/// collectible attributes and trigger a NullReferenceException, killing the entire
/// save. We don't need entity state for the build-along workflow today, so we skip
/// entities entirely. Block entities (chest contents, sign text, chiseled voxels)
/// are also skipped for v0.1.2 because they're not used yet either; future Phase 4
/// chisel work will add per-cell BE capture with proper try/catch.
///
/// Bounds are start-inclusive, endExclusive-exclusive — same convention as
/// BlockSchematic.AddArea, so callers can pass max + (1,1,1) the same way.
/// </summary>
public static BlockSchematic CaptureBlocksOnly(IWorldAccessor world, BlockPos min, BlockPos endExclusive)
{
var schematic = new BlockSchematic();
var ba = world.BlockAccessor;
int sizeX = endExclusive.X - min.X;
int sizeY = endExclusive.Y - min.Y;
int sizeZ = endExclusive.Z - min.Z;
schematic.SizeX = sizeX;
schematic.SizeY = sizeY;
schematic.SizeZ = sizeZ;
var codeToId = new Dictionary<string, int>();
var blockCodes = new Dictionary<int, AssetLocation>();
var blockIds = new List<int>();
var indices = new List<uint>();
int nextId = 1;
for (int dy = 0; dy < sizeY; dy++)
{
for (int dz = 0; dz < sizeZ; dz++)
{
for (int dx = 0; dx < sizeX; dx++)
{
var pos = new BlockPos(min.X + dx, min.Y + dy, min.Z + dz, min.dimension);
var block = ba.GetBlock(pos);
if (block == null || block.Id == 0 || block.Code == null) continue;
var codeStr = block.Code.ToString();
if (!codeToId.TryGetValue(codeStr, out int storedId))
{
storedId = nextId++;
codeToId[codeStr] = storedId;
blockCodes[storedId] = block.Code;
}
blockIds.Add(storedId);
uint packed = (uint)dx | ((uint)dz << 10) | ((uint)dy << 20);
indices.Add(packed);
}
}
}
schematic.BlockCodes = blockCodes;
schematic.BlockIds = blockIds;
schematic.Indices = indices;
EnsureSchematicCollectionsInitialized(schematic);
return schematic;
}
/// <summary>
/// Defensive init for every collection field on BlockSchematic. The JSON round-trip
/// for a v0.1.0-era schematic (or any blueprint saved before BlockSchematic added a
/// given field) leaves those fields null on the loaded copy, and BlockSchematic.Remap
/// inside Init walks them without null checks and NREs.
///
/// Earlier versions hand-typed the null-coalesces for the six fields we knew of,
/// which missed `DecorIndices`, `BlocksUnpacked`, `FluidsLayerUnpacked`,
/// `DecorsUnpacked`, `Connectors`, and the five `Pathway*` / `*CheckPositions`
/// arrays. Fish hit one of those after v0.1.3 shipped (paste still NRE'd in Remap
/// at line 186). Switched to reflection so every present-and-future collection
/// field is covered: any public instance field whose type is `List<T>`,
/// `Dictionary<K,V>`, `HashSet<T>`, or a single-dimensional array, if
/// null, gets assigned an empty default of the right type. Future BlockSchematic
/// additions are covered automatically.
///
/// Reported by Fish, 3CHO, and PhallicYam.
/// </summary>
public static void EnsureSchematicCollectionsInitialized(BlockSchematic schematic)
{
// BlockSchematic.BlockRemaps and ItemRemaps are public static auto-properties on
// the type itself, populated by the game from config at world load. On a remote-
// server-connected client they're never set, and BlockSchematic.Remap iterates
// them at BlockSchematic.cs line 186 / 201 without a null check — this is why
// paste worked in solo (integrated server populated them) but NRE'd on real
// servers in v0.1.4. Initialize both to empty dictionaries if null, which is a
// safe no-op when they're already populated.
BlockSchematic.BlockRemaps ??= new Dictionary<string, Dictionary<string, string>>();
BlockSchematic.ItemRemaps ??= new Dictionary<string, Dictionary<string, string>>();
// Then null-coalesce every instance collection field on the schematic too.
// v0.1.0-era blueprint JSON omits some of these and BlockSchematic.Remap walks
// them without null checks. Reflection means future BlockSchematic fields are
// covered without another patch.
var type = typeof(BlockSchematic);
foreach (var field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (field.GetValue(schematic) != null) continue;
var ft = field.FieldType;
object? empty = null;
if (ft.IsArray)
{
// Empty single-dim array of the element type. Jagged arrays (T[][])
// count as arrays of arrays, so this also covers PathwayOffsets.
var elem = ft.GetElementType();
if (elem != null) empty = System.Array.CreateInstance(elem, 0);
}
else if (ft.IsGenericType)
{
var def = ft.GetGenericTypeDefinition();
if (def == typeof(List<>)
|| def == typeof(Dictionary<,>)
|| def == typeof(HashSet<>))
{
empty = System.Activator.CreateInstance(ft);
}
}
if (empty != null) field.SetValue(schematic, empty);
}
}
public static string GetBlueprintsDirectory(ICoreAPI api)
{
var dir = Path.Combine(GamePaths.DataPath, BlueprintsFolderName);
Directory.CreateDirectory(dir);
return dir;
}
public static string GetBlueprintPath(ICoreAPI api, string name)
{
var sanitized = SanitizeName(name);
return Path.Combine(GetBlueprintsDirectory(api), sanitized + ".json");
}
public static string GetBackupPath(ICoreAPI api, string name)
{
var sanitized = SanitizeName(name);
return Path.Combine(GetBlueprintsDirectory(api), sanitized + ".bak.json");
}
public static bool Exists(ICoreAPI api, string name)
{
return File.Exists(GetBlueprintPath(api, name));
}
/// <summary>
/// If a blueprint with this name exists on disk, copy it to {name}.bak.json
/// (single rolling backup. Previous .bak is overwritten.) Returns the backup
/// path if a backup was made, null otherwise.
/// </summary>
public static string? BackupExisting(ICoreAPI api, string name)
{
var src = GetBlueprintPath(api, name);
if (!File.Exists(src)) return null;
var dst = GetBackupPath(api, name);
File.Copy(src, dst, overwrite: true);
FieldwrightLogger.Info(api, Component, $"backed up '{name}' → {dst}");
return dst;
}
public static void Save(ICoreAPI api, string name, BlueprintFile blueprint)
{
var path = GetBlueprintPath(api, name);
var json = JsonConvert.SerializeObject(blueprint, Formatting.Indented);
File.WriteAllText(path, json);
FieldwrightLogger.Info(api, Component, $"saved blueprint '{name}' → {path} ({json.Length} bytes)");
}
public static BlueprintFile Load(ICoreAPI api, string name)
{
var path = GetBlueprintPath(api, name);
if (!File.Exists(path))
{
throw new FileNotFoundException($"Blueprint not found: {path}");
}
var text = File.ReadAllText(path);
var parsed = JObject.Parse(text);
// Detect bare BlockSchematic (no version / anchorOffset) vs our wrapper.
if (parsed["schematic"] == null && parsed["anchorOffset"] == null)
{
FieldwrightLogger.Info(api, Component,
$"loaded bare schematic '{name}' (no wrapper, implicit anchor (0,0,0))");
return new BlueprintFile
{
Version = "compat-bare",
AnchorOffset = new Vec3iSerializable(),
Schematic = parsed
};
}
var wrapper = parsed.ToObject<BlueprintFile>();
if (wrapper == null)
{
throw new System.InvalidOperationException($"Failed to deserialize blueprint wrapper at {path}");
}
FieldwrightLogger.Info(api, Component,
$"loaded blueprint '{name}' (version={wrapper.Version}, anchor=({wrapper.AnchorOffset.X},{wrapper.AnchorOffset.Y},{wrapper.AnchorOffset.Z}))");
return wrapper;
}
/// <summary>
/// Swap a blueprint with its rolling backup. After the swap, `.bak.json` holds the
/// previously-active file and `.json` holds the previously-backed-up content.
/// Running restore twice in a row returns the original state, so this is a
/// reversible "undo last overwrite" rather than a one-shot rollback.
///
/// Edge case: if the main file is gone (user deleted it) but the backup remains,
/// promote backup → main without a swap.
/// </summary>
public static RestoreResult Restore(ICoreAPI api, string name)
{
var main = GetBlueprintPath(api, name);
var backup = GetBackupPath(api, name);
if (!File.Exists(backup))
{
FieldwrightLogger.Warn(api, Component, $"restore '{name}' failed: no backup at {backup}");
return RestoreResult.NoBackup;
}
if (!File.Exists(main))
{
File.Move(backup, main);
FieldwrightLogger.Info(api, Component, $"restored '{name}' from backup (main was missing)");
return RestoreResult.PromotedFromBackup;
}
// Both exist. Three-step swap via a temp file for atomicity on Windows.
var temp = main + ".swap";
if (File.Exists(temp)) File.Delete(temp);
File.Move(main, temp);
File.Move(backup, main);
File.Move(temp, backup);
FieldwrightLogger.Info(api, Component, $"swapped '{name}' with its backup");
return RestoreResult.Swapped;
}
public enum RestoreResult
{
NoBackup,
PromotedFromBackup,
Swapped,
}
public static List<string> List(ICoreAPI api)
{
var dir = GetBlueprintsDirectory(api);
var files = Directory.GetFiles(dir, "*.json");
var names = new List<string>();
foreach (var file in files)
{
var stem = Path.GetFileNameWithoutExtension(file);
// Skip rolling-backup files ({name}.bak.json → stem is "{name}.bak").
if (stem.EndsWith(".bak", System.StringComparison.OrdinalIgnoreCase)) continue;
names.Add(stem);
}
names.Sort();
return names;
}
/// <summary>
/// List blueprints with size + block-count metadata for the library UI. Each call
/// re-parses every blueprint file, which is O(n) in disk and CPU but matches user
/// expectations after editing files externally. Failed entries are skipped with a
/// warning rather than aborting the whole list.
/// </summary>
public static List<BlueprintEntry> ListWithMetadata(ICoreAPI api)
{
var dir = GetBlueprintsDirectory(api);
var files = Directory.GetFiles(dir, "*.json");
var entries = new List<BlueprintEntry>();
foreach (var file in files)
{
var stem = Path.GetFileNameWithoutExtension(file);
if (stem.EndsWith(".bak", System.StringComparison.OrdinalIgnoreCase)) continue;
try
{
var blueprint = Load(api, stem);
var sch = blueprint.ToBlockSchematic();
var face = blueprint.AnchorFacingResolved();
entries.Add(new BlueprintEntry
{
Name = stem,
SizeX = sch.SizeX,
SizeY = sch.SizeY,
SizeZ = sch.SizeZ,
BlockCount = sch.Indices?.Count ?? 0,
AnchorFaceLabel = face?.Code ?? "-",
ModifiedAt = File.GetLastWriteTime(file),
HasBackup = File.Exists(GetBackupPath(api, stem)),
});
}
catch (System.Exception ex)
{
FieldwrightLogger.Warn(api, Component, $"failed to read metadata for '{stem}': {ex.Message}");
}
}
entries.Sort((a, b) => b.ModifiedAt.CompareTo(a.ModifiedAt));
return entries;
}
/// <summary>
/// Delete a blueprint and its rolling backup. Throws FileNotFoundException if neither
/// the main file nor a backup exists. Returns silently if only one of the two exists
/// (deletes whatever is there).
/// </summary>
public static void Delete(ICoreAPI api, string name)
{
var main = GetBlueprintPath(api, name);
var backup = GetBackupPath(api, name);
bool mainExisted = File.Exists(main);
bool backupExisted = File.Exists(backup);
if (!mainExisted && !backupExisted)
{
throw new FileNotFoundException($"Blueprint not found: {name}");
}
if (mainExisted) File.Delete(main);
if (backupExisted) File.Delete(backup);
FieldwrightLogger.Info(api, Component,
$"deleted '{name}' (main={mainExisted}, backup={backupExisted})");
}
private static string SanitizeName(string name)
{
var invalid = Path.GetInvalidFileNameChars();
var chars = name.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
foreach (var bad in invalid)
{
if (chars[i] == bad) chars[i] = '_';
}
}
var result = new string(chars).Trim();
if (string.IsNullOrEmpty(result)) result = "unnamed";
return result;
}
}