Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate length of texture data during serialization instead of writing a set value & Update ImportGMS2FontData #1048

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b95fc17
Calc length of texture data during serialization
Dobby233Liu Jul 13, 2022
e5da962
Try 2 & Update ImportGMS2FontData
Dobby233Liu Jul 13, 2022
8c4a9f7
Write into blocksize variable
Dobby233Liu Jul 13, 2022
076f12b
Merge branch 'krzys-h:master' into Dobby233Liu-untextureblocksize-1
Dobby233Liu Aug 27, 2022
5c047b5
Merge branch 'krzys-h:master' into Dobby233Liu-untextureblocksize-1
Dobby233Liu Aug 27, 2022
7df75d2
Add GM2022_3 checks
Dobby233Liu Aug 31, 2022
cbaa46f
Fix copypasta
Dobby233Liu Aug 31, 2022
c336330
Slightly redo docs
Dobby233Liu Aug 31, 2022
a7e031e
Merge branch 'krzys-h:master' into Dobby233Liu-untextureblocksize-1
Dobby233Liu Oct 20, 2022
dbdb29f
Apply suggestions from code review
Dobby233Liu Oct 21, 2022
e758cd6
Apply suggestions #2
Dobby233Liu Oct 21, 2022
3f96382
Update ImportGMS2FontData (UNTESTED)
Dobby233Liu Oct 21, 2022
e3fb19b
Merge branch 'master' into Dobby233Liu-untextureblocksize-1
Dobby233Liu Dec 4, 2022
bdd7ce7
Update UndertaleEmbeddedTexture.cs
Dobby233Liu Dec 4, 2022
ca98caf
Merge branch 'master' of https://github.com/krzys-h/UndertaleModTool …
Dobby233Liu Jan 28, 2023
afc3055
Fix ImportGMS2FontData not functioning
Dobby233Liu Jan 28, 2023
2f09f9b
figured i should do this
Dobby233Liu Jan 28, 2023
efbbba7
ditto
Dobby233Liu Jan 28, 2023
cc5357b
the g
Dobby233Liu Jan 28, 2023
7bc8d0d
real
Dobby233Liu Jan 28, 2023
7acac5d
Apply suggestions from code review
Dobby233Liu Jan 29, 2023
40e352c
Add FIXME note for external textures
Dobby233Liu Jan 30, 2023
0208a94
Merge branch 'Dobby233Liu-untextureblocksize-1' of https://github.com…
Dobby233Liu Jan 30, 2023
81f78eb
Write what we have as the texture block size first
Dobby233Liu Jan 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,34 @@ public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable
public uint GeneratedMips { get; set; }

/// <summary>
/// TODO: something. <br/>
/// The size of the texture data that the embedded image contains. <br/>
/// GameMaker: Studio 2 only.
/// </summary>
public uint TextureBlockSize { get; set; }
private uint _TextureBlockSize { get; set; }
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The texture data in the embedded image.
/// </summary>
public TexData TextureData { get; set; } = new TexData();

/// <summary>
/// The position of the TextureBlockSize value to be overwritten <br/>
/// in Serialize. <br/>
/// Only used internally.
/// </summary>
internal uint _TextureBlockSizeLocation { get; set; }

/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.Write(Scaled);
if (writer.undertaleData.GeneralInfo.Major >= 2)
writer.Write(GeneratedMips);
if (writer.undertaleData.GM2022_3)
writer.Write(TextureBlockSize);
// Write a placeholder for the texture blob size,
// so we can overwrite this with the actual value
// later
_TextureBlockSizeLocation = writer.Position;
writer.Write((uint)0);
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
writer.WriteUndertaleObjectPointer(TextureData);
}

Expand All @@ -63,7 +73,7 @@ public void Unserialize(UndertaleReader reader)
if (reader.undertaleData.GeneralInfo.Major >= 2)
GeneratedMips = reader.ReadUInt32();
if (reader.undertaleData.GM2022_3)
TextureBlockSize = reader.ReadUInt32();
_TextureBlockSize = reader.ReadUInt32();
TextureData = reader.ReadUndertaleObjectPointer<TexData>();
}

Expand All @@ -77,7 +87,18 @@ public void SerializeBlob(UndertaleWriter writer)
while (writer.Position % 0x80 != 0)
writer.Write((byte)0);

var objStartPos = writer.Position;
writer.WriteUndertaleObject(TextureData);
var objEndPos = writer.Position;

