-
Notifications
You must be signed in to change notification settings - Fork 3
/
dlg.go
259 lines (233 loc) · 6.89 KB
/
dlg.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package bg
import (
"encoding/binary"
"encoding/json"
"io"
"os"
"strconv"
)
type dlgHeader struct {
Signature, Version [4]byte
StateCount uint32
StateOffset uint32
TransitionCount uint32
TransitionOffset uint32
StateTriggerOffset uint32
StateTriggerCount uint32
TransitionTriggerOffset uint32
TransitionTriggerCount uint32
ActionOffset uint32
ActionCount uint32
Flags uint32
}
type DlgState struct {
Stringref uint32
TransitionIndex uint32
TransitionCount uint32
TriggerIndex int32
}
type DlgTransition struct {
Flags uint32
TransitionText uint32
JournalText uint32
TransitionTriggerIndex uint32
TransitionActionIndex uint32
NextDlg RESREF
NextState uint32
}
type dlgOffsetLength struct {
Offset uint32
Length uint32
}
type DLG struct {
Header dlgHeader
States []DlgState
Transitions []DlgTransition
StateTriggers []string
TransitionTriggers []string
Actions []string
}
func (trans *DlgTransition) HasText() bool {
return trans.Flags&0x0001 == 0x0001
}
func (trans *DlgTransition) HasTrigger() bool {
return trans.Flags&0x0002 == 0x0002
}
func (trans *DlgTransition) HasAction() bool {
return trans.Flags&0x0004 == 0x0004
}
func (trans *DlgTransition) TerminatesDialog() bool {
return trans.Flags&0x0008 == 0x0008
}
func (trans *DlgTransition) HasJournal() bool {
return trans.Flags&0x0010 == 0x0010
}
func (trans *DlgTransition) AddQuest() bool {
return trans.Flags&0x0040 == 0x0040
}
func (trans *DlgTransition) RemoveQuest() bool {
return trans.Flags&0x0080 == 0x0080
}
func (trans *DlgTransition) AddCompleteQuest() bool {
return trans.Flags&0x0100 == 0x0100
}
/*
func (dlg *DLG) Print(tlk *TLK) {
for idx, state := range dlg.States {
str, _ := tlk.String(int(state.Stringref))
fmt.Printf("State[%d]: %s\n", idx, str)
if state.TriggerIndex < int32(len(dlg.StateTriggers)) && state.TriggerIndex >= 0 {
fmt.Printf("Trigger: %#v\n", dlg.StateTriggers[state.TriggerIndex])
}
for _, transition := range dlg.Transitions[state.TransitionIndex : state.TransitionIndex + state.TransitionCount] {
if transition.HasText() {
str, _ := tlk.String(int(transition.TransitionText))
fmt.Printf("\tText: %s\n", str)
}
if transition.HasTrigger() && transition.TransitionTriggerIndex < uint32(len(dlg.TransitionTriggers)) && transition.TransitionTriggerIndex >= 0 {
fmt.Printf("\tTrigger: %#v\n", dlg.TransitionTriggers[transition.TransitionTriggerIndex])
}
if transition.HasAction() && transition.TransitionActionIndex < uint32(len(dlg.Actions)) {
fmt.Printf("\t\tAction: %#v\n", dlg.Actions[transition.TransitionActionIndex])
}
if transition.HasJournal() {
str, _ := tlk.String(int(transition.JournalText))
fmt.Printf("\t\tJournal Entry: %s\n", str)
}
if transition.TerminatesDialog() {
fmt.Printf("\t\tDialog Exit\n")
} else {
fmt.Printf("\t\tNext Dialog: %s [%d]\n", transition.NextDlg, transition.NextState)
}
}
}
}
func (dlg *DLG) PrintDot(tlk *TLK, root string) {
root = strings.ToLower(root)
for idx, state := range dlg.States {
//str, _ := tlk.String(int(state.Stringref))
if state.TriggerIndex >= 0 {
fmt.Printf("%s -> %s_s%d;\n", root, root, idx);
}
for _, transition := range dlg.Transitions[state.TransitionIndex : state.TransitionIndex + state.TransitionCount] {
if !transition.TerminatesDialog() {
//fmt.Printf("%s_s%d -> %s_s%d_t%d;\n", root, idx, root, idx, tIdx)
fmt.Printf("%s_s%d -> %s_s%d;\n", root, idx, strings.ToLower(transition.NextDlg.String()), transition.NextState)
}
}
}
}
*/
func OpenDlg(r io.ReadSeeker) (*DLG, error) {
dlg := &DLG{}
err := binary.Read(r, binary.LittleEndian, &dlg.Header)
if err != nil {
return nil, err
}
dlg.States = make([]DlgState, dlg.Header.StateCount)
_, err = r.Seek(int64(dlg.Header.StateOffset), os.SEEK_SET)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &dlg.States)
if err != nil {
return nil, err
}
dlg.Transitions = make([]DlgTransition, dlg.Header.TransitionCount)
_, err = r.Seek(int64(dlg.Header.TransitionOffset), os.SEEK_SET)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &dlg.Transitions)
if err != nil {
return nil, err
}
dlg.StateTriggers = make([]string, dlg.Header.StateTriggerCount)
for idx := range dlg.StateTriggers {
ol := dlgOffsetLength{}
_, err = r.Seek(int64(int(dlg.Header.StateTriggerOffset)+idx*binary.Size(ol)), os.SEEK_SET)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &ol)
_, err = r.Seek(int64(ol.Offset), os.SEEK_SET)
if err != nil {
return nil, err
}
data := make([]byte, ol.Length)
r.Read(data)
dlg.StateTriggers[idx] = string(data[0:])
}
dlg.TransitionTriggers = make([]string, dlg.Header.TransitionTriggerCount)
for idx := range dlg.TransitionTriggers {
ol := dlgOffsetLength{}
_, err = r.Seek(int64(int(dlg.Header.TransitionTriggerOffset)+idx*binary.Size(ol)), os.SEEK_SET)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &ol)
_, err = r.Seek(int64(ol.Offset), os.SEEK_SET)
if err != nil {
return nil, err
}
data := make([]byte, ol.Length)
r.Read(data)
dlg.TransitionTriggers[idx] = string(data[0:])
}
dlg.Actions = make([]string, dlg.Header.ActionCount)
for idx := range dlg.Actions {
ol := dlgOffsetLength{}
_, err = r.Seek(int64(int(dlg.Header.ActionOffset)+idx*binary.Size(ol)), os.SEEK_SET)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &ol)
_, err = r.Seek(int64(ol.Offset), os.SEEK_SET)
if err != nil {
return nil, err
}
data := make([]byte, ol.Length)
r.Read(data)
dlg.Actions[idx] = string(data[0:])
}
return dlg, nil
}
func (dialog *DLG) WriteJson(w io.Writer) error {
bytes, err := json.MarshalIndent(dialog, "", "\t")
if err != nil {
return err
}
_, err = w.Write(bytes)
return err
}
type dlgEdge struct {
Id string `json:"id"`
Source string `json:"source"`
Target string `json:"target"`
}
type dlgNode struct {
X float32 `json:"x"`
Y float32 `json:"y"`
Label string `json:"label"`
Id string `json:"id"`
Color string `json:"color"`
Size float32 `json:"size"`
}
type dlgGraph struct {
Edges []dlgEdge
Nodes []dlgNode
}
func fetch(tlk *TLK, id uint32) string {
str, _ := tlk.String(int(id))
return str
}
func (d *DLG) ToJson(tlk *TLK) ([]byte, error) {
var graph dlgGraph
for i, state := range d.States {
graph.Nodes = append(graph.Nodes, dlgNode{Label: fetch(tlk, state.Stringref), Id: strconv.Itoa(i)})
}
for i, t := range d.Transitions {
graph.Edges = append(graph.Edges, dlgEdge{Source: "_" + strconv.Itoa(i), Target: t.NextDlg.String() + "_" + strconv.Itoa(int(t.NextState)), Id: strconv.Itoa(i)})
}
return json.MarshalIndent(graph, "", "\t")
}