mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-05 08:07:51 -05:00
7f6ae1b785
* improve AssetManager implementation Notable changes are: * removed the individual managers inside of d2asset, only one asset manager * AssetManager now has caches for the types of files it loads * created a type for TextDictionary (the txt file structs) * fixed a file path bug in d2loader Source * fixed a asset stream bug in d2loader Asset * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files) * updated the mpq file in d2asset test data, added test case for "sub-directory" * added a Data method to d2asset.Asset. The data is cached on first full read. * renamed ArchiveDataStream to DataStream in d2interface * moved palette utility func out of d2asset and into d2util * bugfix for MacOS mpq loader issue * minor lint fixes * removed obsolete interfaces from d2interface * lint fixes, added data caching to filesystem asset * adding comment for mpq asset close * adding comment for mpq asset close
114 lines
2.4 KiB
Go
114 lines
2.4 KiB
Go
package d2tbl
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"
|
|
)
|
|
|
|
// TextDictionary is a string map
|
|
type TextDictionary map[string]string
|
|
|
|
type textDictionaryHashEntry struct {
|
|
IsActive bool
|
|
Index uint16
|
|
HashValue uint32
|
|
IndexString uint32
|
|
NameString uint32
|
|
NameLength uint16
|
|
}
|
|
|
|
var lookupTable TextDictionary //nolint:gochecknoglobals // currently global by design
|
|
|
|
const (
|
|
crcByteCount = 2
|
|
)
|
|
|
|
// TranslateString returns the translation of the given string
|
|
func TranslateString(key string) string {
|
|
result, ok := lookupTable[key]
|
|
if !ok {
|
|
// Fix to allow v.setDescLabels("#123") to be bypassed for a patch in issue #360. Reenable later.
|
|
// log.Panicf("Could not find a string for the key '%s'", key)
|
|
return key
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// LoadTextDictionary loads the text dictionary from the given data
|
|
func LoadTextDictionary(dictionaryData []byte) TextDictionary {
|
|
if lookupTable == nil {
|
|
lookupTable = make(TextDictionary)
|
|
}
|
|
|
|
br := d2datautils.CreateStreamReader(dictionaryData)
|
|
|
|
// skip past the CRC
|
|
br.ReadBytes(crcByteCount)
|
|
|
|
numberOfElements := br.GetUInt16()
|
|
hashTableSize := br.GetUInt32()
|
|
|
|
// Version (always 0)
|
|
if _, err := br.ReadByte(); err != nil {
|
|
log.Fatal("Error reading Version record")
|
|
}
|
|
|
|
br.GetUInt32() // StringOffset
|
|
br.GetUInt32() // When the number of times you have missed a match with a hash key equals this value, you give up because it is not there.
|
|
br.GetUInt32() // FileSize
|
|
|
|
elementIndex := make([]uint16, numberOfElements)
|
|
for i := 0; i < int(numberOfElements); i++ {
|
|
elementIndex[i] = br.GetUInt16()
|
|
}
|
|
|
|
hashEntries := make([]textDictionaryHashEntry, hashTableSize)
|
|
for i := 0; i < int(hashTableSize); i++ {
|
|
hashEntries[i] = textDictionaryHashEntry{
|
|
br.GetByte() == 1,
|
|
br.GetUInt16(),
|
|
br.GetUInt32(),
|
|
br.GetUInt32(),
|
|
br.GetUInt32(),
|
|
br.GetUInt16(),
|
|
}
|
|
}
|
|
|
|
for idx, hashEntry := range hashEntries {
|
|
if !hashEntry.IsActive {
|
|
continue
|
|
}
|
|
|
|
br.SetPosition(uint64(hashEntry.NameString))
|
|
nameVal := br.ReadBytes(int(hashEntry.NameLength - 1))
|
|
value := string(nameVal)
|
|
|
|
br.SetPosition(uint64(hashEntry.IndexString))
|
|
|
|
key := ""
|
|
|
|
for {
|
|
b := br.GetByte()
|
|
if b == 0 {
|
|
break
|
|
}
|
|
|
|
key += string(b)
|
|
}
|
|
|
|
if key == "x" || key == "X" {
|
|
key = "#" + strconv.Itoa(idx)
|
|
}
|
|
|
|
_, exists := lookupTable[key]
|
|
if !exists {
|
|
lookupTable[key] = value
|
|
}
|
|
}
|
|
|
|
return lookupTable
|
|
}
|