Skip to content

Commit

Permalink
SprBlock #124
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Sep 16, 2024
1 parent 0fe636e commit 7c8b1be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,4 @@ paket-files/
/MigrationBackup
/bak
/.vscode
launchSettings.json
82 changes: 47 additions & 35 deletions FreeMote.Psb/Types/M2Types.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<T> CollectResources<T>(PSB psb, bool deDuplication = true) where T : class, IResourceMetadata
{
if (psb.Objects["image"] is PsbResource res)
var results = new List<IResourceMetadata>();
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<T>().ToList();
}
}

Expand All @@ -60,6 +69,7 @@ public List<T> CollectResources<T>(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");
Expand Down Expand Up @@ -137,6 +147,7 @@ public List<T> CollectResources<T>(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(),
Expand All @@ -156,9 +167,10 @@ public List<T> CollectResources<T>(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<T> CollectResources<T>(PSB psb, bool deDuplication = true) where T : class, IResourceMetadata
Expand All @@ -167,4 +179,4 @@ public List<T> CollectResources<T>(PSB psb, bool deDuplication = true) where T :
return [];
}
}
}
}

0 comments on commit 7c8b1be

Please sign in to comment.