1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-16 04:25:23 +00:00

d2font: fixed bug, when was unable to create new file doing just &Font{}.Marshal()

This commit is contained in:
M. Sz 2021-03-28 16:20:04 +02:00
parent ecab467a0f
commit d72b3e3345
2 changed files with 24 additions and 27 deletions

View File

@ -6,9 +6,6 @@ func Create(frame, width, height int) *FontGlyph {
// nolint:gomnd // thes bytes are constant // nolint:gomnd // thes bytes are constant
// comes from https://d2mods.info/forum/viewtopic.php?t=42044 // comes from https://d2mods.info/forum/viewtopic.php?t=42044
result := &FontGlyph{ result := &FontGlyph{
unknown1: []byte{0},
unknown2: []byte{1, 0, 0},
unknown3: []byte{0, 0, 0, 0},
frame: frame, frame: frame,
width: width, width: width,
height: height, height: height,
@ -19,9 +16,6 @@ func Create(frame, width, height int) *FontGlyph {
// FontGlyph represents a single font glyph // FontGlyph represents a single font glyph
type FontGlyph struct { type FontGlyph struct {
unknown1 []byte
unknown2 []byte
unknown3 []byte
frame int frame int
width int width int
height int height int
@ -59,15 +53,15 @@ func (fg *FontGlyph) FrameIndex() int {
// Unknown1 returns unknowns bytes // Unknown1 returns unknowns bytes
func (fg *FontGlyph) Unknown1() []byte { func (fg *FontGlyph) Unknown1() []byte {
return fg.unknown1 return []byte{0}
} }
// Unknown2 returns unknowns bytes // Unknown2 returns unknowns bytes
func (fg *FontGlyph) Unknown2() []byte { func (fg *FontGlyph) Unknown2() []byte {
return fg.unknown2 return []byte{1, 0, 0}
} }
// Unknown3 returns unknowns bytes // Unknown3 returns unknowns bytes
func (fg *FontGlyph) Unknown3() []byte { func (fg *FontGlyph) Unknown3() []byte {
return fg.unknown3 return []byte{0, 0, 0, 0}
} }

View File

@ -27,7 +27,6 @@ const (
// Font represents a displayable font // Font represents a displayable font
type Font struct { type Font struct {
unknownHeaderBytes []byte
sheet d2interface.Animation sheet d2interface.Animation
table []byte table []byte
Glyphs map[rune]*d2fontglyph.FontGlyph Glyphs map[rune]*d2fontglyph.FontGlyph
@ -52,10 +51,7 @@ func Load(data []byte) (*Font, error) {
color: color.White, color: color.White,
} }
font.unknownHeaderBytes, err = sr.ReadBytes(unknownHeaderBytesCount) sr.SkipBytes(unknownHeaderBytesCount)
if err != nil {
return nil, err
}
err = font.initGlyphs(sr) err = font.initGlyphs(sr)
if err != nil { if err != nil {
@ -149,10 +145,11 @@ func (f *Font) RenderText(text string, target d2interface.Surface) error {
func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error { func (f *Font) initGlyphs(sr *d2datautils.StreamReader) error {
glyphs := make(map[rune]*d2fontglyph.FontGlyph) glyphs := make(map[rune]*d2fontglyph.FontGlyph)
for i := numHeaderBytes; i < len(f.table); i += bytesPerGlyph { // for i := numHeaderBytes; i < len(f.table); i += bytesPerGlyph {
for i := numHeaderBytes; true; i += bytesPerGlyph {
code, err := sr.ReadUInt16() code, err := sr.ReadUInt16()
if err != nil { if err != nil {
return err break
} }
// byte of 0 // byte of 0
@ -194,7 +191,13 @@ func (f *Font) Marshal() []byte {
sw := d2datautils.CreateStreamWriter() sw := d2datautils.CreateStreamWriter()
sw.PushBytes([]byte("Woo!\x01")...) sw.PushBytes([]byte("Woo!\x01")...)
sw.PushBytes(f.unknownHeaderBytes...)
// unknown header bytes - constant
sw.PushBytes([]byte{1, 0, 0, 0, 0, 1}...)
// Expected Height of character cell and Expected Width of character cell
// not used in decoder
sw.PushBytes([]byte{0, 0}...)
for c, i := range f.Glyphs { for c, i := range f.Glyphs {
sw.PushUint16(uint16(c)) sw.PushUint16(uint16(c))