Skip to content

Commit 67bc6cf

Browse files
authored
Fix issue when adding bans with reason that has line endings (#133)
* Update message to look for to start RCON on windows * When adding bans replace line endings. - For username remove line endings. - For reason replace line endings with space.
1 parent 72a72ca commit 67bc6cf

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

FactorioWebInterface/Services/FactorioBanService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.IO;
1010
using System.Linq;
11+
using System.Text.RegularExpressions;
1112
using System.Threading.Tasks;
1213

1314
namespace FactorioWebInterface.Services
@@ -26,8 +27,11 @@ public interface IFactorioBanService
2627
Task<Result> BuildBanList(string serverBanListPath);
2728
}
2829

29-
public class FactorioBanService : IFactorioBanService
30+
public partial class FactorioBanService : IFactorioBanService
3031
{
32+
[GeneratedRegex("(\n|\r)+")]
33+
private static partial Regex LineEndingRegex();
34+
3135
private static readonly JsonSerializerSettings banListSerializerSettings = new JsonSerializerSettings()
3236
{
3337
Formatting = Formatting.Indented,
@@ -124,6 +128,8 @@ public async Task DoBanFromGameOutput(FactorioServerData serverData, string cont
124128
public async Task<bool> AddBan(Ban ban, string serverId, bool synchronizeWithServers, string? actor)
125129
{
126130
ban.Username = ban.Username.ToLowerInvariant();
131+
ban.Username = LineEndingRegex().Replace(ban.Username, "");
132+
ban.Reason = LineEndingRegex().Replace(ban.Reason ?? "", " ");
127133

128134
bool added = await AddBanToDatabase(ban);
129135
if (added)

FactorioWebInterfaceTests/Services/FactorioBanServiceTests/AddBan.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,54 @@ void Callback(LogLevel l, object state)
119119
Assert.Equal(expected, message);
120120
}
121121

122+
[Theory]
123+
[InlineData("name\n", "reason", "name", "reason")]
124+
[InlineData("name\n\r", "reason", "name", "reason")]
125+
[InlineData("name\r", "reason", "name", "reason")]
126+
[InlineData("name\r\n", "reason", "name", "reason")]
127+
[InlineData("name\n\n", "reason", "name", "reason")]
128+
[InlineData("name", "reason", "name", "reason")]
129+
[InlineData("name", "reason\n", "name", "reason ")]
130+
[InlineData("name", "reason\n\r", "name", "reason ")]
131+
[InlineData("name", "reason\r", "name", "reason ")]
132+
[InlineData("name", "reason\r\n", "name", "reason ")]
133+
[InlineData("name", "reason\n\n", "name", "reason ")]
134+
[InlineData("name", "reason\nline2", "name", "reason line2")]
135+
[InlineData("name", "reason\nline2\nline3", "name", "reason line2 line3")]
136+
public async Task ReplacesLineEndingsInUsernameAndReasonWithSpace(string username, string reason, string expectedName, string expectedReason)
137+
{
138+
// Arrange.
139+
var ban = new Ban() { Username = username, Admin = "admin", Reason = reason };
140+
const string serverId = "serverId";
141+
const bool sync = true;
142+
143+
var eventRaised = new AsyncManualResetEvent();
144+
FactorioBanEventArgs? eventArgs = null;
145+
void FactorioBanService_BanChanged(IFactorioBanService sender, FactorioBanEventArgs ev)
146+
{
147+
eventArgs = ev;
148+
eventRaised.Set();
149+
}
150+
151+
factorioBanService.BanChanged += FactorioBanService_BanChanged;
152+
153+
// Act.
154+
await factorioBanService.AddBan(ban, serverId, sync, "");
155+
await eventRaised.WaitAsyncWithTimeout(5000);
156+
157+
// Assert.
158+
Assert.NotNull(eventArgs);
159+
Assert.Equal(serverId, eventArgs!.Source);
160+
Assert.Equal(sync, eventArgs.SynchronizeWithServers);
161+
162+
var changeData = eventArgs.ChangeData;
163+
164+
Assert.Equal(CollectionChangeType.Add, changeData.Type);
165+
Assert.Single(changeData.NewItems);
166+
Assert.Equal(expectedName, changeData.NewItems[0].Username);
167+
Assert.Equal(expectedReason, changeData.NewItems[0].Reason);
168+
}
169+
122170
[Fact]
123171
public async Task DoesNotAddDuplicateBan()
124172
{

FactorioWrapper/MainLoop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private async void FactorioProcess_OutputDataReceived(object sender, DataReceive
450450

451451
string line = match.Groups[1].Value;
452452
#if WINDOWS
453-
if (line == "Info UDPSocket.cpp:45: Opening socket for broadcast")
453+
if (line == "Info UDPSocket.cpp:44: Opening socket for broadcast")
454454
{
455455
await ConnectRCON();
456456

0 commit comments

Comments
 (0)