ZenKit: mod world with 10/266/522 materials fails to load (std::bad_alloc) - Mroczne Tajemnice ANCIENTTEMPLE.ZEN
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 45/100
Piste de recherche
Start with the proposed ReadArchiveBinary::read_header fix in ZenKit and its binary header unit test; the affected file is src/archive/ArchiveBinary.cc. Validate the fix against a world with 266 materials and the listed LF, CRLF, and NUL header cases. Done means the material count is read correctly and the world loads; testing with the real ANCIENTTEMPLE.ZEN is still pending, and the ZenKit issue and pull request have not been submitted.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Problem
Mroczne Tajemnice (Gothic 1 mod) world ANCIENTTEMPLE.ZEN doesn't load: ZenKit throws std::bad_alloc while reading the world mesh. The original engine loads it fine.
Cause (ZenKit)
A world mesh's materials are a small binary archive inside the MeshAndBsp chunk (0xB020). Its header ends with objects N…\nEND\n\n, then the binary data starts with the material count.
ReadArchiveBinary::read_header (src/archive/ArchiveBinary.cc:25) reads the second END with read_line_then_ignore("\n"), which skips every \n after it - also the first data byte when it is 0x0A. That's the low byte of the material count for 10, 266, 522, … materials. ANCIENTTEMPLE.ZEN has exactly 266 materials, so everything after is read one byte off → std::bad_alloc.
Workaround we had (removed again, branch split/23-world)
ZenWorldArchiveFixDomain.TryFixMaterialCountHeader: when a world failed to load, ResourceCacheService.LoadWorld copied the world file, replaced the materials archive's END\n\n with END\0 (ends the line without skipping) and retried once.
Workaround code
/// <summary>
/// Workarounds for two ZenKit bugs with mod worlds, always on (ResourceCacheService.LoadWorld).
/// FOLLOW-UP: raise both upstream (issue + PR at GothicKit/ZenKit), remove this class once a ZenKit release has
/// the fixes.
/// 1. Material count (TryFixMaterialCountHeader): a world mesh's materials are a small BINARY archive inside the
/// MeshAndBsp chunk. ZenKit reads its header's second "END" with read_line_then_ignore("\n"), which skips
/// every '\n' after it - also the first byte of the material count when it's 0x0A (10, 266, 522, ...
/// materials). The count and everything after is read one byte off and the loader dies with
/// std::bad_alloc (Mroczne Tajemnice's ANCIENTTEMPLE.ZEN has exactly 266). "END\0" ends the line without
/// skipping anything.
/// 2. Chunk length (TryFixMeshChunkLengths): see there.
/// </summary>
// ...
public static byte[] TryFixMaterialCountHeader(byte[] data, out string reason)
{
var tag = IndexOf(data, _meshAndBspTag, 0, Math.Min(data.Length, 4096));
if (tag < 0)
{
reason = "no ASCII [MeshAndBsp] entry";
return null;
}
// u32 version, u32 size, then the mesh's chunks: u16 id, u32 length.
var blob = Array.IndexOf(data, (byte)'\n', tag) + 1;
var blobSize = BitConverter.ToInt32(data, blob + 4);
var blobEnd = Math.Min(data.Length, blob + 8 + blobSize);
var chunk = blob + 8;
while (chunk + 6 <= blobEnd && BitConverter.ToUInt16(data, chunk) != _meshMaterialsChunk)
chunk += 6 + BitConverter.ToInt32(data, chunk + 2);
if (chunk + 6 > blobEnd)
{
reason = "no materials chunk";
return null;
}
var chunkLength = BitConverter.ToInt32(data, chunk + 2);
var body = chunk + 6;
var bodyEnd = Math.Min(blobEnd, body + chunkLength);
var objects = IndexOf(data, _objectsTag, body, Math.Min(bodyEnd, body + 256));
var end = objects < 0 ? -1 : IndexOf(data, _endTag, objects, Math.Min(bodyEnd, objects + 64));
if (end < 0 || end + _endTag.Length + 4 > bodyEnd)
{
reason = "no binary materials archive header";
return null;
}
var count = BitConverter.ToInt32(data, end + _endTag.Length);
if (data[end + _endTag.Length] != (byte)'\n')
{
reason = $"material count {count} isn't affected";
return null;
}
// "END\n\n" -> "END\0": one byte shorter, the chunk and the MeshAndBsp blob shrink with it.
var fixedData = new byte[data.Length - 1];
Buffer.BlockCopy(data, 0, fixedData, 0, end + 3);
fixedData[end + 3] = 0;
Buffer.BlockCopy(data, end + _endTag.Length, fixedData, end + 4, data.Length - end - _endTag.Length);
BitConverter.GetBytes(chunkLength - 1).CopyTo(fixedData, chunk + 2);
BitConverter.GetBytes(blobSize - 1).CopyTo(fixedData, blob + 4);
reason = $"material count {count} starts with a newline byte";
return fixedData;
}
Proposed upstream fix (prepared locally in ZenKit, not submitted yet)
ReadArchiveBinary::read_header consumes exactly END, one line break (\n or \r\n) and at most one empty line; END\0 is accepted too. ArchiveAscii / ArchiveBinsafe are not affected.
- Unit test (binary header followed by data starting with
0x0A, LF/CRLF/NUL) fails on current ZenKit, passes with the fix; an in-memory world with 266 materials reproduces thebad_allocand loads with the fix. - All 23 vanilla G1/G2/Addon worlds load with identical counts before and after.
Suggested commit: fix(ReadArchiveBinary): only consume the header's line breaks after the second END
Before we go to the ZenKit author
- Test the patched ZenKit with the real
ANCIENTTEMPLE.ZEN(Mroczne Tajemnice) - not done yet, the file wasn't available - Rebuild
libzenkitcapi(Windows, Quest, Pico) with the fix and load the world in Gothic-Unity - Open the ZenKit issue + PR with the results (GothicKit/ZenKit)
- Langage dominant
- C#
- Étoiles
- 33
- Forks
- 1
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 76
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de Gothic-Unity-Project/Gothic-Unity
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Gothic-Unity-Project/Gothic-Unity#533 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 53/100
Gothic-Unity-Project/Gothic-Unity#523 ·
Les mainteneurs répondent en général sous 1 jour
-
improvement
Difficulté 4/5 3-5 jours Accessibilité débutants 52/100
Gothic-Unity-Project/Gothic-Unity#521 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
Gothic-Unity-Project/Gothic-Unity#505 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
Gothic-Unity-Project/Gothic-Unity#501 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de Gothic-Unity-Project/Gothic-Unity
Issues similaires
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P2
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
openclaw/openclaw-windows-node#2287 · 2 commentaires · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
area:host area:operations type:bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
kind:ux needs-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
elsa-workflows/elsa-foundation#4742 ·
Les mainteneurs répondent en général sous 1 jour
-
priority:P2
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
-
[LANG] Add support for C# (.cs)Ouverteenhancement good first issue help wanted new-language
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100