diff --git a/PugSharp.Match/Match.cs b/PugSharp.Match/Match.cs index 9d24e3e..0d6c12a 100644 --- a/PugSharp.Match/Match.cs +++ b/PugSharp.Match/Match.cs @@ -113,7 +113,7 @@ private void InitializeStateMachine() { _MatchStateMachine.Configure(MatchState.None) .PermitDynamicIf(MatchCommand.LoadMatch, () => HasRestoredMatch() ? MatchState.RestoreMatch : MatchState.WaitingForPlayersConnectedReady); - + _MatchStateMachine.Configure(MatchState.WaitingForPlayersConnectedReady) .PermitDynamicIf(MatchCommand.PlayerReady, () => HasRestoredMatch() ? MatchState.MatchRunning : MatchState.DefineTeams, AllPlayersAreReady) .OnEntry(StartWarmup) diff --git a/PugSharp.Server.Contract/ICsServer.cs b/PugSharp.Server.Contract/ICsServer.cs index a92f566..d5c6c80 100644 --- a/PugSharp.Server.Contract/ICsServer.cs +++ b/PugSharp.Server.Contract/ICsServer.cs @@ -26,4 +26,5 @@ public interface ICsServer void SwitchMap(string selectedMap); void UnpauseMatch(); void UpdateConvar(string name, T value); + Task InitializeWorkshopMapLookupAsync(); } diff --git a/PugSharp/Application.cs b/PugSharp/Application.cs index 35bcf69..79c9964 100644 --- a/PugSharp/Application.cs +++ b/PugSharp/Application.cs @@ -85,6 +85,8 @@ public void Initialize(bool hotReload) { var serverConfigResult = _ConfigProvider.LoadServerConfig(); + _CsServer.InitializeWorkshopMapLookupAsync() + serverConfigResult.Switch( error => { }, // Do nothing - Error already logged serverConfig => diff --git a/PugSharp/CsServer.cs b/PugSharp/CsServer.cs index e66999f..1f6e652 100644 --- a/PugSharp/CsServer.cs +++ b/PugSharp/CsServer.cs @@ -9,6 +9,9 @@ using PugSharp.Models; using PugSharp.Server.Contract; +using System.IO; +using System.Text.Json; + namespace PugSharp; public class CsServer : ICsServer @@ -16,10 +19,15 @@ public class CsServer : ICsServer private readonly ILogger _Logger; private readonly ICssDispatcher _Dispatcher; + private Dictionary? _WorkshopMapLookup; + private readonly string _WorkshopMapConfigPath; + public CsServer(ILogger logger, ICssDispatcher dispatcher) { _Logger = logger; _Dispatcher = dispatcher; + + _WorkshopMapConfigPath = Path.Combine(GameDirectory, "csgo", "PugSharp", "Config", "workshop_maps.json"); } public string GameDirectory => CounterStrikeSharp.API.Server.GameDirectory; @@ -206,18 +214,67 @@ public void UnpauseMatch() ExecuteCommand("mp_unpause_match"); } + public async Task InitializeWorkshopMapLookupAsync() + { + if (_WorkshopMapLookup != null) + { + return; // Already loaded + } + try + { + if (File.Exists(_WorkshopMapConfigPath)) + { + string json = await File.ReadAllTextAsync(_WorkshopMapConfigPath); + _WorkshopMapLookup = JsonSerializer.Deserialize>(json, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }) ?? new Dictionary(StringComparer.OrdinalIgnoreCase); + + _Logger.LogInformation("Loaded workshop map config from {Path}", _WorkshopMapConfigPath); + } + else + { + _Logger.LogWarning("Workshop map config not found at {Path}. Workshop maps will not be available.", _WorkshopMapConfigPath); + _WorkshopMapLookup = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + catch (Exception ex) + { + _Logger.LogError(ex, "Failed to load workshop map config from {Path}", _WorkshopMapConfigPath); + _WorkshopMapLookup = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + public void SwitchMap(string selectedMap) { _Dispatcher.NextWorldUpdate(() => { - if (!IsMapValid(selectedMap)) + if (string.IsNullOrWhiteSpace(selectedMap)) { - _Logger.LogInformation("The selected map is not valid: \"{SelectedMap}\"!", selectedMap); + _Logger.LogInformation("The selected map is null or empty!"); return; } - _Logger.LogInformation("Switch map to: \"{SelectedMap}\"!", selectedMap); - ExecuteCommand($"changelevel {selectedMap}"); + string command; + + if (_WorkshopMapLookup != null && _WorkshopMapLookup.TryGetValue(selectedMap, out var workshopId)) + { + _Logger.LogInformation("Translating map \"{SelectedMap}\" to Workshop ID: {WorkshopId}", selectedMap, workshopId); + command = $"host_workshop_map {workshopId}"; + } + else + { + if (!IsMapValid(selectedMap)) + { + _Logger.LogInformation("The selected map is not valid: \"{SelectedMap}\"!", selectedMap); + return; + } + + _Logger.LogInformation("Switching to standard map: \"{SelectedMap}\"", selectedMap); + command = $"changelevel {selectedMap}"; + } + + ExecuteCommand(command); }); }