-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt.go
164 lines (143 loc) · 4.1 KB
/
txt.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
package brlyt
import (
"bytes"
"encoding/binary"
"strings"
"unicode/utf16"
)
func (r *Root) ParseTXT(data []byte, sectionSize uint32) (*XMLTXT, error) {
var text TXT
err := binary.Read(bytes.NewReader(data), binary.BigEndian, &text)
if err != nil {
return nil, err
}
// Strip the null bytes from the strings
name := strings.Replace(string(text.PaneName[:]), "\x00", "", -1)
userData := strings.Replace(string(text.UserData[:]), "\x00", "", -1)
utf16String := data[text.TextOffset-8 : sectionSize-8]
// Convert the UTF-16 string to UTF-8
var full []uint16
for i := 0; i < len(utf16String); i += 2 {
current := binary.BigEndian.Uint16([]byte{utf16String[i], utf16String[i+1]})
if current == 0 {
// Our string was terminated
break
}
full = append(full, current)
}
// Strip null bytes
decodedString := strings.Replace(string(utf16.Decode(full)), "\x00", "", -1)
txtXML := XMLTXT{
Name: name,
UserData: userData,
Visible: text.Flag & 0x1,
Widescreen: (text.Flag & 0x2) >> 1,
Flag: text.Flag,
Origin: Coord2D{X: float32(text.Origin % 3), Y: float32(text.Origin / 3)},
Alpha: text.Alpha,
Padding: 0,
Translate: Coord3D{X: text.XTranslation, Y: text.YTranslation, Z: text.ZTranslation},
Rotate: Coord3D{X: text.XRotate, Y: text.YRotate, Z: text.ZRotate},
Scale: Coord2D{X: text.XScale, Y: text.YScale},
Width: text.Width,
Height: text.Height,
StringLength: text.StringLength,
MaxStringLength: text.MaxStringLength,
MatIndex: text.MatIndex,
StringOrigin: text.StringOrigin,
LineAlignment: text.LineAlignment,
XSize: text.FontSizeX,
YSize: text.FontSizeY,
CharSize: text.CharacterSize,
LineSize: text.LineSize,
TopColor: Color8{
R: text.TopColor[0],
G: text.TopColor[1],
B: text.TopColor[2],
A: text.TopColor[3],
},
BottomColor: Color8{
R: text.BottomColor[0],
G: text.BottomColor[1],
B: text.BottomColor[2],
A: text.BottomColor[3],
},
Text: decodedString,
}
if r.HasChildren() {
txtXML.Children, err = r.ParseChildren()
if err != nil {
return nil, err
}
}
return &txtXML, nil
}
func (b *BRLYTWriter) WriteTXT(txt XMLTXT) error {
temp := bytes.NewBuffer(nil)
header := SectionHeader{
Type: SectionTypeTXT,
Size: 124,
}
var name [16]byte
copy(name[:], txt.Name)
var userData [8]byte
copy(userData[:], txt.UserData)
text := strings.Replace(txt.Text, "\\n", "\n", -1)
encodedText := utf16.Encode([]rune(text))
pane := TXT{
Flag: txt.Flag,
Origin: uint8(txt.Origin.X + (txt.Origin.Y * 3)),
Alpha: txt.Alpha,
PaneName: name,
UserData: userData,
XTranslation: txt.Translate.X,
YTranslation: txt.Translate.Y,
ZTranslation: txt.Translate.Z,
XRotate: txt.Rotate.X,
YRotate: txt.Rotate.Y,
ZRotate: txt.Rotate.Z,
XScale: txt.Scale.X,
YScale: txt.Scale.Y,
Width: txt.Width,
Height: txt.Height,
StringLength: txt.StringLength,
MaxStringLength: txt.MaxStringLength,
MatIndex: txt.MatIndex,
FontIndex: 0,
StringOrigin: txt.StringOrigin,
LineAlignment: txt.LineAlignment,
TextOffset: 116,
TopColor: [4]uint8{txt.TopColor.R, txt.TopColor.G, txt.TopColor.B, txt.TopColor.A},
BottomColor: [4]uint8{txt.BottomColor.R, txt.BottomColor.G, txt.BottomColor.B, txt.BottomColor.A},
FontSizeX: txt.XSize,
FontSizeY: txt.YSize,
CharacterSize: txt.CharSize,
LineSize: txt.LineSize,
}
err := write(temp, header)
if err != nil {
return err
}
err = write(temp, pane)
if err != nil {
return err
}
err = write(temp, encodedText)
if err != nil {
return err
}
pos := 0
for (b.Len()+temp.Len())%4 != 0 {
temp.WriteByte(0)
pos += 1
}
// If there is no modulo padding, pad with an u32
if pos == 0 {
err = write(temp, uint32(0))
if err != nil {
return err
}
}
binary.BigEndian.PutUint32(temp.Bytes()[4:8], uint32(temp.Len()))
return write(b, temp.Bytes())
}