Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions Zero-K.info/Controllers/ModsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Web.Mvc;
using ZkData;

Expand Down Expand Up @@ -78,6 +79,13 @@ public ActionResult EditSubmit(GameMode newGameMode)
ViewBag.Error = "Shorten the name please";
return View("GameModeEdit", newGameMode);
}

var jsonError = GetGameModeJsonError(newGameMode.GameModeJson);
if (jsonError != null)
{
ViewBag.Error = jsonError;
return View("GameModeEdit", newGameMode);
}


if (newGameMode.GameModeID == 0) // create new game mode
Expand Down Expand Up @@ -128,6 +136,28 @@ public ActionResult EditSubmit(GameMode newGameMode)
}
}

// Lobby clients skip mode files that their JSON decoder cannot read, so only standard JSON is saved.
private static string GetGameModeJsonError(string gameModeJson)
{
try
{
using (var doc = JsonDocument.Parse(gameModeJson))
{
JsonElement name;
if (doc.RootElement.ValueKind != JsonValueKind.Object || !doc.RootElement.TryGetProperty("name", out name) || name.ValueKind != JsonValueKind.String)
{
return "Game mode JSON must be an object with a \"name\" text field";
}
}
return null;
}
catch (JsonException ex)
{
var reason = ex.Message.Split(new[] { " LineNumber:" }, StringSplitOptions.None)[0];
return $"Game mode JSON is not valid at line {ex.LineNumber + 1}, position {ex.BytePositionInLine + 1}: {reason}";
}
}

public ActionResult Detail(int id)
{
var db = new ZkDataContext();
Expand Down
1 change: 1 addition & 0 deletions Zero-K.info/asp.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
<PackageReference Include="Validation" Version="2.0.2.13022" />
<PackageReference Include="WebGrease" Version="1.6.0" />
</ItemGroup>
Expand Down