diff --git a/Assets/Scripts/ServerShared/AetheriaStores.cs b/Assets/Scripts/ServerShared/AetheriaStores.cs index e5c03a1e8..c16a2181a 100644 --- a/Assets/Scripts/ServerShared/AetheriaStores.cs +++ b/Assets/Scripts/ServerShared/AetheriaStores.cs @@ -11,8 +11,8 @@ public static class AetheriaStores public static readonly Type[] PlayerTypes = { typeof(PlayerSettings) }; // Attaches (hydrates) the catalog read-only unless catalogWritable, then the run and player stores when given. - // A read-only catalog throws when a [CultGlobal] type routed to it has no record. A writable catalog is being - // authored (the importer starts from an empty file), so it is not held to that. + // Throws when a [CultGlobal] type routed to the catalog has no record. The one exemption is a writable catalog with + // no records at all, a seed store being authored from nothing; a populated catalog is held to it however it opens. public static CultCache Open(string catalogPath, string runPath = null, string playerPath = null, bool catalogWritable = false) { var cache = new CultCache(); @@ -21,7 +21,9 @@ public static class AetheriaStores cache.AddBackingStore(new SingleFileMessagePackBackingStore(catalogPath, !catalogWritable), CatalogTypes); if (runPath != null) cache.AddBackingStore(new SingleFileMessagePackBackingStore(runPath), RunTypes); if (playerPath != null) cache.AddBackingStore(new SingleFileMessagePackBackingStore(playerPath), PlayerTypes); - var missing = catalogWritable ? null : cache.Registry.AllDescriptors.FirstOrDefault(descriptor => + var seeding = catalogWritable && !cache.AllStoredDocuments.Any(stored => + CatalogTypes.Any(home => home.IsAssignableFrom(stored.Descriptor.DocumentType))); + var missing = seeding ? null : cache.Registry.AllDescriptors.FirstOrDefault(descriptor => descriptor.IsGlobal && CatalogTypes.Any(home => home.IsAssignableFrom(descriptor.DocumentType)) && cache.AllStoredDocuments.All(stored => stored.Descriptor != descriptor)); diff --git a/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs b/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs index 023d04877..900ee3a21 100644 --- a/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs +++ b/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs @@ -121,6 +121,21 @@ public sealed class AetheriaStoresTests : IDisposable using (AetheriaStores.Open(Catalog)) { } } + // Writability exempts only an empty seed catalog: a populated catalog missing a global is loud even when writable. + [Fact] + public void WritablePopulatedCatalogMissingGlobalIsLoud() + { + using (var cache = AetheriaStores.Open(Catalog, catalogWritable: true)) + { + var global = cache.GetGlobal(); + var key = cache.RefOf(global).Key; + Assert.True(cache.Commit(batch => batch.Remove(key))); + } + + var error = Assert.Throws(() => AetheriaStores.Open(Catalog, catalogWritable: true)); + Assert.Contains("aetheria.tests.catalogglobal", error.Message); + } + private static string Hash(string path) => Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))); private static string[] SchemaNames(string path)