diff --git a/PCK-Studio/Extensions/PckAssetExtensions.cs b/PCK-Studio/Extensions/PckAssetExtensions.cs index 7ba5895e..afeee3a9 100644 --- a/PCK-Studio/Extensions/PckAssetExtensions.cs +++ b/PCK-Studio/Extensions/PckAssetExtensions.cs @@ -21,6 +21,22 @@ namespace PckStudio.Extensions { private const string MipMap = "MipMapLevel"; + internal static PckAsset CreateNewAssetIf(this PckFile pck, bool condition, string filename, PckAssetType filetype, IDataFormatWriter writer) + { + if (condition) + { + return pck.CreateNewAsset(filename, filetype, writer); + } + return default; + } + + internal static PckAsset CreateNewAsset(this PckFile pck, string filename, PckAssetType filetype, IDataFormatWriter writer) + { + PckAsset asset = pck.CreateNewAsset(filename, filetype); + asset.SetData(writer); + return asset; + } + internal static Image GetTexture(this PckAsset asset) { if (asset.Type != PckAssetType.SkinFile && diff --git a/PCK-Studio/Extensions/PckFileExtensions.cs b/PCK-Studio/Extensions/PckFileExtensions.cs deleted file mode 100644 index 218f52a0..00000000 --- a/PCK-Studio/Extensions/PckFileExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -using OMI.Formats.Pck; -using OMI.Workers; - -namespace PckStudio.Extensions -{ - internal static class PckFileExtensions - { - internal static PckAsset CreateNewAssetIf(this PckFile pck, bool condition, string filename, PckAssetType filetype, IDataFormatWriter writer) - { - if (condition) - { - return pck.CreateNewAsset(filename, filetype, writer); - } - return null; - } - - internal static PckAsset CreateNewAsset(this PckFile pck, string filename, PckAssetType filetype, IDataFormatWriter writer) - { - PckAsset asset = pck.CreateNewAsset(filename, filetype); - asset.SetData(writer); - return asset; - } - } -} diff --git a/PCK-Studio/Internal/ResourceLocation.cs b/PCK-Studio/Internal/ResourceLocation.cs index e83992d8..915d37fb 100644 --- a/PCK-Studio/Internal/ResourceLocation.cs +++ b/PCK-Studio/Internal/ResourceLocation.cs @@ -18,11 +18,15 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; namespace PckStudio.Internal { internal sealed class ResourceLocation { + private static List ResourceGroups = new List(); + private static readonly ResourceLocation Unknown = new ResourceLocation(string.Empty, ResourceCategory.Unknown, 1); + private static readonly Dictionary _categoryLookUp = new Dictionary() { ["textures/items"] = new ResourceLocation("textures/items", ResourceCategory.ItemAnimation, 16, isGroup: true), @@ -43,37 +47,33 @@ namespace PckStudio.Internal { return category switch { - ResourceCategory.ItemAnimation => "res/textures/items", - ResourceCategory.BlockAnimation => "res/textures/blocks", + ResourceCategory.ItemAnimation => _categoryLookUp["textures/items"].ToString(), + ResourceCategory.BlockAnimation => _categoryLookUp["textures/blocks"].ToString(), ResourceCategory.BlockAtlas => "res/terrain.png", - ResourceCategory.ItemAtlas => "res/items.png", - ResourceCategory.ParticleAtlas => "res/particles.png", - ResourceCategory.BannerAtlas => "res/item/banner/Banner_Atlas.png", - ResourceCategory.PaintingAtlas => "res/art/kz.png", - ResourceCategory.ExplosionAtlas => "res/misc/explosion.png", - ResourceCategory.ExperienceOrbAtlas => "res/item/xporb.png", - ResourceCategory.MoonPhaseAtlas => "res/terrain/moon_phases.png", - ResourceCategory.MapIconAtlas => "res/misc/mapicons.png", - ResourceCategory.AdditionalMapIconsAtlas => "res/misc/additionalmapicons.png", + ResourceCategory.BlockAtlas => _categoryLookUp["terrain.png"].ToString(), + ResourceCategory.ItemAtlas => _categoryLookUp["items.png"].ToString(), + ResourceCategory.ParticleAtlas => _categoryLookUp["particles.png"].ToString(), + ResourceCategory.BannerAtlas => _categoryLookUp["item/banner/Banner_Atlas.png"].ToString(), + ResourceCategory.PaintingAtlas => _categoryLookUp["art/kz.png"].ToString(), + ResourceCategory.ExplosionAtlas => _categoryLookUp["misc/explosion.png"].ToString(), + ResourceCategory.ExperienceOrbAtlas => _categoryLookUp["item/xporb.png"].ToString(), + ResourceCategory.MoonPhaseAtlas => _categoryLookUp["terrain/moon_phases.png"].ToString(), + ResourceCategory.MapIconAtlas => _categoryLookUp["misc/mapicons.png"].ToString(), + ResourceCategory.AdditionalMapIconsAtlas => _categoryLookUp["misc/additionalmapicons.png"].ToString(), _ => string.Empty }; } - public static ResourceCategory GetCategoryFromPath(string path) - { - return GetFromPath(path)?.Category ?? ResourceCategory.Unknown; - } + public static ResourceCategory GetCategoryFromPath(string path) => GetFromPath(path).Category; public static ResourceLocation GetFromPath(string path) { if (string.IsNullOrWhiteSpace(path) || !path.StartsWith("res/")) - return null; + return Unknown; string categoryPath = path.Substring("res/".Length); - if (categoryPath.StartsWith("textures/items")) - categoryPath = "textures/items"; - if (categoryPath.StartsWith("textures/blocks")) - categoryPath = "textures/blocks"; - return _categoryLookUp.ContainsKey(categoryPath) ? _categoryLookUp[categoryPath] : null; + if (_categoryLookUp.ContainsKey(categoryPath)) + return _categoryLookUp[categoryPath]; + return ResourceGroups.Where(group => categoryPath.StartsWith(group.Path)).FirstOrDefault() ?? Unknown; } public enum TillingMode @@ -114,6 +114,8 @@ namespace PckStudio.Internal TillingFactor = tillingFactor; Tilling = tilling; IsGroup = isGroup; + if (isGroup) + ResourceGroups.Add(this); } public override string ToString() diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 1c55db3c..d9673da6 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -460,50 +460,54 @@ namespace PckStudio } ResourceLocation resourceLocation = ResourceLocation.GetFromPath(asset.Filename); - Debug.WriteLine("Handling Resource file: " + resourceLocation?.ToString()); - if (resourceLocation is null || resourceLocation.Category == ResourceCategory.Unknown) - return; + Debug.WriteLine("Handling Resource file: " + resourceLocation?.ToString()); - if (resourceLocation.Category != ResourceCategory.BlockAnimation && - resourceLocation.Category != ResourceCategory.ItemAnimation) + switch (resourceLocation.Category) { - Image img = asset.GetTexture(); - var viewer = new TextureAtlasEditor(currentPCK, resourceLocation, img); - if (viewer.ShowDialog(this) == DialogResult.OK) - { - Image texture = viewer.FinalTexture; - asset.SetTexture(texture); - wasModified = true; - BuildMainTreeView(); - } - return; - } + case ResourceCategory.Unknown: + Debug.WriteLine($"Unknown Resource Category."); + break; + case ResourceCategory.ItemAnimation: + case ResourceCategory.BlockAnimation: + Animation animation = asset.GetDeserializedData(AnimationDeserializer.DefaultDeserializer); + string internalName = Path.GetFileNameWithoutExtension(asset.Filename); + IList textureInfos = resourceLocation.Category == ResourceCategory.ItemAnimation ? Tiles.ItemTileInfos : Tiles.BlockTileInfos; + string displayname = textureInfos.FirstOrDefault(p => p.InternalName == internalName)?.DisplayName ?? internalName; - if (resourceLocation.Category != ResourceCategory.ItemAnimation && - resourceLocation.Category != ResourceCategory.BlockAnimation) - return; - - Animation animation = asset.GetDeserializedData(AnimationDeserializer.DefaultDeserializer); - string internalName = Path.GetFileNameWithoutExtension(asset.Filename); - - IList textureInfos = resourceLocation.Category switch - { - ResourceCategory.BlockAnimation => Tiles.BlockTileInfos, - ResourceCategory.ItemAnimation => Tiles.ItemTileInfos, - _ => Array.Empty().ToList() - }; - string displayname = textureInfos.FirstOrDefault(p => p.InternalName == internalName)?.DisplayName ?? internalName; - - string[] specialTileNames = { "clock", "compass" }; - - using (AnimationEditor animationEditor = new AnimationEditor(animation, displayname, internalName.ToLower().EqualsAny(specialTileNames))) - { - if (animationEditor.ShowDialog(this) == DialogResult.OK) - { - wasModified = true; - asset.SetSerializedData(animationEditor.Result, AnimationSerializer.DefaultSerializer); - BuildMainTreeView(); - } + string[] specialTileNames = { "clock", "compass" }; + using (AnimationEditor animationEditor = new AnimationEditor(animation, displayname, internalName.ToLower().EqualsAny(specialTileNames))) + { + if (animationEditor.ShowDialog(this) == DialogResult.OK) + { + wasModified = true; + asset.SetSerializedData(animationEditor.Result, AnimationSerializer.DefaultSerializer); + BuildMainTreeView(); + } + } + break; + case ResourceCategory.ItemAtlas: + case ResourceCategory.BlockAtlas: + case ResourceCategory.ParticleAtlas: + case ResourceCategory.BannerAtlas: + case ResourceCategory.PaintingAtlas: + case ResourceCategory.ExplosionAtlas: + case ResourceCategory.ExperienceOrbAtlas: + case ResourceCategory.MoonPhaseAtlas: + case ResourceCategory.MapIconAtlas: + case ResourceCategory.AdditionalMapIconsAtlas: + Image img = asset.GetTexture(); + var viewer = new TextureAtlasEditor(currentPCK, resourceLocation, img); + if (viewer.ShowDialog(this) == DialogResult.OK) + { + Image texture = viewer.FinalTexture; + asset.SetTexture(texture); + wasModified = true; + BuildMainTreeView(); + } + break; + default: + Debug.WriteLine($"Unhandled Resource Category: {resourceLocation.Category}"); + break; } } diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 651d8a2e..e2ac40ff 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -270,7 +270,6 @@ - Form