From 2c0f3d9cd974aad9953a30add3e55d9c802661d9 Mon Sep 17 00:00:00 2001 From: gravestench Date: Mon, 11 Jan 2021 01:12:46 -0800 Subject: [PATCH] d2tbl.LoadTextDictionary now returns an error --- d2common/d2fileformats/d2tbl/text_dictionary.go | 12 ++++++++---- d2core/d2asset/asset_manager.go | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary.go b/d2common/d2fileformats/d2tbl/text_dictionary.go index 25d67790..e350dc46 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary.go @@ -1,7 +1,7 @@ package d2tbl import ( - "log" + "errors" "strconv" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils" @@ -24,7 +24,7 @@ const ( ) // LoadTextDictionary loads the text dictionary from the given data -func LoadTextDictionary(dictionaryData []byte) TextDictionary { +func LoadTextDictionary(dictionaryData []byte) (TextDictionary, error) { lookupTable := make(TextDictionary) br := d2datautils.CreateStreamReader(dictionaryData) @@ -37,7 +37,7 @@ func LoadTextDictionary(dictionaryData []byte) TextDictionary { // Version (always 0) if _, err := br.ReadByte(); err != nil { - log.Fatal("Error reading Version record") + return nil, errors.New("error reading Version record") } br.GetUInt32() // StringOffset @@ -62,6 +62,10 @@ func LoadTextDictionary(dictionaryData []byte) TextDictionary { } for idx, hashEntry := range hashEntries { + if br.EOF() { + return nil, errors.New("unexpected end of text dictionary file") + } + if !hashEntry.IsActive { continue } @@ -93,5 +97,5 @@ func LoadTextDictionary(dictionaryData []byte) TextDictionary { } } - return lookupTable + return lookupTable, nil } diff --git a/d2core/d2asset/asset_manager.go b/d2core/d2asset/asset_manager.go index fb5f7cc7..4fe91e16 100644 --- a/d2core/d2asset/asset_manager.go +++ b/d2core/d2asset/asset_manager.go @@ -274,9 +274,9 @@ func (am *AssetManager) LoadStringTable(tablePath string) (d2tbl.TextDictionary, return nil, err } - table := d2tbl.LoadTextDictionary(data) - if table == nil { - return nil, fmt.Errorf("table not found: %s", tablePath) + table, err := d2tbl.LoadTextDictionary(data) + if err != nil { + return table, err } am.Debugf(fmtLoadStringTable, tablePath)