/
queryciv3.go
270 lines (248 loc) · 8.04 KB
/
queryciv3.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
260
261
262
263
264
265
266
267
268
269
270
package queryciv3
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/myjimnelson/c3sat/civ3decompress"
)
type sectionType struct {
name string
offset int
}
type saveGameType struct {
path string
data []byte
sections []sectionType
}
var saveGame saveGameType
var defaultBic saveGameType
var currentBic saveGameType
var currentGame saveGameType
// populates the structure given a path to a sav file
func (sav *saveGameType) loadSave(path string) error {
var err error
sav.data, _, err = civ3decompress.ReadFile(path)
if err != nil {
return err
}
sav.path = path
sav.populateSections()
// If this is a save game, populate BIQ and save vars
// This is still hackish
if string(sav.data[0:4]) == "CIV3" {
gameOff, err := sav.sectionOffset("GAME", 2)
if err != nil {
return nil
}
currentGame.data = sav.data[gameOff-4:]
currentGame.populateSections()
bicOff, err := sav.sectionOffset("VER#", 1)
if err != nil {
return nil
}
if sav.readInt32(bicOff+8, Unsigned) == 0xcdcdcdcd {
currentBic = defaultBic
} else {
currentBic.data = sav.data[bicOff-8 : gameOff]
currentBic.populateSections()
}
currentGame.data = saveGame.data[gameOff-4:]
}
return nil
}
// Find sections demarc'ed by 4-character ASCII headers and place into sections[]
func (sav *saveGameType) populateSections() {
var i, count, offset int
sav.sections = make([]sectionType, 0)
// find sections demarc'ed by 4-character ASCII headers
for i < len(sav.data) {
if sav.data[i] < 0x20 || sav.data[i] > 0x5a {
count = 0
} else {
if count == 0 {
offset = i
}
count++
}
i++
if count > 3 {
count = 0
s := new(sectionType)
s.offset = offset
s.name = string(sav.data[offset:i])
sav.sections = append(sav.sections, *s)
}
}
}
// returns just the filename part of the path assuming / or \ separators
func (sav *saveGameType) fileName() string {
var o int
for i := 0; i < len(sav.path); i++ {
if sav.path[i] == 0x2f || sav.path[i] == 0x5c {
o = i
}
}
return sav.path[o+1:]
}
// Transitioning to this from the old SecionOffset stanalone function
func (sav *saveGameType) sectionOffset(sectionName string, nth int) (int, error) {
var i, n int
for i < len(sav.sections) {
if sav.sections[i].name == sectionName {
n++
if n >= nth {
return sav.sections[i].offset + len(sectionName), nil
}
}
i++
}
return -1, errors.New("Could not find " + strconv.Itoa(nth) + " section named " + sectionName)
}
func (sav *saveGameType) readInt32(offset int, signed bool) int {
n := int(sav.data[offset]) +
int(sav.data[offset+1])*0x100 +
int(sav.data[offset+2])*0x10000 +
int(sav.data[offset+3])*0x1000000
if signed && n&0x80000000 != 0 {
n = -(n ^ 0xffffffff + 1)
}
return n
}
func (sav *saveGameType) readInt16(offset int, signed bool) int {
n := int(sav.data[offset]) +
int(sav.data[offset+1])*0x100
if signed && n&0x8000 != 0 {
n = -(n ^ 0xffff + 1)
}
return n
}
func (sav *saveGameType) readInt8(offset int, signed bool) int {
n := int(sav.data[offset])
if signed && n&0x80 != 0 {
n = -(n ^ 0xff + 1)
}
return n
}
// ChangeSavePath updates the package saveGame structure with save file data at <path>
func ChangeSavePath(path string) error {
err := saveGame.loadSave(path)
return err
}
// ChangeSavePath updates the package saveGame structure with save file data at <path>
func ChangeDefaultBicPath(path string) error {
err := defaultBic.loadSave(path)
return err
}
// Handler wrapper to allow adding headers to all responses
// concept yoinked from http://echorand.me/dissecting-golangs-handlerfunc-handle-and-defaultservemux.html
func setHeaders(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set Origin headers for CORS
// yoinked from http://stackoverflow.com/questions/12830095/setting-http-headers-in-golang Matt Bucci's answer
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
// Since we're dynamically setting origin, don't let it get cached
w.Header().Set("Vary", "Origin")
handler.ServeHTTP(w, r)
})
}
// GraphQlHandler returns a GraphQL http handler
func GraphQlHandler() (http.Handler, error) {
Schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
// Mutation: MutationType,
})
if err != nil {
return nil, err
}
// create a graphl-go HTTP handler
graphQlHandler := handler.New(&handler.Config{
Schema: &Schema,
Pretty: false,
// GraphiQL provides simple web browser query interface pulled from Internet
GraphiQL: false,
// Playground provides fancier web browser query interface pulled from Internet
Playground: true,
})
return graphQlHandler, nil
}
func Server(path string, bindAddress, bindPort string) error {
err := saveGame.loadSave(path)
if err != nil {
return err
}
gQlHandler, err := GraphQlHandler()
if err != nil {
return err
}
http.Handle("/graphql", setHeaders(gQlHandler))
http.ListenAndServe(bindAddress+":"+bindPort, nil)
if err != nil {
return err
}
return nil
}
func Query(query, path string) (string, error) {
err := saveGame.loadSave(path)
if err != nil {
return "", err
}
Schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
// Mutation: MutationType,
})
if err != nil {
return "", err
}
result := graphql.Do(graphql.Params{
Schema: Schema,
RequestString: query,
})
out, err := json.Marshal(result)
if err != nil {
return "", err
}
return string(out[:]), nil
}
// Adapting this from the parseciv3 module to pull data this module's way
// To keep the existing seed command without having to gql everything
// WorldSettings Returns the information needed to regenerate the map
// presuming the map was originally generated by Civ3 and not later edited
func WorldSettings(path string) ([][3]string, error) {
var worldSize = [...]string{"Tiny", "Small", "Standard", "Large", "Huge", "Random"}
// // "No Barbarians" is actually -1. To make code simpler, add one to value for array index
var barbs = [...]string{"No Barbarians", "Sedentary", "Roaming", "Restless", "Raging", "Random"}
var landMass = [...]string{"Archipelago", "Continents", "Pangea", "Random"}
var oceanCoverage = [...]string{"80% Water", "70% Water", "60% Water", "Random"}
var climate = [...]string{"Arid", "Normal", "Wet", "Random"}
var temperature = [...]string{"Warm", "Temperate", "Cool", "Random"}
var age = [...]string{"3 Billion", "4 Billion", "5 Billion", "Random"}
var settings [][3]string
err := saveGame.loadSave(path)
if err != nil {
return nil, err
}
wrldSection, err := SectionOffset("WRLD", 1)
if err != nil {
return [][3]string{}, err
}
settings = [][3]string{
{"Setting", "Choice", "Result"},
{"World Seed", strconv.FormatUint(uint64(ReadInt32(wrldSection+170, Signed)), 10), ""},
{"World Size", worldSize[ReadInt32(wrldSection+234, Signed)], ""},
{"Barbarians", barbs[ReadInt32(wrldSection+194, Signed)+1], barbs[ReadInt32(wrldSection+198, Signed)+1]},
{"Land Mass", landMass[ReadInt32(wrldSection+202, Signed)], landMass[ReadInt32(wrldSection+206, Signed)]},
{"Water Coverage", oceanCoverage[ReadInt32(wrldSection+210, Signed)], oceanCoverage[ReadInt32(wrldSection+214, Signed)]},
{"Climate", climate[ReadInt32(wrldSection+186, Signed)], climate[ReadInt32(wrldSection+190, Signed)]},
{"Temperature", temperature[ReadInt32(wrldSection+218, Signed)], temperature[ReadInt32(wrldSection+222, Signed)]},
{"Age", age[ReadInt32(wrldSection+226, Signed)], age[ReadInt32(wrldSection+230, Signed)]},
}
return settings, nil
}