fix(chargen): CC1 review fix round — Custom is template 0, SkillTable cost fallback, frozen model

Implements all six Opus review findings against 04450041 (Campaign CC
CC1 chargen data layer):

- F1 (HIGH, blocking): ChargenTemplate's doc claimed "Custom" has no
  ChargenTemplate entry and cited two nonexistent addresses. Verified
  against the named retail decomp: gmCGProfessionPage::UpdateProfession
  @ 0x004821b0 resolves BOTH the highlighted button and the description
  string from CharGenState.template_ 0..6, and case 0 is button
  0x100003d9 / ID_CharGen_CustomText. Custom IS template index 0 (the
  "Adventurer" row CC1 already found sitting at the attribute floor).
  CharGenState::SetTemplate @ 0x005C5A60 confirms every button (including
  Custom) calls CharGenState::ApplyTemplate @ 0x005C5080 when committing,
  so selecting Custom resets the sliders/skills to that row rather than
  leaving them untouched.

- F2 (MEDIUM): retail's skill-cost lookup is two-tiered
  (ACCharGenData::GetSkillTrainedCost/GetSkillSpecializedCost @
  0x005C26D0/0x005C27D0 fall through to the global SkillTable,
  portal.dat 0x0E000004, on a heritage-list miss — confirmed against
  ACE's identical PlayerFactory.cs precedence). ChargenTableReader now
  also projects the global SkillTable into
  ChargenOptions.GlobalSkillCostsBySkillId, and
  ChargenSkillCreditMath.ComputeSpent/RemainingCredits check the
  heritage list first and the global list on a miss. Added an
  installed-DAT completeness assertion recording reality: the global
  table prices 38/54 advancement skill ids, every one of the 13
  installed heritages ships exactly one heritage-specific override
  (always also priced globally), and 16 ids are genuinely uncostable in
  both tiers. Also filed a CC7 risk-item note: ACE's own heritage-
  override branch over-deducts on Specialize (PlayerFactory.cs:184-211)
  — a retail-legal build may be rejected by local ACE at the CC7
  connected gate; that is an ACE bug, not an acdream defect.

- F3 (MEDIUM): every collection ChargenTableReader hands into the
  record model is now frozen at projection (ToFrozenDictionary/ToArray,
  matching MagicCatalog's house pattern), including both
  ChargenOptions.Empty dictionaries.

- F4 (LOW): added a reflection guard test
  (ChargenNoChoriziteLeakTests) that walks every public
  AcDream.Core.CharGen member (property/indexer/constructor/method
  types, recursively through generic arguments) and fails if any
  resolves to the DatReaderWriter or a Chorizite* assembly.

- F5 (LOW): ChargenGenderOptions.HasAnyAppearanceOptions's doc now
  states precisely what the installed-DAT gate proves (an OR across
  eight lists, for at least one gender per heritage) rather than the
  stronger claim it previously made, and explicitly calls out the three
  omitted color lists. Added a second installed-DAT gate that records
  per-list reality across every gender of every heritage — found
  complete, no empty lists anywhere in the installed DAT today.

- F6 (LOW): ChargenOptions.TryGetHeritage/TryGetStarterArea now use
  [MaybeNullWhen(false)] instead of null! suppression, matching the
  house pattern already used elsewhere in the test suite. Fixed every
  call site this surfaced (more than the five originally estimated,
  since Content.Tests has TreatWarningsAsErrors).

Core.Tests: 4737 passed / 1 skip (pre-existing, unrelated).
Content.Tests: 145 passed / 0 skip.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-08-15 13:32:40 +02:00
parent 70d52b0da2
commit cb4703e8d5
11 changed files with 637 additions and 139 deletions

View file

@ -1,22 +1,30 @@
using System.Collections.Frozen;
using AcDream.Core.CharGen;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Types;
using CoreChargenObjDesc = AcDream.Core.CharGen.ChargenObjDesc;
using DatCharGen = DatReaderWriter.DBObjs.CharGen;
using DatObjDesc = DatReaderWriter.Types.ObjDesc;
using DatSkillTable = DatReaderWriter.DBObjs.SkillTable;
namespace AcDream.Content.CharGen;
/// <summary>
/// Projects portal.dat's CharGen table (id <see cref="ChargenTableDid"/>,
/// retail <c>ACCharGenData::Serialize @ 0x005C36D0</c>) into acdream's
/// retail <c>ACCharGenData::Serialize @ 0x005C36D0</c>) PLUS the global
/// SkillTable (id <see cref="SkillTableDid"/>) into acdream's
/// presentation-free <see cref="AcDream.Core.CharGen.ChargenOptions"/> tree.
/// Mirrors <c>MagicCatalog.Load</c>'s shape: one static entry point over
/// <see cref="IDatReaderWriter"/>, no Chorizite types cross into the
/// <see cref="IDatReaderWriter"/>, every returned collection is frozen at
/// projection (<c>ToFrozenDictionary</c> / <c>ToArray</c>, matching
/// <c>MagicCatalog</c>'s pattern), and no Chorizite types cross into the
/// returned model. Cross-checked against ACE's
/// <c>ACE.DatLoader.FileTypes.CharGen</c> +
/// <c>ACE.DatLoader.Entity.HeritageGroupCG/SexCG/TemplateCG</c> loaders,
/// which unpack the identical field order from the same DAT bytes.
/// which unpack the identical field order from the same DAT bytes, and
/// against <c>ACE.DatLoader.FileTypes.SkillTable</c> for the global
/// skill-cost fallback (see
/// <see cref="AcDream.Core.CharGen.ChargenOptions.GlobalSkillCostsBySkillId"/>).
/// </summary>
public static class ChargenTableReader
{
@ -24,48 +32,83 @@ public static class ChargenTableReader
/// <c>ACE.DatLoader.FileTypes.CharGen.FILE_ID</c>).</summary>
public const uint ChargenTableDid = 0x0E000002u;
/// <summary>Retail's global SkillTable DAT file id (ACE:
/// <c>ACE.DatLoader.FileTypes.SkillTable.FILE_ID</c>) — the fallback
/// retail's <c>ACCharGenData::GetSkillTrainedCost @ 0x005C26D0</c> /
/// <c>GetSkillSpecializedCost @ 0x005C27D0</c> read via
/// <c>DBCache::GetFromEnumStatic(4, 2, 0x10000004)</c> when a heritage's
/// own skill-cost list has no entry for a skill id.</summary>
public const uint SkillTableDid = 0x0E000004u;
/// <summary>
/// Loads and projects the installed CharGen table. Returns
/// <see cref="ChargenOptions.Empty"/> if the table is missing from the
/// supplied dat source (mirrors <c>MagicCatalog</c>'s tolerance for a
/// missing optional table — callers that require the table present
/// should check <c>HeritagesById.Count</c> themselves).
/// Loads and projects the installed CharGen table plus the global
/// SkillTable fallback. Returns <see cref="ChargenOptions.Empty"/> if
/// the CharGen table is missing from the supplied dat source (mirrors
/// <c>MagicCatalog</c>'s tolerance for a missing optional table —
/// callers that require the table present should check
/// <c>HeritagesById.Count</c> themselves). A missing global SkillTable
/// degrades to an empty fallback dictionary rather than failing the
/// whole load — the per-heritage costs (the common case) still work.
/// </summary>
public static ChargenOptions Load(IDatReaderWriter dats)
{
ArgumentNullException.ThrowIfNull(dats);
DatCharGen? table = dats.Get<DatCharGen>(ChargenTableDid);
return table is null ? ChargenOptions.Empty : Project(table);
if (table is null)
return ChargenOptions.Empty;
DatSkillTable? skillTable = dats.Get<DatSkillTable>(SkillTableDid);
return Project(table, skillTable);
}
/// <summary>Pure projection from an already-loaded DAT record — split out
/// from <see cref="Load"/> so tests can exercise it against
/// hand-built <see cref="DatCharGen"/> fixtures without a live DAT.</summary>
public static ChargenOptions Project(DatCharGen table)
/// hand-built <see cref="DatCharGen"/> fixtures without a live DAT.
/// <paramref name="skillTable"/> is optional (mirrors <see cref="Load"/>'s
/// missing-table tolerance) and projects into
/// <see cref="AcDream.Core.CharGen.ChargenOptions.GlobalSkillCostsBySkillId"/>.</summary>
public static ChargenOptions Project(DatCharGen table, DatSkillTable? skillTable = null)
{
ArgumentNullException.ThrowIfNull(table);
var starterAreas = new List<ChargenStarterArea>(table.StartingAreas.Count);
var starterAreas = new ChargenStarterArea[table.StartingAreas.Count];
for (int i = 0; i < table.StartingAreas.Count; i++)
starterAreas.Add(ProjectStarterArea(i, table.StartingAreas[i]));
starterAreas[i] = ProjectStarterArea(i, table.StartingAreas[i]);
var heritagesById = new Dictionary<uint, ChargenHeritageOptions>(table.HeritageGroups.Count);
foreach (KeyValuePair<uint, HeritageGroupCG> pair in table.HeritageGroups)
heritagesById[pair.Key] = ProjectHeritage(pair.Key, pair.Value);
return new ChargenOptions(starterAreas, heritagesById);
var globalSkillCosts = new Dictionary<uint, ChargenSkillCost>(skillTable?.Skills.Count ?? 0);
if (skillTable is not null)
{
foreach (KeyValuePair<DatReaderWriter.Enums.SkillId, SkillBase> pair in skillTable.Skills)
{
uint skillId = (uint)pair.Key;
globalSkillCosts[skillId] = new ChargenSkillCost(
skillId,
pair.Value.TrainedCost,
pair.Value.SpecializedCost);
}
}
return new ChargenOptions(
starterAreas,
heritagesById.ToFrozenDictionary(),
globalSkillCosts.ToFrozenDictionary());
}
private static ChargenStarterArea ProjectStarterArea(int index, StartingArea area)
{
var locations = new List<ChargenPosition>(area.Locations.Count);
foreach (Position position in area.Locations)
var locations = new ChargenPosition[area.Locations.Count];
for (int i = 0; i < area.Locations.Count; i++)
{
locations.Add(new ChargenPosition(
Position position = area.Locations[i];
locations[i] = new ChargenPosition(
position.CellId,
position.Frame.Origin,
position.Frame.Orientation));
position.Frame.Orientation);
}
return new ChargenStarterArea(index, area.Name.Value, locations);
}
@ -79,9 +122,9 @@ public static class ChargenTableReader
skillCosts[skillId] = new ChargenSkillCost(skillId, skill.NormalCost, skill.PrimaryCost);
}
var templates = new List<ChargenTemplate>(cg.Templates.Count);
foreach (TemplateCG template in cg.Templates)
templates.Add(ProjectTemplate(template));
var templates = new ChargenTemplate[cg.Templates.Count];
for (int i = 0; i < cg.Templates.Count; i++)
templates[i] = ProjectTemplate(cg.Templates[i]);
var gendersByKey = new Dictionary<int, ChargenGenderOptions>(cg.Genders.Count);
foreach (KeyValuePair<int, SexCG> pair in cg.Genders)
@ -95,22 +138,22 @@ public static class ChargenTableReader
cg.EnvironmentSetupId.DataId,
cg.AttributeCredits,
cg.SkillCredits,
new List<int>(cg.PrimaryStartAreas),
new List<int>(cg.SecondaryStartAreas),
skillCosts,
cg.PrimaryStartAreas.ToArray(),
cg.SecondaryStartAreas.ToArray(),
skillCosts.ToFrozenDictionary(),
templates,
gendersByKey);
gendersByKey.ToFrozenDictionary());
}
private static ChargenTemplate ProjectTemplate(TemplateCG template)
{
var normalSkills = new List<uint>(template.NormalSkills.Count);
foreach (var skillId in template.NormalSkills)
normalSkills.Add((uint)skillId);
var normalSkills = new uint[template.NormalSkills.Count];
for (int i = 0; i < template.NormalSkills.Count; i++)
normalSkills[i] = (uint)template.NormalSkills[i];
var primarySkills = new List<uint>(template.PrimarySkills.Count);
foreach (var skillId in template.PrimarySkills)
primarySkills.Add((uint)skillId);
var primarySkills = new uint[template.PrimarySkills.Count];
for (int i = 0; i < template.PrimarySkills.Count; i++)
primarySkills[i] = (uint)template.PrimarySkills[i];
return new ChargenTemplate(
template.Name.Value,
@ -129,33 +172,41 @@ public static class ChargenTableReader
private static ChargenGenderOptions ProjectGender(int genderKey, SexCG sex)
{
var hairStyles = new List<ChargenHairStyle>(sex.HairStyles.Count);
foreach (HairStyleCG hair in sex.HairStyles)
var hairStyles = new ChargenHairStyle[sex.HairStyles.Count];
for (int i = 0; i < sex.HairStyles.Count; i++)
{
hairStyles.Add(new ChargenHairStyle(
HairStyleCG hair = sex.HairStyles[i];
hairStyles[i] = new ChargenHairStyle(
hair.IconId.DataId,
hair.Bald,
hair.AlternateSetup,
ProjectObjDesc(hair.ObjDesc)));
ProjectObjDesc(hair.ObjDesc));
}
var eyeStrips = new List<ChargenEyeStrip>(sex.EyeStrips.Count);
foreach (EyeStripCG eye in sex.EyeStrips)
var eyeStrips = new ChargenEyeStrip[sex.EyeStrips.Count];
for (int i = 0; i < sex.EyeStrips.Count; i++)
{
eyeStrips.Add(new ChargenEyeStrip(
EyeStripCG eye = sex.EyeStrips[i];
eyeStrips[i] = new ChargenEyeStrip(
eye.IconId.DataId,
eye.BaldIconId,
ProjectObjDesc(eye.ObjDesc),
ProjectObjDesc(eye.BaldObjDesc)));
ProjectObjDesc(eye.BaldObjDesc));
}
var noseStrips = new List<ChargenFaceStrip>(sex.NoseStrips.Count);
foreach (FaceStripCG strip in sex.NoseStrips)
noseStrips.Add(new ChargenFaceStrip(strip.IconId.DataId, ProjectObjDesc(strip.ObjDesc)));
var noseStrips = new ChargenFaceStrip[sex.NoseStrips.Count];
for (int i = 0; i < sex.NoseStrips.Count; i++)
{
FaceStripCG strip = sex.NoseStrips[i];
noseStrips[i] = new ChargenFaceStrip(strip.IconId.DataId, ProjectObjDesc(strip.ObjDesc));
}
var mouthStrips = new List<ChargenFaceStrip>(sex.MouthStrips.Count);
foreach (FaceStripCG strip in sex.MouthStrips)
mouthStrips.Add(new ChargenFaceStrip(strip.IconId.DataId, ProjectObjDesc(strip.ObjDesc)));
var mouthStrips = new ChargenFaceStrip[sex.MouthStrips.Count];
for (int i = 0; i < sex.MouthStrips.Count; i++)
{
FaceStripCG strip = sex.MouthStrips[i];
mouthStrips[i] = new ChargenFaceStrip(strip.IconId.DataId, ProjectObjDesc(strip.ObjDesc));
}
return new ChargenGenderOptions(
genderKey,
@ -170,9 +221,9 @@ public static class ChargenTableReader
sex.MotionTable.DataId,
sex.CombatTable.DataId,
ProjectObjDesc(sex.BaseObjDesc),
new List<uint>(sex.HairColors),
sex.HairColors.ToArray(),
hairStyles,
new List<uint>(sex.EyeColors),
sex.EyeColors.ToArray(),
eyeStrips,
noseStrips,
mouthStrips,
@ -180,35 +231,45 @@ public static class ChargenTableReader
ProjectGearList(sex.Shirts),
ProjectGearList(sex.Pants),
ProjectGearList(sex.Footwear),
new List<uint>(sex.ClothingColors));
sex.ClothingColors.ToArray());
}
private static List<ChargenGearOption> ProjectGearList(List<GearCG> gearList)
private static ChargenGearOption[] ProjectGearList(List<GearCG> gearList)
{
var result = new List<ChargenGearOption>(gearList.Count);
foreach (GearCG gear in gearList)
result.Add(new ChargenGearOption(gear.Name.Value, gear.ClothingTable.DataId, gear.WeenieDefault));
var result = new ChargenGearOption[gearList.Count];
for (int i = 0; i < gearList.Count; i++)
{
GearCG gear = gearList[i];
result[i] = new ChargenGearOption(gear.Name.Value, gear.ClothingTable.DataId, gear.WeenieDefault);
}
return result;
}
private static CoreChargenObjDesc ProjectObjDesc(DatObjDesc objDesc)
{
var subPalettes = new List<ChargenSubPalette>(objDesc.SubPalettes.Count);
foreach (SubPalette sub in objDesc.SubPalettes)
subPalettes.Add(new ChargenSubPalette(sub.SubId.DataId, sub.Offset, sub.NumColors));
var textureChanges = new List<ChargenTextureChange>(objDesc.TextureChanges.Count);
foreach (TextureMapChange change in objDesc.TextureChanges)
var subPalettes = new ChargenSubPalette[objDesc.SubPalettes.Count];
for (int i = 0; i < objDesc.SubPalettes.Count; i++)
{
textureChanges.Add(new ChargenTextureChange(
change.PartIndex,
change.OldTexture.DataId,
change.NewTexture.DataId));
SubPalette sub = objDesc.SubPalettes[i];
subPalettes[i] = new ChargenSubPalette(sub.SubId.DataId, sub.Offset, sub.NumColors);
}
var animPartChanges = new List<ChargenAnimPartChange>(objDesc.AnimPartChanges.Count);
foreach (AnimationPartChange change in objDesc.AnimPartChanges)
animPartChanges.Add(new ChargenAnimPartChange(change.PartIndex, change.PartId.DataId));
var textureChanges = new ChargenTextureChange[objDesc.TextureChanges.Count];
for (int i = 0; i < objDesc.TextureChanges.Count; i++)
{
TextureMapChange change = objDesc.TextureChanges[i];
textureChanges[i] = new ChargenTextureChange(
change.PartIndex,
change.OldTexture.DataId,
change.NewTexture.DataId);
}
var animPartChanges = new ChargenAnimPartChange[objDesc.AnimPartChanges.Count];
for (int i = 0; i < objDesc.AnimPartChanges.Count; i++)
{
AnimationPartChange change = objDesc.AnimPartChanges[i];
animPartChanges[i] = new ChargenAnimPartChange(change.PartIndex, change.PartId.DataId);
}
return new CoreChargenObjDesc(objDesc.PaletteId.DataId, subPalettes, textureChanges, animPartChanges);
}