Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions PugSharp.Match/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ private void InitializeStateMachine()
#pragma warning restore MA0051 // Method is too long
{
_MatchStateMachine.Configure(MatchState.None)
.PermitDynamicIf(MatchCommand.LoadMatch, () => HasRestoredMatch() ? MatchState.RestoreMatch : MatchState.WaitingForPlayersConnectedReady);

.PermitDynamicIf(MatchCommand.LoadMatch, () => HasRestoredMatch() ? MatchState.RestoreMatch : MatchState.WaitingForPlayersConnectedReady)
.OnEntryAsync(() => _CsServer.InitializeWorkshopMapLookupAsync());

_MatchStateMachine.Configure(MatchState.WaitingForPlayersConnectedReady)
.PermitDynamicIf(MatchCommand.PlayerReady, () => HasRestoredMatch() ? MatchState.MatchRunning : MatchState.DefineTeams, AllPlayersAreReady)
.OnEntry(StartWarmup)
Expand Down
1 change: 1 addition & 0 deletions PugSharp.Server.Contract/ICsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public interface ICsServer
void SwitchMap(string selectedMap);
void UnpauseMatch();
void UpdateConvar<T>(string name, T value);
Task InitializeWorkshopMapLookupAsync();
}
64 changes: 60 additions & 4 deletions PugSharp/CsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@
using PugSharp.Models;
using PugSharp.Server.Contract;

using System.IO;
using System.Text.Json;

namespace PugSharp;

public class CsServer : ICsServer
{
private readonly ILogger<CsServer> _Logger;
private readonly ICssDispatcher _Dispatcher;

private Dictionary<string, string>? _WorkshopMapLookup;
private readonly string _WorkshopMapConfigPath;

public CsServer(ILogger<CsServer> logger, ICssDispatcher dispatcher)
{
_Logger = logger;
_Dispatcher = dispatcher;

_WorkshopMapConfigPath = Path.Combine(GameDirectory, "csgo", "PugSharp", "Config", "workshop_maps.json");
}

public string GameDirectory => CounterStrikeSharp.API.Server.GameDirectory;
Expand Down Expand Up @@ -206,18 +214,66 @@ public void UnpauseMatch()
ExecuteCommand("mp_unpause_match");
}

public async Task InitializeWorkshopMapLookupAsync()
{
if (_WorkshopMapLookup != null)
return; // Already loaded
Comment thread
gubsy420 marked this conversation as resolved.

try
{
if (File.Exists(_WorkshopMapConfigPath))
{
string json = await File.ReadAllTextAsync(_WorkshopMapConfigPath);
_WorkshopMapLookup = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
}) ?? new Dictionary<string, string>(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<string, string>(StringComparer.OrdinalIgnoreCase);
}
}
catch (Exception ex)
{
_Logger.LogError(ex, "Failed to load workshop map config from {Path}", _WorkshopMapConfigPath);
_WorkshopMapLookup = new Dictionary<string, string>(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);
});
}

Expand Down