Skip to content

Commit 0167b87

Browse files
committed
Add settings update during runtime
1 parent bc76ebf commit 0167b87

File tree

11 files changed

+227
-67
lines changed

11 files changed

+227
-67
lines changed

SWBF2Admin/Gameserver/ServerManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ public void Start()
143143
Logger.Log(LogLevel.Info, "Launching server with args '{0}'", ServerArgs);
144144
status = ServerStatus.Starting;
145145

146-
Environment.SetEnvironmentVariable("SPAWN_TIMER", "" + Util.I2f(Core.Server.Settings.AutoAnnouncePeriod));
146+
//TODO
147+
//Environment.SetEnvironmentVariable("SPAWN_TIMER", "" + Util.I2f(Core.Server.Settings.AutoAnnouncePeriod));
147148

148-
ProcessStartInfo startInfo = new ProcessStartInfo(Core.Files.ParseFileName(ServerPath + "/BattlefrontII.exe"), ServerArgs);
149-
startInfo.WorkingDirectory = Core.Files.ParseFileName(ServerPath);
149+
ProcessStartInfo startInfo = new ProcessStartInfo(Core.Files.ParseFileName(ServerPath + "/BattlefrontII.exe"), ServerArgs)
150+
{
151+
WorkingDirectory = Core.Files.ParseFileName(ServerPath)
152+
};
150153

151154

