This repository has been archived by the owner on Jul 28, 2018. It is now read-only.
/
Textures.go
174 lines (146 loc) · 5.37 KB
/
Textures.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package core
import (
"bytes"
"encoding/binary"
"github.com/inkyblackness/res"
"github.com/inkyblackness/res/image"
"github.com/inkyblackness/res/text"
"github.com/inkyblackness/res/textprop"
"github.com/inkyblackness/shocked-core/io"
model "github.com/inkyblackness/shocked-model"
)
// Textures is the adapter of texture information.
type Textures struct {
cybstrng [model.LanguageCount]*io.DynamicChunkStore
images *io.DynamicChunkStore
cp text.Codepage
properties textprop.Store
}
// NewTextures returns a new Textures instance, if possible.
func NewTextures(library io.StoreLibrary) (textures *Textures, err error) {
var cybstrng [model.LanguageCount]*io.DynamicChunkStore
var images *io.DynamicChunkStore
var properties textprop.Store
for i := 0; i < model.LanguageCount && err == nil; i++ {
cybstrng[i], err = library.ChunkStore(localized[i].cybstrng)
}
if err == nil {
images, err = library.ChunkStore("texture.res")
}
if err == nil {
properties, err = library.TextpropStore("textprop.dat")
}
if err == nil {
textures = &Textures{cybstrng: cybstrng, images: images, cp: text.DefaultCodepage(), properties: properties}
}
return
}
// TextureCount returns the number of available textures.
func (textures *Textures) TextureCount() int {
return 273
}
// Image returns the bitmap of identified & sized texture.
func (textures *Textures) Image(index int, size model.TextureSize) (bmp image.Bitmap) {
var resID res.ResourceID
blockIndex := uint16(0)
if size == model.TextureLarge {
resID = res.ResourceID(0x03E8 + index)
} else if size == model.TextureMedium {
resID = res.ResourceID(0x02C3 + index)
} else if size == model.TextureSmall {
resID = res.ResourceID(0x004D)
blockIndex = uint16(index)
} else if size == model.TextureIcon {
resID = res.ResourceID(0x004C)
blockIndex = uint16(index)
}
holder := textures.images.Get(resID)
var blockData []byte
if holder != nil {
blockData = holder.BlockData(blockIndex)
}
if len(blockData) > 0 {
bmp, _ = image.Read(bytes.NewReader(blockData))
}
return
}
// SetImage requests to set the bitmap of identified & sized texture..
func (textures *Textures) SetImage(index int, size model.TextureSize, imgBitmap image.Bitmap) {
writer := bytes.NewBuffer(nil)
image.Write(writer, imgBitmap, image.UncompressedBitmap, false, 0)
blockData := writer.Bytes()
if size == model.TextureLarge {
textures.images.Get(res.ResourceID(0x03E8+index)).SetBlockData(0, blockData)
} else if size == model.TextureMedium {
textures.images.Get(res.ResourceID(0x02C3+index)).SetBlockData(0, blockData)
} else if size == model.TextureSmall {
textures.images.Get(res.ResourceID(0x004D)).SetBlockData(uint16(index), blockData)
} else if size == model.TextureIcon {
textures.images.Get(res.ResourceID(0x004C)).SetBlockData(uint16(index), blockData)
}
}
func (textures *Textures) rawProperties(index int) (entry textprop.Entry) {
rawProperties := textures.properties.Get(uint32(index))
reader := bytes.NewReader(rawProperties)
binary.Read(reader, binary.LittleEndian, &entry)
return
}
func (textures *Textures) setRawProperties(index int, entry textprop.Entry) {
writer := bytes.NewBuffer(nil)
binary.Write(writer, binary.LittleEndian, &entry)
textures.properties.Put(uint32(index), writer.Bytes())
}
// Properties returns the texture properties of the identified texture.
func (textures *Textures) Properties(index int) model.TextureProperties {
prop := model.TextureProperties{}
rawProperties := textures.rawProperties(index)
for i := 0; i < model.LanguageCount; i++ {
names := textures.cybstrng[i].Get(res.ResourceID(0x086A))
cantBeUseds := textures.cybstrng[i].Get(res.ResourceID(0x086B))
prop.Name[i] = textures.decodeString(names.BlockData(uint16(index)))
prop.CantBeUsed[i] = textures.decodeString(cantBeUseds.BlockData(uint16(index)))
}
prop.Climbable = boolAsPointer(rawProperties.IsClimbable())
prop.TransparencyControl = intAsPointer(int(rawProperties.TransparencyControl))
prop.AnimationGroup = intAsPointer(int(rawProperties.AnimationGroup))
prop.AnimationIndex = intAsPointer(int(rawProperties.AnimationIndex))
return prop
}
// SetProperties requests to update the properties of identified texture.
func (textures *Textures) SetProperties(index int, prop model.TextureProperties) {
rawProperties := textures.rawProperties(index)
for i := 0; i < model.LanguageCount; i++ {
if prop.Name[i] != nil {
names := textures.cybstrng[i].Get(res.ResourceID(0x086A))
names.SetBlockData(uint16(index), textures.encodeString(prop.Name[i]))
}
if prop.CantBeUsed[i] != nil {
cantBeUseds := textures.cybstrng[i].Get(res.ResourceID(0x086B))
cantBeUseds.SetBlockData(uint16(index), textures.encodeString(prop.CantBeUsed[i]))
}
}
if prop.Climbable != nil {
rawProperties.Climbable = 0
if *prop.Climbable {
rawProperties.Climbable = 1
}
}
if prop.TransparencyControl != nil {
rawProperties.TransparencyControl = byte(*prop.TransparencyControl)
}
if prop.AnimationGroup != nil {
rawProperties.AnimationGroup = byte(*prop.AnimationGroup)
}
if prop.AnimationIndex != nil {
rawProperties.AnimationIndex = byte(*prop.AnimationIndex)
}
textures.setRawProperties(index, rawProperties)
}
func (textures *Textures) decodeString(data []byte) *string {
value := textures.cp.Decode(data)
return &value
}
func (textures *Textures) encodeString(value *string) []byte {
data := textures.cp.Encode(*value)
return data
}