Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Unity: UrlFlags ignores flags after the fragment (Fixes #2215) (#2262)

Beri24240aae24cf
diff --git a/changelog.d/2215-urlflags-fragment.md b/changelog.d/2215-urlflags-fragment.md
new file mode 100644
index 000000000..0fff385d8
--- /dev/null
+++ b/changelog.d/2215-urlflags-fragment.md
@@ -0,0 +1,3 @@
+### Fixed
+
+- A debug or quality flag in the part of the page URL after the `#` no longer switches on; only the query counts (Fixes #2215).
diff --git a/unity/Assets/Scripts/Boot/UrlFlags.cs b/unity/Assets/Scripts/Boot/UrlFlags.cs
index c31e21079..24b10577d 100644
--- a/unity/Assets/Scripts/Boot/UrlFlags.cs
+++ b/unity/Assets/Scripts/Boot/UrlFlags.cs
@@ -5,16 +5,24 @@ using System.Text.RegularExpressions;
namespace Statecraft.Game.Boot
{
/// <summary>Flags in the WebGL page URL's query: "?golden", "&amp;smoke=5", "?profile", "?camlog". A flag is a whole query
- /// parameter: it starts at '?' or '&amp;' and ends at '=', '&amp;', '#' or the end of the URL (the rule GameRoot's smoke
- /// flag uses), so neither a path such as ".../golden-project/..." nor "?profile-x" or "?golden.y" turns one on.</summary>
+ /// parameter: it starts at '?' or '&amp;' and ends at '=', '&amp;' or the end of the query (the rule GameRoot's smoke
+ /// flag uses), so neither a path such as ".../golden-project/..." nor "?profile-x" or "?golden.y" turns one on. Only the
+ /// part before the first '#' is read: a flag in the fragment is not a query parameter.</summary>
public static class UrlFlags
{
- public static bool Has(string? url, string name) => Regex.IsMatch(url ?? "", @"[?&]" + Regex.Escape(name) + @"(?=[=&#]|$)");
+ public static bool Has(string? url, string name) => Regex.IsMatch(BeforeFragment(url), @"[?&]" + Regex.Escape(name) + @"(?=[=&]|$)");
+
+ static string BeforeFragment(string? url)
+ {
+ url ??= "";
+ var hash = url.IndexOf('#');
+ return hash < 0 ? url : url.Substring(0, hash);
+ }
/// <summary>The flag's integer value ("smoke=5" -> 5), or 0 when absent.</summary>
public static int Int(string? url, string name)
{
- var m = Regex.Match(url ?? "", @"[?&]" + Regex.Escape(name) + @"=(\d+)");
+ var m = Regex.Match(BeforeFragment(url), @"[?&]" + Regex.Escape(name) + @"=(\d+)");
return m.Success && int.TryParse(m.Groups[1].Value, NumberStyles.None, CultureInfo.InvariantCulture, out var v) ? v : 0;
}
}
diff --git a/unity/Assets/Tests/EditMode/GoldenCheckTests.cs b/unity/Assets/Tests/EditMode/GoldenCheckTests.cs
index a37146393..758b15434 100644
--- a/unity/Assets/Tests/EditMode/GoldenCheckTests.cs
+++ b/unity/Assets/Tests/EditMode/GoldenCheckTests.cs
@@ -61,6 +61,9 @@ public class GoldenCheckTests
[TestCase("http://localhost:8000/index.html?golden-x&smoke=5", false)]
[TestCase("http://localhost:8000/index.html?golden#top", true)]
[TestCase("http://localhost:8000/index.html?golden=1", true)]
+ [TestCase("http://x/index.html#?golden", false)] // #2215: the fragment is not the query
+ [TestCase("http://x/index.html?a=1#&golden", false)]
+ [TestCase("http://x/index.html#&golden", false)]
public void TheGoldenFlagIsAQueryParameterNotAPathWord(string url, bool expected) =>
Assert.AreEqual(expected, UrlFlags.Has(url, "golden"));
@@ -69,6 +72,8 @@ public class GoldenCheckTests
{
Assert.AreEqual(5, UrlFlags.Int("http://x/index.html?golden&smoke=5", "smoke"));
Assert.AreEqual(0, UrlFlags.Int("http://x/smoke=5/index.html", "smoke"));
+ Assert.AreEqual(0, UrlFlags.Int("http://x/index.html#&smoke=5", "smoke"));
+ Assert.AreEqual(5, UrlFlags.Int("http://x/index.html?smoke=5#&smoke=7", "smoke"));
}
[Test]