From 7c8b1be587eb7087b6e7c20ec602d5e8124e3ccc Mon Sep 17 00:00:00 2001 From: Ulysses Wu Date: Mon, 16 Sep 2024 21:54:32 +0800 Subject: [PATCH] SprBlock #124 --- .github/workflows/build.yml | 2 +- .gitignore | 1 + FreeMote.Psb/Types/M2Types.cs | 82 ++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ebf2424..8c24461 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,7 +14,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Add NuGet source run: nuget sources add -Name MonarchSolutions -Source https://www.myget.org/F/monarchsolutions/api/v3/index.json diff --git a/.gitignore b/.gitignore index 0c85cd0..194e75a 100644 --- a/.gitignore +++ b/.gitignore @@ -257,3 +257,4 @@ paket-files/ /MigrationBackup /bak /.vscode +launchSettings.json diff --git a/FreeMote.Psb/Types/M2Types.cs b/FreeMote.Psb/Types/M2Types.cs index 10e55db..85e9dec 100644 --- a/FreeMote.Psb/Types/M2Types.cs +++ b/FreeMote.Psb/Types/M2Types.cs @@ -1,5 +1,6 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; +using System.Linq; using FreeMote.Plugins; namespace FreeMote.Psb.Types @@ -12,46 +13,54 @@ class SprBlockType : BaseImageType, IPsbType public bool IsThisType(PSB psb) { - return psb.Objects is {Count: 3} && psb.Objects.ContainsKey("w") && psb.Objects.ContainsKey("h") && - psb.Objects.ContainsKey("image"); + return psb.Objects.All(kv => kv.Value is PsbDictionary {Count: 3} dic && dic.ContainsKey("w") && + dic.ContainsKey("h") && + dic.ContainsKey("image")); } public List CollectResources(PSB psb, bool deDuplication = true) where T : class, IResourceMetadata { - if (psb.Objects["image"] is PsbResource res) + var results = new List(); + foreach (var kv in psb.Objects) { - //test bit depth, for image it's 8bit (1 byte 1 pixel); for palette it's 32bit (4 byte 1 pixel) - var width = psb.Objects["w"].GetInt(); - var height = psb.Objects["h"].GetInt(); - var dataLen = res.Data.Length; - var depth = dataLen / (width * height); - PsbPixelFormat format = PsbPixelFormat.A8; - switch (depth) + var dic = kv.Value as PsbDictionary; + if (dic?["image"] is PsbResource res) { - case 1: - format = PsbPixelFormat.A8; - break; - case 4: - format = PsbPixelFormat.LeRGBA8; - break; - default: - Logger.LogWarn($"Unknown color format: {depth} bytes per pixel. Please submit sample."); - return []; + var name = kv.Key; + //test bit depth, for image it's 8bit (1 byte 1 pixel); for palette it's 32bit (4 byte 1 pixel) + var width = dic["w"].GetInt(); + var height = dic["h"].GetInt(); + var dataLen = res.Data.Length; + var depth = dataLen / (width * height); + PsbPixelFormat format = PsbPixelFormat.A8; + switch (depth) + { + case 1: + format = PsbPixelFormat.A8; + break; + case 4: + format = PsbPixelFormat.LeRGBA8; + break; + default: + Logger.LogWarn($"Unknown color format: {depth} bytes per pixel. Please submit sample."); + return []; + } + + ImageMetadata md = new ImageMetadata() + { + Name = name, + PsbType = PsbType, + Resource = res, + Width = width, + Height = height, + Spec = PsbSpec.none, + TypeString = format.ToStringForPsb().ToPsbString() + }; + results.Add(md); } - - ImageMetadata md = new ImageMetadata() - { - PsbType = PsbType, - Resource = res, - Width = width, - Height = height, - Spec = PsbSpec.none, - TypeString = format.ToStringForPsb().ToPsbString() - }; - return [md as T]; } - - return []; + + return results.Cast().ToList(); } } @@ -60,6 +69,7 @@ public List CollectResources(PSB psb, bool deDuplication = true) where T : class SprDataType : BaseImageType, IPsbType { public PsbType PsbType => PsbType.SprData; + public bool IsThisType(PSB psb) { return psb.Objects != null && psb.Objects.ContainsKey("spr_data") && psb.Objects.ContainsKey("tex_size"); @@ -137,6 +147,7 @@ public List CollectResources(PSB psb, bool deDuplication = true) where T : Logger.LogWarn($"Not supported: clut [{i}] is a > 256 colors palette ({cw}x{ch}). Skip."); continue; } + resList.Add(new ImageMetadata() { Name = i.ToString(), @@ -156,9 +167,10 @@ public List CollectResources(PSB psb, bool deDuplication = true) where T : class ChipType : BaseImageType, IPsbType { public PsbType PsbType => PsbType.ChipImg; + public bool IsThisType(PSB psb) { - return psb.Objects is { Count: 1 } && psb.Objects.ContainsKey("chip") && psb.Objects["chip"] is PsbList; + return psb.Objects is {Count: 1} && psb.Objects.ContainsKey("chip") && psb.Objects["chip"] is PsbList; } public List CollectResources(PSB psb, bool deDuplication = true) where T : class, IResourceMetadata @@ -167,4 +179,4 @@ public List CollectResources(PSB psb, bool deDuplication = true) where T : return []; } } -} +} \ No newline at end of file