{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://uap.cnbarrier.com/schema-v1.json",
  "title": "Unity Assets Patcher Mod Manifest v1",
  "description": "Structural authoring schema for Unity Assets Patcher mod manifests. Runtime validation is performed by Unity Assets Patcher.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "$schema",
    "name",
    "author",
    "version",
    "targets"
  ],
  "properties": {
    "$schema": {
      "title": "Schema",
      "description": "Enables validation and completion in JSON Schema-aware editors.",
      "type": "string",
      "const": "https://uap.cnbarrier.com/schema-v1.json"
    },
    "name": {
      "title": "Mod name",
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "author": {
      "title": "Author",
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "version": {
      "title": "Mod version",
      "description": "Semantic versioning is recommended but not required.",
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "description": {
      "title": "Description",
      "type": "string"
    },
    "game": {
      "title": "Game name",
      "description": "The game name used for automatic Steam installation discovery.",
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "copyFiles": {
      "title": "Payload files",
      "description": "Files copied from the mod archive into the resolved assets directory.",
      "type": "array",
      "items": {
        "$ref": "#/$defs/copyFile"
      }
    },
    "targets": {
      "title": "Required targets",
      "description": "Asset-file patch groups that are always applied.",
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/target"
      }
    },
    "optional": {
      "title": "Optional groups",
      "description": "Independent content groups that users may opt into during installation.",
      "type": "array",
      "items": {
        "$ref": "#/$defs/optionalGroup"
      }
    }
  },
  "$defs": {
    "fieldValueMap": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": true
    },
    "copyFile": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "source"
      ],
      "properties": {
        "source": {
          "title": "Archive source path",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        }
      }
    },
    "patchValue": {
      "description": "A scalar, array, or object patch value. Compatibility with the target asset field is validated during planning.",
      "type": [
        "array",
        "boolean",
        "number",
        "object",
        "string"
      ]
    },
    "setOperation": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "from",
        "to"
      ],
      "properties": {
        "from": {
          "description": "The value expected before the patch is applied.",
          "$ref": "#/$defs/patchValue"
        },
        "to": {
          "description": "The value written by the patch. A $pathId reference may be used where supported.",
          "$ref": "#/$defs/patchValue"
        }
      }
    },
    "setMap": {
      "type": "object",
      "propertyNames": {
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": {
        "$ref": "#/$defs/setOperation"
      }
    },
    "addMap": {
      "type": "object",
      "propertyNames": {
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": {
        "type": "array"
      }
    },
    "replaceAsset": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "fromFile",
        "matchField"
      ],
      "properties": {
        "fromFile": {
          "description": "The path of the source assets file in the mod archive.",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "matchField": {
          "description": "The field used to associate target and replacement assets.",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        }
      }
    },
    "copyAsset": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "from"
      ],
      "properties": {
        "from": {
          "type": "object",
          "additionalProperties": true,
          "required": [
            "type",
            "match"
          ],
          "properties": {
            "type": {
              "type": "string",
              "minLength": 1,
              "pattern": "\\S"
            },
            "match": {
              "$ref": "#/$defs/fieldValueMap"
            }
          }
        }
      }
    },
    "patch": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "type",
        "match"
      ],
      "properties": {
        "type": {
          "title": "Asset type",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "match": {
          "title": "Target match",
          "$ref": "#/$defs/fieldValueMap"
        },
        "componentType": {
          "title": "Component type",
          "description": "Selects a component attached to a matched GameObject.",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "set": {
          "title": "Set operations",
          "$ref": "#/$defs/setMap"
        },
        "add": {
          "title": "Add operations",
          "$ref": "#/$defs/addMap"
        },
        "replaceAsset": {
          "title": "Asset replacement",
          "$ref": "#/$defs/replaceAsset"
        },
        "copyAsset": {
          "title": "Asset copy",
          "$ref": "#/$defs/copyAsset"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "componentType": true
            },
            "required": [
              "componentType"
            ]
          },
          "then": {
            "properties": {
              "type": {
                "type": "string",
                "pattern": "^[Gg][Aa][Mm][Ee][Oo][Bb][Jj][Ee][Cc][Tt]$"
              }
            },
            "not": {
              "properties": {
                "replaceAsset": true
              },
              "required": [
                "replaceAsset"
              ]
            }
          }
        }
      ]
    },
    "target": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "file",
        "patches"
      ],
      "properties": {
        "file": {
          "title": "Assets file",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "patches": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/patch"
          }
        }
      }
    },
    "optionalGroup": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "title": "Group name",
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "description": {
          "title": "Group description",
          "type": "string"
        },
        "copyFiles": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/copyFile"
          }
        },
        "targets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/target"
          }
        }
      },
      "anyOf": [
        {
          "required": [
            "copyFiles"
          ],
          "properties": {
            "copyFiles": {
              "type": "array",
              "minItems": 1
            }
          }
        },
        {
          "required": [
            "targets"
          ],
          "properties": {
            "targets": {
              "type": "array",
              "minItems": 1
            }
          }
        }
      ]
    }
  }
}
