This repository has been archived by the owner on Jul 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexts.go
109 lines (92 loc) · 3.38 KB
/
Texts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package core
import (
"fmt"
"github.com/inkyblackness/res"
"github.com/inkyblackness/res/chunk"
"github.com/inkyblackness/res/text"
"github.com/inkyblackness/shocked-core/io"
model "github.com/inkyblackness/shocked-model"
)
type textInfo struct {
multiblock bool
limit uint16
}
var knownTexts = map[model.ResourceType]textInfo{
model.ResourceTypeTrapMessages: {false, model.MaxTrapMessages},
model.ResourceTypeWords: {false, model.MaxWords},
model.ResourceTypeLogCategories: {false, model.MaxLogCategories},
model.ResourceTypeVariousMessages: {false, model.MaxVariousMessages},
model.ResourceTypeScreenMessages: {false, model.MaxScreenMessages},
model.ResourceTypeInfoNodeMessages: {false, model.MaxInfoNodeMessages},
model.ResourceTypeAccessCardNames: {false, model.MaxAccessCardNames},
model.ResourceTypeDataletMessages: {false, model.MaxDataletMessages},
model.ResourceTypePaperTexts: {true, model.MaxPaperTexts},
model.ResourceTypePanelNames: {false, model.MaxPanelNames}}
// Texts is the adapter for general texts.
type Texts struct {
cybstrng [model.LanguageCount]*io.DynamicChunkStore
cp text.Codepage
}
// NewTexts returns a new Texts instance, if possible.
func NewTexts(library io.StoreLibrary) (texts *Texts, err error) {
var cybstrng [model.LanguageCount]*io.DynamicChunkStore
for i := 0; i < model.LanguageCount && err == nil; i++ {
cybstrng[i], err = library.ChunkStore(localized[i].cybstrng)
}
if err == nil {
texts = &Texts{
cybstrng: cybstrng,
cp: text.DefaultCodepage()}
}
return
}
// Text returns the string of identified text.
func (texts *Texts) Text(key model.ResourceKey) (result string, err error) {
info, known := knownTexts[key.Type]
if known && (key.Index < info.limit) && key.HasValidLanguage() {
if info.multiblock {
holder := texts.cybstrng[key.Language.ToIndex()].Get(res.ResourceID(int(key.Type) + int(key.Index)))
if holder != nil {
for blockIndex := uint16(0); blockIndex < holder.BlockCount(); blockIndex++ {
blockData := holder.BlockData(blockIndex)
result += texts.cp.Decode(blockData)
}
}
} else {
holder := texts.cybstrng[key.Language.ToIndex()].Get(res.ResourceID(key.Type))
if key.Index < holder.BlockCount() {
blockData := holder.BlockData(key.Index)
result = texts.cp.Decode(blockData)
}
}
} else {
err = fmt.Errorf("Unsupported resource key: %v", key)
}
return
}
// SetText requests to set the string of a text resource.
func (texts *Texts) SetText(key model.ResourceKey, value string) (resultKey model.ResourceKey, err error) {
info, known := knownTexts[key.Type]
emptyString := texts.cp.Encode("")
if known && (key.Index < info.limit) && key.HasValidLanguage() {
if info.multiblock {
store := texts.cybstrng[key.Language.ToIndex()]
chunkID := res.ResourceID(int(key.Type) + int(key.Index))
blockData := [][]byte{texts.cp.Encode(value)}
store.Put(chunkID, &chunk.Chunk{
ContentType: chunk.Text,
Fragmented: true,
BlockProvider: chunk.MemoryBlockProvider(blockData)})
} else {
holder := texts.cybstrng[key.Language.ToIndex()].Get(res.ResourceID(key.Type))
for holder.BlockCount() < key.Index {
holder.SetBlockData(holder.BlockCount(), emptyString)
}
holder.SetBlockData(key.Index, texts.cp.Encode(value))
}
resultKey = key
} else {
err = fmt.Errorf("Unsupported resource key: %v", key)
}
return
}