Merge cut package-product (Soul verdict cut-package-product.s4)

This commit is contained in:
Meta Vens 2026-10-12 20:54:40 +02:00
commit 3535660cc5
12 changed files with 578 additions and 30 deletions

View file

@ -88,7 +88,7 @@ public static class ShipAuthoringStore
var cache = new CultCache();
try
{
cache.AddBackingStore(new SingleFileMessagePackBackingStore(path, !writable), new[] { typeof(HullData), typeof(ShipAuthoring) });
cache.AddBackingStore(new SingleFileMessagePackBackingStore(path, !writable), new[] { typeof(HullData), typeof(ShipAuthoring), typeof(FactionProductData) });
return cache;
}
catch
@ -105,6 +105,14 @@ public static class ShipAuthoringStore
cache.UpsertAsync(typeof(ShipAuthoring), visual, ShipModCatalog.AuthoringKey(visual.Id)).GetAwaiter().GetResult();
}
// A package's product by one maker. These two are the only product writers; the key is the ship and the maker, so a
// maker has at most one product of the hull and writing again replaces it.
public static void WriteProduct(CultCache cache, string shipId, FactionProductData product) =>
cache.UpsertAsync(typeof(FactionProductData), product, ShipModCatalog.ProductKey(shipId, product.Manufacturer.Key)).GetAwaiter().GetResult();
public static bool DeleteProduct(CultCache cache, string shipId, CultRecordKey maker) =>
cache.Remove(ShipModCatalog.ProductKey(shipId, maker));
// A new mod hull that flies like a shipped one. Every member of the reference is copied through the type's own
// MessagePack round trip, so a member added to HullData later is copied too; then the six that are the shipped ship's
// own body and layout are reset: Name, Shape (one occupied cell, so the record loads), Hardpoints, Prefab, Schematic,
@ -123,8 +131,8 @@ public static class ShipAuthoringStore
}
// The structure of a ship file, without judging its content (drafts load): exactly one hull and one visual, each at
// its deterministic key, the hull naming that visual by its typed ref.
public static (HullData Hull, ShipAuthoring Visual) Load(string path)
// its deterministic key, the hull naming that visual by its typed ref, and the hull's products, each at its maker's key.
public static (HullData Hull, ShipAuthoring Visual, FactionProductData[] Products) Load(string path)
{
RefuseLegacyEmbeddedHull(path);
try { return LoadRecords(path); }
@ -167,7 +175,7 @@ public static class ShipAuthoringStore
private const int RetiredHullKey = 1;
private static (HullData Hull, ShipAuthoring Visual) LoadRecords(string path)
private static (HullData Hull, ShipAuthoring Visual, FactionProductData[] Products) LoadRecords(string path)
{
using var cache = Open(path);
var hulls = cache.GetAll<HullData>().ToArray();
@ -180,14 +188,44 @@ public static class ShipAuthoringStore
if (!cache.TryGetHandle(hull).Value.Key.Equals(ShipModCatalog.HullKey(visual.Id)) ||
!cache.TryGetHandle(visual).Value.Key.Equals(ShipModCatalog.AuthoringKey(visual.Id)))
throw new InvalidOperationException($"{path}: {visual.Id}: records must be stored under {ShipModCatalog.HullKey(visual.Id).Value} and {ShipModCatalog.AuthoringKey(visual.Id).Value}.");
return (hull, visual);
var products = cache.GetAll<FactionProductData>().ToArray();
foreach (var product in products)
{
if (!product.Manufacturer.IsSet() || ShipModCatalog.IsModKey(product.Manufacturer.Key))
throw new InvalidOperationException($"{path}: {visual.Id}: a product needs a manufacturer that is a shipped faction.");
var productKey = ShipModCatalog.ProductKey(visual.Id, product.Manufacturer.Key);
if (!cache.TryGetHandle(product).Value.Key.Equals(productKey))
throw new InvalidOperationException($"{path}: {visual.Id}: a product must be stored at {productKey.Value}.");
if (!product.Design.IsSet() || !product.Design.Key.Equals(ShipModCatalog.HullKey(visual.Id)))
throw new InvalidOperationException($"{path}: {visual.Id}: a product must name the package hull {ShipModCatalog.HullKey(visual.Id).Value} as its design.");
}
return (hull, visual, products);
}
public static (HullData Hull, ShipAuthoring Visual) Read(string path)
public static (HullData Hull, ShipAuthoring Visual, FactionProductData[] Products) Read(string path)
{
var (hull, visual, products) = Load(path);
ValidateLoaded(hull, visual, products);
return (hull, visual, products);
}
// Every judgement a loaded package's records face, in one place: Read, and validate on a draft with no model yet, call it.
public static void ValidateLoaded(HullData hull, ShipAuthoring visual, FactionProductData[] products)
{
var (hull, visual) = Load(path);
Validate(hull, visual);
return (hull, visual);
ValidateProducts(hull, visual, products);
}
// What a package may sell: every product named, no name twice, and a price above 0, because generation skips price 0.
private static void ValidateProducts(HullData hull, ShipAuthoring visual, FactionProductData[] products)
{
if (products.Length == 0) return;
if (products.Any(product => string.IsNullOrWhiteSpace(product.Name)))
throw new InvalidOperationException($"{visual.Id}: every product needs a name.");
if (products.Select(product => product.Name).Distinct(StringComparer.Ordinal).Count() != products.Length)
throw new InvalidOperationException($"{visual.Id}: product names must be unique within the package.");
if (hull.Price <= 0)
throw new InvalidOperationException($"{visual.Id}: a hull with products needs a price above 0 (generation skips price 0).");
}
// A hull names one body: a Unity prefab (shipped hulls) or a ShipAuthoring record (mod ships), never both, so no
@ -224,6 +262,8 @@ public static class ShipAuthoringStore
if (hull.Shape?.Cells == null || !hull.Shape.Cells.Cast<bool>().Any(occupied => occupied))
throw new InvalidOperationException($"{ship.Id}: schematic must contain at least one cell.");
RequireOneBody(hull, ship.Id);
if (hull.HullType == HullType.Ship && !(hull.Behaviors ?? new List<BehaviorData>()).Any(behavior => behavior is VelocityLimitData))
throw new InvalidOperationException($"{ship.Id}: a ship hull needs a top speed (VelocityLimit).");
if (!hull.Visual.Key.Equals(ShipModCatalog.AuthoringKey(ship.Id)))
throw new InvalidOperationException($"{ship.Id}: the hull must name its visual record {ShipModCatalog.AuthoringKey(ship.Id).Value}.");
if (string.IsNullOrWhiteSpace(ship.ModelAsset) || Path.IsPathRooted(ship.ModelAsset) ||

View file

@ -16,14 +16,18 @@ public static class ShipModCatalog
public ShipAuthoring Visual;
public string ModelPath;
public Dictionary<string, uint> NodeIndices;
// The package's own products, read from its ship.cc; empty for a package taken from the catalog, where gameplay reads products.
public FactionProductData[] Products = Array.Empty<FactionProductData>();
}
public static CultRecordKey HullKey(string id) => new CultRecordKey("mod-hull:" + id);
public static CultRecordKey AuthoringKey(string id) => new CultRecordKey("mod-ship:" + id);
public static CultRecordKey ProductKey(string id, CultRecordKey maker) => new CultRecordKey("mod-product:" + id + ":" + maker.Value);
// A key the mods own. The shipped catalog and its authored records never reference one: a mod may be uninstalled.
public static bool IsModKey(CultRecordKey key) =>
key.IsSet() && (key.Value.StartsWith("mod-hull:", StringComparison.Ordinal) || key.Value.StartsWith("mod-ship:", StringComparison.Ordinal));
key.IsSet() && (key.Value.StartsWith("mod-hull:", StringComparison.Ordinal) || key.Value.StartsWith("mod-ship:", StringComparison.Ordinal) || key.Value.StartsWith("mod-product:", StringComparison.Ordinal));
// A package left out of the composition, named by its directory, with every reason it was left out.
public sealed class Exclusion
@ -93,6 +97,8 @@ public static class ShipModCatalog
var keys = new HashSet<CultRecordKey>(cache.AllStoredDocuments.Select(record => record.Key));
var itemNames = new HashSet<string>(cache.GetAll<ItemData>().Select(item => item.Name), StringComparer.Ordinal);
var shipIds = new HashSet<string>(cache.GetAll<ShipAuthoring>().Select(existing => existing.Id), StringComparer.Ordinal);
var factionKeys = new HashSet<CultRecordKey>(cache.GetAll<Faction>().Select(faction => cache.TryGetHandle(faction).Value.Key));
var productNames = new HashSet<string>(cache.GetAll<FactionProductData>().Select(product => product.Name), StringComparer.Ordinal);
foreach (var (directory, package) in packages)
{
var (hull, ship) = (package.Hull, package.Visual);
@ -102,16 +108,33 @@ public static class ShipModCatalog
Exclude(directory, $"hull name '{hull.Name}' collides with an existing catalog item.");
if (shipIds.Contains(ship.Id))
Exclude(directory, $"ship ID collides with an existing catalog ship authoring record.");
foreach (var product in package.Products)
{
if (!factionKeys.Contains(product.Manufacturer.Key))
Exclude(directory, $"product '{product.Name}' names a manufacturer the shipped catalog holds no faction for.");
if (keys.Contains(ProductKey(ship.Id, product.Manufacturer.Key)))
Exclude(directory, $"a mod product key collides with an existing catalog record.");
if (productNames.Contains(product.Name))
Exclude(directory, $"product name '{product.Name}' collides with an existing catalog product.");
}
}
foreach (var claim in packages.GroupBy(package => package.Package.Hull.Name, StringComparer.Ordinal).Where(group => group.Count() > 1))
foreach (var (directory, package) in claim)
Exclude(directory, $"hull name '{claim.Key}' is also claimed by " +
string.Join(", ", claim.Where(other => other.Directory != directory).Select(other => other.Directory)) + ".");
foreach (var claim in packages.SelectMany(package => package.Package.Products.Select(product => (package.Directory, product.Name)))
.GroupBy(entry => entry.Name, StringComparer.Ordinal).Where(group => group.Select(entry => entry.Directory).Distinct().Count() > 1))
foreach (var directory in claim.Select(entry => entry.Directory).Distinct())
Exclude(directory, $"product name '{claim.Key}' is also claimed by " +
string.Join(", ", claim.Select(entry => entry.Directory).Where(other => other != directory).Distinct()) + ".");
var included = packages.Where(package => !reasons.ContainsKey(package.Directory)).Select(package => package.Package).ToArray();
foreach (var package in included)
// Verbatim: the mod store's own two records, under the keys it already holds them at.
{
// Verbatim: the mod store's own records, under the keys it already holds them at.
ShipAuthoringStore.Write(cache, package.Hull, package.Visual);
foreach (var product in package.Products) ShipAuthoringStore.WriteProduct(cache, package.Visual.Id, product);
}
// Shipped hulls too: the derived catalog must not hold a hull that names two bodies.
foreach (var hull in cache.GetAll<HullData>())
ShipAuthoringStore.RequireOneBody(hull, hull.Name);
@ -135,8 +158,8 @@ public static class ShipModCatalog
public static Package ReadPackage(string shipPath)
{
var path = Path.GetFullPath(shipPath);
var (hull, ship) = ShipAuthoringStore.Read(path);
return Bind(hull, ship, Path.GetDirectoryName(path), path);
var (hull, ship, products) = ShipAuthoringStore.Read(path);
return Bind(hull, ship, products, Path.GetDirectoryName(path), path);
}
// The package a catalog hull names: its two records come from the catalog, and only the GLB comes from the mod
@ -147,7 +170,7 @@ public static class ShipModCatalog
throw new InvalidOperationException($"{hull.Name}: the catalog holds no visual record {hull.Visual.Key.Value}.");
ShipAuthoringStore.Validate(hull, ship);
var directory = Path.Combine(Path.GetFullPath(modsRoot), ship.Id);
return Bind(hull, ship, directory, Path.Combine(directory, "ship.cc"));
return Bind(hull, ship, Array.Empty<FactionProductData>(), directory, Path.Combine(directory, "ship.cc"));
}
// The one game-side choice of catalog: the shipped file when no package is installed or none survives composition,
@ -166,7 +189,7 @@ public static class ShipModCatalog
return (composition.Included.Length == 0 ? shippedCatalog : derivedCatalog, composition.Excluded);
}
private static Package Bind(HullData hull, ShipAuthoring ship, string directory, string path)
private static Package Bind(HullData hull, ShipAuthoring ship, FactionProductData[] products, string directory, string path)
{
if (!string.Equals(Path.GetFileName(directory), ship.Id, StringComparison.Ordinal))
throw new InvalidOperationException($"{path}: ship ID must match its package directory name.");
@ -187,7 +210,7 @@ public static class ShipModCatalog
CultRecordRefs.Validate(hull);
return new Package
{
Hull = hull, Visual = ship, ModelPath = modelPath,
Hull = hull, Visual = ship, Products = products, ModelPath = modelPath,
NodeIndices = modelNodes.ToDictionary(node => node.Key, node => node.Value.Index, StringComparer.Ordinal)
};
}