Skip to content

Commit

Permalink
PVR & ZSTREAM files now remember their format
Browse files Browse the repository at this point in the history
  • Loading branch information
giroletm committed Jul 3, 2023
1 parent 947d4ea commit 8975d0f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
43 changes: 24 additions & 19 deletions ABStudio/FileFormats/ZSTREAM/ZSTREAMFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Meta
{
public string app = "";
public string format = "";
public string image = "";
//public string image = "";
public int sizeW = -1, sizeH = -1, scale = -1;
public string version = "";
}
Expand Down Expand Up @@ -145,7 +145,8 @@ public ZSTREAMFile(byte[] zstreamData)
JToken meta = root.SelectToken("meta");
this.meta.app = meta.SelectToken("app").ToObject<string>();
this.meta.format = meta.SelectToken("format").ToObject<string>();
this.meta.image = meta.SelectToken("image").ToObject<string>();
//this.meta.image = meta.SelectToken("image").ToObject<string>();
string filename = meta.SelectToken("image").ToObject<string>();
this.meta.version = meta.SelectToken("version").ToObject<string>();

JToken metaSize = meta.SelectToken("size");
Expand All @@ -157,7 +158,7 @@ public ZSTREAMFile(byte[] zstreamData)
DATFile.SpriteData spriteData = dat.GetAsSpriteData();
spriteData.associatedZSTREAM = this;

spriteData.filenames.Add(this.meta.image);
spriteData.filenames.Add(filename);

JToken frames = root.SelectToken("frames");
foreach (JToken frame in frames.Children())
Expand Down Expand Up @@ -209,9 +210,25 @@ public ZSTREAMFile(byte[] zstreamData)
associatedSD = spriteData;
}

public void UpdateBitmap(Bitmap bmp, string filename)
public string GetFormatAsPVR()
{
string fmt = this.meta.format.ToLower();
int half = fmt.Length / 2;

char ch1 = (char)((0 < half) ? fmt[0] : 0);
char ch2 = (char)((1 < half) ? fmt[1] : 0);
char ch3 = (char)((2 < half) ? fmt[2] : 0);
char ch4 = (char)((3 < half) ? fmt[3] : 0);
char va1 = (char)((half < fmt.Length) ? fmt[half] : 0);
char va2 = (char)(((half + 1) < fmt.Length) ? fmt[half + 1] : 0);
char va3 = (char)(((half + 2) < fmt.Length) ? fmt[half + 2] : 0);
char va4 = (char)(((half + 3) < fmt.Length) ? fmt[half + 3] : 0);

return new string(new char[] { ch1, va1, ch2, va2, ch3, va3, ch4, va4 });
}

public void UpdateBitmap(Bitmap bmp)
{
this.meta.image = filename;
this.meta.sizeW = bmp.Width;
this.meta.sizeH = bmp.Height;
}
Expand Down Expand Up @@ -373,19 +390,7 @@ public void SaveBitmap(Bitmap bmp, string path)
int pos = 0;
byte[] outBytes = new byte[placementInfos.Last().position + placementInfos.Last().length + 0x28];

string fmt = this.meta.format.ToLower();
int half = fmt.Length / 2;

char ch1 = (char)((0 < half) ? fmt[0] : 0);
char ch2 = (char)((1 < half) ? fmt[1] : 0);
char ch3 = (char)((2 < half) ? fmt[2] : 0);
char ch4 = (char)((3 < half) ? fmt[3] : 0);
char va1 = (char)((half < fmt.Length) ? fmt[half] : 0);
char va2 = (char)(((half + 1) < fmt.Length) ? fmt[half + 1] : 0);
char va3 = (char)(((half + 2) < fmt.Length) ? fmt[half + 2] : 0);
char va4 = (char)(((half + 3) < fmt.Length) ? fmt[half + 3] : 0);

fmt = new string(new char[] { ch1, va1, ch2, va2, ch3, va3, ch4, va4 });
string fmt = GetFormatAsPVR();

