forked from Rollocraft/CS2MultiplayerMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetting.cs
More file actions
499 lines (437 loc) · 21.2 KB
/
Copy pathSetting.cs
File metadata and controls
499 lines (437 loc) · 21.2 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using Colossal.IO.AssetDatabase;
using CS2MultiplayerMod.Core.Diagnostics;
using CS2MultiplayerMod.Core.Networking;
using CS2MultiplayerMod.Core.Session;
using CS2MultiplayerMod.Game.Diagnostics;
using CS2MultiplayerMod.Localization;
using Game;
using Game.Modding;
using Game.SceneFlow;
using Game.Settings;
using Game.UI.Localization;
using Game.UI.Widgets;
namespace CS2MultiplayerMod
{
[FileLocation(nameof(CS2MultiplayerMod))]
[SettingsUITabOrder(GeneralTab, JoinTab, HostTab, AdvancedTab)]
[SettingsUIGroupOrder(GeneralGroup, StatusGroup, SessionGroup, JoinSetupGroup, JoinActionGroup,
HostSetupGroup, HostActionGroup, CompatibilityGroup)]
[SettingsUIShowGroupName(GeneralGroup, StatusGroup, SessionGroup, JoinSetupGroup, JoinActionGroup,
HostSetupGroup, HostActionGroup, CompatibilityGroup)]
public class Setting : ModSetting
{
// The options UI exposes general/session state plus join and host setup.
// The Join tab shares its backing values with the start-screen dialog and
// doubles as the fallback join path when the dialog's UI module cannot
// load (e.g. another mod's broken .mjs aborts the UI-module load chain).
public const string GeneralTab = "General";
public const string JoinTab = "Join";
public const string HostTab = "Host";
public const string AdvancedTab = "Advanced";
public const string GeneralGroup = "General";
public const string StatusGroup = "Status";
public const string SessionGroup = "Session";
public const string JoinSetupGroup = "JoinSetup";
public const string JoinActionGroup = "JoinAction";
public const string HostSetupGroup = "HostSetup";
public const string HostActionGroup = "HostAction";
public const string CompatibilityGroup = "Compatibility";
/// <summary>Values of <see cref="HostConnection"/>. Stored as strings so the UI binding is one plain value.</summary>
public const string ConnectionRelay = "relay";
public const string ConnectionDirect = "direct";
private string _hostPort = "25001";
private string _hostPassword = "";
private string _hostConnection = ConnectionRelay;
private string _joinConnection = ConnectionRelay;
public Setting(IMod mod) : base(mod)
{
}
/// <summary>
/// True when no playable world is loaded. Gates host-side actions only (hosting
/// streams the current city); joining works from anywhere, so it stays unaffected.
/// </summary>
public bool IsNotInGame()
{
return GameManager.instance == null || !GameManager.instance.gameMode.IsGame();
}
public bool IsNotInSession()
{
return Mod.Service == null || Mod.Service.Session.Role == SessionRole.None;
}
public bool IsInSession()
{
return !IsNotInSession();
}
public bool IsHosting()
{
return Mod.Service != null && Mod.Service.Session.Role == SessionRole.Host;
}
public bool IsNotHosting()
{
return !IsHosting();
}
/// <summary>
/// Also false while another mod is live: the options screen's Host button reaches
/// the service directly, so the rule has to hold here too and not only on the
/// multiplayer screens.
/// </summary>
public bool CannotStartHost()
{
return IsNotInGame() || !IsNotInSession() ||
(CS2MultiplayerMod.Game.ModsCheck.AnyOtherMods && !IgnoreModCompatibilityChecks);
}
/// <summary>
/// Whether the relay is a choice on this machine. Copies of the game without Steam
/// (Microsoft Store / Game Pass) have no relay backend, so the picker is hidden and
/// everything behaves as if direct had been chosen.
/// </summary>
public bool RelayUnsupported()
{
return !RelayProvider.IsSupported;
}
/// <summary>Relay hosting opens no port, so the port and LAN controls do not apply.</summary>
public bool IsRelayHosting()
{
return HostConnection != ConnectionDirect;
}
public bool IsDirectHosting()
{
return !IsRelayHosting();
}
/// <summary>How this machine will be reached, resolved once at host/join time.</summary>
public TransportMode HostTransport()
{
return IsRelayHosting() ? TransportMode.SteamRelay : TransportMode.Direct;
}
// ---- General tab ------------------------------------------------------
[SettingsUISection(GeneralTab, GeneralGroup)]
public bool EnableMod { get; set; } = true;
/// <summary>The name every copy of the game falls back to when it knows no better.</summary>
public const string DefaultPlayerName = "Player";
[SettingsUITextInput]
[SettingsUISection(GeneralTab, GeneralGroup)]
public string PlayerName { get; set; } = DefaultPlayerName;
/// <summary>
/// Set once <see cref="ApplyPlatformNamePreset"/> has had its one chance to fill
/// <see cref="PlayerName"/> in. Persisted and hidden: without it a player who
/// deliberately calls themselves "Player" would be renamed on every start.
/// </summary>
[SettingsUIHidden]
public bool PlayerNamePresetApplied { get; set; } = false;
/// <summary>
/// First run only: prefer the platform account's own display name over the plain
/// "Player" default, so a signed-in host is recognisable to the people joining.
/// Copies of the game with no platform backend (Microsoft Store / Game Pass) have
/// no name to read and keep the default.
/// </summary>
public void ApplyPlatformNamePreset()
{
if (PlayerNamePresetApplied) return;
// Anything the player chose themselves wins, and is recorded as the final
// answer so a later start never revisits this.
string current = (PlayerName ?? "").Trim();
if (current.Length > 0 && current != DefaultPlayerName)
{
PlayerNamePresetApplied = true;
ApplyAndSave();
return;
}
// Empty means the platform cannot say (not signed in yet, or no backend at
// all). Leave the flag unset so a later start can still pick the name up.
string platformName = RelayProvider.LocalPlayerName;
if (string.IsNullOrEmpty(platformName.Trim())) return;
string preset = Core.Protocol.WireGuard.SanitizePlayerName(platformName);
PlayerName = preset;
PlayerNamePresetApplied = true;
ApplyAndSave();
SyncLog.Detail(LogTopic.Startup, "Player name preset from the platform account: '" +
preset + "'.");
}
/// <summary>
/// The mod's one logging choice.
///
/// Off, the log still carries everything a report is read for: connects and disconnects,
/// world transfers, every resync and what triggered it, dropped commands, the mod and game
/// versions, which other mods are live, and every fault. On, it also carries the
/// per-action detail underneath those - which is what to send when asked for a full log.
///
/// Safe to leave on: it makes the log longer, not the game slower. Detail lines sit behind
/// a field read and the flight log flushes them in batches. Narrowing the log to a single
/// subsystem is a developer switch and lives in code (Game/Diagnostics/LogTopics.cs) -
/// asking a player which subsystem broke is asking them to diagnose their own bug report.
/// </summary>
[SettingsUISection(GeneralTab, GeneralGroup)]
public bool VerboseLogging { get; set; } = false;
/// <summary>
/// The partner markers are the only thing this mod draws every rendered frame, so they are
/// the one part of it whose cost scales with screen resolution rather than with city size.
/// </summary>
[SettingsUISection(GeneralTab, GeneralGroup)]
public bool ShowPartnerMarkers { get; set; } = true;
/// <summary>
/// Set once the player accepts the in-game disclaimer gate (shown before the
/// first host/join). Persisted so it only appears once; intentionally hidden
/// from the options screen and left out of <see cref="SetDefaults"/> so that
/// resetting other settings does not re-prompt an existing user.
/// </summary>
[SettingsUIHidden]
public bool DisclaimerAccepted { get; set; } = false;
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusRole => Mod.Service != null ? Mod.Service.StatusRoleText : L10n.T(L10n.Key.StatusOffline);
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusState => Mod.Service != null ? Mod.Service.StatusStateText : L10n.T(L10n.Key.StatusOffline);
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusPlayers => Mod.Service != null ? Mod.Service.StatusPlayersText : L10n.T(L10n.Key.PlayersNone);
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusAccess => Mod.Service != null ? Mod.Service.StatusAccessText : L10n.T(L10n.Key.NoSession);
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusExposure => Mod.Service != null ? Mod.Service.StatusExposureText : L10n.T(L10n.Key.NoSession);
[SettingsUISection(GeneralTab, StatusGroup)]
public string StatusWorld => Mod.Service != null ? Mod.Service.StatusWorldText : L10n.T(L10n.Key.WorldNone);
[SettingsUIButton]
[SettingsUIHideByCondition(typeof(Setting), nameof(IsNotInSession))]
[SettingsUISection(GeneralTab, SessionGroup)]
public bool DisconnectButton
{
set { if (Mod.Service != null) Mod.Service.RequestDisconnect(); }
}
// ---- Host tab -----------------------------------------------------------
// Setup stays editable in the main menu so the Host Game flow can use the
// chosen values as soon as its selected city finishes loading. Only the
// direct Host Session action still requires an already loaded city.
/// <summary>
/// Relay hosting needs no reachable port: Steam carries the traffic and players
/// join with the code below. Direct hosting is the original path and still needs
/// a forwarded port (or a LAN).
/// </summary>
[SettingsUIDropdown(typeof(Setting), nameof(GetHostConnectionValues))]
[SettingsUISection(HostTab, HostSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
[SettingsUIHideByCondition(typeof(Setting), nameof(RelayUnsupported))]
public string HostConnection
{
// Reads as direct where there is no relay backend, so the options screen, the
// UI bindings and HostTransport() all agree without each having to check.
get { return RelayProvider.IsSupported ? _hostConnection : ConnectionDirect; }
set
{
if (IsInSession()) return;
_hostConnection = value == ConnectionDirect ? ConnectionDirect : ConnectionRelay;
}
}
public static DropdownItem<string>[] GetHostConnectionValues()
{
return new[]
{
new DropdownItem<string>
{
value = ConnectionRelay,
displayName = LocalizedString.Value(L10n.T(L10n.Key.ConnectionRelay)),
},
new DropdownItem<string>
{
value = ConnectionDirect,
displayName = LocalizedString.Value(L10n.T(L10n.Key.ConnectionDirect)),
},
};
}
/// <summary>The code players type on their Join screen. Relay hosting only.</summary>
[SettingsUIHideByCondition(typeof(Setting), nameof(IsDirectHosting))]
[SettingsUISection(HostTab, HostSetupGroup)]
public string HostJoinCode
{
get
{
string code = RelayProvider.LocalJoinCode;
return string.IsNullOrEmpty(code) ? L10n.T(L10n.Key.JoinCodeUnavailable) : code;
}
}
[SettingsUITextInput]
[SettingsUIHideByCondition(typeof(Setting), nameof(IsRelayHosting))]
[SettingsUISection(HostTab, HostSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsHosting))]
public string HostPort
{
get { return _hostPort; }
set
{
if (IsHosting()) return;
_hostPort = value ?? "";
}
}
[SettingsUITextInput]
[SettingsUISection(HostTab, HostSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public string HostPassword
{
get { return _hostPassword; }
set
{
if (IsInSession()) return;
_hostPassword = value ?? "";
}
}
[SettingsUIHideByCondition(typeof(Setting), nameof(IsRelayHosting))]
[SettingsUISection(HostTab, HostSetupGroup)]
public bool LanOnly { get; set; } = false;
[SettingsUISection(HostTab, HostSetupGroup)]
public bool RequireJoinApproval { get; set; } = true;
[SettingsUITextInput]
[SettingsUISection(HostTab, HostSetupGroup)]
public string MaxPlayers { get; set; } = "8";
/// <summary>
/// Host-side switch for the simulation half of the session, announced to every client
/// in the handshake so both sides agree for its whole life. Player edits - roads,
/// zoning, placed buildings, terrain, services, money, time - are unaffected either way.
///
/// Off, each city runs its own zoning simulation: the buildings that grow, who lives and
/// works in them and the demand bars are decided locally and differ between players. That
/// is the cost; the gain is that none of the per-building capture and correction work
/// runs at all, which is the part of the mod whose cost scales with population.
///
/// Persisted here but shown only in the in-game session settings: it is the host's
/// answer for one session, not a per-player option.
/// </summary>
[SettingsUIHidden]
public bool SimulationSync { get; set; } = true;
[SettingsUISection(HostTab, HostActionGroup)]
public string HostStatus => IsNotInGame()
? L10n.T(L10n.Key.HostLoadCityFirst)
: (IsNotInSession() ? L10n.T(L10n.Key.HostReady) : L10n.T(L10n.Key.HostSessionActive));
[SettingsUIButton]
[SettingsUIHideByCondition(typeof(Setting), nameof(CannotStartHost))]
[SettingsUISection(HostTab, HostActionGroup)]
public bool HostButton
{
set { if (Mod.Service != null) Mod.Service.HostFromSettings(this); }
}
/// <summary>
/// Push the host's world to all clients now - the manual drift safety-net, same as the
/// in-game hub's "Sync World". Duplicated here so it stays reachable if the hub's UI
/// module fails to load. Host-only.
/// </summary>
[SettingsUIButton]
[SettingsUIHideByCondition(typeof(Setting), nameof(IsNotHosting))]
[SettingsUISection(HostTab, HostActionGroup)]
public bool SyncWorldButton
{
set { if (Mod.Service != null) Mod.Service.RequestWorldSync(); }
}
// ---- Join tab -----------------------------------------------------------
// Shared backing values: the start-screen dialog writes the same properties
// through the cs2mp bindings, so dialog and options screen always agree.
// Joining needs no loaded city (the world comes from the host), so these
// stay visible in the main menu and are only disabled mid-session.
[SettingsUITextInput]
[SettingsUIHideByCondition(typeof(Setting), nameof(JoinIsRelay))]
[SettingsUISection(JoinTab, JoinSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public string ServerAddress { get; set; } = "127.0.0.1";
/// <summary>
/// A join code addresses the host through the relay, so no address or port is
/// involved. Chosen explicitly rather than guessed from what was typed: the
/// joining player should see the same choice the host made.
/// </summary>
public bool JoinIsRelay()
{
return JoinConnection != ConnectionDirect;
}
public bool JoinIsDirect()
{
return !JoinIsRelay();
}
public TransportMode JoinTransport()
{
return JoinIsRelay() ? TransportMode.SteamRelay : TransportMode.Direct;
}
[SettingsUIDropdown(typeof(Setting), nameof(GetHostConnectionValues))]
[SettingsUISection(JoinTab, JoinSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
[SettingsUIHideByCondition(typeof(Setting), nameof(RelayUnsupported))]
public string JoinConnection
{
get { return RelayProvider.IsSupported ? _joinConnection : ConnectionDirect; }
set
{
if (IsInSession()) return;
_joinConnection = value == ConnectionDirect ? ConnectionDirect : ConnectionRelay;
}
}
/// <summary>
/// The host's join code. Kept apart from <see cref="ServerAddress"/> so switching
/// between relay and direct does not overwrite whichever one is not in use.
/// </summary>
[SettingsUITextInput]
[SettingsUIHideByCondition(typeof(Setting), nameof(JoinIsDirect))]
[SettingsUISection(JoinTab, JoinSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public string JoinCodeInput { get; set; } = "";
[SettingsUITextInput]
[SettingsUIHideByCondition(typeof(Setting), nameof(JoinIsRelay))]
[SettingsUISection(JoinTab, JoinSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public string JoinPort { get; set; } = "25001";
[SettingsUITextInput]
[SettingsUISection(JoinTab, JoinSetupGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public string JoinPassword { get; set; } = "";
[SettingsUISection(JoinTab, JoinActionGroup)]
public string JoinStatus => Mod.Service == null
? L10n.T(L10n.Key.StatusOffline)
: (string.IsNullOrEmpty(Mod.Service.UiStatusDetail)
? Mod.Service.UiStatusTitle
: Mod.Service.UiStatusTitle + " - " + Mod.Service.UiStatusDetail);
[SettingsUIButton]
[SettingsUIHideByCondition(typeof(Setting), nameof(IsInSession))]
[SettingsUISection(JoinTab, JoinActionGroup)]
public bool JoinButton
{
set
{
if (Mod.Service == null) return;
ApplyAndSave();
Mod.Service.JoinFromSettings(this);
}
}
[SettingsUIButton]
[SettingsUIHideByCondition(typeof(Setting), nameof(IsNotInSession))]
[SettingsUISection(JoinTab, JoinActionGroup)]
public bool JoinDisconnectButton
{
set { if (Mod.Service != null) Mod.Service.RequestDisconnect(); }
}
// ---- Advanced tab -------------------------------------------------------
/// <summary>
/// Expert escape hatch for mod-specific compatibility checks. This permits other
/// active mods locally and lets a host admit a different CS2 Multiplayer Mod build.
/// Wire-protocol, game-version and DLC checks remain mandatory because bypassing
/// those can make the peers unable to interpret one another's data at all.
/// </summary>
[SettingsUISection(AdvancedTab, CompatibilityGroup)]
[SettingsUIDisableByCondition(typeof(Setting), nameof(IsInSession))]
public bool IgnoreModCompatibilityChecks { get; set; } = false;
public override void SetDefaults()
{
EnableMod = true;
VerboseLogging = false;
ShowPartnerMarkers = true;
IgnoreModCompatibilityChecks = false;
PlayerName = DefaultPlayerName;
// Resetting the name asks for the default name again, which on a signed-in
// copy of the game is the account name, not the literal "Player".
PlayerNamePresetApplied = false;
ServerAddress = "127.0.0.1";
HostConnection = ConnectionRelay;
JoinConnection = ConnectionRelay;
JoinCodeInput = "";
HostPort = "25001";
JoinPort = "25001";
HostPassword = "";
JoinPassword = "";
LanOnly = false;
RequireJoinApproval = true;
SimulationSync = true;
MaxPlayers = "8";
}
}
}