11public static partial class TilesheetDefinitionSerializer
13 private static readonly JsonSerializerSettings Settings =
new()
15 Formatting = Formatting.Indented,
16 NullValueHandling = NullValueHandling.Ignore,
17 DefaultValueHandling = DefaultValueHandling.Include,
18 MissingMemberHandling = MissingMemberHandling.Ignore
28 return DefinitionDocumentJson.Select(LoadAll(filePath), definition => definition.Name);
38 return DefinitionDocumentJson.Select(LoadAll(stream), definition => definition.Name);
48 ArgumentNullException.ThrowIfNull(definition);
49 SaveAll(filePath, [definition]);
58 DefinitionDocumentJson.Select(FromDocumentJson(json).Definitions, definition => definition.Name);
67 ArgumentNullException.ThrowIfNull(definition);
68 return JsonConvert.SerializeObject(definition, Settings);
80 string? baseDirectory =
null,
81 bool makePathsRelative =
false)
83 ArgumentNullException.ThrowIfNull(tilesheet);
87 Name = tilesheet.Name,
88 Image = CreateImageDefinition(
92 Regions = tilesheet.Regions
93 .Select(CreateRegionDefinition)
95 Mask = CreateMaskDefinition(tilesheet),
96 PremultiplyAlpha = tilesheet.Premultiplied && tilesheet.MaskColor is
null,
97 Source = TilesheetDefinitionSource.Generated()
114 public static void Save(
117 bool makePathsRelative =
true)
119 if (
string.IsNullOrWhiteSpace(filePath))
120 throw new ArgumentException(
"GTS file path must be a non-empty string.", nameof(filePath));
122 ArgumentNullException.ThrowIfNull(tilesheet);
124 var fullPath = Path.GetFullPath(filePath);
126 if (
string.IsNullOrWhiteSpace(tilesheet.ImageFilePath) &&
127 string.IsNullOrWhiteSpace(tilesheet.AssetIdentifier?.AssetsFile.SourcePath))
129 tilesheet.PersistImageToFile(
130 Path.ChangeExtension(fullPath,
".png"));
133 var definition = FromTilesheet(
135 Path.GetDirectoryName(fullPath),
138 Save(fullPath, definition);
148 public static string ToJson(
150 string? baseDirectory =
null,
151 bool makePathsRelative =
false)
162 string? sourceDescription)
164 if (
string.IsNullOrWhiteSpace(json))
165 throw new ArgumentException(
"GTS JSON content must be a non-empty string.", nameof(json));
171 if (definition is
null)
173 var source =
string.IsNullOrWhiteSpace(sourceDescription)
177 throw new InvalidDataException(
178 $
"Failed to deserialize {source}. Result was null.");
183 definition.Regions ??= [];
185 foreach (var region
in definition.Regions)
188 throw new InvalidDataException(
"GTS Regions cannot contain null entries.");
190 region.Frames ??= [];
195 catch (JsonException ex)
197 var source =
string.IsNullOrWhiteSpace(sourceDescription)
201 throw new InvalidDataException($
"Failed to deserialize {source}.", ex);
205 private static TilesheetImageDefinition CreateImageDefinition(
207 string? baseDirectory,
208 bool makePathsRelative)
210 if (!
string.IsNullOrWhiteSpace(tilesheet.ImageFilePath))
212 return new TilesheetImageDefinition
214 FilePath = NormalizePath(
215 tilesheet.ImageFilePath,
221 if (tilesheet.AssetIdentifier is not
null)
223 var assetIdentifier = tilesheet.AssetIdentifier;
225 if (assetIdentifier.AssetType !=
AssetTypes.Image)
227 throw new InvalidOperationException(
228 $
"Tilesheet '{tilesheet.Name}' references an asset of type '{assetIdentifier.AssetType}', but expected '{AssetTypes.Image}'.");
231 if (assetIdentifier.AssetsFile is
null ||
232 string.IsNullOrWhiteSpace(assetIdentifier.AssetsFile.SourcePath))
234 throw new InvalidOperationException(
235 $
"Tilesheet '{tilesheet.Name}' has an asset identifier, but the assets file path is missing.");
238 return new TilesheetImageDefinition
240 AssetsFilePath = NormalizePath(
241 assetIdentifier.AssetsFile.SourcePath,
244 AssetEntryName = assetIdentifier.AssetName
248 throw new InvalidOperationException(
249 $
"Tilesheet '{tilesheet.Name}' cannot be converted to a TilesheetDefinition because it has no ImageFilePath or AssetIdentifier.");
252 private static TilesheetRegionDefinition CreateRegionDefinition(
253 TilesheetRegion region)
255 var definition =
new TilesheetRegionDefinition
259 TileSize = region.TileSize,
260 TilePadding = region.TilePadding,
261 RegionMargin = region.RegionMargin,
262 Overhang = region.Overhang,
263 CollisionAdjust = region.CollisionAdjust,
264 CollisionType = region.CollisionType
269 for (
int y = 0; y < region.Rows; y++)
271 for (
int x = 0; x < region.Columns; x++)
273 CollisionAdjust? collisionAdjust =
274 region.TryGetFrameCollisionAdjustOverride(
277 out var frameCollisionAdjust)
278 ? frameCollisionAdjust
282 region.TryGetFrameCollisionTypeOverride(
285 out var frameCollisionType)
289 definition.Frames.Add(
290 new TilesheetFrameDefinition
294 CollisionAdjust = collisionAdjust,
295 CollisionType = collisionType
303 private static TilesheetMaskDefinition? CreateMaskDefinition(Tilesheet tilesheet)
305 if (tilesheet.MaskColor is
null)
308 var color = tilesheet.MaskColor.Value;
310 return new TilesheetMaskDefinition
316 Tolerance = tilesheet.MaskTolerance
320 private static void ApplyDefaultSource(
322 TilesheetDefinitionSource source)
325 definition.Source = source;
328 private static string NormalizePath(
330 string? baseDirectory,
333 if (
string.IsNullOrWhiteSpace(path))
336 if (!makeRelative ||
string.IsNullOrWhiteSpace(baseDirectory))
337 return path.Replace(
'\\',
'/');
339 if (!Path.IsPathRooted(path))
340 return path.Replace(
'\\',
'/');
342 var fullPath = Path.GetFullPath(path);
343 var fullBaseDirectory = Path.GetFullPath(baseDirectory);
345 return Path.GetRelativePath(fullBaseDirectory, fullPath).Replace(
'\\',
'/');
AssetTypes
Defines the types of assets that can be stored and managed by the engine.
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....
@ Image
Represents an image file type.
TilesheetDefinitionSourceKind
Indicates where a tilesheet definition came from.
TileCollisionType
Defines the default collision behavior associated with a tilesheet region, frame, or runtime tile.