forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_tyres.go
441 lines (337 loc) · 8.76 KB
/
content_tyres.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package servermanager
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/JustaPenguin/assetto-server-manager/pkg/acd"
"github.com/cj123/ini"
"github.com/sirupsen/logrus"
)
var tyreFiles = []string{
"ks_tyres.ini",
"mod_tyres.ini",
}
type Tyres map[string]map[string]string
func (t Tyres) Name(search string, cars []string) string {
carExists := make(map[string]bool)
for _, car := range cars {
carExists[car] = true
}
for car, carTyres := range t {
if _, ok := carExists[car]; !ok {
continue
}
if name, ok := carTyres[search]; ok {
return name
}
}
return search
}
// ListTyres reads tyres from the TyreFiles and returns them as a map of car => tyre short name => tyre long name
func ListTyres() (Tyres, error) {
out := make(Tyres)
for _, file := range tyreFiles {
tyres, err := loadTyresFromFile(file)
if os.IsNotExist(err) {
logrus.Debugf("Skipping loading tyre data from %s, file does not exist", file)
continue
} else if err != nil {
return nil, err
}
for car, carTyres := range tyres {
if _, ok := out[car]; !ok {
out[car] = make(map[string]string)
}
for key, tyreName := range carTyres {
out[car][key] = tyreName
}
}
}
return out, nil
}
func loadTyresFromFile(name string) (map[string]map[string]string, error) {
f, err := os.Open(filepath.Join(ServerInstallPath, "manager", name))
if err != nil {
return nil, err
}
defer f.Close()
i, err := ini.Load(f)
if err != nil {
return nil, err
}
out := make(map[string]map[string]string)
for _, car := range i.Sections() {
out[car.Name()] = car.KeysHash()
}
return out, nil
}
// CarNameFromFilepath takes a filepath e.g. content/cars/rss_formula_rss_4/data.acd and returns the car name, e.g.
// in this case: rss_formula_rss_4.
func CarNameFromFilepath(path string) (string, error) {
parts := strings.Split(filepath.ToSlash(filepath.Dir(path)), "/")
if len(parts) == 0 {
return "", fmt.Errorf("servermanager: can't get car name from path: %s", path)
}
name := parts[len(parts)-1]
if name == "data" {
// in the case of loading from data/tyres.ini, we need to find the parent of the data folder, i.e. two steps back
if len(parts) > 1 {
name = parts[len(parts)-2]
} else {
return "", fmt.Errorf("servermanager: can't get car name from path: %s", path)
}
}
return name, nil
}
// addTyresFromDataACD looks for tyres within the data.acd file and adds them to mod_tyres.ini if any are found
func addTyresFromDataACD(filename string, dataACD []byte) error {
carName, err := CarNameFromFilepath(filename)
if err != nil {
return err
}
r, err := acd.NewReader(bytes.NewReader(dataACD), carName)
if err != nil {
return err
}
for _, file := range r.Files {
if file.Name() != "tyres.ini" {
continue
}
b, err := file.Bytes()
if err != nil {
return err
}
newTyres, err := LoadTyresFromACDINI(b)
if err != nil {
return err
}
return addTyresToModTyres(carName, newTyres)
}
logrus.Warnf("Couldn't find tyres.ini within filepath: '%s'. Cannot add mod_tyres.ini", filename)
return nil
}
func addTyresFromTyresIni(filename string, iniFile []byte) error {
carName, err := CarNameFromFilepath(filename)
if err != nil {
return err
}
newTyres, err := LoadTyresFromACDINI(iniFile)
if err != nil {
return err
}
return addTyresToModTyres(carName, newTyres)
}
func addTyresToModTyres(model string, newTyres map[string]string) error {
for key, tyreName := range newTyres {
if strings.Contains(key, " ") {
logrus.Errorf("Couldn't import tyre: %s. Tyre is incompatible with AC Server due to space in its short name (%s).", tyreName, key)
delete(newTyres, key)
}
}
managerPath := filepath.Join(ServerInstallPath, "manager")
if _, err := os.Stat(managerPath); os.IsNotExist(err) {
err := os.MkdirAll(managerPath, 0755)
if err != nil {
return err
}
} else if err != nil {
return err
}
modTyresFilename := filepath.Join(managerPath, "mod_tyres.ini")
if _, err := os.Stat(modTyresFilename); os.IsNotExist(err) {
f, err := os.Create(modTyresFilename)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
} else if err != nil {
return err
}
currentTyres, err := ListTyres()
if err != nil {
return err
}
if carTyres, ok := currentTyres[model]; ok {
hasNew := false
for newTyre := range newTyres {
if _, ok := carTyres[newTyre]; !ok {
hasNew = true
}
}
if !hasNew {
return nil
}
}
i, err := ini.Load(modTyresFilename)
if err != nil {
return err
}
sec, err := i.NewSection(model)
if err != nil {
return err
}
for k, v := range newTyres {
_, err := sec.NewKey(k, v)
if err != nil {
return err
}
}
return i.SaveTo(modTyresFilename)
}
// LoadTyresFromACDINI reads the tyres.ini file from within the data.acd of a car and finds all available tyre compounds.
func LoadTyresFromACDINI(data []byte) (map[string]string, error) {
f, err := ini.Load(data)
if err != nil {
return nil, err
}
outTyres := make(map[string]string)
for _, sec := range f.Sections() {
vals := sec.KeysHash()
name, hasName := vals["NAME"]
key, hasKey := vals["SHORT_NAME"]
if hasName && hasKey {
outTyres[key] = name
}
}
return outTyres, nil
}
type TrackSurfacePreset struct {
Name string
Description string
SessionStart int
SessionTransfer int
Randomness int
LapGain int
}
var DefaultTrackSurfacePresets = []TrackSurfacePreset{
{
Name: "Dusty",
SessionStart: 86,
SessionTransfer: 50,
Randomness: 1,
LapGain: 30,
Description: "A very slippery track, improves fast with more laps.",
},
{
Name: "Old",
SessionStart: 89,
SessionTransfer: 80,
Randomness: 3,
LapGain: 50,
Description: "Old tarmac. Bad grip that won't get better soon.",
},
{
Name: "Slow",
SessionStart: 96,
SessionTransfer: 80,
Randomness: 1,
LapGain: 300,
Description: "A slow track that doesn't improve much.",
},
{
Name: "Green",
SessionStart: 95,
SessionTransfer: 90,
Randomness: 2,
LapGain: 132,
Description: "A clean track, gets better with more laps.",
},
{
Name: "Fast",
SessionStart: 98,
SessionTransfer: 80,
Randomness: 2,
LapGain: 700,
Description: "Very grippy track right from the start.",
},
{
Name: "Optimum",
SessionStart: 100,
SessionTransfer: 100,
Randomness: 0,
LapGain: 1,
Description: "Perfect track for hotlapping.",
},
}
var ErrCouldNotFindTyreForCar = errors.New("servermanager: could not find tyres for car")
func findTyreIndex(carModel, tyreName string, raceSetup CurrentRaceConfig) (int, error) {
tyreIndexCount := 0
legalTyres := raceSetup.Tyres()
if carModel == IERP13c {
// the IER P13c's tyre information is encrypted. Hardcoded values are used in place of the normal tyre information.
for _, tyre := range IERP13cTyres {
if tyre == tyreName {
return tyreIndexCount, nil
}
if _, available := legalTyres[tyre]; available {
// if the tyre we just found is in the availableTyres, then increment the tyreIndexCount
tyreIndexCount++
}
}
return -1, ErrCouldNotFindTyreForCar
}
tyres, err := CarDataFile(carModel, "tyres.ini")
if err != nil {
return -1, err
}
defer tyres.Close()
f, err := ini.Load(tyres)
if err != nil {
return -1, err
}
for _, section := range f.Sections() {
if strings.HasPrefix(section.Name(), "FRONT") {
// this is a tyre section for the front tyres
key, err := section.GetKey("SHORT_NAME")
if err != nil {
return -1, err
}
// we found our tyre, return the tyreIndexCount
if key.Value() == tyreName {
return tyreIndexCount, nil
}
if _, available := legalTyres[key.Value()]; available {
// if the tyre we just found is in the availableTyres, then increment the tyreIndexCount
tyreIndexCount++
}
}
}
return -1, ErrCouldNotFindTyreForCar
}
func CarDataFile(carModel, dataFile string) (io.ReadCloser, error) {
carDataFile := filepath.Join(ServerInstallPath, "content", "cars", carModel, "data.acd")
f, err := os.Open(carDataFile)
if os.IsNotExist(err) {
// this is likely an older car with a data folder
f, err := os.Open(filepath.Join(ServerInstallPath, "content", "cars", carModel, "data", dataFile))
if err != nil {
return nil, err
}
return f, nil
} else if err != nil {
return nil, err
}
defer f.Close()
r, err := acd.NewReader(f, carModel)
if err != nil {
return nil, err
}
for _, file := range r.Files {
if file.Name() == dataFile {
b, err := file.Bytes()
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewReader(b)), nil
}
}
return nil, os.ErrNotExist
}