Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<!--Development Variables-->
<PropertyGroup>
<Version>1.5.10</Version>
<Version>1.5.11</Version>
<HarmonyVersion>2.2.2</HarmonyVersion>
<BUTRSharedVersion>3.0.0.142</BUTRSharedVersion>
<BUTRModuleManagerVersion>6.0.247</BUTRModuleManagerVersion>
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 1.5.11
Game Versions: v1.0.x,v1.1.x,v1.2.x,v1.3.x
* Fixed crash related to Chinese, Japanese and Korean localizations\
* Updated Tpac asset reading based on new fork https://github.com/hunharibo/TpacTool
---------------------------------------------------------------------------------------------------
Version: 1.5.10
Game Versions: v1.0.x,v1.1.x,v1.2.x,v1.3.x
* Fixed an issue with Standalone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal static bool Enable(Harmony harmony)

private delegate void SetSpriteNamesDelegate(SpriteData instance, Dictionary<string, Sprite> value);
private static readonly SetSpriteNamesDelegate? SetSpriteNames =
AccessTools2.GetPropertySetterDelegate<SetSpriteNamesDelegate>(typeof(SpriteData), "SpriteNames");
AccessTools2.GetPropertySetterDelegate<SetSpriteNamesDelegate>(typeof(SpriteData), "SpriteNames") ??
AccessTools2.GetPropertySetterDelegate<SetSpriteNamesDelegate>(typeof(SpriteData), "Sprites");

private delegate void AddFontDefinitionDelegate(FontFactory instance, string fontPath, string fontName, SpriteData spriteData);
private static readonly AddFontDefinitionDelegate? AddFontDefinition =
Expand Down Expand Up @@ -77,7 +78,6 @@ private static void LoadAllFontsPostfix(ref FontFactory __instance)
addFont(__instance, Path.Combine(BasePath.Name, "GUI", "GauntletUI", "Fonts", "NanumGothicKR") + "/", "NanumGothicKR", new SpriteData("NanumGothicKR").WithData("NanumGothicKR"));
break;
}

}
private static bool GetMappedFontForLocalizationPrefix(ref FontFactory __instance, ref Font __result)
{
Expand Down
50 changes: 42 additions & 8 deletions src/Bannerlord.LauncherEx/ResourceManagers/SpriteDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,39 @@ private static void PopulateTextureCoordinates(float[] outUVs, int uvsStartIndex
}
}
#elif v134
private sealed class SpriteGenericFromTexture : SpriteGeneric
{
private delegate void SetIsLoadedDelegate(SpriteCategory instance, bool value);
private static readonly SetIsLoadedDelegate? SetIsLoaded =
AccessTools2.GetPropertySetterDelegate<SetIsLoadedDelegate>(typeof(SpriteCategory), "IsLoaded");

private static SpritePart GetSpritePart(string name, Texture texture)
{
var data = new SpriteData(name);
var category = new SpriteCategory(name, 1)
{
SpriteSheets =
{
texture,
},
SpriteSheetCount = 1,
};
SetIsLoaded?.Invoke(category, true);

return new SpritePart(name, category, texture.Width, texture.Height)
{
SheetID = 1,
};
}
public SpriteGenericFromTexture(string name, Texture texture) : base(name, GetSpritePart(name, texture), SpriteNinePatchParameters.Empty) { }
}
internal class SpriteFromTexture : Sprite
{
private readonly Texture _texture;
public override Texture Texture => _texture;

public SpriteFromTexture(Texture texture, int width, int height)
: base("Sprite", width, height, SpriteNinePatchParameters.Empty)
public SpriteFromTexture(string name, Texture texture, int width, int height)
: base(name, width, height, SpriteNinePatchParameters.Empty)
{
_texture = texture;
}
Expand All @@ -217,9 +243,13 @@ public SpriteFromTexture(Texture texture, int width, int height)
? new SpriteFromTexture(name, new Texture(gc.GetTexture(name)))
: null;
#elif v134
return GraphicsContextManager.Instance.TryGetTarget(out var gc) && gc is not null
? new Texture(gc.GetTexture(name)) is { } tex ? new SpriteFromTexture(tex, tex.Width, tex.Height) : null!
: null;
if (GraphicsContextManager.Instance.TryGetTarget(out var gc) && gc is not null)
if (new Texture(gc.GetTexture(name)) is { } tex)
return new SpriteFromTexture(name, tex, tex.Width, tex.Height);
else
return (SpriteFromTexture) null!;
else
return (SpriteFromTexture?) null;
#else
#error DEFINE
#endif
Expand All @@ -232,9 +262,13 @@ public SpriteFromTexture(Texture texture, int width, int height)
? new SpriteGenericFromTexture(name, new Texture(gc.GetTexture(name)))
: null;
#elif v134
return GraphicsContextManager.Instance.TryGetTarget(out var gc) && gc is not null
? new Texture(gc.GetTexture(name)) is { } tex ? new SpriteFromTexture(tex, tex.Width, tex.Height) : null!
: null;
if (GraphicsContextManager.Instance.TryGetTarget(out var gc) && gc is not null)
if (new Texture(gc.GetTexture(name)) is { } tex)
return new SpriteGenericFromTexture(name, new Texture(gc.GetTexture(name)));
else
return (SpriteFromTexture) null!;
else
return (SpriteFromTexture?) null;
#else
#error DEFINE
#endif
Expand Down
14 changes: 13 additions & 1 deletion src/Bannerlord.LauncherEx/TPac/AssetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ protected virtual void Load(BinaryReader stream)
assetItem.Version = assetVersion;
assetItem.Name = stream.ReadSizedString();

// Updated code taken from https://github.com/hunharibo/TpacTool
var metadataSize = stream.ReadUInt64();
assetItem.ReadMetadata(stream, (int) metadataSize);
var metadataStartPos = stream.BaseStream.Position;

assetItem.ReadMetadata(stream, (int)metadataSize);

var expectedPos = metadataStartPos + (long)metadataSize;
var actualPos = stream.BaseStream.Position;
if (actualPos != expectedPos)
{
// Force alignment if metadata reading didn't consume exact size
stream.BaseStream.Seek(expectedPos, SeekOrigin.Begin);
}

var unknownMetadataChecknum = stream.ReadInt64();

var dataSegmentNum = stream.ReadInt32();
Expand Down
42 changes: 36 additions & 6 deletions src/Bannerlord.LauncherEx/TPac/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ public Texture() : base(TYPE_GUID)
Source = string.Empty;
}

