10internal static class TilesheetFactory
12 #region factory methods
14 internal static Tilesheet FromBitmap(
string name, SKBitmap bitmap) =>
new(name, bitmap);
16 internal static Tilesheet FromStream(
string name, Stream stream) =>
new(name, stream);
18 internal static Tilesheet FromImageFile(
string name,
string imageFilePath) =>
new(name, imageFilePath);
20 internal static Tilesheet FromAssetsFile(AssetsFile assetsFile,
string entryName) =>
new(assetsFile, entryName);
26 internal static Tilesheet FromDefinitionFile(
string gtsPath,
string? name =
null)
28 if (
string.IsNullOrWhiteSpace(gtsPath))
29 throw new ArgumentException(
"GTS path must be a non-empty string.", nameof(gtsPath));
31 var fullPath = Path.GetFullPath(gtsPath);
33 if (!File.Exists(fullPath))
34 throw new FileNotFoundException($
"GTS file not found: {fullPath}", fullPath);
36 var definition = TilesheetDefinitionSerializer.Load(fullPath, name);
37 return FromDefinition(definition, Path.GetDirectoryName(fullPath));
45 internal static Tilesheet FromDefinition(
47 string? baseDirectory =
null,
48 AssetsFile? defaultAssetsFile =
null)
50 ArgumentNullException.ThrowIfNull(definition);
53 baseDirectory ??= definition.LoadStamp?.ReferenceBaseDirectory ??
54 (
string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
56 : Path.GetDirectoryName(origin));
60 if (defaultAssetsFile is
null && !
string.IsNullOrWhiteSpace(archivePath) &&
64 defaultAssetsFile = LoadExistingAssetsFile(ResolvePath(archivePath, baseDirectory));
66 var tilesheet = CreateTilesheet(
68 addDefaultRegion:
false,
74 foreach (var region
in definition.
Regions)
76 var runtimeRegion = tilesheet.AddRegion(
83 region.CollisionAdjust,
84 region.CollisionType);
88 foreach (var frame
in region.Frames ?? [])
90 if (frame.CollisionAdjust is { } adjust)
92 runtimeRegion.SetFrameCollisionAdjust(
98 if (frame.CollisionType is { } collisionType)
100 runtimeRegion.SetFrameCollisionType(
108 if (definition.
Mask is not
null)
120 tilesheet.ApplyPremultiplyAlpha();
123 tilesheet.DefinitionProvenance = definition.LoadStamp?.Materialized(
125 () => TilesheetDefinitionSerializer.FromTilesheet(tilesheet));
141 internal static Tilesheet FromDefinitionAsset(AssetsFile assetsFile,
string gtsEntryName,
string? name =
null)
143 ArgumentNullException.ThrowIfNull(assetsFile);
145 if (
string.IsNullOrWhiteSpace(gtsEntryName))
147 throw new ArgumentException(
148 "GTS asset entry name must be a non-empty string.",
149 nameof(gtsEntryName));
152 var definition = TilesheetDefinitionSerializer.Load(assetsFile, gtsEntryName, name);
154 var baseDirectory =
string.IsNullOrWhiteSpace(assetsFile.
SourcePath)
156 : Path.GetDirectoryName(Path.GetFullPath(assetsFile.
SourcePath));
158 return FromDefinition(
161 defaultAssetsFile: assetsFile);
164 #endregion factory methods
166 #region private methods
168 private static Tilesheet CreateTilesheet(
170 bool addDefaultRegion,
171 string? baseDirectory =
null,
172 AssetsFile? defaultAssetsFile =
null)
174 var image = definition.Image
175 ??
throw new InvalidOperationException(
176 "TilesheetDefinition must specify an image source.");
178 ValidateImageDefinition(image, defaultAssetsFile);
180 if (!
string.IsNullOrWhiteSpace(image.FilePath))
182 return new Tilesheet(
184 ResolvePath(image.FilePath, baseDirectory),
188 AssetsFile assetsFile;
190 if (!
string.IsNullOrWhiteSpace(image.AssetsFilePath))
192 assetsFile = LoadExistingAssetsFile(
193 ResolvePath(image.AssetsFilePath, baseDirectory));
197 assetsFile = defaultAssetsFile!;
200 var tilesheet =
new Tilesheet(
202 image.AssetEntryName!,
207 tilesheet.Name = definition.
Name;
212 private static void ValidateImageDefinition(TilesheetImageDefinition image, AssetsFile? defaultAssetsFile)
214 var hasFilePath = !
string.IsNullOrWhiteSpace(image.
FilePath);
215 var hasAssetsFilePath = !
string.IsNullOrWhiteSpace(image.
AssetsFilePath);
216 var hasAssetEntryName = !
string.IsNullOrWhiteSpace(image.
AssetEntryName);
218 if (hasFilePath && (hasAssetsFilePath || hasAssetEntryName))
220 throw new InvalidOperationException(
221 "TilesheetDefinition image source is ambiguous. Image.FilePath cannot be combined with Image.AssetsFilePath or Image.AssetEntryName.");
224 if (hasAssetsFilePath && !hasAssetEntryName)
226 throw new InvalidOperationException(
227 "TilesheetDefinition image specifies an AssetsFilePath but does not specify an AssetEntryName.");
233 if (!hasAssetEntryName)
235 throw new InvalidOperationException(
236 "TilesheetDefinition must specify either Image.FilePath or Image.AssetEntryName.");
239 if (!hasAssetsFilePath && defaultAssetsFile is
null)
241 throw new InvalidOperationException(
242 "TilesheetDefinition image specifies an AssetEntryName but does not specify an AssetsFilePath, and no default AssetsFile was provided.");
246 private static AssetsFile LoadExistingAssetsFile(
string path)
248 if (
string.IsNullOrWhiteSpace(path))
249 throw new ArgumentException(
"Assets file path must be a non-empty string.", nameof(path));
251 var fullPath = Path.GetFullPath(path);
253 var loaded = AssetsFile.AllAssetsFiles.FirstOrDefault(file =>
254 !
string.IsNullOrWhiteSpace(file.SourcePath) &&
string.Equals(
255 Path.GetFullPath(file.SourcePath),
257 OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
259 if (loaded is not
null)
262 if (!File.Exists(fullPath))
263 throw new FileNotFoundException($
"Assets file not found: {fullPath}", fullPath);
265 return AssetsFile.LoadOrCreate(fullPath);
268 private static string ResolvePath(
string path,
string? baseDirectory)
270 if (Path.IsPathRooted(path) ||
string.IsNullOrWhiteSpace(baseDirectory))
273 return Path.GetFullPath(Path.Combine(baseDirectory, path));
276 #endregion private methods
string? SourcePath
Gets the archive's reconstructible origin, including an optional stream origin.
string? AssetsFilePath
Gets the .gaf path when loaded from a packed definition file.
List< TilesheetRegionDefinition > Regions
Gets or sets the list of region definitions that define subdivisions within the tilesheet.
TilesheetImageDefinition Image
Gets or sets the image definition that specifies the source image for the tilesheet.
TilesheetMaskDefinition? Mask
Gets or sets the mask definition for color transparency, or null if no mask is applied.
string Name
Gets or sets the name of the tilesheet.
bool PremultiplyAlpha
Gets or sets a value indicating whether alpha should be premultiplied when loading the tilesheet imag...
TilesheetDefinitionSource Source
Gets or sets the provenance metadata for where this definition came from.
string? AssetsFilePath
Gets or sets the path to the assets file containing the tilesheet image.
string? AssetEntryName
Gets or sets the name of the specific entry within an assets file that contains the tilesheet image.
string? FilePath
Gets or sets the direct file path to the tilesheet image.
byte Tolerance
Gets or sets the tolerance value for color matching, allowing colors within a range to be considered ...
byte Alpha
Gets or sets the alpha (opacity) component of the mask color (0-255).
byte Blue
Gets or sets the blue component of the mask color (0-255).
byte Red
Gets or sets the red component of the mask color (0-255).
byte Green
Gets or sets the green component of the mask color (0-255).
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....