uint length = writer.Position - objStartPos;
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
_TextureBlockSize = length;
// Move to the placeholder zero value wrote
// in Serialize
writer.Position = _TextureBlockSizeLocation;
// Write texture data size
writer.Write(length);
writer.Position = objEndPos;
}

/// <summary>
Expand Down
41 changes: 29 additions & 12 deletions UndertaleModTool/Scripts/Community Scripts/ImportGMS2FontData.csx
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,38 @@ string textureSourcePath = Path.Combine(Path.GetDirectoryName(importFile), fontN
if (!File.Exists(textureSourcePath))
throw new ScriptException("The font texture file " + textureSourcePath + " doesn't exist.");

UndertaleTextureGroupInfo defaultTexGroup = null;
bool disappearMigitation = Data.GM2022_3;
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
if (disappearMigitation)
{
defaultTexGroup = Data.TextureGroupInfo.ByName("Default");
if (defaultTexGroup == null)
throw new ScriptException("Default texture group doesn't exist.");
}

UndertaleFont font = Data.Fonts.ByName(fontName);
if (font == null)
{
font = new UndertaleFont() {
font = new UndertaleFont()
{
Name = Data.Strings.MakeString(fontName)
};
Data.Fonts.Add(font);
if (disappearMigitation)
defaultTexGroup.Fonts.Add(new UndertaleResourceById<UndertaleFont, UndertaleChunkFONT>() { Resource = font });
}

Bitmap textureBitmap = new Bitmap(textureSourcePath);
textureBitmap.SetResolution(96.0F, 96.0F); // dpi fix
UndertaleEmbeddedTexture texture = new UndertaleEmbeddedTexture();
texture.Name = Data.Strings.MakeString("Texture " + Data.EmbeddedTextures.Count); // ???
texture.Name = new UndertaleString("Texture " + Data.EmbeddedTextures.Count); // ???
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
texture.TextureData.TextureBlob = File.ReadAllBytes(textureSourcePath);
Data.EmbeddedTextures.Add(texture);
if (disappearMigitation)
defaultTexGroup.TexturePages.Add(new UndertaleResourceById<UndertaleEmbeddedTexture, UndertaleChunkTXTR>() { Resource = texture });

UndertaleTexturePageItem texturePageItem = new UndertaleTexturePageItem();
texturePageItem.Name = Data.Strings.MakeString("PageItem " + Data.TexturePageItems.Count); // ???
texturePageItem.Name = new UndertaleString("PageItem " + Data.TexturePageItems.Count); // ???
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
texturePageItem.TexturePage = texture;
texturePageItem.SourceX = 0;
texturePageItem.SourceY = 0;
Expand All @@ -67,7 +82,6 @@ texturePageItem.TargetHeight = (ushort)textureBitmap.Height;
texturePageItem.BoundingWidth = (ushort)textureBitmap.Width;
texturePageItem.BoundingHeight = (ushort)textureBitmap.Height;
Data.TexturePageItems.Add(texturePageItem);

font.Texture = texturePageItem;

if (Data.GMS2_3)
Expand All @@ -89,17 +103,19 @@ if (fontData.ContainsKey("ascender"))
if (fontData.ContainsKey("ascenderOffset"))
font.AscenderOffset = (int)fontData["ascenderOffset"];

font.RangeStart = 0;
font.RangeEnd = 0;
ushort rangeStart = 0;
uint rangeEnd = 0;
foreach (JObject range in fontData["ranges"].Values<JObject>())
{
var rangeStart = (ushort)range["lower"];
var rangeEnd = (uint)range["upper"];
if (font.RangeStart > rangeStart)
font.RangeStart = rangeStart;
if (font.RangeEnd > rangeEnd)
font.RangeEnd = rangeEnd;
var rangeStartChk = (ushort)range["lower"];
Dobby233Liu marked this conversation as resolved.
Show resolved Hide resolved
var rangeEndChk = (uint)range["upper"];
if (rangeStart <= 0)
rangeStart = rangeStartChk;
if (rangeEnd < rangeEndChk)
rangeEnd = rangeEndChk;
}
font.RangeStart = rangeStart;
font.RangeEnd = rangeEnd;

foreach (KeyValuePair<string, JToken> glyphMeta in (JObject)fontData["glyphs"])
{
Expand Down Expand Up @@ -131,6 +147,7 @@ foreach (JObject kerningPair in fontData["kerningPairs"].Values<JObject>())
});
}

glyphs = font.Glyphs.ToList();
// Sort glyphs like in UndertaleFontEditor to be safe
glyphs.Sort((x, y) => x.Character.CompareTo(y.Character));
font.Glyphs.Clear();
Expand Down