// Updated code taken from https://github.com/hunharibo/TpacTool
public override void ReadMetadata(BinaryReader stream, int totalSize)
{
var pos = stream.BaseStream.Position;
var startPos = stream.BaseStream.Position;

var version = stream.ReadUInt32();
stream.ReadGuid();
var BillboardMaterial = stream.ReadGuid();
var UnknownUint1 = stream.ReadUInt32();
Source = stream.ReadSizedString();
var UnknownUlong = stream.ReadUInt64();
Expand All @@ -80,11 +82,13 @@ public override void ReadMetadata(BinaryReader stream, int totalSize)
var UnknownUint6 = stream.ReadUInt32();
var UnknownUint7 = stream.ReadUInt32();
}

var currentPos = stream.BaseStream.Position;
var bytesRead = currentPos - startPos;
var remaining = totalSize - bytesRead;

// dirty hack for 1.5.0
// TW introduced a new field for the metadata of texture since 1.5.0
// but they didn't bump the version of metadata
if (version >= 1 || totalSize - (stream.BaseStream.Position - pos) == 4)
// Check if we should read generated assets
if (version >= 1 || remaining == 4)
{
var numPair = stream.ReadUInt32();
for (var i = 0; i < numPair; i++)
Expand All @@ -94,9 +98,35 @@ public override void ReadMetadata(BinaryReader stream, int totalSize)
}
}

// Handle version 2 and 3 fields
if (version >= 2)
{
var UnknownUlong2 = stream.ReadUInt64();
remaining -= 8;
}

if (version >= 3)
{
stream.ReadBytes(32);
remaining -= 32;
}

// Ensure we consume exactly totalSize bytes by reading any remaining data
currentPos = stream.BaseStream.Position;
bytesRead = currentPos - startPos;
remaining = totalSize - bytesRead;

if (remaining > 0)
{
stream.ReadBytes((int)remaining);
}
else if (remaining < 0)
{
#if DEBUG
// This indicates we read too much - this is a serious error
throw new InvalidDataException($"Texture metadata read {-remaining} bytes beyond expected size. " +
$"Asset: {Name}, Expected: {totalSize}, Position: {currentPos}, Start: {startPos}");
#endif
}
}

Expand Down
Loading