152155
//if we're in steam mode, steam will start at launcher exe prior to the actual game
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of SWBF2Admin (https://github.com/jweigelt/swbf2admin).
3+
* Copyright(C) 2017, 2018 Jan Weigelt <[email protected]>
4+
*
5+
* SWBF2Admin is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
10+
* SWBF2Admin is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License
16+
* along with SWBF2Admin. If not, see<http://www.gnu.org/licenses/>.
17+
*/
18+
using System;
19+
using System.Collections.Generic;
20+
21+
namespace SWBF2Admin.Runtime.Rcon.Packets
22+
{
23+
class MapListPacket : RconPacket
24+
{
25+
public List<string> MapList { get; set; }
26+
27+
public MapListPacket() : base("maps") { }
28+
29+
public override void HandleResponse(string response)
30+
{
31+
MapList = new List<string>();
32+
if (response.Length == 0)
33+
{
34+
//no maps
35+
PacketOk = true;
36+
return;
37+
}
38+
MapList.AddRange(response.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries));
39+
PacketOk = true;
40+
}
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of SWBF2Admin (https://github.com/jweigelt/swbf2admin).
3+
* Copyright(C) 2017, 2018 Jan Weigelt <[email protected]>
4+
*
5+
* SWBF2Admin is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
10+
* SWBF2Admin is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13+
* GNU General Public License for more details.
14+
15+
* You should have received a copy of the GNU General Public License
16+
* along with SWBF2Admin. If not, see<http://www.gnu.org/licenses/>.
17+
*/
18+
using SWBF2Admin.Utility;
19+
using SWBF2Admin.Structures;
20+
21+
namespace SWBF2Admin.Runtime.Rcon.Packets
22+
{
23+
class UpdateSettingPacket : RconPacket
24+
{
25+
public UpdateSettingPacket(ServerSettings.ServerSetting setting) : base(setting.RconCommand) { }
26+
27+
public override void HandleResponse(string response)
28+
{
29+
Logger.Log(LogLevel.Verbose, "Setting '{0}': '{1}'", Command, response);
30+
PacketOk = (response != "command not found");
31+
}
32+
}
33+
}

SWBF2Admin/Runtime/Rcon/RconClient.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using SWBF2Admin.Utility;
2626
using SWBF2Admin.Structures;
2727
using SWBF2Admin.Runtime.Rcon.Packets;
28+
using System.Collections.Generic;
2829

2930
namespace SWBF2Admin.Runtime.Rcon
3031
{
@@ -266,6 +267,48 @@ public void IngameLua(string lua)
266267
SendCommand("lua", lua);
267268
}
268269

270+
public bool UpdateServerSettings(List<ServerSettings.ServerSetting> settings)
271+
{
272+
Logger.Log(LogLevel.Verbose, "Updating {0} setting(s)", settings.Count.ToString());
273+
foreach (var setting in settings)
274+
{
275+
SendPacket(new UpdateSettingPacket(setting));
276+
}
277+
return false;
278+
}
279+
280+
public void UpdateMapList(List<string> newMapList)
281+
{
282+
var mp = new MapListPacket();
283+
SendPacket(mp);
284+
if (!mp.PacketOk)
285+
{
286+
Logger.Log(LogLevel.Error, "Map update failed - invalid /maps response");
287+
return;
288+
}
289+
290+
int added = 0;
291+
int dropped = 0;
292+
foreach (var s in mp.MapList)
293+
{
294+
if (!newMapList.Contains(s))
295+
{
296+
SendCommand("dropmap", s);
297+
dropped++;
298+
}
299+
}
300+
301+
foreach (var s in newMapList)
302+
{
303+
if (!mp.MapList.Contains(s))
304+
{
305+
SendCommand("addmap", s);
306+
added++;
307+
}
308+
}
309+
Logger.Log(LogLevel.Verbose, "Added {0} maps, dropped {1} maps", added.ToString(), dropped.ToString());
310+
}
311+
269312
/// <summary>
270313
/// Sends a command to the server but does not retrieve the response
271314
/// <note>
@@ -349,7 +392,7 @@ private void WorkThread_Run()
349392

350393
bytesRead = 0;
351394
}
352-
// Logger.Log(LogLevel.Verbose, "Read rcon message: {0} bytes", message.Length.ToString());
395+
// Logger.Log(LogLevel.Verbose, "Read rcon message: {0} bytes", message.Length.ToString());
353396
ProcessMessage(message);
354397

355398
bytesRead = 0;

SWBF2Admin/SWBF2Admin.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@
177177
<Compile Include="Runtime\Players\PlayerHandlerConfiguration.cs" />
178178
<Compile Include="Runtime\Players\PlayerHandler.cs" />
179179
<Compile Include="ComponentBase.cs" />
180+
<Compile Include="Runtime\Rcon\Packets\MapListPacket.cs" />
180181
<Compile Include="Runtime\Rcon\Packets\PlayerListPacket.cs" />
182+
<Compile Include="Runtime\Rcon\Packets\UpdateSettingPacket.cs" />
181183
<Compile Include="Runtime\Rcon\Packets\WebChatPacket.cs" />
182184
<Compile Include="Runtime\Rcon\Packets\StatusPacket.cs" />
183185
<Compile Include="Runtime\Rcon\RconChatEventArgs.cs" />

SWBF2Admin/Structures/Attributes/ConfigSection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@ class ConfigSection : Attribute
2828
public const int MAPS = 1 << 3;
2929

3030
public int Type { get; set; }
31-
private bool canUpdate;
32-
private bool needsReload;
31+
public virtual bool CanUpdate { get; }
32+
public virtual bool NeedsReload { get; }
3333
public bool YesNoBool { get; } //used for swbf's /command /nocommand parameters
3434

3535
public ConfigSection(int type, bool canUpdate, bool needsReload)
3636
{
3737
Type = type;
38-
this.canUpdate = canUpdate;
39-
this.needsReload = needsReload;
38+
CanUpdate = canUpdate;
39+
NeedsReload = needsReload;
4040
YesNoBool = false;
4141
}
4242

4343
public ConfigSection(int type)
4444
{
4545
Type = type;
46-
canUpdate = needsReload = YesNoBool = false;
46+
CanUpdate = NeedsReload = YesNoBool = false;
4747
}
4848

4949
public ConfigSection(int type, bool yesNoBool)
5050
{
5151
Type = type;
52-
canUpdate = needsReload = false;
52+
CanUpdate = NeedsReload = false;
5353
YesNoBool = yesNoBool;
5454
}
5555
}

SWBF2Admin/Structures/ServerMap.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public static List<string> ReadMapRotation(AdminCore core)
106106
}
107107
return res;
108108
}
109+
109110
public static void SaveMapRotation(AdminCore core, List<string> maps)
110111
{
111112
string sr = string.Empty;
@@ -160,8 +161,10 @@ public static List<ServerMap> ReadServerMapConfig(AdminCore core)
160161
}
161162
if (m == null)
162163
{
163-
m = new ServerMap();
164-
m.Name = name;
164+
m = new ServerMap
165+
{
166+
Name = name
167+
};
165168
maps.Add(m);
166169
}
167170

0 commit comments

Comments
 (0)