diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index fcd554efd..62e8632a1 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -257,8 +257,11 @@ public class LoadoutGenerator // Whether a product is on the market at all (ruling off-market-is-unoffered-product). The one reader of NotOffered. A bin is // offered only if both it and its line are (ruling bin-inherits-line-offered); the catalog read guarantees a bin's line exists. - public static bool IsOffered(CultCache items, FactionProductData product) => - !product.NotOffered && (product.Bin == null || !items.Get(product.Bin.Line).NotOffered); + public static bool IsOffered(CultCache items, FactionProductData product) => IsOffered(product, line => items.Get(line)); + + // The same reader for a caller whose bin lines are not all in one catalog yet (ShipModCatalog.MarketFault, over a package's own products). + public static bool IsOffered(FactionProductData product, Func, FactionProductData> lineOf) => + !product.NotOffered && (product.Bin == null || !lineOf(product.Bin.Line).NotOffered); // How much this outfitter favors a product: its maker's preference times the product's share of it. The one weight of // generation and of ChooseProduct. A bin takes the share of its maker's stock that would pass its floor and its line takes diff --git a/Assets/Scripts/ServerShared/ShipModCatalog.cs b/Assets/Scripts/ServerShared/ShipModCatalog.cs index d085974b4..0e5bd801c 100644 --- a/Assets/Scripts/ServerShared/ShipModCatalog.cs +++ b/Assets/Scripts/ServerShared/ShipModCatalog.cs @@ -110,7 +110,7 @@ public static class ShipModCatalog if (shipIds.Contains(ship.Id)) Exclude(directory, $"ship ID collides with an existing catalog ship authoring record."); if (StationKinds.HullKindFault(cache, hull) is { } kindFault) Exclude(directory, kindFault); - if (SchematicFault(cache, ship, package.Products) is { } schematicFault) Exclude(directory, schematicFault); + if (MarketFault(cache, ship, package.Products) is { } marketFault) Exclude(directory, marketFault); foreach (var product in package.Products) { if (!factionKeys.Contains(product.Manufacturer.Key)) @@ -158,16 +158,24 @@ public static class ShipModCatalog } } - // Why a package's hull cannot go on the market, or null: a hull offered through any of its products is drawn on the map and - // the schematic by its lines (ShipModVisual.BuildLineMesh builds nothing from none), so an offered hull without any is - // excluded. An unoffered stand-in is left alone. Offered is LoadoutGenerator.IsOffered, the one reader of NotOffered - // (ruling off-market-is-unoffered-product); `items` is the catalog the products' bins resolve in. A product whose bin - // line the catalog does not hold is not judged here: the catalog read refuses it. - public static string SchematicFault(CultCache items, ShipAuthoring ship, FactionProductData[] products) + // Why a package's products cannot join the catalog, or null; the one judgement Compose and `ship-authoring validate` share. + // `items` is the shipped catalog. A product's bin may name only a product line the shipped catalog holds or one of the + // package's own products (and that line must not be a bin itself): the catalog read refuses any other bin, and a line in + // another package is no promise, since packages compose independently. With every bin resolved, a hull offered through any of its + // products is drawn on the map and the schematic by its lines (ShipModVisual.BuildLineMesh builds nothing from none), so an + // offered hull without any is refused; an unoffered stand-in is left alone. Offered is LoadoutGenerator.IsOffered, the one + // reader of NotOffered (ruling off-market-is-unoffered-product), resolving a line among the package's own products too. + public static string MarketFault(CultCache items, ShipAuthoring ship, FactionProductData[] products) { + var own = new Dictionary(); + foreach (var product in products) own[ProductKey(ship.Id, product.Manufacturer.Key)] = product; + FactionProductData LineOf(CultRecordRef line) => items.Get(line) ?? (own.TryGetValue(line.Key, out var found) ? found : null); + foreach (var product in products.Where(product => product.Bin != null)) + if (LineOf(product.Bin.Line) is not { } line || line.Bin != null) + return $"{ship.Id}: product '{product.Name}' bins a line that is neither an unbinned shipped product nor an unbinned product of this package."; if (ship.SchematicLines != null && ship.SchematicLines.Any(line => line?.Points != null && line.Points.Length > 0)) return null; - var offered = products.Any(product => (product.Bin == null || items.Get(product.Bin.Line) != null) && LoadoutGenerator.IsOffered(items, product)); - return offered ? $"{ship.Id}: the hull is offered on the market but has no schematic lines." : null; + return products.Any(product => LoadoutGenerator.IsOffered(product, LineOf)) + ? $"{ship.Id}: the hull is offered on the market but has no schematic lines." : null; } public static Package ReadPackage(string shipPath) diff --git a/docs/mining-cut-refresh.md b/docs/mining-cut-refresh.md index b0bf89420..8a5fdcd9e 100644 --- a/docs/mining-cut-refresh.md +++ b/docs/mining-cut-refresh.md @@ -302,12 +302,12 @@ Every mining question is ruled. `ssh:` remote). Push from a `--shared --no-checkout` clone, and mount the LFS blobs. This is recorded so the next Hands does not rediscover it. - LFS staging set. `SubstanceCleanBreakTests` fails loudly on any LFS pointer it scans, so the job must restore - every LFS-tracked file under `Assets/`, `Packages/`, `ProjectSettings/` and `GameData/*.cc` that is not a + every LFS-tracked file under `Assets/`, `Packages/`, `ProjectSettings/`, `GameData/*.cc` and `tests/fixtures/mods/*.cc` that is not a binary-media extension (the test's `BinaryExtensions` denylist): 99 files at `83e6957de`, 96 `*.asset` and 3 `*.cc` (`GameData/Aetheria.cc`, `GameData/Mods/aetheria.nakovalnya/ship.cc`, `tests/fixtures/mods/aetheria.wren/ship.cc`), which includes `ProjectSettings/ProjectSettings.asset` and `Assets/Resources/Settings.asset`. Derive the list from the merged tree instead of hand-copying it: - `git ls-files Assets Packages ProjectSettings 'GameData/*.cc' | git check-attr --stdin filter | grep 'lfs$' | sed 's/: filter: lfs//' | grep -viE '\.(png|jpg|jpeg|tga|tif|tiff|psd|bmp|gif|exr|hdr|dds|wav|mp3|ogg|mp4|fbx|glb|blend|ttf|otf|dll|so|pdb|msgpack|zip|bank)$'`, + `git ls-files Assets Packages ProjectSettings 'GameData/*.cc' 'tests/fixtures/mods/*.cc' | git check-attr --stdin filter | grep 'lfs$' | sed 's/: filter: lfs//' | grep -viE '\.(png|jpg|jpeg|tga|tif|tiff|psd|bmp|gif|exr|hdr|dds|wav|mp3|ogg|mp4|fbx|glb|blend|ttf|otf|dll|so|pdb|msgpack|zip|bank)$'`, then copy each path from the `/lfs` mount over the checkout. That set (99: the 97 the sbsar guards read plus the two mod `ship.cc` files the Nakovalnya and Wren tests open) makes `SubstanceCleanBreakTests` green and nothing more: staging only two files turns it red. For a fully green suite also stage `GameData/Mods/*/ship.glb` and `tests/fixtures/mods/*/ship.glb` (the catalog excludes a package with an invalid GLB header, failing about 520 tests) and the LFS `.ttf`/`.otf` fonts `CreditsTests.FontCreditsShowWhatTheFontsThemselvesEmbed` reads. - Unity batchmode compile of the merged tree (Self, in a worktree, never with the operator's editor open on the main tree). This is the Cut 1 renderer compile the map still owes. diff --git a/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs b/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs index b4003954c..5a4f38093 100644 --- a/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs @@ -49,43 +49,71 @@ public sealed class ShipModCatalogTests : IDisposable AssertOnlyTheDerivedFile(); } - // A package selling one product through the one product writer, with or without schematic lines and offered or not. - private void PricedPackage(string id, bool lines, bool offered) - { - var factionKey = AddFaction(); - WritePackage(id, id, tweak: ship => - { - ship.Hull.Price = 100; - if (!lines) ship.Visual.SchematicLines = new List(); - }); - using var cache = ShipAuthoringStore.Open(Path.Combine(Mods, id, "ship.cc"), writable: true); - ShipAuthoringStore.WriteProduct(cache, id, new FactionProductData - { - Name = id + " product", - Design = new CultRecordRef(ShipModCatalog.HullKey(id)), - Manufacturer = new CultRecordRef(factionKey), - NotOffered = !offered - }); - cache.FlushAsync().Wait(); - } - - private CultRecordKey AddFaction() + // A maker (by name) the shipped catalog holds, made on first use. + private CultRecordKey Maker(string name) { using var cache = AetheriaStores.Open(Shipped, catalogWritable: true); - var existing = cache.GetAll().FirstOrDefault(); - var key = existing != null ? cache.RefOf(existing).Key : cache.Upsert(new Faction { Name = "Acme", ShortName = "ACM" }).Key; + var existing = cache.GetAll().FirstOrDefault(faction => faction.Name == name); + var key = existing != null ? cache.RefOf(existing).Key : cache.Upsert(new Faction { Name = name, ShortName = name.Substring(0, 3).ToUpperInvariant() }).Key; cache.FlushAsync().Wait(); return key; } + // A shipped product line a package's product can bin. + private CultRecordKey ShippedLine(string name, bool notOffered) + { + var maker = Maker("Acme"); + using var cache = AetheriaStores.Open(Shipped, catalogWritable: true); + var hull = cache.Upsert(new HullData { Name = name + " hull", Shape = ShipAuthoringTests.Fixture().Hull.Shape, Price = 10 }); + var line = cache.Upsert(new FactionProductData + { + Name = name, + Design = new CultRecordRef(hull.Key), + Manufacturer = new CultRecordRef(maker), + NotOffered = notOffered + }); + cache.FlushAsync().Wait(); + return line.Key; + } + + // One product of a package: its maker, whether it is itself off the market, and the line it bins, if any. + private sealed record Sale(string Maker, bool NotOffered = false, CultRecordKey? Bin = null); + + // A priced package selling the given products through the one product writer, with or without schematic lines + // (`lines` null leaves the record's SchematicLines null). + private void PricedPackage(string id, bool? lines, params Sale[] sales) + { + var makers = sales.Select(sale => Maker(sale.Maker)).ToArray(); + WritePackage(id, id, tweak: ship => + { + ship.Hull.Price = 100; + if (lines == false) ship.Visual.SchematicLines = new List(); + if (lines == null) ship.Visual.SchematicLines = null; + }); + using var cache = ShipAuthoringStore.Open(Path.Combine(Mods, id, "ship.cc"), writable: true); + for (var n = 0; n < sales.Length; n++) + ShipAuthoringStore.WriteProduct(cache, id, new FactionProductData + { + Name = $"{id} product {n}", + Design = new CultRecordRef(ShipModCatalog.HullKey(id)), + Manufacturer = new CultRecordRef(makers[n]), + NotOffered = sales[n].NotOffered, + Bin = sales[n].Bin is { } line ? new ProductBin { Line = new CultRecordRef(line), Floor = 1f } : null + }); + cache.FlushAsync().Wait(); + } + + private static string Excluded(ShipModCatalog.Composition composition, string package) => + composition.Excluded.SingleOrDefault(entry => entry.Package == package)?.Reason; + // An offered hull is drawn by its schematic lines (ShipModVisual.BuildLineMesh builds nothing from none), so one without any // is left out of the catalog with its reason; an offered hull with lines and an unoffered stand-in without them both compose. [Fact] public void ComposeExcludesAnOfferedHullWithoutSchematicLinesAndKeepsTheRest() { - PricedPackage("mod.offeredbare", lines: false, offered: true); - PricedPackage("mod.offeredlined", lines: true, offered: true); - PricedPackage("mod.standin", lines: false, offered: false); + PricedPackage("mod.offeredbare", lines: false, new Sale("Acme")); + PricedPackage("mod.offeredlined", lines: true, new Sale("Acme")); + PricedPackage("mod.standin", lines: false, new Sale("Acme", NotOffered: true)); var composition = ShipModCatalog.Compose(Shipped, Derived, Mods); @@ -96,6 +124,62 @@ public sealed class ShipModCatalogTests : IDisposable Assert.Contains("no schematic", excluded.Reason); } + // A hull is offered when any product of it is, and a binned product is offered only if its line is (ruling bin-inherits-line-offered): + // the verdict is IsOffered's, not a read of the product's own NotOffered. + [Fact] + public void OfferedNessComesFromAnyProductAndFromTheBinsLine() + { + var offeredLine = ShippedLine("Offered Line", notOffered: false); + var closedLine = ShippedLine("Closed Line", notOffered: true); + PricedPackage("mod.binoffered", lines: false, new Sale("Acme", Bin: offeredLine)); + PricedPackage("mod.binclosed", lines: false, new Sale("Acme", Bin: closedLine)); + PricedPackage("mod.oneoftwo", lines: false, new Sale("Acme", NotOffered: true), new Sale("Rival")); + + var composition = ShipModCatalog.Compose(Shipped, Derived, Mods); + + Assert.Contains("no schematic", Excluded(composition, "mod.binoffered")); + Assert.Null(Excluded(composition, "mod.binclosed")); + Assert.Contains("no schematic", Excluded(composition, "mod.oneoftwo")); + Assert.Equal(new[] { "mod.binclosed" }, composition.Included); + } + + [Fact] + public void ANullSchematicIsNoSchematicWhenOffered() + { + PricedPackage("mod.nulled", lines: null, new Sale("Acme")); + PricedPackage("mod.nulledstandin", lines: null, new Sale("Acme", NotOffered: true)); + + var composition = ShipModCatalog.Compose(Shipped, Derived, Mods); + + Assert.Contains("no schematic", Excluded(composition, "mod.nulled")); + Assert.Equal(new[] { "mod.nulledstandin" }, composition.Included); + } + + // A bin's line is a shipped product or one of the package's own, never another package's and never nothing: the catalog read + // refuses such a bin at boot, so compose leaves the package out and the derived catalog opens. + [Fact] + public void ComposeExcludesAPackageWhoseBinNamesNoShippedOrOwnLine() + { + var shipped = ShippedLine("Shipped Line", notOffered: false); + var maker = Maker("Acme"); + PricedPackage("mod.lined", lines: true, new Sale("Acme")); + PricedPackage("mod.cross", lines: false, new Sale("Rival", Bin: ShipModCatalog.ProductKey("mod.lined", maker))); + PricedPackage("mod.nothing", lines: true, new Sale("Acme", Bin: new CultRecordKey("product:nowhere"))); + PricedPackage("mod.shippedbin", lines: true, new Sale("Acme", Bin: shipped)); + PricedPackage("mod.ownbin", lines: true, new Sale("Acme"), new Sale("Rival", Bin: ShipModCatalog.ProductKey("mod.ownbin", maker))); + + PricedPackage("mod.binofbin", lines: true, new Sale("Acme", Bin: shipped), new Sale("Rival", Bin: ShipModCatalog.ProductKey("mod.binofbin", maker))); + + var (catalog, excluded) = ShipModCatalog.ResolveCatalog(Shipped, Derived, Mods); + + Assert.Equal(new[] { "mod.binofbin", "mod.cross", "mod.nothing" }, excluded.Select(entry => entry.Package).OrderBy(id => id, StringComparer.Ordinal)); + Assert.All(excluded, entry => Assert.Contains("bins a line", entry.Reason)); + Assert.Equal(Derived, catalog); + using var cache = AetheriaStores.Open(Derived); + Assert.Contains("mod.ownbin product 1", cache.GetAll().Select(product => product.Name)); + Assert.DoesNotContain("mod.cross product 0", cache.GetAll().Select(product => product.Name)); + } + // A package's station kind is checked per package: a bad one is left out, named with its reason, and the derived catalog // still opens. The shipped catalog's own check is unchanged. [Fact] diff --git a/tests/Aetheria.Shared.Tests/ShipProductTests.cs b/tests/Aetheria.Shared.Tests/ShipProductTests.cs index 0ee2db012..638f408b6 100644 --- a/tests/Aetheria.Shared.Tests/ShipProductTests.cs +++ b/tests/Aetheria.Shared.Tests/ShipProductTests.cs @@ -104,13 +104,14 @@ public sealed class ShipProductTests : IDisposable public void ValidateRefusesAnOfferedHullWithoutSchematicLinesAsComposeDoes() { Environment.CurrentDirectory = _root.Path; - string Bare(string id, bool offered) + string Bare(string id, bool offered, bool nullLines = false, CultRecordKey? bin = null) { - var directory = ShipFixture.WritePackage(Mods, id, id, tweak: ship => { ship.Hull.Price = 100; ship.Visual.SchematicLines = new List(); }); + var directory = ShipFixture.WritePackage(Mods, id, id, tweak: ship => { ship.Hull.Price = 100; ship.Visual.SchematicLines = nullLines ? null : new List(); }); var path = Path.Combine(directory, "ship.cc"); using var cache = ShipAuthoringStore.Open(path, writable: true); var product = Product(id + " product", _acme, id); product.NotOffered = !offered; + if (bin is { } line) product.Bin = new ProductBin { Line = new CultRecordRef(line), Floor = 1f }; ShipAuthoringStore.WriteProduct(cache, id, product); cache.FlushAsync().Wait(); return path; @@ -120,6 +121,14 @@ public sealed class ShipProductTests : IDisposable Assert.Equal(1, code); Assert.Contains("no schematic", error); Assert.Equal(0, Run("validate", Bare("mod.standin", offered: false)).Item1); + // A null SchematicLines is no schematic, as for compose. + var (nullCode, _, nullError) = Run("validate", Bare("mod.nulled", offered: true, nullLines: true)); + Assert.Equal(1, nullCode); + Assert.Contains("no schematic", nullError); + // A bin naming no shipped product and no product of the package is refused, as compose excludes it. + var (binCode, _, binError) = Run("validate", Bare("mod.badbin", offered: true, bin: new CultRecordKey("product:nowhere"))); + Assert.Equal(1, binCode); + Assert.Contains("bins a line", binError); Assert.Equal(new[] { "mod.standin" }, ShipModCatalog.Compose(Shipped, Derived, Mods).Included); } diff --git a/tools/AetherDb/ShipAuthoringCommands.cs b/tools/AetherDb/ShipAuthoringCommands.cs index 4298bc198..3b3ea9051 100644 --- a/tools/AetherDb/ShipAuthoringCommands.cs +++ b/tools/AetherDb/ShipAuthoringCommands.cs @@ -53,10 +53,10 @@ public static class ShipAuthoringCommands var judged = model != null && File.Exists(model) ? "records and model" : "records only, no model yet"; if (judged == "records and model") ShipModCatalog.ReadPackage(args[1]); else ShipAuthoringStore.ValidateLoaded(hull, ship, products); - // Compose refuses an offered hull with no schematic; the author sees that verdict here, before compose. - // A package with no products is never offered, so the shipped catalog (which only resolves a product's bin) is opened for none. + // Compose's verdict on a package's bins and its offered hull's schematic; the author sees it here, before compose. + // A package with no products has no bin and is never offered, so the shipped catalog is opened for none. using var catalog = products.Length > 0 ? AetherDb.Open().Cache : null; - if (ShipModCatalog.SchematicFault(catalog, ship, products) is { } fault) throw new InvalidOperationException(fault); + if (ShipModCatalog.MarketFault(catalog, ship, products) is { } fault) throw new InvalidOperationException(fault); Console.WriteLine($"{ship.Id}: {hull.Name}, {hull.Hardpoints.Count} hardpoints, {ship.Anchors.Count} anchors, {ship.SchematicLines.Count} lines ({judged})"); return 0; }