2020-01-26 00:39:13 -05:00
|
|
|
package d2common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type textDictionaryHashEntry struct {
|
|
|
|
IsActive bool
|
|
|
|
Index uint16
|
|
|
|
HashValue uint32
|
|
|
|
IndexString uint32
|
|
|
|
NameString uint32
|
|
|
|
NameLength uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
var lookupTable map[string]string
|
|
|
|
|
2020-07-18 18:07:38 -04:00
|
|
|
const (
|
|
|
|
crcByteCount = 2
|
|
|
|
)
|
|
|
|
|
2020-07-08 09:16:56 -04:00
|
|
|
// TranslateString returns the translation of the given string
|
2020-01-26 00:39:13 -05:00
|
|
|
func TranslateString(key string) string {
|
|
|
|
result, ok := lookupTable[key]
|
|
|
|
if !ok {
|
2020-06-20 15:14:52 -04:00
|
|
|
// 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
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:56 -04:00
|
|
|
return result
|
2020-06-28 10:33:09 -04:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:56 -04:00
|
|
|
// LoadTextDictionary loads the text dictionary from the given data
|
2020-06-22 17:36:45 -04:00
|
|
|
func LoadTextDictionary(dictionaryData []byte) {
|
2020-01-31 23:18:11 -05:00
|
|
|
if lookupTable == nil {
|
|
|
|
lookupTable = make(map[string]string)
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
br := CreateStreamReader(dictionaryData)
|
2020-07-18 18:07:38 -04:00
|
|
|
|
|
|
|
// skip past the CRC
|
|
|
|
br.ReadBytes(crcByteCount)
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
numberOfElements := br.GetUInt16()
|
|
|
|
hashTableSize := br.GetUInt32()
|
2020-07-18 18:07:38 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// Version (always 0)
|
|
|
|
if _, err := br.ReadByte(); err != nil {
|
|
|
|
log.Fatal("Error reading Version record")
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
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
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
br.SetPosition(uint64(hashEntry.NameString))
|
2020-06-20 21:07:36 -04:00
|
|
|
nameVal := br.ReadBytes(int(hashEntry.NameLength - 1))
|
2020-01-26 00:39:13 -05:00
|
|
|
value := string(nameVal)
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
br.SetPosition(uint64(hashEntry.IndexString))
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
key := ""
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
for {
|
|
|
|
b := br.GetByte()
|
|
|
|
if b == 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
key += string(b)
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if key == "x" || key == "X" {
|
|
|
|
key = "#" + strconv.Itoa(idx)
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
_, exists := lookupTable[key]
|
|
|
|
if !exists {
|
|
|
|
lookupTable[key] = value
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
}
|