foreach (PlacementInfo pInfo in placementInfos)
{
Expand Down Expand Up @@ -623,7 +628,7 @@ public byte[] Save()
JObject jmeta = new JObject();
jmeta.Add("app", this.meta.app);
jmeta.Add("format", this.meta.format);
jmeta.Add("image", this.meta.image);
jmeta.Add("image", associatedSD.filenames[0]);

JObject jsize = new JObject();
jsize.Add("h", this.meta.sizeH);
Expand Down
56 changes: 54 additions & 2 deletions ABStudio/Forms/SpritesheetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public partial class SpritesheetEditor : Form
private DATFile.SpriteData.Sprite SelectedSprite => spritesheetPictureBox.GetSRectLinkedObject(SelectedObj) as DATFile.SpriteData.Sprite;

private bool legacyPVR = false;
private string pvrFormat = "";

#region Extensions management

Expand Down Expand Up @@ -181,7 +182,52 @@ private bool SaveSpritesheet()

if (ext == ".pvr")
{
PVRFile pvr = new PVRFile(spritesheet);
if (this.pvrFormat == "")
{
bool chosen = false;

while (!chosen)
{
string[] formats = new string[] { "RGBA4444", "RGBA8888", "RGB565" };
using (MCQAskForm mcqAskForm = new MCQAskForm(formats, "PVR image format"))
{
if (mcqAskForm.ShowDialog() == DialogResult.OK)
{
string fmt = formats[mcqAskForm.ChosenAnswer].ToLower();
int half = fmt.Length / 2;

char ch1 = (char)((0 < half) ? fmt[0] : 0);
char ch2 = (char)((1 < half) ? fmt[1] : 0);
char ch3 = (char)((2 < half) ? fmt[2] : 0);
char ch4 = (char)((3 < half) ? fmt[3] : 0);
char va1 = (char)((half < fmt.Length) ? fmt[half] : 0);
char va2 = (char)(((half + 1) < fmt.Length) ? fmt[half + 1] : 0);
char va3 = (char)(((half + 2) < fmt.Length) ? fmt[half + 2] : 0);
char va4 = (char)(((half + 3) < fmt.Length) ? fmt[half + 3] : 0);

this.pvrFormat = new string(new char[] { ch1, va1, ch2, va2, ch3, va3, ch4, va4 });

chosen = true;
}
}
}

chosen = false;

while (!chosen)
{
string[] answers = new string[] { "No", "Yes" };
using (MCQAskForm mcqAskForm = new MCQAskForm(answers, "Save PVR in Legacy form?"))
{
if (mcqAskForm.ShowDialog() == DialogResult.OK)
{
this.legacyPVR = (mcqAskForm.ChosenAnswer == 1) ? true : false;
}
}
}
}

PVRFile pvr = new PVRFile(spritesheet, this.pvrFormat);
pvr.isLegacy = this.legacyPVR;
pvr.Save(fname);
}
Expand Down Expand Up @@ -401,6 +447,7 @@ private void LoadBitmap(string path=null)

bool hasSpecifiedPath = path != null;
legacyPVR = false;
pvrFormat = "";

if (data.filenames.Count <= 0 && !hasSpecifiedPath)
{
Expand All @@ -414,12 +461,17 @@ private void LoadBitmap(string path=null)
{
PVRFile pvr = new PVRFile(fullPath);
legacyPVR = pvr.isLegacy;
pvrFormat = pvr.GetFormatStr();
spritesheet = pvr.AsBitmap();
}
else if(fullPath.EndsWith(".stream") || ext == ".zstream")
{
if (data.associatedZSTREAM != null)
{
spritesheet = data.associatedZSTREAM.GetBitmap(fullPath);
pvrFormat = data.associatedZSTREAM.GetFormatAsPVR();
legacyPVR = false;
}
else
throw new Exception("Non-JSON sheets can't have a stream or zstream texture");
}
Expand All @@ -431,7 +483,7 @@ private void LoadBitmap(string path=null)
}

if (data.associatedZSTREAM != null)
data.associatedZSTREAM.UpdateBitmap(spritesheet, Path.GetFileName(fullPath));
data.associatedZSTREAM.UpdateBitmap(spritesheet);
}
else
{
Expand Down

0 comments on commit 8975d0f

Please sign in to comment.