From 4fca43d4f876a93a1b399f0f2d7d1f138de6bbb6 Mon Sep 17 00:00:00 2001
From: miku-666 <74728189+NessieHax@users.noreply.github.com>
Date: Sun, 24 Nov 2024 17:25:12 +0100
Subject: [PATCH 1/2] Move extensions from 'PckFileExtensions' to
'PckAssetExtensions'
---
PCK-Studio/Extensions/PckAssetExtensions.cs | 16 ++++++++++++++
PCK-Studio/Extensions/PckFileExtensions.cs | 24 ---------------------
PCK-Studio/PckStudio.csproj | 1 -
3 files changed, 16 insertions(+), 25 deletions(-)
delete mode 100644 PCK-Studio/Extensions/PckFileExtensions.cs
diff --git a/PCK-Studio/Extensions/PckAssetExtensions.cs b/PCK-Studio/Extensions/PckAssetExtensions.cs
index 841a5a86..08c3f96e 100644
--- a/PCK-Studio/Extensions/PckAssetExtensions.cs
+++ b/PCK-Studio/Extensions/PckAssetExtensions.cs
@@ -18,6 +18,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/PckStudio.csproj b/PCK-Studio/PckStudio.csproj
index d9c07712..31dd120b 100644
--- a/PCK-Studio/PckStudio.csproj
+++ b/PCK-Studio/PckStudio.csproj
@@ -197,7 +197,6 @@
-
Form
From 0ab9634b7a411fa391764c76d0f3853e19a5a994 Mon Sep 17 00:00:00 2001
From: miku-666 <74728189+NessieHax@users.noreply.github.com>
Date: Sat, 30 Nov 2024 10:18:02 +0100
Subject: [PATCH 2/2] ResourceLocation - Add 'Unknown' ResourceLocation
instance & improved 'ResourceLocation.GetFromPath'
---
PCK-Studio/Internal/ResourceLocation.cs | 44 +++++++------
PCK-Studio/MainForm.cs | 86 +++++++++++++------------
2 files changed, 68 insertions(+), 62 deletions(-)
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 315629a7..3c9d651a 100644
--- a/PCK-Studio/MainForm.cs
+++ b/PCK-Studio/MainForm.cs
@@ -457,50 +457,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;
}
}