-
Notifications
You must be signed in to change notification settings - Fork 3
/
WorkshopDefaultMap.cs
96 lines (74 loc) · 2.87 KB
/
WorkshopDefaultMap.cs
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
namespace WorkshopDefaultMap;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Timers;
using Microsoft.Extensions.Logging;
using System.Text.Json.Serialization;
public class WorkshopDefaultMapConfig : BasePluginConfig
{
public override int Version { get; set; } = 1;
[JsonPropertyName("Map")]
public string Map { get; set; } = "Awp_arena_vlgbeta_gg2";
}
public class WorkshopDefaultMap : BasePlugin, IPluginConfig<WorkshopDefaultMapConfig>
{
public override string ModuleName => "Workshop Collection Default Map";
public override string ModuleVersion => "0.4";
public override string ModuleAuthor => "Cruze";
public override string ModuleDescription => "Sets default map after server restart";
private bool g_bServerStarted = true;
private ulong g_uOldMapId;
private Timer? g_TimerForceReset = null;
private Timer? g_TimerChangeMap = null;
public required WorkshopDefaultMapConfig Config { get; set; } = new();
public void OnConfigParsed(WorkshopDefaultMapConfig config)
{
Config = config;
if(string.IsNullOrEmpty(Config.Map))
Logger.LogError("Map specified in config is blank. Plugin will not work as intended.");
}
public override void Load(bool hotReload)
{
base.Load(hotReload);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
if(hotReload)
{
Logger.LogInformation($"Plugin needs server restart to work.");
}
}
private void OnMapStart(string mapName)
{
if(string.IsNullOrEmpty(Config.Map)) return;
if(g_bServerStarted || g_TimerForceReset != null)
{
g_bServerStarted = false;
g_TimerForceReset?.Kill();
g_TimerForceReset = AddTimer(10.0f, ResetTimer);
g_TimerChangeMap?.Kill();
g_TimerChangeMap = AddTimer(1.0f, ChangeMap);
Logger.LogInformation($"Changing map to {Config.Map}...");
}
}
private void ChangeMap()
{
g_TimerChangeMap = null;
if(string.IsNullOrEmpty(Config.Map)) return;
if(Server.MapName.Equals(Config.Map, StringComparison.OrdinalIgnoreCase)) return;
if(!ulong.TryParse(Config.Map, out ulong mapid))
{
Server.ExecuteCommand($"ds_workshop_changelevel {Config.Map}");
Logger.LogInformation($"Changed map to {Config.Map}.");
return;
}
if(g_uOldMapId == mapid) return; // Hacky fix till there is a way to find workshop id of a map.
Server.ExecuteCommand($"host_workshop_map {mapid}");
Logger.LogInformation($"Changed map to mapid {mapid}.");
g_uOldMapId = mapid;
}
private void ResetTimer()
{
g_TimerForceReset = null;
g_TimerChangeMap?.Kill();
g_TimerChangeMap = null;
}
}