Edit schema

From moorestech/moorestech · View interactive version

Edit schema is an AI agent skill from the GitHub repository moorestech/moorestech. マスターデータのYAMLスキーマを編集するためのガイド。スキーマの追加・変更・削除と、foreignKey追加時のC#バリデーション追加を扱う。 Use when:1.VanillaSchemaのymlファイル(blocks.yml,items.yml等)を編集する必要がある時2.新しいブロックタイプやパラメータを追加する 3.既存スキーマの構造を変更する4.SourceGeneratorのトリガー方法を確認する5.foreignKey(Guid参照)を追加しバリデーションを書く時

How to use this skill

Skill file

Schema Editing Guide

YAMLの書き方(プロパティ・型・設定オプション)は yaml_spec.md が正本。YAMLを書く前に該当箇所を読むこと。

Directory Structure

VanillaSchema/
├── blocks.yml, items.yml, fluids.yml ...  # メインスキーマ
└── ref/                                    # 再利用可能なスキーマ部品
    ├── inventoryConnects.yml
    ├── gearConnects.yml
    └── ...

Editing Procedure

1. Edit Schema YAML

VanillaSchema/ 配下の該当YAMLファイルを編集。

2. Update csc.rsp (Add/Delete Schema)

スキーマファイルの追加・削除時は moorestech_server/Assets/Scripts/Core.Master/csc.rsp を編集:

# 追加時
/additionalfile:Assets/../../VanillaSchema/newSchema.yml

# 削除時は該当行を削除

3. Trigger SourceGenerator

moorestech_server/Assets/Scripts/Core.Master/_CompileRequester.cs の dummyText を変更してコミットする:

private const string dummyText = "new-value-here";

4. Rebuild

uloopでコンパイル。生成コードは Mooresmaster.Model.*Module 名前空間に配置される。

Key Patterns

ref (Reusable Schema)

- key: inventoryConnectors
  ref: inventoryConnects  # VanillaSchema/ref/inventoryConnects.yml を参照

switch/cases (Conditional Properties)

- key: blockParam
  switch: ./blockType
  cases:
  - when: Chest
    type: object
    properties:
    - key: itemSlotCount
      type: integer

defineInterface (Shared Properties)

defineInterface:
- interfaceName: IChestParam
  properties:
  - key: itemSlotCount
    type: integer

# 使用時
implementationInterface:
- IChestParam

foreignKey (Reference to Other Schema)

- key: itemGuid
  type: uuid
  foreignKey:
    schemaId: items
    foreignKeyIdPath: /data/[*]/itemGuid
    displayElementPath: /data/[*]/name

Important Rules

プロパティのリネーム・削除時のJSONデータ更新

プロパティ名の変更・削除、必須プロパティ追加時は全JSONを更新。

必須キー変更時はJSONファイル、C#テストの文字列、JObject/JArray、JSON生成ヘルパーも検索・更新。YAMLのdefault:はローダーの欠損補完に使われない。

JSONデータ配置先:

grep -r '"旧プロパティ名"' --include='*.json' . ../moorestech_master/

スキーマ変更時は TestModDirectory.ForUnitTestModDirectory と外部ピン先の出荷マスタを新版コードでロード。スキーマのコミットとJSON実パス・コミットを組で記録。

プロパティ追加時の生成コンストラクタ破壊

プロパティを追加すると(optional: true でも)、SourceGenerator が生成する要素クラス(例 BlockMasterElement)のコンストラクタに必須の末尾引数が1つ増える。手書きで new XxxMasterElement(...) している箇所(主にテスト)は CS7036 になるので、末尾に引数を足す。JSONローダー経由のロードは影響を受けない。

grep -rn 'new <要素クラス名>(' --include='*.cs' moorestech_server moorestech_client | grep -v '/obj/'

生成順は PropertyTable 順なので、末尾プロパティとして足すと既存の引数順が崩れず差分が最小になる。

例外テストは新必須キーを揃え、対象の異常だけで失敗するか確認。配列添字だけで実データ由来とせず、テスト入力も調べる。

CIはクライアントプロジェクトからEditModeテストを実行する。スキーマ変更の影響テストはクライアントのproject-pathで回す。

foreignKey追加時のC#バリデーション

SourceGeneratorは foreignKey からバリデーションを自動生成しない。手動で足さないと、存在しないGuidが実行時に InvalidOperationException を起こす。

// items / fluids / blocks は Master の IdOrNull で引く
// Resolve items / fluids / blocks via the master's IdOrNull
var id = MasterHolder.ItemMaster.GetItemIdOrNull(element.ItemGuid); // GetFluidIdOrNull / GetBlockIdOrNull
if (id == null)
{
    logs += $"[{MasterName}] Name:{name} has invalid ItemGuid:{element.ItemGuid}\n";
}

// mapObjects は要素を引く
// Resolve mapObjects by element lookup
var mapObjectElement = MasterHolder.MapObjectMaster.GetMapObjectElementOrNull(element.MapObjectGuid);

// IdOrNull を持たない参照先(research・challenge・同一スキーマ内参照)はローカル関数で存在確認
// Targets without IdOrNull (research, challenge, same-schema refs) use a local existence check
bool ExistsResearchGuid(Guid researchGuid) => Array.Exists(research.Data, r => r.ResearchNodeGuid == researchGuid);

見落としやすい箇所: ref: 先のスキーマ内の foreignKey(例 generateFluids の fluidGuid)、switch/cases の各case、配列要素内の foreignKey(foreach で回す)。optional な Guid は未設定時(Guid.Empty / HasValue 無し)をスキップする。

SourceGenerator Troubleshooting

Mooresmaster 名前空間が見つからない等、生成されていないことによるコンパイルエラーが出たら、まず csc.rsp の登録漏れとYAMLの書き方を疑う(SourceGeneratorは worktree・CIでも動く)。YAML全体を yaml_spec.md と照らして直す。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.