From 65a007e5097985210acc3b9b3b0c35f809b6d2d8 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 14 Sep 2026 22:09:37 +0200 Subject: [PATCH] Seed zones from a stable name hash instead of string.GetHashCode (Cut 10) Closes the 8a follow-up. string.GetHashCode is stable on Mono but randomized per process on .NET Core, so a headless sim that generates zones would get different zones on every run. - Extensions.StableHash folds a string's UTF-8 bytes through CultMath's pcg from the FNV-1a offset basis. No new CultMath API. - ZoneGenerator seeds from `Name.StableHash() ^ pcg3d(Position).x`. - Zone seeds from `Name.StableHash() ?? 1337u`. CultMath's Random maps seed 0 to its default, and the abs/Convert.ToUInt32 pair that threw on int.MinValue is gone. Old runs are discarded, so a different galaxy per seed costs nothing. Tests: StableHashIsNotProcessRandomized pins "Adrasteia" to 2427892944, the value an independent Add-Type transcription of CultMath's pcg produced. It fails with GetHashCode substituted back in. Builds: headless, 15 tests pass, Unity batchmode compile reports no error CS. Co-Authored-By: Claude Opus 5 --- Assets/Scripts/ServerShared/Extensions.cs | 12 +++++++++++- Assets/Scripts/ServerShared/Zone.cs | 2 +- Assets/Scripts/ServerShared/ZoneGenerator.cs | 2 +- tests/Aetheria.Shared.Tests/RunSaveTests.cs | 5 +++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/ServerShared/Extensions.cs b/Assets/Scripts/ServerShared/Extensions.cs index e264af2c2..656e85ba2 100644 --- a/Assets/Scripts/ServerShared/Extensions.cs +++ b/Assets/Scripts/ServerShared/Extensions.cs @@ -1,10 +1,11 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Numerics; using System.Reflection; +using System.Text; using System.Text.RegularExpressions; using MessagePack; using CultMath; @@ -20,6 +21,15 @@ public static class Extensions return baseType.GetInterfaces().Any(interfaceType.Equals); } + // A string hash that is the same in every process and runtime, for seeding: string.GetHashCode is randomized per + // process on .NET Core. Folds the UTF-8 bytes through CultMath's PCG hash from the FNV-1a offset basis. + public static uint StableHash(this string s) + { + uint h = 0x811C9DC5; + foreach (var b in Encoding.UTF8.GetBytes(s)) h = pcg(h ^ b); + return h; + } + // Every loadable type assignable to this one. An assembly whose dependencies are missing (a Unity plugin // referencing UnityEngine outside Unity) still yields the types that do load. private static readonly Dictionary ChildClasses = new Dictionary(); diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 740529a1c..edfd043bf 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -51,7 +51,7 @@ public class Zone Pack = pack; _itemManager = itemManager; Settings = settings; - _random = new Random(Convert.ToUInt32(abs(galaxyZone?.Name.GetHashCode() ?? 1337))); + _random = new Random(galaxyZone?.Name.StableHash() ?? 1337u); var cache = itemManager.ItemData; foreach (var orbit in pack.Orbits) diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 533fe2ebb..d182c9e19 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -45,7 +45,7 @@ public static class ZoneGenerator var pack = new ZonePack(); var cache = itemManager.ItemData; - var random = new Random(unchecked((uint) galaxyZone.Name.GetHashCode()) ^ (uint) pcg3d(galaxyZone.Position).x); + var random = new Random(galaxyZone.Name.StableHash() ^ (uint) pcg3d(galaxyZone.Position).x); var density = saturate(galaxy.Background.CloudDensity(galaxyZone.Position)/2); pack.Radius = zoneSettings.ZoneRadius.Evaluate(density); diff --git a/tests/Aetheria.Shared.Tests/RunSaveTests.cs b/tests/Aetheria.Shared.Tests/RunSaveTests.cs index f80a98d74..1f8a7fa7b 100644 --- a/tests/Aetheria.Shared.Tests/RunSaveTests.cs +++ b/tests/Aetheria.Shared.Tests/RunSaveTests.cs @@ -125,6 +125,11 @@ public sealed class RunSaveTests : IDisposable Assert.Equal(before, new[] { Catalog, Player }.Select(Hash).ToArray()); } + // Zone seeds hash names with this; string.GetHashCode differs per process on .NET Core. The literal was computed + // once, from an independent transcription of CultMath's pcg over the UTF-8 bytes. + [Fact] + public void StableHashIsNotProcessRandomized() => Assert.Equal(2427892944u, "Adrasteia".StableHash()); + // As in AetherDb's loadout command: the settings an ItemManager needs to build units, and nothing authored. internal static GameplaySettings TestSettings() => new GameplaySettings {