-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcon_options.go
371 lines (326 loc) · 16.8 KB
/
rcon_options.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
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type PzOptions struct {
AdminSafehouse bool `json:"AdminSafehouse"`
AllowCoop bool `json:"AllowCoop"`
AllowDestructionBySledgehammer bool `json:"AllowDestructionBySledgehammer"`
AllowNonAsciiUsername bool `json:"AllowNonAsciiUsername"`
AnnounceDeath bool `json:"AnnounceDeath"`
AntiCheatProtectionType1 bool `json:"AntiCheatProtectionType1"`
AntiCheatProtectionType2 bool `json:"AntiCheatProtectionType2"`
AntiCheatProtectionType3 bool `json:"AntiCheatProtectionType3"`
AntiCheatProtectionType4 bool `json:"AntiCheatProtectionType4"`
AntiCheatProtectionType5 bool `json:"AntiCheatProtectionType5"`
AntiCheatProtectionType6 bool `json:"AntiCheatProtectionType6"`
AntiCheatProtectionType7 bool `json:"AntiCheatProtectionType7"`
AntiCheatProtectionType8 bool `json:"AntiCheatProtectionType8"`
AntiCheatProtectionType9 bool `json:"AntiCheatProtectionType9"`
AntiCheatProtectionType10 bool `json:"AntiCheatProtectionType10"`
AntiCheatProtectionType11 bool `json:"AntiCheatProtectionType11"`
AntiCheatProtectionType12 bool `json:"AntiCheatProtectionType12"`
AntiCheatProtectionType13 bool `json:"AntiCheatProtectionType13"`
AntiCheatProtectionType14 bool `json:"AntiCheatProtectionType14"`
AntiCheatProtectionType15 bool `json:"AntiCheatProtectionType15"`
AntiCheatProtectionType16 bool `json:"AntiCheatProtectionType16"`
AntiCheatProtectionType17 bool `json:"AntiCheatProtectionType17"`
AntiCheatProtectionType18 bool `json:"AntiCheatProtectionType18"`
AntiCheatProtectionType19 bool `json:"AntiCheatProtectionType19"`
AntiCheatProtectionType20 bool `json:"AntiCheatProtectionType20"`
AntiCheatProtectionType21 bool `json:"AntiCheatProtectionType21"`
AntiCheatProtectionType22 bool `json:"AntiCheatProtectionType22"`
AntiCheatProtectionType23 bool `json:"AntiCheatProtectionType23"`
AntiCheatProtectionType24 bool `json:"AntiCheatProtectionType24"`
AntiCheatProtectionType2ThresholdMultiplier float64 `json:"AntiCheatProtectionType2ThresholdMultiplier"`
AntiCheatProtectionType3ThresholdMultiplier float64 `json:"AntiCheatProtectionType3ThresholdMultiplier"`
AntiCheatProtectionType4ThresholdMultiplier float64 `json:"AntiCheatProtectionType4ThresholdMultiplier"`
AntiCheatProtectionType9ThresholdMultiplier float64 `json:"AntiCheatProtectionType9ThresholdMultiplier"`
AntiCheatProtectionType15ThresholdMultiplier float64 `json:"AntiCheatProtectionType15ThresholdMultiplier"`
AntiCheatProtectionType20ThresholdMultiplier float64 `json:"AntiCheatProtectionType20ThresholdMultiplier"`
AntiCheatProtectionType22ThresholdMultiplier float64 `json:"AntiCheatProtectionType22ThresholdMultiplier"`
AntiCheatProtectionType24ThresholdMultiplier float64 `json:"AntiCheatProtectionType24ThresholdMultiplier"`
AutoCreateUserInWhiteList bool `json:"AutoCreateUserInWhiteList"`
BackupsCount int `json:"BackupsCount"`
BackupsOnStart bool `json:"BackupsOnStart"`
BackupsOnVersionChange bool `json:"BackupsOnVersionChange"`
BackupsPeriod int `json:"BackupsPeriod"`
BanKickGlobalSound bool `json:"BanKickGlobalSound"`
BloodSplatLifespanDays int `json:"BloodSplatLifespanDays"`
CarEngineAttractionModifier float64 `json:"CarEngineAttractionModifier"`
ChatStreams string `json:"ChatStreams"`
ClientActionLogs string `json:"ClientActionLogs"`
ClientCommandFilter string `json:"ClientCommandFilter"`
ConstructionPreventsLootRespawn bool `json:"ConstructionPreventsLootRespawn"`
DefaultPort int `json:"DefaultPort"`
DenyLoginOnOverloadedServer bool `json:"DenyLoginOnOverloadedServer"`
DisableRadioAdmin bool `json:"DisableRadioAdmin"`
DisableRadioGM bool `json:"DisableRadioGM"`
DisableRadioInvisible bool `json:"DisableRadioInvisible"`
DisableRadioModerator bool `json:"DisableRadioModerator"`
DisableRadioOverseer bool `json:"DisableRadioOverseer"`
DisableRadioStaff bool `json:"DisableRadioStaff"`
DisableSafehouseWhenPlayerConnected bool `json:"DisableSafehouseWhenPlayerConnected"`
DiscordEnable bool `json:"DiscordEnable"`
DiscordToken string `json:"DiscordToken"`
DiscordChannel string `json:"DiscordChannel"`
DiscordChannelID string `json:"DiscordChannelID"`
DisplayUserName bool `json:"DisplayUserName"`
DoLuaChecksum bool `json:"DoLuaChecksum"`
DropOffWhiteListAfterDeath bool `json:"DropOffWhiteListAfterDeath"`
Faction bool `json:"Faction"`
FactionDaySurvivedToCreate int `json:"FactionDaySurvivedToCreate"`
FactionPlayersRequiredForTag int `json:"FactionPlayersRequiredForTag"`
FastForwardMultiplier float64 `json:"FastForwardMultiplier"`
GlobalChat bool `json:"GlobalChat"`
HidePlayersBehindYou bool `json:"HidePlayersBehindYou"`
HoursForLootRespawn int `json:"HoursForLootRespawn"`
ItemNumbersLimitPerContainer int `json:"ItemNumbersLimitPerContainer"`
KickFastPlayers bool `json:"KickFastPlayers"`
KnockedDownAllowed bool `json:"KnockedDownAllowed"`
LoginQueueConnectTimeout int `json:"LoginQueueConnectTimeout"`
LoginQueueEnabled bool `json:"LoginQueueEnabled"`
Map string `json:"Map"`
MapRemotePlayerVisibility int `json:"MapRemotePlayerVisibility"`
MaxAccountsPerUser int `json:"MaxAccountsPerUser"`
MaxItemsForLootRespawn int `json:"MaxItemsForLootRespawn"`
MaxPlayers int `json:"MaxPlayers"`
MinutesPerPage float64 `json:"MinutesPerPage"`
Mods string `json:"Mods"`
MouseOverToSeeDisplayName bool `json:"MouseOverToSeeDisplayName"`
NoFire bool `json:"NoFire"`
Open bool `json:"Open"`
PVP bool `json:"PVP"`
PVPFirearmDamageModifier float64 `json:"PVPFirearmDamageModifier"`
PVPMeleeDamageModifier float64 `json:"PVPMeleeDamageModifier"`
PVPMeleeWhileHitReaction bool `json:"PVPMeleeWhileHitReaction"`
PauseEmpty bool `json:"PauseEmpty"`
PerkLogs bool `json:"PerkLogs"`
PingLimit int `json:"PingLimit"`
PlayerBumpPlayer bool `json:"PlayerBumpPlayer"`
PlayerRespawnWithOther bool `json:"PlayerRespawnWithOther"`
PlayerRespawnWithSelf bool `json:"PlayerRespawnWithSelf"`
PlayerSafehouse bool `json:"PlayerSafehouse"`
Public bool `json:"Public"`
PublicDescription string `json:"PublicDescription"`
PublicName string `json:"PublicName"`
RemovePlayerCorpsesOnCorpseRemoval bool `json:"RemovePlayerCorpsesOnCorpseRemoval"`
ResetID int `json:"ResetID"`
SafeHouseRemovalTime int `json:"SafeHouseRemovalTime"`
SafehouseAllowFire bool `json:"SafehouseAllowFire"`
SafehouseAllowLoot bool `json:"SafehouseAllowLoot"`
SafehouseAllowNonResidential bool `json:"SafehouseAllowNonResidential"`
SafehouseAllowRespawn bool `json:"SafehouseAllowRespawn"`
SafehouseAllowTrepass bool `json:"SafehouseAllowTrepass"`
SafehouseDaySurvivedToClaim int `json:"SafehouseDaySurvivedToClaim"`
SafetyCooldownTimer int `json:"SafetyCooldownTimer"`
SafetySystem bool `json:"SafetySystem"`
SafetyToggleTimer int `json:"SafetyToggleTimer"`
SaveWorldEveryMinutes int `json:"SaveWorldEveryMinutes"`
ServerPlayerID int `json:"ServerPlayerID"`
ShowFirstAndLastName bool `json:"ShowFirstAndLastName"`
ShowSafety bool `json:"ShowSafety"`
SledgehammerOnlyInSafehouse bool `json:"SledgehammerOnlyInSafehouse"`
SleepAllowed bool `json:"SleepAllowed"`
SleepNeeded bool `json:"SleepNeeded"`
SneakModeHideFromOtherPlayers bool `json:"SneakModeHideFromOtherPlayers"`
SpawnItems string `json:"SpawnItems"`
SpawnPoint string `json:"SpawnPoint"`
SpeedLimit float64 `json:"SpeedLimit"`
SteamScoreboard bool `json:"SteamScoreboard"`
SteamVAC bool `json:"SteamVAC"`
TrashDeleteAll bool `json:"TrashDeleteAll"`
UDPPort int `json:"UDPPort"`
UPnP bool `json:"UPnP"`
Voice3D bool `json:"Voice3D"`
VoiceEnable bool `json:"VoiceEnable"`
VoiceMaxDistance float64 `json:"VoiceMaxDistance"`
VoiceMinDistance float64 `json:"VoiceMinDistance"`
WorkshopItems string `json:"WorkshopItems"`
ServerWelcomeMessage string `json:"ServerWelcomeMessage"`
}
type OptionPair struct {
Name string
Value string
}
var (
pzOptions PzOptions
lastOptionsHash string
)
func setFieldValue(field reflect.Value, value string) error {
switch field.Kind() {
case reflect.Bool:
field.SetBool(strings.ToLower(value) == "true")
case reflect.Int:
intVal, err := strconv.Atoi(value)
if err != nil {
return err
}
field.SetInt(int64(intVal))
case reflect.Float64:
floatVal, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
field.SetFloat(floatVal)
case reflect.String:
field.SetString(value)
default:
return errors.New("unsupported field type")
}
return nil
}
func hashString(input string) string {
hash := sha256.Sum256([]byte(input))
return hex.EncodeToString(hash[:])
}
func pzOptions_update() error {
res, err := conn.Execute("showoptions")
if err != nil {
return fmt.Errorf("error getting options: %v", err)
}
currentHash := hashString(res)
if currentHash == lastOptionsHash {
runtime.LogTracef(app.ctx, "Options unchanged, skipping sync")
return nil
}
lastOptionsHash = currentHash
lines := strings.Split(res, "\n")
updatedOptions := PzOptions{}
if err := parseOptions(lines, &updatedOptions); err != nil {
return fmt.Errorf("error parsing options: %v", err)
}
pzOptions = updatedOptions
runtime.EventsEmit(app.ctx, "update-options", pzOptions)
runtime.LogDebugf(app.ctx, "Options synced: %v", pzOptions)
return nil
}
func parseOptions(lines []string, target *PzOptions) error {
v := reflect.ValueOf(target).Elem()
for _, line := range lines {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "* ") {
continue
}
parts := strings.SplitN(strings.TrimPrefix(line, "* "), "=", 2)
if len(parts) != 2 {
runtime.LogDebugf(app.ctx, "Invalid option: %s", line)
continue
}
fieldName, fieldValue := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
field := v.FieldByName(fieldName)
if !field.IsValid() {
runtime.LogDebugf(app.ctx, "Unknown option: %s", fieldName)
continue
}
if err := setFieldValue(field, fieldValue); err != nil {
return fmt.Errorf("failed to set field %s: %v", fieldName, err)
}
}
return nil
}
func (app *App) UpdatePzOptions(newOptions PzOptions, reloadOptions bool) bool {
defer pzOptions_update()
optionsToUpdate := app.diffOptions(newOptions)
if len(optionsToUpdate) == 0 {
app.SendNotification(Notification{Title: "rcon.no_options_to_update", Variant: "warning"})
return false
}
successCount := app.applyOptions(optionsToUpdate)
if successCount != len(optionsToUpdate) {
app.SendNotification(Notification{Title: "rcon.failed_to_update_n_options", Parameters: map[string]string{
"n": fmt.Sprintf("%d", len(optionsToUpdate)-successCount),
}, Variant: "error"})
return false
}
if err := pzOptions_update(); err != nil {
runtime.LogErrorf(app.ctx, "Error syncing options after update: %v", err)
app.SendNotification(Notification{Title: "rcon.options_updated_sync_failed", Variant: "error"})
return false
}
if reloadOptions {
command := RCONCommand{
CommandTemplate: "reloadoptions",
SuccessCheck: func(name string, response string) bool {
return response == "Options reloaded"
},
}
success := command.execute() == 1
if !success {
app.SendNotification(Notification{Title: "rcon.reloadOptions.single_fail", Variant: "error"})
return false
} else {
app.SendNotification(Notification{Title: "rcon.options_saved_and_reloaded", Variant: "success"})
return true
}
}
app.SendNotification(Notification{Title: "rcon.options_updated", Variant: "success"})
return true
}
func (app *App) diffOptions(newOptions PzOptions) []OptionPair {
var optionsToUpdate []OptionPair
newVal := reflect.ValueOf(newOptions)
oldVal := reflect.ValueOf(pzOptions)
for i := 0; i < newVal.NumField(); i++ {
fieldName := newVal.Type().Field(i).Name
newField := newVal.Field(i).Interface()
oldField := oldVal.FieldByName(fieldName).Interface()
if newField != oldField {
runtime.LogDebugf(app.ctx, "Updating %s from %v to %v", fieldName, oldField, newField)
optionsToUpdate = append(optionsToUpdate, OptionPair{
Name: fieldName,
Value: fmt.Sprintf("%v", newField),
})
}
}
return optionsToUpdate
}
func (app *App) applyOptions(options []OptionPair) int {
defer runtime.EventsEmit(app.ctx, "setProgress", 0)
runtime.EventsEmit(app.ctx, "setProgress", 10)
successCount := 0
optionCount := len(options)
for _, option := range options {
runtime.EventsEmit(app.ctx, "setProgress", float64(successCount)/float64(optionCount)*100)
command := fmt.Sprintf("changeoption %s \"%s\"", option.Name, option.Value)
res, err := conn.Execute(command)
if err == nil && isOptionUpdateSuccessful(option, res) {
successCount++
} else {
runtime.LogErrorf(app.ctx, "Failed to update %s: %v", option.Name, err)
}
}
return successCount
}
func isOptionUpdateSuccessful(option OptionPair, res string) bool {
// Extract the server response value for the given option
expectedResponse := fmt.Sprintf("Option : %s is now : %s", option.Name, option.Value)
if res == expectedResponse {
return true
}
// For floats, handle formatting differences
field := reflect.ValueOf(pzOptions).FieldByName(option.Name)
if !field.IsValid() {
runtime.LogErrorf(nil, "Invalid field name: %s", option.Name)
return false
}
switch field.Kind() {
case reflect.Float64:
value, err := strconv.ParseFloat(option.Value, 64)
if err != nil {
runtime.LogErrorf(nil, "Failed to parse float: %s", option.Value)
return false
}
normalized := fmt.Sprintf("Option : %s is now : %.1f", option.Name, value)
return res == normalized
default:
return false
}
}