diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ebeb12f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,48 @@ +name: CI + +on: + pull_request: + push: + branches: [main, story/curseforge] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +jobs: + analyze: + name: Format and analyze + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@v1 + with: + sdk: 3.13.4 + - run: dart pub get + - name: Check formatting + run: dart format --output=none --set-exit-if-changed . + - name: Analyze + run: dart analyze --fatal-infos + + test: + name: Test (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@v4 + - uses: dart-lang/setup-dart@v1 + with: + sdk: 3.13.4 + - run: dart pub get + # Live-API tests are tagged `network` and excluded by dart_test.yaml. + - name: Test + run: dart test --reporter expanded diff --git a/.gitignore b/.gitignore index ebd6297..d20dba8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ # Created by `dart pub` .dart_tool/ /example/build +**/.gitrinth/publications/curseforge/ diff --git a/README.md b/README.md index 83cac73..2b24d5d 100644 --- a/README.md +++ b/README.md @@ -259,13 +259,17 @@ metadata fetches at a local fake server. ### Locations & credentials -| Variable | Used by | Purpose | -|-------------------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `GITRINTH_CACHE` | every command | Override the cache root. Defaults to `/.gitrinth/cache`. | -| `GITRINTH_CONFIG` | every command | Override the user config file path. `--config` wins. | -| `GITRINTH_MODRINTH_URL` | every command | Override the default Modrinth API base URL. Used to point at a self-hosted labrinth instance for the default host. | -| `GITRINTH_TOKEN` | every command | Override the stored Modrinth PAT for the *default* host. Sent bare (no `Bearer` prefix). Other hosts always use the token stored via `gitrinth modrinth token add`. | -| `HOME` / `USERPROFILE` | every command | Resolves the default cache root and user config path. `USERPROFILE` is consulted on Windows; `HOME` everywhere else. | +| Variable | Used by | Purpose | +|-----------------------------------------------|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `GITRINTH_CACHE` | every command | Override the cache root. Defaults to `/.gitrinth/cache`. | +| `GITRINTH_CONFIG` | every command | Override the user config file path. `--config` wins. | +| `GITRINTH_MODRINTH_URL` | every command | Override the default Modrinth API base URL. Used to point at a self-hosted labrinth instance for the default host. | +| `GITRINTH_TOKEN` | every command | Override the stored Modrinth PAT for the *default* host. Sent bare (no `Bearer` prefix). Other hosts always use the token stored via `gitrinth modrinth token add`. | +| `GITRINTH_CURSEFORGE_URL` | every command | Override the CurseForge download API base URL. Defaults to `https://api.curseforge.com`. | +| `GITRINTH_CURSEFORGE_TOKEN` | every command | Override the default CurseForge download API key. This is separate from the publish key stored by `gitrinth token add curseforge.com`. | +| `GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64` | build-time | Base64-encoded default CurseForge download API key embedded into official builds. | +| `GITRINTH_CURSEFORGE_UPLOAD_TOKEN` | `publish` | Override the stored CurseForge publish key. Falls back to the key stored by `gitrinth token add curseforge.com`. | +| `HOME` / `USERPROFILE` | every command | Resolves the default cache root and user config path. `USERPROFILE` is consulted on Windows; `HOME` everywhere else. | ### Java runtime diff --git a/assets/schema/mods.schema.yaml b/assets/schema/mods.schema.yaml index 4f288dd..c4cc659 100644 --- a/assets/schema/mods.schema.yaml +++ b/assets/schema/mods.schema.yaml @@ -11,7 +11,7 @@ required: - version - description - loader - - mc-version + - mc_version properties: @@ -119,12 +119,31 @@ properties: description: Server-side compatibility. publish_to: - description: | - URL of the Modrinth-compatible server `gitrinth publish` uploads - to. Omit (or set `null`) to publish to modrinth.com. Set to - `none` to disable publishing. - type: [ string, "null" ] - format: uri + description: Publish targets; omitted/null follows enabled gitrinth platforms. + oneOf: + - type: "null" + - enum: [none, modrinth, curseforge] + - type: array + minItems: 1 + uniqueItems: true + items: + enum: [modrinth, curseforge] + - type: object + minProperties: 1 + additionalProperties: false + properties: + modrinth: + oneOf: + - type: "null" + - type: string + pattern: '^https?://[^/]+.*$' + - type: object + maxProperties: 0 + curseforge: + oneOf: + - type: "null" + - type: object + maxProperties: 0 loader: type: object @@ -151,7 +170,7 @@ properties: Mod loader the modpack targets, optionally with a version tag in docker-image style: `` or `:`. - - Loader: one of `forge`, `fabric`, `neoforge`, or `vanilla`. + - Loader: a lowercase registered identity (built-ins include Forge, Fabric, NeoForge, Quilt, and vanilla). - Tag (optional): `stable` (default — latest stable loader), `latest` (newest of any stability), or a concrete version string (e.g. `0.17.3`). Resolved against Modrinth at `get` @@ -170,17 +189,14 @@ properties: server-binary selection. oneOf: - type: string - pattern: ^(forge|fabric|neoforge)(:[^:\s][^:]*)?$ + pattern: ^(?!vanilla(?::|$))[a-z][a-z0-9_-]*(:[^:\s][^:]*)?$ - const: vanilla shaders: description: | Shader loader. Required when the `shaders` section has entries; otherwise omit. - enum: - - iris - - optifine - - canvas - - vanilla + type: string + pattern: ^[a-z][a-z0-9_-]*$ plugins: description: | Plugin loader. Required when the `plugins:` section has @@ -192,9 +208,9 @@ properties: SpongeVanilla. The resolved variant and concrete version are stored in `mods.lock`. type: string - pattern: ^(bukkit|folia|paper|spigot|sponge)(:[^:\s][^:]*)?$ + pattern: ^[a-z][a-z0-9_-]*(:[^:\s][^:]*)?$ - mc-version: + mc_version: type: string description: | Exact Minecraft version, e.g. "1.21.1". No ranges or wildcards. @@ -204,15 +220,45 @@ properties: compatibility, and server-binary selection alongside `loader`. pattern: ^[A-Za-z0-9][A-Za-z0-9._+-]*$ - tooling: + modrinth_host: + type: [ string, "null" ] + format: uri + description: | + Pack-level default base URL for the Modrinth source kind. When + set, every Modrinth-source entry that does not declare its own + `modrinth_host:` resolves against this URL instead of + `https://api.modrinth.com/v2`. + + gitrinth: type: [ object, "null" ] - description: Tooling version constraints. + description: | + Tooling configuration for gitrinth itself. Holds the semver + constraint on the running gitrinth CLI (`gitrinth.version`) and + the platform default toggles (`gitrinth.modrinth`, + `gitrinth.curseforge`) that decide which sources participate by + default. additionalProperties: false properties: - gitrinth: + version: type: [ string, "null" ] description: Semver range for the gitrinth CLI (e.g. "^1.0.0", ">=1.0.0 <2.0.0"). minLength: 1 + modrinth: + description: | + Whether Modrinth participates by default for entries that do + not restrict their `sources:` set. Defaults to `enabled`. + enum: + - enabled + - disabled + curseforge: + description: | + Whether CurseForge participates by default for entries that + do not restrict their `sources:` set. Defaults to `disabled`. + Explicit `sources: curseforge` or `cf:` opts an entry + into CurseForge regardless of this toggle. + enum: + - enabled + - disabled mods: description: | @@ -336,8 +382,10 @@ $defs: modDependency: description: | Mod dependency. Short form: a version constraint (from default - Modrinth). Long form: object with at most one source (`hosted`, - `url`, `path`) and optional `version`. + Modrinth). Long form: object with at most one of `url:` / `path:` + and optional `version`. `modrinth_host:` may co-exist with the + Modrinth source kind (`modrinth:` or no source override) but is + mutually exclusive with `url:` / `path:`. oneOf: - $ref: "#/$defs/modVersion" - $ref: "#/$defs/modSource" @@ -345,8 +393,10 @@ $defs: modSource: type: object description: | - Long-form mod dependency. At most one of `hosted`, `url`, `path`. - `url` and `path` make the modpack non-publishable to Modrinth. + Long-form mod dependency. At most one of `url`, `path`. + `modrinth_host:` is a host override on the Modrinth source kind + and is mutually exclusive with `url`/`path`. `url` and `path` + make the modpack non-publishable to Modrinth. additionalProperties: false properties: version: @@ -378,12 +428,66 @@ $defs: - required - optional - unsupported - accepts-mc: + accepts_mc: $ref: "#/$defs/acceptsMc" - hosted: + modrinth: + type: string + description: | + Modrinth project slug for this entry, when it differs from + the section-map key. The section-map key is treated as the + local identifier; this field overrides the slug used to + query the Modrinth source. + minLength: 1 + curseforge: + type: string + description: | + CurseForge project slug or numeric project ID for this + entry. CurseForge resolution itself ships in a later part + of the bridge — the field is parsed today so manifests + can be authored ahead of resolver support. + minLength: 1 + sources: + description: | + Restrict this entry's resolution to a subset of platforms. + Accepts a single platform name or a list of unique platform + names from `{modrinth, curseforge}`. When omitted, the entry + falls back to the `gitrinth.modrinth` / `gitrinth.curseforge` + defaults. + oneOf: + - type: string + enum: [ modrinth, curseforge ] + - type: array + items: + type: string + enum: [ modrinth, curseforge ] + uniqueItems: true + minItems: 1 + modrinth_host: type: string format: uri - description: Base URL of a Modrinth-compatible server. + description: | + Per-entry override for the Modrinth source kind's base URL. + Co-exists with the Modrinth source — the entry still speaks + the Modrinth API, just against a different host. + hash_scan_depth: + type: integer + minimum: 0 + description: | + When both Modrinth and CurseForge participate for this entry, + how many additional version pairs beyond the newest pair to + scan looking for a SHA1 match before failing. `0` disables + the scan; the default is `10`. Mutually exclusive with a + single-element `sources:`. + no_cross_platform_search: + type: boolean + description: Disable cross-platform search fallback for this entry. + allow_hash_mismatch: + type: boolean + description: | + When both Modrinth and CurseForge participate for this entry + and no SHA1 match is found within the scan window, accept the + mismatch and lock the newest pair instead of failing. + Mutually exclusive with a single-element `sources:`. url: type: string format: uri @@ -394,11 +498,35 @@ $defs: minLength: 1 allOf: - not: - required: [ hosted, url ] + required: [ modrinth_host, url ] - not: - required: [ hosted, path ] + required: [ modrinth_host, path ] - not: required: [ url, path ] + # `hash_scan_depth` / `allow_hash_mismatch` only make sense when + # both platforms participate. A single-element `sources:` already + # pins the entry to one platform, so combining the fields is a + # parse-time error. + - not: + allOf: + - required: [ hash_scan_depth ] + - properties: + sources: + oneOf: + - type: string + - type: array + maxItems: 1 + required: [ sources ] + - not: + allOf: + - required: [ allow_hash_mismatch ] + - properties: + sources: + oneOf: + - type: string + - type: array + maxItems: 1 + required: [ sources ] modMap: type: object diff --git a/assets/schema/project-overrides.schema.yaml b/assets/schema/project-overrides.schema.yaml index 8f1f99c..ffb6fb1 100644 --- a/assets/schema/project-overrides.schema.yaml +++ b/assets/schema/project-overrides.schema.yaml @@ -15,11 +15,12 @@ properties: project_overrides: description: | Map of Modrinth project slug to mod dependency (short or long - form). Pins a version, flips per-side install state, or - redirects an entry to an alternate source (`hosted`, `url`, - `path`). An entry here forces its version to be used regardless - of other mods' constraints or `incompatible:` declarations. - Same shape as `project_overrides` in mods.yaml. + form). Pins a version, flips per-side install state, redirects + the Modrinth source to a labrinth deployment via + `modrinth_host`, or swaps it out for a `url:` / `path:` source. + An entry here forces its version to be used regardless of other + mods' constraints or `incompatible:` declarations. Same shape + as `project_overrides` in mods.yaml. oneOf: - type: "null" - $ref: https://gitrinth.dev/schemas/mods.schema.yaml#/$defs/modMap diff --git a/assets/template/mods.yaml b/assets/template/mods.yaml index 618f2a4..19d80eb 100644 --- a/assets/template/mods.yaml +++ b/assets/template/mods.yaml @@ -5,10 +5,10 @@ description: {{description}} loader: {{loader-block}} -mc-version: {{mc-version}} +mc_version: {{mc_version}} -tooling: - gitrinth: ">={{gitrinth-version}} <{{gitrinth-next-major}}" +gitrinth: + version: ">={{gitrinth-version}} <{{gitrinth-next-major}}" mods: globalpacks: stable diff --git a/build.yaml b/build.yaml index d389e39..c914d29 100644 --- a/build.yaml +++ b/build.yaml @@ -27,4 +27,5 @@ targets: retrofit_generator: enabled: true generate_for: - - lib/src/service/modrinth_api.dart + - lib/src/service/modrinth/modrinth_api.dart + - lib/src/service/curseforge/curseforge_api.dart diff --git a/docs/cli.md b/docs/cli.md index ae7d671..5f94735 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -21,34 +21,51 @@ Use [`--directory`](#global-options) to target a different modpack. ## Global options -| Option | Description | -|----------------------------|-----------------------------------------------------------------------------------------| -| `-h`, `--help` | Print usage. After a command name, prints that command's usage. | -| `--version` | Print the `gitrinth` version and exit. | +| Option | Description | +|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------| +| `-h`, `--help` | Print usage. After a command name, prints that command's usage. | +| `--version` | Print the `gitrinth` version and exit. | | `--verbosity ` | Set the output floor. One of `error`, `warning`, `normal` (default), `io`, `solver`, `all`. See [Verbosity levels](#verbosity-levels). | -| `-v`, `--verbose` | Shorthand for `--verbosity=all`. Mutually exclusive with `--quiet` and `--verbosity`. | -| `-q`, `--quiet` | Shorthand for `--verbosity=warning`. Mutually exclusive with `--verbose` and `--verbosity`. | -| `--color` / `--no-color` | Force ANSI colour on or off. Defaults to auto-detection (honours `NO_COLOR`). | -| `--config ` | Use an alternate user config file. Overrides `GITRINTH_CONFIG` and the platform default. | -| `-C`, `--directory ` | Run as if invoked from ``. | +| `-v`, `--verbose` | Shorthand for `--verbosity=all`. Mutually exclusive with `--quiet` and `--verbosity`. | +| `-q`, `--quiet` | Shorthand for `--verbosity=warning`. Mutually exclusive with `--verbose` and `--verbosity`. | +| `--color` / `--no-color` | Force ANSI colour on or off. Defaults to auto-detection (honours `NO_COLOR`). | +| `--config ` | Use an alternate user config file. Overrides `GITRINTH_CONFIG` and the platform default. | +| `-C`, `--directory ` | Run as if invoked from ``. | ## Commands +### Platform defaults and credentials + +`gitrinth modrinth enable|disable` and `gitrinth curseforge enable|disable` +edit the matching `gitrinth:` setting in `mods.yaml`, preserving its version +constraint and existing entries. They do not resolve or download artifacts. + +`gitrinth token add [--token ]`, `token list`, and +`token remove ` manage credentials. Without `--token`, add reads +a hidden prompt or piped stdin. Modrinth and labrinth URLs are validated against +their `/user` endpoint before storage. `gitrinth token add curseforge.com` +stores a CurseForge **publish** key without invoking a Modrinth endpoint; obtain +it from https://authors-old.curseforge.com/account/api-tokens. Listing masks +tokens and identifies environment overrides. `GITRINTH_CURSEFORGE_UPLOAD_TOKEN` +overrides the stored publish key. Downloads use the separate +`GITRINTH_CURSEFORGE_TOKEN` or embedded official-build key, never this stored key. +The existing `gitrinth modrinth token` commands remain available. + ### Dependencies -| Command | Purpose | -|---------------------------|-----------------------------------------------------------------| -| [`get`](#get) | Resolve entries, write `mods.lock`, download into the cache. | -| [`upgrade`](#upgrade) | Re-resolve to the newest version allowed by each constraint. | -| [`downgrade`](#downgrade) | Re-resolve to the oldest version allowed by each constraint. | +| Command | Purpose | +|---------------------------|------------------------------------------------------------------| +| [`get`](#get) | Resolve entries, write `mods.lock`, download into the cache. | +| [`upgrade`](#upgrade) | Re-resolve to the newest version allowed by each constraint. | +| [`downgrade`](#downgrade) | Re-resolve to the oldest version allowed by each constraint. | | [`outdated`](#outdated) | Report locked entries that are behind newer compatible versions. | -| [`deps`](#deps) | Print the resolved dependency tree. | -| [`migrate`](#migrate) | Re-target the pack to a new Minecraft version or loader. | -| [`add`](#add) | Add an entry to a section. | -| [`remove`](#remove) | Remove an entry. | -| [`override`](#override) | Add a sticky override to `project_overrides`. | -| [`pin`](#pin) | Freeze an entry to its currently-locked version. | -| [`unpin`](#unpin) | Restore a caret on a pinned entry. | +| [`deps`](#deps) | Print the resolved dependency tree. | +| [`migrate`](#migrate) | Re-target the pack to a new Minecraft version or loader. | +| [`add`](#add) | Add an entry to a section. | +| [`remove`](#remove) | Remove an entry. | +| [`override`](#override) | Add a sticky override to `project_overrides`. | +| [`pin`](#pin) | Freeze an entry to its currently-locked version. | +| [`unpin`](#unpin) | Restore a caret on a pinned entry. | ### Cache @@ -119,6 +136,22 @@ available versions. [`create`](#create) also accepts `--offline`, where it skips the slug-availability round-trip; that's the only network call `create` makes. +CurseForge offline resolution also requires the selected file's +`/curseforge///metadata.json` sidecar. +Missing or corrupt metadata fails before replacing `mods.lock`; run one +online `gitrinth get` to refresh it. Older locks still support read-only +inspection. + +## Loader descriptors + +Commands that use loader metadata accept `--loader-descriptors `. +The default is `loaders.yaml` beside the resolved user config; a repository +file is not loaded automatically. Loader IDs remain strings in manifests +and locks. Discovery can add provider tags and versions; packing requires +archive metadata, and build/launch require an installed runtime adapter. +Quilt has a built-in adapter. See [loader capabilities](loader-descriptors.md) +for configuration, offline snapshots, and supported strategy families. + ## Command details ### `get` @@ -128,6 +161,14 @@ Resolve every entry in `mods.yaml`, apply artifacts into the cache. Does not upgrade entries already locked to a satisfying version — use [`upgrade`](#upgrade) for that. +CurseForge files must be available and their final selected relationships +compatible. Listings query each accepted Minecraft version within the +10,000-result API window; oversized or incomplete histories fail explicitly. +For a file without a download URL, the error names its project/file IDs, +filename, and exact cache destination. Download that file manually there +and rerun `get`; the locked hash must match. Without a verifiable hash, +use a `path:` source. Cached peer-platform bytes do not satisfy this check. + ```text gitrinth get [--dry-run] [--enforce-lockfile] [--offline] ``` @@ -150,13 +191,13 @@ gitrinth upgrade [...] [--major-versions] [--tighten] [--unlock-transitive] [--dry-run] [--offline] ``` -| Option | Description | -|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `--major-versions` | Ignore caret boundaries and pick the absolute newest version. Rewrites the constraint in `mods.yaml`. | -| `--tighten` | After resolving, raise each caret-bound entry's lower bound in `mods.yaml` to match the resolved version. | -| `--unlock-transitive` | Also re-resolve every entry transitively reachable from the named targets via `mods.lock` dependency edges. | -| `--dry-run` | Print changes without writing. | -| `--offline` | Use cached versions only; do not hit the network. Resolution narrows to versions already in the cache. | +| Option | Description | +|-----------------------|-------------------------------------------------------------------------------------------------------------| +| `--major-versions` | Ignore caret boundaries and pick the absolute newest version. Rewrites the constraint in `mods.yaml`. | +| `--tighten` | After resolving, raise each caret-bound entry's lower bound in `mods.yaml` to match the resolved version. | +| `--unlock-transitive` | Also re-resolve every entry transitively reachable from the named targets via `mods.lock` dependency edges. | +| `--dry-run` | Print changes without writing. | +| `--offline` | Use cached versions only; do not hit the network. Resolution narrows to versions already in the cache. | When `--major-versions` is set, `upgrade` auto-recovers existing `gitrinth:disabled-by-conflict` markers (set by an earlier `migrate`): @@ -219,12 +260,12 @@ newer version. Read-only — never writes `mods.lock`. gitrinth outdated [--json] [--show-all] [--no-transitive] [--offline] ``` -| Option | Description | -|-------------------|--------------------------------------------------------------------------------------------------------| -| `--json` | Emit a machine-readable JSON report instead of the table. | -| `--show-all` | Include up-to-date entries in the report. Off by default. | -| `--no-transitive` | Hide entries whose `dependency:` is `transitive`. On by default (transitives shown). | -| `--offline` | Use cached versions only; do not hit the network. | +| Option | Description | +|-------------------|--------------------------------------------------------------------------------------| +| `--json` | Emit a machine-readable JSON report instead of the table. | +| `--show-all` | Include up-to-date entries in the report. Off by default. | +| `--no-transitive` | Hide entries whose `dependency:` is `transitive`. On by default (transitives shown). | +| `--offline` | Use cached versions only; do not hit the network. | The report has three columns: @@ -264,16 +305,16 @@ gitrinth deps [] [--style ] [--env ] [--json] ``` -| Option | Description | -|--------------|----------------------------------------------------------------------------| -| `` | Limit output to a single direct entry and its transitive dependencies. | -| `-s, --style`| Output style: `compact`, `tree` (default), or `list`. | -| `--env` | Filter by [`environment`](mods-yaml.md#per-mod-environment). Default `both`. | -| `--json` | Emit a machine-readable JSON report. Mutually exclusive with `--style`. | +| Option | Description | +|---------------|------------------------------------------------------------------------------| +| `` | Limit output to a single direct entry and its transitive dependencies. | +| `-s, --style` | Output style: `compact`, `tree` (default), or `list`. | +| `--env` | Filter by [`environment`](mods-yaml.md#per-mod-environment). Default `both`. | +| `--json` | Emit a machine-readable JSON report. Mutually exclusive with `--style`. | -`tree` style indents children with `├── └── │ ` connectors when +`tree` style indents children with `├── └── │` connectors when the terminal supports unicode/ANSI; falls back to ASCII (`|-- -'-- | `) otherwise. Slugs that appear more than once in the +'-- |`) otherwise. Slugs that appear more than once in the graph (typical for shared transitives) render as `...` in gray on second and later occurrences. @@ -332,6 +373,22 @@ non-zero. ### `add` +Platform entries resolve against the enabled, eligible platforms. Modrinth is +enabled by default; CurseForge is disabled. `--modrinth`, `--no-modrinth`, +`--curseforge`, and `--no-curseforge` override those defaults for the new entry. +Explicit choices persist as `sources:` restrictions. An automatic dual-platform +add writes shorthand when hashes align, or a one-platform restriction when only +one requested platform has a compatible release. A hash mismatch fails before +writing; `--allow-hash-mismatch` persists `allow_hash_mismatch: true` to accept it. +`--no-search` suppresses peer discovery for both preview and final resolution. + +`gitrinth add cf:[@]` selects CurseForge alone without changing +the pack defaults. All sections support this syntax; CurseForge plugin entries +require Bukkit, Spigot, or Paper. Use `--type plugin` to select the plugin section +in a pack that also has mods. Sponge and Folia reject explicit CurseForge plugin +requests before querying the network. The Modrinth side uses the pack's +`modrinth_host`, including cross-platform hash comparison on hosted servers. + Add an entry to [`mods`](mods-yaml.md#mods), [`resource_packs`](mods-yaml.md#resource_packs), [`data_packs`](mods-yaml.md#data_packs), @@ -341,7 +398,7 @@ Add an entry to [`mods`](mods-yaml.md#mods), ```text gitrinth add [@] [--env ] [--url | --path ] - [--type ] + [--type ] [--accepts-mc ]... [--exact | --pin] [--dry-run] [--offline] ``` @@ -367,9 +424,9 @@ whose filename doesn't uniquely identify a type (non-`.jar` files). `--accepts-mc ` widens the Modrinth `game_versions` query when picking a default constraint, and persists the list under -[`accepts-mc`](mods-yaml.md#per-entry-mc-version-tolerance-accepts-mc) +[`accepts_mc`](mods-yaml.md#per-entry-mc-version-tolerance-accepts_mc) on the new entry (forces long form). Use when the mod works on the -pack's [`mc-version`](mods-yaml.md#mc-version) but the author tagged +pack's [`mc_version`](mods-yaml.md#mc_version) but the author tagged only adjacent versions on Modrinth. Incompatible with `--url` / `--path`. @@ -433,21 +490,21 @@ want a particular mod in the pack despite a declared incompatibility. gitrinth override [@] [arguments] ``` -| Option | Description | -|--------|-------------| -| `--env client\|server\|both` | Restrict the override to a side. | -| `--url ` | Use a `url:` source. | -| `--path ` | Use a `path:` source. | +| Option | Description | +|----------------------------------------------|------------------------------------------------------------------------------------------------------------------------| +| `--env client\|server\|both` | Restrict the override to a side. | +| `--url ` | Use a `url:` source. | +| `--path ` | Use a `path:` source. | | `--type mod\|resourcepack\|datapack\|shader` | Force the section used for loader filtering. Useful when the slug is purely transitive and section inference is wrong. | -| `--accepts-mc ` | Additively widen the Minecraft version filter for this override (repeatable). | -| `--exact` | Caret-pin including build metadata. | -| `--pin` | Bare-version pin. | -| `--standalone` | Write to `project_overrides.yaml` instead of `mods.yaml`. Creates the file if it doesn't exist. | -| `--dry-run` | Report what would change without writing or resolving. | -| `--offline` | Skip Modrinth queries; use the cache only. | +| `--accepts-mc ` | Additively widen the Minecraft version filter for this override (repeatable). | +| `--exact` | Caret-pin including build metadata. | +| `--pin` | Bare-version pin. | +| `--standalone` | Write to `project_overrides.yaml` instead of `mods.yaml`. Creates the file if it doesn't exist. | +| `--dry-run` | Report what would change without writing or resolving. | +| `--offline` | Skip Modrinth queries; use the cache only. | With no `@`, `override` picks the latest release matching -loader and `mc-version`. The override version is sticky — other mods' +loader and `mc_version`. The override version is sticky — other mods' constraints on the same slug are silently ignored. `override` does **not** run the incompatibility-prevention check @@ -469,10 +526,10 @@ gitrinth pin [--type ] [--dry-run] ``` -| Option | Description | -|-------------|------------------------------------------------------------------------------| -| `--type` | Disambiguate `` when it lives in multiple sections of `mods.yaml`. | -| `--dry-run` | Print the edit without writing. | +| Option | Description | +|-------------|--------------------------------------------------------------------------| +| `--type` | Disambiguate `` when it lives in multiple sections of `mods.yaml`. | +| `--dry-run` | Print the edit without writing. | Only applies to Modrinth-sourced entries — `url:` / `path:` entries have no semver to pin. Does not re-resolve: the bare version still @@ -489,10 +546,10 @@ gitrinth unpin [--type ] [--dry-run] ``` -| Option | Description | -|-------------|------------------------------------------------------------------------------| -| `--type` | Disambiguate `` when it lives in multiple sections of `mods.yaml`. | -| `--dry-run` | Print the edit without writing. | +| Option | Description | +|-------------|--------------------------------------------------------------------------| +| `--type` | Disambiguate `` when it lives in multiple sections of `mods.yaml`. | +| `--dry-run` | Print the edit without writing. | Errors if the constraint already has a caret, is a channel token (`release`, `beta`, `alpha`), or isn't semver-shaped. @@ -520,15 +577,15 @@ slug must be 3–64 characters from Modrinth's allowed set (`a-zA-Z0-9!@$()` `` ` `` `.+,_"-`); pass `--slug` to override the derived value. -| Option | Description | -|-------------------|-------------------------------------------------------------------------------------------------------------| -| `--mod-loader` | Pre-fill [`loader.mods`](mods-yaml.md#loader). Defaults to `neoforge` when no plugin loader is specified. | +| Option | Description | +|-------------------|-----------------------------------------------------------------------------------------------------------| +| `--mod-loader` | Pre-fill [`loader.mods`](mods-yaml.md#loader). Defaults to `neoforge` when no plugin loader is specified. | | `--plugin-loader` | Pre-fill [`loader.plugins`](mods-yaml.md#plugin-loaders). Omit `--mod-loader` for a plugin-only scaffold. | -| `--mc-version` | Pre-fill [`mc-version`](mods-yaml.md#mc-version). Defaults to `1.21.1`. | -| `--slug` | Override the derived slug. | -| `--name` | Override the display [`name`](mods-yaml.md#name). | -| `--force` | Allow scaffolding into a non-empty directory; overwrites existing `mods.yaml`. | -| `--offline` | Skip the Modrinth slug-availability check. | +| `--mc-version` | Pre-fill [`mc_version`](mods-yaml.md#mc_version). Defaults to `1.21.1`. | +| `--slug` | Override the derived slug. | +| `--name` | Override the display [`name`](mods-yaml.md#name). | +| `--force` | Allow scaffolding into a non-empty directory; overwrites existing `mods.yaml`. | +| `--offline` | Skip the Modrinth slug-availability check. | Refuses to run when `` exists and is non-empty without `--force`. @@ -561,10 +618,10 @@ description: A new Modrinth modpack. loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 -tooling: - gitrinth: ">=0.1.0 <1.0.0" +gitrinth: + version: ">=0.1.0 <1.0.0" mods: @@ -585,7 +642,7 @@ or server. The server distribution includes the matching server binary for [`loader`](mods-yaml.md#loader) and -[`mc-version`](mods-yaml.md#mc-version), fetched and installed +[`mc_version`](mods-yaml.md#mc_version), fetched and installed automatically from the upstream Maven repositories. Fabric drops in `fabric-server-launch.jar`; Forge and NeoForge run their official installer in `--installServer` mode against `build/server/`, so the @@ -638,7 +695,7 @@ binary path and install marker. When neither `loader.plugins` nor a real `loader.mods` is declared (pure-vanilla pack), `gitrinth build server` downloads the official -Mojang `server.jar` for the pack's `mc-version` from +Mojang `server.jar` for the pack's `mc_version` from `piston-meta.mojang.com`. #### Prune behavior @@ -709,17 +766,18 @@ Non-existent paths are a silent no-op. ### `pack` -Modpack-specific. Produce Modrinth `.mrpack` archives. +Modpack-specific. Produce Modrinth `.mrpack` archives or CurseForge `.zip` archives. ```text -gitrinth pack [--output ] [--combined] [--publishable] [--offline] +gitrinth pack [--output ] [--combined] [--curseforge] [--server-distribution] [--publishable] [--offline] ``` | Option | Description | |------------------|---------------------------------------------------------------------------------------------------------------------------------------------------| | `--output`, `-o` | Override the base output path. Defaults to `./build/-.mrpack`. The server pack is derived by inserting `-server` before `.mrpack`. | | `--combined` | Produce a single `.mrpack` that contains both client and server files instead of a client + server pair. | -| `--publishable` | Refuse to pack if any [`mods`](mods-yaml.md#mods) entry uses a `url` or `path` source. Other sections are unaffected. | +| `--publishable` | Refuse entries unavailable on the selected platform or marked `allow_hash_mismatch: true`, scoped to the side being built. | +| `--curseforge` | Write CurseForge `manifest.json` plus `overrides/` in `.zip` archives. | | `--offline` | Use cached versions only; do not hit the network. Resolution narrows to versions already in the cache. | By default `pack` produces two artifacts: @@ -750,11 +808,25 @@ written, containing every file partitioned by the per-file `env` map. list because every entry there needs a Modrinth CDN URL, so they are packed as loose files under `overrides//` (routed to `client-overrides/` / `server-overrides/` when their `environment` -narrows the side). `--publishable` gates this for the -[`mods`](mods-yaml.md#mods) section only, since per Modrinth's policy -executable mod artifacts require explicit author permission to -redistribute. Resource packs, data packs, and shaders remain free to -bundle non-Modrinth artifacts even with `--publishable`. +narrows the side). Entries missing the selected target platform are +also bundled loose, with author permission. `--publishable` rejects +these entries across all content sections and also rejects entries +marked `allow_hash_mismatch: true`. + +`--curseforge` changes the extension to `.zip`, writes CurseForge +project/file references, and routes loose files through `overrides/`. +Side filtering still produces client/server archives by default. +Sponge and Folia plugins cannot be CurseForge project references; +their loose overrides retain the `plugins/` path. + +`pack --curseforge --server-distribution` instead writes +`build/--server-distribution.zip` with actual server-side +artifacts and configs at their installation paths. The legacy `-server.zip` +contains a CurseForge manifest and remains available through the default +`pack --curseforge` invocation. The distribution excludes client-only files, +private cache/credential paths, and EULA acceptance files. Explicit CurseForge +redistribution denial prevents bundling those bytes. This mode does not bundle +a runtime or start a server; install the matching runtime separately. [`files:`](mods-yaml.md#files) entries are bundled the same way: the destination key carries the full sub-path (e.g. @@ -789,16 +861,16 @@ gitrinth launch server [--accept-eula] [--no-build] [--memory ] [--offline] [-- ] ``` -| Option | Description | -|---------------------|--------------------------------------------------------------------------------------------------------| -| `--accept-eula` | Write `eula=true` into `build/server/eula.txt` before starting. You agree to the Mojang EULA. | -| `--no-build` | Skip the implicit `gitrinth build server`. Use when the build tree is already up to date. | -| `--memory`, `-m` | JVM heap size, applied as `-Xmx`/`-Xms`. Examples: `2G`, `4G`, `6144M`. Defaults to `2G`. | -| `--output`, `-o` | Override the build output directory. Defaults to `./build`. | -| `--java` | `java` binary or JDK home. See [Java runtime selection](#java-runtime-selection). | -| `--no-managed-java` | Refuse the auto-download fallback. Requires `--java` or a satisfying JAVA_HOME / PATH-`java`. | -| `--offline` | Forwarded to the auto-build step and to the JDK auto-download fallback (both refuse network). | -| `-- ` | Trailing args after `--` are appended to the server JVM/script invocation (e.g. `-- --port 25566`). | +| Option | Description | +|---------------------|-----------------------------------------------------------------------------------------------------| +| `--accept-eula` | Write `eula=true` into `build/server/eula.txt` before starting. You agree to the Mojang EULA. | +| `--no-build` | Skip the implicit `gitrinth build server`. Use when the build tree is already up to date. | +| `--memory`, `-m` | JVM heap size, applied as `-Xmx`/`-Xms`. Examples: `2G`, `4G`, `6144M`. Defaults to `2G`. | +| `--output`, `-o` | Override the build output directory. Defaults to `./build`. | +| `--java` | `java` binary or JDK home. See [Java runtime selection](#java-runtime-selection). | +| `--no-managed-java` | Refuse the auto-download fallback. Requires `--java` or a satisfying JAVA_HOME / PATH-`java`. | +| `--offline` | Forwarded to the auto-build step and to the JDK auto-download fallback (both refuse network). | +| `-- ` | Trailing args after `--` are appended to the server JVM/script invocation (e.g. `-- --port 25566`). | `launch server` reads `mods.lock` to pick the right command per loader: @@ -819,13 +891,13 @@ gitrinth launch client [--no-build] [--output ] [--java ] [--no-managed-java] [--offline] ``` -| Option | Description | -|---------------------|-------------------------------------------------------------------------------------------------| -| `--no-build` | Skip the implicit `gitrinth build client`. Use when the build tree is already up to date. | -| `--output`, `-o` | Override the build output directory. Defaults to `./build`. | -| `--java` | `java` binary or JDK home. See [Java runtime selection](#java-runtime-selection). | -| `--no-managed-java` | Refuse the auto-download fallback. Requires `--java` or a satisfying JAVA_HOME / PATH-`java`. | -| `--offline` | Rejected — the launcher needs network on first run to download libraries and assets. | +| Option | Description | +|---------------------|-----------------------------------------------------------------------------------------------| +| `--no-build` | Skip the implicit `gitrinth build client`. Use when the build tree is already up to date. | +| `--output`, `-o` | Override the build output directory. Defaults to `./build`. | +| `--java` | `java` binary or JDK home. See [Java runtime selection](#java-runtime-selection). | +| `--no-managed-java` | Refuse the auto-download fallback. Requires `--java` or a satisfying JAVA_HOME / PATH-`java`. | +| `--offline` | Rejected — the launcher needs network on first run to download libraries and assets. | Each modpack gets its own `.minecraft`-shaped workdir under the gitrinth cache at `~/.gitrinth_cache/launchers//`. The artifact @@ -892,7 +964,7 @@ or `:` elsewhere) — useful for portable installs and CI. `launch server`, `launch client`, and `build server` all need a JVM — the server's launch script, the loader installer, and the Forge/NeoForge `--installServer` step. gitrinth picks one automatically -based on the modpack's `mc-version` so you don't have to install or +based on the modpack's `mc_version` so you don't have to install or switch JDKs yourself. ##### Required JDK feature per Minecraft version @@ -972,9 +1044,9 @@ an invalid token never lands on disk. gitrinth modrinth login [--token ] ``` -| Option | Description | -|----------------|---------------------------------------------------------------------------------------------------| -| `--token `| Pass the PAT directly. Designed for headless / CI use; falls back to a hidden stdin prompt when omitted. The prompted form also accepts piped stdin (e.g. `echo $TOKEN | gitrinth modrinth login`). | +| Option | Description | +|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `--token ` | Pass the PAT directly. Designed for headless / CI use; falls back to a hidden stdin prompt when omitted. The prompted form also accepts piped stdin (e.g. `echo $TOKEN \| gitrinth modrinth login`). | The PAT for `gitrinth modrinth publish` must include these labrinth scopes: `USER_READ`, `PROJECT_READ`, `VERSION_CREATE`. Generate one @@ -1011,11 +1083,11 @@ gitrinth modrinth token list gitrinth modrinth token remove ``` -| Subcommand | Description | -|---------------------|----------------------------------------------------------------------------------------------| -| `add ` | Validate against `/user` and store the PAT under the normalized URL key. | -| `list` | Print every stored host with its token masked (first / last 4 chars, middle elided). | -| `remove `| Clear the entry. Errors when no token is stored under that key. | +| Subcommand | Description | +|-----------------------|--------------------------------------------------------------------------------------| +| `add ` | Validate against `/user` and store the PAT under the normalized URL key. | +| `list` | Print every stored host with its token masked (first / last 4 chars, middle elided). | +| `remove ` | Clear the entry. Errors when no token is stored under that key. | Server URLs are normalized to scheme + host + port + path before being used as map keys, so `https://my.host/api` and `HTTPS://my.host/api/` @@ -1034,16 +1106,16 @@ gitrinth modrinth publish [--dry-run] [--force] [--draft] [--changelog ] [--pack ] ``` -| Option | Description | -|--------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------| -| `--dry-run` | Hash the artifact and emit the JSON payload without uploading. | -| `-f`, `--force` | Skip the interactive `Publish v to ? [y/N]` confirmation. Required when stdin is not a terminal. | -| `--draft` | Set `status: draft` instead of `status: listed` on the new version. | -| `--version-type ` | Modrinth version channel. Defaults to `beta` when `mods.yaml.version` carries a pre-release suffix (e.g. `1.0.0-beta.2`), else `release`. | -| `--changelog ` | Markdown changelog. Defaults to the matching `## []` section of `CHANGELOG.md` in the project directory; absent → empty changelog.| -| `--pack ` | Override the `.mrpack` location. Defaults to `build/-.mrpack` (the path produced by `gitrinth pack`). | +| Option | Description | +|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------| +| `--dry-run` | Hash the artifact and emit the JSON payload without uploading. | +| `-f`, `--force` | Skip the interactive `Publish v to ? [y/N]` confirmation. Required when stdin is not a terminal. | +| `--draft` | Set `status: draft` instead of `status: listed` on the new version. | +| `--version-type ` | Modrinth version channel. Defaults to `beta` when `mods.yaml.version` carries a pre-release suffix (e.g. `1.0.0-beta.2`), else `release`. | +| `--changelog ` | Markdown changelog. Defaults to the matching `## []` section of `CHANGELOG.md` in the project directory; absent → empty changelog. | +| `--pack ` | Override the `.mrpack` location. Defaults to `build/-.mrpack` (the path produced by `gitrinth pack`). | -The target server is resolved from `mods.yaml.publish_to` if set, +The target server is resolved from `mods.yaml.publish_to.modrinth` if set, otherwise from `GITRINTH_MODRINTH_URL` / the built-in default. A `publish_to: 'none'` disables publishing for the pack. @@ -1052,6 +1124,84 @@ CLI (`GITRINTH_TOKEN` → stored token for the resolved host). If neither source has a token, `publish` exits with code `4` before hitting the network. +### `publish` + +Publish built artifacts to every platform selected by +[`publish_to`](mods-yaml.md#publish_to), or the enabled `gitrinth` +platforms when that field is omitted. Build each target with `pack` +first. Credentials and artifact paths are checked for every target +before the first upload; a remote failure can still leave an earlier +target successfully published. + +```text +gitrinth publish --dry-run --curseforge-project 12345 +gitrinth publish --force --curseforge-project 12345 +gitrinth curseforge publish --force --curseforge-project 12345 +``` + +Common flags match `modrinth publish`. `--curseforge-project` supplies +the numeric project ID required by CurseForge. The default CurseForge +artifact is `build/-.zip`; `--pack` overrides the path for +a single target, and `--curseforge-pack` overrides the ZIP path when +publishing both. `--publishable` rejects target-missing entries and +hash-mismatch overrides using `mods.lock` (required with this flag). +Without it, publishing warns about content bundled as loose overrides. + +CurseForge authenticates with `GITRINTH_CURSEFORGE_UPLOAD_TOKEN`, falling +back to `gitrinth token add curseforge.com`. Its resolver download key +cannot authorize uploads. `--draft` requests manual release after +CurseForge approval. `--dry-run` assembles metadata without upload. + +To publish a client release with its actual server distribution: + +```console +$ gitrinth pack --curseforge +$ gitrinth pack --curseforge --server-distribution +$ gitrinth curseforge publish --with-server --curseforge-project 12345 --force +``` + +`--with-server` prevalidates both ZIPs, uploads the client first, then uses +its returned file ID as the server file's `parentFileID`. Override the server +path with `--curseforge-server-pack `; the default is +`build/--server-distribution.zip`. A missing or modified server +distribution fails preflight. A legacy manifest-server ZIP is rejected. +`--draft` deliberately applies manual-release marking to both uploads. + +The documented upload API creates a generic additional-file association. +Unless read-back confirms both the child's server-pack flag and its parent's +server-pack file ID, the command reports **classification pending**. Open the +matching client release in the CurseForge author console and classify the +additional file as its server pack. Rerun the same command to verify; completed +uploads are reused. Moderation visibility can keep classification pending. + +If the client succeeds and the server fails, the command exits nonzero, prints +the surviving IDs, and supplies an exact server-only retry: + +```console +$ gitrinth curseforge publish --server-only --curseforge-project 12345 --curseforge-parent-file 67890 --force +``` + +The printed command includes the absolute project `-C` path and preserves +explicit `--config` and `--loader-descriptors` paths, so it can be pasted from +the original working directory. It contains paths and IDs, never token values. + +The explicit parent must belong to the project and match the intended client +release display name (` `) and channel. Keep the original +`--version-type` and `--draft` settings. Server-only retries do not upload a +client or Modrinth release; use the `curseforge publish` command when +`publish_to` selects both platforms. + +Token-free receipts live under +`.gitrinth/publications/curseforge//.json`; this tree is +ignored by Git. They bind artifact SHA256 hashes and per-step outcomes to the +project, release, and endpoint. Changed artifacts require a new release rather +than silently replacing an existing receipt. In-flight, timeout, and invalid-ID +outcomes are unknown: inspect remote files before retrying. A server retry +proceeds only after a unique remote file matches its parent and artifact hash, +or the missing client outcome is supplied as an explicit verified parent ID. +CurseForge upload POSTs are not retried automatically. A dry run prints the dependent upload plan +without creating receipts or remote files. + ### `completion` Emit a shell-completion script for the given shell to stdout. No @@ -1089,38 +1239,42 @@ want a particular mod in the pack despite a declared incompatibility. ## Exit codes -| Code | Meaning | -|------|----------------------------------------------------------------------------------------| -| `0` | Success. | -| `1` | Recoverable user-facing error. | -| `2` | Validation or resolution failed. | -| `4` | Authentication failure — no PAT configured, or upstream rejected the supplied token. | -| `5` | Cache corruption `cache repair` could not fix. | -| `64` | Usage error — matches `sysexits.h` `EX_USAGE`. | +| Code | Meaning | +|------|--------------------------------------------------------------------------------------| +| `0` | Success. | +| `1` | Recoverable user-facing error. | +| `2` | Validation or resolution failed. | +| `4` | Authentication failure — no PAT configured, or upstream rejected the supplied token. | +| `5` | Cache corruption `cache repair` could not fix. | +| `64` | Usage error — matches `sysexits.h` `EX_USAGE`. | ## Environment variables -| Variable | Used by | Purpose | -|-----------------------------------------|--------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `GITRINTH_CACHE` | every command | Override the cache root. Defaults to `/.gitrinth/cache`. | -| `GITRINTH_CONFIG` | every command | Override the user config file path. `--config` wins. | -| `GITRINTH_MODRINTH_URL` | every command | Override the default Modrinth API base URL. | -| `GITRINTH_TOKEN` | every command | Override the stored Modrinth PAT for the *default* host. Sent bare (no `Bearer` prefix) per the Modrinth auth doc. Other hosts always use the stored token. | -| `HOME` / `USERPROFILE` | every command | Resolves the default cache root and user config path. `USERPROFILE` on Windows; `HOME` elsewhere. | -| `JAVA_HOME` | `launch` / `build` | Honored by the Java resolver if its major version matches the JDK required for the pack's `mc-version`. | -| `GITRINTH_JAVA_METADATA_URL` | `launch` / `build` | Override Adoptium metadata URL. | -| `GITRINTH_FABRIC_META_URL` | every command | Override the fabric-meta loader-list URL. | -| `GITRINTH_FORGE_PROMOTIONS_URL` | every command | Override the Forge `promotions_slim.json` URL. | -| `GITRINTH_FORGE_VERSIONS_URL` | every command | Override the Forge `maven-metadata.json` URL. | -| `GITRINTH_FORGE_INSTALLER_URL` | `build` / `launch` | Override the Forge installer-jar URL template (`{mc}` / `{v}` placeholders). | -| `GITRINTH_NEOFORGE_VERSIONS_URL` | every command | Override the modern NeoForge versions endpoint (MC ≥ 1.20.2). | -| `GITRINTH_NEOFORGE_LEGACY_VERSIONS_URL` | every command | Override the legacy NeoForge versions endpoint (MC 1.20.1). | -| `GITRINTH_NEOFORGE_INSTALLER_URL` | `build` / `launch` | Override the modern NeoForge installer-jar URL template. | -| `GITRINTH_NEOFORGE_LEGACY_INSTALLER_URL`| `build` / `launch` | Override the legacy NeoForge installer-jar URL template. | -| `GITRINTH_LAUNCHER` | `launch client` | Absolute path to the Minecraft Launcher executable; bypasses per-OS search. | -| `GITRINTH_LAUNCHER_SEARCH_PATHS` | `launch client` | Custom search paths for the Minecraft Launcher (`;`-separated on Windows, `:`-separated elsewhere). Tried before the per-OS defaults. | -| `NO_COLOR` | every command | Disable ANSI colour. `--color` overrides; `--no-color` matches. | -| `HTTPS_PROXY` / `HTTP_PROXY` | every command | Standard proxy variables, honoured by every HTTP request. | +| Variable | Used by | Purpose | +|-----------------------------------------------|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `GITRINTH_CACHE` | every command | Override the cache root. Defaults to `/.gitrinth/cache`. | +| `GITRINTH_CONFIG` | every command | Override the user config file path. `--config` wins. | +| `GITRINTH_MODRINTH_URL` | every command | Override the default Modrinth API base URL. | +| `GITRINTH_TOKEN` | every command | Override the stored Modrinth PAT for the *default* host. Sent bare (no `Bearer` prefix) per the Modrinth auth doc. Other hosts always use the stored token. | +| `GITRINTH_CURSEFORGE_URL` | every command | Override the CurseForge download API base URL. Defaults to `https://api.curseforge.com`. | +| `GITRINTH_CURSEFORGE_TOKEN` | every command | Override the default CurseForge download API key. This is separate from the publish key stored by `gitrinth token add curseforge.com`. | +| `GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64` | build-time | Base64-encoded default CurseForge download API key embedded into official builds. | +| `GITRINTH_CURSEFORGE_UPLOAD_TOKEN` | `publish` | Override the stored CurseForge publish key. Falls back to the key stored by `gitrinth token add curseforge.com`. | +| `HOME` / `USERPROFILE` | every command | Resolves the default cache root and user config path. `USERPROFILE` on Windows; `HOME` elsewhere. | +| `JAVA_HOME` | `launch` / `build` | Honored by the Java resolver if its major version matches the JDK required for the pack's `mc_version`. | +| `GITRINTH_JAVA_METADATA_URL` | `launch` / `build` | Override Adoptium metadata URL. | +| `GITRINTH_FABRIC_META_URL` | every command | Override the fabric-meta loader-list URL. | +| `GITRINTH_FORGE_PROMOTIONS_URL` | every command | Override the Forge `promotions_slim.json` URL. | +| `GITRINTH_FORGE_VERSIONS_URL` | every command | Override the Forge `maven-metadata.json` URL. | +| `GITRINTH_FORGE_INSTALLER_URL` | `build` / `launch` | Override the Forge installer-jar URL template (`{mc}` / `{v}` placeholders). | +| `GITRINTH_NEOFORGE_VERSIONS_URL` | every command | Override the modern NeoForge versions endpoint (MC ≥ 1.20.2). | +| `GITRINTH_NEOFORGE_LEGACY_VERSIONS_URL` | every command | Override the legacy NeoForge versions endpoint (MC 1.20.1). | +| `GITRINTH_NEOFORGE_INSTALLER_URL` | `build` / `launch` | Override the modern NeoForge installer-jar URL template. | +| `GITRINTH_NEOFORGE_LEGACY_INSTALLER_URL` | `build` / `launch` | Override the legacy NeoForge installer-jar URL template. | +| `GITRINTH_LAUNCHER` | `launch client` | Absolute path to the Minecraft Launcher executable; bypasses per-OS search. | +| `GITRINTH_LAUNCHER_SEARCH_PATHS` | `launch client` | Custom search paths for the Minecraft Launcher (`;`-separated on Windows, `:`-separated elsewhere). Tried before the per-OS defaults. | +| `NO_COLOR` | every command | Disable ANSI colour. `--color` overrides; `--no-color` matches. | +| `HTTPS_PROXY` / `HTTP_PROXY` | every command | Standard proxy variables, honoured by every HTTP request. | ## Shell completion @@ -1172,19 +1326,19 @@ gitrinth completion powershell >> $PROFILE ## Files -| Path | Purpose | -|-----------------------------------|----------------------------------------------------------------------------------------| -| `./mods.yaml` | Modpack manifest. See [`mods.yaml`](mods-yaml.md). | +| Path | Purpose | +|-----------------------------------|-------------------------------------------------------------------------------------------| +| `./mods.yaml` | Modpack manifest. See [`mods.yaml`](mods-yaml.md). | | `./project_overrides.yaml` | Optional standalone overrides. See [`project_overrides.yaml`](project-overrides-yaml.md). | -| `./mods.lock` | Resolved versions. Commit to git. | -| `./build/` | Default output directory for [`build`](#build). | -| `./build/-.mrpack` | Default output path for [`pack`](#pack). | -| `~/.gitrinth_cache/` | Cache root. `~` is `$HOME` (`$USERPROFILE` on Windows). Override via `GITRINTH_CACHE`. | +| `./mods.lock` | Resolved versions. Commit to git. | +| `./build/` | Default output directory for [`build`](#build). | +| `./build/-.mrpack` | Default output path for [`pack`](#pack). | +| `~/.gitrinth_cache/` | Cache root. `~` is `$HOME` (`$USERPROFILE` on Windows). Override via `GITRINTH_CACHE`. | ## Compatibility `gitrinth` follows semver for its CLI surface. -[`tooling.gitrinth`](mods-yaml.md#gitrinth) pins the expected +[`gitrinth.version`](mods-yaml.md#gitrinth) pins the expected compatibility window. ## See also diff --git a/docs/curseforge-bridge.md b/docs/curseforge-bridge.md index f5c7e4c..5862de3 100644 --- a/docs/curseforge-bridge.md +++ b/docs/curseforge-bridge.md @@ -28,105 +28,318 @@ CLI, and publishing parts depend on the CF client but can be built against fakes in parallel. The hosted-Modrinth slice can land first and independently. -- [ ] [Part 1: Foundation and hosted Modrinth](#part-1-foundation-and-hosted-modrinth) — normalize hyphenated manifest fields to underscores, rename schema/parser `hosted:` → `modrinth_host:`, drop the deferred-source guard, thread the host through `ModrinthApi`, wire tokens via `UserConfig.tokens[host]`, accept pack-level `modrinth_host:` as a default. -- [ ] [Part 2: Manifest source model](#part-2-manifest-source-model) — `curseforge:` / `modrinth:` peer fields, `sources: [...]` restriction (scalar or list), `tooling.modrinth` / `tooling.curseforge` defaults, `cf:` short-form sugar, section-aware source eligibility, plugin source limits. -- [ ] [Part 3: CurseForge API client](#part-3-curseforge-api-client) — typed client, token lookup for `curseforge.com`, content-type filters, loader/version filters, file/hash/dependency models, cache boundaries. -- [ ] [Part 4: Resolver and lockfile](#part-4-resolver-and-lockfile) — multi-source resolution, per-platform lock blocks, `not_found` markers, plugin lock behavior, cross-platform SHA1 verification, `hash_scan_depth`, `allow_hash_mismatch`. -- [ ] [Part 5: Search fallback](#part-5-search-fallback) — slug-not-found and hash-mismatch triggers, hash-first ranking, section-aware search filters, `no_cross_platform_search` / `--no-search` opt-outs. -- [ ] [Part 6: Transitive dependencies and deduplication](#part-6-transitive-dependencies-and-deduplication) — slug-table index, synthetic entries, cross-platform synthetic promotion, slug-divergence post-merge. -- [ ] [Part 7: CLI integration](#part-7-cli-integration) — `add` entry-write matrix, `--[no-]modrinth` / `--[no-]curseforge` / `--allow-hash-mismatch` flags, `cf:` handling, token commands. -- [ ] [Part 8: Pack and publish](#part-8-pack-and-publish) — `publish_to` target selector/config map, CF manifest emitter, `--curseforge` flag on `pack`, platform-aware `publish`, plugin publishing constraints. +- [x] [Part 1: Foundation and hosted Modrinth](#part-1-foundation-and-hosted-modrinth) — normalize hyphenated manifest fields to underscores, rename schema/parser `hosted:` → `modrinth_host:`, drop the deferred-source guard, thread the host through `ModrinthApi`, wire tokens via `UserConfig.tokens[host]`, accept pack-level `modrinth_host:` as a default. +- [x] [Part 2: Manifest source model](#part-2-manifest-source-model) — `curseforge:` / `modrinth:` peer fields, `sources: [...]` restriction (scalar or list), top-level `gitrinth:` block (with `gitrinth.version` semver constraint plus `gitrinth.modrinth` / `gitrinth.curseforge` defaults — replaces the old `tooling:` block), `cf:` short-form sugar, section-aware source eligibility, plugin source limits. +- [x] [Part 3: CurseForge API client](#part-3-curseforge-api-client) — typed client, env-only download key lookup, content-type filters, loader/version filters, file/hash/dependency models, cache boundaries. +- [x] [Part 4: Resolver and lockfile](#part-4-resolver-and-lockfile) — multi-source resolution, per-platform lock blocks, hard-fail on missing platforms (no `not_found` lockfile state), plugin lock behavior, cross-platform SHA1 verification, `hash_scan_depth`, `allow_hash_mismatch`. Includes a refactor of PubGrub to drop its Modrinth-specific imports so future cross-platform graph work can plug in. +- [x] [Part 5: Search fallback](#part-5-search-fallback) — slug-not-found and hash-mismatch triggers, hash-first ranking, section-aware search filters, `no_cross_platform_search` / `--no-search` opt-outs. +- [x] [Part 6: Transitive dependencies and deduplication](#part-6-transitive-dependencies-and-deduplication) — slug-table index, synthetic entries, cross-platform synthetic promotion, slug-divergence post-merge. +- [x] [Part 7: CLI integration](#part-7-cli-integration) — `add` entry-write matrix, `--[no-]modrinth` / `--[no-]curseforge` / `--allow-hash-mismatch` flags, `cf:` handling, token commands. +- [x] [Part 8: Pack and publish](#part-8-pack-and-publish) — `publish_to` target selector/config map, CF manifest emitter, `--curseforge` flag on `pack`, platform-aware `publish`, plugin publishing constraints. The [Mixing CF and Modrinth in one pack](#mixing-cf-and-modrinth-in-one-pack) section describes the resulting lockfile shape — it's not a task on its own, but a consolidated view of what the parts above produce. +### Resolution and manual recovery + +The bridge resolves, locks, downloads, assembles, and publishes packs. +CurseForge ZIP import, desktop integration, and general author metadata +editing remain separate features. + +File listings query each distinct `mc_version` / `accepts_mc` version, +retain the loader filter, and deduplicate file IDs before sorting. +A filtered history above 10,000 results fails explicitly; an incomplete +listing never counts as a complete candidate set. Authentication and +service failures retain their cause. Unavailable files cannot be selected, +and a project with unknown content type fails with its project identity. + +After cross-platform hash selection, incompatible CurseForge relations +are checked against the final numeric project identities, including local +aliases and transitive dependencies. Pin a compatible file explicitly; +the bridge does not search older files to solve incompatibilities. +`project_overrides` retain their explicit incompatibility bypass. +Optional, embedded, tool, and unknown relations do not install projects. + +An online resolve saves selected-file metadata at +`/curseforge///metadata.json` before replacing +`mods.lock`. Offline resolution requires this sidecar to validate the +selected file's availability and relationships. Older locks still parse; +run one online `gitrinth get` if their sidecars are missing or corrupt. +Read-only lock inspection and existing build artifacts remain usable. + +When a file has no download URL, download that exact file from CurseForge +and save it at the destination printed by the error: +`/curseforge///`. Rerun the command. +Only bytes matching the locked hash count as a cache hit, regardless of +the project's distribution setting. Missing or corrupt bytes fail; +without a verifiable hash, use an explicit `path:` source instead. +A cached Modrinth peer does not replace the required CurseForge artifact. +No CDN URL is reconstructed. Cache verification does not grant permission +to redistribute manually downloaded bytes. + ## Implementation plan +### Server distributions and parent-linked publication + +`pack --curseforge --server-distribution` produces actual server artifacts and +configs in `--server-distribution.zip`, with a versioned +`gitrinth-server-distribution.json` marker and SHA256 file inventory. The marker +preserves mod and plugin loader identities and concrete locked versions. +Client-only files, credential/cache paths, and EULA acceptance files are +excluded. A denied CurseForge redistribution flag blocks physical bundling. +The distribution does not include a runtime; metadata-only loaders can still +produce it. The existing `-server.zip` manifest-based archive remains available. + +`publish --with-server` uploads the client, requires its positive returned ID, +and uploads the server ZIP as an additional file with that exact `parentFileID`. +Child uploads omit `gameVersions` and `gameVersionNames`; channel and +manual-release settings are propagated deliberately. All selected artifacts, +credentials, descriptors, and existing receipts are checked before the first +upload. `--server-only --curseforge-parent-file ` retries against a verified +matching project and client release without reuploading the client or Modrinth. + +The public [upload API](https://support.curseforge.com/support/solutions/articles/9000197321-curseforge-upload-api) +does not document a server-classification switch. A generic parent association +is reported as pending manual classification in the author console until +read-back confirms `isServerPack` and the parent's `serverPackFileId`. No private +classification endpoint is used. Successful additional-file uploads can remain +pending while moderation/read visibility catches up. + +Atomic, token-free receipts under `.gitrinth/publications/curseforge/` record +artifact identities, returned IDs, dependent-step outcomes, and classification. +They preserve partial successes across retries and prevent duplicate POSTs +after an ambiguous timeout or crash. Unknown server outcomes require a unique +remote parent/hash match before proceeding; unknown client outcomes require an +explicit verified parent. Changed artifacts cannot resume the old receipt. +See [publish recovery](cli.md#publish) for flags and the tested retry workflow. + ### Part 1: Foundation and hosted Modrinth -Ship first because it is independently useful and forces the resolver -to stop assuming one global Modrinth host. Details live in -[Hosted Modrinth (labrinth)](#hosted-modrinth-labrinth). +Shipped. The foundational refactor lands the multi-host resolver, the +underscore field rename, and the `modrinth_host:` per-entry / pack +default. Details live in [Hosted Modrinth (labrinth)](#hosted-modrinth-labrinth). -Done means: +What landed: - `hosted:` is renamed to `modrinth_host:` in schema, parser, docs, - and examples. -- Existing hyphenated manifest fields are renamed to underscores for - consistency: `mc-version` → `mc_version`, `accepts-mc` → - `accepts_mc`, `modrinth-host` → `modrinth_host`, - `hash-scan-depth` → `hash_scan_depth`, - `allow-hash-mismatch` → `allow_hash_mismatch`, - `no-cross-platform-search` → `no_cross_platform_search`, - `discovered-via-search` → `discovered_via_search`, and - `required-by` → `required_by`. -- Pack-level and per-entry Modrinth host defaults are parsed. -- The resolver can obtain a host-scoped `ModrinthApi` and token. -- Existing default-host Modrinth behavior is unchanged. + and examples; the parser also accepts a top-level `modrinth_host:` + as the pack-wide default. +- Hyphenated manifest fields are normalized to underscore form: + `mc-version` → `mc_version`, `accepts-mc` → `accepts_mc`, + plus the lockfile-only fields `project-id` → `project_id`, + `version-id` → `version_id`, and `game-versions` → + `game_versions`. Old hyphenated keys stop parsing — fixtures and + example packs were updated in the same change. (The bridge spec's + remaining hyphenated fields — `hash-scan-depth`, + `allow-hash-mismatch`, `no-cross-platform-search`, + `discovered-via-search`, `required-by` — land underscored when their + parts ship.) +- The resolver dispatches against a host-keyed `ModrinthApiFactory` + with one `Dio` + rate-limit interceptor per host. The cache layout + is host-segmented: `////...` + (default host renders to a stable segment so the layout is + uniform). +- `ModrinthAuthInterceptor` already handled per-host token lookup; + the factory wires one auth interceptor per host instance so + `GITRINTH_TOKEN` continues to apply only to the default host. +- Default-host Modrinth behavior is unchanged on the wire — the + cache layout is a one-time break (existing caches are re-fetched + on first run after upgrade). ### Part 2: Manifest source model -Add the cross-platform source grammar before adding live CF behavior. -Details live in [Fetching mods](#fetching-mods) and -[Host overrides](#host-overrides). - -Done means: - -- Entries accept `modrinth:`, `curseforge:`, and `sources:`. -- `sources:` normalizes scalar and list forms into one internal set. -- `tooling.modrinth` and `tooling.curseforge` toggle default - participation for each platform; Modrinth defaults enabled and - CurseForge defaults disabled. -- Local section keys are identifiers, not necessarily platform slugs. -- Source eligibility is section-aware: `plugins:` can only include - CurseForge when the plugin loader is Bukkit, Spigot, or Paper. +Shipped. The cross-platform grammar is parsed today even though the +CurseForge resolver itself lands later — manifests can be authored +ahead of CF resolver support. Details live in [Fetching mods](#fetching-mods) +and [Host overrides](#host-overrides). + +What landed: + +- Entries accept `modrinth:` / `curseforge:` peer slug fields and + `sources:` (scalar or list); the parser normalizes both source + forms into a single internal `Set`. Empty lists, + duplicates, and unknown enum values fail at parse time. +- The legacy `tooling:` block is replaced by a top-level `gitrinth:` + block. `gitrinth.version` carries the semver constraint formerly + written as `tooling.gitrinth`; `gitrinth.modrinth` and + `gitrinth.curseforge` toggle default participation for each + platform (Modrinth enabled, CurseForge disabled). Examples and + the schema were updated in the same change. +- Local section keys are treated as opaque identifiers; the model + already used `Map` keys generically, and the + docs were tightened to call them out as identifiers rather than + slugs. +- Source eligibility is enforced at parse time: `plugins:` entries + with `sources: curseforge` (or a `curseforge:` peer) require + `loader.plugins ∈ {bukkit, spigot, paper}` and otherwise throw a + manifest error pointing at the + [Source eligibility matrix](#source-eligibility-matrix). +- The `cf:[@]` short-form sugar is intentionally an + `add` CLI feature only — it lands with Part 7 and the parser + never sees raw `cf:` strings as values. ### Part 3: CurseForge API client -Build a typed client and adapter layer that returns resolver-friendly -project/version/file/dependency data. Keep CurseForge details out of -the manifest parser. - -Done means: - -- `lib/src/service/curseforge_api.dart` can look up projects by slug - or numeric project ID. -- The adapter can list compatible files by Minecraft version, - section, loader, and channel floor. -- File models expose SHA1 and any dependency relation data needed by - [Transitive dependencies](#transitive-dependencies-and-deduplication). -- CF requests use the configured CurseForge token and have cache keys - separated from Modrinth/labrinth hosts. +Shipped. The CurseForge read API is callable in isolation and ready +for Part 4's resolver dispatch to consume. Resolver and CLI exposure +remain out of scope. + +What landed: + +- New `lib/src/service/curseforge_api.dart` is a retrofit-generated + abstract client with `getMod`, `searchMods`, `listFiles`, and + `getFile` endpoints. Every endpoint is gated on + `@Extra({kCurseForgeAuthRequired: true})` so the auth interceptor + controls when the API key is attached. +- `CurseForgeAuthInterceptor` injects `x-api-key:` for requests + destined for the resolved CurseForge read-API host. Download-key resolution is + environment-only: `GITRINTH_CURSEFORGE_TOKEN` at runtime overrides + the official build default decoded from + `kCurseForgeDefaultApiKeyB64`, which is populated from + `GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64` at build time. User + config tokens are intentionally not consulted for downloads; + `gitrinth token add curseforge.com` is reserved for the separate + CurseForge publish key. +- `defaultCurseForgeUploadBaseUrl`, `curseForgeUploadTokenKey`, and + `resolveCurseForgeUploadToken(...)` are declared in + `curseforge_url.dart` so Parts 7 and 8 have a stable shape to + build the publish path against. The publish token has *no* + embedded default by design: uploads attribute artifacts to a + specific developer account, so each developer must supply their + own with `GITRINTH_CURSEFORGE_UPLOAD_TOKEN` or + `gitrinth token add curseforge.com`. +- `CurseForgeRateLimitInterceptor` is reactive only (CF publishes + no budget headers). On a 429 it sleeps using `Retry-After` if + present or an attempt-ramped backoff capped at 65s, retrying up + to 5 times before propagating the original `DioException`. +- `CurseForgeErrorInterceptor` mirrors `ModrinthErrorInterceptor`: + 401/403 → `AuthenticationError`, 404 on `/v1/mods/` names + the missing project, other non-2xx codes wrap as `UserError` + with the request URI. +- `CurseForgeApiFactory` composes one cached `Dio` + interceptor + bundle (offline guard → auth → rate-limit → error) with the + standard `gitrinth/` User-Agent. Unlike + `ModrinthApiFactory` it caches a single bundle rather than a + host-keyed map because CF has one canonical host. +- DTOs live under `lib/src/model/curseforge/`: `Mod`, `ModFile`, + `FileHash` (with a `HashAlgo` decoder), `FileRelation` (with a + `RelationType` decoder), `Pagination`, `ModSearchResponse`, + `ModFileSearchResponse`, `ModEnvelope`, `ModFileEnvelope`. All + use `dart_mappable` and round-trip via fixtures captured under + `test/fixtures/curseforge/`. `Mod.allowModDistribution` is + exposed (defaulting to `true` when the API omits it) so Parts 4 + and 8 can branch on it; `ModFile.downloadUrl` is nullable so the + `allowModDistribution: false` case can surface to the resolver + without an attempted fetch. +- CurseForge taxonomy is encoded as extension methods on the + `Section` and `Channel` enums plus registered loader identities. + `cf_constants.dart` exposes `Section.cfClassId`, + the built-in `ModLoader.cfModLoaderType`, `cfReleaseTypesFor(Channel)`, + `pluginLoaderEligibleForCurseforge(PluginLoader)`. The plugin + eligibility predicate delegates to `sectionAllowsCurseforge` + rather than restating the rule. + Resolution uses the operation's immutable loader registry for provider + mappings. See [loader capabilities](loader-descriptors.md) for discovery, + explicit user mappings, archive metadata, and runtime boundaries. +- `listCompatibleFiles(...)` in `curseforge_filters.dart` is the + single entry point Part 4 will call. It accepts pack context + (section, optional mod loader, plugin loader for plugin + sections, the union of `mc_version` + `accepts_mc`, channel), + queries CF's `/files` endpoint per accepted game version within + the API window, filters by channel and availability, and returns + deduplicated files sorted newest-first. Plugin-loader + eligibility is checked before any HTTP request — Folia/Sponge + throw `ArgumentError` immediately. `findModBySlug(...)` is the + exact-slug lookup path. +- `cache.dart` gained `curseforgeRoot` and a + `curseforgePath({projectId, fileId, filename})` helper. The + layout is `/curseforge///` + with no host segment because CF has a single host. The cache is + for local-developer testing only — CF mods are never bundled + into a published `.mrpack` per the project policy on CurseForge + mod distribution. +- `build.yaml` now generates retrofit code for both the Modrinth + and CurseForge clients. ### Part 4: Resolver and lockfile -Turn the existing single-source resolver into a platform-dispatching -resolver. Details live in -[Cross-platform hash verification](#cross-platform-hash-verification) +Shipped. The single-source resolver became a platform-dispatching one +and `mods.lock` now carries per-platform source blocks. Details live +in [Cross-platform hash verification](#cross-platform-hash-verification) and [Mixing CF and Modrinth in one pack](#mixing-cf-and-modrinth-in-one-pack). -Done means: - -- Each requested eligible source resolves independently, with - `not_found` recorded when a declared or tooling-enabled default - source is missing. -- Dual-source entries compare SHA1, scan older compatible versions, - and respect `allow_hash_mismatch`. -- `mods.lock` records per-platform IDs and hashes for every section, - including `plugins:`. -- Lockfile source states are explicit: resolved, `not_found`, or - omitted because the source was excluded, ineligible, or not - requested. -- Unsupported source/section combinations fail early with manifest - errors instead of surfacing as resolver misses. +What landed: + +- PubGrub is platform-agnostic — `lib/src/model/resolver/pubgrub.dart` + imports nothing from `lib/src/model/modrinth/` and operates on a + small `SolverVersion` / `SolverDependency` interface in + `version_solver_types.dart`. The Modrinth side feeds in via + `ModrinthSolverVersion` (adapter in `modrinth_solver_adapter.dart`); + selection-order is constructor-injected so a future CurseForge-side + transitive graph adds its own adapter without touching the solver. +- The `Resolver` accepts an optional `Set modrinthSlugs` + dispatch gate so entries whose effective sources exclude Modrinth + (e.g. `sources: [curseforge]` or pack-default-disabled) skip the + Modrinth root constraints and are handled by the CurseForge + resolver pass. +- `service/resolve/curseforge_resolver.dart` walks the merged + manifest, calling `findModBySlug` / `getMod` (numeric short-form) + and `listCompatibleFiles`, picking the newest compatible file per + CF-eligible entry. Slug-not-found / no-compatible-files cases + accumulate into `CfNotFound`. +- Modrinth and CurseForge "missing on a requested platform" + outcomes aggregate into a single `ValidationError` listing every + affected entry plus the three remediation options. `not_found` + is **not** a lockfile state; it remains a `mods.yaml` + `constraintRaw` marker written exclusively by `gitrinth migrate`. +- Dual-source entries run `service/resolve/hash_scan.dart` — + newest-pair SHA1 compare, then a windowed scan of `hash_scan_depth` + additional pairs (default 10, `0` disables) looking for a match. + `allow_hash_mismatch: true` accepts the newest pair with a + console warn; otherwise the depth-exhausted case throws a + `ValidationError` carrying both hashes and the three remediations. +- `mods.lock` is a clean break: each entry has `kind: resolved | url + | path`. `kind: resolved` carries one or both of `modrinth:` / + `curseforge:` per-platform blocks (each with `project_id`, + `version_id` / `file_id`, `file:` payload, and `game_versions`). + Per-platform blocks are either fully populated (resolved) or + entirely omitted (excluded by `sources:`, ineligible per the + matrix, or pack-default-disabled) — no `status` enum, no + `not_found` lockfile state. The lockfile builder is now a free + function `buildLock(...)` rather than `part of resolve_and_sync`. +- The artifact-fetch loop is extracted into + `service/resolve/download_artifacts.dart`. Each per-platform + source block downloads independently. A CurseForge entry with no + download URL requires verified bytes in its canonical cache path; + otherwise the error identifies the exact file and manual recovery. +- Folder reorganization: `lib/src/service/` now has + `modrinth/`, `curseforge/`, and `resolve/` feature subfolders + for the touched-by-Part-4 files. Cross-cutting utilities + (`console`, `cache`, `downloader`, etc.) stay flat. ### Part 5: Search fallback -Implement search only after deterministic resolution and hash -verification are in place. Details live in +Shipped. Search runs after deterministic lookup or an exhausted hash +scan, using the resolved peer as a hash anchor. Details live in [Search fallback](#search-fallback). +What landed: + +- Both missing-slug and hash-mismatch paths search the eligible peer + platform, including hosted Modrinth. A unique project with a SHA1 + match within `hash_scan_depth` is selected; fuzzy and ambiguous + matches remain candidate diagnostics. +- Search respects source restrictions, section, loader, Minecraft + versions, channel, and Modrinth version constraints. Hash-selected + Modrinth versions go through dependency solving before locking. +- Per-entry `no_cross_platform_search: true` and `--no-search` on + `get`, `add`, and `upgrade` suppress fallback. Offline runs never + search and preserve the lock's discovery markers. +- Source blocks record `discovered_via_search: true`; notices suggest + explicit peer slugs. Search responses are cached for the resolution + session, with host and compatibility filters in the cache key. + Done means: - Slug-not-found and hash-mismatch paths can search the missing or @@ -139,11 +352,11 @@ Done means: ### Part 6: Transitive dependencies and deduplication -Layer graph behavior on top of stable source resolution. Details live +Shipped. The graph resolver builds on stable source resolution. Details live in [Transitive dependencies and deduplication](#transitive-dependencies-and-deduplication). -Done means: +What landed: - Top-level entries are indexed by source/host/slug. - Transitive dependencies are assigned to lockfile sections by the @@ -151,16 +364,19 @@ Done means: - Synthetic entries are created only when no top-level entry matches. - Cross-platform synthetics merge only on hash evidence. - `required_by:` is retained for `deps` output. +- Hash-selected files are re-walked until both dependency graphs stabilize; + obsolete children are pruned. Offline resolution retains reachable peers + from the lock and cached metadata without network access. ### Part 7: CLI integration -Expose the new behavior through commands after parser and resolver -semantics are stable. Details live in +Shipped. The CLI exposes source selection, peer discovery, and credentials. +Details live in [`add` command cross-platform behavior](#add-command-cross-platform-behavior) and the command docs linked from [Implementation touches](#implementation-touches). -Done means: +What landed: - `gitrinth add` writes the minimal source shape for one-sided or dual-source entries. @@ -168,17 +384,17 @@ Done means: - `--[no-]modrinth`, `--[no-]curseforge`, `--allow-hash-mismatch`, and `--no-search` are wired. - `gitrinth curseforge enable/disable` and - `gitrinth modrinth enable/disable` edit the matching `tooling` + `gitrinth modrinth enable/disable` edit the matching `gitrinth` platform toggle in `mods.yaml`. -- `token add/list/remove` can manage both host URLs and - `curseforge.com`. +- `token add/list/remove` can manage Modrinth/labrinth host URLs and + the CurseForge publish credential at `curseforge.com`. ### Part 8: Pack and publish -Finish with archive output and remote upload behavior. Details live +Shipped. Archive output and remote upload share the target selection model. Details live in [Publishing to CurseForge](#publishing-to-curseforge). -Done means: +What landed: - `pack --curseforge` emits a CF-compatible archive. - `publish` can target Modrinth, CurseForge, or both. @@ -186,13 +402,16 @@ Done means: target config map; `publish_to.modrinth` can carry a Modrinth-compatible publishing host independently from `modrinth_host:`. When `publish_to` is unset, publish targets - follow the enabled `tooling` platforms. + follow the enabled `gitrinth` platforms. - Entries unavailable on a target are handled as target-scoped overrides or publish errors according to command flags. - Plugin publishing is limited to Bukkit/Spigot/Paper-compatible plugin entries; Sponge and Folia plugin entries are excluded from the CurseForge target unless represented as loose overrides. +Archive contents and multipart uploads are covered by local HTTP tests; +verification does not publish a release to either service. + ## Reference architecture The bridge is not a greenfield design — substantial pieces of @@ -230,8 +449,8 @@ Other sections should reference it rather than restating policy. | `plugins:` with `folia` | enabled Modrinth only | manifest error | no | no | | `plugins:` with `sponge` | enabled Modrinth only | manifest error | no | no | -"Enabled platforms" are controlled by `tooling.modrinth` and -`tooling.curseforge`. Modrinth defaults to `enabled`; CurseForge +"Enabled platforms" are controlled by `gitrinth.modrinth` and +`gitrinth.curseforge`. Modrinth defaults to `enabled`; CurseForge defaults to `disabled`. Explicit entry-level `sources: curseforge` or `cf:` opts that entry into CurseForge without changing the pack-wide CurseForge default, as long as the section and loader are @@ -248,27 +467,35 @@ Each source block in `mods.lock` has one of these meanings: - **Resolved** — serialized as the platform-specific project/file ID and hash block. -- **Not found** — serialized as `{ status: not_found }` only when an - eligible declared or tooling-enabled default platform was queried, - including any allowed search fallback, and no compatible - project/file was found. -- **Omitted** — used when the platform was excluded by `sources:`, - ineligible under the [Source eligibility matrix](#source-eligibility-matrix), - or never requested. +- **Omitted** — the field is entirely absent from the entry's YAML. + Used when the platform was excluded by `sources:`, ineligible under + the [Source eligibility matrix](#source-eligibility-matrix), or + never requested (pack-default disabled). + +There is no `not_found` lockfile state. A requested eligible platform +that is missing the project (or has it but lacks a satisfying version, +or — in Part 5 — fails search fallback) raises a `ValidationError` at +resolution time; the lockfile is not written. Users resolve this by +adding `modrinth:` / `curseforge:` peer slug overrides, restricting +the entry's `sources:` to platforms that have it, or running +`gitrinth migrate ` — which writes the `not_found` marker as +the entry's `mods.yaml` `constraintRaw` value, keeping the manifest +parseable while removing the entry from active resolution. Explicitly requesting an ineligible source is a manifest error, not a lockfile state. For example, `sources: curseforge` on a Folia `plugins:` entry fails before resolution. -## Platform tooling toggles +## Platform toggles (`gitrinth:` block) The bridge keeps the expanded `mods.yaml` source grammar available -while leaving CurseForge off by default. Platform defaults live under -`tooling`: +while leaving CurseForge off by default. Platform defaults and the +`gitrinth` CLI semver constraint live under a top-level `gitrinth:` +block: ```yaml -tooling: - gitrinth: ^1.0.0 +gitrinth: + version: ^1.0.0 modrinth: enabled curseforge: disabled ``` @@ -279,13 +506,13 @@ These toggles affect implicit platform participation only. They do not make an otherwise invalid source valid, and they do not block an entry from explicitly selecting a platform with `sources:` or `cf:`. Peer slug fields such as `curseforge:` only override the slug after -that platform is selected by tooling defaults, `sources:`, or `cf:`. +that platform is selected by `gitrinth` defaults, `sources:`, or `cf:`. -`gitrinth curseforge enable` writes `tooling.curseforge: enabled`. -`gitrinth curseforge disable` writes `tooling.curseforge: disabled`. -`gitrinth modrinth enable` writes `tooling.modrinth: enabled`. -`gitrinth modrinth disable` writes `tooling.modrinth: disabled`. -The command writers preserve any existing `tooling.gitrinth` +`gitrinth curseforge enable` writes `gitrinth.curseforge: enabled`. +`gitrinth curseforge disable` writes `gitrinth.curseforge: disabled`. +`gitrinth modrinth enable` writes `gitrinth.modrinth: enabled`. +`gitrinth modrinth disable` writes `gitrinth.modrinth: disabled`. +The command writers preserve any existing `gitrinth.version` constraint. ## Fetching mods @@ -293,12 +520,12 @@ constraint. Every platform-backed entry resolves on the enabled sources that support that entry's section and loader according to the [Source eligibility matrix](#source-eligibility-matrix). With no -`tooling` override, this means Modrinth only. When -`tooling.curseforge: enabled` is present, eligible entries also +`gitrinth` overrides, this means Modrinth only. When +`gitrinth.curseforge: enabled` is present, eligible entries also resolve on CurseForge by default. The Modrinth side of that resolution can be redirected at a labrinth deployment via -`modrinth_host:` (per-entry or pack-wide); the CurseForge side always -targets `api.curseforge.com`. +`modrinth_host:` (per-entry or pack-wide); the CurseForge side targets +`GITRINTH_CURSEFORGE_URL` when set, otherwise `api.curseforge.com`. The examples below use `mods:`, but the same source grammar applies to `resource_packs:`, `data_packs:`, `shaders:`, and supported @@ -334,7 +561,7 @@ mods: version: ^2.3.0 # Explicit CurseForge-only entry; works even when - # tooling.curseforge is disabled globally. + # gitrinth.curseforge is disabled globally. ae2: sources: curseforge curseforge: applied-energistics-2 @@ -354,7 +581,7 @@ loader: plugins: paper plugins: - # Default: resolves on Modrinth unless tooling.curseforge is enabled. + # Default: resolves on Modrinth unless gitrinth.curseforge is enabled. luckperms: ^5.5.17 # Slug differs on CurseForge — override CF side only when @@ -381,24 +608,30 @@ peer fields override that platform's slug when it differs. use it when an entry only exists on one side, or when you want to pin an entry to one platform even though another enabled source has it. Explicit `sources: curseforge` opts that entry into CurseForge even -when `tooling.curseforge` is disabled. The schema accepts both a +when `gitrinth.curseforge` is disabled. The schema accepts both a scalar (`sources: curseforge`) and a list (`sources: [curseforge]`) form via a `oneOf` of string-or-array; the parser normalizes both into a single internal `Set`. -If a declared or tooling-enabled default platform doesn't have the -entry, that platform gets a `not_found` marker in the matching -`mods.lock` section and a warning is emitted; the entry still -succeeds as long as at least one platform resolves. `mods-yaml.md` -will be updated alongside this task to document the relaxed key -semantics and the new fields. +If a declared or `gitrinth`-enabled default platform doesn't have the +entry, resolution fails with `ValidationError` listing each platform +queried and the slug used; the lockfile is not written. The user +fixes this by adding `modrinth:` / `curseforge:` peer slug overrides +when the slug differs across platforms, restricting `sources:` to a +single platform when the entry only exists on one side, or running +`gitrinth migrate ` to mark the entry not-found in the +manifest. (Part 5's [Search fallback](#search-fallback) inserts a +search step before the failure for slug-not-found and hash-mismatch +paths.) `mods-yaml.md` will be updated alongside this task to +document the relaxed key semantics and the new fields. The resolver uses the same `loader` + `mc_version` filters (plus per-entry [`accepts_mc`](todo.md#accepts-mc--per-entry-mc-version-tolerance)) and the same channel floor (`release`/`beta`/`alpha`) across -platforms. Downloads hit each platform's CDN. The CF API requires a -key, managed via [`token` add curseforge.com](todo.md#token-command); -labrinth hosts named via `modrinth_host:` look up +platforms. Downloads hit each platform's CDN. The CF download API key +is supplied from `GITRINTH_CURSEFORGE_TOKEN` or from the official +build's `GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64`; it is not read from +the user token store. Labrinth hosts named via `modrinth_host:` look up `UserConfig.tokens[]` through the same store. ### Plugin source eligibility @@ -468,8 +701,8 @@ Rules: `gitrinth add ` resolves on every enabled, eligible platform by default and writes the minimal entry that captures what it found. With -no `tooling` override, this means Modrinth only. When -`tooling.curseforge: enabled` is present, most sections resolve on +no `gitrinth` overrides, this means Modrinth only. When +`gitrinth.curseforge: enabled` is present, most sections resolve on both Modrinth and CurseForge. For `plugins:`, CurseForge is eligible only under `loader.plugins: bukkit`, `spigot`, or `paper`; Sponge and Folia plugin packs default to Modrinth only. The resolved form depends @@ -494,11 +727,11 @@ makes one-sided entries visible in the manifest. `add` CLI flags: -| Flag | Effect | -|-------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `--[no-]modrinth` | Toggle Modrinth resolution. `--no-modrinth` forces `sources: [curseforge]` even when Modrinth has the entry. | -| `--[no-]curseforge` | Toggle CurseForge resolution for this add. `--curseforge` opts into CF without changing `tooling.curseforge`; `--no-curseforge` forces `sources: [modrinth]` when CF would otherwise participate. | -| `--allow-hash-mismatch` | Accept divergent hashes on both platforms; writes `allow_hash_mismatch: true` on the entry. | +| Flag | Effect | +|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `--[no-]modrinth` | Toggle Modrinth resolution. `--no-modrinth` forces `sources: [curseforge]` even when Modrinth has the entry. | +| `--[no-]curseforge` | Toggle CurseForge resolution for this add. `--curseforge` opts into CF without changing `gitrinth.curseforge`; `--no-curseforge` forces `sources: [modrinth]` when CF would otherwise participate. | +| `--allow-hash-mismatch` | Accept divergent hashes on both platforms; writes `allow_hash_mismatch: true` on the entry. | `cf:` short-form sugar (`gitrinth add cf:applied-energistics-2`) implies `--no-modrinth` and writes a single-platform entry with @@ -593,12 +826,12 @@ scanning when the user is explicitly accepting divergence. ## Search fallback -When a declared or tooling-enabled default eligible platform returns +When a declared or `gitrinth`-enabled default eligible platform returns no project for the declared slug, gitrinth runs a search on that -platform before marking it `not_found`. This handles the common case -where an entry exists on both platforms but the CurseForge project -carries a different slug than the Modrinth one — e.g., Modrinth -`fabric-api` vs CurseForge `fabric-api-0-102-0`. +platform before failing the entry with `ValidationError`. This handles +the common case where an entry exists on both platforms but the +CurseForge project carries a different slug than the Modrinth one — +e.g., Modrinth `fabric-api` vs CurseForge `fabric-api-0-102-0`. ### When search runs @@ -639,16 +872,10 @@ scored using the already-resolved platform's entry as the anchor: 3. **Author** match against the anchor's author. 4. **Category** compatibility. -A hash match is definitive. Without a hash match, behavior depends -on the triggering path: - -- **Slug-not-found path** — at least one of name or author must - align for auto-lock; otherwise fail with a candidate list. -- **Hash-mismatch path** — auto-lock requires a hash match. - Name/author matches are listed as candidates in the error output - but never auto-applied, because a declared slug already matches - (just with divergent content) and silently switching to a - different slug based on fuzzy signals is too risky. +A unique hash match is definitive. On both triggering paths, +name, author, and category matches rank candidate diagnostics but +are never auto-applied. Multiple projects with matching hashes are +ambiguous and require an explicit peer slug override. ### Outcomes @@ -682,10 +909,11 @@ listed (project title, author, latest compatible version each). User resolves via explicit `curseforge:` override, `sources: [...]` restriction, or `allow_hash_mismatch: true`. -**Search returns nothing** — falls through to the existing -`not_found` behavior: the missing platform gets the marker, the -entry succeeds as long as at least one platform resolved, and a -warning is emitted. +**Search returns nothing** — resolution fails with `ValidationError` +listing the platform queried, the slug used, and the +[remediation options](#fetching-mods) (peer slug overrides, +`sources: [...]` restriction, or `gitrinth migrate`). The lockfile +is not written. ### Transitive deps @@ -721,10 +949,11 @@ debugging resolver behavior. ### Performance Search adds at most one API call per platform per not-found slug. -Results are cached in the gitrinth cache keyed by `(host, platform, -query, loader, mc_version)` for the resolution session, so repeated -`get`/`upgrade` runs don't re-query and labrinth queries don't -pollute the modrinth.com cache (or vice versa). To avoid search entirely at +Search responses are cached in memory for the resolution session, +keyed by host, platform, query, section, loader, and Minecraft versions. +Repeated lookups within that session reuse the response, and labrinth +queries remain separate from modrinth.com queries. A new CLI run +starts a new session. To avoid search entirely at scale, migrate discovered slugs into explicit `modrinth:` / `curseforge:` overrides in `mods.yaml`; gitrinth's notice output includes the exact override to paste. @@ -789,7 +1018,7 @@ When one path requires `flywheel` from CF and another requires 2. Apply the same hash check as top-level entries — match → dual-source synthetic; mismatch → scan; scan-exhaust → error. 3. If only one platform has it, lock the synthetic as single-source - with a `not_found` marker on the missing platform. + with the missing platform's source block omitted (no marker). ### Cross-platform slug divergence @@ -917,22 +1146,25 @@ mods: jei: version: 19.27.0 modrinth: { project_id: ..., version_id: ..., sha512: ... } - curseforge: { project_id: 238222, file_id: ..., sha512: ... } + curseforge: { project_id: 238222, file_id: ..., file: { sha1: ... } } distanthorizons: version: 2.3.0 modrinth: { project_id: ..., version_id: ..., sha512: ... } - # curseforge excluded via sources: [modrinth] + # curseforge excluded via sources: [modrinth] — field is absent some-modrinth-only-mod: version: 1.2.3 modrinth: { project_id: ..., version_id: ..., sha512: ... } - curseforge: { status: not_found } + # curseforge omitted because the entry sets sources: [modrinth]. + # If the entry had requested CurseForge and CF lacked it, + # resolution would have failed instead — there is no not_found + # lockfile state. plugins: # In a Paper/Bukkit/Spigot plugin pack, plugins can be dual-source. luckperms: version: 5.5.17 modrinth: { project_id: ..., version_id: ..., sha512: ... } - curseforge: { project_id: ..., file_id: ..., sha512: ... } + curseforge: { project_id: ..., file_id: ..., file: { sha1: ... } } ``` [`deps`](todo.md#deps-command) surfaces per-platform source @@ -949,8 +1181,8 @@ selector rather than a Modrinth host field. `modrinth_host:` remains the default Modrinth/labrinth source host for resolving entries; it does not opt the pack into publishing and does not select the Modrinth publish destination. When `publish_to` is unset, publishing -targets the enabled platform set from `tooling` (Modrinth only by -default). Explicit `publish_to` values can target one or both +targets the enabled platform set from the `gitrinth:` block (Modrinth +only by default). Explicit `publish_to` values can target one or both platforms in a single [`publish`](todo.md#publish-command) run. @@ -980,7 +1212,12 @@ mapping if future config fields are needed). URL strings are no longer accepted as the top-level `publish_to` scalar. Explicit `publish_to: curseforge`, `[curseforge]`, or a `curseforge:` map key opts that publish operation into CurseForge even when -`tooling.curseforge` is disabled by default. +`gitrinth.curseforge` is disabled by default. + +Publishing requires a CurseForge publish API key, which is distinct +from the download key used by the resolver. It is resolved from +`GITRINTH_CURSEFORGE_UPLOAD_TOKEN` first, then from the user token +store entry managed by `gitrinth token add curseforge.com`. ```yaml # Resolve Modrinth-source entries against a labrinth instance by @@ -1072,20 +1309,20 @@ Concrete work: ## Implementation touches -| Path | Status | Role in bridge | -|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| [`lib/src/model/manifest/mods_yaml.dart`](../lib/src/model/manifest/mods_yaml.dart) | exists | Add `modrinth:` / `curseforge:` slug overrides, `sources:` (scalar or list), hash flags, source eligibility helpers, and `tooling.modrinth` / `tooling.curseforge` platform toggles; grow `ModrinthEntrySource` with an optional host (or add a peer); add pack-level `modrinth_host:` and normalized `publish_to` target config to the top-level manifest model | -| [`lib/src/model/manifest/parser.dart`](../lib/src/model/manifest/parser.dart) | exists | Rename `hosted` → `modrinth_host`, normalize hyphenated fields to underscores, drop the deferred-source guard, parse the new entry and pack-level fields plus `tooling` platform toggles, reject CurseForge plugin sources outside Bukkit/Spigot/Paper loaders | -| [`lib/src/model/manifest/mods_lock.dart`](../lib/src/model/manifest/mods_lock.dart) | exists | Per-source hash blocks for every section, `not_found` markers, `discovered_via_search`, `required_by:`, plugin lock blocks | -| [`lib/src/service/resolve_and_sync.dart`](../lib/src/service/resolve_and_sync.dart) and [`lib/src/model/resolver/resolver.dart`](../lib/src/model/resolver/resolver.dart) | exists | Multi-source branching; today's single-source `if (entry.source is! ModrinthEntrySource)` early-out (in both files) becomes the dispatch point; apply section-aware source eligibility before network resolution | -| [`lib/src/service/modrinth_api.dart`](../lib/src/service/modrinth_api.dart) | exists | Already supports per-call `baseUrl`; needs a host-keyed factory so each labrinth host gets its own client + rate-limit budget | -| `lib/src/service/curseforge_api.dart` | new | Retrofit client mirroring `ModrinthApi` shape, including content-type filters and Bukkit/Spigot/Paper plugin compatibility filters | -| [`lib/src/service/user_config.dart`](../lib/src/service/user_config.dart) | exists | Token lookup by host already supported via `tokens: Map` | -| [`lib/src/cli/runner.dart`](../lib/src/cli/runner.dart) | exists | Wire new commands, including `gitrinth curseforge enable/disable` and `gitrinth modrinth enable/disable` | -| [`lib/src/commands/add_command.dart`](../lib/src/commands/add_command.dart) | exists | `cf:` short form that writes `sources: curseforge` without enabling pack-wide CF, `--[no-]modrinth` / `--[no-]curseforge` / `--allow-hash-mismatch` flags, plugin-source eligibility errors | -| [`lib/src/commands/pack_assembler.dart`](../lib/src/commands/pack_assembler.dart) and [`lib/src/commands/pack_command.dart`](../lib/src/commands/pack_command.dart) | exists | Add CF archive/manifest output, target-scoped publishability checks, and `plugins/` override paths for plugin entries | -| `lib/src/commands/curseforge.dart` | new | Hosts CF-specific subcommands, starting with `enable` / `disable` for `tooling.curseforge` | -| `lib/src/commands/token_command.dart` | new | `token add` / `list` / `remove` (also referenced from [`todo.md`](todo.md#token-command)) | -| [`assets/schema/mods.schema.yaml`](../assets/schema/mods.schema.yaml) | exists | Rename `hosted` → `modrinth_host`; normalize hyphenated fields to underscores; add new entry fields; add `tooling.modrinth` / `tooling.curseforge` enum toggles; add pack-level `modrinth_host:` and `publish_to` target config; express plugin-source eligibility where schema can, leaving loader-dependent checks to the parser | -| [`docs/mods-yaml.md`](mods-yaml.md) | exists | Update `hosted:` → `modrinth_host:` in docs and examples; document renamed underscore fields; document new entry shape; remove "Modrinth-only" framing where it's no longer accurate; document CurseForge plugin limits | -| [`docs/cli.md`](cli.md) | exists | `cf:` short form, new `add` flags, platform enable/disable commands, plugin-source errors, `token` subcommands, `--curseforge` on `pack` | +| Path | Status | Role in bridge | +|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| [`lib/src/model/manifest/mods_yaml.dart`](../lib/src/model/manifest/mods_yaml.dart) | exists | Add `modrinth:` / `curseforge:` slug overrides, `sources:` (scalar or list), hash flags, source eligibility helpers, and `gitrinth.modrinth` / `gitrinth.curseforge` platform toggles; grow `ModrinthEntrySource` with an optional host (or add a peer); add pack-level `modrinth_host:` and normalized `publish_to` target config to the top-level manifest model | +| [`lib/src/model/manifest/parser.dart`](../lib/src/model/manifest/parser.dart) | exists | Rename `hosted` → `modrinth_host`, normalize hyphenated fields to underscores, drop the deferred-source guard, parse the new entry and pack-level fields plus the top-level `gitrinth:` block (with `gitrinth.version` semver constraint and `gitrinth.modrinth` / `gitrinth.curseforge` platform toggles, replacing the old `tooling:` block), reject CurseForge plugin sources outside Bukkit/Spigot/Paper loaders | +| [`lib/src/model/manifest/mods_lock.dart`](../lib/src/model/manifest/mods_lock.dart) | exists | Per-source hash blocks for every section, present-or-omitted (no `not_found` lockfile state), `discovered_via_search`, `required_by:`, plugin lock blocks | +| [`lib/src/service/resolve_and_sync.dart`](../lib/src/service/resolve_and_sync.dart) and [`lib/src/model/resolver/resolver.dart`](../lib/src/model/resolver/resolver.dart) | exists | Multi-source branching; today's single-source `if (entry.source is! ModrinthEntrySource)` early-out (in both files) becomes the dispatch point; apply section-aware source eligibility before network resolution | +| [`lib/src/service/modrinth_api.dart`](../lib/src/service/modrinth_api.dart) | exists | Already supports per-call `baseUrl`; needs a host-keyed factory so each labrinth host gets its own client + rate-limit budget | +| `lib/src/service/curseforge_api.dart` | new | Retrofit client mirroring `ModrinthApi` shape, including content-type filters and Bukkit/Spigot/Paper plugin compatibility filters | +| [`lib/src/service/user_config.dart`](../lib/src/service/user_config.dart) | exists | Token lookup by host already supported via `tokens: Map` | +| [`lib/src/cli/runner.dart`](../lib/src/cli/runner.dart) | exists | Wire new commands, including `gitrinth curseforge enable/disable` and `gitrinth modrinth enable/disable` | +| [`lib/src/commands/add_command.dart`](../lib/src/commands/add_command.dart) | exists | `cf:` short form that writes `sources: curseforge` without enabling pack-wide CF, `--[no-]modrinth` / `--[no-]curseforge` / `--allow-hash-mismatch` flags, plugin-source eligibility errors | +| [`lib/src/commands/pack_assembler.dart`](../lib/src/commands/pack_assembler.dart) and [`lib/src/commands/pack_command.dart`](../lib/src/commands/pack_command.dart) | exists | Add CF archive/manifest output, target-scoped publishability checks, and `plugins/` override paths for plugin entries | +| `lib/src/commands/curseforge.dart` | new | Hosts CF-specific subcommands, starting with `enable` / `disable` for `gitrinth.curseforge` | +| `lib/src/commands/token_command.dart` | new | `token add` / `list` / `remove` (also referenced from [`todo.md`](todo.md#token-command)) | +| [`assets/schema/mods.schema.yaml`](../assets/schema/mods.schema.yaml) | exists | Rename `hosted` → `modrinth_host`; normalize hyphenated fields to underscores; add new entry fields; add `gitrinth.modrinth` / `gitrinth.curseforge` enum toggles; add pack-level `modrinth_host:` and `publish_to` target config; express plugin-source eligibility where schema can, leaving loader-dependent checks to the parser | +| [`docs/mods-yaml.md`](mods-yaml.md) | exists | Update `hosted:` → `modrinth_host:` in docs and examples; document renamed underscore fields; document new entry shape; remove "Modrinth-only" framing where it's no longer accurate; document CurseForge plugin limits | +| [`docs/cli.md`](cli.md) | exists | `cf:` short form, new `add` flags, platform enable/disable commands, plugin-source errors, `token` subcommands, `--curseforge` on `pack` | diff --git a/docs/loader-descriptors.md b/docs/loader-descriptors.md new file mode 100644 index 0000000..90c2388 --- /dev/null +++ b/docs/loader-descriptors.md @@ -0,0 +1,92 @@ +# Loader capabilities + +Loader identities are lowercase strings, such as `fabric`, `quilt`, or +`future-loader`. Existing manifests and locks retain their serialized names. +Unknown identities can be parsed and inspected; each operation then checks +its required capabilities. + +Modrinth discovery reads loader names and supported project types from the +selected host. CurseForge discovery preserves numeric types and version names. +An unknown CurseForge type is identified as `curseforge-type-`; it does +not imply a Modrinth alias, archive token, or installer. + +Discovery does not provide executable behavior. Archive dependency keys also +require explicit metadata: a loader tag alone does not define its archive key +(for example, Fabric uses `fabric-loader` in a Modrinth archive). + +## User mappings + +Optional mappings live in `loaders.yaml` beside the resolved user config. +Use `--loader-descriptors ` on a metadata-consuming command to select +another file explicitly. A project-local file is never discovered automatically. + +```yaml +version: 1 +loaders: + future-loader: + role: mod + modrinth: + https://api.modrinth.com/v2: future-loader + curseforge_type: 42 + archive: + modrinth: future-loader + curseforge: future + version_source: catalog +``` + +The numeric type and archive keys above are examples. Supply the exact values +documented by the provider; names do not establish cross-provider aliases. + +```console +gitrinth get --loader-descriptors C:/Users/me/.gitrinth/loaders.yaml +gitrinth pack --offline --loader-descriptors C:/Users/me/.gitrinth/loaders.yaml +``` + +Version 1 accepts these descriptor fields: + +| Field | Meaning | +|----------------------|-------------------------------------------------------------------| +| `role` | `mod`, `plugin`, `shader`, or `vanilla`. | +| `modrinth` | Provider base URL to exact filter/publish tag mappings. | +| `curseforge_type` | Positive numeric mod-loader filter type. | +| `curseforge_plugins` | Whether the provider supports this plugin platform. | +| `archive` | Optional `modrinth` dependency key and `curseforge` loader token. | +| `version_source` | Trusted version strategy; defaults to `catalog`. | +| `installer_family` | An installed, trusted installer strategy; omitted by default. | +| `compatible_mods` | Mod-loader identities a plugin server can also run. | +| `server_launch_jar` | Plain launcher JAR filename for an explicitly mapped recipe. | +| `server_run_script` | Whether the trusted runtime supplies server run scripts. | + +Unknown keys, built-in identity shadowing, duplicate provider aliases, and +numeric-type collisions fail validation. Descriptors cannot contain shell +commands, scripts, arbitrary URL templates, or expressions. + +Version strategies are `fabric`, `forge`, `neoforge`, `quilt`, `paper`, `sponge`, +`buildtools`, and `catalog`. Installer families are `fabric`, `forge`, `quilt`, +`paper`, `sponge`, `buildtools`, and `vanilla`. Selecting a family is an explicit +user mapping to its installed protocol; it cannot implement a new protocol. +Existing built-in mappings include the required provider-specific recipe data. + +## Offline and runtime behavior + +Catalog snapshots are versioned and stored under +`/loader-catalogs///catalog.json`. +Offline resolution uses the selected host's cached snapshot or explicit user +mappings and reuses concrete locked versions. A missing capability fails +without network access. Discovery refreshes missing identities; catalog-backed +`stable` / `latest` requests refresh available provider metadata online. + +A metadata-only loader can resolve and publish using its provider tags. Packing +requires archive tokens. Build and launch require an installed runtime adapter; +a missing adapter fails before changing the output directory. + +Quilt uses its official installer metadata, verifies the installer SHA512, and +accepts only the canonical Quilt Maven artifact URL. Its client/server recipes +use the official positional CLI arguments. Server installation verifies that +both `quilt-server-launch.jar` and `server.jar` exist. It does not accept the +Minecraft EULA or launch the server. + +Protocol references: [Modrinth loader discovery](https://docs.modrinth.com/api/operations/loaderlist/), +[Modrinth archive format](https://support.modrinth.com/en/articles/8802351-modrinth-modpack-format-mrpack), +[CurseForge REST API](https://docs.curseforge.com/rest-api/), and +[Quilt installer CLI](https://github.com/QuiltMC/quilt-installer/blob/master/src/main/java/org/quiltmc/installer/CliInstaller.java). diff --git a/docs/mods-yaml.md b/docs/mods-yaml.md index 2b53a57..f83e84d 100644 --- a/docs/mods-yaml.md +++ b/docs/mods-yaml.md @@ -17,7 +17,7 @@ A machine-readable schema lives alongside this document at A `mods.yaml` file can contain the following top-level fields. The required fields are [`slug`](#slug), [`name`](#name), [`version`](#version), [`description`](#description), [`loader`](#loader), and -[`mc-version`](#mc-version). +[`mc_version`](#mc_version). | Field | Required | Description | |-------------------------------------------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| @@ -28,8 +28,9 @@ fields are [`slug`](#slug), [`name`](#name), [`version`](#version), | [`project`](#project) | no | Modrinth project metadata — links, body, license, categories, client/server compatibility. See below for the full list of sub-fields. | | [`publish_to`](#publish_to) | no | Where the modpack publishes to. | | [`loader`](#loader) | yes | Per-section loaders (object). `mods` required; `shaders` required when the `shaders:` section has entries; `plugins` required when the `plugins:` section has entries. | -| [`mc-version`](#mc-version) | yes | The exact Minecraft version the modpack targets (e.g. `1.21.1`). | -| [`tooling`](#tooling) | no | Version constraints on the tooling used to build the modpack (currently just `gitrinth`). | +| [`mc_version`](#mc_version) | yes | The exact Minecraft version the modpack targets (e.g. `1.21.1`). | +| [`modrinth_host`](#modrinth_host) | no | Pack-level default base URL for the Modrinth source kind. When unset, falls back to `https://api.modrinth.com/v2`. | +| [`gitrinth`](#gitrinth) | no | Tooling configuration: semver constraint on the gitrinth CLI plus per-platform default toggles (`gitrinth.modrinth`, `gitrinth.curseforge`). | | [`mods`](#mods) | no | Every mod in the pack. Each entry may declare a per-mod [per-side install state](#sides-client--server) (`client`, `server`, or `both`). May be blank while the pack is being assembled. | | [`resource_packs`](#resource_packs) | no | Resource packs to ship with the pack. Same syntax as `mods`. | | [`data_packs`](#data_packs) | no | Data packs to ship with the pack. Same syntax as `mods`. | @@ -65,15 +66,15 @@ project: client_side: required server_side: required -publish_to: https://modrinth.com +publish_to: modrinth loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 -tooling: - gitrinth: ^1.0.0 +gitrinth: + version: ^1.0.0 mods: create: ^6.0.10+mc1.21.1 @@ -117,7 +118,7 @@ description: Paper server with client-side QoL mods. loader: mods: neoforge plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.2 @@ -132,12 +133,12 @@ Under `loader.plugins: sponge`, the concrete server distribution (SpongeForge, SpongeNeo, or SpongeVanilla) is selected at lock time from `loader.mods`: -| `loader.mods` | resolved server | server-side mods? | -|---|---|---| -| `forge` | SpongeForge | yes (Forge mods load alongside plugins) | -| `neoforge` | SpongeNeo | yes (NeoForge mods load alongside plugins) | -| `fabric` | SpongeVanilla | no — Fabric mods coerce to client-only | -| omitted / `vanilla` | SpongeVanilla | no | +| `loader.mods` | resolved server | server-side mods? | +|---------------------|-----------------|--------------------------------------------| +| `forge` | SpongeForge | yes (Forge mods load alongside plugins) | +| `neoforge` | SpongeNeo | yes (NeoForge mods load alongside plugins) | +| `fabric` | SpongeVanilla | no — Fabric mods coerce to client-only | +| omitted / `vanilla` | SpongeVanilla | no | `mods:` entries keep their declared per-side state under SpongeForge / SpongeNeo; under SpongeVanilla they are coerced to @@ -152,7 +153,7 @@ description: Forge tech pack with Sponge plugins. loader: mods: forge plugins: sponge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10+mc1.21.1 @@ -175,7 +176,7 @@ description: Plugin server, no mods. loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 plugins: chunky: ^1.4.40 @@ -407,24 +408,30 @@ project: ### `publish_to` -**Optional.** URL of the Modrinth-compatible server -[`gitrinth modrinth publish`](cli.md#gitrinth-modrinth-publish) -uploads the modpack to. +**Optional.** Select the targets used by `gitrinth publish`. Omit or set +`null` to follow the enabled `gitrinth` platforms (Modrinth by default). +Use `none` to disable publishing, `modrinth` or `curseforge` for one +target, or `[modrinth, curseforge]` for both. Explicit CurseForge +selection works even when the resolution default is disabled. -| Value | Meaning | -|---------|---------------------------------------------------------------------------------| -| *unset* | Publish to the default host (`GITRINTH_MODRINTH_URL` if set, else modrinth.com).| -| *URL* | Publish to the Modrinth-compatible server at that URL instead. | -| `none` | Publishing is disabled for this pack — `publish` exits with an error. | +A mapping selects targets by key presence and can override the +Modrinth publishing host independently of `modrinth_host`: ```yaml -publish_to: https://modrinth.example.com +publish_to: + modrinth: https://modrinth.example.com + curseforge: null ``` -Authentication uses the per-host token configured via -[`gitrinth modrinth token add `](cli.md#gitrinth-modrinth-token). -For the default host, use [`login`](cli.md#gitrinth-modrinth-login) or -the `GITRINTH_TOKEN` env var instead. +Both values may be null or empty mappings; only Modrinth accepts a URL. +Empty target lists/maps, duplicate targets, unknown target names, and +`none` inside a list/map are errors. Top-level URL scalars are no +longer accepted. + +Modrinth uses the per-host token store or `GITRINTH_TOKEN` for the +default host. CurseForge requires a personal upload token from +`GITRINTH_CURSEFORGE_UPLOAD_TOKEN` or `gitrinth token add curseforge.com`. +The download API key is a separate credential and cannot publish packs. ### `loader` @@ -442,16 +449,21 @@ loader: Recognised keys: -| Key | Required | Values | -|------------------|--------------------------------------------------------------------|------------------------------------------| -| `loader.mods` | Optional. Defaults to `vanilla` (no mod runtime). | `forge`, `fabric`, `neoforge`, `vanilla`. | -| `loader.shaders` | When [`shaders:`](#shaders) has entries. | `iris`, `optifine`, `canvas`, `vanilla`. | -| `loader.plugins` | When [`plugins:`](#plugins) has entries. | `bukkit`, `folia`, `paper`, `spigot`, `sponge`, each optionally tagged as `:`. | +| Key | Required | Values | +|------------------|---------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| `loader.mods` | Optional. Defaults to `vanilla` (no mod runtime). | Registered lowercase identity; built-ins include `forge`, `fabric`, `neoforge`, `quilt`, and `vanilla`. | +| `loader.shaders` | When [`shaders:`](#shaders) has entries. | `iris`, `optifine`, `canvas`, `vanilla`. | +| `loader.plugins` | When [`plugins:`](#plugins) has entries. | `bukkit`, `folia`, `paper`, `spigot`, `sponge`, each optionally tagged as `:`. | `resource_packs` and `data_packs` each have a single valid Modrinth loader (`minecraft` and `datapack` respectively), so they are not declared under `loader`. +Loader names are open lowercase identities. Discovery and explicit user-level +[loader descriptors](loader-descriptors.md) supply provider and archive mappings. +A new identity can be locked without an enum change; each operation checks the +metadata or runtime capability it needs. Existing spellings retain their meaning. + `loader.mods` is **optional** and defaults to `vanilla` — the "no mod runtime" sentinel — when omitted. Use this for purely-vanilla packs and for plugin servers that do not run mods. Adding any entry under @@ -464,7 +476,7 @@ When set to a real mod loader, `loader.mods` has three roles: [`mods`](#mods) and [`project_overrides`](#project_overrides) (when the override targets a mod), `gitrinth` only considers published mod versions tagged with this loader. Combined with - [`mc-version`](#mc-version), this determines which "latest" + [`mc_version`](#mc_version), this determines which "latest" version satisfies a blank or caret [mod-version constraint](#mod-version-constraints). - **Published compatibility.** When `gitrinth publish` uploads the @@ -474,9 +486,9 @@ When set to a real mod loader, `loader.mods` has three roles: - **Server distribution selection.** The server distribution assembled by [`gitrinth build`](cli.md#build) includes the matching server binary. Mod loaders use [`loader.mods`](#loader) - plus [`mc-version`](#mc-version). Plugin servers use + plus [`mc_version`](#mc_version). Plugin servers use [`loader.plugins`](#plugin-loaders), its optional tag, and - [`mc-version`](#mc-version). Resolved concrete loader versions are + [`mc_version`](#mc_version). Resolved concrete loader versions are written to `mods.lock`. `loader.shaders` applies version resolution for entries under @@ -525,12 +537,12 @@ lock time, based on `loader.mods`: Plugin-loader tags resolve as follows: - **Paper / Folia.** `stable` chooses the highest stable PaperMC build - for the exact `mc-version`; `latest` chooses the highest build for - that exact `mc-version` regardless of channel; a concrete tag is an + for the exact `mc_version`; `latest` chooses the highest build for + that exact `mc_version` regardless of channel; a concrete tag is an exact PaperMC build number. - **Sponge.** `stable` chooses the newest recommended build for the - resolved Sponge artifact and exact `mc-version`; `latest` chooses the - newest available build for that exact `mc-version`; a concrete tag is + resolved Sponge artifact and exact `mc_version`; `latest` chooses the + newest available build for that exact `mc_version`; a concrete tag is an exact Sponge artifact version. - **Bukkit / Spigot.** `stable` and `latest` resolve to the latest successful Jenkins BuildTools build number. A concrete tag is a bare @@ -569,7 +581,7 @@ every entry honours its declared [per-side install state](#sides-client--server) Spigot and Bukkit server jars are produced by SpigotMC's `BuildTools.jar` on first build. The resolved plugin-loader version is the Jenkins BuildTools build number used to download `BuildTools.jar`; -the pack's `mc-version` is still passed as BuildTools `--rev`. +the pack's `mc_version` is still passed as BuildTools `--rev`. BuildTools requires `git` and a JDK, takes several minutes to compile sources, and the produced jar is cached for re-use on subsequent builds. Paper, Folia, and the three Sponge variants are downloaded as @@ -579,17 +591,17 @@ pre-built jars from upstream. A pack with neither `loader.plugins` nor a real `loader.mods` declares no mod runtime and no plugin runtime — `gitrinth build server` -downloads the official Mojang `server.jar` for the pack's `mc-version` +downloads the official Mojang `server.jar` for the pack's `mc_version` from `piston-meta.mojang.com`. Useful for vanilla servers that ship only resource packs / data packs / loose `files:` entries. -### `mc-version` +### `mc_version` **Required.** The exact Minecraft release the modpack targets, for example `1.21.1`. Version ranges and wildcards are intentionally disallowed — a modpack targets a single Minecraft version so that mod-version resolution is deterministic. Like [`loader`](#loader), -`mc-version` has three roles: +`mc_version` has three roles: - **Version resolution.** When picking the version of each entry in [`mods`](#mods), [`resource_packs`](#resource_packs), @@ -601,45 +613,72 @@ mod-version resolution is deterministic. Like [`loader`](#loader), [mod-version constraints](#mod-version-constraints) resolve deterministically. - **Published compatibility.** When `gitrinth publish` uploads the - modpack, `mc-version` is declared as the modpack's supported + modpack, `mc_version` is declared as the modpack's supported Minecraft version on Modrinth. End users see it on the project page, and launchers use it to decide whether the pack is installable. - **Server distribution selection.** Together with [`loader`](#loader), - `mc-version` is what `gitrinth` uses to pick the server binary + `mc_version` is what `gitrinth` uses to pick the server binary bundled into the server distribution. The user does not specify the server binary, and it is not added after the build — see the [Server distribution selection](#loader) role on `loader`. ```yaml -mc-version: 1.21.1 +mc_version: 1.21.1 ``` -### `tooling` +### `modrinth_host` -**Optional.** Version constraints on the tooling used to build the -modpack. The block may be omitted entirely when no constraint is needed. +**Optional.** Pack-level default base URL for the Modrinth source +kind. Every entry that does not declare its own `modrinth_host:` +resolves against this URL instead of `https://api.modrinth.com/v2`. ```yaml -tooling: - gitrinth: ^1.0.0 +modrinth_host: https://modrinth.example.com ``` -| Key | Required | Description | -|------------|----------|----------------------------------------------------------------------------------------------------------------------------| -| `gitrinth` | no | A version constraint on the `gitrinth` CLI itself, using semver range syntax (for example `^1.0.0` or `">=1.0.0 <2.0.0"`). | +A per-entry `modrinth_host:` on a long-form entry overrides the +pack-level value for that entry only — see +[Mod dependencies](#mod-dependencies). -#### `gitrinth` +### `gitrinth` -Declares which versions of the `gitrinth` CLI the modpack is known to work -with. Unlike mod versions, this field accepts the full semver range syntax -(`^`, `>=`, `<`, `<=`, `>`), because the compatibility window for the tool -itself may span several majors. +**Optional.** Tooling configuration for `gitrinth` itself. Holds the +semver constraint on the running CLI and the platform default toggles +that decide which sources participate by default. The block may be +omitted entirely when neither field is needed. ```yaml -tooling: - gitrinth: ^1.0.0 +gitrinth: + version: ^1.0.0 + modrinth: enabled + curseforge: disabled ``` +| Key | Required | Description | +|--------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------| +| `version` | no | Semver range constraint on the `gitrinth` CLI itself (e.g. `^1.0.0`, `">=1.0.0 <2.0.0"`). | +| `modrinth` | no | `enabled` or `disabled`. Defaults to `enabled`. Toggles whether Modrinth participates by default for entries that do not set `sources:`. | +| `curseforge` | no | `enabled` or `disabled`. Defaults to `disabled`. Toggles whether CurseForge participates by default for entries that do not set `sources:`. | + +#### `gitrinth.version` + +Declares which versions of the `gitrinth` CLI the modpack is known to +work with. Unlike mod versions, this field accepts the full semver +range syntax (`^`, `>=`, `<`, `<=`, `>`), because the compatibility +window for the tool itself may span several majors. + +```yaml +gitrinth: + version: ^1.0.0 +``` + +#### `gitrinth.modrinth` / `gitrinth.curseforge` + +These toggles only affect *implicit* platform participation. They do +not make an otherwise invalid source valid, and they do not block an +entry from explicitly selecting a platform via `sources:` (or the +`cf:` short form on `add`). + ### Mod dependencies [`mods`](#mods), [`resource_packs`](#resource_packs), @@ -676,14 +715,14 @@ The value is a map with any combination of: - a `version` — a [mod-version constraint](#mod-version-constraints); - a `channel` — a [release-channel floor](#release-channel-channel); - a `client:` and/or `server:` install state — see [Sides](#sides-client--server); -- an `accepts-mc` widening — see [Per-entry MC version tolerance](#per-entry-mc-version-tolerance-accepts-mc); +- an `accepts_mc` widening — see [Per-entry MC version tolerance](#per-entry-mc-version-tolerance-accepts_mc); - at most one **source** — where `gitrinth` should fetch the mod from. ```yaml mods: - # Hosted on a custom Modrinth-compatible server. + # Modrinth source against a custom Modrinth-compatible host (labrinth). journeymap: - hosted: https://modrinth.example.com + modrinth_host: https://modrinth.example.com version: ^5.10.0 # Direct download URL. @@ -700,17 +739,26 @@ mods: server: unsupported ``` -Each entry must specify **at most one** of `hosted`, `url`, or `path`. -Omitting all three is equivalent to `hosted:` on the default Modrinth -instance — useful when you only want to express a version constraint -or per-side install state in the long form. - -| Source | Publishable? | Notes | -|----------|--------------|------------------------------------------------------| -| *(none)* | yes | Default Modrinth instance. | -| `hosted` | yes | Any Modrinth-compatible server. Value is a base URL. | -| `url` | no | Direct download URL for a `.jar`. | -| `path` | no | Local filesystem path relative to `mods.yaml`. | +`url:` and `path:` are mutually exclusive with each other. Omitting +both is equivalent to selecting the Modrinth source kind. The +optional `modrinth_host:` field is a host override on that Modrinth +source — it co-exists with the implicit Modrinth selection but is +mutually exclusive with `url:` / `path:`. + +| Source | Publishable? | Notes | +|------------------------|--------------|-----------------------------------------------------------------------------------| +| *(none)* | yes | Default Modrinth instance (`https://api.modrinth.com/v2`). | +| `modrinth_host: ` | yes | Modrinth source against a custom host (labrinth deployment). Value is a base URL. | +| `url` | no | Direct download URL for a `.jar`. | +| `path` | no | Local filesystem path relative to `mods.yaml`. | + +A pack-level `modrinth_host: ` field at the top of `mods.yaml` +sets the default base URL for every entry that does not declare its +own `modrinth_host:`. Section-map keys (the `:` part of each +entry) are local identifiers — by default they double as the +Modrinth project slug, but `modrinth:` and `curseforge:` peer +fields override the slug used on each platform when the section key +differs. **Caution.** For [`mods`](#mods) entries (and [`project_overrides`](#project_overrides) targeting a mod), `url` and @@ -732,7 +780,7 @@ tier *and every more-stable tier* — never an exact-match filter. | Value | Admits | Use when | |-----------|------------------------|------------------------------------------------------------------------------------------------| | `release` | release only | The pack should ship only stable builds for this entry. | -| `beta` | release + beta | A stable build doesn't yet exist for the target `mc-version`, but a tagged beta is acceptable. | +| `beta` | release + beta | A stable build doesn't yet exist for the target `mc_version`, but a tagged beta is acceptable. | | `alpha` | release + beta + alpha | The mod only publishes alpha builds, or the pack tracks bleeding-edge versions deliberately. | Omitting `channel` defaults to `alpha`, matching Modrinth's default of returning @@ -842,27 +890,27 @@ The parser raises a clear migration error if a manifest still uses the old fields, so a stale `mods.yaml` will fail loud rather than silently ship under the wrong defaults. -#### Per-entry MC version tolerance (`accepts-mc`) +#### Per-entry MC version tolerance (`accepts_mc`) -A long-form entry may include an `accepts-mc` field to **additively** +A long-form entry may include an `accepts_mc` field to **additively** widen the Minecraft versions the resolver queries for that one entry. -The pack's [`mc-version`](#mc-version) remains the single source of -truth: `accepts-mc` never overrides it, never influences the server +The pack's [`mc_version`](#mc_version) remains the single source of +truth: `accepts_mc` never overrides it, never influences the server binary or loader, and never applies to other entries. -Use it when a mod works on the pack's `mc-version` but the author +Use it when a mod works on the pack's `mc_version` but the author only tagged adjacent versions on Modrinth. For example, a pack on `1.21.1` with a mod that's still tagged `1.21`: ```yaml -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9+mc1.21 - accepts-mc: 1.21 # scalar shorthand for [1.21] + accepts_mc: 1.21 # scalar shorthand for [1.21] some-other-mod: version: ^2.0.0 - accepts-mc: [1.21, 1.20.1] # explicit list form + accepts_mc: [1.21, 1.20.1] # explicit list form ``` Snapshot-style tags are accepted too — Modrinth publishes a single @@ -875,10 +923,10 @@ At resolve time, the Modrinth `game_versions` filter for `appleskin` becomes `["1.21.1", "1.21"]` instead of `["1.21.1"]`; every other entry in the pack is unaffected. The resolved version's actual Modrinth `game_versions` tag is recorded in `mods.lock` under -`game-versions:`, so a future `mc-version` bump can surface entries -that were only admitted via `accepts-mc`. +`game_versions:`, so a future `mc_version` bump can surface entries +that were only admitted via `accepts_mc`. -`accepts-mc` is only available on long-form entries and works in the +`accepts_mc` is only available on long-form entries and works in the same sections that support long form — [`mods`](#mods), [`resource_packs`](#resource_packs), [`data_packs`](#data_packs), [`shaders`](#shaders), and [`plugins`](#plugins). When adding a new @@ -894,7 +942,7 @@ Four forms are supported: | Form | Example | Meaning | |-------|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| | Exact | `19.27.0.340` | Use this version and no other. Resolution fails if it is not compatible with the environment or with another mod. | -| Blank | *(empty value)* | Use the **latest** version of the mod that is compatible with the declared [`loader`](#loader) and [`mc-version`](#mc-version) and every other mod. | +| Blank | *(empty value)* | Use the **latest** version of the mod that is compatible with the declared [`loader`](#loader) and [`mc_version`](#mc_version) and every other mod. | | Caret | `^6.0.10+mc1.21.1` | Use the latest version that is both compatible with the environment and *compatible with* `6.0.10+mc1.21.1` (same major for `1.x.y`, same minor for `0.x.y`). | | Range | `">=1.0.0 <3.0.0"` | Latest version inside the half-open semver range. Bounds may combine or stand alone (`">=1.5.0"`, `"<2.0.0"`). Quote so YAML doesn't read `<` as a tag. | @@ -948,7 +996,7 @@ mods: ``` Since the Minecraft version and loader already live in -[`loader`](#loader) and [`mc-version`](#mc-version), a shorter +[`loader`](#loader) and [`mc_version`](#mc_version), a shorter constraint like `^6.0.10` is usually enough; `gitrinth` picks the matching build. @@ -1065,7 +1113,7 @@ mods: client: required server: unsupported journeymap: - hosted: https://modrinth.example.com + modrinth_host: https://modrinth.example.com version: ^5.10.0 ``` @@ -1279,8 +1327,8 @@ When `gitrinth` installs or updates a modpack it: override targets a slug not declared anywhere else — synthesizes a new entry in the section matching the Modrinth project type. 3. For each entry, queries Modrinth (or the override source) for every - version whose `loader` and `mc-version` match the modpack's - [`loader`](#loader) and [`mc-version`](#mc-version). + version whose `loader` and `mc_version` match the modpack's + [`loader`](#loader) and [`mc_version`](#mc_version). 4. Filters that list with the entry's version constraint. 5. Picks the highest remaining version, preferring versions that are compatible with every *other* entry's declared dependencies. diff --git a/docs/todo.md b/docs/todo.md index 634aa08..4fa222d 100644 --- a/docs/todo.md +++ b/docs/todo.md @@ -15,7 +15,7 @@ Planned improvements: - [x] [`pin` / `unpin` commands](#pin--unpin-commands) - [x] [Shell completion](#shell-completion) - [x] [`add` writes caret semver constraints](#add-writes-caret-semver-constraints-by-default) -- [ ] [CurseForge bridge](curseforge-bridge.md) +- [x] [CurseForge bridge](curseforge-bridge.md) - [x] [Modrinth API rate-limit handling](#modrinth-api-rate-limit-handling) - [x] Add plugin loader flag to `create` command, mod loader becomes optional in `create` (e.g. for data packs or resource packs) - [x] Allow specifying plugin loader version @@ -23,7 +23,7 @@ Planned improvements: Deferred MVP work: - [x] [Modrinth slug-validity check in `create`](#modrinth-slug-validity-check-in-create) -- [ ] [Hosted source support](curseforge-bridge.md#hosted-modrinth-labrinth) +- [x] [Hosted source support](curseforge-bridge.md#hosted-modrinth-labrinth) - [x] [Plugin-loader support](#plugin-loader-support) - [x] [Global options: `-q`/`--quiet`, `--no-color`, `--config`](#deferred-global-options) (`--offline` shipped per-command) - [x] [Loose-files override support in `.mrpack`](#loose-files-override-support) @@ -503,25 +503,25 @@ Unpack a modpack into a new project directory and reconstruct `mods.yaml` by resolving each file's Modrinth URL back to its slug and version. The source is either a Modrinth project slug (in which case the matching `.mrpack` is downloaded from Modrinth or the server given -by `--hosted` first) or a path to a local `.mrpack` file. Loose +by `--modrinth-host` first) or a path to a local `.mrpack` file. Loose override files from the archive are preserved inside the output directory. Inverse of [`pack`](cli.md#pack). ```text -gitrinth unpack [:] [--hosted ] +gitrinth unpack [:] [--modrinth-host ] [--output ] [--force] [--no-resolve] gitrinth unpack [--output ] [--force] [--no-resolve] ``` -| Option | Description | -|------------------|--------------------------------------------------------------------------------------------------------------------------------------| -| `` | Modrinth project slug. Downloads the matching `.mrpack` before extracting. | -| `` | Version constraint on the slug form. Defaults to the newest release on the resolved server. | -| `` | Path to a local `.mrpack` file. Skips the download step. Chosen when the positional resolves to a file on disk or ends in `.mrpack`. | -| `--hosted ` | Fetch from a Modrinth-compatible server at ``. Slug form only. | -| `--output`, `-o` | Output directory. Defaults to the current directory. | -| `--force`, `-f` | Overwrite existing files in the output directory. | -| `--no-resolve` | Skip the implicit [`get`](cli.md#get) that normally runs after unpacking. | +| Option | Description | +|-------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------| +| `` | Modrinth project slug. Downloads the matching `.mrpack` before extracting. | +| `` | Version constraint on the slug form. Defaults to the newest release on the resolved server. | +| `` | Path to a local `.mrpack` file. Skips the download step. Chosen when the positional resolves to a file on disk or ends in `.mrpack`. | +| `--modrinth-host ` | Fetch from a Modrinth-compatible server at ``. Slug form only. Also written into the reconstructed `mods.yaml` as the pack-level `modrinth_host:`. | +| `--output`, `-o` | Output directory. Defaults to the current directory. | +| `--force`, `-f` | Overwrite existing files in the output directory. | +| `--no-resolve` | Skip the implicit [`get`](cli.md#get) that normally runs after unpacking. | ## Modrinth API rate-limit handling diff --git a/example/example_fabric/mods.yaml b/example/example_fabric/mods.yaml index 0022015..0d1999d 100644 --- a/example/example_fabric/mods.yaml +++ b/example/example_fabric/mods.yaml @@ -5,10 +5,10 @@ description: A new Modrinth modpack. loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 -tooling: - gitrinth: ">=0.1.0 <1.0.0" +gitrinth: + version: ">=0.1.0 <1.0.0" mods: globalpacks: stable diff --git a/example/example_forge/mods.yaml b/example/example_forge/mods.yaml index d38454b..22b92a3 100644 --- a/example/example_forge/mods.yaml +++ b/example/example_forge/mods.yaml @@ -5,10 +5,10 @@ description: A new Modrinth modpack. loader: mods: forge:stable -mc-version: 1.21.1 +mc_version: 1.21.1 -tooling: - gitrinth: ">=0.1.0 <1.0.0" +gitrinth: + version: ">=0.1.0 <1.0.0" mods: globalpacks: stable diff --git a/example/example_neoforge/mods.lock b/example/example_neoforge/mods.lock index 50e195b..6e484e0 100644 --- a/example/example_neoforge/mods.lock +++ b/example/example_neoforge/mods.lock @@ -4,13 +4,13 @@ gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.228" shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: source: modrinth version: 3.0.9+mc1.21 - project-id: EsAfCjCV - version-id: uAKA6Laj + project_id: EsAfCjCV + version_id: uAKA6Laj file: name: appleskin-neoforge-mc1.21-3.0.9.jar url: "https://cdn.modrinth.com/data/EsAfCjCV/versions/uAKA6Laj/appleskin-neoforge-mc1.21-3.0.9.jar" @@ -19,13 +19,13 @@ mods: size: 75804 client: required server: required - game-versions: ["1.21", 1.21.1, 1.21.2] - accepts-mc: [1.21.1] + game_versions: ["1.21", 1.21.1, 1.21.2] + accepts_mc: [1.21.1] balm: source: modrinth version: 21.0.57+neoforge-1.21.1 - project-id: MBAkmtvl - version-id: XC1JUHmA + project_id: MBAkmtvl + version_id: XC1JUHmA file: name: balm-neoforge-1.21.1-21.0.57.jar url: "https://cdn.modrinth.com/data/MBAkmtvl/versions/XC1JUHmA/balm-neoforge-1.21.1-21.0.57.jar" @@ -35,12 +35,12 @@ mods: client: required server: required dependency: transitive - game-versions: [1.21.1] + game_versions: [1.21.1] create: source: modrinth version: 6.0.10+mc1.21.1 - project-id: LNytGWDc - version-id: UjX6dr61 + project_id: LNytGWDc + version_id: UjX6dr61 file: name: create-1.21.1-6.0.10.jar url: "https://cdn.modrinth.com/data/LNytGWDc/versions/UjX6dr61/create-1.21.1-6.0.10.jar" @@ -49,12 +49,12 @@ mods: size: 19123767 client: required server: required - game-versions: [1.21.1] + game_versions: [1.21.1] create-aeronautics: source: modrinth version: 1.1.3+mc1.21.1 - project-id: oWaK0Q19 - version-id: 1sv6OtSz + project_id: oWaK0Q19 + version_id: 1sv6OtSz file: name: create-aeronautics-bundled-1.21.1-1.1.3.jar url: "https://cdn.modrinth.com/data/oWaK0Q19/versions/1sv6OtSz/create-aeronautics-bundled-1.21.1-1.1.3.jar" @@ -63,12 +63,12 @@ mods: size: 33003314 client: required server: required - game-versions: [1.21.1] + game_versions: [1.21.1] distanthorizons: source: modrinth version: 3.0.2-b-1.21.1 - project-id: uCdwusMi - version-id: KkaaQtTD + project_id: uCdwusMi + version_id: KkaaQtTD file: name: DistantHorizons-3.0.2-b-1.21.1-fabric-neoforge.jar url: "https://cdn.modrinth.com/data/uCdwusMi/versions/KkaaQtTD/DistantHorizons-3.0.2-b-1.21.1-fabric-neoforge.jar" @@ -77,12 +77,12 @@ mods: size: 30019727 client: optional server: required - game-versions: [1.21.1] + game_versions: [1.21.1] ferrite-core: source: modrinth version: 7.0.3-neoforge - project-id: uXXizFIs - version-id: x7kQWVju + project_id: uXXizFIs + version_id: x7kQWVju file: name: ferritecore-7.0.3-neoforge.jar url: "https://cdn.modrinth.com/data/uXXizFIs/versions/x7kQWVju/ferritecore-7.0.3-neoforge.jar" @@ -91,12 +91,12 @@ mods: size: 121559 client: required server: required - game-versions: [1.21.1] + game_versions: [1.21.1] globalpacks: source: modrinth version: 21.0.6 - project-id: NRLPy2mk - version-id: FPcUOp35 + project_id: NRLPy2mk + version_id: FPcUOp35 file: name: globalpacks-neoforge-1.21.1-21.0.6.jar url: "https://cdn.modrinth.com/data/NRLPy2mk/versions/FPcUOp35/globalpacks-neoforge-1.21.1-21.0.6.jar" @@ -105,12 +105,12 @@ mods: size: 27239 client: required server: required - game-versions: ["1.21", 1.21.1] + game_versions: ["1.21", 1.21.1] iris: source: modrinth version: 1.8.12+1.21.1-neoforge - project-id: YL57xq9U - version-id: t3ruzodq + project_id: YL57xq9U + version_id: t3ruzodq file: name: iris-neoforge-1.8.12+mc1.21.1.jar url: "https://cdn.modrinth.com/data/YL57xq9U/versions/t3ruzodq/iris-neoforge-1.8.12%2Bmc1.21.1.jar" @@ -119,13 +119,13 @@ mods: size: 2438548 client: required server: unsupported - game-versions: [1.21.1] - accepts-mc: [1.21.1] + game_versions: [1.21.1] + accepts_mc: [1.21.1] jei: source: modrinth version: 19.27.0.340 - project-id: u6dRKJwZ - version-id: YAcQ6elZ + project_id: u6dRKJwZ + version_id: YAcQ6elZ file: name: jei-1.21.1-neoforge-19.27.0.340.jar url: "https://cdn.modrinth.com/data/u6dRKJwZ/versions/YAcQ6elZ/jei-1.21.1-neoforge-19.27.0.340.jar" @@ -134,12 +134,12 @@ mods: size: 1529391 client: required server: required - game-versions: ["1.21", 1.21.1] + game_versions: ["1.21", 1.21.1] netherportalfix: source: modrinth version: 21.1.1+neoforge-1.21.1 - project-id: nPZr02ET - version-id: O09BGtgh + project_id: nPZr02ET + version_id: O09BGtgh file: name: netherportalfix-neoforge-1.21.1-21.1.1.jar url: "https://cdn.modrinth.com/data/nPZr02ET/versions/O09BGtgh/netherportalfix-neoforge-1.21.1-21.1.1.jar" @@ -148,12 +148,12 @@ mods: size: 211972 client: optional server: required - game-versions: ["1.21", 1.21.1] + game_versions: ["1.21", 1.21.1] sable: source: modrinth version: 1.1.3+mc1.21.1 - project-id: T9PomCSv - version-id: g8CObHcP + project_id: T9PomCSv + version_id: g8CObHcP file: name: sable-neoforge-1.21.1-1.1.3.jar url: "https://cdn.modrinth.com/data/T9PomCSv/versions/g8CObHcP/sable-neoforge-1.21.1-1.1.3.jar" @@ -162,12 +162,12 @@ mods: size: 24766579 client: required server: required - game-versions: [1.21.1] + game_versions: [1.21.1] sodium: source: modrinth version: mc1.21.1-0.6.13-neoforge - project-id: AANobbMI - version-id: Pb3OXVqC + project_id: AANobbMI + version_id: Pb3OXVqC file: name: sodium-neoforge-0.6.13+mc1.21.1.jar url: "https://cdn.modrinth.com/data/AANobbMI/versions/Pb3OXVqC/sodium-neoforge-0.6.13%2Bmc1.21.1.jar" @@ -176,13 +176,13 @@ mods: size: 1162994 client: required server: required - game-versions: [1.21.1] + game_versions: [1.21.1] resource_packs: faithful-32x: source: modrinth version: 1.21.1-december-2025 - project-id: w0TnApzs - version-id: Zlz4qPlw + project_id: w0TnApzs + version_id: Zlz4qPlw file: name: Faithful 32x - 1.21.1.zip url: "https://cdn.modrinth.com/data/w0TnApzs/versions/Zlz4qPlw/Faithful%2032x%20-%201.21.1.zip" @@ -191,13 +191,13 @@ resource_packs: size: 10895511 client: optional server: unsupported - game-versions: ["1.21", 1.21.1] + game_versions: ["1.21", 1.21.1] data_packs: terralith: source: modrinth version: 2.5.8 - project-id: 8oi3bsk5 - version-id: urbokcOc + project_id: 8oi3bsk5 + version_id: urbokcOc file: name: Terralith_1.21_v2.5.8.zip url: "https://cdn.modrinth.com/data/8oi3bsk5/versions/urbokcOc/Terralith_1.21_v2.5.8.zip" @@ -206,12 +206,12 @@ data_packs: size: 3112105 client: required server: required - game-versions: ["1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4] + game_versions: ["1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4] terralith-restoned: source: modrinth version: 1.3.1 - project-id: oTQsXi8U - version-id: QbaRDOcI + project_id: oTQsXi8U + version_id: QbaRDOcI file: name: Terralith_ReStoned_v1.3.1.zip url: "https://cdn.modrinth.com/data/oTQsXi8U/versions/QbaRDOcI/Terralith_ReStoned_v1.3.1.zip" @@ -220,13 +220,13 @@ data_packs: size: 6937 client: required server: required - game-versions: ["1.20", 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6, "1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5, 1.21.6, 1.21.7, 1.21.8, 1.21.9, 1.21.10, 1.21.11, "26.1", 26.1.1, 26.1.2] + game_versions: ["1.20", 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6, "1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5, 1.21.6, 1.21.7, 1.21.8, 1.21.9, 1.21.10, 1.21.11, "26.1", 26.1.1, 26.1.2] shaders: complementary-reimagined: source: modrinth version: r5.7.1 - project-id: HVnmMxH1 - version-id: 836bPNGo + project_id: HVnmMxH1 + version_id: 836bPNGo file: name: ComplementaryReimagined_r5.7.1.zip url: "https://cdn.modrinth.com/data/HVnmMxH1/versions/836bPNGo/ComplementaryReimagined_r5.7.1.zip" @@ -235,6 +235,6 @@ shaders: size: 522970 client: required server: unsupported - game-versions: [1.7.10, "1.8", 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, "1.9", 1.9.1, 1.9.2, 1.9.3, 1.9.4, "1.10", 1.10.1, 1.10.2, "1.11", 1.11.1, 1.11.2, "1.12", 1.12.1, 1.12.2, "1.13", 1.13.1, 1.13.2, "1.14", 1.14.1, 1.14.2, 1.14.3, 1.14.4, "1.15", 1.15.1, 1.15.2, "1.16", 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, "1.17", 1.17.1, "1.18", 1.18.1, 1.18.2, "1.19", 1.19.1, 1.19.2, 1.19.3, 1.19.4, "1.20", 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6, "1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5, 1.21.6, 1.21.7, 1.21.8, 1.21.9, 1.21.10, 1.21.11, "26.1", 26.1.1, 26.1.2] + game_versions: [1.7.10, "1.8", 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, "1.9", 1.9.1, 1.9.2, 1.9.3, 1.9.4, "1.10", 1.10.1, 1.10.2, "1.11", 1.11.1, 1.11.2, "1.12", 1.12.1, 1.12.2, "1.13", 1.13.1, 1.13.2, "1.14", 1.14.1, 1.14.2, 1.14.3, 1.14.4, "1.15", 1.15.1, 1.15.2, "1.16", 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, "1.17", 1.17.1, "1.18", 1.18.1, 1.18.2, "1.19", 1.19.1, 1.19.2, 1.19.3, 1.19.4, "1.20", 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6, "1.21", 1.21.1, 1.21.2, 1.21.3, 1.21.4, 1.21.5, 1.21.6, 1.21.7, 1.21.8, 1.21.9, 1.21.10, 1.21.11, "26.1", 26.1.1, 26.1.2] plugins: {} files: {} diff --git a/example/example_neoforge/mods.yaml b/example/example_neoforge/mods.yaml index 12b727f..55214df 100644 --- a/example/example_neoforge/mods.yaml +++ b/example/example_neoforge/mods.yaml @@ -37,13 +37,15 @@ loader: shaders: iris # Enum: iris, optifine, canvas, vanilla. # plugins: sponge # Enum: sponge, bukkit, folia, paper, spigot. -mc-version: 1.21.1 # Exact version only — no ranges. +mc_version: 1.21.1 # Exact version only — no ranges. -tooling: - gitrinth: ">=1.0.0 < 2.0.0" # Semver range constraint on the gitrinth CLI version. +gitrinth: + version: ">=1.0.0 < 2.0.0" # Semver range constraint on the gitrinth CLI version. mods: - # Keys are Modrinth project slugs (modrinth.com/mod/). + # Keys are local identifiers; default to the Modrinth project slug + # (modrinth.com/mod/). Use `modrinth:` / `curseforge:` to + # override the platform's slug when the section key differs. # Caution: `url` and `path` sources make the modpack unpublishable on modrinth. create: ^6.0.10 # Downloads version 6.0.10+mc1.21.1 or later stable 6.x releases. create-aeronautics: ^1.1.3 # 1.1.0+mc1.21.1 @@ -66,7 +68,7 @@ mods: # both. Set a side to `unsupported` to drop it there. client: required server: unsupported - accepts-mc: + accepts_mc: - 1.21.1 # Use a mod that doesn't declare mc-version support netherportalfix: version: ^21.1.1 @@ -75,7 +77,7 @@ mods: server: required appleskin: version: ^3.0.9 - accepts-mc: 1.21.1 # Scalar shorthand for [1.21.1] + accepts_mc: 1.21.1 # Scalar shorthand for [1.21.1] globalpacks: ^21.0.6 # Semver range constraint — quote because bare < and > would # confuse the YAML parser. Pairs of bounds intersect; either side @@ -99,7 +101,7 @@ plugins: project_overrides: # jei: # version: 19.27.0.340 - # # hosted: https://some-modrinth-server.com # Optional. Specify a custom modrinth server to download the mod from. + # # modrinth_host: https://some-modrinth-server.com # Optional. Specify a custom Modrinth-compatible server to download the mod from. # # url: https://example.com/path/to/jei.jar # # path: ./path/to/jei.jar # Optional. Specify a local path to a mod instead of downloading it from modrinth. diff --git a/lib/src/app/loader_registry_provider.dart b/lib/src/app/loader_registry_provider.dart new file mode 100644 index 0000000..7a29c91 --- /dev/null +++ b/lib/src/app/loader_registry_provider.dart @@ -0,0 +1,153 @@ +import 'package:path/path.dart' as p; +import 'package:riverpod/riverpod.dart'; + +import '../cli/exceptions.dart'; +import '../model/manifest/mods_yaml.dart'; +import '../service/loader_catalog_cache.dart'; +import '../service/loader_descriptor_config.dart'; +import '../service/loader_registry.dart'; +import '../service/modrinth/modrinth_url.dart'; +import '../service/user_config.dart'; +import 'env.dart'; +import 'providers.dart'; +import 'runner_settings.dart'; + +class LoaderRegistryNotifier extends Notifier { + @override + LoaderRegistry build() { + final settings = ref.watch(runnerSettingsProvider); + final explicit = settings.loaderDescriptorsPath; + String path; + try { + path = + explicit ?? + p.join( + p.dirname( + resolveUserConfigPath( + ref.read(environmentProvider), + override: settings.configPath, + ), + ), + 'loaders.yaml', + ); + } on UserError { + return LoaderRegistry.builtins; + } + return LoaderDescriptorConfig.read(path, required: explicit != null); + } + + void replace(LoaderRegistry registry) => state = registry; +} + +final loaderRegistryProvider = + NotifierProvider( + LoaderRegistryNotifier.new, + ); + +Future prepareLoaderRegistry( + ProviderContainer container, + ModsYaml manifest, { + required bool offline, +}) async { + final cfFactory = container.read(curseForgeApiFactoryProvider); + var registry = container.read(loaderRegistryProvider); + final defaultHost = resolveModrinthBaseUrl( + container.read(environmentProvider), + ); + final hosts = { + manifest.modrinthHost ?? defaultHost, + for (final section in Section.values) + for (final entry in manifest.sectionEntries(section).values) + manifest.effectiveModrinthHost(entry, defaultHost), + }; + final discoverMr = + manifest.gitrinth.isPlatformEnabled(SourceKind.modrinth) || + Section.values.any( + (section) => manifest + .sectionEntries(section) + .values + .any( + (entry) => entry.sources?.contains(SourceKind.modrinth) ?? false, + ), + ); + final discoverCf = + manifest.gitrinth.isPlatformEnabled(SourceKind.curseforge) || + Section.values.any( + (section) => manifest + .sectionEntries(section) + .values + .any( + (entry) => + entry.sources?.contains(SourceKind.curseforge) ?? false, + ), + ); + final ids = [ + manifest.loader.mods, + ?manifest.loader.plugins, + ?manifest.loader.shaders, + ]; + final catalogTag = + !offline && + [ + (manifest.loader.mods, manifest.loader.modLoaderVersion), + if (manifest.loader.plugins != null) + (manifest.loader.plugins, manifest.loader.pluginLoaderVersion), + ].any( + (pair) => + registry[pair.$1]?.versionSource == VersionSourceFamily.catalog && + const {'stable', 'latest'}.contains(pair.$2), + ); + final needsCatalog = + catalogTag || + ids.any( + (id) => + registry[id] == null || + (discoverMr && + registry[id]?.role != LoaderRole.vanilla && + hosts.any((host) => registry[id]?.modrinthTag(host) == null)), + ); + if (needsCatalog) { + for (final host in hosts) { + final service = LoaderRegistryService( + configured: registry, + cache: LoaderCatalogCache(container.read(cacheProvider)), + dio: container.read(dioProvider), + ); + registry = await service.snapshot( + modrinthHost: host, + curseforgeHost: cfFactory.baseUrl, + needed: ids, + offline: offline, + refresh: catalogTag, + discoverModrinth: discoverMr, + readCurseforge: discoverCf ? cfFactory.listMinecraftLoaders : null, + ); + } + } + for (final pair in [ + ( + manifest.loader.mods, + manifest.loader.hasModRuntime ? LoaderRole.mod : LoaderRole.vanilla, + ), + if (manifest.loader.plugins != null) + (manifest.loader.plugins, LoaderRole.plugin), + if (manifest.loader.shaders != null) + (manifest.loader.shaders, LoaderRole.shader), + ]) { + final id = pair.$1; + if (id == null) continue; + final role = registry[id]?.role; + if (role != pair.$2 && id != LoaderId.vanilla) { + throw ValidationError( + 'Loader ${id.name} has role ${role?.name}, not ${pair.$2.name}.', + ); + } + } + validateLoaderCompatibility( + registry, + manifest.loader.mods, + manifest.loader.plugins, + ); + container.read(loaderRegistryProvider.notifier).replace(registry); + return registry; +} diff --git a/lib/src/app/providers.dart b/lib/src/app/providers.dart index 80437ca..7a437d8 100644 --- a/lib/src/app/providers.dart +++ b/lib/src/app/providers.dart @@ -10,23 +10,27 @@ import '../service/java_runtime_fetcher.dart'; import '../service/java_runtime_resolver.dart'; import '../service/loader_binary_fetcher.dart'; import '../service/loader_client_installer.dart'; -import '../service/mod_loader_version_resolver.dart'; +import '../service/resolve/mod_loader_version_resolver.dart'; import '../service/minecraft_launcher_locator.dart'; import '../service/paper_api_client.dart'; -import '../service/plugin_loader_version_resolver.dart'; +import '../service/resolve/plugin_loader_version_resolver.dart'; import '../service/server_installer.dart'; import '../service/sponge_api_client.dart'; import '../service/vanilla_server_source.dart'; -import '../service/modrinth_api.dart'; -import '../service/modrinth_auth_interceptor.dart'; -import '../service/modrinth_error_interceptor.dart'; -import '../service/modrinth_rate_limit_interceptor.dart'; -import '../service/modrinth_url.dart'; +import '../service/curseforge/curseforge_api_factory.dart'; +import '../service/curseforge/curseforge_auth_interceptor.dart'; +import '../service/curseforge/curseforge_url.dart'; +import '../service/modrinth/modrinth_api_factory.dart'; +import '../service/modrinth/modrinth_auth_interceptor.dart'; +import '../service/modrinth/modrinth_error_interceptor.dart'; +import '../service/modrinth/modrinth_rate_limit_interceptor.dart'; +import '../service/modrinth/modrinth_url.dart'; import '../service/offline_guard_interceptor.dart'; import '../service/user_config.dart'; import '../cli/exceptions.dart'; import '../version.dart'; import 'env.dart'; +import 'loader_registry_provider.dart'; import 'offline_notifier.dart'; import 'runner_settings.dart'; @@ -81,12 +85,37 @@ final dioProvider = Provider((ref) { return dio; }); -final modrinthApiProvider = Provider( - (ref) => ModrinthApi( - ref.read(dioProvider), - baseUrl: resolveModrinthBaseUrl(ref.read(environmentProvider)), - ), -); +final modrinthApiFactoryProvider = Provider((ref) { + final factory = ModrinthApiFactory( + console: ref.read(consoleProvider), + auth: ref.read(modrinthAuthInterceptorProvider), + offline: () => ref.read(offlineProvider), + defaultBaseUrl: resolveModrinthBaseUrl(ref.read(environmentProvider)), + ); + ref.onDispose(factory.close); + return factory; +}); + +final curseForgeAuthInterceptorProvider = Provider(( + ref, +) { + final env = ref.read(environmentProvider); + return CurseForgeAuthInterceptor( + envTokenLookup: () => env['GITRINTH_CURSEFORGE_TOKEN'], + baseUrl: resolveCurseForgeBaseUrl(env), + ); +}); + +final curseForgeApiFactoryProvider = Provider((ref) { + final factory = CurseForgeApiFactory( + console: ref.read(consoleProvider), + auth: ref.read(curseForgeAuthInterceptorProvider), + offline: () => ref.read(offlineProvider), + baseUrl: resolveCurseForgeBaseUrl(ref.read(environmentProvider)), + ); + ref.onDispose(factory.close); + return factory; +}); final cacheProvider = Provider( (ref) => GitrinthCache( @@ -109,6 +138,7 @@ final modLoaderVersionResolverProvider = Provider( final loaderBinaryFetcherProvider = Provider( (ref) => LoaderBinaryFetcher( + registry: ref.watch(loaderRegistryProvider), cache: ref.read(cacheProvider), downloader: ref.read(downloaderProvider), environment: ref.read(environmentProvider), @@ -142,6 +172,7 @@ final vanillaServerSourceProvider = Provider( final serverInstallerProvider = Provider( (ref) => ServerInstaller( + registry: ref.watch(loaderRegistryProvider), environment: ref.read(environmentProvider), resolver: ref.read(javaRuntimeResolverProvider), console: ref.read(consoleProvider), @@ -154,6 +185,7 @@ final minecraftLauncherLocatorProvider = Provider( final loaderClientInstallerProvider = Provider( (ref) => LoaderClientInstaller( + registry: ref.watch(loaderRegistryProvider), environment: ref.read(environmentProvider), resolver: ref.read(javaRuntimeResolverProvider), console: ref.read(consoleProvider), diff --git a/lib/src/app/runner_settings.dart b/lib/src/app/runner_settings.dart index 5b32f66..0905a3b 100644 --- a/lib/src/app/runner_settings.dart +++ b/lib/src/app/runner_settings.dart @@ -10,11 +10,13 @@ class RunnerSettings { final bool? color; final String? configPath; + final String? loaderDescriptorsPath; const RunnerSettings({ this.level = LogLevel.normal, this.color, this.configPath, + this.loaderDescriptorsPath, }); } diff --git a/lib/src/asset_strings.g.dart b/lib/src/asset_strings.g.dart index 922c4f0..0adddbf 100644 --- a/lib/src/asset_strings.g.dart +++ b/lib/src/asset_strings.g.dart @@ -21,7 +21,7 @@ final String modrinthIgnoreTemplate = utf8.decode( ); final String modsYamlTemplate = utf8.decode( base64.decode( - 'c2x1Zzoge3tzbHVnfX0NCm5hbWU6IHt7bmFtZX19DQp2ZXJzaW9uOiB7e3ZlcnNpb259fQ0KZGVzY3JpcHRpb246IHt7ZGVzY3JpcHRpb259fQ0KDQpsb2FkZXI6Cnt7bG9hZGVyLWJsb2NrfX0KbWMtdmVyc2lvbjoge3ttYy12ZXJzaW9ufX0KDQp0b29saW5nOg0KICBnaXRyaW50aDogIj49e3tnaXRyaW50aC12ZXJzaW9ufX0gPHt7Z2l0cmludGgtbmV4dC1tYWpvcn19Ig0KDQptb2RzOg0KICBnbG9iYWxwYWNrczogc3RhYmxlDQoNCnJlc291cmNlX3BhY2tzOg0KDQpkYXRhX3BhY2tzOg0KDQpzaGFkZXJzOg0KDQpwbHVnaW5zOg0K', + 'c2x1Zzoge3tzbHVnfX0NCm5hbWU6IHt7bmFtZX19DQp2ZXJzaW9uOiB7e3ZlcnNpb259fQ0KZGVzY3JpcHRpb246IHt7ZGVzY3JpcHRpb259fQ0KDQpsb2FkZXI6DQp7e2xvYWRlci1ibG9ja319DQptY192ZXJzaW9uOiB7e21jX3ZlcnNpb259fQ0KDQpnaXRyaW50aDoNCiAgdmVyc2lvbjogIj49e3tnaXRyaW50aC12ZXJzaW9ufX0gPHt7Z2l0cmludGgtbmV4dC1tYWpvcn19Ig0KDQptb2RzOg0KICBnbG9iYWxwYWNrczogc3RhYmxlDQoNCnJlc291cmNlX3BhY2tzOg0KDQpkYXRhX3BhY2tzOg0KDQpzaGFkZXJzOg0KDQpwbHVnaW5zOg0K', ), ); final String readmeTemplate = utf8.decode( diff --git a/lib/src/cli/base_command.dart b/lib/src/cli/base_command.dart index a8a16b2..52a6954 100644 --- a/lib/src/cli/base_command.dart +++ b/lib/src/cli/base_command.dart @@ -2,15 +2,25 @@ import 'package:args/command_runner.dart'; import 'package:riverpod/riverpod.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/resolver/version_selection.dart'; import '../service/console.dart'; import '../service/manifest_io.dart'; -import '../service/resolve_and_sync.dart'; +import '../service/resolve/resolve_and_sync.dart'; import 'exceptions.dart'; import 'runner.dart'; abstract class GitrinthCommand extends Command { + GitrinthCommand() { + argParser.addOption( + 'loader-descriptors', + valueHelp: 'path', + help: + 'User-level declarative loader mappings (defaults to loaders.yaml beside user config).', + ); + } + GitrinthRunner get gitrinthRunner => runner as GitrinthRunner; ProviderContainer get container => gitrinthRunner.container; @@ -75,13 +85,21 @@ abstract class GitrinthCommand extends Command { bool enforce = false, Set freshSlugs = const {}, Set relaxConstraints = const {}, + Set allowMissingSourcesForSlugs = const {}, ModsYaml? manifestOverride, SolveType solveType = SolveType.get, - }) { + }) async { + final registry = await prepareLoaderRegistry( + container, + manifestOverride ?? io.readModsYaml(), + offline: offline, + ); return resolveAndSync( io: io, + loaderRegistry: registry, console: console, - api: read(modrinthApiProvider), + apiFactory: read(modrinthApiFactoryProvider), + cfFactory: read(curseForgeApiFactoryProvider), cache: read(cacheProvider), downloader: read(downloaderProvider), modLoaderResolver: read(modLoaderVersionResolverProvider), @@ -89,8 +107,12 @@ abstract class GitrinthCommand extends Command { offline: offline, dryRun: dryRun, enforce: enforce, + noSearch: + argResults?.options.contains('search') == true && + !(argResults!['search'] as bool), freshSlugs: freshSlugs, relaxConstraints: relaxConstraints, + allowMissingSourcesForSlugs: allowMissingSourcesForSlugs, manifestOverride: manifestOverride, solveType: solveType, ); diff --git a/lib/src/cli/runner.dart b/lib/src/cli/runner.dart index 46d9c2d..e2d0118 100644 --- a/lib/src/cli/runner.dart +++ b/lib/src/cli/runner.dart @@ -20,6 +20,9 @@ import '../commands/get_command.dart'; import '../commands/launch_command.dart'; import '../commands/migrate_command.dart'; import '../commands/modrinth_command.dart'; +import '../commands/platform_command.dart'; +import '../commands/token_command.dart'; +import '../commands/publish_command.dart'; import '../commands/outdated_command.dart'; import '../commands/override_command.dart'; import '../commands/pack_command.dart'; @@ -113,6 +116,9 @@ class GitrinthRunner extends CommandRunner { addCommand(MigrateCommand()); addCommand(CacheCommand()); addCommand(ModrinthCommand()); + addCommand(CurseForgeCommand()); + addCommand(TokenCommand()); + addCommand(PublishCommand()); addCommand(CompletionCommand()); } @@ -161,10 +167,25 @@ class GitrinthRunner extends CommandRunner { : null; configPath = topLevelResults['config'] as String?; + var commandResults = topLevelResults; + while (commandResults.command != null) { + final next = commandResults.command; + if (next == null) break; + commandResults = next; + } + final descriptorsPath = + commandResults.options.contains('loader-descriptors') + ? commandResults['loader-descriptors'] as String? + : null; container .read(runnerSettingsProvider.notifier) .set( - RunnerSettings(level: level, color: color, configPath: configPath), + RunnerSettings( + level: level, + color: color, + configPath: configPath, + loaderDescriptorsPath: descriptorsPath, + ), ); if (topLevelResults['version'] as bool) { diff --git a/lib/src/commands/_constraint_helpers.dart b/lib/src/commands/_constraint_helpers.dart index eced1a9..7356ed3 100644 --- a/lib/src/commands/_constraint_helpers.dart +++ b/lib/src/commands/_constraint_helpers.dart @@ -1,4 +1,49 @@ import '../cli/exceptions.dart'; +import '../model/resolver/constraint.dart'; +import 'version_picker.dart'; + +String constraintForSelectedVersion( + String version, { + required bool exact, + required bool pin, +}) => exact + ? '^$version' + : pin + ? bareVersionForPin(version) + : caretOrPinFallback(version); + +String validatedEntryConstraint(String constraint) { + if (parseChannelToken(constraint) == null) parseConstraint(constraint); + return constraint; +} + +Map versionEntryFields( + String version, { + String? env, + List acceptsMc = const [], +}) { + final fields = {'version': version}; + writeSideFields(fields, env); + if (acceptsMc.isNotEmpty) { + fields['accepts_mc'] = acceptsMc.length == 1 ? acceptsMc.first : acceptsMc; + } + return fields; +} + +String noCompatibleReleaseMessage( + String slug, { + required String? loaderName, + required String mcVersion, + required List acceptsMc, +}) { + final widened = acceptsMc.isEmpty + ? '' + : ' (widened with accepts-mc=${acceptsMc.join(",")})'; + return "No release version of '$slug' is compatible with " + "loader=${loaderName ?? ''} " + 'mc=$mcVersion$widened. ' + 'Pass `@` explicitly to pin an alpha/beta.'; +} /// Accepts releases, pre/rc, and snapshots; Modrinth validates the /// actual tag server-side. diff --git a/lib/src/commands/add_command.dart b/lib/src/commands/add_command.dart index e0448fd..1a22f2e 100644 --- a/lib/src/commands/add_command.dart +++ b/lib/src/commands/add_command.dart @@ -4,20 +4,24 @@ import 'dart:io'; import 'package:dio/dio.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; -import '../model/manifest/mods_lock.dart'; import '../model/manifest/mods_yaml.dart'; +import '../model/manifest/parser.dart'; +import '../model/manifest/source_eligibility.dart'; +import '../model/curseforge/cf_constants.dart'; +import '../service/curseforge/curseforge_filters.dart'; +import '../service/curseforge/curseforge_version.dart'; import '../model/modrinth/dependency.dart'; import '../model/modrinth/project.dart'; import '../model/modrinth/version.dart' as modrinth; -import '../model/resolver/constraint.dart'; import '../service/cache.dart'; import '../service/manifest_io.dart'; -import '../service/section_inference.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/section_inference.dart'; +import '../service/resolve/solve_report.dart'; import '_constraint_helpers.dart'; import 'add_command_editor.dart'; import 'slug_constraint_parser.dart'; @@ -35,6 +39,19 @@ class AddCommand extends GitrinthCommand with OfflineFlag { AddCommand() { argParser + ..addFlag('modrinth', help: 'Resolve on Modrinth for this entry.') + ..addFlag('curseforge', help: 'Resolve on CurseForge for this entry.') + ..addFlag( + 'allow-hash-mismatch', + negatable: false, + help: 'Accept and record different platform hashes.', + ) + ..addFlag( + 'search', + defaultsTo: true, + help: + 'Search for cross-platform peers when a slug is missing or hashes differ.', + ) ..addOption( 'env', allowed: ['client', 'server', 'both'], @@ -128,7 +145,13 @@ class AddCommand extends GitrinthCommand with OfflineFlag { ); } - final (:slug, :constraintRaw) = parseSlugConstraint(positional); + final cfSugar = positional.startsWith('cf:'); + final (:slug, :constraintRaw) = parseSlugConstraint( + cfSugar ? positional.substring(3) : positional, + ); + if (slug.isEmpty) { + throw const UsageError('Missing CurseForge slug after cf:.'); + } validateConstraintFlags( exact: exactFlag, pin: pinFlag, @@ -137,6 +160,27 @@ class AddCommand extends GitrinthCommand with OfflineFlag { final io = ManifestIo(); final existingManifest = io.readModsYaml(); + await prepareLoaderRegistry(container, existingManifest, offline: offline); + + final platformFlags = + argResults!.wasParsed('modrinth') || + argResults!.wasParsed('curseforge'); + final mismatch = argResults!['allow-hash-mismatch'] as bool; + if ((cfSugar || platformFlags || mismatch) && + (urlOpt != null || pathOpt != null)) { + throw const UsageError( + 'Platform options cannot be combined with --url or --path.', + ); + } + if (cfSugar && + ((argResults!.wasParsed('modrinth') && + argResults!['modrinth'] == true) || + (argResults!.wasParsed('curseforge') && + argResults!['curseforge'] == false))) { + throw const UsageError( + 'cf: requires CurseForge enabled and Modrinth disabled.', + ); + } // Duplicate check — scan every section. `dart pub add` rejects adds for // already-declared packages; we mirror that. @@ -150,6 +194,33 @@ class AddCommand extends GitrinthCommand with OfflineFlag { } } + if (urlOpt == null && + pathOpt == null && + (cfSugar || + platformFlags || + mismatch || + existingManifest.gitrinth.isPlatformEnabled( + SourceKind.curseforge, + ) || + !existingManifest.gitrinth.isPlatformEnabled( + SourceKind.modrinth, + ))) { + return _addPlatformEntry( + io: io, + manifest: existingManifest, + slug: slug, + constraint: constraintRaw, + cfSugar: cfSugar, + sectionOverride: typeOverride, + env: envOpt, + acceptsMc: acceptsMc, + exact: exactFlag, + pin: pinFlag, + dryRun: dryRun, + offline: offline, + ); + } + final Section section; final String? writtenValue; final Map? longForm; @@ -184,8 +255,9 @@ class AddCommand extends GitrinthCommand with OfflineFlag { longForm = long; writtenValue = null; } else { - // Modrinth source. - final api = read(modrinthApiProvider); + final api = read( + modrinthApiFactoryProvider, + ).forHost(existingManifest.modrinthHost); final Project project; try { project = await api.getProject(slug); @@ -214,6 +286,10 @@ class AddCommand extends GitrinthCommand with OfflineFlag { final String effectiveConstraint; if (constraintRaw == null) { final latest = await pickLatestReleaseVersion( + registry: read(loaderRegistryProvider), + host: + existingManifest.modrinthHost ?? + read(modrinthApiFactoryProvider).defaultBaseUrl, api: api, slug: slug, section: section, @@ -222,14 +298,16 @@ class AddCommand extends GitrinthCommand with OfflineFlag { acceptsMc: acceptsMc, ); if (latest == null) { - final widened = acceptsMc.isEmpty - ? '' - : ' (widened with accepts-mc=${acceptsMc.join(",")})'; throw UserError( - "No release version of '$slug' is compatible with " - "loader=${loaderNameForSection(existingManifest.loader, section) ?? ''} " - "mc=${existingManifest.mcVersion}$widened. " - 'Pass `@` explicitly to pin an alpha/beta.', + noCompatibleReleaseMessage( + slug, + loaderName: loaderNameForSection( + existingManifest.loader, + section, + ), + mcVersion: existingManifest.mcVersion, + acceptsMc: acceptsMc, + ), ); } await _validateNoIncompatibility( @@ -240,43 +318,22 @@ class AddCommand extends GitrinthCommand with OfflineFlag { newProjectId: project.id, pickedVersion: latest, ); - if (exactFlag) { - effectiveConstraint = '^${latest.versionNumber}'; - } else if (pinFlag) { - effectiveConstraint = bareVersionForPin(latest.versionNumber); - } else { - // Default: caret on major.minor.patch. If Modrinth's version - // isn't semver-shaped (some mods use arbitrary strings), - // carets are meaningless — fall back to pinning the raw - // version verbatim. - effectiveConstraint = caretOrPinFallback(latest.versionNumber); - } + effectiveConstraint = constraintForSelectedVersion( + latest.versionNumber, + exact: exactFlag, + pin: pinFlag, + ); } else { - // Validate the user-supplied constraint so a bad `@xyz` fails fast - // with a single-line error before we touch mods.yaml. - final channel = parseChannelToken(constraintRaw); - if (channel == null) { - parseConstraint(constraintRaw); // throws ValidationError on bad input - } - effectiveConstraint = constraintRaw; + effectiveConstraint = validatedEntryConstraint(constraintRaw); } - final needsLongForm = - (envOpt != null && envOpt != 'both') || acceptsMc.isNotEmpty; - if (needsLongForm) { - final long = {'version': effectiveConstraint}; - writeSideFields(long, envOpt); - if (acceptsMc.isNotEmpty) { - long['accepts-mc'] = acceptsMc.length == 1 - ? acceptsMc.first - : acceptsMc; - } - longForm = long; - writtenValue = null; - } else { - longForm = null; - writtenValue = effectiveConstraint; - } + final fields = versionEntryFields( + effectiveConstraint, + env: envOpt, + acceptsMc: acceptsMc, + ); + longForm = fields.length == 1 ? null : fields; + writtenValue = longForm == null ? effectiveConstraint : null; } if (section == Section.mods && !existingManifest.loader.hasModRuntime) { @@ -327,6 +384,250 @@ class AddCommand extends GitrinthCommand with OfflineFlag { return exitOk; } + Future _addPlatformEntry({ + required ManifestIo io, + required ModsYaml manifest, + required String slug, + required String? constraint, + required bool cfSugar, + required Section? sectionOverride, + required String? env, + required List acceptsMc, + required bool exact, + required bool pin, + required bool dryRun, + required bool offline, + }) async { + final requested = {}; + for (final kind in SourceKind.values) { + final selected = cfSugar + ? kind == SourceKind.curseforge + : argResults!.wasParsed(kind.name) + ? argResults![kind.name] as bool + : manifest.gitrinth.isPlatformEnabled(kind); + if (selected) requested.add(kind); + } + if (requested.isEmpty) { + throw const UsageError('At least one platform must be enabled for add.'); + } + var section = sectionOverride; + section ??= + !manifest.loader.hasModRuntime && manifest.loader.plugins != null + ? Section.plugins + : null; + void checkEligibility() { + if (section != null && + requested.contains(SourceKind.curseforge) && + !sectionAllowsCurseforge(section, manifest.loader.plugins)) { + if (cfSugar || + argResults!.wasParsed('curseforge') || + (argResults!.wasParsed('modrinth') && + argResults!['modrinth'] == false)) { + throw const ValidationError( + 'CurseForge plugins require loader.plugins bukkit, spigot, or paper. See the source eligibility matrix.', + ); + } + requested.remove(SourceKind.curseforge); + } + } + + checkEligibility(); + if (section == null && requested.contains(SourceKind.modrinth)) { + try { + final project = await read( + modrinthApiFactoryProvider, + ).forHost(manifest.modrinthHost).getProject(slug); + section = inferSectionFromProject( + projectType: project.projectType, + loaders: project.loaders, + ); + } on DioException catch (e) { + if (e.response?.statusCode != 404) rethrow; + } + } + if (section == null && requested.contains(SourceKind.curseforge)) { + final api = read(curseForgeApiFactoryProvider).api; + if (int.tryParse(slug) case final int id) { + final project = (await api.getMod(id)).data; + section = Section.values + .where((s) => s.cfClassId == project.classId) + .firstOrNull; + } else { + for (final candidate in Section.values) { + if (!sectionAllowsCurseforge(candidate, manifest.loader.plugins)) { + continue; + } + if (await findModBySlug(api, slug: slug, section: candidate) != + null) { + section = candidate; + break; + } + } + } + } + if (section == null) throw UserError('No eligible platform has "$slug".'); + checkEligibility(); + if (requested.isEmpty) { + throw const UsageError( + 'No eligible platform is enabled for this section.', + ); + } + if (section == Section.mods && !manifest.loader.hasModRuntime) { + throw UserError('cannot add mod "$slug": loader.mods is not set.'); + } + final original = File(io.modsYamlPath).readAsStringSync(); + final fields = { + 'version': constraint ?? 'release', + 'sources': requested.map((k) => k.name).toList(), + if (argResults!['allow-hash-mismatch'] as bool) + 'allow_hash_mismatch': true, + if (acceptsMc.isNotEmpty) + 'accepts_mc': acceptsMc.length == 1 ? acceptsMc.first : acceptsMc, + }; + writeSideFields(fields, env); + String render(Map value) => injectEntry( + original, + section: section!, + slug: slug, + shorthandValue: value.length == 1 ? value['version'] as String : null, + longForm: value.length == 1 ? null : value, + ); + final preview = + await runResolveAndSync( + io: io, + offline: offline, + dryRun: true, + manifestOverride: parseModsYaml( + render(fields), + filePath: io.modsYamlPath, + ), + allowMissingSourcesForSlugs: {slug}, + ).catchError((Object error) { + if (error is ValidationError) { + throw ValidationError( + '${error.message}\nFor add, select a source with ' + '--[no-]modrinth / --[no-]curseforge, or accept divergent hashes ' + 'with --allow-hash-mismatch.', + ); + } + throw error; + }); + final locked = preview.newLock?.allEntries + .where((e) => e.key == slug) + .firstOrNull + ?.value; + if (locked == null) { + throw UserError( + 'No eligible platform has a compatible version of "$slug".', + ); + } + final resolved = { + if (locked.modrinth != null) SourceKind.modrinth, + if (locked.curseforge != null) SourceKind.curseforge, + }; + if (resolved.isEmpty) { + throw UserError( + 'No eligible platform has a compatible version of "$slug".', + ); + } + if (locked.modrinth != null && locked.curseforge != null) { + final latestMr = locked.modrinth!.discoveredViaSearch + ? null + : await pickLatestReleaseVersion( + registry: read(loaderRegistryProvider), + host: + manifest.modrinthHost ?? + read(modrinthApiFactoryProvider).defaultBaseUrl, + api: read( + modrinthApiFactoryProvider, + ).forHost(manifest.modrinthHost), + slug: slug, + section: section, + loaderConfig: manifest.loader, + mcVersion: manifest.mcVersion, + acceptsMc: acceptsMc, + ); + final latestCf = await listCompatibleFiles( + registry: read(loaderRegistryProvider), + api: read(curseForgeApiFactoryProvider).api, + projectId: locked.curseforge!.projectId, + section: section, + modLoader: section == Section.mods ? manifest.loader.mods : null, + pluginLoader: manifest.loader.plugins, + gameVersions: [manifest.mcVersion, ...acceptsMc], + channel: Channel.release, + ); + if ((latestMr != null && latestMr.id != locked.modrinth!.versionId) || + (latestCf.isNotEmpty && + latestCf.first.id != locked.curseforge!.fileId)) { + console.message( + 'Selected matching version ${locked.modrinth!.versionNumber}; ' + 'latest Modrinth: ${latestMr?.versionNumber ?? ""}, ' + 'latest CurseForge: ${latestCf.firstOrNull?.displayName ?? ""}.', + ); + } + } + if (constraint == null) { + var version = locked.modrinth?.versionNumber; + if (version == null && locked.curseforge != null) { + final cf = locked.curseforge!; + final file = (await read( + curseForgeApiFactoryProvider, + ).api.getFile(cf.projectId, cf.fileId)).data; + version = curseForgeFileVersion(file); + } + // CF display names need not contain a semantic version. + version ??= 'release'; + fields['version'] = constraintForSelectedVersion( + version, + exact: exact, + pin: pin, + ); + } + final explicit = + cfSugar || + argResults!.wasParsed('modrinth') || + argResults!.wasParsed('curseforge'); + final defaults = effectiveSources(manifest, ModEntry(slug: slug), section); + if (resolved.length == requested.length && + !explicit && + defaults.length == resolved.length && + defaults.containsAll(resolved)) { + fields.remove('sources'); + } else { + fields['sources'] = cfSugar + ? 'curseforge' + : resolved.map((k) => k.name).toList(); + } + if (resolved.length < requested.length) { + console.message( + 'Only ${resolved.single.name} has a compatible version of $slug; restricting sources.', + ); + } + final updated = render(fields); + parseModsYaml(updated, filePath: io.modsYamlPath); + if (dryRun) { + console.message('Would add to ${sectionKeyFor(section)}:'); + console.message( + _describeEntry( + slug: slug, + shorthand: fields.length == 1 ? fields['version'] as String : null, + longForm: fields.length == 1 ? null : fields, + ), + ); + return exitOk; + } + io.writeModsYaml(updated); + final result = await runResolveAndSync(io: io, offline: offline); + if (result.exitCode == exitOk) { + SolveReporter(console).printSummary( + changeCount: result.changeCount, + outdated: result.outdated, + ); + } + return result.exitCode; + } + // Same permissive pattern the YAML parser uses for accepts-mc. Future _validateNoIncompatibility({ required ManifestIo io, @@ -340,7 +641,8 @@ class AddCommand extends GitrinthCommand with OfflineFlag { final pidToSlug = { if (lock != null) for (final entry in lock.allEntries) - if (entry.value.projectId != null) entry.value.projectId!: entry.key, + if (entry.value.modrinth?.projectId != null) + entry.value.modrinth!.projectId: entry.key, }; final declaredSlugs = { for (final s in Section.values) @@ -363,13 +665,31 @@ class AddCommand extends GitrinthCommand with OfflineFlag { } if (lock == null) return; + final defaultBaseUrl = read(modrinthApiFactoryProvider).defaultBaseUrl; for (final entry in lock.allEntries) { final locked = entry.value; - if (locked.sourceKind != LockedSourceKind.modrinth) continue; - final pid = locked.projectId; - final vid = locked.versionId; - if (pid == null || vid == null) continue; - final cachedDeps = _readCachedDeps(cache, pid, vid); + final mr = locked.modrinth; + if (mr == null) continue; + final pid = mr.projectId; + final vid = mr.versionId; + if (pid.isEmpty || vid.isEmpty) continue; + // Re-derive the host the lock entry was fetched against by + // looking up the same slug in the live manifest. Locked entries + // don't carry host metadata yet — that lands with Part 4. + String host = existingManifest.modrinthHost ?? defaultBaseUrl; + for (final section in Section.values) { + final manifestEntry = existingManifest.sectionEntries( + section, + )[entry.key]; + if (manifestEntry != null) { + host = existingManifest.effectiveModrinthHost( + manifestEntry, + defaultBaseUrl, + ); + break; + } + } + final cachedDeps = _readCachedDeps(cache, host, pid, vid); if (cachedDeps == null) continue; for (final d in cachedDeps) { if (d['dependency_type'] != 'incompatible') continue; @@ -387,10 +707,12 @@ class AddCommand extends GitrinthCommand with OfflineFlag { List>? _readCachedDeps( GitrinthCache cache, + String host, String projectId, String versionId, ) { final path = cache.modrinthVersionMetadataPath( + host: host, projectId: projectId, versionId: versionId, ); diff --git a/lib/src/commands/build_assembler.dart b/lib/src/commands/build_assembler.dart index 8103b16..6113c03 100644 --- a/lib/src/commands/build_assembler.dart +++ b/lib/src/commands/build_assembler.dart @@ -3,7 +3,7 @@ import 'package:path/path.dart' as p; import '../cli/exceptions.dart'; import '../model/manifest/mods_lock.dart'; import '../model/manifest/mods_yaml.dart'; -import '../service/cache.dart'; +export '../service/artifact_locator.dart' show resolveSourcePath; enum BuildEnv { client, server } @@ -71,56 +71,8 @@ String mrpackSubdirFor(Section section) => switch (section) { Section.plugins => 'plugins', }; -String resolveSourcePath( - GitrinthCache cache, - LockedEntry entry, { - required String projectDir, -}) { - switch (entry.sourceKind) { - case LockedSourceKind.modrinth: - final file = entry.file; - final projectId = entry.projectId; - final versionId = entry.versionId; - if (file == null || projectId == null || versionId == null) { - throw ValidationError( - 'lockfile entry "${entry.slug}" is missing modrinth source fields.', - ); - } - return cache.modrinthPath( - projectId: projectId, - versionId: versionId, - filename: file.name, - ); - case LockedSourceKind.url: - final file = entry.file; - if (file == null) { - throw ValidationError( - 'lockfile entry "${entry.slug}" is missing url source fields.', - ); - } - // When sha512 is known, the artifact lives at its sha-addressed - // path; otherwise the downloader stashed it under `_unverified` - // keyed by slug (same fallback resolve_and_sync uses). Mirroring - // that here avoids forcing callers to re-run `get` just to - // populate a hash they may never need. - final sha512 = file.sha512; - if (sha512 != null) { - return cache.urlPath(sha512: sha512, filename: file.name); - } - return cache.unverifiedUrlPath(entry.slug, file.name); - case LockedSourceKind.path: - final raw = entry.path; - if (raw == null) { - throw ValidationError( - 'lockfile entry "${entry.slug}" is missing path source fields.', - ); - } - return p.isAbsolute(raw) ? raw : p.normalize(p.join(projectDir, raw)); - } -} - String destFilenameFor(LockedEntry entry) { - final file = entry.file; + final file = entry.primaryFile; if (file != null) return file.name; final path = entry.path; if (path != null) return p.basename(path); diff --git a/lib/src/commands/build_orchestrator.dart b/lib/src/commands/build_orchestrator.dart index b1cf94d..22741e1 100644 --- a/lib/src/commands/build_orchestrator.dart +++ b/lib/src/commands/build_orchestrator.dart @@ -4,6 +4,7 @@ import 'package:path/path.dart' as p; import 'package:riverpod/riverpod.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; import '../model/manifest/mods_lock.dart'; @@ -14,9 +15,9 @@ import '../service/console.dart'; import '../service/loader_binary_fetcher.dart'; import '../service/manifest_io.dart'; import '../service/plugin_server_source.dart'; -import '../service/resolve_and_sync.dart'; +import '../service/resolve/resolve_and_sync.dart'; +import '../service/resolve/solve_report.dart'; import '../service/server_installer.dart'; -import '../service/solve_report.dart'; import '../service/vanilla_server_source.dart'; import '../version.dart'; import 'build_assembler.dart'; @@ -80,13 +81,21 @@ Future runBuild({ ); } } else { - final api = container.read(modrinthApiProvider); + final apiFactory = container.read(modrinthApiFactoryProvider); + final cfFactory = container.read(curseForgeApiFactoryProvider); final downloader = container.read(downloaderProvider); final modLoaderResolver = container.read(modLoaderVersionResolverProvider); + final registry = await prepareLoaderRegistry( + container, + manifestIo.readModsYaml(), + offline: options.offline, + ); final result = await resolveAndSync( + loaderRegistry: registry, io: manifestIo, console: console, - api: api, + apiFactory: apiFactory, + cfFactory: cfFactory, cache: cache, downloader: downloader, modLoaderResolver: modLoaderResolver, @@ -105,6 +114,16 @@ Future runBuild({ } } + final registry = container.read(loaderRegistryProvider); + if (envs.contains(BuildEnv.server)) { + final id = lock.loader.plugins ?? lock.loader.mods; + if (!(registry[id]?.canInstallServer ?? false)) { + throw ValidationError( + 'Loader ${id.name} has no installed server adapter. Resolve and pack remain supported; build requires a runtime adapter.', + ); + } + } + final outputDir = Directory( p.normalize( p.absolute( @@ -117,9 +136,13 @@ Future runBuild({ } final projectDir = manifestIo.directory.path; + final defaultBaseUrl = container + .read(modrinthApiFactoryProvider) + .defaultBaseUrl; for (final env in envs) { final result = _assembleEnv( env: env, + defaultModrinthBaseUrl: defaultBaseUrl, lock: lock, cache: cache, outputDir: outputDir, @@ -157,6 +180,7 @@ Future runBuild({ installer: container.read(serverInstallerProvider), source: PluginServerSource.forLoader( pluginLoader, + registry: registry, paperApi: container.read(paperApiClientProvider), spongeApi: container.read(spongeApiClientProvider), buildTools: container.read(buildToolsRunnerProvider), @@ -175,6 +199,7 @@ Future runBuild({ cache: cache, serverDir: serverDir, fetcher: container.read(loaderBinaryFetcherProvider), + registry: registry, installer: container.read(serverInstallerProvider), skipDownload: options.skipDownload, offline: options.offline, @@ -286,6 +311,7 @@ Future _installModdedServerBinary({ required GitrinthCache cache, required Directory serverDir, required LoaderBinaryFetcher fetcher, + required LoaderRegistry registry, required ServerInstaller installer, required bool skipDownload, required bool offline, @@ -306,6 +332,7 @@ Future _installModdedServerBinary({ if (skipDownload) { final cachedPath = _expectedCachedInstallerPath( + registry: registry, cache: cache, loader: loader, mcVersion: mcVersion, @@ -343,43 +370,22 @@ Future _installModdedServerBinary({ } String _expectedCachedInstallerPath({ + required LoaderRegistry registry, required GitrinthCache cache, required ModLoader loader, required String mcVersion, required String modLoaderVersion, -}) { - switch (loader) { - case ModLoader.vanilla: - throw StateError( - '_expectedCachedInstallerPath called for vanilla; gate on ' - 'LoaderConfig.hasModRuntime.', - ); - case ModLoader.forge: - return cache.loaderArtifactPath( - loader: loader, - mcVersion: mcVersion, - modLoaderVersion: modLoaderVersion, - filename: 'forge-$mcVersion-$modLoaderVersion-installer.jar', - ); - case ModLoader.neoforge: - final filename = mcVersion == '1.20.1' - ? 'forge-$mcVersion-$modLoaderVersion-installer.jar' - : 'neoforge-$modLoaderVersion-installer.jar'; - return cache.loaderArtifactPath( - loader: loader, - mcVersion: mcVersion, - modLoaderVersion: modLoaderVersion, - filename: filename, - ); - case ModLoader.fabric: - return cache.loaderArtifactPath( - loader: loader, - mcVersion: mcVersion, - modLoaderVersion: modLoaderVersion, - filename: 'fabric-server-launch.jar', - ); - } -} +}) => cache.loaderArtifactPath( + loader: loader, + mcVersion: mcVersion, + modLoaderVersion: modLoaderVersion, + filename: LoaderBinaryFetcher.serverArtifactFilename( + loader, + mcVersion, + modLoaderVersion, + registry: registry, + ), +); /// Result of [_assembleEnv]: count of newly-written/copied files plus /// the count of obsolete files pruned from the prior ledger. @@ -391,6 +397,7 @@ class _AssembleResult { _AssembleResult _assembleEnv({ required BuildEnv env, + required String defaultModrinthBaseUrl, required ModsLock lock, required GitrinthCache cache, required Directory outputDir, @@ -469,6 +476,7 @@ _AssembleResult _assembleEnv({ cache, entry, projectDir: projectDir, + modrinthHost: defaultModrinthBaseUrl, ); final sourceFile = File(sourcePath); if (!sourceFile.existsSync()) { @@ -517,7 +525,7 @@ _AssembleResult _assembleEnv({ ledgerFiles[relKey] = LedgerModSource( section: section.name, slug: entry.slug, - sha512: entry.file?.sha512, + sha512: entry.primaryFile?.sha512, ); count++; } diff --git a/lib/src/commands/create_command.dart b/lib/src/commands/create_command.dart index ef076eb..80f2827 100644 --- a/lib/src/commands/create_command.dart +++ b/lib/src/commands/create_command.dart @@ -38,7 +38,7 @@ class CreateCommand extends GitrinthCommand with OfflineFlag { 'mod-loader', valueHelp: 'loader[:tag]', help: - 'Pre-fill loader.mods. One of ${modLoaderRefNames.join(', ')}, ' + 'Pre-fill loader.mods. Built-ins: ${modLoaderRefNames.join(', ')}, ' 'optionally with a docker-style version tag ' '(e.g. `neoforge:21.1.50`, `fabric:latest`). Defaults to ' '$defaultModLoader when no plugin loader is specified.', @@ -47,7 +47,7 @@ class CreateCommand extends GitrinthCommand with OfflineFlag { 'plugin-loader', valueHelp: 'loader[:tag]', help: - 'Pre-fill loader.plugins. One of ' + 'Pre-fill loader.plugins. Built-ins: ' '${pluginLoaderRefNames.join(', ')}, optionally with a ' 'docker-style version tag (e.g. `paper:187`, `sponge:stable`).', ) @@ -110,7 +110,7 @@ class CreateCommand extends GitrinthCommand with OfflineFlag { 'name': packName, 'version': '0.1.0', 'description': 'A new Modrinth modpack.', - 'mc-version': mcVersion, + 'mc_version': mcVersion, 'gitrinth-version': packageVersion, 'gitrinth-next-major': nextMajor(packageVersion), }; @@ -172,7 +172,9 @@ class CreateCommand extends GitrinthCommand with OfflineFlag { /// a project with this slug already exists. Network failures are /// non-fatal — they degrade to a warning so offline scaffolding still works. Future _warnIfSlugTaken(String slug) async { - final api = read(modrinthApiProvider); + // No manifest exists yet at scaffold time — talk to the global + // default Modrinth host. + final api = read(modrinthApiFactoryProvider).forHost(null); try { final resp = await api.checkProjectValidity(slug); final status = resp.response.statusCode; diff --git a/lib/src/commands/curseforge_pack_assembler.dart b/lib/src/commands/curseforge_pack_assembler.dart new file mode 100644 index 0000000..797e07b --- /dev/null +++ b/lib/src/commands/curseforge_pack_assembler.dart @@ -0,0 +1,128 @@ +import 'dart:convert'; +import 'dart:io'; + +import '../cli/exceptions.dart'; +import '../model/manifest/mods_lock.dart'; +import '../model/manifest/mods_yaml.dart'; +import '../service/cache.dart'; +import '../service/modrinth/modrinth_url.dart'; +import '../service/pack_archive_writer.dart'; +import 'pack_assembler.dart'; + +/// CurseForge's manifest/overrides format has one override root. Separate +/// client/server archives preserve side filtering without inventing CF fields. +Map buildCurseForgeManifest({ + required ModsYaml yaml, + LoaderRegistry registry = LoaderRegistry.builtins, + required ModsLock lock, + required PackTarget target, + required bool publishable, +}) { + final files = >[]; + final offenders = []; + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).values) { + final include = switch (target) { + PackTarget.client => entry.client.includes, + PackTarget.server => entry.server.includes, + PackTarget.combined => entry.client.includes || entry.server.includes, + }; + if (!include) continue; + final cf = entry.curseforge; + final eligible = + section != Section.plugins || + (lock.loader.plugins != null && + (registry[lock.loader.plugins]?.curseforgePlugins ?? false)); + if (cf == null || + !eligible || + yaml.sectionEntries(section)[entry.slug]?.allowHashMismatch == true) { + offenders.add(entry.slug); + } + if (cf != null && eligible) { + files.add({ + 'projectID': cf.projectId, + 'fileID': cf.fileId, + 'required': true, + }); + } + } + } + if (publishable && offenders.isNotEmpty) { + throw ValidationError( + '--publishable refused for CurseForge: ' + '${offenders.join(', ')} require loose overrides or allow_hash_mismatch.', + ); + } + final version = lock.loader.modLoaderVersion; + if (lock.loader.hasModRuntime && + (version == null || version == 'stable' || version == 'latest')) { + throw const ValidationError( + 'mods.lock has no concrete loader version. Re-run `gitrinth get`.', + ); + } + final archiveToken = registry[lock.loader.mods]?.curseforgeArchiveToken; + if (lock.loader.hasModRuntime && archiveToken == null) { + throw ValidationError( + 'Loader ${lock.loader.mods.name} has no CurseForge archive metadata capability.', + ); + } + return { + 'minecraft': { + 'version': lock.mcVersion, + 'modLoaders': [ + if (lock.loader.hasModRuntime) + {'id': '$archiveToken-$version', 'primary': true}, + ], + }, + 'manifestType': 'minecraftModpack', + 'manifestVersion': 1, + 'name': yaml.name, + 'author': '', + 'version': yaml.version, + 'files': files, + 'overrides': 'overrides', + }; +} + +void writePackArchive( + File output, { + required String manifestName, + required Map manifest, + required List overrides, +}) { + final json = utf8.encode( + const JsonEncoder.withIndent(' ').convert(manifest), + ); + for (final entry in overrides) { + final source = File(entry.sourcePath); + if (!source.existsSync()) { + throw UserError( + 'override source for "${entry.slug}" missing on disk: ${entry.sourcePath}. Re-run `gitrinth get`.', + ); + } + } + output.parent.createSync(recursive: true); + writeArchive( + output, + manifestName: manifestName, + manifest: json, + entries: overrides.map( + (entry) => (path: entry.zipPath, source: File(entry.sourcePath)), + ), + ); +} + +OverridesPlan curseForgeOverrides({ + required ModsLock lock, + required GitrinthCache cache, + required String projectDir, + required PackTarget target, + String modrinthHost = defaultModrinthBaseUrl, +}) => collectOverrides( + lock: lock, + cache: cache, + projectDir: projectDir, + target: target, + platform: SourceKind.curseforge, + modrinthHost: modrinthHost, +); diff --git a/lib/src/commands/deps_command.dart b/lib/src/commands/deps_command.dart index b481ccb..6f1b51d 100644 --- a/lib/src/commands/deps_command.dart +++ b/lib/src/commands/deps_command.dart @@ -6,7 +6,7 @@ import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; import '../model/manifest/mods_lock.dart'; import '../model/manifest/mods_yaml.dart'; -import '../service/lock_graph.dart'; +import '../service/resolve/lock_graph.dart'; import '../service/manifest_io.dart'; import '../util/ascii_tree.dart' as ascii_tree; @@ -65,49 +65,34 @@ class DepsCommand extends GitrinthCommand { _checkLockFreshness(manifest, lock); final cache = read(cacheProvider); - - // Index entries by slug for quick lookup; capture sections so the - // renderer can prefix `mods/` etc. on direct entries (mirrors dart - // pub's `mods/` flavor). - final lockBySlug = {}; - for (final section in Section.values) { - lock.sectionFor(section).forEach((slug, locked) { - lockBySlug[slug] = _LockedWithSection(locked, section); - }); + final defaultBaseUrl = read(modrinthApiFactoryProvider).defaultBaseUrl; + final indexed = LockedDependencyGraph( + lock, + cache, + defaultHost: defaultBaseUrl, + ); + for (final diagnostic in indexed.diagnostics) { + console.warn(diagnostic); } - - // Build child edges per slug from the cached version.json sidecars. - // Cold-cache entries get an empty edge list; we count them so a - // trailing footer can warn the user. - final children = >{}; - var coldCacheCount = 0; - final projectIdToSlug = { - for (final entry in lockBySlug.entries) - if (entry.value.locked.projectId != null) - entry.value.locked.projectId!: entry.key, - }; - for (final entry in lockBySlug.entries) { - final slug = entry.key; - final locked = entry.value.locked; - if (locked.sourceKind != LockedSourceKind.modrinth) { - children[slug] = const []; - continue; - } - final pid = locked.projectId; - final vid = locked.versionId; - if (pid == null || vid == null) { - children[slug] = const []; - continue; - } - final raw = readCachedRequiredChildren(cache, pid, vid); - if (raw == null) { - coldCacheCount++; - children[slug] = const []; - continue; - } - children[slug] = [for (final childPid in raw) ?projectIdToSlug[childPid]] - ..sort(); + final counts = {}; + for (final node in indexed.entries.keys) { + counts.update(node.localKey, (n) => n + 1, ifAbsent: () => 1); } + final labels = { + for (final node in indexed.entries.keys) + node: counts[node.localKey] == 1 ? node.localKey : node.toString(), + }; + final lockBySlug = { + for (final item in indexed.entries.entries) + labels[item.key]!: _LockedWithSection(item.value, item.key.section), + }; + final children = >{ + for (final node in indexed.entries.keys) + labels[node]!: [ + for (final child in indexed.graph.children(node)) labels[child]!, + ]..sort(), + }; + final coldCacheCount = indexed.coldCacheCount; final visibleSlugs = _filterByEnv(lockBySlug, env); @@ -235,9 +220,7 @@ class DepsCommand extends GitrinthCommand { String label(String slug, {bool prefixSection = false}) { final entry = lockBySlug[slug]; if (entry == null) return slug; - final ver = entry.locked.version ?? entry.locked.path ?? ''; - final prefix = prefixSection ? '${entry.section.name}/' : ''; - return ver.isEmpty ? '$prefix$slug' : '$prefix$slug $ver'; + return entry.label(slug, prefixSection: prefixSection); } final seen = {}; @@ -379,9 +362,8 @@ class DepsCommand extends GitrinthCommand { }) { final entry = lockBySlug[slug]; if (entry == null) return; - final ver = entry.locked.version ?? entry.locked.path ?? ''; - final label = sectionPrefix ? '${entry.section.name}/$slug' : slug; - console.message('- $label${ver.isEmpty ? '' : ' $ver'}'); + final label = entry.label(slug, prefixSection: sectionPrefix); + console.message('- $label'); final kids = (children[slug] ?? const []) .where(visibleSlugs.contains) .toList(); @@ -450,13 +432,12 @@ class DepsCommand extends GitrinthCommand { }) { final entry = lockBySlug[slug]; if (entry == null) return; - final ver = entry.locked.version ?? entry.locked.path ?? ''; - final label = sectionPrefix ? '${entry.section.name}/$slug' : slug; + final label = entry.label(slug, prefixSection: sectionPrefix); final kids = (children[slug] ?? const []) .where(visibleSlugs.contains) .toList(); final kidsText = kids.isEmpty ? '[]' : '[${kids.join(' ')}]'; - console.message('- $label${ver.isEmpty ? '' : ' $ver'} $kidsText'); + console.message('- $label $kidsText'); } void _printJson({ @@ -488,8 +469,8 @@ class DepsCommand extends GitrinthCommand { 'slug': slug, 'version': ver.isEmpty ? null : ver, 'kind': entry.locked.dependency.name, + 'entry_kind': entry.locked.kind.name, 'section': entry.section.name, - 'source': entry.locked.sourceKind.name, 'dependencies': kids, }); } @@ -523,4 +504,12 @@ class _LockedWithSection { final LockedEntry locked; final Section section; const _LockedWithSection(this.locked, this.section); + + String label(String slug, {bool prefixSection = false}) { + final version = locked.version ?? locked.path ?? ''; + final name = prefixSection && slug != '${section.name}/${locked.slug}' + ? '${section.name}/$slug' + : slug; + return version.isEmpty ? name : '$name $version'; + } } diff --git a/lib/src/commands/downgrade_command.dart b/lib/src/commands/downgrade_command.dart index bf5f762..9b47bef 100644 --- a/lib/src/commands/downgrade_command.dart +++ b/lib/src/commands/downgrade_command.dart @@ -6,7 +6,7 @@ import '../model/manifest/mods_yaml.dart'; import '../model/resolver/constraint.dart'; import '../model/resolver/version_selection.dart'; import '../service/manifest_io.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/solve_report.dart'; class DowngradeCommand extends GitrinthCommand with OfflineFlag { @override diff --git a/lib/src/commands/entry_version_editor.dart b/lib/src/commands/entry_version_editor.dart new file mode 100644 index 0000000..8e1f0b5 --- /dev/null +++ b/lib/src/commands/entry_version_editor.dart @@ -0,0 +1,44 @@ +import 'package:yaml/yaml.dart'; +import 'package:yaml_edit/yaml_edit.dart'; + +import '../cli/exceptions.dart'; +import '../model/manifest/mods_yaml.dart'; +import '../util/yaml_root.dart'; +import 'add_command_editor.dart'; + +String rewriteEntryVersion( + String yamlText, { + required Section section, + required String slug, + required String version, + required String missingVersionAction, +}) { + final editor = YamlEditor(yamlText); + final sectionKey = sectionKeyFor(section); + final root = parseYamlRoot(editor, filename: 'mods.yaml'); + final sectionNode = root.nodes[sectionKey]; + if (sectionNode == null || sectionNode.value == null) { + throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); + } + if (sectionNode is! YamlMap) { + throw UserError( + "mods.yaml section '$sectionKey' is not a mapping; cannot edit.", + ); + } + final entryNode = sectionNode.nodes[slug]; + if (entryNode == null) { + throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); + } + if (entryNode is YamlMap) { + if (!entryNode.containsKey('version')) { + throw UserError( + "'$slug' has no `version:` field — likely a url/path source; " + 'cannot $missingVersionAction.', + ); + } + editor.update([sectionKey, slug, 'version'], version); + } else { + editor.update([sectionKey, slug], version); + } + return editor.toString(); +} diff --git a/lib/src/commands/get_command.dart b/lib/src/commands/get_command.dart index 0d7b3cd..388fd09 100644 --- a/lib/src/commands/get_command.dart +++ b/lib/src/commands/get_command.dart @@ -2,7 +2,7 @@ import '../cli/base_command.dart'; import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; import '../service/manifest_io.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/solve_report.dart'; class GetCommand extends GitrinthCommand with OfflineFlag { @override @@ -16,6 +16,12 @@ class GetCommand extends GitrinthCommand with OfflineFlag { GetCommand() { argParser + ..addFlag( + 'search', + defaultsTo: true, + help: + 'Search for cross-platform peers when a slug is missing or hashes differ.', + ) ..addFlag( 'dry-run', negatable: false, diff --git a/lib/src/commands/launch_command.dart b/lib/src/commands/launch_command.dart index 0403a44..0ab5348 100644 --- a/lib/src/commands/launch_command.dart +++ b/lib/src/commands/launch_command.dart @@ -6,6 +6,7 @@ import 'package:riverpod/riverpod.dart'; import '../app/env.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; @@ -296,6 +297,13 @@ Future runLaunchServer({ ); } + final registry = container.read(loaderRegistryProvider); + if (registry[lock.loader.mods]?.installer == null) { + throw ValidationError( + 'Loader ${lock.loader.mods.name} has no installed launch adapter.', + ); + } + final outputDir = Directory( p.normalize( p.absolute( @@ -325,6 +333,7 @@ Future runLaunchServer({ } final (executable, args, useShell) = _serverLaunchCommand( + registry: registry, loader: lock.loader.mods, serverDir: serverDir, memoryMax: options.memoryMax, @@ -381,6 +390,7 @@ Map _spawnEnvironment({ } (String executable, List args, bool runInShell) _serverLaunchCommand({ + LoaderRegistry registry = LoaderRegistry.builtins, required ModLoader loader, required Directory serverDir, required String memoryMax, @@ -390,7 +400,13 @@ Map _spawnEnvironment({ required int javaMajor, required bool headless, }) { - final jar = loader.serverLaunchJar; + final descriptor = registry[loader]; + if (descriptor?.installer == null) { + throw ValidationError( + 'Loader ${loader.name} has no installed launch adapter.', + ); + } + final jar = descriptor?.serverLaunchJar; if (jar != null) { return ( javaPath, @@ -789,6 +805,15 @@ Future runLaunchClient({ ); } + if (container + .read(loaderRegistryProvider)[lock.loader.mods] + ?.canInstallClient != + true) { + throw ValidationError( + 'Loader ${lock.loader.mods.name} has no installed client launch adapter.', + ); + } + final outputDir = Directory( p.normalize( p.absolute( diff --git a/lib/src/commands/migrate_command.dart b/lib/src/commands/migrate_command.dart index edfcd95..1c42ee0 100644 --- a/lib/src/commands/migrate_command.dart +++ b/lib/src/commands/migrate_command.dart @@ -3,6 +3,8 @@ import 'dart:io'; import 'package:dio/dio.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; +import 'version_picker.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; @@ -11,10 +13,10 @@ import '../model/manifest/loader_ref.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/modrinth/version.dart' as modrinth; import '../model/resolver/constraint.dart'; -import '../service/conflict_disable.dart'; import '../service/manifest_io.dart'; -import '../service/modrinth_api.dart'; -import '../service/resolve_and_sync.dart'; +import '../service/modrinth/modrinth_api.dart'; +import '../service/resolve/conflict_disable.dart'; +import '../service/resolve/resolve_and_sync.dart'; import 'caret_rewriter.dart'; import 'migrate_editor.dart'; @@ -175,10 +177,18 @@ Future _runMigrate({ newLoader: newLoader, newLoaderTag: newLoaderTag, ); + final registry = await prepareLoaderRegistry( + command.container, + mutated, + offline: offline, + ); final targetMc = mutated.mcVersion; final targetLoader = mutated.loader.mods; - final api = command.read(modrinthApiProvider); + // Migrate's recovery loop walks Modrinth-source entries; each one + // may have its own host. Pull the factory once and use it per + // entry inside the loop below. + final apiFactory = command.read(modrinthApiFactoryProvider); final available = <(Section, String)>{}; final recovered = <(Section, String)>{}; @@ -213,11 +223,23 @@ Future _runMigrate({ // `^x.y.z` on success or re-mark on the next conflict catch. final isMarker = isAnyGitrinthMarker(entry.constraintRaw); final gameVersions = {targetMc, ...entry.acceptsMc}.toList(); + final entryApi = apiFactory.forHost( + manifest.effectiveModrinthHost(entry, apiFactory.defaultBaseUrl), + ); List versions; try { - versions = await api.listVersions( + final filters = filterLoadersForSection( + mutated.loader, + section, + registry: registry, + host: manifest.effectiveModrinthHost( + entry, + apiFactory.defaultBaseUrl, + ), + ); + versions = await entryApi.listVersions( slug, - loadersJson: encodeFilterArray([targetLoader.name]), + loadersJson: filters == null ? null : encodeFilterArray(filters), gameVersionsJson: encodeFilterArray(gameVersions), ); } on DioException catch (e) { @@ -261,7 +283,7 @@ Future _runMigrate({ }; final resolutionManifest = stripSlugs.isEmpty ? mutated - : _stripSlugs(mutated, stripSlugs); + : mutated.withoutEntrySlugs(stripSlugs); final targets = { for (final s in available) s.$2, @@ -277,6 +299,7 @@ Future _runMigrate({ }; final cache = command.read(cacheProvider); + final cfFactory = command.read(curseForgeApiFactoryProvider); final downloader = command.read(downloaderProvider); final modLoaderResolver = command.read(modLoaderVersionResolverProvider); final pluginLoaderResolver = command.read( @@ -295,9 +318,11 @@ Future _runMigrate({ required freshSlugs, required relaxConstraints, }) => resolveAndSync( + loaderRegistry: registry, io: io, console: console, - api: api, + apiFactory: apiFactory, + cfFactory: cfFactory, cache: cache, downloader: downloader, modLoaderResolver: modLoaderResolver, @@ -333,7 +358,7 @@ Future _runMigrate({ if (newMcVersion != null) { yamlText = updateTopLevelScalar( yamlText, - path: const ['mc-version'], + path: const ['mc_version'], newValue: newMcVersion, ); } else if (newLoader == ModLoader.vanilla) { @@ -470,19 +495,6 @@ ModsYaml _applyTarget( ); } -ModsYaml _stripSlugs(ModsYaml manifest, Set slugs) { - Map strip(Map m) => { - for (final e in m.entries) - if (!slugs.contains(e.key)) e.key: e.value, - }; - return manifest.copyWith( - mods: strip(manifest.mods), - resourcePacks: strip(manifest.resourcePacks), - dataPacks: strip(manifest.dataPacks), - shaders: strip(manifest.shaders), - ); -} - String _summaryTarget( String? newMcVersion, ModLoader? newLoader, diff --git a/lib/src/commands/migrate_editor.dart b/lib/src/commands/migrate_editor.dart index c4fbc55..fcb2471 100644 --- a/lib/src/commands/migrate_editor.dart +++ b/lib/src/commands/migrate_editor.dart @@ -1,12 +1,11 @@ import 'package:yaml/yaml.dart'; import 'package:yaml_edit/yaml_edit.dart'; -import '../cli/exceptions.dart'; import '../model/manifest/mods_yaml.dart'; import '../util/yaml_root.dart'; -import 'add_command_editor.dart'; +import 'entry_version_editor.dart'; -/// Updates a top-level scalar at [path] (e.g. `['mc-version']`, +/// Updates a top-level scalar at [path] (e.g. `['mc_version']`, /// `['loader', 'mods']`). String updateTopLevelScalar( String yamlText, { @@ -58,46 +57,10 @@ String setEntryVersion( required Section section, required String slug, required String newVersion, -}) { - final editor = YamlEditor(yamlText); - final sectionKey = sectionKeyFor(section); - - final YamlNode root; - try { - root = editor.parseAt([]); - } on Object { - throw const UserError('mods.yaml is not valid YAML; cannot edit.'); - } - if (root is! YamlMap) { - throw const UserError('mods.yaml top-level must be a mapping.'); - } - - final sectionNode = root.nodes[sectionKey]; - if (sectionNode == null || sectionNode.value == null) { - throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); - } - if (sectionNode is! YamlMap) { - throw UserError( - "mods.yaml section '$sectionKey' is not a mapping; cannot edit.", - ); - } - - final entryNode = sectionNode.nodes[slug]; - if (entryNode == null) { - throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); - } - - if (entryNode is YamlMap) { - if (!entryNode.containsKey('version')) { - throw UserError( - "'$slug' has no `version:` field — likely a url/path source; " - 'cannot rewrite version.', - ); - } - editor.update([sectionKey, slug, 'version'], newVersion); - } else { - editor.update([sectionKey, slug], newVersion); - } - - return editor.toString(); -} +}) => rewriteEntryVersion( + yamlText, + section: section, + slug: slug, + version: newVersion, + missingVersionAction: 'rewrite version', +); diff --git a/lib/src/commands/modrinth_command.dart b/lib/src/commands/modrinth_command.dart index d7c7898..4e34460 100644 --- a/lib/src/commands/modrinth_command.dart +++ b/lib/src/commands/modrinth_command.dart @@ -5,10 +5,12 @@ import '../app/providers.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; -import '../service/modrinth_url.dart'; +import '../service/modrinth/modrinth_url.dart'; import '../service/secret_input.dart'; import '../service/user_config.dart'; +import '../service/curseforge/curseforge_url.dart'; import 'modrinth_publish_command.dart'; +import 'platform_command.dart'; class ModrinthCommand extends GitrinthCommand { @override @@ -22,6 +24,8 @@ class ModrinthCommand extends GitrinthCommand { String get invocation => 'gitrinth modrinth '; ModrinthCommand() { + addSubcommand(PlatformToggleCommand('modrinth', true)); + addSubcommand(PlatformToggleCommand('modrinth', false)); addSubcommand(ModrinthLoginCommand()); addSubcommand(ModrinthLogoutCommand()); addSubcommand(ModrinthTokenCommand()); @@ -185,7 +189,7 @@ class ModrinthTokenAddCommand extends GitrinthCommand { } final String key; try { - key = normalizeServerKey(rest.single); + key = normalizeTokenServerKey(rest.single); } on FormatException catch (e) { throw UserError(e.message); } @@ -195,21 +199,37 @@ class ModrinthTokenAddCommand extends GitrinthCommand { if (flag != null && flag.isNotEmpty) { token = flag; } else { - final piped = readSecret(prompt: 'Modrinth token for $key: ').trim(); + final piped = readSecret( + prompt: + '${key == curseForgeUploadTokenKey ? 'CurseForge publish' : 'Modrinth'} token for $key: ', + ).trim(); if (piped.isEmpty) throw const UserError('No token provided.'); token = piped; } - final username = await _validateTokenAgainstUserEndpoint( - read(dioProvider), - key, - token, - ); + final username = key == curseForgeUploadTokenKey + ? null + : await _validateTokenAgainstUserEndpoint( + read(dioProvider), + key, + token, + ); final store = read(userConfigStoreProvider); store.write(store.read().withToken(key, token)); - console.message('Stored token for $key (user: $username).'); + console.message( + username == null + ? 'Stored CurseForge publish token for $key.' + : 'Stored token for $key (user: $username).', + ); + if (key == curseForgeUploadTokenKey && + (read(environmentProvider)['GITRINTH_CURSEFORGE_UPLOAD_TOKEN'] ?? '') + .isNotEmpty) { + console.warn( + 'GITRINTH_CURSEFORGE_UPLOAD_TOKEN overrides the stored publish token.', + ); + } return exitOk; } } @@ -233,6 +253,8 @@ class ModrinthTokenListCommand extends GitrinthCommand { final env = read(environmentProvider); final defaultKey = normalizeServerKey(resolveModrinthBaseUrl(env)); final envOverride = (env['GITRINTH_TOKEN'] ?? '').isNotEmpty; + final cfOverride = + (env['GITRINTH_CURSEFORGE_UPLOAD_TOKEN'] ?? '').isNotEmpty; final cfg = read(userConfigStoreProvider).read(); if (cfg.tokens.isEmpty) { @@ -241,6 +263,11 @@ class ModrinthTokenListCommand extends GitrinthCommand { ? '(no stored tokens; GITRINTH_TOKEN override active for $defaultKey)' : '(no stored tokens)', ); + if (cfOverride) { + console.message( + '$curseForgeUploadTokenKey (GITRINTH_CURSEFORGE_UPLOAD_TOKEN override)', + ); + } return exitOk; } final entries = cfg.tokens.entries.toList() @@ -249,12 +276,20 @@ class ModrinthTokenListCommand extends GitrinthCommand { final masked = _maskToken(entry.value); final suffix = (envOverride && entry.key == defaultKey) ? ' (GITRINTH_TOKEN override)' + : entry.key == curseForgeUploadTokenKey && + (env['GITRINTH_CURSEFORGE_UPLOAD_TOKEN'] ?? '').isNotEmpty + ? ' (GITRINTH_CURSEFORGE_UPLOAD_TOKEN override)' : ''; console.message('${entry.key} $masked$suffix'); } if (envOverride && !cfg.tokens.containsKey(defaultKey)) { console.message('$defaultKey (GITRINTH_TOKEN override)'); } + if (cfOverride && !cfg.tokens.containsKey(curseForgeUploadTokenKey)) { + console.message( + '$curseForgeUploadTokenKey (GITRINTH_CURSEFORGE_UPLOAD_TOKEN override)', + ); + } return exitOk; } } @@ -280,7 +315,7 @@ class ModrinthTokenRemoveCommand extends GitrinthCommand { } final String key; try { - key = normalizeServerKey(rest.single); + key = normalizeTokenServerKey(rest.single); } on FormatException catch (e) { throw UserError(e.message); } @@ -327,3 +362,13 @@ String _maskToken(String token) { if (token.length <= 8) return '*' * token.length; return '${token.substring(0, 4)}…${token.substring(token.length - 4)}'; } + +String normalizeTokenServerKey(String input) { + final value = input.trim(); + if (value == 'curseforge.com' || + value == 'https://curseforge.com' || + value == 'https://curseforge.com/') { + return curseForgeUploadTokenKey; + } + return normalizeServerKey(value); +} diff --git a/lib/src/commands/modrinth_publish_command.dart b/lib/src/commands/modrinth_publish_command.dart index 5a8574f..8c5803b 100644 --- a/lib/src/commands/modrinth_publish_command.dart +++ b/lib/src/commands/modrinth_publish_command.dart @@ -1,259 +1,13 @@ -import 'dart:convert'; -import 'dart:io'; - -import 'package:crypto/crypto.dart'; -import 'package:dio/dio.dart'; -import 'package:path/path.dart' as p; - -import '../app/env.dart'; -import '../app/providers.dart'; -import '../cli/base_command.dart'; -import '../cli/exceptions.dart'; -import '../cli/exit_codes.dart'; import '../model/manifest/mods_yaml.dart'; -import '../service/changelog_reader.dart'; -import '../service/manifest_io.dart'; -import '../service/modrinth_auth_interceptor.dart'; -import '../service/modrinth_url.dart'; -import '../service/user_config.dart'; - -class ModrinthPublishCommand extends GitrinthCommand { - @override - String get name => 'publish'; +import 'publish_command.dart'; - @override - String get description => - 'Upload a built .mrpack as a new version on the Modrinth-compatible server.'; +class ModrinthPublishCommand extends PublicationCommand { + ModrinthPublishCommand() : super(forcedPlatform: SourceKind.modrinth); @override String get invocation => 'gitrinth modrinth publish [arguments]'; - ModrinthPublishCommand() { - argParser - ..addFlag( - 'dry-run', - negatable: false, - help: - 'Assemble the payload, hash the artifact, and print the JSON ' - 'without uploading.', - ) - ..addFlag( - 'force', - abbr: 'f', - negatable: false, - help: 'Skip the interactive confirmation prompt.', - ) - ..addFlag( - 'draft', - negatable: false, - help: - 'Upload as a draft (Modrinth `status: draft`) instead of ' - 'publicly listed.', - ) - ..addOption( - 'version-type', - valueHelp: 'release|beta|alpha', - allowed: ['release', 'beta', 'alpha'], - help: - 'Modrinth version channel. Defaults to `beta` when ' - '`mods.yaml.version` carries a pre-release suffix, else `release`.', - ) - ..addOption( - 'changelog', - valueHelp: 'path', - help: - 'Path to a Markdown changelog. Defaults to the matching ' - 'section of CHANGELOG.md when present.', - ) - ..addOption( - 'pack', - valueHelp: 'path', - help: - 'Path to the .mrpack to upload. Defaults to ' - 'build/-.mrpack.', - ); - } - @override - Future run() async { - if (argResults!.rest.isNotEmpty) { - throw UsageError('Unexpected arguments: ${argResults!.rest.join(' ')}'); - } - final io = ManifestIo(); - final yaml = io.readModsYaml(); - - if (yaml.publishTo?.toLowerCase() == 'none') { - throw const UserError( - "publish_to: 'none' in mods.yaml — publishing is disabled for this pack.", - ); - } - - final env = read(environmentProvider); - final publishTo = yaml.publishTo; - final baseUrl = publishTo == null || publishTo.isEmpty - ? resolveModrinthBaseUrl(env) - : publishTo; - - final hasEnvToken = (env['GITRINTH_TOKEN'] ?? '').isNotEmpty; - final stored = _readTokens(); - if (!hasEnvToken && _bestTokenFor(stored, baseUrl) == null) { - throw AuthenticationError( - 'No token configured for $baseUrl. Run `gitrinth modrinth login` ' - '(default host) or `gitrinth modrinth token add $baseUrl`.', - ); - } - - final pack = _resolvePackPath(yaml: yaml); - if (!pack.existsSync()) { - throw UserError( - 'Pack artifact not found at ${pack.path}. Run `gitrinth pack` first ' - 'or pass --pack .', - ); - } - - final bytes = pack.readAsBytesSync(); - final sha1Hex = sha1.convert(bytes).toString(); - final sha512Hex = sha512.convert(bytes).toString(); - - final versionType = - (argResults!['version-type'] as String?) ?? - _defaultVersionType(yaml.version); - final draft = argResults!['draft'] as bool; - final changelog = _resolveChangelog(yaml: yaml, projectDir: io.directory); - - final payload = { - 'name': '${yaml.name} ${yaml.version}', - 'version_number': yaml.version, - 'changelog': changelog ?? '', - 'dependencies': >[], - 'game_versions': [yaml.mcVersion], - 'version_type': versionType, - 'loaders': [yaml.loader.mods.name], - 'featured': false, - 'status': draft ? 'draft' : 'listed', - 'project_id': yaml.slug, - 'file_parts': ['file'], - 'primary_file': 'file', - }; - - final dryRun = argResults!['dry-run'] as bool; - if (dryRun) { - console.message('Dry run: would publish to $baseUrl'); - console.message('Pack: ${pack.path} (sha1=$sha1Hex sha512=$sha512Hex)'); - console.message(const JsonEncoder.withIndent(' ').convert(payload)); - return exitOk; - } - - final force = argResults!['force'] as bool; - if (!force) { - if (!stdin.hasTerminal) { - throw const UsageError( - 'publish: refusing to upload without --force when stdin is not a ' - 'terminal. Re-run with -f / --force.', - ); - } - stdout.write('Publish ${yaml.slug} v${yaml.version} to $baseUrl? [y/N] '); - final answer = (stdin.readLineSync() ?? '').trim().toLowerCase(); - if (answer != 'y' && answer != 'yes') { - console.message('Aborted.'); - return exitOk; - } - } - - final uploadUrl = _joinPath(baseUrl, '/project/${yaml.slug}/version'); - final form = FormData.fromMap({ - 'data': MultipartFile.fromBytes( - utf8.encode(jsonEncode(payload)), - filename: 'data.json', - contentType: DioMediaType('application', 'json'), - ), - 'file': MultipartFile.fromBytes( - bytes, - filename: p.basename(pack.path), - contentType: DioMediaType('application', 'x-modrinth-modpack+zip'), - ), - }); - - final dio = read(dioProvider); - final response = await dio.post( - uploadUrl, - data: form, - options: Options( - contentType: 'multipart/form-data', - extra: const {kModrinthAuthRequired: true}, - ), - ); - final body = response.data; - final versionId = body is Map ? body['id'] : null; - console.message( - 'Published ${yaml.slug} v${yaml.version} to $baseUrl' - '${versionId == null ? '' : ' (version id: $versionId)'}.', - ); - return exitOk; - } - - Map _readTokens() { - try { - return read(userConfigStoreProvider).read().tokens; - } on UserError { - return const {}; - } - } - - String? _bestTokenFor(Map tokens, String baseUrl) { - final key = normalizeServerKey(baseUrl); - String? best; - for (final entry in tokens.entries) { - if (key == entry.key || key.startsWith('${entry.key}/')) { - if (best == null || entry.key.length > best.length) { - best = entry.key; - } - } - } - return best == null ? null : tokens[best]; - } - - File _resolvePackPath({required ModsYaml yaml}) { - final raw = - (argResults!['pack'] as String?) ?? - p.join('build', '${yaml.slug}-${yaml.version}.mrpack'); - return File(p.normalize(p.absolute(raw))); - } - - /// Reads `--changelog ` if provided, else extracts the matching - /// section from `CHANGELOG.md` in the project directory. Returns null - /// when neither source yields content. - String? _resolveChangelog({ - required ModsYaml yaml, - required Directory projectDir, - }) { - final flag = argResults!['changelog'] as String?; - if (flag != null && flag.isNotEmpty) { - final file = File(flag); - if (!file.existsSync()) { - throw UserError('Changelog not found: $flag'); - } - return file.readAsStringSync().trim(); - } - final defaultPath = File(p.join(projectDir.path, 'CHANGELOG.md')); - return readChangelogSection( - changelogFile: defaultPath, - version: yaml.version, - ); - } - - String _defaultVersionType(String version) { - final hyphen = version.indexOf('-'); - if (hyphen < 0) return 'release'; - final suffix = version.substring(hyphen + 1).toLowerCase(); - if (suffix.startsWith('alpha')) return 'alpha'; - return 'beta'; - } - - String _joinPath(String baseUrl, String path) { - final trimmed = baseUrl.endsWith('/') - ? baseUrl.substring(0, baseUrl.length - 1) - : baseUrl; - return path.startsWith('/') ? '$trimmed$path' : '$trimmed/$path'; - } + String get description => + 'Upload a built .mrpack as a new version on the Modrinth-compatible server.'; } diff --git a/lib/src/commands/outdated_command.dart b/lib/src/commands/outdated_command.dart index 8d31476..292e68e 100644 --- a/lib/src/commands/outdated_command.dart +++ b/lib/src/commands/outdated_command.dart @@ -4,6 +4,7 @@ import 'package:dio/dio.dart'; import 'package:pub_semver/pub_semver.dart' as semver; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/base_command.dart'; import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; @@ -15,7 +16,7 @@ import '../model/resolver/version_selection.dart'; import '../service/cache.dart'; import '../service/console.dart'; import '../service/manifest_io.dart'; -import '../service/modrinth_api.dart'; +import '../service/modrinth/modrinth_api.dart'; import 'version_picker.dart'; class OutdatedCommand extends GitrinthCommand with OfflineFlag { @@ -66,11 +67,13 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { return exitUserError; } final manifest = io.readModsYaml(); + await prepareLoaderRegistry(container, manifest, offline: offline); final overrides = io.readProjectOverrides(); final overriddenSlugs = overrides.entries.keys.toSet(); - final api = read(modrinthApiProvider); + final apiFactory = read(modrinthApiFactoryProvider); final cache = read(cacheProvider); + final defaultBaseUrl = apiFactory.defaultBaseUrl; final rows = <_Row>[]; for (final section in Section.values) { @@ -88,6 +91,9 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { } final manifestEntry = manifestSection[slug]; final isOverridden = overriddenSlugs.contains(slug); + final entryHost = manifestEntry == null + ? (manifest.modrinthHost ?? defaultBaseUrl) + : manifest.effectiveModrinthHost(manifestEntry, defaultBaseUrl); final row = await _computeRow( slug: slug, section: section, @@ -95,7 +101,8 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { manifestEntry: manifestEntry, mcVersion: lock.mcVersion, loaderConfig: lock.loader, - api: api, + api: apiFactory.forHost(entryHost), + host: entryHost, cache: cache, offline: offline, isOverridden: isOverridden, @@ -120,6 +127,7 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { required String mcVersion, required LoaderConfig loaderConfig, required ModrinthApi api, + required String host, required GitrinthCache cache, required bool offline, required bool isOverridden, @@ -128,12 +136,13 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { final marker = manifestEntry == null ? null : _markerOf(manifestEntry.constraintRaw); - if (locked.sourceKind != LockedSourceKind.modrinth) { + if (locked.modrinth == null) { return _Row( slug: slug, section: section, kind: locked.dependency, - sourceKind: locked.sourceKind, + hasModrinthSource: locked.modrinth != null, + sourceKind: locked.kind, current: currentVersion, upgradable: null, latest: null, @@ -146,7 +155,8 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { slug: slug, section: section, kind: locked.dependency, - sourceKind: locked.sourceKind, + hasModrinthSource: locked.modrinth != null, + sourceKind: locked.kind, current: currentVersion, upgradable: null, latest: null, @@ -157,19 +167,25 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { final acceptsMc = manifestEntry?.acceptsMc ?? const []; final gameVersions = {mcVersion, ...acceptsMc}.toList(); - final loaderFilter = filterLoadersForSection(loaderConfig, section); + final loaderFilter = filterLoadersForSection( + loaderConfig, + section, + registry: read(loaderRegistryProvider), + host: host, + ); final channel = manifestEntry?.channel ?? Channel.alpha; final constraint = _safeParseConstraint(manifestEntry?.constraintRaw); final List versions; if (offline) { - final pid = locked.projectId; + final pid = locked.modrinth?.projectId; if (pid == null) { return _Row( slug: slug, section: section, kind: locked.dependency, - sourceKind: locked.sourceKind, + hasModrinthSource: locked.modrinth != null, + sourceKind: locked.kind, current: currentVersion, upgradable: null, latest: null, @@ -177,7 +193,7 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { marker: marker, ); } - versions = cache.listCachedVersions(pid).where((v) { + versions = cache.listCachedVersions(host, pid).where((v) { final loaderOk = loaderFilter == null || v.loaders.any(loaderFilter.contains); final mcOk = v.gameVersions.any(gameVersions.contains); @@ -198,7 +214,8 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { slug: slug, section: section, kind: locked.dependency, - sourceKind: locked.sourceKind, + hasModrinthSource: locked.modrinth != null, + sourceKind: locked.kind, current: currentVersion, upgradable: null, latest: null, @@ -225,7 +242,8 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { slug: slug, section: section, kind: locked.dependency, - sourceKind: locked.sourceKind, + hasModrinthSource: locked.modrinth != null, + sourceKind: locked.kind, current: currentVersion, upgradable: upgradable?.versionNumber, latest: latest?.versionNumber, @@ -256,7 +274,8 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { 'slug': r.slug, 'section': r.section.name, 'kind': r.kind.name, - 'source': r.sourceKind.name, + 'source': r.source, + 'has_modrinth_source': r.hasModrinthSource, 'current': r.current.isEmpty ? null : {'version': r.current}, 'upgradable': r.upgradable == null ? null @@ -407,7 +426,7 @@ class OutdatedCommand extends GitrinthCommand with OfflineFlag { } bool _isUpToDate(_Row r) { - if (r.sourceKind != LockedSourceKind.modrinth) return true; + if (!r.hasModrinthSource) return true; if (r.upgradable == null && r.latest == null) return true; if (r.latest == null) return r.upgradable == r.current; return r.current == r.latest; @@ -441,7 +460,11 @@ class _Row { final String slug; final Section section; final LockedDependencyKind kind; - final LockedSourceKind sourceKind; + final bool hasModrinthSource; + final LockedEntryKind sourceKind; + String get source => sourceKind == LockedEntryKind.resolved + ? (hasModrinthSource ? 'modrinth' : 'curseforge') + : sourceKind.name; final String current; final String? upgradable; final String? latest; @@ -451,6 +474,7 @@ class _Row { required this.slug, required this.section, required this.kind, + required this.hasModrinthSource, required this.sourceKind, required this.current, required this.upgradable, diff --git a/lib/src/commands/override_command.dart b/lib/src/commands/override_command.dart index b7d9844..191330f 100644 --- a/lib/src/commands/override_command.dart +++ b/lib/src/commands/override_command.dart @@ -3,16 +3,16 @@ import 'dart:io'; import 'package:dio/dio.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/modrinth/project.dart'; -import '../model/resolver/constraint.dart'; import '../service/manifest_io.dart'; -import '../service/section_inference.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/section_inference.dart'; +import '../service/resolve/solve_report.dart'; import '_constraint_helpers.dart'; import 'add_command_editor.dart'; import 'override_command_editor.dart'; @@ -156,6 +156,7 @@ class OverrideCommand extends GitrinthCommand with OfflineFlag { final io = ManifestIo(); final existingManifest = io.readModsYaml(); + await prepareLoaderRegistry(container, existingManifest, offline: offline); final existingStandalone = io.readProjectOverrides(); // Duplicate check — refuse if the slug is already overridden in @@ -225,7 +226,9 @@ class OverrideCommand extends GitrinthCommand with OfflineFlag { break; } } - final api = read(modrinthApiProvider); + final api = read( + modrinthApiFactoryProvider, + ).forHost(existingManifest.modrinthHost); final Section inferredSection; Project? project; if (declaredSection != null) { @@ -253,10 +256,13 @@ class OverrideCommand extends GitrinthCommand with OfflineFlag { section = inferredSection; } - // Version resolution (mirrors `add`'s no-`@constraint` path). final String effectiveConstraint; if (constraintRaw == null) { final latest = await pickLatestReleaseVersion( + registry: read(loaderRegistryProvider), + host: + existingManifest.modrinthHost ?? + read(modrinthApiFactoryProvider).defaultBaseUrl, api: api, slug: slug, section: section, @@ -265,49 +271,34 @@ class OverrideCommand extends GitrinthCommand with OfflineFlag { acceptsMc: acceptsMc, ); if (latest == null) { - final widened = acceptsMc.isEmpty - ? '' - : ' (widened with accepts-mc=${acceptsMc.join(",")})'; throw UserError( - "No release version of '$slug' is compatible with " - "loader=${loaderNameForSection(existingManifest.loader, section) ?? ''} " - "mc=${existingManifest.mcVersion}$widened. " - 'Pass `@` explicitly to pin an alpha/beta.', + noCompatibleReleaseMessage( + slug, + loaderName: loaderNameForSection( + existingManifest.loader, + section, + ), + mcVersion: existingManifest.mcVersion, + acceptsMc: acceptsMc, + ), ); } - if (exactFlag) { - effectiveConstraint = '^${latest.versionNumber}'; - } else if (pinFlag) { - effectiveConstraint = bareVersionForPin(latest.versionNumber); - } else { - effectiveConstraint = caretOrPinFallback(latest.versionNumber); - } + effectiveConstraint = constraintForSelectedVersion( + latest.versionNumber, + exact: exactFlag, + pin: pinFlag, + ); } else { - // Validate the user-supplied constraint so a bad `@xyz` fails - // fast with a single-line error before we touch any file. - final channel = parseChannelToken(constraintRaw); - if (channel == null) { - parseConstraint(constraintRaw); // throws ValidationError - } - effectiveConstraint = constraintRaw; + effectiveConstraint = validatedEntryConstraint(constraintRaw); } - final needsLongForm = - (envOpt != null && envOpt != 'both') || acceptsMc.isNotEmpty; - if (needsLongForm) { - final long = {'version': effectiveConstraint}; - writeSideFields(long, envOpt); - if (acceptsMc.isNotEmpty) { - long['accepts-mc'] = acceptsMc.length == 1 - ? acceptsMc.first - : acceptsMc; - } - longForm = long; - writtenValue = null; - } else { - longForm = null; - writtenValue = effectiveConstraint; - } + final fields = versionEntryFields( + effectiveConstraint, + env: envOpt, + acceptsMc: acceptsMc, + ); + longForm = fields.length == 1 ? null : fields; + writtenValue = longForm == null ? effectiveConstraint : null; } final String updated; diff --git a/lib/src/commands/pack_assembler.dart b/lib/src/commands/pack_assembler.dart index 37f21dc..45954b1 100644 --- a/lib/src/commands/pack_assembler.dart +++ b/lib/src/commands/pack_assembler.dart @@ -4,7 +4,9 @@ import '../cli/exceptions.dart'; import '../model/manifest/mods_lock.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/manifest/mrpack_index.dart'; +import '../model/curseforge/cf_constants.dart'; import '../service/cache.dart'; +import '../service/modrinth/modrinth_url.dart'; import 'build_assembler.dart'; /// Which side of a modpack a single `.mrpack` artifact targets. @@ -98,13 +100,11 @@ class OverridesPlan { /// Only modrinth-source entries land in `files[]`; url/path entries are /// routed to [collectOverrides] and packed loose under `overrides/`. /// -/// When [publishable] is true and any url/path entry exists in -/// [Section.mods], throws [ValidationError] listing every offending -/// slug. Other sections (resource packs, data packs, shaders) are -/// allowed under `--publishable` because Modrinth's permission policy -/// targets executable code only. +/// Publishable builds reject target-missing sources and explicit hash +/// mismatch allowances across included content sections. MrpackIndex buildIndex({ required ModsYaml yaml, + LoaderRegistry registry = LoaderRegistry.builtins, required ModsLock lock, required PackTarget target, required bool publishable, @@ -125,19 +125,22 @@ MrpackIndex buildIndex({ } if (publishable) { - // --publishable is global, not per-target: even a server-only mod - // is published via the same Modrinth pipeline as the client pack. final offenders = []; - for (final entry in lock.mods.values) { - if (entry.sourceKind != LockedSourceKind.modrinth) { - offenders.add('${entry.slug} (${entry.sourceKind.name})'); + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).values) { + if (!_includeForTarget(entry, target)) continue; + if (entry.modrinth == null || + yaml.sectionEntries(section)[entry.slug]?.allowHashMismatch == + true) { + offenders.add('${entry.slug} (${entry.kind.name})'); + } } } if (offenders.isNotEmpty) { offenders.sort(); throw ValidationError( - '--publishable refused: the following mods use a non-Modrinth ' - 'source and cannot be referenced by a publishable .mrpack:\n' + '--publishable refused: the following entries lack a Modrinth ' + 'source or enable allow_hash_mismatch:\n' '${offenders.map((o) => ' - $o').join('\n')}\n' 'Re-run without --publishable to bundle them as loose files ' 'under overrides/, or remove them from mods.yaml.', @@ -149,7 +152,7 @@ MrpackIndex buildIndex({ for (final section in Section.values) { final subdir = mrpackSubdirFor(section); for (final entry in lock.sectionFor(section).values) { - if (entry.sourceKind != LockedSourceKind.modrinth) continue; + if (entry.modrinth == null) continue; if (!_includeForTarget(entry, target)) continue; files.add(_modrinthFileFor(entry, subdir)); } @@ -167,19 +170,20 @@ MrpackIndex buildIndex({ // collection-if also lets the loader entry drop cleanly when // the pack is vanilla. if (lock.loader.hasModRuntime && modLoaderVersion != null) - mrpackLoaderKey(lock.loader.mods): modLoaderVersion, + mrpackLoaderKey(lock.loader.mods, registry: registry): modLoaderVersion, }, ); } MrpackFile _modrinthFileFor(LockedEntry entry, String subdir) { - final file = entry.file; - if (file == null) { + final mr = entry.modrinth; + if (mr == null) { throw ValidationError( - 'lockfile entry "${entry.slug}" is a modrinth source but has no ' - '`file` block. Re-run `gitrinth get` to repopulate.', + 'lockfile entry "${entry.slug}" has no Modrinth source block; ' + 'cannot include in a publishable .mrpack.', ); } + final file = mr.file; final sha512 = file.sha512; if (sha512 == null) { throw ValidationError( @@ -213,15 +217,16 @@ MrpackFile _modrinthFileFor(LockedEntry entry, String subdir) { } String _canonicalModrinthUrl(LockedEntry entry, LockedFile file) { - if (entry.projectId == null || entry.versionId == null) { + final mr = entry.modrinth; + if (mr == null) { throw ValidationError( 'lockfile entry "${entry.slug}" has no download url and is missing ' - 'projectId/versionId; cannot synthesize a Modrinth CDN URL.', + 'a Modrinth source block; cannot synthesize a Modrinth CDN URL.', ); } final encodedName = Uri.encodeComponent(file.name); - return 'https://cdn.modrinth.com/data/${entry.projectId}/versions/' - '${entry.versionId}/$encodedName'; + return 'https://cdn.modrinth.com/data/${mr.projectId}/versions/' + '${mr.versionId}/$encodedName'; } /// Collects `overrides//` plans for every url/path @@ -244,6 +249,8 @@ OverridesPlan collectOverrides({ required GitrinthCache cache, required String projectDir, required PackTarget target, + SourceKind platform = SourceKind.modrinth, + String modrinthHost = defaultModrinthBaseUrl, }) { final entries = []; var hasModOverrides = false; @@ -251,22 +258,38 @@ OverridesPlan collectOverrides({ for (final section in Section.values) { final subdir = mrpackSubdirFor(section); for (final entry in lock.sectionFor(section).values) { - if (entry.sourceKind == LockedSourceKind.modrinth) continue; + // Skip entries that already participate in the publishable + // `files[]` mrpack list via Modrinth. + if (platform == SourceKind.modrinth && entry.modrinth != null) continue; + if (platform == SourceKind.curseforge && + entry.curseforge != null && + (section != Section.plugins || + (lock.loader.plugins != null && + pluginLoaderEligibleForCurseforge(lock.loader.plugins!)))) { + continue; + } if (!_includeForTarget(entry, target)) continue; + // Resolve against the source host even when publishing to another + // platform; publication host selection does not relocate cached bytes. final sourcePath = resolveSourcePath( cache, entry, projectDir: projectDir, + modrinthHost: modrinthHost, ); final destName = destFilenameFor(entry); entries.add( OverridePlan( slug: entry.slug, section: section, - sourceKind: entry.sourceKind.name, + sourceKind: entry.kind == LockedEntryKind.resolved + ? (entry.modrinth != null ? 'modrinth' : 'curseforge') + : entry.kind.name, sourcePath: sourcePath, zipPath: p.posix.join( - _overridesRootFor(entry.client, entry.server), + platform == SourceKind.curseforge + ? 'overrides' + : _overridesRootFor(entry.client, entry.server), subdir, destName, ), @@ -316,7 +339,9 @@ OverridesPlan collectOverrides({ sourceKind: 'file', sourcePath: sourcePath, zipPath: p.posix.join( - _overridesRootFor(entry.client, entry.server), + platform == SourceKind.curseforge + ? 'overrides' + : _overridesRootFor(entry.client, entry.server), entry.destination, ), ), diff --git a/lib/src/commands/pack_command.dart b/lib/src/commands/pack_command.dart index 9eb7920..05d05b8 100644 --- a/lib/src/commands/pack_command.dart +++ b/lib/src/commands/pack_command.dart @@ -1,10 +1,11 @@ import 'dart:convert'; import 'dart:io'; -import 'package:archive/archive.dart'; import 'package:path/path.dart' as p; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; +import '../app/env.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; @@ -13,9 +14,13 @@ import '../model/manifest/mods_lock.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/manifest/mrpack_index.dart'; import '../service/cache.dart'; +import '../service/pack_archive_writer.dart'; +import '../service/modrinth/modrinth_url.dart'; import '../service/manifest_io.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/solve_report.dart'; import 'pack_assembler.dart'; +import 'curseforge_pack_assembler.dart'; +import 'server_distribution_assembler.dart'; const String _permissionsArticle = 'https://support.modrinth.com/en/articles/8797527-obtaining-modpack-permissions'; @@ -29,7 +34,8 @@ class PackCommand extends GitrinthCommand with OfflineFlag { String get name => 'pack'; @override - String get description => 'Produce Modrinth .mrpack artifacts.'; + String get description => + 'Produce Modrinth .mrpack or CurseForge .zip artifacts.'; @override String get invocation => 'gitrinth pack [arguments]'; @@ -54,8 +60,19 @@ class PackCommand extends GitrinthCommand with OfflineFlag { ..addFlag( 'publishable', negatable: false, - help: 'Refuse to pack if any mod uses a url: or path: source.', + help: 'Reject target-missing sources and allow_hash_mismatch entries.', ); + argParser.addFlag( + 'curseforge', + negatable: false, + help: 'Produce CurseForge .zip archives.', + ); + argParser.addFlag( + 'server-distribution', + negatable: false, + help: + 'With --curseforge, bundle physical server files in a distinct ZIP; runtime is not bundled.', + ); addOfflineFlag(); } @@ -67,6 +84,15 @@ class PackCommand extends GitrinthCommand with OfflineFlag { final outputOpt = argResults!['output'] as String?; final combined = argResults!['combined'] as bool; + final serverDistribution = argResults!['server-distribution'] as bool; + if (serverDistribution && !(argResults!['curseforge'] as bool)) { + throw const UsageError('--server-distribution requires --curseforge.'); + } + if (serverDistribution && combined) { + throw const UsageError( + '--server-distribution cannot be combined with --combined.', + ); + } final publishable = argResults!['publishable'] as bool; final offline = readOfflineFlag(); @@ -90,6 +116,33 @@ class PackCommand extends GitrinthCommand with OfflineFlag { ); } + if (serverDistribution) { + final output = p.normalize( + p.absolute( + outputOpt ?? + p.join( + 'build', + '${yaml.slug}-${yaml.version}-server-distribution.zip', + ), + ), + ); + await writeServerDistribution( + File(output), + yaml: yaml, + lock: lock, + cache: cache, + projectDir: io.directory.path, + registry: read(loaderRegistryProvider), + modrinthHost: resolveModrinthBaseUrl(read(environmentProvider)), + publishable: publishable, + ); + console.message('Wrote CurseForge server distribution: $output'); + console.message( + 'The server runtime is not bundled. Install the matching runtime separately; Minecraft EULA acceptance is required before running it.', + ); + return exitOk; + } + // The permissions warning fires on the union of all mod overrides // across every produced pack — collect them once up-front so it // doesn't double-fire when we emit two packs. @@ -98,6 +151,10 @@ class PackCommand extends GitrinthCommand with OfflineFlag { cache: cache, projectDir: io.directory.path, target: PackTarget.combined, + platform: argResults!['curseforge'] as bool + ? SourceKind.curseforge + : SourceKind.modrinth, + modrinthHost: resolveModrinthBaseUrl(read(environmentProvider)), ); final basePath = _resolveBasePath(yaml: yaml, outputOpt: outputOpt); @@ -131,10 +188,13 @@ class PackCommand extends GitrinthCommand with OfflineFlag { projectDir: io.directory.path, publishable: publishable, ); - console.message(_serverInstallerHint); + if (!(argResults!['curseforge'] as bool)) { + console.message(_serverInstallerHint); + } } - if (!publishable && allOverridesForWarning.hasModOverrides) { + if (!publishable && + allOverridesForWarning.entries.any((e) => e.section != null)) { _printPermissionsWarning(allOverridesForWarning.entries); } @@ -148,8 +208,9 @@ class PackCommand extends GitrinthCommand with OfflineFlag { required ModsYaml yaml, required String? outputOpt, }) { + final extension = argResults!['curseforge'] as bool ? 'zip' : 'mrpack'; final raw = - outputOpt ?? p.join('build', '${yaml.slug}-${yaml.version}.mrpack'); + outputOpt ?? p.join('build', '${yaml.slug}-${yaml.version}.$extension'); return p.normalize(p.absolute(raw)); } @@ -171,7 +232,32 @@ class PackCommand extends GitrinthCommand with OfflineFlag { required String projectDir, required bool publishable, }) { + if (argResults!['curseforge'] as bool) { + final manifest = buildCurseForgeManifest( + registry: read(loaderRegistryProvider), + yaml: yaml, + lock: lock, + target: target, + publishable: publishable, + ); + final overrides = curseForgeOverrides( + lock: lock, + cache: cache, + projectDir: projectDir, + target: target, + modrinthHost: resolveModrinthBaseUrl(read(environmentProvider)), + ); + writePackArchive( + File(outputPath), + manifestName: 'manifest.json', + manifest: manifest, + overrides: overrides.entries, + ); + console.message('Wrote ${target.name} CurseForge pack: $outputPath'); + return; + } final index = buildIndex( + registry: read(loaderRegistryProvider), yaml: yaml, lock: lock, target: target, @@ -182,6 +268,7 @@ class PackCommand extends GitrinthCommand with OfflineFlag { cache: cache, projectDir: projectDir, target: target, + modrinthHost: resolveModrinthBaseUrl(read(environmentProvider)), ); for (final o in overrides.entries) { @@ -217,13 +304,8 @@ class PackCommand extends GitrinthCommand with OfflineFlag { required MrpackIndex index, required List overrides, }) { - final archive = Archive(); - final indexJson = const JsonEncoder.withIndent(' ').convert(index.toMap()); final indexBytes = utf8.encode(indexJson); - archive.addFile( - ArchiveFile('modrinth.index.json', indexBytes.length, indexBytes), - ); for (final o in overrides) { final src = File(o.sourcePath); @@ -233,27 +315,25 @@ class PackCommand extends GitrinthCommand with OfflineFlag { 'Re-run `gitrinth get` to repopulate the cache.', ); } - final bytes = src.readAsBytesSync(); - archive.addFile(ArchiveFile(o.zipPath, bytes.length, bytes)); } - final encoded = ZipEncoder().encode(archive); - outputFile.writeAsBytesSync(encoded); + writeArchive( + outputFile, + manifestName: 'modrinth.index.json', + manifest: indexBytes, + entries: overrides.map( + (entry) => (path: entry.zipPath, source: File(entry.sourcePath)), + ), + ); } void _printPermissionsWarning(List overrides) { - // Mod overrides may live under any of the three loose-files roots - // (overrides/, client-overrides/, server-overrides/) depending on - // env, so filter by section rather than by zipPath prefix. - final modOverrides = - overrides.where((o) => o.section == Section.mods).toList() - ..sort((a, b) => a.slug.compareTo(b.slug)); + final artifactOverrides = overrides.where((o) => o.section != null).toList() + ..sort((a, b) => a.slug.compareTo(b.slug)); final buf = StringBuffer() - ..writeln( - 'Note: this pack bundles non-Modrinth mod artifacts. You may need', - ) + ..writeln('Note: this pack bundles loose artifacts. You may need') ..writeln('permission from each mod author to distribute them:'); - for (final o in modOverrides) { + for (final o in artifactOverrides) { buf.writeln(' - ${o.slug} (${o.sourceKind})'); } buf.write('See: $_permissionsArticle'); diff --git a/lib/src/commands/pin_command.dart b/lib/src/commands/pin_command.dart index dc12408..496dd76 100644 --- a/lib/src/commands/pin_command.dart +++ b/lib/src/commands/pin_command.dart @@ -3,7 +3,6 @@ import 'dart:io'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; -import '../model/manifest/mods_lock.dart'; import '../model/resolver/constraint.dart'; import '../service/manifest_io.dart'; import 'add_command_editor.dart'; @@ -70,10 +69,10 @@ class PinCommand extends GitrinthCommand { '— run `gitrinth get`.', ); } - if (locked.sourceKind != LockedSourceKind.modrinth) { + if (locked.modrinth == null) { throw UserError( - "'$slug' is a ${locked.sourceKind.name} source; only Modrinth " - 'entries can be pinned.', + "'$slug' has no Modrinth source block (kind: ${locked.kind.name}); " + 'only Modrinth entries can be pinned.', ); } final rawVersion = locked.version; diff --git a/lib/src/commands/pin_editor.dart b/lib/src/commands/pin_editor.dart index 8e09a61..4a4a9a4 100644 --- a/lib/src/commands/pin_editor.dart +++ b/lib/src/commands/pin_editor.dart @@ -1,10 +1,5 @@ -import 'package:yaml/yaml.dart'; -import 'package:yaml_edit/yaml_edit.dart'; - -import '../cli/exceptions.dart'; import '../model/manifest/mods_yaml.dart'; -import '../util/yaml_root.dart'; -import 'add_command_editor.dart'; +import 'entry_version_editor.dart'; /// Rewrites the constraint for `
.` in [yamlText] to /// [newConstraint], preserving comments and formatting via `yaml_edit`. @@ -18,38 +13,10 @@ String updateEntryConstraint( required Section section, required String slug, required String newConstraint, -}) { - final editor = YamlEditor(yamlText); - final sectionKey = sectionKeyFor(section); - - final root = parseYamlRoot(editor, filename: 'mods.yaml'); - - final sectionNode = root.nodes[sectionKey]; - if (sectionNode == null || sectionNode.value == null) { - throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); - } - if (sectionNode is! YamlMap) { - throw UserError( - "mods.yaml section '$sectionKey' is not a mapping; cannot edit.", - ); - } - - final entryNode = sectionNode.nodes[slug]; - if (entryNode == null) { - throw UserError("'$slug' not found under '$sectionKey' in mods.yaml."); - } - - if (entryNode is YamlMap) { - if (!entryNode.containsKey('version')) { - throw UserError( - "'$slug' has no `version:` field — likely a url/path source; " - 'cannot (un)pin.', - ); - } - editor.update([sectionKey, slug, 'version'], newConstraint); - } else { - editor.update([sectionKey, slug], newConstraint); - } - - return editor.toString(); -} +}) => rewriteEntryVersion( + yamlText, + section: section, + slug: slug, + version: newConstraint, + missingVersionAction: '(un)pin', +); diff --git a/lib/src/commands/platform_command.dart b/lib/src/commands/platform_command.dart new file mode 100644 index 0000000..46436ab --- /dev/null +++ b/lib/src/commands/platform_command.dart @@ -0,0 +1,64 @@ +import 'dart:io'; + +import 'package:yaml_edit/yaml_edit.dart'; + +import '../cli/base_command.dart'; +import '../cli/exceptions.dart'; +import '../cli/exit_codes.dart'; +import '../model/manifest/parser.dart'; +import '../service/manifest_io.dart'; +import '../util/yaml_root.dart'; +import 'publish_command.dart'; + +/// Changes implicit source participation without resolving or changing entries. +class PlatformToggleCommand extends GitrinthCommand { + final String platform; + final bool enabled; + PlatformToggleCommand(this.platform, this.enabled); + + @override + String get name => enabled ? 'enable' : 'disable'; + @override + String get description => + '${enabled ? 'Enable' : 'Disable'} $platform by default in mods.yaml.'; + + @override + Future run() async { + if (argResults!.rest.isNotEmpty) { + throw UsageError('Unexpected arguments: ${argResults!.rest.join(' ')}'); + } + final io = ManifestIo(); + io.readModsYaml(); + final editor = YamlEditor(File(io.modsYamlPath).readAsStringSync()); + final root = parseYamlRoot(editor, filename: 'mods.yaml'); + final value = enabled ? 'enabled' : 'disabled'; + if (root['gitrinth'] == null) { + editor.update(['gitrinth'], {platform: value}); + } else { + editor.update(['gitrinth', platform], value); + } + final text = editor.toString(); + parseModsYaml(text, filePath: io.modsYamlPath); + io.writeModsYaml(text); + console.message('$platform $value in mods.yaml.'); + return exitOk; + } +} + +class CurseForgeCommand extends GitrinthCommand { + CurseForgeCommand() { + addSubcommand(CurseForgePublishCommand()); + addSubcommand(PlatformToggleCommand('curseforge', true)); + addSubcommand(PlatformToggleCommand('curseforge', false)); + } + @override + String get name => 'curseforge'; + @override + String get description => + 'Configure CurseForge participation and publishing.'; + @override + Future run() async { + printUsage(); + return exitOk; + } +} diff --git a/lib/src/commands/publish_command.dart b/lib/src/commands/publish_command.dart new file mode 100644 index 0000000..7e3b7d2 --- /dev/null +++ b/lib/src/commands/publish_command.dart @@ -0,0 +1,720 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:path/path.dart' as p; + +import '../app/env.dart'; +import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; +import '../app/runner_settings.dart'; +import '../cli/base_command.dart'; +import '../cli/exceptions.dart'; +import '../cli/exit_codes.dart'; +import '../model/manifest/mods_yaml.dart'; +import '../service/changelog_reader.dart'; +import '../service/file_digest.dart'; +import '../service/curseforge/curseforge_upload_client.dart'; +import '../service/curseforge/curseforge_publication_receipt.dart'; +import '../service/curseforge/curseforge_publication_service.dart'; +import '../service/curseforge/curseforge_url.dart'; +import '../service/manifest_io.dart'; +import '../service/modrinth/modrinth_auth_interceptor.dart'; +import '../service/modrinth/modrinth_url.dart'; +import '../service/prepared_publication.dart'; +import '../service/user_config.dart'; +import 'curseforge_pack_assembler.dart'; +import 'pack_assembler.dart'; + +class PublishCommand extends PublicationCommand { + @override + String get invocation => 'gitrinth publish [arguments]'; + + @override + String get description => + 'Publish built artifacts to the selected platforms.'; +} + +class CurseForgePublishCommand extends PublicationCommand { + CurseForgePublishCommand() : super(forcedPlatform: SourceKind.curseforge); + + @override + String get invocation => 'gitrinth curseforge publish [arguments]'; + + @override + String get description => 'Upload a built CurseForge .zip.'; +} + +abstract class PublicationCommand extends GitrinthCommand { + final SourceKind? forcedPlatform; + + @override + String get name => 'publish'; + + PublicationCommand({this.forcedPlatform}) { + argParser + ..addFlag( + 'dry-run', + negatable: false, + help: + 'Assemble the payload, hash the artifact, and print the JSON without uploading.', + ) + ..addFlag( + 'force', + abbr: 'f', + negatable: false, + help: 'Skip the interactive confirmation prompt.', + ) + ..addFlag( + 'draft', + negatable: false, + help: + 'Upload as a draft (Modrinth `status: draft`) instead of publicly listed.', + ) + ..addFlag( + 'with-server', + negatable: false, + help: + 'Upload the client, then associate a server-distribution ZIP as an additional file.', + ) + ..addFlag( + 'server-only', + negatable: false, + help: + 'Retry only the CurseForge server distribution against an explicit verified parent.', + ) + ..addOption( + 'curseforge-parent-file', + valueHelp: 'id', + help: 'Existing client file ID required by --server-only.', + ) + ..addOption( + 'curseforge-server-pack', + valueHelp: 'path', + help: + 'Actual server-distribution ZIP, distinct from the legacy manifest-server ZIP.', + ) + ..addOption( + 'curseforge-pack', + valueHelp: 'path', + help: 'CurseForge .zip artifact path when publishing both targets.', + ) + ..addOption( + 'curseforge-project', + valueHelp: 'id', + help: + 'Numeric CurseForge project ID (required for CurseForge uploads).', + ) + ..addFlag( + 'publishable', + negatable: false, + help: 'Reject target-missing sources and allow_hash_mismatch entries.', + ) + ..addOption( + 'version-type', + valueHelp: 'release|beta|alpha', + allowed: ['release', 'beta', 'alpha'], + help: + 'Modrinth version channel. Defaults to `beta` when `mods.yaml.version` carries a pre-release suffix, else `release`.', + ) + ..addOption( + 'changelog', + valueHelp: 'path', + help: + 'Path to a Markdown changelog. Defaults to the matching section of CHANGELOG.md when present.', + ) + ..addOption( + 'pack', + valueHelp: 'path', + help: + 'Path to the .mrpack to upload. Defaults to build/-.mrpack.', + ); + } + + String _publishLoaderToken(LoaderId id, String host) { + final token = read(loaderRegistryProvider)[id]?.modrinthTag(host); + if (token == null) { + throw ValidationError( + 'Loader ${id.name} has no Modrinth publish mapping for $host.', + ); + } + return token; + } + + @override + Future run() async { + if (argResults!.rest.isNotEmpty) { + throw UsageError('Unexpected arguments: ${argResults!.rest.join(' ')}'); + } + final io = ManifestIo(); + final yaml = io.readModsYaml(); + if (yaml.publishTargets.isEmpty) { + throw const UserError( + "publish_to: 'none' in mods.yaml — publishing is disabled for this pack.", + ); + } + final targets = forcedPlatform == null + ? yaml.publishTargets.keys.toList() + : [forcedPlatform!]; + for (final target in targets) { + if (!yaml.publishTargets.containsKey(target)) { + throw UserError('${target.name} is not selected by publish_to.'); + } + } + final withServer = argResults!['with-server'] as bool; + final serverOnly = argResults!['server-only'] as bool; + final parentRaw = argResults!['curseforge-parent-file'] as String?; + final parentId = int.tryParse(parentRaw ?? ''); + if (withServer && serverOnly) { + throw const UsageError( + '--with-server and --server-only cannot be combined.', + ); + } + if ((withServer || serverOnly) && + !targets.contains(SourceKind.curseforge)) { + throw const UsageError( + 'Server publication requires CurseForge as a selected target.', + ); + } + if (serverOnly && targets.contains(SourceKind.modrinth)) { + throw const UsageError( + '--server-only cannot publish Modrinth. Use `gitrinth curseforge publish --server-only` for a CurseForge-only retry.', + ); + } + if (serverOnly && (parentId == null || parentId <= 0)) { + throw const UsageError( + '--server-only requires --curseforge-parent-file .', + ); + } + if (!serverOnly && parentRaw != null) { + throw const UsageError( + '--curseforge-parent-file is only valid with --server-only.', + ); + } + if (!withServer && + !serverOnly && + argResults!['curseforge-server-pack'] != null) { + throw const UsageError( + '--curseforge-server-pack requires --with-server or --server-only.', + ); + } + await prepareLoaderRegistry(container, yaml, offline: true); + final plans = _preparePublications( + yaml, + io, + targets, + withServer: withServer, + serverOnly: serverOnly, + parentId: parentId, + ); + final dryRun = argResults!['dry-run'] as bool; + final cfPlan = plans + .where( + (plan) => + plan.role == PublicationArtifactRole.server || + plan.dependentServer != null, + ) + .firstOrNull; + final factory = cfPlan == null ? null : read(curseForgeApiFactoryProvider); + final service = factory == null + ? null + : CurseForgePublicationService( + uploadClient: CurseForgeUploadClient( + read(dioProvider), + baseUrl: cfPlan?.serverUrl ?? defaultCurseForgeUploadBaseUrl, + ), + readFile: factory.publicationFile, + listFiles: factory.publicationFiles, + store: CurseForgePublicationReceiptStore(io.directory.path), + ); + final locked = io.readModsLock(); + final loader = locked?.loader ?? yaml.loader; + final prepared = cfPlan == null || service == null + ? null + : await service.prepare( + plan: cfPlan, + slug: yaml.slug, + version: yaml.version, + displayName: '${yaml.name} ${yaml.version}', + minecraft: yaml.mcVersion, + loaderId: loader.mods.name, + loaderVersion: loader.modLoaderVersion, + pluginLoaderId: loader.plugins?.name, + pluginLoaderVersion: loader.pluginLoaderVersion, + clientLoaderToken: read( + loaderRegistryProvider, + )[loader.mods]?.curseforgeArchiveToken, + requireLoaderVersionMatch: !const { + 'stable', + 'latest', + }.contains(loader.modLoaderVersion), + requirePluginVersionMatch: !const { + 'stable', + 'latest', + }.contains(loader.pluginLoaderVersion), + peers: plans + .where((plan) => plan.target == SourceKind.modrinth) + .toList(), + ); + if (!dryRun && !(argResults!['force'] as bool)) { + if (!stdin.hasTerminal) { + throw const UsageError( + 'publish: refusing to upload without --force when stdin is not a terminal.', + ); + } + stdout.write( + 'Publish ${yaml.slug} v${yaml.version} to ${targets.map((e) => e.name).join(', ')}? [y/N] ', + ); + final answer = (stdin.readLineSync() ?? '').trim().toLowerCase(); + if (answer != 'y' && answer != 'yes') { + console.message('Aborted.'); + return exitOk; + } + } + final completed = []; + for (final plan in plans) { + if (identical(plan, cfPlan) && service != null && prepared != null) { + if (dryRun) { + final payload = + { + ...prepared.server.payload, + 'parentFileID': + prepared.receipt.parentId ?? '', + } + ..remove('gameVersions') + ..remove('gameVersionNames'); + console.message( + 'Dry run: CurseForge client -> server additional file (server-pack classification requires verification).', + ); + console.message( + jsonEncode({ + 'client': serverOnly + ? {'existingFileID': parentId} + : {'artifact': plan.artifact.path, 'metadata': plan.payload}, + 'server': { + 'artifact': prepared.server.artifact.path, + 'metadata': payload, + }, + }), + ); + } else { + try { + final result = await service.publish(prepared); + console.message( + 'Published CurseForge client file ${result.parentId} and additional server file ${result.childId}.', + ); + if (result.classificationConfirmed) { + console.message( + 'Server-pack classification confirmed by CurseForge read-back.', + ); + } else { + console.message( + 'Server-pack classification pending: open the matching client release in the CurseForge author console and classify the additional file as its server pack. Rerun the same command to verify; completed uploads are reused.', + ); + } + } on CurseForgePublicationFailure catch (e) { + final parent = e.parentId; + final surviving = [ + if (completed.isNotEmpty) completed.join(', '), + if (parent != null) 'CurseForge client file $parent', + if (e.childId != null) 'CurseForge child file ${e.childId}', + ]; + final retry = parent == null + ? 'Inspect remote files before any retry.' + : 'Retry only the server: ${_serverRetryCommand(io.directory.path, prepared, parent)}'; + final pendingPeers = plans + .where( + (peer) => + peer.target == SourceKind.modrinth && + !service.peerComplete(prepared, peer), + ) + .map((peer) => peer.target.name) + .toList(); + throw UserError( + '${surviving.isEmpty ? "Publication failed" : "Partial publication; ${surviving.join(', ')} survives"}: ${e.message}\n${e.unknownOutcome ? "Remote outcome is unknown; inspect remote files before retrying. Reconciliation must succeed before another POST.\n" : ""}$retry${pendingPeers.isEmpty ? "" : "\nOther selected targets remain pending: ${pendingPeers.join(', ')}."}', + ); + } + } + } else if (service != null && + prepared != null && + plan.target == SourceKind.modrinth && + !dryRun) { + if (service.peerComplete(prepared, plan)) { + console.message( + 'Reusing completed Modrinth version ${service.peerId(prepared, plan)}.', + ); + completed.add('Modrinth version ${service.peerId(prepared, plan)}'); + continue; + } + await service.startPeer(prepared, plan); + try { + final id = await _publish(plan, yaml, dryRun: false); + service.completePeer(prepared, plan, id); + completed.add('Modrinth version $id'); + } on Object catch (e) { + service.failPeer(prepared, plan, e); + throw UserError( + 'Peer-platform publication failed. ${e is GitrinthException ? e.message : "Inspect its remote release and the publication receipt before retrying."}', + ); + } + } else { + final id = await _publish(plan, yaml, dryRun: dryRun); + if (!dryRun) completed.add('${plan.target.name} file $id'); + } + } + return exitOk; + } + + List _preparePublications( + ModsYaml yaml, + ManifestIo io, + List targets, { + required bool withServer, + required bool serverOnly, + required int? parentId, + }) { + final lock = io.readModsLock(); + final publishable = argResults!['publishable'] as bool; + if (lock == null && publishable) { + throw const UserError( + '--publishable requires mods.lock. Run `gitrinth get` first.', + ); + } + final env = read(environmentProvider); + final stored = _readTokens(); + final pending = + < + ({ + SourceKind target, + String serverUrl, + String token, + File artifact, + int? projectId, + }) + >[]; + for (final target in targets) { + final projectId = target == SourceKind.curseforge + ? int.tryParse(argResults!['curseforge-project'] as String? ?? '') + : null; + if (target == SourceKind.curseforge && (projectId ?? 0) <= 0) { + throw const UsageError( + 'CurseForge publishing requires --curseforge-project .', + ); + } + final serverUrl = target == SourceKind.modrinth + ? yaml.publishTargets[target] ?? resolveModrinthBaseUrl(env) + : env['GITRINTH_CURSEFORGE_UPLOAD_URL'] ?? + defaultCurseForgeUploadBaseUrl; + final String? token; + if (target == SourceKind.curseforge) { + token = resolveCurseForgeUploadToken(env: env, userTokens: stored); + if (token == null) { + throw const AuthenticationError( + 'No CurseForge publish token configured. Set GITRINTH_CURSEFORGE_UPLOAD_TOKEN or run `gitrinth token add curseforge.com`.', + ); + } + } else { + final envToken = env['GITRINTH_TOKEN']; + token = + normalizeServerKey(serverUrl) == + normalizeServerKey(resolveModrinthBaseUrl(env)) && + (envToken ?? '').isNotEmpty + ? envToken + : _bestTokenFor(stored, serverUrl); + if (token == null || token.isEmpty) { + throw AuthenticationError( + 'No token configured for $serverUrl. Run `gitrinth token add $serverUrl`.', + ); + } + } + final artifact = serverOnly && target == SourceKind.curseforge + ? _serverArtifactFor(yaml) + : _artifactFor(yaml, target, targets.length > 1); + if (!artifact.existsSync()) { + throw UserError( + 'Pack artifact not found at ${artifact.path}. Run `gitrinth pack${target == SourceKind.curseforge ? ' --curseforge' : ''}` first.', + ); + } + if (lock != null && !serverOnly) { + if (target == SourceKind.modrinth) { + buildIndex( + registry: read(loaderRegistryProvider), + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: publishable, + ); + } else { + buildCurseForgeManifest( + registry: read(loaderRegistryProvider), + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: publishable, + ); + } + final overrides = collectOverrides( + lock: lock, + cache: read(cacheProvider), + projectDir: io.directory.path, + target: PackTarget.client, + platform: target, + modrinthHost: resolveModrinthBaseUrl(env), + ); + for (final entry in overrides.entries.where((e) => e.section != null)) { + console.message( + 'Warning: ${entry.slug} is unavailable on ${target.name}; bundled as a loose override with author permission.', + ); + } + } + pending.add(( + target: target, + serverUrl: serverUrl, + token: token, + artifact: artifact, + projectId: projectId, + )); + } + final changelog = + _resolveChangelog(yaml: yaml, projectDir: io.directory) ?? ''; + final versionType = + (argResults!['version-type'] as String?) ?? + _defaultVersionType(yaml.version); + final draft = argResults!['draft'] as bool; + return [ + for (final target in pending) + PreparedPublication( + target: target.target, + serverUrl: target.serverUrl, + token: target.token, + artifact: target.artifact, + curseforgeProjectId: target.projectId, + role: serverOnly + ? PublicationArtifactRole.server + : PublicationArtifactRole.client, + parentFileId: serverOnly ? parentId : null, + dependentServer: withServer && target.target == SourceKind.curseforge + ? PreparedPublication( + target: SourceKind.curseforge, + serverUrl: target.serverUrl, + token: target.token, + artifact: _serverArtifactFor(yaml), + curseforgeProjectId: target.projectId, + role: PublicationArtifactRole.server, + payload: { + 'displayName': '${yaml.name} ${yaml.version} Server', + 'changelog': changelog, + 'changelogType': 'markdown', + 'releaseType': versionType, + 'isMarkedForManualRelease': draft, + }, + ) + : null, + payload: target.target == SourceKind.modrinth + ? { + 'name': '${yaml.name} ${yaml.version}', + 'version_number': yaml.version, + 'changelog': changelog, + 'dependencies': const >[], + 'game_versions': List.unmodifiable([yaml.mcVersion]), + 'version_type': versionType, + 'loaders': List.unmodifiable([ + _publishLoaderToken(yaml.loader.mods, target.serverUrl), + ]), + 'featured': false, + 'status': draft ? 'draft' : 'listed', + 'project_id': yaml.slug, + 'file_parts': const ['file'], + 'primary_file': 'file', + } + : { + 'displayName': + '${yaml.name} ${yaml.version}${serverOnly ? ' Server' : ''}', + 'changelog': changelog, + 'changelogType': 'markdown', + 'gameVersionNames': List.unmodifiable([ + yaml.mcVersion, + ]), + 'releaseType': versionType, + 'isMarkedForManualRelease': draft, + }, + ), + ]; + } + + Future _publish( + PreparedPublication plan, + ModsYaml yaml, { + required bool dryRun, + }) async { + if (plan.target == SourceKind.curseforge) { + if (dryRun) { + console.message('Dry run: would publish to CurseForge'); + console.message( + const JsonEncoder.withIndent(' ').convert(plan.payload), + ); + return null; + } + final result = + await CurseForgeUploadClient( + read(dioProvider), + baseUrl: plan.serverUrl, + ).upload( + projectId: plan.curseforgeProjectId!, + token: plan.token, + archive: plan.artifact, + metadata: plan.payload, + ); + console.message( + 'Published ${yaml.slug} v${yaml.version} to CurseForge (file id: ${result.fileId}).', + ); + return result.fileId.toString(); + } + if (dryRun) { + final [sha1Digest, sha512Digest] = await fileDigests(plan.artifact, [ + sha1, + sha512, + ]); + console.message('Dry run: would publish to ${plan.serverUrl}'); + console.message( + 'Pack: ${plan.artifact.path} (sha1=$sha1Digest sha512=$sha512Digest)', + ); + console.message(const JsonEncoder.withIndent(' ').convert(plan.payload)); + return null; + } + final response = await read(dioProvider).post( + '${plan.serverUrl.replaceFirst(RegExp(r'/$'), '')}/project/${yaml.slug}/version', + data: FormData.fromMap({ + 'data': MultipartFile.fromBytes( + utf8.encode(jsonEncode(plan.payload)), + filename: 'data.json', + contentType: DioMediaType('application', 'json'), + ), + 'file': await MultipartFile.fromFile( + plan.artifact.path, + filename: p.basename(plan.artifact.path), + contentType: DioMediaType('application', 'x-modrinth-modpack+zip'), + ), + }), + options: Options( + contentType: 'multipart/form-data', + headers: {'Authorization': plan.token}, + extra: const {kModrinthAuthRequired: true}, + ), + ); + final body = response.data; + final versionId = body is Map ? body['id'] : null; + console.message( + 'Published ${yaml.slug} v${yaml.version} to ${plan.serverUrl}${versionId == null ? '' : ' (version id: $versionId)'}.', + ); + return versionId?.toString(); + } + + File _serverArtifactFor(ModsYaml yaml) { + final raw = + (argResults!['curseforge-server-pack'] as String?) ?? + p.join('build', '${yaml.slug}-${yaml.version}-server-distribution.zip'); + final file = File(p.normalize(p.absolute(raw))); + if (!file.existsSync()) { + throw UserError( + 'Server distribution not found at ${file.path}. Run `gitrinth pack --curseforge --server-distribution` first.', + ); + } + return file; + } + + String _serverRetryCommand( + String projectDir, + PreparedCurseForgePublication prepared, + int parent, + ) { + final settings = read(runnerSettingsProvider); + final config = settings.configPath; + final descriptors = settings.loaderDescriptorsPath; + return [ + 'gitrinth', + '-C', + _shellQuote(p.normalize(p.absolute(projectDir))), + if (config != null && config.isNotEmpty) ...[ + '--config', + _shellQuote(read(userConfigPathProvider)), + ], + 'curseforge', + 'publish', + if (descriptors != null) ...[ + '--loader-descriptors', + _shellQuote(p.normalize(p.absolute(descriptors))), + ], + '--server-only', + '--curseforge-project', + prepared.receipt.projectId.toString(), + '--curseforge-parent-file', + parent.toString(), + '--curseforge-server-pack', + _shellQuote(prepared.server.artifact.path), + '--version-type', + prepared.receipt.releaseType, + if (prepared.receipt.manualRelease) '--draft', + '--force', + ].join(' '); + } + + String _shellQuote(String value) => Platform.isWindows + ? "'${value.replaceAll("'", "''")}'" + : "'${value.replaceAll("'", "'\\''")}'"; + + Map _readTokens() { + try { + return read(userConfigStoreProvider).read().tokens; + } on UserError { + return const {}; + } + } + + String? _bestTokenFor(Map tokens, String baseUrl) { + final key = normalizeServerKey(baseUrl); + String? best; + for (final entry in tokens.entries) { + if ((key == entry.key || key.startsWith('${entry.key}/')) && + (best == null || entry.key.length > best.length)) { + best = entry.key; + } + } + return best == null ? null : tokens[best]; + } + + File _artifactFor(ModsYaml yaml, SourceKind target, bool multiple) { + final raw = target == SourceKind.modrinth + ? (argResults!['pack'] as String?) ?? + p.join('build', '${yaml.slug}-${yaml.version}.mrpack') + : (argResults!['curseforge-pack'] as String?) ?? + (multiple ? null : argResults!['pack'] as String?) ?? + p.join('build', '${yaml.slug}-${yaml.version}.zip'); + return File(p.normalize(p.absolute(raw))); + } + + String? _resolveChangelog({ + required ModsYaml yaml, + required Directory projectDir, + }) { + final flag = argResults!['changelog'] as String?; + if (flag != null && flag.isNotEmpty) { + final file = File(flag); + if (!file.existsSync()) throw UserError('Changelog not found: $flag'); + return file.readAsStringSync().trim(); + } + return readChangelogSection( + changelogFile: File(p.join(projectDir.path, 'CHANGELOG.md')), + version: yaml.version, + ); + } + + String _defaultVersionType(String version) { + final hyphen = version.indexOf('-'); + if (hyphen < 0) return 'release'; + return version.substring(hyphen + 1).toLowerCase().startsWith('alpha') + ? 'alpha' + : 'beta'; + } +} diff --git a/lib/src/commands/remove_command.dart b/lib/src/commands/remove_command.dart index 0e1fae2..cedf397 100644 --- a/lib/src/commands/remove_command.dart +++ b/lib/src/commands/remove_command.dart @@ -6,7 +6,7 @@ import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; import '../model/manifest/mods_yaml.dart'; import '../service/manifest_io.dart'; -import '../service/solve_report.dart'; +import '../service/resolve/solve_report.dart'; import 'add_command_editor.dart'; import 'remove_command_editor.dart'; diff --git a/lib/src/commands/server_distribution_assembler.dart b/lib/src/commands/server_distribution_assembler.dart new file mode 100644 index 0000000..57e8030 --- /dev/null +++ b/lib/src/commands/server_distribution_assembler.dart @@ -0,0 +1,144 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; + +import '../cli/exceptions.dart'; +import '../model/manifest/mods_lock.dart'; +import '../model/manifest/mods_yaml.dart'; +import '../model/manifest/server_distribution.dart'; +import '../service/artifact_locator.dart'; +import '../service/cache.dart'; +import '../service/file_digest.dart'; +import '../service/modrinth/modrinth_url.dart'; +import '../service/pack_archive_writer.dart'; +import 'build_assembler.dart'; + +/// Packages selected server artifacts; installing the runtime is a separate step. +Future writeServerDistribution( + File output, { + required ModsYaml yaml, + required ModsLock lock, + required GitrinthCache cache, + required String projectDir, + LoaderRegistry registry = LoaderRegistry.builtins, + String modrinthHost = defaultModrinthBaseUrl, + bool publishable = false, +}) async { + if (registry[lock.loader.mods] == null) { + throw ValidationError( + 'Loader ${lock.loader.mods.name} has no metadata capability snapshot for a server distribution.', + ); + } + final pluginLoader = lock.loader.plugins; + if (pluginLoader != null && registry[pluginLoader] == null) { + throw ValidationError( + 'Plugin loader ${pluginLoader.name} has no metadata capability snapshot for a server distribution.', + ); + } + final locator = ArtifactLocator( + cache: cache, + projectDir: projectDir, + defaultModrinthHost: modrinthHost, + ); + final sources = {}; + final destinations = {}; + void include(String path, File source) { + if (!isSafeServerDistributionPath(path)) { + throw ValidationError('Unsafe server distribution path: $path'); + } + if (isPrivateServerDistributionPath(path)) return; + if (!destinations.add(path.toLowerCase())) { + throw ValidationError('Duplicate server distribution path: $path'); + } + if (!source.existsSync()) { + throw UserError( + 'Server distribution source is missing: ${source.path}. Run `gitrinth get` first.', + ); + } + sources[path] = source; + } + + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).values) { + final subdir = buildSubdirFor(section, BuildEnv.server, entry); + if (subdir == null) continue; + final cf = entry.curseforge; + if (cf != null && !cf.allowModDistribution) { + throw ValidationError( + 'CurseForge project ${cf.projectId}, file ${cf.fileId} (${entry.slug}) disallows redistribution; it cannot be bundled in a server distribution.', + ); + } + if (publishable && + (cf == null || + yaml.sectionEntries(section)[entry.slug]?.allowHashMismatch == + true)) { + throw ValidationError( + '--publishable refused for CurseForge server distribution: ${entry.slug} requires loose overrides or allow_hash_mismatch.', + ); + } + include( + p.posix.join(subdir, destFilenameFor(entry)), + File(locator.preferred(entry)), + ); + } + } + for (final entry in lock.files.values) { + if (!entry.server.includes) continue; + if (isPrivateServerDistributionPath( + p.posix.joinAll(p.split(entry.sourcePath)), + )) { + continue; + } + final source = File(locator.path(entry.sourcePath)); + if (!source.existsSync()) { + include(entry.destination, source); + continue; + } + final resolved = File(source.resolveSymbolicLinksSync()); + if (isPrivateServerDistributionPath( + p.posix.joinAll(p.split(resolved.path)), + )) { + continue; + } + include(entry.destination, resolved); + } + if (sources.isEmpty) { + throw const ValidationError( + 'No physical server files were selected for the server distribution.', + ); + } + final files = []; + for (final entry in sources.entries) { + files.add( + ServerDistributionFile( + path: entry.key, + sha256: await fileDigest(entry.value, sha256), + ), + ); + } + final marker = ServerDistributionManifest( + slug: yaml.slug, + version: yaml.version, + minecraft: lock.mcVersion, + loaderId: lock.loader.mods.name, + loaderVersion: lock.loader.hasModRuntime + ? lock.loader.modLoaderVersion + : null, + pluginLoaderId: pluginLoader?.name, + pluginLoaderVersion: lock.loader.pluginLoaderVersion, + files: files, + ); + output.parent.createSync(recursive: true); + writeArchive( + output, + manifestName: serverDistributionManifestName, + manifest: utf8.encode( + const JsonEncoder.withIndent(' ').convert(marker.toMap()), + ), + entries: sources.entries.map( + (entry) => (path: entry.key, source: entry.value), + ), + ); +} diff --git a/lib/src/commands/slug_constraint_parser.dart b/lib/src/commands/slug_constraint_parser.dart index 0221f8f..2da154b 100644 --- a/lib/src/commands/slug_constraint_parser.dart +++ b/lib/src/commands/slug_constraint_parser.dart @@ -1,4 +1,4 @@ -import '../service/modrinth_project_url.dart'; +import '../service/modrinth/modrinth_project_url.dart'; /// Parses an `add`/`override` positional of the form /// `[@]`, accepting full Modrinth project URLs in diff --git a/lib/src/commands/token_command.dart b/lib/src/commands/token_command.dart new file mode 100644 index 0000000..6907988 --- /dev/null +++ b/lib/src/commands/token_command.dart @@ -0,0 +1,22 @@ +import '../cli/base_command.dart'; +import '../cli/exit_codes.dart'; +import 'modrinth_command.dart'; + +/// Shared credentials namespace; legacy `modrinth token` remains supported. +class TokenCommand extends GitrinthCommand { + TokenCommand() { + addSubcommand(ModrinthTokenAddCommand()); + addSubcommand(ModrinthTokenListCommand()); + addSubcommand(ModrinthTokenRemoveCommand()); + } + @override + String get name => 'token'; + @override + String get description => + 'Manage Modrinth host and CurseForge publish tokens.'; + @override + Future run() async { + printUsage(); + return exitOk; + } +} diff --git a/lib/src/commands/upgrade_command.dart b/lib/src/commands/upgrade_command.dart index 3b5083a..12d2d3b 100644 --- a/lib/src/commands/upgrade_command.dart +++ b/lib/src/commands/upgrade_command.dart @@ -1,20 +1,22 @@ +import 'version_picker.dart'; import 'dart:io'; import 'package:dio/dio.dart'; import '../app/providers.dart'; +import '../app/loader_registry_provider.dart'; import '../cli/base_command.dart'; import '../cli/exceptions.dart'; import '../cli/exit_codes.dart'; import '../cli/offline_flag.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/resolver/constraint.dart'; -import '../service/conflict_disable.dart'; -import '../service/lock_graph.dart'; import '../service/manifest_io.dart'; -import '../service/modrinth_api.dart'; -import '../service/resolve_and_sync.dart'; -import '../service/solve_report.dart'; +import '../service/modrinth/modrinth_api.dart'; +import '../service/resolve/conflict_disable.dart'; +import '../service/resolve/lock_graph.dart'; +import '../service/resolve/resolve_and_sync.dart'; +import '../service/resolve/solve_report.dart'; import 'caret_rewriter.dart'; import 'migrate_editor.dart'; @@ -31,6 +33,12 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { UpgradeCommand() { argParser + ..addFlag( + 'search', + defaultsTo: true, + help: + 'Search for cross-platform peers when a slug is missing or hashes differ.', + ) ..addFlag( 'major-versions', negatable: false, @@ -71,6 +79,11 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { final io = ManifestIo(); final manifest = io.readModsYaml(); + final registry = await prepareLoaderRegistry( + container, + manifest, + offline: offline, + ); final modrinthByEntry = <(Section, String), ModEntry>{}; final markerByEntry = <(Section, String), ModEntry>{}; @@ -96,23 +109,35 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { final markerSlugs = {for (final k in markerByEntry.keys) k.$2}; final allSlugs = {...modrinthSlugs, ...markerSlugs, ...nonModrinthSlugs}; - final api = read(modrinthApiProvider); + final apiFactory = read(modrinthApiFactoryProvider); // Recovery rewrites a constraint, so it's gated on --major-versions. final recovered = <(Section, String)>{}; if (majorVersions && !offline && markerByEntry.isNotEmpty) { for (final pair in markerByEntry.entries) { - final (_, slug) = pair.key; + final (section, slug) = pair.key; final entry = pair.value; final gameVersions = { manifest.mcVersion, ...entry.acceptsMc, }.toList(); + final api = apiFactory.forHost( + manifest.effectiveModrinthHost(entry, apiFactory.defaultBaseUrl), + ); List versions; try { + final filters = filterLoadersForSection( + manifest.loader, + section, + registry: registry, + host: manifest.effectiveModrinthHost( + entry, + apiFactory.defaultBaseUrl, + ), + ); versions = await api.listVersions( slug, - loadersJson: encodeFilterArray([manifest.loader.mods.name]), + loadersJson: filters == null ? null : encodeFilterArray(filters), gameVersionsJson: encodeFilterArray(gameVersions), ); } on DioException catch (e) { @@ -171,6 +196,7 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { targets, io.readModsLock(), read(cacheProvider), + defaultHost: apiFactory.defaultBaseUrl, console: console, verboseLabel: 'upgrade --unlock-transitive', ); @@ -193,7 +219,7 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { }; final manifestForResolve = unrecoverableMarkers.isEmpty ? null - : _stripSlugs(manifest, unrecoverableMarkers); + : manifest.withoutEntrySlugs(unrecoverableMarkers); final reporter = SolveReporter(console); @@ -331,16 +357,3 @@ class UpgradeCommand extends GitrinthCommand with OfflineFlag { return exitOk; } } - -ModsYaml _stripSlugs(ModsYaml manifest, Set slugs) { - Map strip(Map m) => { - for (final e in m.entries) - if (!slugs.contains(e.key)) e.key: e.value, - }; - return manifest.copyWith( - mods: strip(manifest.mods), - resourcePacks: strip(manifest.resourcePacks), - dataPacks: strip(manifest.dataPacks), - shaders: strip(manifest.shaders), - ); -} diff --git a/lib/src/commands/version_picker.dart b/lib/src/commands/version_picker.dart index 515ced0..3fa5424 100644 --- a/lib/src/commands/version_picker.dart +++ b/lib/src/commands/version_picker.dart @@ -4,7 +4,7 @@ import '../cli/exceptions.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/modrinth/version.dart' as modrinth; import '../model/resolver/constraint.dart'; -import '../service/modrinth_api.dart'; +import '../service/modrinth/modrinth_api.dart'; /// Picks the latest **release** version of [slug] satisfying the /// supplied loader/mc filter. Returns null when no eligible version @@ -17,8 +17,15 @@ Future pickLatestReleaseVersion({ required LoaderConfig loaderConfig, required String mcVersion, List acceptsMc = const [], + LoaderRegistry registry = LoaderRegistry.builtins, + String host = '*', }) async { - final loaderFilter = filterLoadersForSection(loaderConfig, section); + final loaderFilter = filterLoadersForSection( + loaderConfig, + section, + registry: registry, + host: host, + ); final gameVersions = {mcVersion, ...acceptsMc}.toList(); final List versions; try { @@ -59,7 +66,23 @@ Future pickLatestReleaseVersion({ /// Modrinth's fixed sentinel values. Single source of truth — callers /// route through this helper instead of switching on [Section] /// themselves. -List? filterLoadersForSection(LoaderConfig config, Section section) { +List? filterLoadersForSection( + LoaderConfig config, + Section section, { + LoaderRegistry registry = LoaderRegistry.builtins, + String host = '*', +}) { + String token(LoaderId id) { + final descriptor = registry[id]; + final tag = descriptor?.modrinthTag(host); + if (tag == null) { + throw ValidationError( + 'Loader ${id.name} has no Modrinth filter mapping for $host.', + ); + } + return tag; + } + switch (section) { case Section.mods: // No mod runtime — there is nothing to filter against. Modrinth @@ -67,17 +90,17 @@ List? filterLoadersForSection(LoaderConfig config, Section section) { // resolution for the mods section. The parser already rejects // populated `mods:` entries under vanilla, so this branch is // only ever hit by code that walks every section unconditionally. - return config.hasModRuntime ? [config.mods.name] : null; + return config.hasModRuntime ? [token(config.mods)] : null; case Section.shaders: final shaders = config.shaders; - return shaders == null ? null : [shaders.name]; + return shaders == null ? null : [token(shaders)]; case Section.resourcePacks: return const ['minecraft']; case Section.dataPacks: return const ['datapack']; case Section.plugins: final plugins = config.plugins; - return plugins == null ? null : [plugins.modrinthLoaderToken]; + return plugins == null ? null : [token(plugins)]; } } diff --git a/lib/src/model/curseforge/cf_constants.dart b/lib/src/model/curseforge/cf_constants.dart new file mode 100644 index 0000000..145ff6f --- /dev/null +++ b/lib/src/model/curseforge/cf_constants.dart @@ -0,0 +1,50 @@ +import '../manifest/mods_yaml.dart'; + +/// CurseForge `gameId` for Minecraft. +const int kCurseForgeGameIdMinecraft = 432; + +/// Maps a [Section] to its CurseForge `classId`. +/// +/// IDs come from `GET /v1/categories?gameId=432`. Confirm the values +/// against the live category list when the constants drift — CF's +/// taxonomy is occasionally renamed. +extension SectionCfMapping on Section { + int get cfClassId => switch (this) { + Section.mods => 6, + Section.resourcePacks => 12, + Section.dataPacks => 6945, + Section.shaders => 6552, + Section.plugins => 5, + }; +} + +/// Legacy convenience mapping for built-in loader identities. +/// Service consumers use the operation's registry for user/provider mappings. +extension ModLoaderCfMapping on ModLoader { + int get cfModLoaderType { + final type = LoaderRegistry.builtins[this]?.curseforgeType; + if (type == null) { + throw StateError('Loader $name has no CurseForge modLoaderType mapping.'); + } + return type; + } +} + +/// Returns the set of CurseForge `releaseType` values admitted by a +/// channel floor. Mirrors `allowedVersionTypes(Channel)` so the two +/// helpers stay in sync structurally. +/// +/// CurseForge releaseType: 1=release, 2=beta, 3=alpha. The floor is +/// inclusive — `Channel.beta` admits release+beta, `Channel.alpha` +/// admits everything. +Set cfReleaseTypesFor(Channel channel) => switch (channel) { + Channel.release => const {1}, + Channel.beta => const {1, 2}, + Channel.alpha => const {1, 2, 3}, +}; + +/// True when [loader] is a CurseForge-eligible plugin loader. Delegates +/// to [sectionAllowsCurseforge] so the source-eligibility rule has a +/// single source of truth. +bool pluginLoaderEligibleForCurseforge(PluginLoader loader) => + sectionAllowsCurseforge(Section.plugins, loader); diff --git a/lib/src/model/curseforge/cf_file_hash.dart b/lib/src/model/curseforge/cf_file_hash.dart new file mode 100644 index 0000000..b92a3f0 --- /dev/null +++ b/lib/src/model/curseforge/cf_file_hash.dart @@ -0,0 +1,28 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +part 'cf_file_hash.mapper.dart'; + +/// Algorithm used for a [FileHash]. CurseForge encodes the algorithm as +/// an integer code on the wire (1=sha1, 2=md5); [FileHash.algo] decodes +/// it. Unknown codes fall through to [HashAlgo.unknown] to keep parsing +/// forward-compatible. +@MappableEnum() +enum HashAlgo { sha1, md5, unknown } + +/// One entry in CurseForge's `ModFile.hashes` list. +@MappableClass() +class FileHash with FileHashMappable { + final String value; + + /// Raw `algo` integer from the API. Use [algo] for the decoded enum. + @MappableField(key: 'algo') + final int algoCode; + + const FileHash({required this.value, required this.algoCode}); + + HashAlgo get algo => switch (algoCode) { + 1 => HashAlgo.sha1, + 2 => HashAlgo.md5, + _ => HashAlgo.unknown, + }; +} diff --git a/lib/src/model/curseforge/cf_file_hash.mapper.dart b/lib/src/model/curseforge/cf_file_hash.mapper.dart new file mode 100644 index 0000000..6b0d8da --- /dev/null +++ b/lib/src/model/curseforge/cf_file_hash.mapper.dart @@ -0,0 +1,181 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// dart format off +// ignore_for_file: type=lint +// ignore_for_file: invalid_use_of_protected_member +// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member +// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter + +part of 'cf_file_hash.dart'; + +class HashAlgoMapper extends EnumMapper { + HashAlgoMapper._(); + + static HashAlgoMapper? _instance; + static HashAlgoMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = HashAlgoMapper._()); + } + return _instance!; + } + + static HashAlgo fromValue(dynamic value) { + ensureInitialized(); + return MapperContainer.globals.fromValue(value); + } + + @override + HashAlgo decode(dynamic value) { + switch (value) { + case r'sha1': + return HashAlgo.sha1; + case r'md5': + return HashAlgo.md5; + case r'unknown': + return HashAlgo.unknown; + default: + throw MapperException.unknownEnumValue(value); + } + } + + @override + dynamic encode(HashAlgo self) { + switch (self) { + case HashAlgo.sha1: + return r'sha1'; + case HashAlgo.md5: + return r'md5'; + case HashAlgo.unknown: + return r'unknown'; + } + } +} + +extension HashAlgoMapperExtension on HashAlgo { + String toValue() { + HashAlgoMapper.ensureInitialized(); + return MapperContainer.globals.toValue(this) as String; + } +} + +class FileHashMapper extends ClassMapperBase { + FileHashMapper._(); + + static FileHashMapper? _instance; + static FileHashMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = FileHashMapper._()); + } + return _instance!; + } + + @override + final String id = 'FileHash'; + + static String _$value(FileHash v) => v.value; + static const Field _f$value = Field('value', _$value); + static int _$algoCode(FileHash v) => v.algoCode; + static const Field _f$algoCode = Field( + 'algoCode', + _$algoCode, + key: r'algo', + ); + + @override + final MappableFields fields = const { + #value: _f$value, + #algoCode: _f$algoCode, + }; + + static FileHash _instantiate(DecodingData data) { + return FileHash(value: data.dec(_f$value), algoCode: data.dec(_f$algoCode)); + } + + @override + final Function instantiate = _instantiate; + + static FileHash fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static FileHash fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin FileHashMappable { + String toJson() { + return FileHashMapper.ensureInitialized().encodeJson( + this as FileHash, + ); + } + + Map toMap() { + return FileHashMapper.ensureInitialized().encodeMap( + this as FileHash, + ); + } + + FileHashCopyWith get copyWith => + _FileHashCopyWithImpl( + this as FileHash, + $identity, + $identity, + ); + @override + String toString() { + return FileHashMapper.ensureInitialized().stringifyValue(this as FileHash); + } + + @override + bool operator ==(Object other) { + return FileHashMapper.ensureInitialized().equalsValue( + this as FileHash, + other, + ); + } + + @override + int get hashCode { + return FileHashMapper.ensureInitialized().hashValue(this as FileHash); + } +} + +extension FileHashValueCopy<$R, $Out> on ObjectCopyWith<$R, FileHash, $Out> { + FileHashCopyWith<$R, FileHash, $Out> get $asFileHash => + $base.as((v, t, t2) => _FileHashCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class FileHashCopyWith<$R, $In extends FileHash, $Out> + implements ClassCopyWith<$R, $In, $Out> { + $R call({String? value, int? algoCode}); + FileHashCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _FileHashCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, FileHash, $Out> + implements FileHashCopyWith<$R, FileHash, $Out> { + _FileHashCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + FileHashMapper.ensureInitialized(); + @override + $R call({String? value, int? algoCode}) => $apply( + FieldCopyWithData({ + if (value != null) #value: value, + if (algoCode != null) #algoCode: algoCode, + }), + ); + @override + FileHash $make(CopyWithData data) => FileHash( + value: data.get(#value, or: $value.value), + algoCode: data.get(#algoCode, or: $value.algoCode), + ); + + @override + FileHashCopyWith<$R2, FileHash, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _FileHashCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + diff --git a/lib/src/model/curseforge/cf_file_relation.dart b/lib/src/model/curseforge/cf_file_relation.dart new file mode 100644 index 0000000..80a16d4 --- /dev/null +++ b/lib/src/model/curseforge/cf_file_relation.dart @@ -0,0 +1,44 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +part 'cf_file_relation.mapper.dart'; + +/// CurseForge `FileRelationType`. Wire codes: +/// 1 = embeddedLibrary +/// 2 = optionalDependency +/// 3 = requiredDependency +/// 4 = tool +/// 5 = incompatible +/// 6 = include +@MappableEnum() +enum RelationType { + embeddedLibrary, + optionalDependency, + requiredDependency, + tool, + incompatible, + include, + unknown, +} + +/// One entry in CurseForge's `ModFile.dependencies` list. +@MappableClass() +class FileRelation with FileRelationMappable { + final int modId; + + /// Raw integer code from the API. Use [relationType] for the decoded + /// enum. + @MappableField(key: 'relationType') + final int relationTypeCode; + + const FileRelation({required this.modId, required this.relationTypeCode}); + + RelationType get relationType => switch (relationTypeCode) { + 1 => RelationType.embeddedLibrary, + 2 => RelationType.optionalDependency, + 3 => RelationType.requiredDependency, + 4 => RelationType.tool, + 5 => RelationType.incompatible, + 6 => RelationType.include, + _ => RelationType.unknown, + }; +} diff --git a/lib/src/model/curseforge/cf_file_relation.mapper.dart b/lib/src/model/curseforge/cf_file_relation.mapper.dart new file mode 100644 index 0000000..94d932f --- /dev/null +++ b/lib/src/model/curseforge/cf_file_relation.mapper.dart @@ -0,0 +1,205 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// dart format off +// ignore_for_file: type=lint +// ignore_for_file: invalid_use_of_protected_member +// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member +// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter + +part of 'cf_file_relation.dart'; + +class RelationTypeMapper extends EnumMapper { + RelationTypeMapper._(); + + static RelationTypeMapper? _instance; + static RelationTypeMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = RelationTypeMapper._()); + } + return _instance!; + } + + static RelationType fromValue(dynamic value) { + ensureInitialized(); + return MapperContainer.globals.fromValue(value); + } + + @override + RelationType decode(dynamic value) { + switch (value) { + case r'embeddedLibrary': + return RelationType.embeddedLibrary; + case r'optionalDependency': + return RelationType.optionalDependency; + case r'requiredDependency': + return RelationType.requiredDependency; + case r'tool': + return RelationType.tool; + case r'incompatible': + return RelationType.incompatible; + case r'include': + return RelationType.include; + case r'unknown': + return RelationType.unknown; + default: + throw MapperException.unknownEnumValue(value); + } + } + + @override + dynamic encode(RelationType self) { + switch (self) { + case RelationType.embeddedLibrary: + return r'embeddedLibrary'; + case RelationType.optionalDependency: + return r'optionalDependency'; + case RelationType.requiredDependency: + return r'requiredDependency'; + case RelationType.tool: + return r'tool'; + case RelationType.incompatible: + return r'incompatible'; + case RelationType.include: + return r'include'; + case RelationType.unknown: + return r'unknown'; + } + } +} + +extension RelationTypeMapperExtension on RelationType { + String toValue() { + RelationTypeMapper.ensureInitialized(); + return MapperContainer.globals.toValue(this) as String; + } +} + +class FileRelationMapper extends ClassMapperBase { + FileRelationMapper._(); + + static FileRelationMapper? _instance; + static FileRelationMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = FileRelationMapper._()); + } + return _instance!; + } + + @override + final String id = 'FileRelation'; + + static int _$modId(FileRelation v) => v.modId; + static const Field _f$modId = Field('modId', _$modId); + static int _$relationTypeCode(FileRelation v) => v.relationTypeCode; + static const Field _f$relationTypeCode = Field( + 'relationTypeCode', + _$relationTypeCode, + key: r'relationType', + ); + + @override + final MappableFields fields = const { + #modId: _f$modId, + #relationTypeCode: _f$relationTypeCode, + }; + + static FileRelation _instantiate(DecodingData data) { + return FileRelation( + modId: data.dec(_f$modId), + relationTypeCode: data.dec(_f$relationTypeCode), + ); + } + + @override + final Function instantiate = _instantiate; + + static FileRelation fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static FileRelation fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin FileRelationMappable { + String toJson() { + return FileRelationMapper.ensureInitialized().encodeJson( + this as FileRelation, + ); + } + + Map toMap() { + return FileRelationMapper.ensureInitialized().encodeMap( + this as FileRelation, + ); + } + + FileRelationCopyWith get copyWith => + _FileRelationCopyWithImpl( + this as FileRelation, + $identity, + $identity, + ); + @override + String toString() { + return FileRelationMapper.ensureInitialized().stringifyValue( + this as FileRelation, + ); + } + + @override + bool operator ==(Object other) { + return FileRelationMapper.ensureInitialized().equalsValue( + this as FileRelation, + other, + ); + } + + @override + int get hashCode { + return FileRelationMapper.ensureInitialized().hashValue( + this as FileRelation, + ); + } +} + +extension FileRelationValueCopy<$R, $Out> + on ObjectCopyWith<$R, FileRelation, $Out> { + FileRelationCopyWith<$R, FileRelation, $Out> get $asFileRelation => + $base.as((v, t, t2) => _FileRelationCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class FileRelationCopyWith<$R, $In extends FileRelation, $Out> + implements ClassCopyWith<$R, $In, $Out> { + $R call({int? modId, int? relationTypeCode}); + FileRelationCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _FileRelationCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, FileRelation, $Out> + implements FileRelationCopyWith<$R, FileRelation, $Out> { + _FileRelationCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + FileRelationMapper.ensureInitialized(); + @override + $R call({int? modId, int? relationTypeCode}) => $apply( + FieldCopyWithData({ + if (modId != null) #modId: modId, + if (relationTypeCode != null) #relationTypeCode: relationTypeCode, + }), + ); + @override + FileRelation $make(CopyWithData data) => FileRelation( + modId: data.get(#modId, or: $value.modId), + relationTypeCode: data.get(#relationTypeCode, or: $value.relationTypeCode), + ); + + @override + FileRelationCopyWith<$R2, FileRelation, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _FileRelationCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + diff --git a/lib/src/model/curseforge/cf_mod.dart b/lib/src/model/curseforge/cf_mod.dart new file mode 100644 index 0000000..9d7a6c6 --- /dev/null +++ b/lib/src/model/curseforge/cf_mod.dart @@ -0,0 +1,45 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +import 'cf_mod_file.dart'; + +part 'cf_mod.mapper.dart'; + +/// A CurseForge mod project, as returned by `/v1/mods/{id}` and +/// `/v1/mods/search`. +@MappableClass() +class Mod with ModMappable { + final int id; + final int gameId; + final String name; + final String slug; + final String? summary; + final List> authors; + final List> categories; + + /// CurseForge `classId` — content type (mods=6, plugins=5, + /// resourcePacks=12, etc.). Mapped from the manifest [Section] + /// taxonomy in `cf_constants.dart`. + final int? classId; + + final List latestFiles; + + /// Whether CurseForge permits programmatic distribution of this mod's + /// files. When `false`, [ModFile.downloadUrl] is `null` and Part 4 + /// raises a "manual download required" error rather than attempting + /// to fetch. Defaults to `true` for response shapes that omit the + /// field — older CF endpoints didn't include it. + final bool allowModDistribution; + + const Mod({ + required this.id, + required this.gameId, + required this.name, + required this.slug, + this.summary, + this.authors = const [], + this.categories = const [], + required this.classId, + this.latestFiles = const [], + this.allowModDistribution = true, + }); +} diff --git a/lib/src/model/curseforge/cf_mod.mapper.dart b/lib/src/model/curseforge/cf_mod.mapper.dart new file mode 100644 index 0000000..6ce4a88 --- /dev/null +++ b/lib/src/model/curseforge/cf_mod.mapper.dart @@ -0,0 +1,258 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// dart format off +// ignore_for_file: type=lint +// ignore_for_file: invalid_use_of_protected_member +// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member +// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter + +part of 'cf_mod.dart'; + +class ModMapper extends ClassMapperBase { + ModMapper._(); + + static ModMapper? _instance; + static ModMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModMapper._()); + ModFileMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'Mod'; + + static int _$id(Mod v) => v.id; + static const Field _f$id = Field('id', _$id); + static int _$gameId(Mod v) => v.gameId; + static const Field _f$gameId = Field('gameId', _$gameId); + static String _$name(Mod v) => v.name; + static const Field _f$name = Field('name', _$name); + static String _$slug(Mod v) => v.slug; + static const Field _f$slug = Field('slug', _$slug); + static String? _$summary(Mod v) => v.summary; + static const Field _f$summary = Field( + 'summary', + _$summary, + opt: true, + ); + static List> _$authors(Mod v) => v.authors; + static const Field>> _f$authors = Field( + 'authors', + _$authors, + opt: true, + def: const [], + ); + static List> _$categories(Mod v) => v.categories; + static const Field>> _f$categories = Field( + 'categories', + _$categories, + opt: true, + def: const [], + ); + static int? _$classId(Mod v) => v.classId; + static const Field _f$classId = Field('classId', _$classId); + static List _$latestFiles(Mod v) => v.latestFiles; + static const Field> _f$latestFiles = Field( + 'latestFiles', + _$latestFiles, + opt: true, + def: const [], + ); + static bool _$allowModDistribution(Mod v) => v.allowModDistribution; + static const Field _f$allowModDistribution = Field( + 'allowModDistribution', + _$allowModDistribution, + opt: true, + def: true, + ); + + @override + final MappableFields fields = const { + #id: _f$id, + #gameId: _f$gameId, + #name: _f$name, + #slug: _f$slug, + #summary: _f$summary, + #authors: _f$authors, + #categories: _f$categories, + #classId: _f$classId, + #latestFiles: _f$latestFiles, + #allowModDistribution: _f$allowModDistribution, + }; + + static Mod _instantiate(DecodingData data) { + return Mod( + id: data.dec(_f$id), + gameId: data.dec(_f$gameId), + name: data.dec(_f$name), + slug: data.dec(_f$slug), + summary: data.dec(_f$summary), + authors: data.dec(_f$authors), + categories: data.dec(_f$categories), + classId: data.dec(_f$classId), + latestFiles: data.dec(_f$latestFiles), + allowModDistribution: data.dec(_f$allowModDistribution), + ); + } + + @override + final Function instantiate = _instantiate; + + static Mod fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static Mod fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModMappable { + String toJson() { + return ModMapper.ensureInitialized().encodeJson(this as Mod); + } + + Map toMap() { + return ModMapper.ensureInitialized().encodeMap(this as Mod); + } + + ModCopyWith get copyWith => + _ModCopyWithImpl(this as Mod, $identity, $identity); + @override + String toString() { + return ModMapper.ensureInitialized().stringifyValue(this as Mod); + } + + @override + bool operator ==(Object other) { + return ModMapper.ensureInitialized().equalsValue(this as Mod, other); + } + + @override + int get hashCode { + return ModMapper.ensureInitialized().hashValue(this as Mod); + } +} + +extension ModValueCopy<$R, $Out> on ObjectCopyWith<$R, Mod, $Out> { + ModCopyWith<$R, Mod, $Out> get $asMod => + $base.as((v, t, t2) => _ModCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class ModCopyWith<$R, $In extends Mod, $Out> + implements ClassCopyWith<$R, $In, $Out> { + ListCopyWith< + $R, + Map, + ObjectCopyWith<$R, Map, Map> + > + get authors; + ListCopyWith< + $R, + Map, + ObjectCopyWith<$R, Map, Map> + > + get categories; + ListCopyWith<$R, ModFile, ModFileCopyWith<$R, ModFile, ModFile>> + get latestFiles; + $R call({ + int? id, + int? gameId, + String? name, + String? slug, + String? summary, + List>? authors, + List>? categories, + int? classId, + List? latestFiles, + bool? allowModDistribution, + }); + ModCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _ModCopyWithImpl<$R, $Out> extends ClassCopyWithBase<$R, Mod, $Out> + implements ModCopyWith<$R, Mod, $Out> { + _ModCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = ModMapper.ensureInitialized(); + @override + ListCopyWith< + $R, + Map, + ObjectCopyWith<$R, Map, Map> + > + get authors => ListCopyWith( + $value.authors, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(authors: v), + ); + @override + ListCopyWith< + $R, + Map, + ObjectCopyWith<$R, Map, Map> + > + get categories => ListCopyWith( + $value.categories, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(categories: v), + ); + @override + ListCopyWith<$R, ModFile, ModFileCopyWith<$R, ModFile, ModFile>> + get latestFiles => ListCopyWith( + $value.latestFiles, + (v, t) => v.copyWith.$chain(t), + (v) => call(latestFiles: v), + ); + @override + $R call({ + int? id, + int? gameId, + String? name, + String? slug, + Object? summary = $none, + List>? authors, + List>? categories, + Object? classId = $none, + List? latestFiles, + bool? allowModDistribution, + }) => $apply( + FieldCopyWithData({ + if (id != null) #id: id, + if (gameId != null) #gameId: gameId, + if (name != null) #name: name, + if (slug != null) #slug: slug, + if (summary != $none) #summary: summary, + if (authors != null) #authors: authors, + if (categories != null) #categories: categories, + if (classId != $none) #classId: classId, + if (latestFiles != null) #latestFiles: latestFiles, + if (allowModDistribution != null) + #allowModDistribution: allowModDistribution, + }), + ); + @override + Mod $make(CopyWithData data) => Mod( + id: data.get(#id, or: $value.id), + gameId: data.get(#gameId, or: $value.gameId), + name: data.get(#name, or: $value.name), + slug: data.get(#slug, or: $value.slug), + summary: data.get(#summary, or: $value.summary), + authors: data.get(#authors, or: $value.authors), + categories: data.get(#categories, or: $value.categories), + classId: data.get(#classId, or: $value.classId), + latestFiles: data.get(#latestFiles, or: $value.latestFiles), + allowModDistribution: data.get( + #allowModDistribution, + or: $value.allowModDistribution, + ), + ); + + @override + ModCopyWith<$R2, Mod, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t) => + _ModCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + diff --git a/lib/src/model/curseforge/cf_mod_file.dart b/lib/src/model/curseforge/cf_mod_file.dart new file mode 100644 index 0000000..688be05 --- /dev/null +++ b/lib/src/model/curseforge/cf_mod_file.dart @@ -0,0 +1,77 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +import 'cf_file_hash.dart'; +import 'cf_file_relation.dart'; + +part 'cf_mod_file.mapper.dart'; + +/// One file attached to a CurseForge mod project. +@MappableClass() +class ModFile with ModFileMappable { + final int id; + final int modId; + final String displayName; + final String fileName; + + /// CurseForge release type. 1=release, 2=beta, 3=alpha. Filtered + /// against `cfReleaseTypesFor(channel)` in the resolver. + final int releaseType; + + /// Explicitly unavailable files cannot be selected. Older responses that + /// omit this field retain their prior available behavior. + final bool isAvailable; + + /// ISO-8601 publish date string. Stored as-is so callers can sort + /// lexicographically (ISO-8601 sorts equivalently to chronological + /// order). + final String fileDate; + + /// Loose tags CurseForge attaches to the file — Minecraft versions + /// (e.g. `"1.21.1"`) and loader markers (`"Forge"`, `"Fabric"`). + /// Filtering happens client-side because the API's `gameVersion` + /// query parameter only accepts a single value. + final List gameVersions; + + final List hashes; + + /// Dependency relations declared by the file. Mapped from the JSON + /// `dependencies` array. + final List dependencies; + + /// CDN download URL. `null` when the project's author has set + /// `allowModDistribution: false` on CurseForge — Part 4 surfaces a + /// "manual download required" error in that case rather than + /// attempting to fetch. + final String? downloadUrl; + + const ModFile({ + required this.id, + required this.modId, + required this.displayName, + required this.fileName, + required this.releaseType, + this.isAvailable = true, + required this.fileDate, + required this.gameVersions, + required this.hashes, + required this.dependencies, + this.downloadUrl, + }); + + /// First sha1 hash on the file, lowercased. `null` when the file + /// reports no sha1 entry (CF historically also includes md5; sha1 is + /// the cross-platform anchor for hash verification). + String? get sha1Hash { + for (final h in hashes) { + if (h.algo == HashAlgo.sha1) return h.value.toLowerCase(); + } + return null; + } + + /// Subset of [dependencies] whose relation type is + /// [RelationType.requiredDependency]. Drives transitive-dep + /// resolution in Part 6. + Iterable get requiredDependencies => dependencies.where( + (d) => d.relationType == RelationType.requiredDependency, + ); +} diff --git a/lib/src/model/curseforge/cf_mod_file.mapper.dart b/lib/src/model/curseforge/cf_mod_file.mapper.dart new file mode 100644 index 0000000..3a2b72f --- /dev/null +++ b/lib/src/model/curseforge/cf_mod_file.mapper.dart @@ -0,0 +1,273 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// dart format off +// ignore_for_file: type=lint +// ignore_for_file: invalid_use_of_protected_member +// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member +// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter + +part of 'cf_mod_file.dart'; + +class ModFileMapper extends ClassMapperBase { + ModFileMapper._(); + + static ModFileMapper? _instance; + static ModFileMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModFileMapper._()); + FileHashMapper.ensureInitialized(); + FileRelationMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'ModFile'; + + static int _$id(ModFile v) => v.id; + static const Field _f$id = Field('id', _$id); + static int _$modId(ModFile v) => v.modId; + static const Field _f$modId = Field('modId', _$modId); + static String _$displayName(ModFile v) => v.displayName; + static const Field _f$displayName = Field( + 'displayName', + _$displayName, + ); + static String _$fileName(ModFile v) => v.fileName; + static const Field _f$fileName = Field( + 'fileName', + _$fileName, + ); + static int _$releaseType(ModFile v) => v.releaseType; + static const Field _f$releaseType = Field( + 'releaseType', + _$releaseType, + ); + static bool _$isAvailable(ModFile v) => v.isAvailable; + static const Field _f$isAvailable = Field( + 'isAvailable', + _$isAvailable, + opt: true, + def: true, + ); + static String _$fileDate(ModFile v) => v.fileDate; + static const Field _f$fileDate = Field( + 'fileDate', + _$fileDate, + ); + static List _$gameVersions(ModFile v) => v.gameVersions; + static const Field> _f$gameVersions = Field( + 'gameVersions', + _$gameVersions, + ); + static List _$hashes(ModFile v) => v.hashes; + static const Field> _f$hashes = Field( + 'hashes', + _$hashes, + ); + static List _$dependencies(ModFile v) => v.dependencies; + static const Field> _f$dependencies = Field( + 'dependencies', + _$dependencies, + ); + static String? _$downloadUrl(ModFile v) => v.downloadUrl; + static const Field _f$downloadUrl = Field( + 'downloadUrl', + _$downloadUrl, + opt: true, + ); + + @override + final MappableFields fields = const { + #id: _f$id, + #modId: _f$modId, + #displayName: _f$displayName, + #fileName: _f$fileName, + #releaseType: _f$releaseType, + #isAvailable: _f$isAvailable, + #fileDate: _f$fileDate, + #gameVersions: _f$gameVersions, + #hashes: _f$hashes, + #dependencies: _f$dependencies, + #downloadUrl: _f$downloadUrl, + }; + + static ModFile _instantiate(DecodingData data) { + return ModFile( + id: data.dec(_f$id), + modId: data.dec(_f$modId), + displayName: data.dec(_f$displayName), + fileName: data.dec(_f$fileName), + releaseType: data.dec(_f$releaseType), + isAvailable: data.dec(_f$isAvailable), + fileDate: data.dec(_f$fileDate), + gameVersions: data.dec(_f$gameVersions), + hashes: data.dec(_f$hashes), + dependencies: data.dec(_f$dependencies), + downloadUrl: data.dec(_f$downloadUrl), + ); + } + + @override + final Function instantiate = _instantiate; + + static ModFile fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static ModFile fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModFileMappable { + String toJson() { + return ModFileMapper.ensureInitialized().encodeJson( + this as ModFile, + ); + } + + Map toMap() { + return ModFileMapper.ensureInitialized().encodeMap( + this as ModFile, + ); + } + + ModFileCopyWith get copyWith => + _ModFileCopyWithImpl( + this as ModFile, + $identity, + $identity, + ); + @override + String toString() { + return ModFileMapper.ensureInitialized().stringifyValue(this as ModFile); + } + + @override + bool operator ==(Object other) { + return ModFileMapper.ensureInitialized().equalsValue( + this as ModFile, + other, + ); + } + + @override + int get hashCode { + return ModFileMapper.ensureInitialized().hashValue(this as ModFile); + } +} + +extension ModFileValueCopy<$R, $Out> on ObjectCopyWith<$R, ModFile, $Out> { + ModFileCopyWith<$R, ModFile, $Out> get $asModFile => + $base.as((v, t, t2) => _ModFileCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class ModFileCopyWith<$R, $In extends ModFile, $Out> + implements ClassCopyWith<$R, $In, $Out> { + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get gameVersions; + ListCopyWith<$R, FileHash, FileHashCopyWith<$R, FileHash, FileHash>> + get hashes; + ListCopyWith< + $R, + FileRelation, + FileRelationCopyWith<$R, FileRelation, FileRelation> + > + get dependencies; + $R call({ + int? id, + int? modId, + String? displayName, + String? fileName, + int? releaseType, + bool? isAvailable, + String? fileDate, + List? gameVersions, + List? hashes, + List? dependencies, + String? downloadUrl, + }); + ModFileCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _ModFileCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, ModFile, $Out> + implements ModFileCopyWith<$R, ModFile, $Out> { + _ModFileCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + ModFileMapper.ensureInitialized(); + @override + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> + get gameVersions => ListCopyWith( + $value.gameVersions, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(gameVersions: v), + ); + @override + ListCopyWith<$R, FileHash, FileHashCopyWith<$R, FileHash, FileHash>> + get hashes => ListCopyWith( + $value.hashes, + (v, t) => v.copyWith.$chain(t), + (v) => call(hashes: v), + ); + @override + ListCopyWith< + $R, + FileRelation, + FileRelationCopyWith<$R, FileRelation, FileRelation> + > + get dependencies => ListCopyWith( + $value.dependencies, + (v, t) => v.copyWith.$chain(t), + (v) => call(dependencies: v), + ); + @override + $R call({ + int? id, + int? modId, + String? displayName, + String? fileName, + int? releaseType, + bool? isAvailable, + String? fileDate, + List? gameVersions, + List? hashes, + List? dependencies, + Object? downloadUrl = $none, + }) => $apply( + FieldCopyWithData({ + if (id != null) #id: id, + if (modId != null) #modId: modId, + if (displayName != null) #displayName: displayName, + if (fileName != null) #fileName: fileName, + if (releaseType != null) #releaseType: releaseType, + if (isAvailable != null) #isAvailable: isAvailable, + if (fileDate != null) #fileDate: fileDate, + if (gameVersions != null) #gameVersions: gameVersions, + if (hashes != null) #hashes: hashes, + if (dependencies != null) #dependencies: dependencies, + if (downloadUrl != $none) #downloadUrl: downloadUrl, + }), + ); + @override + ModFile $make(CopyWithData data) => ModFile( + id: data.get(#id, or: $value.id), + modId: data.get(#modId, or: $value.modId), + displayName: data.get(#displayName, or: $value.displayName), + fileName: data.get(#fileName, or: $value.fileName), + releaseType: data.get(#releaseType, or: $value.releaseType), + isAvailable: data.get(#isAvailable, or: $value.isAvailable), + fileDate: data.get(#fileDate, or: $value.fileDate), + gameVersions: data.get(#gameVersions, or: $value.gameVersions), + hashes: data.get(#hashes, or: $value.hashes), + dependencies: data.get(#dependencies, or: $value.dependencies), + downloadUrl: data.get(#downloadUrl, or: $value.downloadUrl), + ); + + @override + ModFileCopyWith<$R2, ModFile, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t) => + _ModFileCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + diff --git a/lib/src/model/curseforge/cf_search_response.dart b/lib/src/model/curseforge/cf_search_response.dart new file mode 100644 index 0000000..7557bb4 --- /dev/null +++ b/lib/src/model/curseforge/cf_search_response.dart @@ -0,0 +1,57 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +import 'cf_mod.dart'; +import 'cf_mod_file.dart'; + +part 'cf_search_response.mapper.dart'; + +/// Pagination block returned alongside CurseForge list endpoints. +@MappableClass() +class Pagination with PaginationMappable { + final int index; + final int pageSize; + final int resultCount; + final int totalCount; + + const Pagination({ + required this.index, + required this.pageSize, + required this.resultCount, + required this.totalCount, + }); +} + +/// `{ "data": }` — CurseForge wraps every single-resource +/// response in a `data` envelope. Concrete (non-generic) wrappers +/// avoid dart_mappable / retrofit generics complications. +@MappableClass() +class ModEnvelope with ModEnvelopeMappable { + final Mod data; + const ModEnvelope({required this.data}); +} + +/// `{ "data": }`. +@MappableClass() +class ModFileEnvelope with ModFileEnvelopeMappable { + final ModFile data; + const ModFileEnvelope({required this.data}); +} + +/// `{ "data": [...], "pagination": ... }` — search-by-slug and +/// search-by-query endpoints. +@MappableClass() +class ModSearchResponse with ModSearchResponseMappable { + final List data; + final Pagination pagination; + + const ModSearchResponse({required this.data, required this.pagination}); +} + +/// `{ "data": [...], "pagination": ... }` — `/v1/mods/{id}/files`. +@MappableClass() +class ModFileSearchResponse with ModFileSearchResponseMappable { + final List data; + final Pagination pagination; + + const ModFileSearchResponse({required this.data, required this.pagination}); +} diff --git a/lib/src/model/curseforge/cf_search_response.mapper.dart b/lib/src/model/curseforge/cf_search_response.mapper.dart new file mode 100644 index 0000000..77a52f6 --- /dev/null +++ b/lib/src/model/curseforge/cf_search_response.mapper.dart @@ -0,0 +1,698 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// dart format off +// ignore_for_file: type=lint +// ignore_for_file: invalid_use_of_protected_member +// ignore_for_file: unused_element, unnecessary_cast, override_on_non_overriding_member +// ignore_for_file: strict_raw_type, inference_failure_on_untyped_parameter + +part of 'cf_search_response.dart'; + +class PaginationMapper extends ClassMapperBase { + PaginationMapper._(); + + static PaginationMapper? _instance; + static PaginationMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = PaginationMapper._()); + } + return _instance!; + } + + @override + final String id = 'Pagination'; + + static int _$index(Pagination v) => v.index; + static const Field _f$index = Field('index', _$index); + static int _$pageSize(Pagination v) => v.pageSize; + static const Field _f$pageSize = Field( + 'pageSize', + _$pageSize, + ); + static int _$resultCount(Pagination v) => v.resultCount; + static const Field _f$resultCount = Field( + 'resultCount', + _$resultCount, + ); + static int _$totalCount(Pagination v) => v.totalCount; + static const Field _f$totalCount = Field( + 'totalCount', + _$totalCount, + ); + + @override + final MappableFields fields = const { + #index: _f$index, + #pageSize: _f$pageSize, + #resultCount: _f$resultCount, + #totalCount: _f$totalCount, + }; + + static Pagination _instantiate(DecodingData data) { + return Pagination( + index: data.dec(_f$index), + pageSize: data.dec(_f$pageSize), + resultCount: data.dec(_f$resultCount), + totalCount: data.dec(_f$totalCount), + ); + } + + @override + final Function instantiate = _instantiate; + + static Pagination fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static Pagination fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin PaginationMappable { + String toJson() { + return PaginationMapper.ensureInitialized().encodeJson( + this as Pagination, + ); + } + + Map toMap() { + return PaginationMapper.ensureInitialized().encodeMap( + this as Pagination, + ); + } + + PaginationCopyWith get copyWith => + _PaginationCopyWithImpl( + this as Pagination, + $identity, + $identity, + ); + @override + String toString() { + return PaginationMapper.ensureInitialized().stringifyValue( + this as Pagination, + ); + } + + @override + bool operator ==(Object other) { + return PaginationMapper.ensureInitialized().equalsValue( + this as Pagination, + other, + ); + } + + @override + int get hashCode { + return PaginationMapper.ensureInitialized().hashValue(this as Pagination); + } +} + +extension PaginationValueCopy<$R, $Out> + on ObjectCopyWith<$R, Pagination, $Out> { + PaginationCopyWith<$R, Pagination, $Out> get $asPagination => + $base.as((v, t, t2) => _PaginationCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class PaginationCopyWith<$R, $In extends Pagination, $Out> + implements ClassCopyWith<$R, $In, $Out> { + $R call({int? index, int? pageSize, int? resultCount, int? totalCount}); + PaginationCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _PaginationCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, Pagination, $Out> + implements PaginationCopyWith<$R, Pagination, $Out> { + _PaginationCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + PaginationMapper.ensureInitialized(); + @override + $R call({int? index, int? pageSize, int? resultCount, int? totalCount}) => + $apply( + FieldCopyWithData({ + if (index != null) #index: index, + if (pageSize != null) #pageSize: pageSize, + if (resultCount != null) #resultCount: resultCount, + if (totalCount != null) #totalCount: totalCount, + }), + ); + @override + Pagination $make(CopyWithData data) => Pagination( + index: data.get(#index, or: $value.index), + pageSize: data.get(#pageSize, or: $value.pageSize), + resultCount: data.get(#resultCount, or: $value.resultCount), + totalCount: data.get(#totalCount, or: $value.totalCount), + ); + + @override + PaginationCopyWith<$R2, Pagination, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _PaginationCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + +class ModEnvelopeMapper extends ClassMapperBase { + ModEnvelopeMapper._(); + + static ModEnvelopeMapper? _instance; + static ModEnvelopeMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModEnvelopeMapper._()); + ModMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'ModEnvelope'; + + static Mod _$data(ModEnvelope v) => v.data; + static const Field _f$data = Field('data', _$data); + + @override + final MappableFields fields = const {#data: _f$data}; + + static ModEnvelope _instantiate(DecodingData data) { + return ModEnvelope(data: data.dec(_f$data)); + } + + @override + final Function instantiate = _instantiate; + + static ModEnvelope fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static ModEnvelope fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModEnvelopeMappable { + String toJson() { + return ModEnvelopeMapper.ensureInitialized().encodeJson( + this as ModEnvelope, + ); + } + + Map toMap() { + return ModEnvelopeMapper.ensureInitialized().encodeMap( + this as ModEnvelope, + ); + } + + ModEnvelopeCopyWith get copyWith => + _ModEnvelopeCopyWithImpl( + this as ModEnvelope, + $identity, + $identity, + ); + @override + String toString() { + return ModEnvelopeMapper.ensureInitialized().stringifyValue( + this as ModEnvelope, + ); + } + + @override + bool operator ==(Object other) { + return ModEnvelopeMapper.ensureInitialized().equalsValue( + this as ModEnvelope, + other, + ); + } + + @override + int get hashCode { + return ModEnvelopeMapper.ensureInitialized().hashValue(this as ModEnvelope); + } +} + +extension ModEnvelopeValueCopy<$R, $Out> + on ObjectCopyWith<$R, ModEnvelope, $Out> { + ModEnvelopeCopyWith<$R, ModEnvelope, $Out> get $asModEnvelope => + $base.as((v, t, t2) => _ModEnvelopeCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class ModEnvelopeCopyWith<$R, $In extends ModEnvelope, $Out> + implements ClassCopyWith<$R, $In, $Out> { + ModCopyWith<$R, Mod, Mod> get data; + $R call({Mod? data}); + ModEnvelopeCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); +} + +class _ModEnvelopeCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, ModEnvelope, $Out> + implements ModEnvelopeCopyWith<$R, ModEnvelope, $Out> { + _ModEnvelopeCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + ModEnvelopeMapper.ensureInitialized(); + @override + ModCopyWith<$R, Mod, Mod> get data => + $value.data.copyWith.$chain((v) => call(data: v)); + @override + $R call({Mod? data}) => + $apply(FieldCopyWithData({if (data != null) #data: data})); + @override + ModEnvelope $make(CopyWithData data) => + ModEnvelope(data: data.get(#data, or: $value.data)); + + @override + ModEnvelopeCopyWith<$R2, ModEnvelope, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _ModEnvelopeCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + +class ModFileEnvelopeMapper extends ClassMapperBase { + ModFileEnvelopeMapper._(); + + static ModFileEnvelopeMapper? _instance; + static ModFileEnvelopeMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModFileEnvelopeMapper._()); + ModFileMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'ModFileEnvelope'; + + static ModFile _$data(ModFileEnvelope v) => v.data; + static const Field _f$data = Field('data', _$data); + + @override + final MappableFields fields = const {#data: _f$data}; + + static ModFileEnvelope _instantiate(DecodingData data) { + return ModFileEnvelope(data: data.dec(_f$data)); + } + + @override + final Function instantiate = _instantiate; + + static ModFileEnvelope fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static ModFileEnvelope fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModFileEnvelopeMappable { + String toJson() { + return ModFileEnvelopeMapper.ensureInitialized() + .encodeJson(this as ModFileEnvelope); + } + + Map toMap() { + return ModFileEnvelopeMapper.ensureInitialized().encodeMap( + this as ModFileEnvelope, + ); + } + + ModFileEnvelopeCopyWith + get copyWith => + _ModFileEnvelopeCopyWithImpl( + this as ModFileEnvelope, + $identity, + $identity, + ); + @override + String toString() { + return ModFileEnvelopeMapper.ensureInitialized().stringifyValue( + this as ModFileEnvelope, + ); + } + + @override + bool operator ==(Object other) { + return ModFileEnvelopeMapper.ensureInitialized().equalsValue( + this as ModFileEnvelope, + other, + ); + } + + @override + int get hashCode { + return ModFileEnvelopeMapper.ensureInitialized().hashValue( + this as ModFileEnvelope, + ); + } +} + +extension ModFileEnvelopeValueCopy<$R, $Out> + on ObjectCopyWith<$R, ModFileEnvelope, $Out> { + ModFileEnvelopeCopyWith<$R, ModFileEnvelope, $Out> get $asModFileEnvelope => + $base.as((v, t, t2) => _ModFileEnvelopeCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class ModFileEnvelopeCopyWith<$R, $In extends ModFileEnvelope, $Out> + implements ClassCopyWith<$R, $In, $Out> { + ModFileCopyWith<$R, ModFile, ModFile> get data; + $R call({ModFile? data}); + ModFileEnvelopeCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _ModFileEnvelopeCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, ModFileEnvelope, $Out> + implements ModFileEnvelopeCopyWith<$R, ModFileEnvelope, $Out> { + _ModFileEnvelopeCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + ModFileEnvelopeMapper.ensureInitialized(); + @override + ModFileCopyWith<$R, ModFile, ModFile> get data => + $value.data.copyWith.$chain((v) => call(data: v)); + @override + $R call({ModFile? data}) => + $apply(FieldCopyWithData({if (data != null) #data: data})); + @override + ModFileEnvelope $make(CopyWithData data) => + ModFileEnvelope(data: data.get(#data, or: $value.data)); + + @override + ModFileEnvelopeCopyWith<$R2, ModFileEnvelope, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _ModFileEnvelopeCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + +class ModSearchResponseMapper extends ClassMapperBase { + ModSearchResponseMapper._(); + + static ModSearchResponseMapper? _instance; + static ModSearchResponseMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModSearchResponseMapper._()); + ModMapper.ensureInitialized(); + PaginationMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'ModSearchResponse'; + + static List _$data(ModSearchResponse v) => v.data; + static const Field> _f$data = Field( + 'data', + _$data, + ); + static Pagination _$pagination(ModSearchResponse v) => v.pagination; + static const Field _f$pagination = Field( + 'pagination', + _$pagination, + ); + + @override + final MappableFields fields = const { + #data: _f$data, + #pagination: _f$pagination, + }; + + static ModSearchResponse _instantiate(DecodingData data) { + return ModSearchResponse( + data: data.dec(_f$data), + pagination: data.dec(_f$pagination), + ); + } + + @override + final Function instantiate = _instantiate; + + static ModSearchResponse fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static ModSearchResponse fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModSearchResponseMappable { + String toJson() { + return ModSearchResponseMapper.ensureInitialized() + .encodeJson(this as ModSearchResponse); + } + + Map toMap() { + return ModSearchResponseMapper.ensureInitialized() + .encodeMap(this as ModSearchResponse); + } + + ModSearchResponseCopyWith< + ModSearchResponse, + ModSearchResponse, + ModSearchResponse + > + get copyWith => + _ModSearchResponseCopyWithImpl( + this as ModSearchResponse, + $identity, + $identity, + ); + @override + String toString() { + return ModSearchResponseMapper.ensureInitialized().stringifyValue( + this as ModSearchResponse, + ); + } + + @override + bool operator ==(Object other) { + return ModSearchResponseMapper.ensureInitialized().equalsValue( + this as ModSearchResponse, + other, + ); + } + + @override + int get hashCode { + return ModSearchResponseMapper.ensureInitialized().hashValue( + this as ModSearchResponse, + ); + } +} + +extension ModSearchResponseValueCopy<$R, $Out> + on ObjectCopyWith<$R, ModSearchResponse, $Out> { + ModSearchResponseCopyWith<$R, ModSearchResponse, $Out> + get $asModSearchResponse => $base.as( + (v, t, t2) => _ModSearchResponseCopyWithImpl<$R, $Out>(v, t, t2), + ); +} + +abstract class ModSearchResponseCopyWith< + $R, + $In extends ModSearchResponse, + $Out +> + implements ClassCopyWith<$R, $In, $Out> { + ListCopyWith<$R, Mod, ModCopyWith<$R, Mod, Mod>> get data; + PaginationCopyWith<$R, Pagination, Pagination> get pagination; + $R call({List? data, Pagination? pagination}); + ModSearchResponseCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _ModSearchResponseCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, ModSearchResponse, $Out> + implements ModSearchResponseCopyWith<$R, ModSearchResponse, $Out> { + _ModSearchResponseCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + ModSearchResponseMapper.ensureInitialized(); + @override + ListCopyWith<$R, Mod, ModCopyWith<$R, Mod, Mod>> get data => ListCopyWith( + $value.data, + (v, t) => v.copyWith.$chain(t), + (v) => call(data: v), + ); + @override + PaginationCopyWith<$R, Pagination, Pagination> get pagination => + $value.pagination.copyWith.$chain((v) => call(pagination: v)); + @override + $R call({List? data, Pagination? pagination}) => $apply( + FieldCopyWithData({ + if (data != null) #data: data, + if (pagination != null) #pagination: pagination, + }), + ); + @override + ModSearchResponse $make(CopyWithData data) => ModSearchResponse( + data: data.get(#data, or: $value.data), + pagination: data.get(#pagination, or: $value.pagination), + ); + + @override + ModSearchResponseCopyWith<$R2, ModSearchResponse, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _ModSearchResponseCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + +class ModFileSearchResponseMapper + extends ClassMapperBase { + ModFileSearchResponseMapper._(); + + static ModFileSearchResponseMapper? _instance; + static ModFileSearchResponseMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = ModFileSearchResponseMapper._()); + ModFileMapper.ensureInitialized(); + PaginationMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'ModFileSearchResponse'; + + static List _$data(ModFileSearchResponse v) => v.data; + static const Field> _f$data = Field( + 'data', + _$data, + ); + static Pagination _$pagination(ModFileSearchResponse v) => v.pagination; + static const Field _f$pagination = Field( + 'pagination', + _$pagination, + ); + + @override + final MappableFields fields = const { + #data: _f$data, + #pagination: _f$pagination, + }; + + static ModFileSearchResponse _instantiate(DecodingData data) { + return ModFileSearchResponse( + data: data.dec(_f$data), + pagination: data.dec(_f$pagination), + ); + } + + @override + final Function instantiate = _instantiate; + + static ModFileSearchResponse fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static ModFileSearchResponse fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin ModFileSearchResponseMappable { + String toJson() { + return ModFileSearchResponseMapper.ensureInitialized() + .encodeJson(this as ModFileSearchResponse); + } + + Map toMap() { + return ModFileSearchResponseMapper.ensureInitialized() + .encodeMap(this as ModFileSearchResponse); + } + + ModFileSearchResponseCopyWith< + ModFileSearchResponse, + ModFileSearchResponse, + ModFileSearchResponse + > + get copyWith => + _ModFileSearchResponseCopyWithImpl< + ModFileSearchResponse, + ModFileSearchResponse + >(this as ModFileSearchResponse, $identity, $identity); + @override + String toString() { + return ModFileSearchResponseMapper.ensureInitialized().stringifyValue( + this as ModFileSearchResponse, + ); + } + + @override + bool operator ==(Object other) { + return ModFileSearchResponseMapper.ensureInitialized().equalsValue( + this as ModFileSearchResponse, + other, + ); + } + + @override + int get hashCode { + return ModFileSearchResponseMapper.ensureInitialized().hashValue( + this as ModFileSearchResponse, + ); + } +} + +extension ModFileSearchResponseValueCopy<$R, $Out> + on ObjectCopyWith<$R, ModFileSearchResponse, $Out> { + ModFileSearchResponseCopyWith<$R, ModFileSearchResponse, $Out> + get $asModFileSearchResponse => $base.as( + (v, t, t2) => _ModFileSearchResponseCopyWithImpl<$R, $Out>(v, t, t2), + ); +} + +abstract class ModFileSearchResponseCopyWith< + $R, + $In extends ModFileSearchResponse, + $Out +> + implements ClassCopyWith<$R, $In, $Out> { + ListCopyWith<$R, ModFile, ModFileCopyWith<$R, ModFile, ModFile>> get data; + PaginationCopyWith<$R, Pagination, Pagination> get pagination; + $R call({List? data, Pagination? pagination}); + ModFileSearchResponseCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _ModFileSearchResponseCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, ModFileSearchResponse, $Out> + implements ModFileSearchResponseCopyWith<$R, ModFileSearchResponse, $Out> { + _ModFileSearchResponseCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + ModFileSearchResponseMapper.ensureInitialized(); + @override + ListCopyWith<$R, ModFile, ModFileCopyWith<$R, ModFile, ModFile>> get data => + ListCopyWith( + $value.data, + (v, t) => v.copyWith.$chain(t), + (v) => call(data: v), + ); + @override + PaginationCopyWith<$R, Pagination, Pagination> get pagination => + $value.pagination.copyWith.$chain((v) => call(pagination: v)); + @override + $R call({List? data, Pagination? pagination}) => $apply( + FieldCopyWithData({ + if (data != null) #data: data, + if (pagination != null) #pagination: pagination, + }), + ); + @override + ModFileSearchResponse $make(CopyWithData data) => ModFileSearchResponse( + data: data.get(#data, or: $value.data), + pagination: data.get(#pagination, or: $value.pagination), + ); + + @override + ModFileSearchResponseCopyWith<$R2, ModFileSearchResponse, $Out2> + $chain<$R2, $Out2>(Then<$Out2, $R2> t) => + _ModFileSearchResponseCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + diff --git a/lib/src/model/loader/loader_descriptor.dart b/lib/src/model/loader/loader_descriptor.dart new file mode 100644 index 0000000..3d3b881 --- /dev/null +++ b/lib/src/model/loader/loader_descriptor.dart @@ -0,0 +1,337 @@ +import 'loader_id.dart'; + +enum LoaderRole { mod, plugin, shader, vanilla } + +enum InstallerFamily { + fabric, + forge, + quilt, + paper, + sponge, + buildtools, + vanilla, +} + +enum VersionSourceFamily { + fabric, + forge, + neoforge, + quilt, + paper, + sponge, + buildtools, + catalog, +} + +/// A recipe version is a path segment and a positional process argument. +bool isSafeRuntimeToken(String value) => + RegExp(r'^[A-Za-z0-9][A-Za-z0-9._+\-]*$').hasMatch(value) && value != '..'; + +/// Provider release applicability, retained independently of its display name. +class LoaderCatalogVersion { + final String name; + final String? gameVersion; + final bool latest; + final bool recommended; + const LoaderCatalogVersion({ + required this.name, + this.gameVersion, + this.latest = false, + this.recommended = false, + }); +} + +/// Immutable metadata. Discovery never grants an installer family. +class LoaderDescriptor { + final LoaderId id; + final LoaderRole role; + final Map modrinthTags; + final int? curseforgeType; + final bool curseforgePlugins; + final String? modrinthArchiveKey; + final String? curseforgeArchiveToken; + final VersionSourceFamily? versionSource; + final InstallerFamily? installer; + final String? serverLaunchJar; + final bool serverRunScript; + final List compatibleMods; + final LoaderId? declaredId; + final Map variants; + final List versions; + final List? catalogVersions; + final String? runtimeArtifact; + + const LoaderDescriptor._({ + required this.id, + required this.role, + this.modrinthTags = const {}, + this.curseforgeType, + this.curseforgePlugins = false, + this.modrinthArchiveKey, + this.curseforgeArchiveToken, + this.versionSource, + this.installer, + this.serverLaunchJar, + this.serverRunScript = false, + this.compatibleMods = const [], + this.declaredId, + this.variants = const {}, + this.versions = const [], + this.catalogVersions, + this.runtimeArtifact, + }); + + factory LoaderDescriptor({ + required LoaderId id, + required LoaderRole role, + Map modrinthTags = const {}, + int? curseforgeType, + bool curseforgePlugins = false, + String? modrinthArchiveKey, + String? curseforgeArchiveToken, + VersionSourceFamily? versionSource, + InstallerFamily? installer, + String? serverLaunchJar, + bool serverRunScript = false, + List compatibleMods = const [], + LoaderId? declaredId, + Map variants = const {}, + List versions = const [], + List? catalogVersions, + String? runtimeArtifact, + }) => LoaderDescriptor._( + id: id, + role: role, + modrinthTags: Map.unmodifiable(modrinthTags), + curseforgeType: curseforgeType, + curseforgePlugins: curseforgePlugins, + modrinthArchiveKey: modrinthArchiveKey, + curseforgeArchiveToken: curseforgeArchiveToken, + versionSource: versionSource, + installer: installer, + serverLaunchJar: serverLaunchJar, + serverRunScript: serverRunScript, + compatibleMods: List.unmodifiable(compatibleMods), + declaredId: declaredId, + variants: Map.unmodifiable(variants), + versions: List.unmodifiable(versions), + catalogVersions: catalogVersions == null + ? null + : List.unmodifiable(catalogVersions), + runtimeArtifact: runtimeArtifact, + ); + + String? modrinthTag(String host) => modrinthTags[host] ?? modrinthTags['*']; + bool get runsServerMods => compatibleMods.isNotEmpty; + bool get canInstallClient => + role == LoaderRole.mod && + const { + InstallerFamily.fabric, + InstallerFamily.forge, + InstallerFamily.quilt, + }.contains(installer); + bool get canInstallServer => switch (role) { + LoaderRole.vanilla => installer == InstallerFamily.vanilla, + LoaderRole.mod => switch (installer) { + InstallerFamily.fabric => versionSource == VersionSourceFamily.fabric, + InstallerFamily.forge => const { + VersionSourceFamily.forge, + VersionSourceFamily.neoforge, + }.contains(versionSource), + InstallerFamily.quilt => true, + _ => false, + }, + LoaderRole.plugin => const { + InstallerFamily.paper, + InstallerFamily.sponge, + InstallerFamily.buildtools, + }.contains(installer), + LoaderRole.shader => false, + }; + + LoaderDescriptor withProvider({ + String? host, + String? tag, + List? versions, + List? catalogVersions, + }) => LoaderDescriptor( + id: id, + role: role, + modrinthTags: {...modrinthTags, if (host != null && tag != null) host: tag}, + curseforgeType: curseforgeType, + curseforgePlugins: curseforgePlugins, + modrinthArchiveKey: modrinthArchiveKey, + curseforgeArchiveToken: curseforgeArchiveToken, + versionSource: versionSource, + installer: installer, + serverLaunchJar: serverLaunchJar, + serverRunScript: serverRunScript, + compatibleMods: compatibleMods, + declaredId: declaredId, + variants: variants, + versions: versions ?? this.versions, + catalogVersions: catalogVersions ?? this.catalogVersions, + runtimeArtifact: runtimeArtifact, + ); +} + +/// One immutable snapshot shared by model helpers and service consumers. +class LoaderRegistry { + final Map descriptors; + const LoaderRegistry._(this.descriptors); + factory LoaderRegistry(Iterable descriptors) => + LoaderRegistry._( + Map.unmodifiable({for (final d in descriptors) d.id.name: d}), + ); + + LoaderDescriptor? operator [](LoaderId? id) => descriptors[id?.name]; + LoaderRegistry merge(Iterable additions) => LoaderRegistry([ + ...descriptors.values.where((d) => !additions.any((a) => a.id == d.id)), + ...additions, + ]); + + static const builtins = LoaderRegistry._({ + 'vanilla': LoaderDescriptor._( + id: LoaderId.vanilla, + role: LoaderRole.vanilla, + modrinthTags: {'*': 'vanilla'}, + installer: InstallerFamily.vanilla, + serverLaunchJar: 'server.jar', + ), + 'fabric': LoaderDescriptor._( + id: LoaderId.fabric, + role: LoaderRole.mod, + modrinthTags: {'*': 'fabric'}, + curseforgeType: 4, + modrinthArchiveKey: 'fabric-loader', + curseforgeArchiveToken: 'fabric', + versionSource: VersionSourceFamily.fabric, + installer: InstallerFamily.fabric, + serverLaunchJar: 'fabric-server-launch.jar', + ), + 'forge': LoaderDescriptor._( + id: LoaderId.forge, + role: LoaderRole.mod, + modrinthTags: {'*': 'forge'}, + curseforgeType: 1, + modrinthArchiveKey: 'forge', + curseforgeArchiveToken: 'forge', + versionSource: VersionSourceFamily.forge, + installer: InstallerFamily.forge, + serverRunScript: true, + ), + 'neoforge': LoaderDescriptor._( + id: LoaderId.neoforge, + role: LoaderRole.mod, + modrinthTags: {'*': 'neoforge'}, + curseforgeType: 6, + modrinthArchiveKey: 'neoforge', + curseforgeArchiveToken: 'neoforge', + versionSource: VersionSourceFamily.neoforge, + installer: InstallerFamily.forge, + serverRunScript: true, + ), + 'quilt': LoaderDescriptor._( + id: LoaderId.quilt, + role: LoaderRole.mod, + modrinthTags: {'*': 'quilt'}, + curseforgeType: 5, + modrinthArchiveKey: 'quilt-loader', + curseforgeArchiveToken: 'quilt', + versionSource: VersionSourceFamily.quilt, + installer: InstallerFamily.quilt, + serverLaunchJar: 'quilt-server-launch.jar', + ), + 'iris': LoaderDescriptor._( + id: LoaderId.iris, + role: LoaderRole.shader, + modrinthTags: {'*': 'iris'}, + ), + 'optifine': LoaderDescriptor._( + id: LoaderId.optifine, + role: LoaderRole.shader, + modrinthTags: {'*': 'optifine'}, + ), + 'canvas': LoaderDescriptor._( + id: LoaderId.canvas, + role: LoaderRole.shader, + modrinthTags: {'*': 'canvas'}, + ), + 'paper': LoaderDescriptor._( + id: LoaderId.paper, + role: LoaderRole.plugin, + modrinthTags: {'*': 'paper'}, + curseforgePlugins: true, + versionSource: VersionSourceFamily.paper, + installer: InstallerFamily.paper, + runtimeArtifact: 'paper', + ), + 'folia': LoaderDescriptor._( + id: LoaderId.folia, + role: LoaderRole.plugin, + modrinthTags: {'*': 'folia'}, + versionSource: VersionSourceFamily.paper, + installer: InstallerFamily.paper, + runtimeArtifact: 'folia', + ), + 'spigot': LoaderDescriptor._( + id: LoaderId.spigot, + role: LoaderRole.plugin, + modrinthTags: {'*': 'spigot'}, + curseforgePlugins: true, + versionSource: VersionSourceFamily.buildtools, + installer: InstallerFamily.buildtools, + runtimeArtifact: 'spigot', + ), + 'bukkit': LoaderDescriptor._( + id: LoaderId.bukkit, + role: LoaderRole.plugin, + modrinthTags: {'*': 'bukkit'}, + curseforgePlugins: true, + versionSource: VersionSourceFamily.buildtools, + installer: InstallerFamily.buildtools, + runtimeArtifact: 'craftbukkit', + ), + 'sponge': LoaderDescriptor._( + id: LoaderId.sponge, + role: LoaderRole.plugin, + modrinthTags: {'*': 'sponge'}, + variants: { + 'forge': LoaderId.spongeforge, + 'neoforge': LoaderId.spongeneo, + 'vanilla': LoaderId.spongevanilla, + 'fabric': LoaderId.spongevanilla, + 'quilt': LoaderId.spongevanilla, + }, + ), + 'spongeforge': LoaderDescriptor._( + id: LoaderId.spongeforge, + role: LoaderRole.plugin, + modrinthTags: {'*': 'sponge'}, + versionSource: VersionSourceFamily.sponge, + installer: InstallerFamily.sponge, + declaredId: LoaderId.sponge, + compatibleMods: [LoaderId.forge, LoaderId.neoforge], + runtimeArtifact: 'spongeforge', + ), + 'spongeneo': LoaderDescriptor._( + id: LoaderId.spongeneo, + role: LoaderRole.plugin, + modrinthTags: {'*': 'sponge'}, + versionSource: VersionSourceFamily.sponge, + installer: InstallerFamily.sponge, + declaredId: LoaderId.sponge, + compatibleMods: [LoaderId.forge, LoaderId.neoforge], + runtimeArtifact: 'spongeneo', + ), + 'spongevanilla': LoaderDescriptor._( + id: LoaderId.spongevanilla, + role: LoaderRole.plugin, + modrinthTags: {'*': 'sponge'}, + versionSource: VersionSourceFamily.sponge, + installer: InstallerFamily.sponge, + declaredId: LoaderId.sponge, + runtimeArtifact: 'spongevanilla', + ), + }); +} diff --git a/lib/src/model/loader/loader_id.dart b/lib/src/model/loader/loader_id.dart new file mode 100644 index 0000000..f3a485f --- /dev/null +++ b/lib/src/model/loader/loader_id.dart @@ -0,0 +1,47 @@ +import 'package:dart_mappable/dart_mappable.dart'; + +/// Stable serialized identity. Provider capabilities belong to a registry. +final class LoaderId { + final String name; + const LoaderId._(this.name); + + factory LoaderId(String name) { + if (!RegExp(r'^[a-z][a-z0-9_-]*$').hasMatch(name)) { + throw FormatException('Invalid lowercase loader identity "$name".'); + } + return LoaderId._(name); + } + + static const values = [forge, fabric, neoforge, quilt, vanilla]; + static const forge = LoaderId._('forge'); + static const fabric = LoaderId._('fabric'); + static const neoforge = LoaderId._('neoforge'); + static const quilt = LoaderId._('quilt'); + static const vanilla = LoaderId._('vanilla'); + static const iris = LoaderId._('iris'); + static const optifine = LoaderId._('optifine'); + static const canvas = LoaderId._('canvas'); + static const bukkit = LoaderId._('bukkit'); + static const folia = LoaderId._('folia'); + static const paper = LoaderId._('paper'); + static const spigot = LoaderId._('spigot'); + static const sponge = LoaderId._('sponge'); + static const spongeforge = LoaderId._('spongeforge'); + static const spongeneo = LoaderId._('spongeneo'); + static const spongevanilla = LoaderId._('spongevanilla'); + + @override + bool operator ==(Object other) => other is LoaderId && other.name == name; + @override + int get hashCode => name.hashCode; + @override + String toString() => name; +} + +class LoaderIdMapper extends SimpleMapper { + const LoaderIdMapper(); + @override + LoaderId decode(Object value) => LoaderId(value as String); + @override + Object encode(LoaderId self) => self.name; +} diff --git a/lib/src/model/manifest/emitter.dart b/lib/src/model/manifest/emitter.dart index 0786a55..42f08e8 100644 --- a/lib/src/model/manifest/emitter.dart +++ b/lib/src/model/manifest/emitter.dart @@ -32,7 +32,7 @@ String emitModsLock(ModsLock lock) { buf.writeln(' plugins: ${_str("${plugins.name}:$pluginVersion")}'); } } - buf.writeln('mc-version: ${_str(lock.mcVersion)}'); + buf.writeln('mc_version: ${_str(lock.mcVersion)}'); _emitSection(buf, 'mods', lock.mods); _emitSection(buf, 'resource_packs', lock.resourcePacks); @@ -85,47 +85,82 @@ void _emitSection( void _emitEntry(StringBuffer buf, String slug, LockedEntry e) { buf.writeln(' ${_key(slug)}:'); - buf.writeln(' source: ${_sourceKindName(e.sourceKind)}'); + buf.writeln(' kind: ${e.kind.name}'); final version = e.version; if (version != null) buf.writeln(' version: ${_str(version)}'); - final projectId = e.projectId; - if (projectId != null) buf.writeln(' project-id: ${_str(projectId)}'); - final versionId = e.versionId; - if (versionId != null) buf.writeln(' version-id: ${_str(versionId)}'); - final path = e.path; - if (path != null) buf.writeln(' path: ${_str(path)}'); - final f = e.file; - if (f != null) { - buf.writeln(' file:'); - buf.writeln(' name: ${_str(f.name)}'); - final url = f.url; - if (url != null) buf.writeln(' url: ${_str(url)}'); - final sha1 = f.sha1; - if (sha1 != null) { - buf.writeln(' sha1: ${_str(sha1.toLowerCase())}'); - } - final sha512 = f.sha512; - if (sha512 != null) { - buf.writeln(' sha512: ${_str(sha512.toLowerCase())}'); - } - if (f.size != null) buf.writeln(' size: ${f.size}'); - } buf.writeln(' client: ${e.client.name}'); buf.writeln(' server: ${e.server.name}'); if (e.dependency != LockedDependencyKind.direct) { buf.writeln(' dependency: ${e.dependency.name}'); } - if (e.gameVersions.isNotEmpty) { - final quoted = e.gameVersions.map(_str).join(', '); - buf.writeln(' game-versions: [$quoted]'); + if (e.requiredBy.isNotEmpty) { + buf.writeln(' required_by: [${e.requiredBy.map(_str).join(', ')}]'); } if (e.acceptsMc.isNotEmpty) { final quoted = e.acceptsMc.map(_str).join(', '); - buf.writeln(' accepts-mc: [$quoted]'); + buf.writeln(' accepts_mc: [$quoted]'); + } + switch (e.kind) { + case LockedEntryKind.resolved: + final mr = e.modrinth; + if (mr != null) _emitModrinthSource(buf, mr); + final cf = e.curseforge; + if (cf != null) _emitCurseforgeSource(buf, cf); + case LockedEntryKind.url: + final url = e.url; + if (url != null) buf.writeln(' url: ${_str(url)}'); + final f = e.urlFile; + if (f != null) _emitFile(buf, f, indent: ' '); + case LockedEntryKind.path: + final p = e.path; + if (p != null) buf.writeln(' path: ${_str(p)}'); } } -String _sourceKindName(LockedSourceKind k) => k.name; +void _emitModrinthSource(StringBuffer buf, LockedModrinthSource mr) { + buf.writeln(' modrinth:'); + if (mr.discoveredViaSearch) buf.writeln(' discovered_via_search: true'); + buf.writeln(' project_id: ${_str(mr.projectId)}'); + buf.writeln(' version_id: ${_str(mr.versionId)}'); + final vn = mr.versionNumber; + if (vn != null) buf.writeln(' version_number: ${_str(vn)}'); + final host = mr.host; + if (host != null) buf.writeln(' host: ${_str(host)}'); + _emitFile(buf, mr.file, indent: ' '); + if (mr.gameVersions.isNotEmpty) { + final quoted = mr.gameVersions.map(_str).join(', '); + buf.writeln(' game_versions: [$quoted]'); + } +} + +void _emitCurseforgeSource(StringBuffer buf, LockedCurseforgeSource cf) { + buf.writeln(' curseforge:'); + if (cf.discoveredViaSearch) buf.writeln(' discovered_via_search: true'); + buf.writeln(' project_id: ${cf.projectId}'); + buf.writeln(' file_id: ${cf.fileId}'); + final slug = cf.slug; + if (slug != null) buf.writeln(' slug: ${_str(slug)}'); + buf.writeln(' allow_mod_distribution: ${cf.allowModDistribution}'); + _emitFile(buf, cf.file, indent: ' '); + if (cf.gameVersions.isNotEmpty) { + final quoted = cf.gameVersions.map(_str).join(', '); + buf.writeln(' game_versions: [$quoted]'); + } +} + +void _emitFile(StringBuffer buf, LockedFile f, {required String indent}) { + buf.writeln('${indent}file:'); + buf.writeln('$indent name: ${_str(f.name)}'); + final url = f.url; + if (url != null) buf.writeln('$indent url: ${_str(url)}'); + final sha1 = f.sha1; + if (sha1 != null) buf.writeln('$indent sha1: ${_str(sha1.toLowerCase())}'); + final sha512 = f.sha512; + if (sha512 != null) { + buf.writeln('$indent sha512: ${_str(sha512.toLowerCase())}'); + } + if (f.size != null) buf.writeln('$indent size: ${f.size}'); +} String _key(String slug) { if (_needsQuotes(slug)) return _str(slug); diff --git a/lib/src/model/manifest/loader_ref.dart b/lib/src/model/manifest/loader_ref.dart index 5b299f7..30e27ae 100644 --- a/lib/src/model/manifest/loader_ref.dart +++ b/lib/src/model/manifest/loader_ref.dart @@ -6,6 +6,7 @@ const List modLoaderRefNames = [ 'forge', 'fabric', 'neoforge', + 'quilt', 'vanilla', ]; @@ -24,7 +25,7 @@ typedef TaggedRef = (String name, String? tag); /// Parses a generic `` or `:` reference. /// /// This deliberately knows nothing about Minecraft loaders. Callers map -/// [name] to their enum and decide whether [tag] is allowed or defaulted. +/// [name] to its validated identity and decide whether [tag] is allowed or defaulted. TaggedRef parseTaggedRef(String raw, Never Function(String message) onError) { final colon = raw.indexOf(':'); final namePart = (colon < 0 ? raw : raw.substring(0, colon)).toLowerCase(); @@ -77,18 +78,20 @@ LoaderRef parseModLoaderRef( return (ModLoader.vanilla, null); } - switch (namePart) { - case 'forge': - return (ModLoader.forge, tagPart); - case 'fabric': - return (ModLoader.fabric, tagPart); - case 'neoforge': - return (ModLoader.neoforge, tagPart); + final id = _parseId(namePart, onError); + final role = LoaderRegistry.builtins[id]?.role; + if (role != null && role != LoaderRole.mod) { + onError('"$namePart" is not a mod loader.'); + } + return (id, tagPart); +} + +LoaderId _parseId(String raw, Never Function(String) onError) { + try { + return LoaderId(raw); + } on FormatException catch (e) { + onError(e.message); } - onError( - '"$namePart" is not a recognized loader ' - '(allowed: forge, fabric, neoforge, vanilla).', - ); } /// Result of [parseDeclaredPluginLoaderRef]. @@ -100,29 +103,17 @@ DeclaredPluginLoaderRef parseDeclaredPluginLoaderRef( Never Function(String message) onError, ) { final (namePart, tagPart) = parseTaggedRef(raw, onError); - switch (namePart) { - case 'bukkit': - return (DeclaredPluginLoader.bukkit, tagPart); - case 'folia': - return (DeclaredPluginLoader.folia, tagPart); - case 'paper': - return (DeclaredPluginLoader.paper, tagPart); - case 'spigot': - return (DeclaredPluginLoader.spigot, tagPart); - case 'sponge': - return (DeclaredPluginLoader.sponge, tagPart); - case 'spongeforge': - case 'spongeneo': - case 'spongevanilla': - onError( - '"$namePart" is a resolved lockfile plugin loader; write ' - '`sponge` in mods.yaml instead.', - ); + if (const {'spongeforge', 'spongeneo', 'spongevanilla'}.contains(namePart)) { + onError( + '"$namePart" is a resolved lockfile plugin loader; write `sponge` in mods.yaml instead.', + ); } - onError( - '"$namePart" is not a recognized plugin loader ' - '(allowed: bukkit, folia, paper, spigot, sponge).', - ); + final id = _parseId(namePart, onError); + final role = LoaderRegistry.builtins[id]?.role; + if (role != null && role != LoaderRole.plugin) { + onError('"$namePart" is not a plugin loader.'); + } + return (id, tagPart); } /// Result of [parseResolvedPluginLoaderRef]. @@ -134,25 +125,5 @@ ResolvedPluginLoaderRef parseResolvedPluginLoaderRef( Never Function(String message) onError, ) { final (namePart, tagPart) = parseTaggedRef(raw, onError); - switch (namePart) { - case 'bukkit': - return (PluginLoader.bukkit, tagPart); - case 'folia': - return (PluginLoader.folia, tagPart); - case 'paper': - return (PluginLoader.paper, tagPart); - case 'spigot': - return (PluginLoader.spigot, tagPart); - case 'spongeforge': - return (PluginLoader.spongeforge, tagPart); - case 'spongeneo': - return (PluginLoader.spongeneo, tagPart); - case 'spongevanilla': - return (PluginLoader.spongevanilla, tagPart); - } - onError( - '"$namePart" is not a recognized resolved plugin loader ' - '(allowed: bukkit, folia, paper, spigot, spongeforge, ' - 'spongeneo, spongevanilla).', - ); + return (_parseId(namePart, onError), tagPart); } diff --git a/lib/src/model/manifest/mods_lock.dart b/lib/src/model/manifest/mods_lock.dart index 44c4bd7..2715e9e 100644 --- a/lib/src/model/manifest/mods_lock.dart +++ b/lib/src/model/manifest/mods_lock.dart @@ -4,8 +4,13 @@ import 'mods_yaml.dart'; part 'mods_lock.mapper.dart'; +/// What kind of [LockedEntry] this is — drives the lock-emit branches +/// and the artifact-fetch loop. `resolved` entries carry zero or more +/// per-platform source blocks ([LockedEntry.modrinth] / +/// [LockedEntry.curseforge]); `url` entries carry [LockedEntry.url] + +/// [LockedEntry.urlFile]; `path` entries carry [LockedEntry.path]. @MappableEnum() -enum LockedSourceKind { modrinth, url, path } +enum LockedEntryKind { resolved, url, path } /// Mirrors dart pub's lockfile dependency classification. @MappableEnum() @@ -28,14 +33,82 @@ class LockedFile with LockedFileMappable { }); } +/// Per-platform Modrinth source block. Present only when Modrinth +/// participated for this entry (eligible per `effectiveSources` and +/// resolved successfully). Omitted entirely otherwise — there is no +/// "marker" state. +@MappableClass() +class LockedModrinthSource with LockedModrinthSourceMappable { + final String projectId; + final String versionId; + final String? versionNumber; + + /// Resolved Modrinth source host. `null` means the global API default, + /// including its environment override, rather than the pack's host. + final String? host; + final bool discoveredViaSearch; + + final LockedFile file; + final List gameVersions; + + const LockedModrinthSource({ + required this.projectId, + required this.versionId, + this.versionNumber, + this.host, + this.discoveredViaSearch = false, + required this.file, + this.gameVersions = const [], + }); +} + +/// Per-platform CurseForge source block. Present only when CurseForge +/// participated for this entry. `file.url` is `null` when +/// [allowModDistribution] is `false` — the user has to download the +/// jar manually from the CurseForge project page. +@MappableClass() +class LockedCurseforgeSource with LockedCurseforgeSourceMappable { + final int projectId; + final int fileId; + final String? slug; + final LockedFile file; + final List gameVersions; + final bool allowModDistribution; + final bool discoveredViaSearch; + + const LockedCurseforgeSource({ + required this.projectId, + required this.fileId, + this.slug, + required this.file, + this.gameVersions = const [], + this.allowModDistribution = true, + this.discoveredViaSearch = false, + }); +} + @MappableClass() class LockedEntry with LockedEntryMappable { final String slug; - final LockedSourceKind sourceKind; + final LockedEntryKind kind; + + /// Joint label for `kind: resolved` — Modrinth's `versionNumber` when + /// it participated, else CurseForge's `file.name` or similar. `null` + /// for `kind: url` and `kind: path`. final String? version; - final String? projectId; - final String? versionId; - final LockedFile? file; + + /// Per-platform source block. Either fully populated (platform + /// participated and resolved) or null (excluded by `sources:`, + /// matrix-ineligible, or pack-default-disabled). For `kind: resolved`, + /// at least one of [modrinth] / [curseforge] must be non-null. + final LockedModrinthSource? modrinth; + final LockedCurseforgeSource? curseforge; + + /// `kind: url` payload. + final String? url; + final LockedFile? urlFile; + + /// `kind: path` payload. final String? path; /// Resolved client-side install state. @@ -47,34 +120,53 @@ class LockedEntry with LockedEntryMappable { /// Whether this entry is direct or transitive. final LockedDependencyKind dependency; - /// Minecraft versions tagged on the resolved Modrinth version. - final List gameVersions; + /// Logical parents requiring this entry, across all resolved platforms. + @MappableField(key: 'required_by') + final List requiredBy; /// Per-entry `accepts-mc` override from `mods.yaml`. final List acceptsMc; const LockedEntry({ required this.slug, - required this.sourceKind, + required this.kind, this.version, - this.projectId, - this.versionId, - this.file, + this.modrinth, + this.curseforge, + this.url, + this.urlFile, this.path, this.client = SideEnv.required, this.server = SideEnv.required, this.dependency = LockedDependencyKind.direct, - this.gameVersions = const [], + this.requiredBy = const [], this.acceptsMc = const [], }); /// Returns the install state for the requested build env. SideEnv sideFor(bool isClient) => isClient ? client : server; + + /// The most-relevant downloadable file for this entry, for callers + /// that don't care which platform sourced it (build copy loops, + /// hash-based lookups). For `kind: resolved`, prefers Modrinth's + /// file when present, otherwise falls back to CurseForge's. + /// For `kind: url`, returns [urlFile]. For `kind: path`, returns + /// null — the caller reads [path] directly. + LockedFile? get primaryFile { + switch (kind) { + case LockedEntryKind.resolved: + return modrinth?.file ?? curseforge?.file; + case LockedEntryKind.url: + return urlFile; + case LockedEntryKind.path: + return null; + } + } } /// Locked counterpart of `FileEntry`. /// Separate from [LockedEntry] because `files:` entries do not use -/// `LockedEntry.sourceKind`. +/// the platform-source-block layout. @MappableClass() class LockedFileEntry with LockedFileEntryMappable { /// Destination path relative to the build env root. diff --git a/lib/src/model/manifest/mods_lock.mapper.dart b/lib/src/model/manifest/mods_lock.mapper.dart index 507b24c..fcd8879 100644 --- a/lib/src/model/manifest/mods_lock.mapper.dart +++ b/lib/src/model/manifest/mods_lock.mapper.dart @@ -8,53 +8,53 @@ part of 'mods_lock.dart'; -class LockedSourceKindMapper extends EnumMapper { - LockedSourceKindMapper._(); +class LockedEntryKindMapper extends EnumMapper { + LockedEntryKindMapper._(); - static LockedSourceKindMapper? _instance; - static LockedSourceKindMapper ensureInitialized() { + static LockedEntryKindMapper? _instance; + static LockedEntryKindMapper ensureInitialized() { if (_instance == null) { - MapperContainer.globals.use(_instance = LockedSourceKindMapper._()); + MapperContainer.globals.use(_instance = LockedEntryKindMapper._()); } return _instance!; } - static LockedSourceKind fromValue(dynamic value) { + static LockedEntryKind fromValue(dynamic value) { ensureInitialized(); return MapperContainer.globals.fromValue(value); } @override - LockedSourceKind decode(dynamic value) { + LockedEntryKind decode(dynamic value) { switch (value) { - case r'modrinth': - return LockedSourceKind.modrinth; + case r'resolved': + return LockedEntryKind.resolved; case r'url': - return LockedSourceKind.url; + return LockedEntryKind.url; case r'path': - return LockedSourceKind.path; + return LockedEntryKind.path; default: throw MapperException.unknownEnumValue(value); } } @override - dynamic encode(LockedSourceKind self) { + dynamic encode(LockedEntryKind self) { switch (self) { - case LockedSourceKind.modrinth: - return r'modrinth'; - case LockedSourceKind.url: + case LockedEntryKind.resolved: + return r'resolved'; + case LockedEntryKind.url: return r'url'; - case LockedSourceKind.path: + case LockedEntryKind.path: return r'path'; } } } -extension LockedSourceKindMapperExtension on LockedSourceKind { +extension LockedEntryKindMapperExtension on LockedEntryKind { String toValue() { - LockedSourceKindMapper.ensureInitialized(); - return MapperContainer.globals.toValue(this) as String; + LockedEntryKindMapper.ensureInitialized(); + return MapperContainer.globals.toValue(this) as String; } } @@ -268,6 +268,461 @@ class _LockedFileCopyWithImpl<$R, $Out> ) => _LockedFileCopyWithImpl<$R2, $Out2>($value, $cast, t); } +class LockedModrinthSourceMapper extends ClassMapperBase { + LockedModrinthSourceMapper._(); + + static LockedModrinthSourceMapper? _instance; + static LockedModrinthSourceMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = LockedModrinthSourceMapper._()); + LockedFileMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'LockedModrinthSource'; + + static String _$projectId(LockedModrinthSource v) => v.projectId; + static const Field _f$projectId = Field( + 'projectId', + _$projectId, + ); + static String _$versionId(LockedModrinthSource v) => v.versionId; + static const Field _f$versionId = Field( + 'versionId', + _$versionId, + ); + static String? _$versionNumber(LockedModrinthSource v) => v.versionNumber; + static const Field _f$versionNumber = Field( + 'versionNumber', + _$versionNumber, + opt: true, + ); + static String? _$host(LockedModrinthSource v) => v.host; + static const Field _f$host = Field( + 'host', + _$host, + opt: true, + ); + static bool _$discoveredViaSearch(LockedModrinthSource v) => + v.discoveredViaSearch; + static const Field _f$discoveredViaSearch = Field( + 'discoveredViaSearch', + _$discoveredViaSearch, + opt: true, + def: false, + ); + static LockedFile _$file(LockedModrinthSource v) => v.file; + static const Field _f$file = Field( + 'file', + _$file, + ); + static List _$gameVersions(LockedModrinthSource v) => v.gameVersions; + static const Field> _f$gameVersions = + Field('gameVersions', _$gameVersions, opt: true, def: const []); + + @override + final MappableFields fields = const { + #projectId: _f$projectId, + #versionId: _f$versionId, + #versionNumber: _f$versionNumber, + #host: _f$host, + #discoveredViaSearch: _f$discoveredViaSearch, + #file: _f$file, + #gameVersions: _f$gameVersions, + }; + + static LockedModrinthSource _instantiate(DecodingData data) { + return LockedModrinthSource( + projectId: data.dec(_f$projectId), + versionId: data.dec(_f$versionId), + versionNumber: data.dec(_f$versionNumber), + host: data.dec(_f$host), + discoveredViaSearch: data.dec(_f$discoveredViaSearch), + file: data.dec(_f$file), + gameVersions: data.dec(_f$gameVersions), + ); + } + + @override + final Function instantiate = _instantiate; + + static LockedModrinthSource fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static LockedModrinthSource fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin LockedModrinthSourceMappable { + String toJson() { + return LockedModrinthSourceMapper.ensureInitialized() + .encodeJson(this as LockedModrinthSource); + } + + Map toMap() { + return LockedModrinthSourceMapper.ensureInitialized() + .encodeMap(this as LockedModrinthSource); + } + + LockedModrinthSourceCopyWith< + LockedModrinthSource, + LockedModrinthSource, + LockedModrinthSource + > + get copyWith => + _LockedModrinthSourceCopyWithImpl< + LockedModrinthSource, + LockedModrinthSource + >(this as LockedModrinthSource, $identity, $identity); + @override + String toString() { + return LockedModrinthSourceMapper.ensureInitialized().stringifyValue( + this as LockedModrinthSource, + ); + } + + @override + bool operator ==(Object other) { + return LockedModrinthSourceMapper.ensureInitialized().equalsValue( + this as LockedModrinthSource, + other, + ); + } + + @override + int get hashCode { + return LockedModrinthSourceMapper.ensureInitialized().hashValue( + this as LockedModrinthSource, + ); + } +} + +extension LockedModrinthSourceValueCopy<$R, $Out> + on ObjectCopyWith<$R, LockedModrinthSource, $Out> { + LockedModrinthSourceCopyWith<$R, LockedModrinthSource, $Out> + get $asLockedModrinthSource => $base.as( + (v, t, t2) => _LockedModrinthSourceCopyWithImpl<$R, $Out>(v, t, t2), + ); +} + +abstract class LockedModrinthSourceCopyWith< + $R, + $In extends LockedModrinthSource, + $Out +> + implements ClassCopyWith<$R, $In, $Out> { + LockedFileCopyWith<$R, LockedFile, LockedFile> get file; + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get gameVersions; + $R call({ + String? projectId, + String? versionId, + String? versionNumber, + String? host, + bool? discoveredViaSearch, + LockedFile? file, + List? gameVersions, + }); + LockedModrinthSourceCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _LockedModrinthSourceCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, LockedModrinthSource, $Out> + implements LockedModrinthSourceCopyWith<$R, LockedModrinthSource, $Out> { + _LockedModrinthSourceCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + LockedModrinthSourceMapper.ensureInitialized(); + @override + LockedFileCopyWith<$R, LockedFile, LockedFile> get file => + $value.file.copyWith.$chain((v) => call(file: v)); + @override + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> + get gameVersions => ListCopyWith( + $value.gameVersions, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(gameVersions: v), + ); + @override + $R call({ + String? projectId, + String? versionId, + Object? versionNumber = $none, + Object? host = $none, + bool? discoveredViaSearch, + LockedFile? file, + List? gameVersions, + }) => $apply( + FieldCopyWithData({ + if (projectId != null) #projectId: projectId, + if (versionId != null) #versionId: versionId, + if (versionNumber != $none) #versionNumber: versionNumber, + if (host != $none) #host: host, + if (discoveredViaSearch != null) + #discoveredViaSearch: discoveredViaSearch, + if (file != null) #file: file, + if (gameVersions != null) #gameVersions: gameVersions, + }), + ); + @override + LockedModrinthSource $make(CopyWithData data) => LockedModrinthSource( + projectId: data.get(#projectId, or: $value.projectId), + versionId: data.get(#versionId, or: $value.versionId), + versionNumber: data.get(#versionNumber, or: $value.versionNumber), + host: data.get(#host, or: $value.host), + discoveredViaSearch: data.get( + #discoveredViaSearch, + or: $value.discoveredViaSearch, + ), + file: data.get(#file, or: $value.file), + gameVersions: data.get(#gameVersions, or: $value.gameVersions), + ); + + @override + LockedModrinthSourceCopyWith<$R2, LockedModrinthSource, $Out2> + $chain<$R2, $Out2>(Then<$Out2, $R2> t) => + _LockedModrinthSourceCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + +class LockedCurseforgeSourceMapper + extends ClassMapperBase { + LockedCurseforgeSourceMapper._(); + + static LockedCurseforgeSourceMapper? _instance; + static LockedCurseforgeSourceMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = LockedCurseforgeSourceMapper._()); + LockedFileMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'LockedCurseforgeSource'; + + static int _$projectId(LockedCurseforgeSource v) => v.projectId; + static const Field _f$projectId = Field( + 'projectId', + _$projectId, + ); + static int _$fileId(LockedCurseforgeSource v) => v.fileId; + static const Field _f$fileId = Field( + 'fileId', + _$fileId, + ); + static String? _$slug(LockedCurseforgeSource v) => v.slug; + static const Field _f$slug = Field( + 'slug', + _$slug, + opt: true, + ); + static LockedFile _$file(LockedCurseforgeSource v) => v.file; + static const Field _f$file = Field( + 'file', + _$file, + ); + static List _$gameVersions(LockedCurseforgeSource v) => + v.gameVersions; + static const Field> _f$gameVersions = + Field('gameVersions', _$gameVersions, opt: true, def: const []); + static bool _$allowModDistribution(LockedCurseforgeSource v) => + v.allowModDistribution; + static const Field _f$allowModDistribution = + Field( + 'allowModDistribution', + _$allowModDistribution, + opt: true, + def: true, + ); + static bool _$discoveredViaSearch(LockedCurseforgeSource v) => + v.discoveredViaSearch; + static const Field _f$discoveredViaSearch = + Field( + 'discoveredViaSearch', + _$discoveredViaSearch, + opt: true, + def: false, + ); + + @override + final MappableFields fields = const { + #projectId: _f$projectId, + #fileId: _f$fileId, + #slug: _f$slug, + #file: _f$file, + #gameVersions: _f$gameVersions, + #allowModDistribution: _f$allowModDistribution, + #discoveredViaSearch: _f$discoveredViaSearch, + }; + + static LockedCurseforgeSource _instantiate(DecodingData data) { + return LockedCurseforgeSource( + projectId: data.dec(_f$projectId), + fileId: data.dec(_f$fileId), + slug: data.dec(_f$slug), + file: data.dec(_f$file), + gameVersions: data.dec(_f$gameVersions), + allowModDistribution: data.dec(_f$allowModDistribution), + discoveredViaSearch: data.dec(_f$discoveredViaSearch), + ); + } + + @override + final Function instantiate = _instantiate; + + static LockedCurseforgeSource fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static LockedCurseforgeSource fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin LockedCurseforgeSourceMappable { + String toJson() { + return LockedCurseforgeSourceMapper.ensureInitialized() + .encodeJson(this as LockedCurseforgeSource); + } + + Map toMap() { + return LockedCurseforgeSourceMapper.ensureInitialized() + .encodeMap(this as LockedCurseforgeSource); + } + + LockedCurseforgeSourceCopyWith< + LockedCurseforgeSource, + LockedCurseforgeSource, + LockedCurseforgeSource + > + get copyWith => + _LockedCurseforgeSourceCopyWithImpl< + LockedCurseforgeSource, + LockedCurseforgeSource + >(this as LockedCurseforgeSource, $identity, $identity); + @override + String toString() { + return LockedCurseforgeSourceMapper.ensureInitialized().stringifyValue( + this as LockedCurseforgeSource, + ); + } + + @override + bool operator ==(Object other) { + return LockedCurseforgeSourceMapper.ensureInitialized().equalsValue( + this as LockedCurseforgeSource, + other, + ); + } + + @override + int get hashCode { + return LockedCurseforgeSourceMapper.ensureInitialized().hashValue( + this as LockedCurseforgeSource, + ); + } +} + +extension LockedCurseforgeSourceValueCopy<$R, $Out> + on ObjectCopyWith<$R, LockedCurseforgeSource, $Out> { + LockedCurseforgeSourceCopyWith<$R, LockedCurseforgeSource, $Out> + get $asLockedCurseforgeSource => $base.as( + (v, t, t2) => _LockedCurseforgeSourceCopyWithImpl<$R, $Out>(v, t, t2), + ); +} + +abstract class LockedCurseforgeSourceCopyWith< + $R, + $In extends LockedCurseforgeSource, + $Out +> + implements ClassCopyWith<$R, $In, $Out> { + LockedFileCopyWith<$R, LockedFile, LockedFile> get file; + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get gameVersions; + $R call({ + int? projectId, + int? fileId, + String? slug, + LockedFile? file, + List? gameVersions, + bool? allowModDistribution, + bool? discoveredViaSearch, + }); + LockedCurseforgeSourceCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _LockedCurseforgeSourceCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, LockedCurseforgeSource, $Out> + implements + LockedCurseforgeSourceCopyWith<$R, LockedCurseforgeSource, $Out> { + _LockedCurseforgeSourceCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + LockedCurseforgeSourceMapper.ensureInitialized(); + @override + LockedFileCopyWith<$R, LockedFile, LockedFile> get file => + $value.file.copyWith.$chain((v) => call(file: v)); + @override + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> + get gameVersions => ListCopyWith( + $value.gameVersions, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(gameVersions: v), + ); + @override + $R call({ + int? projectId, + int? fileId, + Object? slug = $none, + LockedFile? file, + List? gameVersions, + bool? allowModDistribution, + bool? discoveredViaSearch, + }) => $apply( + FieldCopyWithData({ + if (projectId != null) #projectId: projectId, + if (fileId != null) #fileId: fileId, + if (slug != $none) #slug: slug, + if (file != null) #file: file, + if (gameVersions != null) #gameVersions: gameVersions, + if (allowModDistribution != null) + #allowModDistribution: allowModDistribution, + if (discoveredViaSearch != null) + #discoveredViaSearch: discoveredViaSearch, + }), + ); + @override + LockedCurseforgeSource $make(CopyWithData data) => LockedCurseforgeSource( + projectId: data.get(#projectId, or: $value.projectId), + fileId: data.get(#fileId, or: $value.fileId), + slug: data.get(#slug, or: $value.slug), + file: data.get(#file, or: $value.file), + gameVersions: data.get(#gameVersions, or: $value.gameVersions), + allowModDistribution: data.get( + #allowModDistribution, + or: $value.allowModDistribution, + ), + discoveredViaSearch: data.get( + #discoveredViaSearch, + or: $value.discoveredViaSearch, + ), + ); + + @override + LockedCurseforgeSourceCopyWith<$R2, LockedCurseforgeSource, $Out2> + $chain<$R2, $Out2>(Then<$Out2, $R2> t) => + _LockedCurseforgeSourceCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + class LockedEntryMapper extends ClassMapperBase { LockedEntryMapper._(); @@ -275,7 +730,9 @@ class LockedEntryMapper extends ClassMapperBase { static LockedEntryMapper ensureInitialized() { if (_instance == null) { MapperContainer.globals.use(_instance = LockedEntryMapper._()); - LockedSourceKindMapper.ensureInitialized(); + LockedEntryKindMapper.ensureInitialized(); + LockedModrinthSourceMapper.ensureInitialized(); + LockedCurseforgeSourceMapper.ensureInitialized(); LockedFileMapper.ensureInitialized(); SideEnvMapper.ensureInitialized(); LockedDependencyKindMapper.ensureInitialized(); @@ -288,10 +745,10 @@ class LockedEntryMapper extends ClassMapperBase { static String _$slug(LockedEntry v) => v.slug; static const Field _f$slug = Field('slug', _$slug); - static LockedSourceKind _$sourceKind(LockedEntry v) => v.sourceKind; - static const Field _f$sourceKind = Field( - 'sourceKind', - _$sourceKind, + static LockedEntryKind _$kind(LockedEntry v) => v.kind; + static const Field _f$kind = Field( + 'kind', + _$kind, ); static String? _$version(LockedEntry v) => v.version; static const Field _f$version = Field( @@ -299,22 +756,28 @@ class LockedEntryMapper extends ClassMapperBase { _$version, opt: true, ); - static String? _$projectId(LockedEntry v) => v.projectId; - static const Field _f$projectId = Field( - 'projectId', - _$projectId, + static LockedModrinthSource? _$modrinth(LockedEntry v) => v.modrinth; + static const Field _f$modrinth = Field( + 'modrinth', + _$modrinth, opt: true, ); - static String? _$versionId(LockedEntry v) => v.versionId; - static const Field _f$versionId = Field( - 'versionId', - _$versionId, + static LockedCurseforgeSource? _$curseforge(LockedEntry v) => v.curseforge; + static const Field _f$curseforge = Field( + 'curseforge', + _$curseforge, opt: true, ); - static LockedFile? _$file(LockedEntry v) => v.file; - static const Field _f$file = Field( - 'file', - _$file, + static String? _$url(LockedEntry v) => v.url; + static const Field _f$url = Field( + 'url', + _$url, + opt: true, + ); + static LockedFile? _$urlFile(LockedEntry v) => v.urlFile; + static const Field _f$urlFile = Field( + 'urlFile', + _$urlFile, opt: true, ); static String? _$path(LockedEntry v) => v.path; @@ -344,10 +807,11 @@ class LockedEntryMapper extends ClassMapperBase { opt: true, def: LockedDependencyKind.direct, ); - static List _$gameVersions(LockedEntry v) => v.gameVersions; - static const Field> _f$gameVersions = Field( - 'gameVersions', - _$gameVersions, + static List _$requiredBy(LockedEntry v) => v.requiredBy; + static const Field> _f$requiredBy = Field( + 'requiredBy', + _$requiredBy, + key: r'required_by', opt: true, def: const [], ); @@ -362,32 +826,34 @@ class LockedEntryMapper extends ClassMapperBase { @override final MappableFields fields = const { #slug: _f$slug, - #sourceKind: _f$sourceKind, + #kind: _f$kind, #version: _f$version, - #projectId: _f$projectId, - #versionId: _f$versionId, - #file: _f$file, + #modrinth: _f$modrinth, + #curseforge: _f$curseforge, + #url: _f$url, + #urlFile: _f$urlFile, #path: _f$path, #client: _f$client, #server: _f$server, #dependency: _f$dependency, - #gameVersions: _f$gameVersions, + #requiredBy: _f$requiredBy, #acceptsMc: _f$acceptsMc, }; static LockedEntry _instantiate(DecodingData data) { return LockedEntry( slug: data.dec(_f$slug), - sourceKind: data.dec(_f$sourceKind), + kind: data.dec(_f$kind), version: data.dec(_f$version), - projectId: data.dec(_f$projectId), - versionId: data.dec(_f$versionId), - file: data.dec(_f$file), + modrinth: data.dec(_f$modrinth), + curseforge: data.dec(_f$curseforge), + url: data.dec(_f$url), + urlFile: data.dec(_f$urlFile), path: data.dec(_f$path), client: data.dec(_f$client), server: data.dec(_f$server), dependency: data.dec(_f$dependency), - gameVersions: data.dec(_f$gameVersions), + requiredBy: data.dec(_f$requiredBy), acceptsMc: data.dec(_f$acceptsMc), ); } @@ -452,21 +918,30 @@ extension LockedEntryValueCopy<$R, $Out> abstract class LockedEntryCopyWith<$R, $In extends LockedEntry, $Out> implements ClassCopyWith<$R, $In, $Out> { - LockedFileCopyWith<$R, LockedFile, LockedFile>? get file; - ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get gameVersions; + LockedModrinthSourceCopyWith<$R, LockedModrinthSource, LockedModrinthSource>? + get modrinth; + LockedCurseforgeSourceCopyWith< + $R, + LockedCurseforgeSource, + LockedCurseforgeSource + >? + get curseforge; + LockedFileCopyWith<$R, LockedFile, LockedFile>? get urlFile; + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get requiredBy; ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get acceptsMc; $R call({ String? slug, - LockedSourceKind? sourceKind, + LockedEntryKind? kind, String? version, - String? projectId, - String? versionId, - LockedFile? file, + LockedModrinthSource? modrinth, + LockedCurseforgeSource? curseforge, + String? url, + LockedFile? urlFile, String? path, SideEnv? client, SideEnv? server, LockedDependencyKind? dependency, - List? gameVersions, + List? requiredBy, List? acceptsMc, }); LockedEntryCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); @@ -481,15 +956,26 @@ class _LockedEntryCopyWithImpl<$R, $Out> late final ClassMapperBase $mapper = LockedEntryMapper.ensureInitialized(); @override - LockedFileCopyWith<$R, LockedFile, LockedFile>? get file => - $value.file?.copyWith.$chain((v) => call(file: v)); + LockedModrinthSourceCopyWith<$R, LockedModrinthSource, LockedModrinthSource>? + get modrinth => $value.modrinth?.copyWith.$chain((v) => call(modrinth: v)); @override - ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> - get gameVersions => ListCopyWith( - $value.gameVersions, - (v, t) => ObjectCopyWith(v, $identity, t), - (v) => call(gameVersions: v), - ); + LockedCurseforgeSourceCopyWith< + $R, + LockedCurseforgeSource, + LockedCurseforgeSource + >? + get curseforge => + $value.curseforge?.copyWith.$chain((v) => call(curseforge: v)); + @override + LockedFileCopyWith<$R, LockedFile, LockedFile>? get urlFile => + $value.urlFile?.copyWith.$chain((v) => call(urlFile: v)); + @override + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get requiredBy => + ListCopyWith( + $value.requiredBy, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(requiredBy: v), + ); @override ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get acceptsMc => ListCopyWith( @@ -500,46 +986,49 @@ class _LockedEntryCopyWithImpl<$R, $Out> @override $R call({ String? slug, - LockedSourceKind? sourceKind, + LockedEntryKind? kind, Object? version = $none, - Object? projectId = $none, - Object? versionId = $none, - Object? file = $none, + Object? modrinth = $none, + Object? curseforge = $none, + Object? url = $none, + Object? urlFile = $none, Object? path = $none, SideEnv? client, SideEnv? server, LockedDependencyKind? dependency, - List? gameVersions, + List? requiredBy, List? acceptsMc, }) => $apply( FieldCopyWithData({ if (slug != null) #slug: slug, - if (sourceKind != null) #sourceKind: sourceKind, + if (kind != null) #kind: kind, if (version != $none) #version: version, - if (projectId != $none) #projectId: projectId, - if (versionId != $none) #versionId: versionId, - if (file != $none) #file: file, + if (modrinth != $none) #modrinth: modrinth, + if (curseforge != $none) #curseforge: curseforge, + if (url != $none) #url: url, + if (urlFile != $none) #urlFile: urlFile, if (path != $none) #path: path, if (client != null) #client: client, if (server != null) #server: server, if (dependency != null) #dependency: dependency, - if (gameVersions != null) #gameVersions: gameVersions, + if (requiredBy != null) #requiredBy: requiredBy, if (acceptsMc != null) #acceptsMc: acceptsMc, }), ); @override LockedEntry $make(CopyWithData data) => LockedEntry( slug: data.get(#slug, or: $value.slug), - sourceKind: data.get(#sourceKind, or: $value.sourceKind), + kind: data.get(#kind, or: $value.kind), version: data.get(#version, or: $value.version), - projectId: data.get(#projectId, or: $value.projectId), - versionId: data.get(#versionId, or: $value.versionId), - file: data.get(#file, or: $value.file), + modrinth: data.get(#modrinth, or: $value.modrinth), + curseforge: data.get(#curseforge, or: $value.curseforge), + url: data.get(#url, or: $value.url), + urlFile: data.get(#urlFile, or: $value.urlFile), path: data.get(#path, or: $value.path), client: data.get(#client, or: $value.client), server: data.get(#server, or: $value.server), dependency: data.get(#dependency, or: $value.dependency), - gameVersions: data.get(#gameVersions, or: $value.gameVersions), + requiredBy: data.get(#requiredBy, or: $value.requiredBy), acceptsMc: data.get(#acceptsMc, or: $value.acceptsMc), ); diff --git a/lib/src/model/manifest/mods_yaml.dart b/lib/src/model/manifest/mods_yaml.dart index 66eb0c2..78175d5 100644 --- a/lib/src/model/manifest/mods_yaml.dart +++ b/lib/src/model/manifest/mods_yaml.dart @@ -1,97 +1,46 @@ import 'package:dart_mappable/dart_mappable.dart'; import 'file_entry.dart'; +import '../loader/loader_id.dart'; +import '../loader/loader_descriptor.dart'; +export '../loader/loader_id.dart'; +export '../loader/loader_descriptor.dart'; part 'mods_yaml.mapper.dart'; @MappableEnum() enum SpongeLoader { vanilla, forge, neoforge } -/// Mod loader the pack targets. -/// `vanilla` means no mod runtime. -@MappableEnum() -enum ModLoader { forge, fabric, neoforge, vanilla } - -@MappableEnum() -enum ShaderLoader { iris, optifine, canvas, vanilla } - -/// Resolved plugin loader stored in `mods.lock`. -/// `mods.yaml` uses [DeclaredPluginLoader]; the parser resolves it. -@MappableEnum() -enum PluginLoader { - bukkit, - folia, - paper, - spigot, - spongeneo, - spongeforge, - spongevanilla, -} - -/// Plugin loader as declared in `mods.yaml`. -/// Sponge variants are resolved later from `loader.mods`. -enum DeclaredPluginLoader { bukkit, folia, paper, spigot, sponge } - -/// Runtime entry point for a built server distribution. -/// `null` means the loader uses `run.bat` / `run.sh`. -extension ModLoaderServerInvocation on ModLoader { - String? get serverLaunchJar => switch (this) { - ModLoader.vanilla => 'server.jar', - ModLoader.fabric => 'fabric-server-launch.jar', - ModLoader.forge || ModLoader.neoforge => null, - }; - - bool get usesServerRunScript => serverLaunchJar == null; -} - -extension PluginLoaderToDeclared on PluginLoader { - /// Maps a resolved [PluginLoader] back to the declared `mods.yaml` value. - DeclaredPluginLoader toDeclared() => switch (this) { - PluginLoader.bukkit => DeclaredPluginLoader.bukkit, - PluginLoader.folia => DeclaredPluginLoader.folia, - PluginLoader.paper => DeclaredPluginLoader.paper, - PluginLoader.spigot => DeclaredPluginLoader.spigot, - PluginLoader.spongeforge || - PluginLoader.spongeneo || - PluginLoader.spongevanilla => DeclaredPluginLoader.sponge, - }; -} - -/// Resolves `(declared plugin loader, mod loader)` to [PluginLoader]. -extension DeclaredPluginLoaderResolution on DeclaredPluginLoader { - PluginLoader resolveWith(ModLoader mods) => switch (this) { - DeclaredPluginLoader.bukkit => PluginLoader.bukkit, - DeclaredPluginLoader.folia => PluginLoader.folia, - DeclaredPluginLoader.paper => PluginLoader.paper, - DeclaredPluginLoader.spigot => PluginLoader.spigot, - DeclaredPluginLoader.sponge => switch (mods) { - ModLoader.forge => PluginLoader.spongeforge, - ModLoader.neoforge => PluginLoader.spongeneo, - ModLoader.fabric || ModLoader.vanilla => PluginLoader.spongevanilla, - }, - }; -} +/// Compatibility spellings share one open, serialized loader identity. +typedef ModLoader = LoaderId; +typedef ShaderLoader = LoaderId; +typedef PluginLoader = LoaderId; +typedef DeclaredPluginLoader = LoaderId; + +extension LoaderRuntimeTraits on LoaderId { + String? get serverLaunchJar => LoaderRegistry.builtins[this]?.serverLaunchJar; + bool get usesServerRunScript => + LoaderRegistry.builtins[this]?.serverRunScript ?? false; + LoaderId toDeclared() => LoaderRegistry.builtins[this]?.declaredId ?? this; + LoaderId resolveWith(LoaderId mods) { + final descriptor = LoaderRegistry.builtins[this]; + if (descriptor == null || descriptor.variants.isEmpty) return this; + final variant = descriptor.variants[mods.name]; + if (variant == null) { + throw FormatException( + 'Loader $name has no declared compatibility with ${mods.name}.', + ); + } + return variant; + } -/// Per-loader behavior helpers. -extension PluginLoaderTraits on PluginLoader { - /// True when this server platform also runs server-side mods. bool get runsServerMods => - this == PluginLoader.spongeforge || this == PluginLoader.spongeneo; - - /// Modrinth `loaders` filter token for this loader. - String get modrinthLoaderToken => switch (this) { - PluginLoader.spongeforge || - PluginLoader.spongeneo || - PluginLoader.spongevanilla => 'sponge', - _ => name, - }; - - /// Mod loaders this plugin loader's server runtime accepts. - Set get compatibleModLoaders => switch (this) { - PluginLoader.spongeforge || - PluginLoader.spongeneo => const {ModLoader.forge, ModLoader.neoforge}, - _ => const {}, - }; + LoaderRegistry.builtins[this]?.runsServerMods ?? false; + String get modrinthLoaderToken => + LoaderRegistry.builtins[this]?.modrinthTag('*') ?? name; + Set get compatibleModLoaders => + (LoaderRegistry.builtins[this]?.compatibleMods ?? const []) + .toSet(); } /// Per-side install state. @@ -125,9 +74,12 @@ enum Section { mods, resourcePacks, dataPacks, shaders, plugins } /// Forces `server: unsupported` for mods on pure plugin-server platforms. Map coerceModsForPluginLoader( Map mods, - PluginLoader? pluginLoader, -) { - if (pluginLoader == null || pluginLoader.runsServerMods) { + PluginLoader? pluginLoader, { + LoaderRegistry registry = LoaderRegistry.builtins, +}) { + if (pluginLoader == null || + registry[pluginLoader] == null || + (registry[pluginLoader]?.runsServerMods ?? false)) { return mods; } return { @@ -139,9 +91,34 @@ Map coerceModsForPluginLoader( @MappableEnum() enum Channel { release, beta, alpha } +/// Cross-platform source kinds an entry can resolve against. +/// `modrinth` covers any Modrinth-protocol host (default modrinth.com +/// or a labrinth deployment named via [ModrinthEntrySource.host] / +/// [ModsYaml.modrinthHost]). +@MappableEnum() +enum SourceKind { modrinth, curseforge } + +/// Pack-level toggle for a platform's default participation. +@MappableEnum() +enum GitrinthPlatformState { enabled, disabled } + +/// Section + plugin loader → CurseForge eligibility. Single source of +/// truth for the source-eligibility matrix in +/// `docs/curseforge-bridge.md`. The CurseForge bridge defers to this +/// helper before parser validation, `add`, resolver dispatch, search, +/// and publish all touch the same predicate. +bool sectionAllowsCurseforge( + Section section, + PluginLoader? pluginLoader, { + LoaderRegistry registry = LoaderRegistry.builtins, +}) { + if (section != Section.plugins) return true; + return registry[pluginLoader]?.curseforgePlugins ?? (pluginLoader != null); +} + /// Per-section loader configuration. /// `mods.yaml` is declared; `mods.lock` is resolved. -@MappableClass() +@MappableClass(includeCustomMappers: [LoaderIdMapper()]) class LoaderConfig with LoaderConfigMappable { final ModLoader mods; @@ -174,7 +151,12 @@ sealed class EntrySource with EntrySourceMappable { @MappableClass(discriminatorValue: 'modrinth') class ModrinthEntrySource extends EntrySource with ModrinthEntrySourceMappable { - const ModrinthEntrySource(); + /// Per-entry override for the Modrinth-protocol base URL. `null` + /// means "use the pack-level default" ([ModsYaml.modrinthHost]), + /// which itself falls back to `defaultModrinthBaseUrl`. + final String? host; + + const ModrinthEntrySource({this.host}); } @MappableClass(discriminatorValue: 'url') @@ -205,11 +187,36 @@ class ModEntry with ModEntryMappable { final EntrySource source; - /// Additional Minecraft versions to union with the pack's `mc-version` + /// Additional Minecraft versions to union with the pack's `mc_version` /// when querying Modrinth for this entry. Query-time only; does not /// influence pack-level decisions. final List acceptsMc; + /// Modrinth project slug override. Used when the section-map key is + /// a local identifier and the entry's slug on Modrinth differs. + final String? modrinthSlug; + + /// CurseForge project slug or numeric project ID. + final String? curseforgeSlug; + + /// Explicit restriction on which platforms participate for this + /// entry. `null` means "fall back to `gitrinth.modrinth` / + /// `gitrinth.curseforge` pack-level defaults". + final Set? sources; + + /// Per-entry override for the "scan older versions on both platforms + /// looking for a SHA1 match" depth used when both Modrinth and + /// CurseForge participate. `null` falls back to the global default + /// (10). `0` disables the scan; only the newest pair is compared. + /// Mutually exclusive with a single-element [sources]. + final int? hashScanDepth; + + /// Per-entry override allowing a Modrinth/CurseForge SHA1 mismatch to + /// be locked despite no match in the scan window. `null` falls back + /// to `false`. Mutually exclusive with a single-element [sources]. + final bool? allowHashMismatch; + final bool noCrossPlatformSearch; + const ModEntry({ required this.slug, this.constraintRaw, @@ -218,7 +225,43 @@ class ModEntry with ModEntryMappable { this.server = SideEnv.required, this.source = const ModrinthEntrySource(), this.acceptsMc = const [], + this.modrinthSlug, + this.curseforgeSlug, + this.sources, + this.hashScanDepth, + this.allowHashMismatch, + this.noCrossPlatformSearch = false, + }); +} + +/// Top-level `gitrinth:` block. Holds the semver constraint on the +/// running gitrinth CLI and the platform default toggles consumed by +/// the source-eligibility logic. Replaces the legacy `tooling:` block +/// (which only ever held `tooling.gitrinth`). +@MappableClass() +class GitrinthConfig with GitrinthConfigMappable { + /// Semver constraint string, e.g. "^1.0.0" or ">=1.0.0 <2.0.0". + /// Stored verbatim — enforcement against the running CLI version is + /// deferred to a later task. + final String? version; + + /// Modrinth participation default; on by default. + final GitrinthPlatformState modrinth; + + /// CurseForge participation default; off by default. + final GitrinthPlatformState curseforge; + + const GitrinthConfig({ + this.version, + this.modrinth = GitrinthPlatformState.enabled, + this.curseforge = GitrinthPlatformState.disabled, }); + + /// True when [kind] participates by default for this pack. + bool isPlatformEnabled(SourceKind kind) => switch (kind) { + SourceKind.modrinth => modrinth == GitrinthPlatformState.enabled, + SourceKind.curseforge => curseforge == GitrinthPlatformState.enabled, + }; } @MappableClass() @@ -229,6 +272,17 @@ class ModsYaml with ModsYamlMappable { final String description; final LoaderConfig loader; final String mcVersion; + + /// Pack-level default for the Modrinth-source base URL. Per-entry + /// `modrinth_host:` wins; otherwise this is consulted before the + /// hard-coded `defaultModrinthBaseUrl` fallback. + final String? modrinthHost; + + /// `gitrinth:` block — semver constraint plus per-platform default + /// toggles. Defaults: Modrinth enabled, CurseForge disabled, + /// version null. + final GitrinthConfig gitrinth; + final Map mods; final Map resourcePacks; final Map dataPacks; @@ -242,10 +296,17 @@ class ModsYaml with ModsYamlMappable { /// pubgrub, mrpack `files[]` resolution, or the globalpacks tree. final Map files; - /// Modrinth-compatible server URL `gitrinth modrinth publish` - /// uploads to. `null` means the default (modrinth.com); the literal - /// string `none` means publishing is disabled for this pack. - final String? publishTo; + /// Explicit publish targets with optional Modrinth publishing host. + /// Null follows enabled gitrinth platforms; empty disables publication. + final Map? publishTo; + + /// Null follows enabled platforms; an empty map disables publishing. + Map get publishTargets => + publishTo ?? + { + for (final kind in SourceKind.values) + if (gitrinth.isPlatformEnabled(kind)) kind: null, + }; const ModsYaml({ required this.slug, @@ -254,6 +315,8 @@ class ModsYaml with ModsYamlMappable { required this.description, required this.loader, required this.mcVersion, + this.modrinthHost, + this.gitrinth = const GitrinthConfig(), this.mods = const {}, this.resourcePacks = const {}, this.dataPacks = const {}, @@ -264,6 +327,15 @@ class ModsYaml with ModsYamlMappable { this.publishTo, }); + /// Effective Modrinth-protocol base URL for [entry]. Single source + /// of truth so callers don't repeat the per-entry → pack-level → + /// global default chain inline. + String effectiveModrinthHost(ModEntry entry, String defaultBaseUrl) { + final source = entry.source; + final entryHost = source is ModrinthEntrySource ? source.host : null; + return entryHost ?? modrinthHost ?? defaultBaseUrl; + } + Map sectionEntries(Section section) { switch (section) { case Section.mods: @@ -278,4 +350,38 @@ class ModsYaml with ModsYamlMappable { return plugins; } } + + /// Transforms each artifact section while preserving pack configuration, + /// project overrides, and loose files. + ModsYaml mapSectionEntries( + Map Function( + Section section, + Map entries, + ) + transform, + ) { + final sections = { + for (final section in Section.values) + section: transform(section, sectionEntries(section)), + }; + return copyWith( + mods: sections[Section.mods]!, + resourcePacks: sections[Section.resourcePacks]!, + dataPacks: sections[Section.dataPacks]!, + shaders: sections[Section.shaders]!, + plugins: sections[Section.plugins]!, + ); + } + + ModsYaml withSectionEntries(Section section, Map entries) => + mapSectionEntries( + (current, previous) => current == section ? entries : previous, + ); + + ModsYaml withoutEntrySlugs(Set slugs) => mapSectionEntries( + (_, entries) => { + for (final entry in entries.entries) + if (!slugs.contains(entry.key)) entry.key: entry.value, + }, + ); } diff --git a/lib/src/model/manifest/mods_yaml.mapper.dart b/lib/src/model/manifest/mods_yaml.mapper.dart index 4bf8333..40ab00a 100644 --- a/lib/src/model/manifest/mods_yaml.mapper.dart +++ b/lib/src/model/manifest/mods_yaml.mapper.dart @@ -58,180 +58,6 @@ extension SpongeLoaderMapperExtension on SpongeLoader { } } -class ModLoaderMapper extends EnumMapper { - ModLoaderMapper._(); - - static ModLoaderMapper? _instance; - static ModLoaderMapper ensureInitialized() { - if (_instance == null) { - MapperContainer.globals.use(_instance = ModLoaderMapper._()); - } - return _instance!; - } - - static ModLoader fromValue(dynamic value) { - ensureInitialized(); - return MapperContainer.globals.fromValue(value); - } - - @override - ModLoader decode(dynamic value) { - switch (value) { - case r'forge': - return ModLoader.forge; - case r'fabric': - return ModLoader.fabric; - case r'neoforge': - return ModLoader.neoforge; - case r'vanilla': - return ModLoader.vanilla; - default: - throw MapperException.unknownEnumValue(value); - } - } - - @override - dynamic encode(ModLoader self) { - switch (self) { - case ModLoader.forge: - return r'forge'; - case ModLoader.fabric: - return r'fabric'; - case ModLoader.neoforge: - return r'neoforge'; - case ModLoader.vanilla: - return r'vanilla'; - } - } -} - -extension ModLoaderMapperExtension on ModLoader { - String toValue() { - ModLoaderMapper.ensureInitialized(); - return MapperContainer.globals.toValue(this) as String; - } -} - -class ShaderLoaderMapper extends EnumMapper { - ShaderLoaderMapper._(); - - static ShaderLoaderMapper? _instance; - static ShaderLoaderMapper ensureInitialized() { - if (_instance == null) { - MapperContainer.globals.use(_instance = ShaderLoaderMapper._()); - } - return _instance!; - } - - static ShaderLoader fromValue(dynamic value) { - ensureInitialized(); - return MapperContainer.globals.fromValue(value); - } - - @override - ShaderLoader decode(dynamic value) { - switch (value) { - case r'iris': - return ShaderLoader.iris; - case r'optifine': - return ShaderLoader.optifine; - case r'canvas': - return ShaderLoader.canvas; - case r'vanilla': - return ShaderLoader.vanilla; - default: - throw MapperException.unknownEnumValue(value); - } - } - - @override - dynamic encode(ShaderLoader self) { - switch (self) { - case ShaderLoader.iris: - return r'iris'; - case ShaderLoader.optifine: - return r'optifine'; - case ShaderLoader.canvas: - return r'canvas'; - case ShaderLoader.vanilla: - return r'vanilla'; - } - } -} - -extension ShaderLoaderMapperExtension on ShaderLoader { - String toValue() { - ShaderLoaderMapper.ensureInitialized(); - return MapperContainer.globals.toValue(this) as String; - } -} - -class PluginLoaderMapper extends EnumMapper { - PluginLoaderMapper._(); - - static PluginLoaderMapper? _instance; - static PluginLoaderMapper ensureInitialized() { - if (_instance == null) { - MapperContainer.globals.use(_instance = PluginLoaderMapper._()); - } - return _instance!; - } - - static PluginLoader fromValue(dynamic value) { - ensureInitialized(); - return MapperContainer.globals.fromValue(value); - } - - @override - PluginLoader decode(dynamic value) { - switch (value) { - case r'bukkit': - return PluginLoader.bukkit; - case r'folia': - return PluginLoader.folia; - case r'paper': - return PluginLoader.paper; - case r'spigot': - return PluginLoader.spigot; - case r'spongeneo': - return PluginLoader.spongeneo; - case r'spongeforge': - return PluginLoader.spongeforge; - case r'spongevanilla': - return PluginLoader.spongevanilla; - default: - throw MapperException.unknownEnumValue(value); - } - } - - @override - dynamic encode(PluginLoader self) { - switch (self) { - case PluginLoader.bukkit: - return r'bukkit'; - case PluginLoader.folia: - return r'folia'; - case PluginLoader.paper: - return r'paper'; - case PluginLoader.spigot: - return r'spigot'; - case PluginLoader.spongeneo: - return r'spongeneo'; - case PluginLoader.spongeforge: - return r'spongeforge'; - case PluginLoader.spongevanilla: - return r'spongevanilla'; - } - } -} - -extension PluginLoaderMapperExtension on PluginLoader { - String toValue() { - PluginLoaderMapper.ensureInitialized(); - return MapperContainer.globals.toValue(this) as String; - } -} - class SideEnvMapper extends EnumMapper { SideEnvMapper._(); @@ -390,6 +216,99 @@ extension ChannelMapperExtension on Channel { } } +class SourceKindMapper extends EnumMapper { + SourceKindMapper._(); + + static SourceKindMapper? _instance; + static SourceKindMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = SourceKindMapper._()); + } + return _instance!; + } + + static SourceKind fromValue(dynamic value) { + ensureInitialized(); + return MapperContainer.globals.fromValue(value); + } + + @override + SourceKind decode(dynamic value) { + switch (value) { + case r'modrinth': + return SourceKind.modrinth; + case r'curseforge': + return SourceKind.curseforge; + default: + throw MapperException.unknownEnumValue(value); + } + } + + @override + dynamic encode(SourceKind self) { + switch (self) { + case SourceKind.modrinth: + return r'modrinth'; + case SourceKind.curseforge: + return r'curseforge'; + } + } +} + +extension SourceKindMapperExtension on SourceKind { + String toValue() { + SourceKindMapper.ensureInitialized(); + return MapperContainer.globals.toValue(this) as String; + } +} + +class GitrinthPlatformStateMapper extends EnumMapper { + GitrinthPlatformStateMapper._(); + + static GitrinthPlatformStateMapper? _instance; + static GitrinthPlatformStateMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = GitrinthPlatformStateMapper._()); + } + return _instance!; + } + + static GitrinthPlatformState fromValue(dynamic value) { + ensureInitialized(); + return MapperContainer.globals.fromValue(value); + } + + @override + GitrinthPlatformState decode(dynamic value) { + switch (value) { + case r'enabled': + return GitrinthPlatformState.enabled; + case r'disabled': + return GitrinthPlatformState.disabled; + default: + throw MapperException.unknownEnumValue(value); + } + } + + @override + dynamic encode(GitrinthPlatformState self) { + switch (self) { + case GitrinthPlatformState.enabled: + return r'enabled'; + case GitrinthPlatformState.disabled: + return r'disabled'; + } + } +} + +extension GitrinthPlatformStateMapperExtension on GitrinthPlatformState { + String toValue() { + GitrinthPlatformStateMapper.ensureInitialized(); + return MapperContainer.globals.toValue(this) + as String; + } +} + class LoaderConfigMapper extends ClassMapperBase { LoaderConfigMapper._(); @@ -397,9 +316,7 @@ class LoaderConfigMapper extends ClassMapperBase { static LoaderConfigMapper ensureInitialized() { if (_instance == null) { MapperContainer.globals.use(_instance = LoaderConfigMapper._()); - ModLoaderMapper.ensureInitialized(); - ShaderLoaderMapper.ensureInitialized(); - PluginLoaderMapper.ensureInitialized(); + MapperContainer.globals.useAll([LoaderIdMapper()]); } return _instance!; } @@ -407,8 +324,8 @@ class LoaderConfigMapper extends ClassMapperBase { @override final String id = 'LoaderConfig'; - static ModLoader _$mods(LoaderConfig v) => v.mods; - static const Field _f$mods = Field('mods', _$mods); + static LoaderId _$mods(LoaderConfig v) => v.mods; + static const Field _f$mods = Field('mods', _$mods); static String? _$modLoaderVersion(LoaderConfig v) => v.modLoaderVersion; static const Field _f$modLoaderVersion = Field( 'modLoaderVersion', @@ -416,14 +333,14 @@ class LoaderConfigMapper extends ClassMapperBase { opt: true, def: 'stable', ); - static ShaderLoader? _$shaders(LoaderConfig v) => v.shaders; - static const Field _f$shaders = Field( + static LoaderId? _$shaders(LoaderConfig v) => v.shaders; + static const Field _f$shaders = Field( 'shaders', _$shaders, opt: true, ); - static PluginLoader? _$plugins(LoaderConfig v) => v.plugins; - static const Field _f$plugins = Field( + static LoaderId? _$plugins(LoaderConfig v) => v.plugins; + static const Field _f$plugins = Field( 'plugins', _$plugins, opt: true, @@ -517,10 +434,10 @@ extension LoaderConfigValueCopy<$R, $Out> abstract class LoaderConfigCopyWith<$R, $In extends LoaderConfig, $Out> implements ClassCopyWith<$R, $In, $Out> { $R call({ - ModLoader? mods, + LoaderId? mods, String? modLoaderVersion, - ShaderLoader? shaders, - PluginLoader? plugins, + LoaderId? shaders, + LoaderId? plugins, String? pluginLoaderVersion, }); LoaderConfigCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); @@ -536,7 +453,7 @@ class _LoaderConfigCopyWithImpl<$R, $Out> LoaderConfigMapper.ensureInitialized(); @override $R call({ - ModLoader? mods, + LoaderId? mods, Object? modLoaderVersion = $none, Object? shaders = $none, Object? plugins = $none, @@ -637,8 +554,15 @@ class ModrinthEntrySourceMapper @override final String id = 'ModrinthEntrySource'; + static String? _$host(ModrinthEntrySource v) => v.host; + static const Field _f$host = Field( + 'host', + _$host, + opt: true, + ); + @override - final MappableFields fields = const {}; + final MappableFields fields = const {#host: _f$host}; @override final String discriminatorKey = 'kind'; @@ -649,7 +573,7 @@ class ModrinthEntrySourceMapper EntrySourceMapper.ensureInitialized(); static ModrinthEntrySource _instantiate(DecodingData data) { - return ModrinthEntrySource(); + return ModrinthEntrySource(host: data.dec(_f$host)); } @override @@ -723,7 +647,7 @@ abstract class ModrinthEntrySourceCopyWith< > implements EntrySourceCopyWith<$R, $In, $Out> { @override - $R call(); + $R call({String? host}); ModrinthEntrySourceCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( Then<$Out2, $R2> t, ); @@ -738,9 +662,11 @@ class _ModrinthEntrySourceCopyWithImpl<$R, $Out> late final ClassMapperBase $mapper = ModrinthEntrySourceMapper.ensureInitialized(); @override - $R call() => $apply(FieldCopyWithData({})); + $R call({Object? host = $none}) => + $apply(FieldCopyWithData({if (host != $none) #host: host})); @override - ModrinthEntrySource $make(CopyWithData data) => ModrinthEntrySource(); + ModrinthEntrySource $make(CopyWithData data) => + ModrinthEntrySource(host: data.get(#host, or: $value.host)); @override ModrinthEntrySourceCopyWith<$R2, ModrinthEntrySource, $Out2> @@ -1004,6 +930,7 @@ class ModEntryMapper extends ClassMapperBase { ChannelMapper.ensureInitialized(); SideEnvMapper.ensureInitialized(); EntrySourceMapper.ensureInitialized(); + SourceKindMapper.ensureInitialized(); } return _instance!; } @@ -1053,6 +980,43 @@ class ModEntryMapper extends ClassMapperBase { opt: true, def: const [], ); + static String? _$modrinthSlug(ModEntry v) => v.modrinthSlug; + static const Field _f$modrinthSlug = Field( + 'modrinthSlug', + _$modrinthSlug, + opt: true, + ); + static String? _$curseforgeSlug(ModEntry v) => v.curseforgeSlug; + static const Field _f$curseforgeSlug = Field( + 'curseforgeSlug', + _$curseforgeSlug, + opt: true, + ); + static Set? _$sources(ModEntry v) => v.sources; + static const Field> _f$sources = Field( + 'sources', + _$sources, + opt: true, + ); + static int? _$hashScanDepth(ModEntry v) => v.hashScanDepth; + static const Field _f$hashScanDepth = Field( + 'hashScanDepth', + _$hashScanDepth, + opt: true, + ); + static bool? _$allowHashMismatch(ModEntry v) => v.allowHashMismatch; + static const Field _f$allowHashMismatch = Field( + 'allowHashMismatch', + _$allowHashMismatch, + opt: true, + ); + static bool _$noCrossPlatformSearch(ModEntry v) => v.noCrossPlatformSearch; + static const Field _f$noCrossPlatformSearch = Field( + 'noCrossPlatformSearch', + _$noCrossPlatformSearch, + opt: true, + def: false, + ); @override final MappableFields fields = const { @@ -1063,6 +1027,12 @@ class ModEntryMapper extends ClassMapperBase { #server: _f$server, #source: _f$source, #acceptsMc: _f$acceptsMc, + #modrinthSlug: _f$modrinthSlug, + #curseforgeSlug: _f$curseforgeSlug, + #sources: _f$sources, + #hashScanDepth: _f$hashScanDepth, + #allowHashMismatch: _f$allowHashMismatch, + #noCrossPlatformSearch: _f$noCrossPlatformSearch, }; static ModEntry _instantiate(DecodingData data) { @@ -1074,6 +1044,12 @@ class ModEntryMapper extends ClassMapperBase { server: data.dec(_f$server), source: data.dec(_f$source), acceptsMc: data.dec(_f$acceptsMc), + modrinthSlug: data.dec(_f$modrinthSlug), + curseforgeSlug: data.dec(_f$curseforgeSlug), + sources: data.dec(_f$sources), + hashScanDepth: data.dec(_f$hashScanDepth), + allowHashMismatch: data.dec(_f$allowHashMismatch), + noCrossPlatformSearch: data.dec(_f$noCrossPlatformSearch), ); } @@ -1144,6 +1120,12 @@ abstract class ModEntryCopyWith<$R, $In extends ModEntry, $Out> SideEnv? server, EntrySource? source, List? acceptsMc, + String? modrinthSlug, + String? curseforgeSlug, + Set? sources, + int? hashScanDepth, + bool? allowHashMismatch, + bool? noCrossPlatformSearch, }); ModEntryCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); } @@ -1175,6 +1157,12 @@ class _ModEntryCopyWithImpl<$R, $Out> SideEnv? server, EntrySource? source, List? acceptsMc, + Object? modrinthSlug = $none, + Object? curseforgeSlug = $none, + Object? sources = $none, + Object? hashScanDepth = $none, + Object? allowHashMismatch = $none, + bool? noCrossPlatformSearch, }) => $apply( FieldCopyWithData({ if (slug != null) #slug: slug, @@ -1184,6 +1172,13 @@ class _ModEntryCopyWithImpl<$R, $Out> if (server != null) #server: server, if (source != null) #source: source, if (acceptsMc != null) #acceptsMc: acceptsMc, + if (modrinthSlug != $none) #modrinthSlug: modrinthSlug, + if (curseforgeSlug != $none) #curseforgeSlug: curseforgeSlug, + if (sources != $none) #sources: sources, + if (hashScanDepth != $none) #hashScanDepth: hashScanDepth, + if (allowHashMismatch != $none) #allowHashMismatch: allowHashMismatch, + if (noCrossPlatformSearch != null) + #noCrossPlatformSearch: noCrossPlatformSearch, }), ); @override @@ -1195,6 +1190,18 @@ class _ModEntryCopyWithImpl<$R, $Out> server: data.get(#server, or: $value.server), source: data.get(#source, or: $value.source), acceptsMc: data.get(#acceptsMc, or: $value.acceptsMc), + modrinthSlug: data.get(#modrinthSlug, or: $value.modrinthSlug), + curseforgeSlug: data.get(#curseforgeSlug, or: $value.curseforgeSlug), + sources: data.get(#sources, or: $value.sources), + hashScanDepth: data.get(#hashScanDepth, or: $value.hashScanDepth), + allowHashMismatch: data.get( + #allowHashMismatch, + or: $value.allowHashMismatch, + ), + noCrossPlatformSearch: data.get( + #noCrossPlatformSearch, + or: $value.noCrossPlatformSearch, + ), ); @override @@ -1203,6 +1210,163 @@ class _ModEntryCopyWithImpl<$R, $Out> ) => _ModEntryCopyWithImpl<$R2, $Out2>($value, $cast, t); } +class GitrinthConfigMapper extends ClassMapperBase { + GitrinthConfigMapper._(); + + static GitrinthConfigMapper? _instance; + static GitrinthConfigMapper ensureInitialized() { + if (_instance == null) { + MapperContainer.globals.use(_instance = GitrinthConfigMapper._()); + GitrinthPlatformStateMapper.ensureInitialized(); + } + return _instance!; + } + + @override + final String id = 'GitrinthConfig'; + + static String? _$version(GitrinthConfig v) => v.version; + static const Field _f$version = Field( + 'version', + _$version, + opt: true, + ); + static GitrinthPlatformState _$modrinth(GitrinthConfig v) => v.modrinth; + static const Field _f$modrinth = Field( + 'modrinth', + _$modrinth, + opt: true, + def: GitrinthPlatformState.enabled, + ); + static GitrinthPlatformState _$curseforge(GitrinthConfig v) => v.curseforge; + static const Field _f$curseforge = + Field( + 'curseforge', + _$curseforge, + opt: true, + def: GitrinthPlatformState.disabled, + ); + + @override + final MappableFields fields = const { + #version: _f$version, + #modrinth: _f$modrinth, + #curseforge: _f$curseforge, + }; + + static GitrinthConfig _instantiate(DecodingData data) { + return GitrinthConfig( + version: data.dec(_f$version), + modrinth: data.dec(_f$modrinth), + curseforge: data.dec(_f$curseforge), + ); + } + + @override + final Function instantiate = _instantiate; + + static GitrinthConfig fromMap(Map map) { + return ensureInitialized().decodeMap(map); + } + + static GitrinthConfig fromJson(String json) { + return ensureInitialized().decodeJson(json); + } +} + +mixin GitrinthConfigMappable { + String toJson() { + return GitrinthConfigMapper.ensureInitialized().encodeJson( + this as GitrinthConfig, + ); + } + + Map toMap() { + return GitrinthConfigMapper.ensureInitialized().encodeMap( + this as GitrinthConfig, + ); + } + + GitrinthConfigCopyWith + get copyWith => _GitrinthConfigCopyWithImpl( + this as GitrinthConfig, + $identity, + $identity, + ); + @override + String toString() { + return GitrinthConfigMapper.ensureInitialized().stringifyValue( + this as GitrinthConfig, + ); + } + + @override + bool operator ==(Object other) { + return GitrinthConfigMapper.ensureInitialized().equalsValue( + this as GitrinthConfig, + other, + ); + } + + @override + int get hashCode { + return GitrinthConfigMapper.ensureInitialized().hashValue( + this as GitrinthConfig, + ); + } +} + +extension GitrinthConfigValueCopy<$R, $Out> + on ObjectCopyWith<$R, GitrinthConfig, $Out> { + GitrinthConfigCopyWith<$R, GitrinthConfig, $Out> get $asGitrinthConfig => + $base.as((v, t, t2) => _GitrinthConfigCopyWithImpl<$R, $Out>(v, t, t2)); +} + +abstract class GitrinthConfigCopyWith<$R, $In extends GitrinthConfig, $Out> + implements ClassCopyWith<$R, $In, $Out> { + $R call({ + String? version, + GitrinthPlatformState? modrinth, + GitrinthPlatformState? curseforge, + }); + GitrinthConfigCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ); +} + +class _GitrinthConfigCopyWithImpl<$R, $Out> + extends ClassCopyWithBase<$R, GitrinthConfig, $Out> + implements GitrinthConfigCopyWith<$R, GitrinthConfig, $Out> { + _GitrinthConfigCopyWithImpl(super.value, super.then, super.then2); + + @override + late final ClassMapperBase $mapper = + GitrinthConfigMapper.ensureInitialized(); + @override + $R call({ + Object? version = $none, + GitrinthPlatformState? modrinth, + GitrinthPlatformState? curseforge, + }) => $apply( + FieldCopyWithData({ + if (version != $none) #version: version, + if (modrinth != null) #modrinth: modrinth, + if (curseforge != null) #curseforge: curseforge, + }), + ); + @override + GitrinthConfig $make(CopyWithData data) => GitrinthConfig( + version: data.get(#version, or: $value.version), + modrinth: data.get(#modrinth, or: $value.modrinth), + curseforge: data.get(#curseforge, or: $value.curseforge), + ); + + @override + GitrinthConfigCopyWith<$R2, GitrinthConfig, $Out2> $chain<$R2, $Out2>( + Then<$Out2, $R2> t, + ) => _GitrinthConfigCopyWithImpl<$R2, $Out2>($value, $cast, t); +} + class ModsYamlMapper extends ClassMapperBase { ModsYamlMapper._(); @@ -1211,8 +1375,10 @@ class ModsYamlMapper extends ClassMapperBase { if (_instance == null) { MapperContainer.globals.use(_instance = ModsYamlMapper._()); LoaderConfigMapper.ensureInitialized(); + GitrinthConfigMapper.ensureInitialized(); ModEntryMapper.ensureInitialized(); FileEntryMapper.ensureInitialized(); + SourceKindMapper.ensureInitialized(); } return _instance!; } @@ -1241,6 +1407,19 @@ class ModsYamlMapper extends ClassMapperBase { 'mcVersion', _$mcVersion, ); + static String? _$modrinthHost(ModsYaml v) => v.modrinthHost; + static const Field _f$modrinthHost = Field( + 'modrinthHost', + _$modrinthHost, + opt: true, + ); + static GitrinthConfig _$gitrinth(ModsYaml v) => v.gitrinth; + static const Field _f$gitrinth = Field( + 'gitrinth', + _$gitrinth, + opt: true, + def: const GitrinthConfig(), + ); static Map _$mods(ModsYaml v) => v.mods; static const Field> _f$mods = Field( 'mods', @@ -1287,8 +1466,8 @@ class ModsYamlMapper extends ClassMapperBase { opt: true, def: const {}, ); - static String? _$publishTo(ModsYaml v) => v.publishTo; - static const Field _f$publishTo = Field( + static Map? _$publishTo(ModsYaml v) => v.publishTo; + static const Field> _f$publishTo = Field( 'publishTo', _$publishTo, opt: true, @@ -1302,6 +1481,8 @@ class ModsYamlMapper extends ClassMapperBase { #description: _f$description, #loader: _f$loader, #mcVersion: _f$mcVersion, + #modrinthHost: _f$modrinthHost, + #gitrinth: _f$gitrinth, #mods: _f$mods, #resourcePacks: _f$resourcePacks, #dataPacks: _f$dataPacks, @@ -1320,6 +1501,8 @@ class ModsYamlMapper extends ClassMapperBase { description: data.dec(_f$description), loader: data.dec(_f$loader), mcVersion: data.dec(_f$mcVersion), + modrinthHost: data.dec(_f$modrinthHost), + gitrinth: data.dec(_f$gitrinth), mods: data.dec(_f$mods), resourcePacks: data.dec(_f$resourcePacks), dataPacks: data.dec(_f$dataPacks), @@ -1389,6 +1572,7 @@ extension ModsYamlValueCopy<$R, $Out> on ObjectCopyWith<$R, ModsYaml, $Out> { abstract class ModsYamlCopyWith<$R, $In extends ModsYaml, $Out> implements ClassCopyWith<$R, $In, $Out> { LoaderConfigCopyWith<$R, LoaderConfig, LoaderConfig> get loader; + GitrinthConfigCopyWith<$R, GitrinthConfig, GitrinthConfig> get gitrinth; MapCopyWith<$R, String, ModEntry, ModEntryCopyWith<$R, ModEntry, ModEntry>> get mods; MapCopyWith<$R, String, ModEntry, ModEntryCopyWith<$R, ModEntry, ModEntry>> @@ -1408,6 +1592,8 @@ abstract class ModsYamlCopyWith<$R, $In extends ModsYaml, $Out> FileEntryCopyWith<$R, FileEntry, FileEntry> > get files; + MapCopyWith<$R, SourceKind, String?, ObjectCopyWith<$R, String?, String?>?>? + get publishTo; $R call({ String? slug, String? name, @@ -1415,6 +1601,8 @@ abstract class ModsYamlCopyWith<$R, $In extends ModsYaml, $Out> String? description, LoaderConfig? loader, String? mcVersion, + String? modrinthHost, + GitrinthConfig? gitrinth, Map? mods, Map? resourcePacks, Map? dataPacks, @@ -1422,7 +1610,7 @@ abstract class ModsYamlCopyWith<$R, $In extends ModsYaml, $Out> Map? plugins, Map? projectOverrides, Map? files, - String? publishTo, + Map? publishTo, }); ModsYamlCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); } @@ -1439,6 +1627,9 @@ class _ModsYamlCopyWithImpl<$R, $Out> LoaderConfigCopyWith<$R, LoaderConfig, LoaderConfig> get loader => $value.loader.copyWith.$chain((v) => call(loader: v)); @override + GitrinthConfigCopyWith<$R, GitrinthConfig, GitrinthConfig> get gitrinth => + $value.gitrinth.copyWith.$chain((v) => call(gitrinth: v)); + @override MapCopyWith<$R, String, ModEntry, ModEntryCopyWith<$R, ModEntry, ModEntry>> get mods => MapCopyWith( $value.mods, @@ -1493,6 +1684,15 @@ class _ModsYamlCopyWithImpl<$R, $Out> (v) => call(files: v), ); @override + MapCopyWith<$R, SourceKind, String?, ObjectCopyWith<$R, String?, String?>?>? + get publishTo => $value.publishTo != null + ? MapCopyWith( + $value.publishTo!, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(publishTo: v), + ) + : null; + @override $R call({ String? slug, String? name, @@ -1500,6 +1700,8 @@ class _ModsYamlCopyWithImpl<$R, $Out> String? description, LoaderConfig? loader, String? mcVersion, + Object? modrinthHost = $none, + GitrinthConfig? gitrinth, Map? mods, Map? resourcePacks, Map? dataPacks, @@ -1516,6 +1718,8 @@ class _ModsYamlCopyWithImpl<$R, $Out> if (description != null) #description: description, if (loader != null) #loader: loader, if (mcVersion != null) #mcVersion: mcVersion, + if (modrinthHost != $none) #modrinthHost: modrinthHost, + if (gitrinth != null) #gitrinth: gitrinth, if (mods != null) #mods: mods, if (resourcePacks != null) #resourcePacks: resourcePacks, if (dataPacks != null) #dataPacks: dataPacks, @@ -1534,6 +1738,8 @@ class _ModsYamlCopyWithImpl<$R, $Out> description: data.get(#description, or: $value.description), loader: data.get(#loader, or: $value.loader), mcVersion: data.get(#mcVersion, or: $value.mcVersion), + modrinthHost: data.get(#modrinthHost, or: $value.modrinthHost), + gitrinth: data.get(#gitrinth, or: $value.gitrinth), mods: data.get(#mods, or: $value.mods), resourcePacks: data.get(#resourcePacks, or: $value.resourcePacks), dataPacks: data.get(#dataPacks, or: $value.dataPacks), diff --git a/lib/src/model/manifest/mrpack_index.dart b/lib/src/model/manifest/mrpack_index.dart index 1fc5d75..1765fa3 100644 --- a/lib/src/model/manifest/mrpack_index.dart +++ b/lib/src/model/manifest/mrpack_index.dart @@ -1,3 +1,4 @@ +import '../../cli/exceptions.dart'; import 'package:dart_mappable/dart_mappable.dart'; import 'mods_yaml.dart'; @@ -44,21 +45,17 @@ class MrpackFile with MrpackFileMappable { } /// Maps [ModLoader] to the Modrinth dependency key. -String mrpackLoaderKey(ModLoader loader) { - switch (loader) { - case ModLoader.forge: - return 'forge'; - case ModLoader.neoforge: - return 'neoforge'; - case ModLoader.fabric: - return 'fabric-loader'; - case ModLoader.vanilla: - throw StateError( - 'mrpackLoaderKey called for vanilla; the dependencies block ' - 'must omit the mod-loader entry under vanilla (gate on ' - 'LoaderConfig.hasModRuntime).', - ); +String mrpackLoaderKey( + ModLoader loader, { + LoaderRegistry registry = LoaderRegistry.builtins, +}) { + final key = registry[loader]?.modrinthArchiveKey; + if (key == null) { + throw ValidationError( + 'Loader ${loader.name} has no Modrinth archive metadata capability.', + ); } + return key; } /// Maps per-side install state to a mrpack `env` map. diff --git a/lib/src/model/manifest/overrides_merger.dart b/lib/src/model/manifest/overrides_merger.dart index 8c92288..0e5e653 100644 --- a/lib/src/model/manifest/overrides_merger.dart +++ b/lib/src/model/manifest/overrides_merger.dart @@ -35,15 +35,7 @@ Future applyOverrides( base.plugins.containsKey(slug); if (alreadyDeclared) continue; final section = await inferSectionForTransitive(slug); - final synthesized = ModEntry( - slug: slug, - constraintRaw: ov.constraintRaw, - channel: ov.channel, - client: ov.client, - server: ov.server, - source: ov.source, - acceptsMc: ov.acceptsMc, - ); + final synthesized = ov.copyWith(slug: slug); switch (section) { case Section.mods: mods[slug] = synthesized; @@ -63,13 +55,7 @@ Future applyOverrides( } } - final mergedManifest = ModsYaml( - slug: base.slug, - name: base.name, - version: base.version, - description: base.description, - loader: base.loader, - mcVersion: base.mcVersion, + final mergedManifest = base.copyWith( mods: mods, resourcePacks: resourcePacks, dataPacks: dataPacks, @@ -109,14 +95,24 @@ Map _applyTo( return; } // Override version/source, and replace any transient gitrinth marker. - result[slug] = ModEntry( - slug: slug, + result[slug] = entry.copyWith( constraintRaw: ov.constraintRaw ?? entry.constraintRaw, channel: ov.channel ?? entry.channel, client: ov.client, server: ov.server, - source: ov.source, + source: switch ((entry.source, ov.source)) { + (ModrinthEntrySource(), ModrinthEntrySource(host: null)) => + entry.source, + _ => ov.source, + }, acceptsMc: ov.acceptsMc.isNotEmpty ? ov.acceptsMc : entry.acceptsMc, + modrinthSlug: ov.modrinthSlug ?? entry.modrinthSlug, + curseforgeSlug: ov.curseforgeSlug ?? entry.curseforgeSlug, + sources: ov.sources ?? entry.sources, + hashScanDepth: ov.hashScanDepth ?? entry.hashScanDepth, + allowHashMismatch: ov.allowHashMismatch ?? entry.allowHashMismatch, + noCrossPlatformSearch: + ov.noCrossPlatformSearch || entry.noCrossPlatformSearch, ); }); return result; diff --git a/lib/src/model/manifest/parser.dart b/lib/src/model/manifest/parser.dart index da1f9dd..4a6db26 100644 --- a/lib/src/model/manifest/parser.dart +++ b/lib/src/model/manifest/parser.dart @@ -46,9 +46,17 @@ ModsYaml parseModsYaml(String yamlText, {required String filePath}) { final version = _requireString(map, 'version', filePath); final description = _requireString(map, 'description', filePath); final loader = _parseLoaderConfigYaml(map['loader'], filePath); - final mcVersion = _requireString(map, 'mc-version', filePath); + final mcVersion = _requireString(map, 'mc_version', filePath); + final modrinthHost = _parseModrinthHost(map['modrinth_host'], filePath); + final gitrinth = _parseGitrinthBlock(map['gitrinth'], filePath); - final mods = _parseSection(map['mods'], 'mods', filePath, Section.mods); + final mods = _parseSection( + map['mods'], + 'mods', + filePath, + Section.mods, + loader: loader, + ); if (mods.isNotEmpty && !loader.hasModRuntime) { throw _err( "$filePath: mods: section has ${mods.length} " @@ -62,24 +70,28 @@ ModsYaml parseModsYaml(String yamlText, {required String filePath}) { 'resource_packs', filePath, Section.resourcePacks, + loader: loader, ); final dataPacks = _parseSection( map['data_packs'], 'data_packs', filePath, Section.dataPacks, + loader: loader, ); final shaders = _parseSection( map['shaders'], 'shaders', filePath, Section.shaders, + loader: loader, ); final plugins = _parseSection( map['plugins'], 'plugins', filePath, Section.plugins, + loader: loader, ); // The legacy `overrides:` key was renamed to `project_overrides:` // in v2; surface a clear migration error rather than silently @@ -99,21 +111,11 @@ ModsYaml parseModsYaml(String yamlText, {required String filePath}) { 'project_overrides', filePath, Section.mods, + loader: loader, ); final files = _parseFilesSection(map['files'], filePath); - final publishToRaw = map['publish_to']; - final String? publishTo; - if (publishToRaw == null) { - publishTo = null; - } else if (publishToRaw is String) { - final trimmed = publishToRaw.trim(); - publishTo = trimmed.isEmpty ? null : trimmed; - } else { - throw _err( - "$filePath: 'publish_to' must be a string URL, 'none', or omitted.", - ); - } + final publishTo = _parsePublishTo(map['publish_to'], filePath); if (shaders.isNotEmpty && loader.shaders == null) { throw _err( @@ -140,6 +142,8 @@ ModsYaml parseModsYaml(String yamlText, {required String filePath}) { description: description, loader: loader, mcVersion: mcVersion, + modrinthHost: modrinthHost, + gitrinth: gitrinth, mods: coercedMods, resourcePacks: resourcePacks, dataPacks: dataPacks, @@ -151,6 +155,132 @@ ModsYaml parseModsYaml(String yamlText, {required String filePath}) { ); } +Map? _parsePublishTo(dynamic raw, String filePath) { + if (raw == null) return null; + if (raw == 'none') return const {}; + final result = {}; + void add(dynamic name, dynamic config) { + final kind = switch (name) { + 'modrinth' => SourceKind.modrinth, + 'curseforge' => SourceKind.curseforge, + _ => throw _err("$filePath: invalid publish_to target '$name'."), + }; + if (result.containsKey(kind)) { + throw _err("$filePath: duplicate publish_to target '$name'."); + } + String? host; + if (kind == SourceKind.modrinth && config is String) { + final uri = Uri.tryParse(config); + if (uri == null || + !uri.hasAuthority || + !['https', 'http'].contains(uri.scheme) || + uri.userInfo.isNotEmpty || + uri.hasQuery || + uri.hasFragment) { + throw _err('$filePath: publish_to.modrinth must be an HTTP(S) URL.'); + } + host = config; + } else if (config != null && !(config is Map && config.isEmpty)) { + throw _err('$filePath: invalid publish_to.$name configuration.'); + } + result[kind] = host; + } + + if (raw is String) { + add(raw, null); + } else if (raw is List) { + for (final name in raw) { + add(name, null); + } + } else if (raw is Map) { + for (final item in raw.entries) { + add(item.key, item.value); + } + } else { + throw _err('$filePath: publish_to must be a target, list, or mapping.'); + } + if (result.isEmpty) { + throw _err('$filePath: publish_to cannot be empty; use none to disable.'); + } + return result; +} + +String? _parseModrinthHost(dynamic raw, String filePath) { + if (raw == null) return null; + if (raw is! String) { + throw _err("$filePath: 'modrinth_host' must be a string URL or omitted."); + } + final trimmed = raw.trim(); + if (trimmed.isEmpty) return null; + return trimmed; +} + +GitrinthConfig _parseGitrinthBlock(dynamic raw, String filePath) { + if (raw == null) return const GitrinthConfig(); + if (raw is! Map) { + throw _err("$filePath: 'gitrinth' must be a mapping or omitted."); + } + final m = _toPlainMap(raw); + for (final key in m.keys) { + if (key != 'version' && key != 'modrinth' && key != 'curseforge') { + throw _err( + "$filePath: gitrinth.$key is not a recognized field. " + "Known fields: version, modrinth, curseforge.", + ); + } + } + String? version; + final versionRaw = m['version']; + if (versionRaw != null) { + if (versionRaw is! String || versionRaw.trim().isEmpty) { + throw _err( + "$filePath: gitrinth.version must be a non-empty semver " + "constraint string (e.g. '^1.0.0').", + ); + } + version = versionRaw.trim(); + } + final modrinth = _parseGitrinthPlatformState( + m['modrinth'], + 'gitrinth.modrinth', + filePath, + GitrinthPlatformState.enabled, + ); + final curseforge = _parseGitrinthPlatformState( + m['curseforge'], + 'gitrinth.curseforge', + filePath, + GitrinthPlatformState.disabled, + ); + return GitrinthConfig( + version: version, + modrinth: modrinth, + curseforge: curseforge, + ); +} + +GitrinthPlatformState _parseGitrinthPlatformState( + dynamic raw, + String where, + String filePath, + GitrinthPlatformState defaultValue, +) { + if (raw == null) return defaultValue; + if (raw is! String) { + throw _err("$filePath: $where must be 'enabled' or 'disabled'."); + } + switch (raw.trim()) { + case 'enabled': + return GitrinthPlatformState.enabled; + case 'disabled': + return GitrinthPlatformState.disabled; + default: + throw _err( + "$filePath: $where value '$raw' must be 'enabled' or 'disabled'.", + ); + } +} + ProjectOverrides parseProjectOverrides( String yamlText, { required String filePath, @@ -187,7 +317,7 @@ ModsLock parseModsLock(String yamlText, {required String filePath}) { final map = _loadYamlMap(yamlText, filePath); final gitrinthVersion = _requireString(map, 'gitrinth-version', filePath); final loader = _parseLoaderConfigLock(map['loader'], filePath); - final mcVersion = _requireString(map, 'mc-version', filePath); + final mcVersion = _requireString(map, 'mc_version', filePath); final mods = _parseLockSection(map['mods'], 'mods', filePath); final resourcePacks = _parseLockSection( map['resource_packs'], diff --git a/lib/src/model/manifest/parsers/loader_config_parser.dart b/lib/src/model/manifest/parsers/loader_config_parser.dart index 0a84c65..ac5f0d8 100644 --- a/lib/src/model/manifest/parsers/loader_config_parser.dart +++ b/lib/src/model/manifest/parsers/loader_config_parser.dart @@ -47,7 +47,11 @@ LoaderConfig _parseLoaderConfigYaml(dynamic raw, String filePath) { String? pluginLoaderVersion; if (map.containsKey('plugins')) { final pluginTag = _parseYamlPluginLoader(map['plugins'], filePath); - plugins = pluginTag.loader.resolveWith(modLoader); + try { + plugins = pluginTag.loader.resolveWith(modLoader); + } on FormatException catch (e) { + throw _err('$filePath: loader.plugins ${e.message}'); + } pluginLoaderVersion = pluginTag.version; } @@ -189,19 +193,9 @@ _ResolvedPluginLoaderTag _parseLockPluginLoader(dynamic raw, String filePath) { ShaderLoader _parseShaderLoader(dynamic raw, String filePath) { final lower = raw.toString().toLowerCase(); - switch (lower) { - case 'iris': - return ShaderLoader.iris; - case 'optifine': - return ShaderLoader.optifine; - case 'canvas': - return ShaderLoader.canvas; - case 'vanilla': - return ShaderLoader.vanilla; - default: - throw _err( - '$filePath: loader.shaders "$raw" is not recognized ' - '(allowed: iris, optifine, canvas, vanilla).', - ); + try { + return LoaderId(lower); + } on FormatException catch (e) { + throw _err('$filePath: loader.shaders ${e.message}'); } } diff --git a/lib/src/model/manifest/parsers/mod_entry_parser.dart b/lib/src/model/manifest/parsers/mod_entry_parser.dart index 2cf2589..21a01bd 100644 --- a/lib/src/model/manifest/parsers/mod_entry_parser.dart +++ b/lib/src/model/manifest/parsers/mod_entry_parser.dart @@ -4,8 +4,9 @@ Map _parseSection( dynamic raw, String sectionName, String filePath, - Section section, -) { + Section section, { + LoaderConfig? loader, +}) { if (raw == null) return const {}; if (raw is! Map) { throw _err('$filePath: $sectionName must be a mapping.'); @@ -16,7 +17,14 @@ Map _parseSection( if (slug == null || slug.isEmpty) { throw _err('$filePath: $sectionName has an empty key.'); } - result[slug] = _parseEntry(slug, value, sectionName, filePath, section); + result[slug] = _parseEntry( + slug, + value, + sectionName, + filePath, + section, + loader: loader, + ); }); return result; } @@ -26,8 +34,9 @@ ModEntry _parseEntry( dynamic raw, String sectionName, String filePath, - Section section, -) { + Section section, { + LoaderConfig? loader, +}) { final defaults = defaultSidesFor(section); // Short forms: null (latest), a channel token, or a scalar version constraint. if (raw == null) { @@ -63,23 +72,39 @@ ModEntry _parseEntry( ); } final m = _toPlainMap(raw); - final hosted = m['hosted']; + final modrinthHostRaw = m['modrinth_host']; final url = m['url']; final path = m['path']; - final sourceCount = - (hosted == null ? 0 : 1) + (url == null ? 0 : 1) + (path == null ? 0 : 1); - if (sourceCount > 1) { + // `modrinth_host` co-exists with the Modrinth source kind (it's a + // host override on `modrinth:`-style entries), but is mutually + // exclusive with `url:` / `path:` because those select different + // source kinds entirely. The schema's `not` clauses already enforce + // this; mirror the check here so the parser-only path also gets a + // clear error. + if (modrinthHostRaw != null && (url != null || path != null)) { throw _err( - '$filePath: $sectionName/$slug declares more than one of ' - 'hosted/url/path. Choose at most one.', + '$filePath: $sectionName/$slug declares modrinth_host with ' + '${url != null ? 'url' : 'path'}; modrinth_host applies to the ' + 'Modrinth source kind only.', ); } - if (hosted != null) { - throw const UserError( - 'hosted source is deferred; use a default-Modrinth slug, url:, or path:.', + if (url != null && path != null) { + throw _err( + '$filePath: $sectionName/$slug declares both url: and path:. ' + 'Choose at most one.', ); } - EntrySource source = const ModrinthEntrySource(); + String? modrinthHost; + if (modrinthHostRaw != null) { + if (modrinthHostRaw is! String || modrinthHostRaw.isEmpty) { + throw _err( + '$filePath: $sectionName/$slug modrinth_host must be a ' + 'non-empty URL string.', + ); + } + modrinthHost = modrinthHostRaw; + } + EntrySource source = ModrinthEntrySource(host: modrinthHost); if (url != null) { if (url is! String || url.isEmpty) { throw _err( @@ -95,6 +120,44 @@ ModEntry _parseEntry( } source = PathEntrySource(path: path); } + final modrinthSlug = _parseOptionalStringField( + m, + 'modrinth', + '$sectionName/$slug', + filePath, + ); + final curseforgeSlug = _parseOptionalStringField( + m, + 'curseforge', + '$sectionName/$slug', + filePath, + ); + final sourceSet = _parseSourcesField( + m['sources'], + '$sectionName/$slug', + filePath, + ); + + // CurseForge eligibility check. The plan locates this with the + // existing plugin/shader validation pass (lines around 173) so all + // section-aware policy lives in one place. CurseForge resolution + // proper ships in a later part of the bridge — this guard already + // applies because `curseforge:` and `sources: curseforge` are now + // syntactically valid. + if (loader != null) { + final wantsCurseforge = + (sourceSet?.contains(SourceKind.curseforge) ?? false) || + curseforgeSlug != null; + if (wantsCurseforge && !sectionAllowsCurseforge(section, loader.plugins)) { + throw _err( + '$filePath: $sectionName/$slug declares a CurseForge source ' + '(sources: curseforge or curseforge:) but the section/loader ' + 'combination does not allow CurseForge. CurseForge plugin ' + 'entries are limited to bukkit/spigot/paper. See ' + 'docs/curseforge-bridge.md#source-eligibility-matrix.', + ); + } + } // `version:` accepts the same union the short form does: a channel // token (`release`/`beta`/`alpha`) routes to `channel` with no @@ -187,11 +250,47 @@ ModEntry _parseEntry( } final acceptsMc = _parseAcceptsMc( - m['accepts-mc'], + m['accepts_mc'], '$sectionName/$slug', filePath, ); + final hashScanDepth = _parseHashScanDepth( + m['hash_scan_depth'], + '$sectionName/$slug', + filePath, + ); + final allowHashMismatch = _parseAllowHashMismatch( + m['allow_hash_mismatch'], + '$sectionName/$slug', + filePath, + ); + final noSearch = m['no_cross_platform_search']; + if (noSearch != null && noSearch is! bool) { + throw _err( + '$filePath: $sectionName/$slug no_cross_platform_search must be a boolean.', + ); + } + // Both fields only make sense when both Modrinth and CurseForge + // participate. A single-element `sources:` already pins the entry to + // one platform — there's nothing to scan or mismatch against. + if (sourceSet != null && sourceSet.length == 1) { + if (hashScanDepth != null) { + throw _err( + '$filePath: $sectionName/$slug declares hash_scan_depth with ' + 'sources: [${sourceSet.first.name}]; the cross-platform hash ' + 'scan only applies when both platforms participate.', + ); + } + if (allowHashMismatch != null) { + throw _err( + '$filePath: $sectionName/$slug declares allow_hash_mismatch with ' + 'sources: [${sourceSet.first.name}]; mismatch handling only ' + 'applies when both platforms participate.', + ); + } + } + return ModEntry( slug: slug, constraintRaw: constraintRaw, @@ -200,9 +299,116 @@ ModEntry _parseEntry( server: server, source: source, acceptsMc: acceptsMc, + modrinthSlug: modrinthSlug, + curseforgeSlug: curseforgeSlug, + sources: sourceSet, + hashScanDepth: hashScanDepth, + allowHashMismatch: allowHashMismatch, + noCrossPlatformSearch: noSearch as bool? ?? false, ); } +int? _parseHashScanDepth(dynamic raw, String where, String filePath) { + if (raw == null) return null; + if (raw is! int || raw < 0) { + throw _err( + '$filePath: $where hash_scan_depth must be a non-negative integer; ' + 'got $raw.', + ); + } + return raw; +} + +bool? _parseAllowHashMismatch(dynamic raw, String where, String filePath) { + if (raw == null) return null; + if (raw is! bool) { + throw _err( + '$filePath: $where allow_hash_mismatch must be a boolean; got ' + '${raw.runtimeType}.', + ); + } + return raw; +} + +String? _parseOptionalStringField( + Map m, + String key, + String where, + String filePath, +) { + if (!m.containsKey(key)) return null; + final raw = m[key]; + if (raw == null) return null; + if (raw is! String || raw.isEmpty) { + throw _err('$filePath: $where $key must be a non-empty string.'); + } + return raw.trim(); +} + +Set? _parseSourcesField( + dynamic raw, + String where, + String filePath, +) { + if (raw == null) return null; + final List items; + if (raw is String) { + if (raw.trim().isEmpty) { + throw _err( + '$filePath: $where sources must be a non-empty platform name ' + 'or list (modrinth, curseforge).', + ); + } + items = [raw]; + } else if (raw is List) { + if (raw.isEmpty) { + throw _err( + '$filePath: $where sources must declare at least one ' + 'platform when present.', + ); + } + items = raw; + } else { + throw _err( + '$filePath: $where sources must be a platform name or a list ' + '(e.g. `modrinth`, `[modrinth, curseforge]`).', + ); + } + final seen = {}; + for (final item in items) { + if (item is! String) { + throw _err( + '$filePath: $where sources entries must be strings; got ' + '${item.runtimeType}.', + ); + } + final parsed = _parseSourceKind(item); + if (parsed == null) { + throw _err( + '$filePath: $where sources entry "$item" is not a known platform. ' + 'Use `modrinth` or `curseforge`.', + ); + } + if (!seen.add(parsed)) { + throw _err( + '$filePath: $where sources lists "${parsed.name}" more than once.', + ); + } + } + return seen; +} + +SourceKind? _parseSourceKind(String raw) { + switch (raw.trim()) { + case 'modrinth': + return SourceKind.modrinth; + case 'curseforge': + return SourceKind.curseforge; + default: + return null; + } +} + Map _parseLockSection( dynamic raw, String sectionName, @@ -222,12 +428,7 @@ Map _parseLockSection( throw _err('$filePath: $sectionName/$slug must be a mapping.'); } final m = _toPlainMap(value); - final sourceKind = _parseLockSourceKind( - m['source'], - sectionName, - slug, - filePath, - ); + final kind = _parseLockEntryKind(m['kind'], sectionName, slug, filePath); final client = _parseSideEnv( m['client'] ?? 'required', '$sectionName/$slug', @@ -241,66 +442,137 @@ Map _parseLockSection( filePath, ); final dependency = _parseLockDependencyKind(m['dependency'], m['auto']); - LockedFile? file; - final fileRaw = m['file']; - if (fileRaw != null) { - if (fileRaw is! Map) { - throw _err('$filePath: $sectionName/$slug file must be a mapping.'); - } - final fm = _toPlainMap(fileRaw); - file = LockedFile( - name: (fm['name'] as String?) ?? '', - url: fm['url'] as String?, - sha1: (fm['sha1'] as String?)?.toLowerCase(), - sha512: (fm['sha512'] as String?)?.toLowerCase(), - size: fm['size'] is int - ? fm['size'] as int - : (fm['size'] as num?)?.toInt(), + final requiredByRaw = m['required_by']; + if (requiredByRaw != null && + (requiredByRaw is! List || + requiredByRaw.any((v) => v is! String || v.isEmpty))) { + throw _err( + '$filePath: $sectionName/$slug required_by must be a list of non-empty strings.', ); } - final gameVersionsRaw = m['game-versions']; - final gameVersions = gameVersionsRaw is List - ? List.unmodifiable(gameVersionsRaw.map((v) => v.toString())) - : const []; - final acceptsMcRaw = m['accepts-mc']; + final requiredBy = requiredByRaw == null + ? [] + : List.from(requiredByRaw as List); + final acceptsMcRaw = m['accepts_mc']; final acceptsMc = acceptsMcRaw is List ? List.unmodifiable(acceptsMcRaw.map((v) => v.toString())) : const []; + + LockedModrinthSource? modrinthSource; + final modrinthRaw = m['modrinth']; + if (modrinthRaw is Map) { + final mm = _toPlainMap(modrinthRaw); + final file = _parseLockedFile( + mm['file'], + '$sectionName/$slug.modrinth', + filePath, + ); + if (file == null) { + throw _err( + '$filePath: $sectionName/$slug.modrinth must include a file: block.', + ); + } + final gameVersionsRaw = mm['game_versions']; + final gameVersions = gameVersionsRaw is List + ? List.unmodifiable(gameVersionsRaw.map((v) => v.toString())) + : const []; + modrinthSource = LockedModrinthSource( + projectId: (mm['project_id'] as String?) ?? '', + versionId: (mm['version_id'] as String?) ?? '', + versionNumber: mm['version_number'] as String?, + host: mm['host'] as String?, + discoveredViaSearch: mm['discovered_via_search'] == true, + file: file, + gameVersions: gameVersions, + ); + } + + LockedCurseforgeSource? curseforgeSource; + final curseforgeRaw = m['curseforge']; + if (curseforgeRaw is Map) { + final cm = _toPlainMap(curseforgeRaw); + final file = _parseLockedFile( + cm['file'], + '$sectionName/$slug.curseforge', + filePath, + ); + if (file == null) { + throw _err( + '$filePath: $sectionName/$slug.curseforge must include a ' + 'file: block.', + ); + } + final gameVersionsRaw = cm['game_versions']; + final gameVersions = gameVersionsRaw is List + ? List.unmodifiable(gameVersionsRaw.map((v) => v.toString())) + : const []; + final allow = cm['allow_mod_distribution']; + curseforgeSource = LockedCurseforgeSource( + projectId: (cm['project_id'] as num?)?.toInt() ?? 0, + fileId: (cm['file_id'] as num?)?.toInt() ?? 0, + slug: cm['slug'] as String?, + file: file, + gameVersions: gameVersions, + allowModDistribution: allow is bool ? allow : true, + discoveredViaSearch: cm['discovered_via_search'] == true, + ); + } + + final urlFile = kind == LockedEntryKind.url + ? _parseLockedFile(m['file'], '$sectionName/$slug', filePath) + : null; + result[slug] = LockedEntry( slug: slug, - sourceKind: sourceKind, + kind: kind, version: m['version'] as String?, - projectId: m['project-id'] as String?, - versionId: m['version-id'] as String?, - file: file, + modrinth: modrinthSource, + curseforge: curseforgeSource, + url: m['url'] as String?, + urlFile: urlFile, path: m['path'] as String?, client: client, server: server, dependency: dependency, - gameVersions: gameVersions, + requiredBy: requiredBy, acceptsMc: acceptsMc, ); }); return result; } -LockedSourceKind _parseLockSourceKind( +LockedFile? _parseLockedFile(dynamic raw, String where, String filePath) { + if (raw == null) return null; + if (raw is! Map) { + throw _err('$filePath: $where file must be a mapping.'); + } + final fm = _toPlainMap(raw); + return LockedFile( + name: (fm['name'] as String?) ?? '', + url: fm['url'] as String?, + sha1: (fm['sha1'] as String?)?.toLowerCase(), + sha512: (fm['sha512'] as String?)?.toLowerCase(), + size: fm['size'] is int ? fm['size'] as int : (fm['size'] as num?)?.toInt(), + ); +} + +LockedEntryKind _parseLockEntryKind( dynamic raw, String sectionName, String slug, String filePath, ) { switch (raw?.toString()) { - case 'modrinth': - return LockedSourceKind.modrinth; + case 'resolved': + return LockedEntryKind.resolved; case 'url': - return LockedSourceKind.url; + return LockedEntryKind.url; case 'path': - return LockedSourceKind.path; + return LockedEntryKind.path; default: throw _err( - '$filePath: $sectionName/$slug source "$raw" must be one of ' - 'modrinth, url, path.', + '$filePath: $sectionName/$slug kind "$raw" must be one of ' + 'resolved, url, path.', ); } } diff --git a/lib/src/model/manifest/server_distribution.dart b/lib/src/model/manifest/server_distribution.dart new file mode 100644 index 0000000..a0d203c --- /dev/null +++ b/lib/src/model/manifest/server_distribution.dart @@ -0,0 +1,223 @@ +import 'package:path/path.dart' as p; + +import '../loader/loader_id.dart'; + +const serverDistributionManifestName = 'gitrinth-server-distribution.json'; +const serverDistributionArtifactType = 'gitrinth-server-distribution'; + +bool isSafeServerDistributionPath(String path) => + path.isNotEmpty && + !path.contains('\\') && + !p.posix.isAbsolute(path) && + !p.windows.isAbsolute(path) && + !path.contains(':') && + p.posix.normalize(path) == path && + !p.posix + .split(path) + .any((part) => part == '.' || part == '..' || part.isEmpty); + +bool isPrivateServerDistributionPath(String path) { + final parts = p.posix.split(path).map((part) => part.toLowerCase()).toList(); + const directories = { + '.git', + '.gitrinth', + '.cache', + '.ssh', + 'cache', + 'caches', + 'logs', + 'runtimes', + 'launchers', + 'assets', + 'libraries', + 'versions', + }; + if (parts.any(directories.contains)) return true; + final name = parts.last; + return name == serverDistributionManifestName || + name == 'manifest.json' || + name == 'modrinth.index.json' || + name == 'eula.txt' || + name == '.env' || + name.startsWith('.env.') || + const { + 'launcher_accounts.json', + 'launcher_profiles.json', + 'usercache.json', + 'usernamecache.json', + }.contains(name) || + RegExp( + r'^(credentials?|secrets?|tokens?)(\.(json|ya?ml|toml|ini|conf|txt|properties|pem|key))?$', + ).hasMatch(name); +} + +class ServerDistributionFile { + final String path; + final String sha256; + + factory ServerDistributionFile({ + required String path, + required String sha256, + }) { + if (!isSafeServerDistributionPath(path) || + isPrivateServerDistributionPath(path)) { + throw FormatException( + 'Unsafe or private server distribution path: $path', + ); + } + if (!RegExp(r'^[a-f0-9]{64}$').hasMatch(sha256)) { + throw FormatException( + 'Invalid SHA256 for server distribution path $path.', + ); + } + return ServerDistributionFile._(path, sha256); + } + const ServerDistributionFile._(this.path, this.sha256); + Map toMap() => {'path': path, 'sha256': sha256}; +} + +/// Identifies a physical server distribution and binds its payload inventory. +class ServerDistributionManifest { + final String slug; + final String version; + final String minecraft; + final String loaderId; + final String? loaderVersion; + final String? pluginLoaderId; + final String? pluginLoaderVersion; + final List files; + + factory ServerDistributionManifest({ + required String slug, + required String version, + required String minecraft, + required String loaderId, + required String? loaderVersion, + String? pluginLoaderId, + String? pluginLoaderVersion, + required List files, + }) => ServerDistributionManifest.fromMap({ + 'formatVersion': 1, + 'artifactType': serverDistributionArtifactType, + 'slug': slug, + 'version': version, + 'minecraft': minecraft, + 'loader': { + 'id': loaderId, + 'version': loaderVersion, + if (pluginLoaderId != null) + 'plugins': {'id': pluginLoaderId, 'version': pluginLoaderVersion}, + }, + 'runtimeBundled': false, + 'files': files.map((file) => file.toMap()).toList(), + }); + + const ServerDistributionManifest._({ + required this.slug, + required this.version, + required this.minecraft, + required this.loaderId, + required this.loaderVersion, + required this.pluginLoaderId, + required this.pluginLoaderVersion, + required this.files, + }); + + factory ServerDistributionManifest.fromMap(Map data) { + const keys = { + 'formatVersion', + 'artifactType', + 'slug', + 'version', + 'minecraft', + 'loader', + 'runtimeBundled', + 'files', + }; + if (data.keys.any((key) => !keys.contains(key)) || + data['formatVersion'] != 1 || + data['artifactType'] != serverDistributionArtifactType || + data['runtimeBundled'] != false) { + throw const FormatException('Unsupported server distribution manifest.'); + } + String text(Map map, String key) { + final value = map[key]; + if (value is! String || value.trim().isEmpty) { + throw FormatException( + 'Server distribution $key must be a nonempty string.', + ); + } + return value; + } + + final loader = data['loader']; + final rawFiles = data['files']; + if (loader is! Map || + loader.keys.any( + (key) => key != 'id' && key != 'version' && key != 'plugins', + ) || + (loader['version'] != null && loader['version'] is! String) || + rawFiles is! List || + rawFiles.isEmpty) { + throw const FormatException( + 'Server distribution requires loader metadata and physical files.', + ); + } + final plugins = loader['plugins']; + if (plugins != null && + (plugins is! Map || + plugins.keys.any((key) => key != 'id' && key != 'version') || + (plugins['version'] != null && plugins['version'] is! String))) { + throw const FormatException( + 'Invalid server distribution plugin loader metadata.', + ); + } + final paths = {}; + final files = []; + for (final raw in rawFiles) { + if (raw is! Map || + raw.keys.any((key) => key != 'path' && key != 'sha256')) { + throw const FormatException( + 'Invalid server distribution file inventory.', + ); + } + final path = text(raw, 'path'); + if (!paths.add(path.toLowerCase())) { + throw FormatException('Duplicate server distribution path: $path'); + } + files.add( + ServerDistributionFile(path: path, sha256: text(raw, 'sha256')), + ); + } + return ServerDistributionManifest._( + slug: text(data, 'slug'), + version: text(data, 'version'), + minecraft: text(data, 'minecraft'), + loaderId: LoaderId(text(loader, 'id')).name, + loaderVersion: loader['version'] as String?, + pluginLoaderId: plugins is Map + ? LoaderId(text(plugins, 'id')).name + : null, + pluginLoaderVersion: plugins is Map + ? plugins['version'] as String? + : null, + files: List.unmodifiable(files), + ); + } + + Map toMap() => { + 'formatVersion': 1, + 'artifactType': serverDistributionArtifactType, + 'slug': slug, + 'version': version, + 'minecraft': minecraft, + 'loader': { + 'id': loaderId, + 'version': loaderVersion, + if (pluginLoaderId != null) + 'plugins': {'id': pluginLoaderId, 'version': pluginLoaderVersion}, + }, + 'runtimeBundled': false, + 'files': files.map((file) => file.toMap()).toList(), + }; +} diff --git a/lib/src/model/manifest/source_eligibility.dart b/lib/src/model/manifest/source_eligibility.dart new file mode 100644 index 0000000..ee9a588 --- /dev/null +++ b/lib/src/model/manifest/source_eligibility.dart @@ -0,0 +1,49 @@ +import 'mods_yaml.dart'; + +/// Single source of truth for "which platforms participate for this +/// entry". Resolves the entry's `sources:` override (if any) against +/// the pack-level `gitrinth.modrinth` / `gitrinth.curseforge` defaults, +/// then filters CurseForge out when the section/loader combination is +/// CF-ineligible (see [sectionAllowsCurseforge]). +/// +/// Returns the set of [SourceKind]s that should attempt resolution. +/// May be empty when both platforms are disabled at the pack level and +/// the entry has no override. +/// +/// Used by: +/// - the Modrinth resolver dispatch gate (`Resolver` / `pubgrub`) +/// - the CurseForge resolver pass +/// - the lockfile builder, when deciding whether to emit a +/// per-platform source block +/// +/// Per-entry hard-restrictions (`url:` / `path:`) are not handled here +/// — those bypass platform resolution entirely. Callers that operate +/// on `kind: resolved` entries should read [ModEntry.source] separately. +Set effectiveSources( + ModsYaml manifest, + ModEntry entry, + Section section, { + LoaderRegistry registry = LoaderRegistry.builtins, +}) { + final base = entry.sources ?? _packDefault(manifest); + if (base.isEmpty) return base; + if (sectionAllowsCurseforge( + section, + manifest.loader.plugins, + registry: registry, + )) { + return base; + } + return base.where((k) => k != SourceKind.curseforge).toSet(); +} + +Set _packDefault(ModsYaml manifest) { + final out = {}; + if (manifest.gitrinth.isPlatformEnabled(SourceKind.modrinth)) { + out.add(SourceKind.modrinth); + } + if (manifest.gitrinth.isPlatformEnabled(SourceKind.curseforge)) { + out.add(SourceKind.curseforge); + } + return out; +} diff --git a/lib/src/model/modrinth/project.dart b/lib/src/model/modrinth/project.dart index 49f9dc1..4e2d369 100644 --- a/lib/src/model/modrinth/project.dart +++ b/lib/src/model/modrinth/project.dart @@ -11,6 +11,7 @@ class Project with ProjectMappable { final String projectType; @MappableField(key: 'loaders') final List loaders; + final List categories; const Project({ required this.id, @@ -18,5 +19,6 @@ class Project with ProjectMappable { required this.title, required this.projectType, this.loaders = const [], + this.categories = const [], }); } diff --git a/lib/src/model/modrinth/project.mapper.dart b/lib/src/model/modrinth/project.mapper.dart index 4aabcf8..0a3e816 100644 --- a/lib/src/model/modrinth/project.mapper.dart +++ b/lib/src/model/modrinth/project.mapper.dart @@ -41,6 +41,13 @@ class ProjectMapper extends ClassMapperBase { opt: true, def: const [], ); + static List _$categories(Project v) => v.categories; + static const Field> _f$categories = Field( + 'categories', + _$categories, + opt: true, + def: const [], + ); @override final MappableFields fields = const { @@ -49,6 +56,7 @@ class ProjectMapper extends ClassMapperBase { #title: _f$title, #projectType: _f$projectType, #loaders: _f$loaders, + #categories: _f$categories, }; static Project _instantiate(DecodingData data) { @@ -58,6 +66,7 @@ class ProjectMapper extends ClassMapperBase { title: data.dec(_f$title), projectType: data.dec(_f$projectType), loaders: data.dec(_f$loaders), + categories: data.dec(_f$categories), ); } @@ -119,12 +128,14 @@ extension ProjectValueCopy<$R, $Out> on ObjectCopyWith<$R, Project, $Out> { abstract class ProjectCopyWith<$R, $In extends Project, $Out> implements ClassCopyWith<$R, $In, $Out> { ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get loaders; + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get categories; $R call({ String? id, String? slug, String? title, String? projectType, List? loaders, + List? categories, }); ProjectCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t); } @@ -145,12 +156,20 @@ class _ProjectCopyWithImpl<$R, $Out> (v) => call(loaders: v), ); @override + ListCopyWith<$R, String, ObjectCopyWith<$R, String, String>> get categories => + ListCopyWith( + $value.categories, + (v, t) => ObjectCopyWith(v, $identity, t), + (v) => call(categories: v), + ); + @override $R call({ String? id, String? slug, String? title, String? projectType, List? loaders, + List? categories, }) => $apply( FieldCopyWithData({ if (id != null) #id: id, @@ -158,6 +177,7 @@ class _ProjectCopyWithImpl<$R, $Out> if (title != null) #title: title, if (projectType != null) #projectType: projectType, if (loaders != null) #loaders: loaders, + if (categories != null) #categories: categories, }), ); @override @@ -167,6 +187,7 @@ class _ProjectCopyWithImpl<$R, $Out> title: data.get(#title, or: $value.title), projectType: data.get(#projectType, or: $value.projectType), loaders: data.get(#loaders, or: $value.loaders), + categories: data.get(#categories, or: $value.categories), ); @override diff --git a/lib/src/model/resolver/modrinth_solver_adapter.dart b/lib/src/model/resolver/modrinth_solver_adapter.dart new file mode 100644 index 0000000..93ea1e7 --- /dev/null +++ b/lib/src/model/resolver/modrinth_solver_adapter.dart @@ -0,0 +1,103 @@ +import 'package:pub_semver/pub_semver.dart'; + +import '../manifest/mods_yaml.dart'; +import '../modrinth/dependency.dart'; +import '../modrinth/version.dart' as modrinth; +import 'constraint.dart'; +import 'version_selection.dart'; +import 'version_solver_types.dart'; + +/// [SolverVersion] implementation that wraps a [modrinth.Version]. +/// Eagerly computes [parsedVersion] via [parseModrinthVersionBestEffort] +/// and projects [dependencies] to required/incompatible edges only — +/// `optional` and `embedded` Modrinth deps are dropped here so the +/// solver's edge handling stays platform-agnostic. +class ModrinthSolverVersion implements SolverVersion { + /// The wrapped Modrinth version. Exposed so the resolver boundary + /// can downcast and unwrap when projecting back to [ResolvedEntry]s. + final modrinth.Version raw; + + @override + final Version parsedVersion; + + @override + final Channel channel; + + @override + final List dependencies; + + ModrinthSolverVersion(this.raw) + : parsedVersion = _safeParse(raw.versionNumber), + channel = _channelFor(raw.versionType), + dependencies = _projectDependencies(raw.dependencies); + + @override + String get id => raw.id; + + @override + String get versionNumber => raw.versionNumber; + + static Version _safeParse(String raw) { + try { + return parseModrinthVersionBestEffort(raw); + } on FormatException { + // Fully-unparseable raws (pure-symbol after sanitization). Use a + // unique placeholder so the solver can still iterate; constraint + // matching will fall through to the candidate filter. + return Version(0, 0, 0, pre: 'unparseable'); + } + } + + static Channel _channelFor(String? versionType) { + switch (versionType) { + case 'release': + return Channel.release; + case 'beta': + return Channel.beta; + case 'alpha': + return Channel.alpha; + default: + return Channel.release; + } + } + + static List _projectDependencies(List deps) { + final out = []; + for (final d in deps) { + final t = switch (d.dependencyType) { + DependencyType.required => SolverDependencyType.required, + DependencyType.incompatible => SolverDependencyType.incompatible, + DependencyType.optional || DependencyType.embedded => null, + }; + if (t == null) continue; + out.add( + SolverDependency( + targetProjectId: d.projectId, + targetVersionId: d.versionId, + type: t, + ), + ); + } + return out; + } +} + +/// Selection-order comparator passed into [PubGrubSolver.selectionOrder]. +/// Delegates to the existing Modrinth comparator for date_published → +/// parsed-semver ordering. Throws `StateError` if either argument +/// isn't a [ModrinthSolverVersion] — the resolver wires only Modrinth +/// adapters into this comparator today. +int compareModrinthSolverOrder(SolverVersion a, SolverVersion b) { + if (a is! ModrinthSolverVersion || b is! ModrinthSolverVersion) { + throw StateError( + 'compareModrinthSolverOrder requires ModrinthSolverVersion, ' + 'got ${a.runtimeType} vs ${b.runtimeType}', + ); + } + return compareModrinthSelectionOrder( + a.raw, + a.parsedVersion, + b.raw, + b.parsedVersion, + ); +} diff --git a/lib/src/model/resolver/pubgrub.dart b/lib/src/model/resolver/pubgrub.dart index c25fb55..af644c6 100644 --- a/lib/src/model/resolver/pubgrub.dart +++ b/lib/src/model/resolver/pubgrub.dart @@ -2,14 +2,19 @@ import 'package:pub_semver/pub_semver.dart'; import '../../cli/exceptions.dart'; import '../manifest/mods_yaml.dart'; -import '../modrinth/dependency.dart'; -import '../modrinth/version.dart' as modrinth; -import 'constraint.dart'; import 'version_selection.dart'; +import 'version_solver_types.dart'; -typedef ListVersions = Future> Function(String slug); +typedef ListVersions = Future> Function(String slug); typedef ResolveSlug = Future Function(String projectId); +/// Comparator: orders [SolverVersion]s "newest first" under the +/// platform's selection rule. Constructor-injected on [PubGrubSolver] +/// so the solver itself stays platform-agnostic — Modrinth callers +/// pass `compareModrinthSolverOrder`, future CurseForge callers pass +/// their own. +typedef SelectionOrder = int Function(SolverVersion a, SolverVersion b); + /// Thrown when the solver can't satisfy the graph after exhausting every /// candidate. Subclass of [ValidationError] so existing callers that catch /// the parent type still work; new callers that want to disable the @@ -23,7 +28,7 @@ class UnsatisfiableGraphError extends ValidationError { UnsatisfiableGraphError(super.message, {required this.conflictingUserSlugs}); } -/// Pure-data root constraint (no Modrinth lookups required). +/// Pure-data root constraint (no platform lookups required). class RootConstraint { final String slug; final VersionConstraint constraint; @@ -56,13 +61,13 @@ class LockSuggestion { /// overridden slug or originate from one, are silently dropped. class OverridePin { final String slug; - final modrinth.Version version; + final SolverVersion version; const OverridePin(this.slug, this.version); } class PubGrubResult { /// slug -> chosen version - final Map decisions; + final Map decisions; /// slug -> true if this slug was added transitively (not in roots). final Map auto; @@ -75,28 +80,35 @@ class PubGrubResult { /// implement a backtracking depth-first search with version-list caching that /// preserves locked decisions where they remain compatible. /// -/// This is sufficient for `gitrinth get`'s MVP scope: typical modpack graphs -/// are small, mod versions usually carry no inter-mod ranges (`required` -/// dependencies are presence-only on Modrinth's side), and conflict cases -/// fall back to a clear error message. +/// Platform-agnostic: operates on [SolverVersion] / [SolverDependency] +/// abstractions so a single solver instance can take Modrinth versions +/// (via `ModrinthSolverVersion`) today and CurseForge versions in the +/// future via the same interface. class PubGrubSolver { final ListVersions listVersions; final ResolveSlug resolveSlugForProjectId; + final Future Function(String projectId, String parentSlug)? + resolveDependencySlug; final List lockSuggestions; final List overridePins; + final Map requiredVersionIds; + final SelectionOrder selectionOrder; /// Selection direction — `get`/`upgrade` pick newest, `downgrade` /// picks oldest. `downgrade` also disables the lock-suggestion head /// promotion below: a downgrade is meant to ignore the existing pin. final SolveType solveType; - final Map> _versionCache = {}; + final Map> _versionCache = {}; PubGrubSolver({ required this.listVersions, required this.resolveSlugForProjectId, + this.resolveDependencySlug, + required this.selectionOrder, this.lockSuggestions = const [], this.overridePins = const [], + this.requiredVersionIds = const {}, this.solveType = SolveType.get, }); @@ -126,21 +138,23 @@ class PubGrubSolver { // - drop ALL incompatible deps (the user takes responsibility // for whatever incompatibilities the override mod declares) for (final dep in pin.version.dependencies) { - if (dep.dependencyType == DependencyType.required) { - final depProjectId = dep.projectId; + if (dep.type == SolverDependencyType.required) { + final depProjectId = dep.targetProjectId; if (depProjectId == null) continue; - final depSlug = await resolveSlugForProjectId(depProjectId); + final depSlug = + await (resolveDependencySlug?.call(depProjectId, pin.slug) ?? + resolveSlugForProjectId(depProjectId)); if (depSlug == null) continue; if (state.overriddenSlugs.contains(depSlug)) continue; final depConstraint = await _constraintFromVersionId( slug: depSlug, - versionId: dep.versionId, + versionId: dep.targetVersionId, ); state.addConstraint(depSlug, depConstraint, parentSlug: pin.slug); state.addChannel(depSlug, Channel.alpha); state.recordIntroducer(depSlug, pin.slug, pin.version.versionNumber); } - // DependencyType.incompatible deps from an override are + // SolverDependencyType.incompatible deps from an override are // silently dropped — both endpoints. See spec case 6. } } @@ -227,7 +241,7 @@ class PubGrubSolver { } } - Future> _versionsFor(String slug) async { + Future> _versionsFor(String slug) async { final cached = _versionCache[slug]; if (cached != null) return cached; final list = await listVersions(slug); @@ -235,18 +249,17 @@ class PubGrubSolver { return list; } - /// Builds the lower-bound [VersionRange] implied by a Modrinth dependency - /// pinned to [versionId] for [slug]. Returns [VersionConstraint.any] when - /// the version isn't found in the listing or its `version_number` doesn't - /// parse as semver — both fall back to the permissive default the solver - /// uses when no pin is present. + /// Builds the lower-bound [VersionRange] implied by a dependency + /// pinned to [versionId] for [slug]. Returns [VersionConstraint.any] + /// when the version isn't found in the listing — which falls back to + /// the permissive default the solver uses when no pin is present. Future _constraintFromVersionId({ required String slug, required String? versionId, }) async { if (versionId == null) return VersionConstraint.any; final versions = await _versionsFor(slug); - modrinth.Version? pinned; + SolverVersion? pinned; for (final dv in versions) { if (dv.id == versionId) { pinned = dv; @@ -254,12 +267,7 @@ class PubGrubSolver { } } if (pinned == null) return VersionConstraint.any; - try { - final floor = parseModrinthVersion(pinned.versionNumber); - return VersionRange(min: floor, includeMin: true); - } on FormatException { - return VersionConstraint.any; - } + return VersionRange(min: pinned.parsedVersion, includeMin: true); } Future _solveStep(_SolverState state) async { @@ -280,7 +288,7 @@ class PubGrubSolver { final slug = picked.key; final constraint = picked.value; final channel = state.channels[slug] ?? Channel.alpha; - final allowed = allowedVersionTypes(channel); + final allowedChannels = _allowedChannels(channel); final allVersions = await _versionsFor(slug); if (allVersions.isEmpty) { @@ -292,25 +300,14 @@ class PubGrubSolver { return false; } - // Collect (parsed_version, modrinth_version) candidates. - final candidates = <_Candidate>[]; + // Collect candidates that pass the channel + constraint filter. + final candidates = []; for (final v in allVersions) { - final vt = v.versionType ?? 'release'; - if (!allowed.contains(vt)) continue; - try { - // Best-effort: truly-weird Modrinth version strings fall back - // to `Version(0.0.0-)` so an exact-pin constraint on - // the same raw string can still match. Pure-symbol versions - // (empty after sanitisation) still throw and get skipped. - final parsed = parseModrinthVersionBestEffort(v.versionNumber); - if (constraint.allows(parsed)) { - candidates.add(_Candidate(parsed, v)); - } - } on FormatException { - // Skip versions we cannot parse — recorded for diagnosis on failure. - state.unparseableVersions - .putIfAbsent(slug, () => []) - .add(v.versionNumber); + final requiredId = requiredVersionIds[slug]; + if (requiredId != null && v.id != requiredId) continue; + if (!allowedChannels.contains(v.channel)) continue; + if (constraint.allows(v.parsedVersion)) { + candidates.add(v); } } if (candidates.isEmpty) { @@ -322,14 +319,7 @@ class PubGrubSolver { return false; } - candidates.sort( - (a, b) => compareModrinthSelectionOrder( - a.modrinthVersion, - a.parsed, - b.modrinthVersion, - b.parsed, - ), - ); + candidates.sort(selectionOrder); if (solveType == SolveType.downgrade) { // Downgrade reverses the candidate order so the oldest matching // version is tried first. Lock-pin promotion still runs below: @@ -345,9 +335,7 @@ class PubGrubSolver { // Lock-suggestion preference: if the current lock pin satisfies, try it first. final pin = state.lockSuggestions[slug]; if (pin != null) { - final pinIdx = candidates.indexWhere( - (c) => c.modrinthVersion.versionNumber == pin, - ); + final pinIdx = candidates.indexWhere((c) => c.versionNumber == pin); if (pinIdx > 0) { final pinned = candidates.removeAt(pinIdx); candidates.insert(0, pinned); @@ -357,18 +345,20 @@ class PubGrubSolver { for (final cand in candidates) { // Snapshot state so we can backtrack. final snapshot = state.snapshot(); - state.decisions[slug] = cand.modrinthVersion; + state.decisions[slug] = cand; var ok = true; // The user-root ancestor of `slug` — used to attribute any // constraint this candidate's deps add on a transitive back to a // user-controllable disable target. Computed once per candidate // because every dep below shares the same ancestor. final ancestor = _userRootAncestorOf(slug, state); - for (final dep in cand.modrinthVersion.dependencies) { - if (dep.dependencyType == DependencyType.required) { - final depProjectId = dep.projectId; + for (final dep in cand.dependencies) { + if (dep.type == SolverDependencyType.required) { + final depProjectId = dep.targetProjectId; if (depProjectId == null) continue; - final depSlug = await resolveSlugForProjectId(depProjectId); + final depSlug = + await (resolveDependencySlug?.call(depProjectId, slug) ?? + resolveSlugForProjectId(depProjectId)); if (depSlug == null) { // Cannot resolve transitive dependency to a slug — skip. // The user can pin it explicitly if desired. @@ -378,13 +368,13 @@ class PubGrubSolver { // from any other source on them are silently dropped (spec // cases 4 and 5). if (state.overriddenSlugs.contains(depSlug)) continue; - // A `version_id` on the dep is a lower-bound hint: Modrinth - // mods don't declare upper compatibility bounds, so two + // A `version_id` on the dep is a lower-bound hint: mods + // don't declare upper compatibility bounds, so two // parents pinning across majors should resolve to the higher // floor instead of conflicting. final depConstraint = await _constraintFromVersionId( slug: depSlug, - versionId: dep.versionId, + versionId: dep.targetVersionId, ); state.addConstraint(depSlug, depConstraint, parentSlug: ancestor); // Transitive deps inherit the permissive default (all @@ -395,15 +385,13 @@ class PubGrubSolver { // Record who pulled this transitive in so failure messages // can narrate "every depends on " — backtracking // restores the previous introducer if this candidate is abandoned. - state.recordIntroducer( - depSlug, - slug, - cand.modrinthVersion.versionNumber, - ); - } else if (dep.dependencyType == DependencyType.incompatible) { - final depProjectId = dep.projectId; + state.recordIntroducer(depSlug, slug, cand.versionNumber); + } else if (dep.type == SolverDependencyType.incompatible) { + final depProjectId = dep.targetProjectId; if (depProjectId == null) continue; - final depSlug = await resolveSlugForProjectId(depProjectId); + final depSlug = + await (resolveDependencySlug?.call(depProjectId, slug) ?? + resolveSlugForProjectId(depProjectId)); if (depSlug == null) continue; // Spec case 3: incompatible edges that target an override // slug are silently dropped. (Edges originating from an @@ -417,7 +405,7 @@ class PubGrubSolver { ok = false; state.recordSpecificFailure( slug, - '$slug ${cand.modrinthVersion.versionNumber} is ' + '$slug ${cand.versionNumber} is ' 'incompatible with $depSlug, which is already in the modpack', counterpartSlug: depSlug, ); @@ -472,10 +460,20 @@ class PubGrubSolver { } } -class _Candidate { - final Version parsed; - final modrinth.Version modrinthVersion; - const _Candidate(this.parsed, this.modrinthVersion); +/// Channels admissible under [floor]: `release` floor admits only +/// `release`, `beta` admits release+beta, `alpha` admits everything. +/// Mirrors `allowedVersionTypes` but operates on the [Channel] enum +/// directly so the solver doesn't have to know platform-specific +/// version-type strings. +Set _allowedChannels(Channel floor) { + switch (floor) { + case Channel.release: + return const {Channel.release}; + case Channel.beta: + return const {Channel.release, Channel.beta}; + case Channel.alpha: + return const {Channel.release, Channel.beta, Channel.alpha}; + } } /// Records who introduced a transitive constraint on a slug. Used at @@ -520,7 +518,7 @@ class _Failure { class _SolverState { final Map constraints = {}; final Map channels = {}; - final Map decisions = {}; + final Map decisions = {}; final Set incompatibleSlugs = {}; /// Slugs decided at solve-start by an [OverridePin]. The solver @@ -533,7 +531,7 @@ class _SolverState { /// Snapshot of override decisions taken at solve-start, used by /// [restore] to re-seat them after the search loop backtracks. /// Never mutated after `solve` populates it. - final Map initialOverrideDecisions = {}; + final Map initialOverrideDecisions = {}; /// `incompatibleBy[depSlug] = parentSlug`: the slug whose dep loop /// added `depSlug` to [incompatibleSlugs]. Used at conflict-detection @@ -550,7 +548,6 @@ class _SolverState { final Map introducers = {}; final Map lockSuggestions; final Set userSlugs; - final Map> unparseableVersions = {}; /// Real-cause failures collected across the entire solve. Not part of /// snapshot/restore — failures persist past backtracking so we still @@ -665,15 +662,6 @@ class _SolverState { } buf.writeln('Version solving failed.'); } - if (unparseableVersions.isNotEmpty) { - buf.writeln( - '\nNote: some Modrinth version strings could not be parsed and were skipped:', - ); - unparseableVersions.forEach((slug, versions) { - buf.writeln(' - $slug: ${versions.join(', ')}'); - }); - buf.writeln('Pin an exact version in mods.yaml as a workaround.'); - } return buf.toString().trimRight(); } @@ -755,7 +743,7 @@ class _SolverState { class _Snapshot { final Map constraints; final Map channels; - final Map decisions; + final Map decisions; final Set incompatibleSlugs; final Map incompatibleBy; final Map introducers; diff --git a/lib/src/model/resolver/resolver.dart b/lib/src/model/resolver/resolver.dart index ac1faef..ac8857e 100644 --- a/lib/src/model/resolver/resolver.dart +++ b/lib/src/model/resolver/resolver.dart @@ -1,10 +1,13 @@ import '../../cli/exceptions.dart'; import '../manifest/mods_lock.dart'; import '../manifest/mods_yaml.dart'; +import '../modrinth/version.dart' as modrinth; import 'constraint.dart'; +import 'modrinth_solver_adapter.dart'; import 'pubgrub.dart'; import 'result.dart'; import 'version_selection.dart'; +import 'version_solver_types.dart'; /// Resolves the merged [manifest] into a [ResolutionResult] using the supplied /// version-listing callback. Pure: no service/ imports. @@ -16,9 +19,27 @@ import 'version_selection.dart'; /// dependencies' Modrinth project IDs back to slugs. If a transitive dep /// cannot be resolved to a slug (returns null), it is skipped — it will not /// participate in conflict resolution. +/// +/// [modrinthSlugs] (if non-null) restricts which user-declared slugs +/// participate in the Modrinth resolution roots. Slugs absent from the +/// set are skipped — they're handled by another platform's resolver +/// (today, only the CurseForge resolver). Transitive deps pulled in +/// by required-dep edges are unaffected. Pass null to include every +/// `ModrinthEntrySource` entry (legacy behavior). +/// +/// The PubGrub solver underneath operates on a platform-agnostic +/// [SolverVersion] surface; [Resolver] only ever feeds it +/// [ModrinthSolverVersion] adapters today and unwraps decisions back +/// to Modrinth versions at the result boundary. class Resolver { final ListVersions listVersions; final ResolveSlug resolveSlugForProjectId; + final Future Function(String projectId, String parentSlug)? + resolveDependencySlug; + final Section Function(String slug)? sectionForSlug; + final String Function(Section section, String slug)? nodeKeyFor; + final String Function(String key)? localNameFor; + final String Function(String message)? formatError; /// Selection direction passed through to [PubGrubSolver]. Defaults to /// [SolveType.get] so existing callers keep newest-first behavior. @@ -27,6 +48,11 @@ class Resolver { Resolver({ required this.listVersions, required this.resolveSlugForProjectId, + this.resolveDependencySlug, + this.sectionForSlug, + this.nodeKeyFor, + this.localNameFor, + this.formatError, this.solveType = SolveType.get, }); @@ -34,6 +60,8 @@ class Resolver { ModsYaml manifest, { ModsLock? existingLock, Map overridePins = const {}, + Set? modrinthSlugs, + Map requiredVersionIds = const {}, }) async { final roots = []; final entryBySection = {}; @@ -52,19 +80,27 @@ class Resolver { // until the user re-runs `migrate` / `upgrade --major-versions`. return; } + final key = nodeKeyFor?.call(section, slug) ?? slug; + if (modrinthSlugs != null && !modrinthSlugs.contains(key)) { + // Dispatch gate: the entry's effective sources exclude + // Modrinth (e.g. `sources: [curseforge]` or pack-default + // disable). Skip it here — the CurseForge resolver pass + // handles it. + return; + } // The lock builder still needs to file an overridden slug // under the right section even though no RootConstraint is // built for it (the override pin pre-decides the version). - entryBySection[slug] = section; - entryBySlug[slug] = entry; - if (overridePins.containsKey(slug)) return; + entryBySection[key] = section; + entryBySlug[key] = entry; + if (overridePins.containsKey(key)) return; // Permissive default: an entry with no explicit channel accepts // every Modrinth `version_type`. Users narrow to `release`/`beta` // only when they want a stricter stability floor. final effectiveChannel = entry.channel ?? Channel.alpha; roots.add( RootConstraint( - slug: slug, + slug: key, constraint: parseConstraint(entry.constraintRaw), channel: effectiveChannel, isUserDeclared: true, @@ -82,7 +118,11 @@ class Resolver { for (final section in Section.values) { existingLock.sectionFor(section).forEach((slug, locked) { final v = locked.version; - if (v != null) pins.add(LockSuggestion(slug, v)); + if (v != null) { + pins.add( + LockSuggestion(nodeKeyFor?.call(section, slug) ?? slug, v), + ); + } }); } } @@ -90,25 +130,51 @@ class Resolver { final solver = PubGrubSolver( listVersions: listVersions, resolveSlugForProjectId: resolveSlugForProjectId, + resolveDependencySlug: resolveDependencySlug, + selectionOrder: compareModrinthSolverOrder, lockSuggestions: pins, overridePins: overridePins.values.toList(), + requiredVersionIds: requiredVersionIds, solveType: solveType, ); - final result = await solver.solve(roots); + final PubGrubResult result; + try { + result = await solver.solve(roots); + } on UnsatisfiableGraphError catch (error) { + throw UnsatisfiableGraphError( + formatError?.call(error.message) ?? error.message, + conflictingUserSlugs: { + for (final key in error.conflictingUserSlugs) + localNameFor?.call(key) ?? key, + }, + ); + } on ValidationError catch (error) { + throw ValidationError(formatError?.call(error.message) ?? error.message); + } final entries = []; final sortedDecisions = result.decisions.entries.toList() ..sort((a, b) => a.key.compareTo(b.key)); for (final decision in sortedDecisions) { final slug = decision.key; - final v = decision.value; + final solverV = decision.value; + // `Resolver` only ever feeds Modrinth adapters into the solver; + // any other adapter type at this boundary is a bug in the caller. + if (solverV is! ModrinthSolverVersion) { + throw StateError( + 'Resolver expected ModrinthSolverVersion for $slug, got ' + '${solverV.runtimeType}', + ); + } + final v = solverV.raw; final file = v.primaryFile; if (file == null) { throw ValidationError( - 'Resolved $slug version ${v.versionNumber} has no downloadable file.', + 'Resolved ${localNameFor?.call(slug) ?? slug} version ${v.versionNumber} has no downloadable file.', ); } - final section = entryBySection[slug] ?? Section.mods; + final section = + entryBySection[slug] ?? sectionForSlug?.call(slug) ?? Section.mods; final declared = entryBySlug[slug]; final defaults = defaultSidesFor(section); final client = declared?.client ?? defaults.client; @@ -118,7 +184,7 @@ class Resolver { : LockedDependencyKind.direct; entries.add( ResolvedEntry( - slug: slug, + slug: localNameFor?.call(slug) ?? slug, section: section, client: client, server: server, @@ -131,3 +197,16 @@ class Resolver { return ResolutionResult(entries); } } + +/// Type alias for callers that build a Modrinth-typed list-versions +/// callback and want to feed it into [Resolver]. Wraps each +/// [modrinth.Version] in a [ModrinthSolverVersion] before handing off +/// to PubGrub. +ListVersions modrinthListVersionsAdapter( + Future> Function(String slug) underlying, +) { + return (slug) async { + final mr = await underlying(slug); + return mr.map(ModrinthSolverVersion.new).toList(); + }; +} diff --git a/lib/src/model/resolver/version_solver_types.dart b/lib/src/model/resolver/version_solver_types.dart new file mode 100644 index 0000000..ce9f9bb --- /dev/null +++ b/lib/src/model/resolver/version_solver_types.dart @@ -0,0 +1,56 @@ +import 'package:pub_semver/pub_semver.dart'; + +import '../manifest/mods_yaml.dart'; + +/// Platform-agnostic dependency-edge classification consumed by the +/// solver. Modrinth's `optional` / `embedded` are dropped at adapter +/// time — they have no effect on resolution. +enum SolverDependencyType { required, incompatible } + +/// Platform-agnostic dependency edge. Adapter implementations populate +/// the relevant identifier field (`targetSlug` for direct slug pins, +/// `targetProjectId` for indirect lookups via +/// `resolveSlugForProjectId`). `targetVersionId` (Modrinth-style +/// floor hint) stays optional and may be null on platforms where +/// version-id pinning isn't expressible. +class SolverDependency { + final String? targetSlug; + final String? targetProjectId; + final String? targetVersionId; + final SolverDependencyType type; + + const SolverDependency({ + this.targetSlug, + this.targetProjectId, + this.targetVersionId, + required this.type, + }); +} + +/// Platform-agnostic version surface consumed by [PubGrubSolver]. +/// One adapter implementation lives per platform — see +/// `modrinth_solver_adapter.dart` for the Modrinth wrapper. +abstract interface class SolverVersion { + /// Stable per-platform version identifier (Modrinth `id`, CurseForge + /// `fileId.toString()`). Used for `targetVersionId` cross-references + /// and for platform-specific debugging. + String get id; + + /// Raw version-number string as published by the platform. Used for + /// lock-suggestion equality checks and failure messages — the solver + /// does no parsing on it directly. + String get versionNumber; + + /// Pre-parsed semver of [versionNumber]. Adapters compute this once + /// (best-effort, falling back to `0.0.0-`) so the solver + /// can match constraints without re-parsing. + Version get parsedVersion; + + /// Stability channel (release/beta/alpha) used for the channel-floor + /// filter inside the solver. + Channel get channel; + + /// Edges to other slugs (required + incompatible only). Optional and + /// embedded deps are dropped at adapter time. + List get dependencies; +} diff --git a/lib/src/service/artifact_locator.dart b/lib/src/service/artifact_locator.dart new file mode 100644 index 0000000..c732982 --- /dev/null +++ b/lib/src/service/artifact_locator.dart @@ -0,0 +1,77 @@ +import 'package:path/path.dart' as p; +import '../cli/exceptions.dart'; +import '../model/manifest/mods_lock.dart'; +import 'cache.dart'; + +String resolveSourcePath( + GitrinthCache cache, + LockedEntry entry, { + required String projectDir, + required String modrinthHost, +}) => ArtifactLocator( + cache: cache, + projectDir: projectDir, + defaultModrinthHost: modrinthHost, +).preferred(entry); + +/// Cache and project locations shared by fetch, build, and loose overrides. +/// A missing locked host means the global source default, never a pack host. +final class ArtifactLocator { + final GitrinthCache cache; + final String projectDir; + final String defaultModrinthHost; + const ArtifactLocator({ + required this.cache, + required this.projectDir, + required this.defaultModrinthHost, + }); + String preferred(LockedEntry entry) { + switch (entry.kind) { + case LockedEntryKind.resolved: + final mr = entry.modrinth; + if (mr != null) { + return modrinth(mr); + } + final cf = entry.curseforge; + if (cf != null) { + return curseforge(cf); + } + throw ValidationError( + 'lockfile entry "${entry.slug}" has no source block to fetch.', + ); + case LockedEntryKind.url: + final file = entry.urlFile; + if (file == null) { + throw ValidationError( + 'lockfile entry "${entry.slug}" is missing url source fields.', + ); + } + return url(entry.slug, file); + case LockedEntryKind.path: + final raw = entry.path; + if (raw == null) { + throw ValidationError( + 'lockfile entry "${entry.slug}" is missing path source fields.', + ); + } + return path(raw); + } + } + + String modrinth(LockedModrinthSource source) => cache.modrinthPath( + host: source.host ?? defaultModrinthHost, + projectId: source.projectId, + versionId: source.versionId, + filename: source.file.name, + ); + String curseforge(LockedCurseforgeSource source) => cache.curseforgePath( + projectId: source.projectId, + fileId: source.fileId, + filename: source.file.name, + ); + String url(String slug, LockedFile file) => file.sha512 == null + ? cache.unverifiedUrlPath(slug, file.name) + : cache.urlPath(sha512: file.sha512!, filename: file.name); + String path(String raw) => + p.isAbsolute(raw) ? raw : p.normalize(p.join(projectDir, raw)); +} diff --git a/lib/src/service/cache.dart b/lib/src/service/cache.dart index 7abc4d0..4a1b4bd 100644 --- a/lib/src/service/cache.dart +++ b/lib/src/service/cache.dart @@ -1,6 +1,5 @@ import 'dart:convert'; import 'dart:io'; -import 'dart:typed_data'; import 'package:crypto/crypto.dart'; import 'package:path/path.dart' as p; @@ -9,6 +8,8 @@ import '../cli/exceptions.dart'; import '../model/manifest/mods_yaml.dart'; import '../model/modrinth/version.dart' as modrinth; import 'console.dart'; +import 'file_digest.dart'; +import 'modrinth/modrinth_url.dart'; class GitrinthCache { final String root; @@ -18,6 +19,7 @@ class GitrinthCache { : _console = console ?? const Console(); String get modrinthRoot => p.normalize(p.join(root, 'modrinth')); + String get curseforgeRoot => p.normalize(p.join(root, 'curseforge')); String get urlRoot => p.normalize(p.join(root, 'url')); String get tmpRoot => p.normalize(p.join(root, 'tmp')); String get loadersRoot => p.normalize(p.join(root, 'loaders')); @@ -73,6 +75,12 @@ class GitrinthCache { required String modLoaderVersion, required String filename, }) { + if (!isSafeRuntimeToken(mcVersion) || + !isSafeRuntimeToken(modLoaderVersion)) { + throw const ValidationError( + 'Unsafe runtime version in loader cache path.', + ); + } return p.join( loadersRoot, loader.name, @@ -97,33 +105,118 @@ class GitrinthCache { return p.join(pluginServersRoot, artifactKey, mcVersion, version, filename); } - /// Path where a Modrinth-sourced artifact should live. + /// Filesystem-safe segment of [hostUrl] used to scope cached Modrinth + /// artifacts per-host. Built from [normalizeServerKey] so the same + /// physical host always maps to the same segment regardless of how + /// callers spell its URL. Strips the `scheme://` prefix and replaces + /// path separators with `_`. The default modrinth.com host renders as + /// `api.modrinth.com_v2`, giving every host (including the default) + /// a stable cache segment. + static String hostCacheSegment(String hostUrl) { + final key = (() { + try { + return normalizeServerKey(hostUrl); + } on FormatException { + return hostUrl; + } + })(); + final stripped = key.replaceFirst( + RegExp(r'^[a-zA-Z][a-zA-Z0-9+.-]*://'), + '', + ); + return stripped + .replaceAll('/', '_') + .replaceAll('\\', '_') + .replaceAll(':', '_'); + } + + /// Path where a Modrinth-sourced artifact should live for [host]. + /// Layout: `////`. String modrinthPath({ + required String host, required String projectId, required String versionId, required String filename, }) { - return p.join(modrinthRoot, projectId, versionId, filename); + return p.join( + modrinthRoot, + hostCacheSegment(host), + projectId, + versionId, + filename, + ); } + /// Path where a CurseForge-sourced artifact lives. Layout: + /// `///`. The layout has + /// no host segment because CurseForge project/file IDs are canonical + /// even when `GITRINTH_CURSEFORGE_URL` points at a staging or proxy + /// read-API host. + String curseforgePath({ + required int projectId, + required int fileId, + required String filename, + }) { + final destination = p.join( + curseforgeRoot, + projectId.toString(), + fileId.toString(), + filename, + ); + final metadata = curseforgeMetadataPath( + projectId: projectId, + fileId: fileId, + ); + if (p.equals(p.normalize(destination), metadata) || + p.equals(p.normalize(destination), '$metadata.lock')) { + throw ValidationError( + 'CurseForge project $projectId, file $fileId uses reserved artifact filename $filename. Use a path: source with a different filename.', + ); + } + return destination; + } + + /// Selected-file relations and verification metadata, separate from bytes. + String curseforgeMetadataPath({ + required int projectId, + required int fileId, + }) => p.join( + curseforgeRoot, + projectId.toString(), + fileId.toString(), + 'metadata.json', + ); + /// Path where the resolved Modrinth `Version`'s metadata (mainly its /// `dependencies` array) is persisted alongside the artifact. Mirrors /// dart pub's "graph in cache, not lock" architecture — this file is /// the source of truth for `gitrinth upgrade --unlock-transitive`'s /// transitive-closure walk. String modrinthVersionMetadataPath({ + required String host, required String projectId, required String versionId, }) { - return p.join(modrinthRoot, projectId, versionId, 'version.json'); + return p.join( + modrinthRoot, + hostCacheSegment(host), + projectId, + versionId, + 'version.json', + ); } - /// Walks `//*/version.json` and yields each - /// successfully-parsed [modrinth.Version]. Malformed sidecars are skipped - /// with a stderr warning so a single corrupt file doesn't abort an - /// `--offline` resolve. - Iterable listCachedVersions(String projectId) sync* { - final dir = Directory(p.join(modrinthRoot, projectId)); + /// Walks `///*/version.json` and + /// yields each successfully-parsed [modrinth.Version]. Malformed + /// sidecars are skipped with a stderr warning so a single corrupt + /// file doesn't abort an `--offline` resolve. + Iterable listCachedVersions( + String host, + String projectId, + ) sync* { + final dir = Directory( + p.join(modrinthRoot, hostCacheSegment(host), projectId), + ); if (!dir.existsSync()) return; for (final entry in dir.listSync()) { if (entry is! Directory) continue; @@ -169,8 +262,7 @@ class GitrinthCache { /// Verifies the contents of [file] against [expectedSha512]. static Future verifyFileSha512(File file, String expectedSha512) async { - final bytes = await file.readAsBytes(); - verifySha512(Uint8List.fromList(bytes), expectedSha512); + await verifyFileDigest(file, sha512, expectedSha512); } /// Verifies [bytes] against [expectedSha256] (case-insensitive). Throws @@ -186,8 +278,7 @@ class GitrinthCache { /// Verifies the contents of [file] against [expectedSha256]. static Future verifyFileSha256(File file, String expectedSha256) async { - final bytes = await file.readAsBytes(); - verifySha256(Uint8List.fromList(bytes), expectedSha256); + await verifyFileDigest(file, sha256, expectedSha256); } /// Verifies [bytes] against [expectedSha1] (case-insensitive). Throws @@ -202,7 +293,6 @@ class GitrinthCache { /// Verifies the contents of [file] against [expectedSha1]. static Future verifyFileSha1(File file, String expectedSha1) async { - final bytes = await file.readAsBytes(); - verifySha1(Uint8List.fromList(bytes), expectedSha1); + await verifyFileDigest(file, sha1, expectedSha1); } } diff --git a/lib/src/service/cache_inspector.dart b/lib/src/service/cache_inspector.dart index 2e579b9..46ab057 100644 --- a/lib/src/service/cache_inspector.dart +++ b/lib/src/service/cache_inspector.dart @@ -18,6 +18,10 @@ class CachedArtifact { final String filename; final int size; + /// Modrinth-only. Filesystem-safe segment of the host this artifact + /// was fetched from (`api.modrinth.com_v2`, etc.). + final String? hostSegment; + /// Modrinth-only. final String? projectId; final String? versionId; @@ -30,6 +34,7 @@ class CachedArtifact { required this.location, required this.filename, required this.size, + this.hostSegment, this.projectId, this.versionId, this.sha512, @@ -38,6 +43,7 @@ class CachedArtifact { Map toJson() { return { 'source': source, + if (hostSegment != null) 'host': hostSegment, if (projectId != null) 'projectId': projectId, if (versionId != null) 'versionId': versionId, if (sha512 != null) 'sha512': sha512, @@ -81,14 +87,16 @@ class CacheInspector { if (filename == 'version.json') continue; final rel = p.relative(entity.path, from: cache.modrinthRoot); final parts = p.split(rel); - if (parts.length < 3) continue; + // New layout: ///. + if (parts.length < 4) continue; yield CachedArtifact( source: 'modrinth', location: entity.path, filename: filename, size: entity.lengthSync(), - projectId: parts[0], - versionId: parts[1], + hostSegment: parts[0], + projectId: parts[1], + versionId: parts[2], ); } } @@ -221,10 +229,10 @@ class CacheInspector { ); return _ModrinthRepair.skippedOrphan; } - final metaPath = cache.modrinthVersionMetadataPath( - projectId: projectId, - versionId: versionId, - ); + // Metadata lives next to the artifact in the same versioned dir. + // Compute it relative to the artifact rather than re-deriving the + // host from the segment. + final metaPath = p.join(p.dirname(artifact.location), 'version.json'); final metaFile = File(metaPath); if (!metaFile.existsSync()) { console.warn( diff --git a/lib/src/service/curseforge/curseforge_api.dart b/lib/src/service/curseforge/curseforge_api.dart new file mode 100644 index 0000000..0cc672b --- /dev/null +++ b/lib/src/service/curseforge/curseforge_api.dart @@ -0,0 +1,63 @@ +import 'package:dio/dio.dart'; +import 'package:retrofit/retrofit.dart'; + +import '../../model/curseforge/cf_search_response.dart'; + +part 'curseforge_api.g.dart'; + +/// Marker key on `RequestOptions.extra` that opts a request into +/// CurseForge auth. When `extra[kCurseForgeAuthRequired] == true`, +/// [CurseForgeAuthInterceptor] resolves and attaches the +/// `x-api-key` header. Every CurseForge endpoint requires auth, so all +/// retrofit methods set the marker — but the marker is exposed so a raw +/// `dio.get` caller can opt in too. +const String kCurseForgeAuthRequired = 'gitrinth.cf.auth'; + +@RestApi(parser: Parser.DartMappable) +abstract class CurseForgeApi { + factory CurseForgeApi(Dio dio, {String baseUrl}) = _CurseForgeApi; + + /// Fetches a single mod by its numeric project ID. + @GET('/v1/mods/{id}') + @Extra({kCurseForgeAuthRequired: true}) + Future getMod(@Path('id') int id); + + /// Searches for mods on [gameId] (Minecraft is 432). [classId] scopes + /// the result to a content type (mods=6, plugins=5, etc. — see + /// `cf_constants.dart`). [slug] does an exact slug match; pass + /// [searchFilter] for a substring search instead. + @GET('/v1/mods/search') + @Extra({kCurseForgeAuthRequired: true}) + Future searchMods({ + @Query('gameId') required int gameId, + @Query('classId') int? classId, + @Query('slug') String? slug, + @Query('searchFilter') String? searchFilter, + @Query('index') int? index, + @Query('pageSize') int? pageSize, + }); + + /// Lists the files attached to a mod project. + /// + /// CurseForge's `gameVersion` query parameter only accepts a single + /// value — packs that declare `accepts_mc:` need to filter + /// client-side, which `listCompatibleFiles` does on top of this raw + /// call. + @GET('/v1/mods/{modId}/files') + @Extra({kCurseForgeAuthRequired: true}) + Future listFiles( + @Path('modId') int modId, { + @Query('gameVersion') String? gameVersion, + @Query('modLoaderType') int? modLoaderType, + @Query('index') int? index, + @Query('pageSize') int? pageSize, + }); + + /// Fetches a specific file by its `(modId, fileId)` pair. + @GET('/v1/mods/{modId}/files/{fileId}') + @Extra({kCurseForgeAuthRequired: true}) + Future getFile( + @Path('modId') int modId, + @Path('fileId') int fileId, + ); +} diff --git a/lib/src/service/curseforge/curseforge_api.g.dart b/lib/src/service/curseforge/curseforge_api.g.dart new file mode 100644 index 0000000..8791598 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_api.g.dart @@ -0,0 +1,185 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'curseforge_api.dart'; + +// dart format off + +// ************************************************************************** +// RetrofitGenerator +// ************************************************************************** + +// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers,unused_element,unnecessary_string_interpolations,unused_element_parameter,avoid_unused_constructor_parameters,unreachable_from_main + +class _CurseForgeApi implements CurseForgeApi { + _CurseForgeApi(this._dio, {this.baseUrl, this.errorLogger}); + + final Dio _dio; + + String? baseUrl; + + final ParseErrorLogger? errorLogger; + + @override + Future getMod(int id) async { + final _extra = {'gitrinth.cf.auth': true}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/v1/mods/${id}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch>(_options); + late ModEnvelope _value; + try { + _value = ModEnvelopeMapper.fromMap(_result.data!); + } on Object catch (e, s) { + errorLogger?.logError(e, s, _options, response: _result); + rethrow; + } + return _value; + } + + @override + Future searchMods({ + required int gameId, + int? classId, + String? slug, + String? searchFilter, + int? index, + int? pageSize, + }) async { + final _extra = {'gitrinth.cf.auth': true}; + final queryParameters = { + r'gameId': gameId, + r'classId': classId, + r'slug': slug, + r'searchFilter': searchFilter, + r'index': index, + r'pageSize': pageSize, + }; + queryParameters.removeWhere((k, v) => v == null); + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/v1/mods/search', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch>(_options); + late ModSearchResponse _value; + try { + _value = ModSearchResponseMapper.fromMap(_result.data!); + } on Object catch (e, s) { + errorLogger?.logError(e, s, _options, response: _result); + rethrow; + } + return _value; + } + + @override + Future listFiles( + int modId, { + String? gameVersion, + int? modLoaderType, + int? index, + int? pageSize, + }) async { + final _extra = {'gitrinth.cf.auth': true}; + final queryParameters = { + r'gameVersion': gameVersion, + r'modLoaderType': modLoaderType, + r'index': index, + r'pageSize': pageSize, + }; + queryParameters.removeWhere((k, v) => v == null); + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/v1/mods/${modId}/files', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch>(_options); + late ModFileSearchResponse _value; + try { + _value = ModFileSearchResponseMapper.fromMap(_result.data!); + } on Object catch (e, s) { + errorLogger?.logError(e, s, _options, response: _result); + rethrow; + } + return _value; + } + + @override + Future getFile(int modId, int fileId) async { + final _extra = {'gitrinth.cf.auth': true}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/v1/mods/${modId}/files/${fileId}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch>(_options); + late ModFileEnvelope _value; + try { + _value = ModFileEnvelopeMapper.fromMap(_result.data!); + } on Object catch (e, s) { + errorLogger?.logError(e, s, _options, response: _result); + rethrow; + } + return _value; + } + + RequestOptions _setStreamType(RequestOptions requestOptions) { + if (T != dynamic && + !(requestOptions.responseType == ResponseType.bytes || + requestOptions.responseType == ResponseType.stream)) { + if (T == String) { + requestOptions.responseType = ResponseType.plain; + } else { + requestOptions.responseType = ResponseType.json; + } + } + return requestOptions; + } + + String _combineBaseUrls(String dioBaseUrl, String? baseUrl) { + if (baseUrl == null || baseUrl.trim().isEmpty) { + return dioBaseUrl; + } + + final url = Uri.parse(baseUrl); + + if (url.isAbsolute) { + return url.toString(); + } + + return Uri.parse(dioBaseUrl).resolveUri(url).toString(); + } +} + +// dart format on diff --git a/lib/src/service/curseforge/curseforge_api_factory.dart b/lib/src/service/curseforge/curseforge_api_factory.dart new file mode 100644 index 0000000..b4f6dc6 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_api_factory.dart @@ -0,0 +1,190 @@ +import 'package:dio/dio.dart'; + +import '../../cli/exceptions.dart'; +import '../../version.dart'; +import '../console.dart'; +import '../offline_guard_interceptor.dart'; +import 'curseforge_api.dart'; +import 'curseforge_auth_interceptor.dart'; +import 'curseforge_error_interceptor.dart'; +import 'curseforge_rate_limit_interceptor.dart'; + +/// Builds and caches a single [CurseForgeApi] (with its own [Dio] + +/// reactive [CurseForgeRateLimitInterceptor]) for the CurseForge read +/// API. +/// +/// Unlike [ModrinthApiFactory], CurseForge has one canonical host so the +/// factory caches a single client bundle rather than a host-keyed map. +class CurseForgeApiFactory { + final Console _console; + final CurseForgeAuthInterceptor _auth; + final bool Function() _offline; + final String baseUrl; + + _Bundle? _bundle; + + CurseForgeApiFactory({ + required Console console, + required CurseForgeAuthInterceptor auth, + required bool Function() offline, + required this.baseUrl, + }) : _console = console, + _auth = auth, + _offline = offline; + + /// Returns the cached [CurseForgeApi], building one on first access. + CurseForgeApi get api { + final cached = _bundle; + if (cached != null) return cached.api; + final dio = Dio(); + dio + ..interceptors.add(OfflineGuardInterceptor(_offline)) + ..interceptors.add(_auth) + ..interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + baseUrl: baseUrl, + console: _console, + ), + ) + ..interceptors.add(CurseForgeErrorInterceptor()) + ..options.headers['User-Agent'] = + 'gitrinth/$packageVersion (+github.com/getBoolean/gitrinth)'; + final cf = CurseForgeApi(dio, baseUrl: baseUrl); + _bundle = _Bundle(dio: dio, api: cf); + return cf; + } + + /// Raw provider catalog retains numeric loader types and version names. + Future>> listMinecraftLoaders() async { + api; // Initialize the same authenticated bundle used by resolution. + final client = dio; + if (client == null) { + throw StateError('CurseForge bundle was not initialized.'); + } + final response = await client.get( + '$baseUrl/v1/minecraft/modloader', + options: Options(extra: const {kCurseForgeAuthRequired: true}), + ); + final raw = response.data; + final data = raw is Map ? raw['data'] : null; + // A malformed upstream catalog is invalid provider data, not a usage + // error, and must never be cached. + if (data is! List || data.any((entry) => entry is! Map)) { + throw const ValidationError( + 'CurseForge loader discovery returned no valid catalog.', + ); + } + return [for (final entry in data) Map.from(entry as Map)]; + } + + /// Returns the underlying [Dio] for the cached bundle. Visible for + /// tests that need to inspect interceptor wiring. + Dio? get dio => _bundle?.dio; + + /// Publication verification retains additional-file/server-pack fields. + Future> publicationFile( + int projectId, + int fileId, + ) async { + final response = await _publicationGet('/v1/mods/$projectId/files/$fileId'); + final data = response['data']; + if (data is! Map) { + throw const ValidationError( + 'CurseForge publication file must be an object.', + ); + } + return Map.from(data); + } + + Future>> publicationFiles(int projectId) async { + final files = >[]; + var index = 0; + while (index < 10000) { + final remaining = 10000 - index; + final response = await _publicationGet( + '/v1/mods/$projectId/files', + query: {'index': index, 'pageSize': remaining < 50 ? remaining : 50}, + ); + final data = response['data']; + final pagination = response['pagination']; + final total = pagination is Map ? pagination['totalCount'] : null; + if (data is! List || total is! int || total > 10000) { + throw const ValidationError( + 'Cannot reconcile an incomplete CurseForge file history.', + ); + } + files.addAll( + data.map((entry) => Map.from(entry as Map)), + ); + index += data.length; + if (index >= total) return files; + if (data.isEmpty) break; + } + throw const ValidationError( + 'Cannot reconcile an incomplete CurseForge file history.', + ); + } + + Future> _publicationGet( + String path, { + Map? query, + }) async { + api; + final client = dio; + if (client == null) { + throw StateError('CurseForge bundle was not initialized.'); + } + final Response response; + final cancellation = CancelToken(); + final configured = client.options.receiveTimeout; + final deadline = configured != null && configured > Duration.zero + ? configured + : const Duration(seconds: 30); + try { + response = await client + .get( + '$baseUrl$path', + queryParameters: query, + options: Options(extra: const {kCurseForgeAuthRequired: true}), + cancelToken: cancellation, + ) + .timeout( + deadline, + onTimeout: () { + cancellation.cancel( + 'Publication verification deadline exceeded.', + ); + throw const UserError( + 'CurseForge publication verification timed out. Uploaded files remain recorded; retry verification after read access recovers.', + ); + }, + ); + } on DioException catch (e) { + final cause = e.error; + if (cause is GitrinthException) throw cause; + throw UserError( + 'CurseForge publication verification failed (HTTP ${e.response?.statusCode ?? "unavailable"}).', + ); + } + if (response.data is! Map) { + throw const ValidationError( + 'Invalid CurseForge publication verification response.', + ); + } + return Map.from(response.data as Map); + } + + /// Disposes the cached client. + void close() { + _bundle?.dio.close(force: true); + _bundle = null; + } +} + +class _Bundle { + final Dio dio; + final CurseForgeApi api; + + _Bundle({required this.dio, required this.api}); +} diff --git a/lib/src/service/curseforge/curseforge_auth_interceptor.dart b/lib/src/service/curseforge/curseforge_auth_interceptor.dart new file mode 100644 index 0000000..7b918a0 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_auth_interceptor.dart @@ -0,0 +1,146 @@ +import 'package:dio/dio.dart'; + +import '../../cli/exceptions.dart'; +import '../dio_error_helpers.dart'; +import 'curseforge_api.dart'; +import 'curseforge_url.dart'; + +/// Attaches `x-api-key: ` to outbound CurseForge requests that +/// opt into auth via `extra[kCurseForgeAuthRequired]`. Resolution order: +/// 1. `envTokenLookup()` — typically `GITRINTH_CURSEFORGE_TOKEN`. +/// 2. The build-time default key from `decodeDefaultCfApiKey()` +/// (or a caller-supplied resolver in tests). +/// +/// User config tokens are not used for downloads; `gitrinth token add +/// curseforge.com` stores the separate CurseForge publish key. +/// +/// The key is attached only when the request targets the exact origin +/// (scheme, host and port) of the resolved read-API base URL, so a stray +/// `dio.get('https://example.com/...')` does not leak it. Credentialed +/// requests never follow redirects: the HTTP client would replay the +/// key to the redirect target without re-running this origin check, so +/// any 3xx response is rejected instead. +class CurseForgeAuthInterceptor extends Interceptor { + final String? Function() envTokenLookup; + final String Function() _defaultKeyResolver; + final Uri _origin; + + CurseForgeAuthInterceptor({ + required this.envTokenLookup, + String baseUrl = defaultCurseForgeBaseUrl, + String Function()? defaultKeyResolver, + }) : _origin = Uri.parse(baseUrl), + _defaultKeyResolver = defaultKeyResolver ?? decodeDefaultCfApiKey; + + @override + void onRequest(RequestOptions options, RequestInterceptorHandler handler) { + if (options.extra[kCurseForgeAuthRequired] != true) { + handler.next(options); + return; + } + if (!_sameOrigin(options.uri)) { + handler.next(options); + return; + } + final token = _resolveToken(); + if (token != null && token.isNotEmpty) { + options.headers['x-api-key'] = token; + options.followRedirects = false; + options.extra[_credentialedKey] = true; + handler.next(options); + return; + } + handler.reject( + DioException( + requestOptions: options, + type: DioExceptionType.cancel, + error: const AuthenticationError(_missingTokenMessage), + message: _missingTokenMessage, + ), + ); + } + + @override + void onResponse( + Response response, + ResponseInterceptorHandler handler, + ) { + if (!_isCredentialedRedirect(response)) { + handler.next(response); + return; + } + handler.reject( + DioException( + requestOptions: response.requestOptions, + response: response, + type: DioExceptionType.badResponse, + error: UserError(_redirectMessage(response)), + message: _redirectMessage(response), + ), + true, + ); + } + + @override + void onError(DioException err, ErrorInterceptorHandler handler) { + final response = err.response; + if (response != null && + err.error is! GitrinthException && + _isCredentialedRedirect(response)) { + handler.reject(wrapDioError(err, UserError(_redirectMessage(response)))); + return; + } + if (response?.statusCode != 401) { + handler.next(err); + return; + } + if (!_sameOrigin(err.requestOptions.uri)) { + handler.next(err); + return; + } + handler.reject( + wrapDioError(err, const AuthenticationError(_unauthorizedMessage)), + ); + } + + bool _sameOrigin(Uri uri) => + uri.scheme.toLowerCase() == _origin.scheme.toLowerCase() && + uri.host.toLowerCase() == _origin.host.toLowerCase() && + uri.port == _origin.port; + + static bool _isCredentialedRedirect(Response response) { + final status = response.statusCode ?? 0; + return response.requestOptions.extra[_credentialedKey] == true && + status >= 300 && + status < 400; + } + + static String _redirectMessage(Response response) { + final location = response.headers.value('location'); + return 'CurseForge API request to ${response.requestOptions.uri} was ' + 'redirected (HTTP ${response.statusCode}' + '${location == null ? '' : ' to $location'}). Redirects are not ' + 'followed for requests that carry the download API key; check ' + 'GITRINTH_CURSEFORGE_URL.'; + } + + String? _resolveToken() { + final fromEnv = envTokenLookup(); + if (fromEnv != null && fromEnv.isNotEmpty) return fromEnv; + final fromDefault = _defaultKeyResolver(); + if (fromDefault.isEmpty) return null; + return fromDefault; + } +} + +const String _credentialedKey = 'gitrinth.curseforgeCredentialed'; + +const String _missingTokenMessage = + 'No CurseForge download API key is configured. Set ' + 'GITRINTH_CURSEFORGE_TOKEN or build with ' + 'GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64.'; + +const String _unauthorizedMessage = + 'CurseForge rejected the configured download API key. Set ' + 'GITRINTH_CURSEFORGE_TOKEN or rebuild with ' + 'GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64.'; diff --git a/lib/src/service/curseforge/curseforge_error_interceptor.dart b/lib/src/service/curseforge/curseforge_error_interceptor.dart new file mode 100644 index 0000000..07ab6f2 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_error_interceptor.dart @@ -0,0 +1,102 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; + +import '../../cli/exceptions.dart'; +import '../dio_error_helpers.dart'; + +/// Maps raw CurseForge HTTP failures into typed [GitrinthException]s. +/// Mirrors `ModrinthErrorInterceptor`: 401/403 surface as +/// [AuthenticationError], 404 surfaces a slug/id-aware +/// [UserError], and other non-2xx codes wrap as a generic [UserError] +/// with the request URI for context. +class CurseForgeErrorInterceptor extends Interceptor { + @override + void onError(DioException err, ErrorInterceptorHandler handler) { + if (err.error is GitrinthException) { + handler.next(err); + return; + } + + final response = err.response; + final reqUri = err.requestOptions.uri; + final status = response?.statusCode; + + if (status == 401 || status == 403) { + _rejectWith( + err, + handler, + const AuthenticationError( + 'CurseForge rejected the download API key. Set ' + 'GITRINTH_CURSEFORGE_TOKEN or rebuild with ' + 'GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64.', + ), + ); + return; + } + + if (status == 404) { + final modIdent = _modIdentFromPath(reqUri.path); + if (modIdent != null) { + _rejectWith( + err, + handler, + UserError( + "CurseForge mod '$modIdent' not found. Verify the project on " + 'curseforge.com.', + ), + ); + return; + } + } + + String? bodyMessage; + if (response != null) { + final data = response.data; + if (data is Map && data['error'] is String) { + bodyMessage = data['error'] as String; + } else if (data is String && data.isNotEmpty) { + try { + final decoded = jsonDecode(data); + if (decoded is Map && decoded['error'] is String) { + bodyMessage = decoded['error'] as String; + } + } catch (_) { + // Not JSON; ignore. + } + } + } + final summary = bodyMessage != null + ? '$bodyMessage (HTTP ${status ?? '?'})' + : 'HTTP ${status ?? '?'} ${response?.statusMessage ?? err.message ?? ''}' + .trim(); + _rejectWith( + err, + handler, + UserError('CurseForge request failed for $reqUri: $summary'), + ); + } + + void _rejectWith( + DioException err, + ErrorInterceptorHandler handler, + GitrinthException wrapped, + ) { + handler.reject(wrapDioError(err, wrapped)); + } +} + +/// Extracts the mod identifier (numeric ID or slug-on-search) from +/// `/v1/mods/(/...)` so 404s can name the missing project. +String? _modIdentFromPath(String path) { + final segments = path + .split('/') + .where((s) => s.isNotEmpty) + .toList(growable: false); + for (var i = 0; i + 1 < segments.length; i++) { + if (segments[i] == 'mods' && segments[i + 1] != 'search') { + return Uri.decodeComponent(segments[i + 1]); + } + } + return null; +} diff --git a/lib/src/service/curseforge/curseforge_filters.dart b/lib/src/service/curseforge/curseforge_filters.dart new file mode 100644 index 0000000..85764e1 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_filters.dart @@ -0,0 +1,121 @@ +import '../../cli/exceptions.dart'; +import '../../model/curseforge/cf_constants.dart'; +import '../../model/curseforge/cf_mod.dart'; +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/manifest/mods_yaml.dart'; +import 'curseforge_api.dart'; + +/// Maximum [CurseForgeApi.listFiles] page size. CurseForge caps page +/// size at 50 for the files endpoint. +const int _filesPageSize = 50; +const int _filesWindow = 10000; + +/// Lists files for [projectId] that are compatible with the requested +/// pack context, sorted newest-first. +/// +/// `gameVersions` is the union of the pack's `mc_version` and the +/// entry's `accepts_mc` — CurseForge's `gameVersion` query parameter is +/// single-string only, so each distinct version gets a filtered query. +/// Files returned by multiple queries are deduplicated before sorting. +/// The channel floor maps to the set returned by [cfReleaseTypesFor]. +/// +/// Plugin sections require [pluginLoader] and the pluginLoader must +/// satisfy [pluginLoaderEligibleForCurseforge] — Folia/Sponge throw +/// [ArgumentError] before any HTTP request to mirror the source +/// eligibility matrix. +Future> listCompatibleFiles({ + required CurseForgeApi api, + LoaderRegistry registry = LoaderRegistry.builtins, + required int projectId, + required Section section, + ModLoader? modLoader, + PluginLoader? pluginLoader, + required List gameVersions, + required Channel channel, + bool diagnoseUnavailable = true, +}) async { + if (section == Section.plugins) { + if (pluginLoader == null || + !(registry[pluginLoader]?.curseforgePlugins ?? false)) { + throw ArgumentError( + 'CurseForge does not support plugin loader $pluginLoader', + ); + } + } + + final allowedReleaseTypes = cfReleaseTypesFor(channel); + // Plugin sections don't filter by mod loader on the wire — CF's + // plugin universe is Bukkit/Spigot/Paper-shape regardless of the + // pack's `loader.mods`. + final modLoaderType = section == Section.plugins + ? null + : (modLoader == null ? null : registry[modLoader]?.curseforgeType); + if (modLoader != null && (modLoaderType == null || modLoaderType <= 0)) { + throw ValidationError( + 'Loader ${modLoader.name} has no CurseForge numeric filter mapping; add a provider mapping in loaders.yaml.', + ); + } + + final all = {}; + final wanted = gameVersions.toSet(); + for (final gameVersion in wanted) { + var index = 0; + while (true) { + final remaining = _filesWindow - index; + final page = await api.listFiles( + projectId, + gameVersion: gameVersion, + modLoaderType: modLoaderType, + index: index, + pageSize: remaining < _filesPageSize ? remaining : _filesPageSize, + ); + if (page.pagination.totalCount > _filesWindow) { + throw ValidationError( + 'CurseForge project $projectId has more than 10,000 files for ' + 'Minecraft $gameVersion. This filtered history exceeds the supported ' + 'API window; resolution cannot use an incomplete candidate list.', + ); + } + for (final file in page.data) { + all.putIfAbsent(file.id, () => file); + } + index += page.data.length; + if (index >= page.pagination.totalCount) break; + if (page.data.isEmpty || index >= _filesWindow) { + throw ValidationError( + 'CurseForge returned an incomplete file history for project ' + '$projectId and Minecraft $gameVersion at index $index. ' + 'Retry when the service can return the complete filtered history.', + ); + } + } + } + + final compatible = all.values + .where((f) => f.gameVersions.any(wanted.contains)) + .where((f) => allowedReleaseTypes.contains(f.releaseType)) + .toList(); + final available = compatible.where((f) => f.isAvailable).toList(); + if (diagnoseUnavailable && compatible.isNotEmpty && available.isEmpty) { + throw ValidationError( + 'All compatible CurseForge files for project $projectId are unavailable.', + ); + } + return available..sort((a, b) => b.fileDate.compareTo(a.fileDate)); +} + +/// Looks up a mod by exact-slug match within [section]. Returns null +/// when CurseForge has no project with that slug for the section's +/// classId. +Future findModBySlug( + CurseForgeApi api, { + required String slug, + required Section section, +}) async { + final response = await api.searchMods( + gameId: kCurseForgeGameIdMinecraft, + classId: section.cfClassId, + slug: slug, + ); + return response.data.isEmpty ? null : response.data.first; +} diff --git a/lib/src/service/curseforge/curseforge_metadata_store.dart b/lib/src/service/curseforge/curseforge_metadata_store.dart new file mode 100644 index 0000000..8dca80f --- /dev/null +++ b/lib/src/service/curseforge/curseforge_metadata_store.dart @@ -0,0 +1,82 @@ +import 'dart:convert'; +import 'dart:io'; + +import '../../cli/exceptions.dart'; +import '../../model/curseforge/cf_mod_file.dart'; +import '../../util/atomic_file_writer.dart'; +import '../cache.dart'; + +/// Persists the selected file's provider metadata without changing mods.lock. +class CurseForgeMetadataStore { + final GitrinthCache cache; + const CurseForgeMetadataStore(this.cache); + + void write(ModFile file) { + cache.curseforgePath( + projectId: file.modId, + fileId: file.id, + filename: file.fileName, + ); + final path = cache.curseforgeMetadataPath( + projectId: file.modId, + fileId: file.id, + ); + try { + atomicReplaceString( + path, + jsonEncode({ + 'schemaVersion': 1, + 'projectId': file.modId, + 'fileId': file.id, + 'file': file.toMap(), + }), + ); + } on Object catch (e) { + throw ValidationError( + 'Cannot persist CurseForge metadata for project ${file.modId}, file ${file.id} at $path: $e. mods.lock was not replaced.', + ); + } + } + + ModFile read({ + required int projectId, + required int fileId, + String? expectedSha1, + }) { + final path = cache.curseforgeMetadataPath( + projectId: projectId, + fileId: fileId, + ); + try { + final raw = jsonDecode(File(path).readAsStringSync()); + if (raw is! Map || + raw['schemaVersion'] != 1 || + raw['projectId'] != projectId || + raw['fileId'] != fileId || + raw['file'] is! Map) { + throw const FormatException('invalid metadata envelope or identity'); + } + final data = raw['file'] as Map; + if (data['dependencies'] is! List || + data['isAvailable'] is! bool || + data['hashes'] is! List) { + throw const FormatException('missing selected-file validation fields'); + } + final file = ModFileMapper.fromMap(data); + if (file.id != fileId || + file.modId != projectId || + !file.isAvailable || + (expectedSha1 != null && + file.sha1Hash != expectedSha1.toLowerCase())) { + throw const FormatException( + 'selected-file identity, availability, or hash mismatch', + ); + } + return file; + } on Object catch (e) { + throw ValidationError( + 'Cannot validate offline CurseForge project $projectId, file $fileId from $path: $e. Run `gitrinth get` online once to refresh selected-file metadata.', + ); + } + } +} diff --git a/lib/src/service/curseforge/curseforge_publication_receipt.dart b/lib/src/service/curseforge/curseforge_publication_receipt.dart new file mode 100644 index 0000000..d95d89c --- /dev/null +++ b/lib/src/service/curseforge/curseforge_publication_receipt.dart @@ -0,0 +1,211 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; + +import '../../cli/exceptions.dart'; +import '../../util/atomic_file_writer.dart'; + +enum PublicationStepState { pending, inFlight, failed, complete, unknown } + +class PublicationStepReceipt { + PublicationStepState state; + String? id; + final String? sha256; + PublicationStepReceipt({ + this.state = PublicationStepState.pending, + this.id, + this.sha256, + }); + Map toMap() => { + 'state': state.name, + if (id != null) 'id': id, + if (sha256 != null) 'sha256': sha256, + }; + factory PublicationStepReceipt.fromMap(Map raw, {bool numericId = false}) { + final state = PublicationStepState.values + .where((v) => v.name == raw['state']) + .firstOrNull; + final id = raw['id']; + final hash = raw['sha256']; + if (state == null || + (id != null && id is! String) || + (hash != null && + (hash is! String || !RegExp(r'^[a-f0-9]{64}$').hasMatch(hash))) || + (state == PublicationStepState.complete && + (id is! String || + id.isEmpty || + (numericId && (int.tryParse(id) ?? 0) <= 0)))) { + throw const FormatException('Invalid publication step.'); + } + return PublicationStepReceipt( + state: state, + id: id as String?, + sha256: hash as String?, + ); + } +} + +/// Token-free durable state. In-flight steps are never safe to repeat blindly. +class CurseForgePublicationReceipt { + final int projectId; + final String release; + final String slug; + final String serverUrl; + final String displayName; + final String releaseType; + final bool manualRelease; + final PublicationStepReceipt client; + final PublicationStepReceipt server; + final Map peers; + String classification; + CurseForgePublicationReceipt({ + required this.projectId, + required this.release, + required this.slug, + required this.serverUrl, + required this.displayName, + required this.releaseType, + required this.manualRelease, + required this.client, + required this.server, + Map? peers, + this.classification = 'pending', + }) : peers = peers ?? {}; + + int? get parentId => int.tryParse(client.id ?? ''); + int? get childId => int.tryParse(server.id ?? ''); + Map toMap() => { + 'version': 1, + 'target': 'curseforge', + 'projectId': projectId, + 'release': release, + 'slug': slug, + 'serverUrl': serverUrl, + 'displayName': displayName, + 'releaseType': releaseType, + 'manualRelease': manualRelease, + 'client': client.toMap(), + 'server': server.toMap(), + 'classification': classification, + 'peers': {for (final e in peers.entries) e.key: e.value.toMap()}, + }; + factory CurseForgePublicationReceipt.fromMap(Map raw) { + if (raw['version'] != 1 || + raw['target'] != 'curseforge' || + raw['projectId'] is! int || + (raw['projectId'] as int) <= 0 || + raw['release'] is! String || + raw['slug'] is! String || + raw['serverUrl'] is! String || + raw['client'] is! Map || + raw['server'] is! Map || + raw['peers'] is! Map || + raw['displayName'] is! String || + !const {'release', 'beta', 'alpha'}.contains(raw['releaseType']) || + raw['manualRelease'] is! bool || + !const {'pending', 'confirmed'}.contains(raw['classification'])) { + throw const FormatException('Invalid publication receipt.'); + } + final peers = {}; + for (final entry in (raw['peers'] as Map).entries) { + if (entry.key is! String || entry.value is! Map) { + throw const FormatException('Invalid publication peer.'); + } + peers[entry.key as String] = PublicationStepReceipt.fromMap( + entry.value as Map, + ); + } + return CurseForgePublicationReceipt( + projectId: raw['projectId'] as int, + release: raw['release'] as String, + slug: raw['slug'] as String, + serverUrl: raw['serverUrl'] as String, + client: PublicationStepReceipt.fromMap( + raw['client'] as Map, + numericId: true, + ), + displayName: raw['displayName'] as String, + releaseType: raw['releaseType'] as String, + manualRelease: raw['manualRelease'] as bool, + server: PublicationStepReceipt.fromMap( + raw['server'] as Map, + numericId: true, + ), + peers: peers, + classification: raw['classification'] as String, + ); + } +} + +class CurseForgePublicationReceiptStore { + final String projectDir; + const CurseForgePublicationReceiptStore(this.projectDir); + String path(int projectId, String release) { + final key = sha256 + .convert(utf8.encode(jsonEncode([projectId.toString(), release]))) + .toString(); + return p.join( + projectDir, + '.gitrinth', + 'publications', + 'curseforge', + projectId.toString(), + '$key.json', + ); + } + + CurseForgePublicationReceipt? read(int projectId, String release) { + final file = File(path(projectId, release)); + if (!file.existsSync()) return null; + try { + final raw = jsonDecode(file.readAsStringSync()); + if (raw is! Map) { + throw const FormatException('Receipt must be an object.'); + } + return CurseForgePublicationReceipt.fromMap(raw); + } on Object catch (_) { + throw ValidationError( + 'Cannot read publication receipt at ${file.path}. Inspect and reconcile the recorded remote outcomes before publishing again.', + ); + } + } + + /// Replaces only the snapshot this invocation actually prepared against. + String writeIfUnchanged( + CurseForgePublicationReceipt receipt, + String? expected, + ) { + final file = File(path(receipt.projectId, receipt.release)); + try { + file.parent.createSync(recursive: true); + final lock = File( + '${file.path}.transaction.lock', + ).openSync(mode: FileMode.append); + try { + lock.lockSync(FileLock.blockingExclusive); + try { + final current = read(receipt.projectId, receipt.release); + final snapshot = current == null ? null : jsonEncode(current.toMap()); + if (snapshot != expected) { + throw const ValidationError( + 'Publication receipt changed after preparation. Another invocation may have uploaded files; rerun to reload and reconcile its state before any further POST.', + ); + } + final contents = jsonEncode(receipt.toMap()); + atomicReplaceString(file.path, contents); + return contents; + } finally { + lock.unlockSync(); + } + } finally { + lock.closeSync(); + } + } on FileSystemException { + throw UserError( + 'Cannot persist publication receipt at ${file.path}. No further uploads were attempted; inspect remote files before retrying.', + ); + } + } +} diff --git a/lib/src/service/curseforge/curseforge_publication_service.dart b/lib/src/service/curseforge/curseforge_publication_service.dart new file mode 100644 index 0000000..9b9c648 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_publication_service.dart @@ -0,0 +1,685 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive_io.dart'; +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; + +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_yaml.dart' show SourceKind; +import '../../model/manifest/server_distribution.dart'; +import '../file_digest.dart'; +import '../modrinth/modrinth_url.dart'; +import '../prepared_publication.dart'; +import 'curseforge_publication_receipt.dart'; +import 'curseforge_upload_client.dart'; + +class PreparedCurseForgePublication { + final PreparedPublication root; + final PreparedPublication server; + final CurseForgePublicationReceipt receipt; + final Map peerKeys; + String? _persistedSnapshot; + PreparedCurseForgePublication({ + required this.root, + required this.server, + required this.receipt, + required Map peerKeys, + required String? persistedSnapshot, + }) : peerKeys = Map.unmodifiable(peerKeys), + _persistedSnapshot = persistedSnapshot; +} + +class CurseForgePublicationResult { + final int parentId; + final int childId; + final bool classificationConfirmed; + const CurseForgePublicationResult( + this.parentId, + this.childId, + this.classificationConfirmed, + ); +} + +class CurseForgePublicationFailure extends UserError { + final int? parentId; + final int? childId; + final bool unknownOutcome; + const CurseForgePublicationFailure( + super.message, { + this.parentId, + this.childId, + this.unknownOutcome = false, + }); +} + +/// Executes dependent uploads and records exactly which remote steps survived. +class CurseForgePublicationService { + final CurseForgeUploadClient uploadClient; + final Future> Function(int projectId, int fileId) + readFile; + final Future>> Function(int projectId) listFiles; + final CurseForgePublicationReceiptStore store; + const CurseForgePublicationService({ + required this.uploadClient, + required this.readFile, + required this.listFiles, + required this.store, + }); + + static String _serverKey(String raw) { + final uri = Uri.tryParse(raw); + if (uri == null || + !const {'http', 'https'}.contains(uri.scheme) || + uri.host.isEmpty || + uri.userInfo.isNotEmpty || + uri.hasQuery || + uri.hasFragment) { + throw const ValidationError( + 'Publication endpoints must be HTTP(S) base URLs without embedded credentials, queries, or fragments.', + ); + } + return normalizeServerKey(raw); + } + + Future prepare({ + required PreparedPublication plan, + required String slug, + required String version, + required String displayName, + required String minecraft, + required String loaderId, + required String? loaderVersion, + String? pluginLoaderId, + String? pluginLoaderVersion, + String? clientLoaderToken, + bool requireLoaderVersionMatch = true, + bool requirePluginVersionMatch = true, + List peers = const [], + }) async { + final server = plan.role == PublicationArtifactRole.server + ? plan + : plan.dependentServer; + final project = plan.curseforgeProjectId; + if (server == null || project == null || project <= 0) { + throw const ValidationError( + 'A server publication requires a CurseForge project and distribution artifact.', + ); + } + if (plan.target != SourceKind.curseforge || + server.target != SourceKind.curseforge || + server.curseforgeProjectId != project || + _serverKey(plan.serverUrl) != _serverKey(server.serverUrl) || + plan.token.isEmpty || + server.token.isEmpty) { + throw const ValidationError( + 'Dependent server publication requires matching CurseForge project/endpoints and nonempty upload credentials.', + ); + } + final distribution = _validateDistribution( + server.artifact, + slug: slug, + version: version, + minecraft: minecraft, + loaderId: loaderId, + loaderVersion: loaderVersion, + pluginLoaderId: pluginLoaderId, + pluginLoaderVersion: pluginLoaderVersion, + requireLoaderVersionMatch: requireLoaderVersionMatch, + requirePluginVersionMatch: requirePluginVersionMatch, + ); + if (plan.role == PublicationArtifactRole.client) { + if (loaderId != 'vanilla' && + (clientLoaderToken == null || + distribution.loaderVersion == null || + const { + 'stable', + 'latest', + }.contains(distribution.loaderVersion))) { + throw const ValidationError( + 'Client publication requires a CurseForge archive loader mapping and a concrete matching server loader version.', + ); + } + _validateClient( + plan.artifact, + displayName: displayName, + version: version, + minecraft: minecraft, + expectedLoader: loaderId == 'vanilla' + ? null + : '$clientLoaderToken-${distribution.loaderVersion}', + ); + } + final serverHash = await fileDigest(server.artifact, sha256); + final serverSha1 = await fileDigest(server.artifact, sha1); + final clientHash = plan.role == PublicationArtifactRole.client + ? await fileDigest(plan.artifact, sha256) + : null; + final key = _serverKey(plan.serverUrl); + final releaseType = plan.payload['releaseType']; + final manual = plan.payload['isMarkedForManualRelease']; + if (displayName.isEmpty || + !const {'release', 'beta', 'alpha'}.contains(releaseType) || + manual is! bool) { + throw const ValidationError('Invalid CurseForge release metadata.'); + } + if (server.payload['releaseType'] != releaseType || + server.payload['isMarkedForManualRelease'] != manual || + server.payload['displayName'] is! String || + (server.payload['displayName'] as String).isEmpty) { + throw const ValidationError( + 'Client/server release channel and manual-release metadata must match before publication.', + ); + } + final prior = store.read(project, version); + final persistedSnapshot = prior == null ? null : jsonEncode(prior.toMap()); + final receipt = + prior ?? + CurseForgePublicationReceipt( + projectId: project, + release: version, + slug: slug, + serverUrl: key, + displayName: displayName, + releaseType: releaseType as String, + manualRelease: manual, + client: PublicationStepReceipt(sha256: clientHash), + server: PublicationStepReceipt(sha256: serverHash), + ); + if (receipt.projectId != project || + receipt.release != version || + receipt.slug != slug || + receipt.serverUrl != key || + receipt.displayName != displayName || + receipt.releaseType != releaseType || + receipt.manualRelease != manual || + receipt.server.sha256 != serverHash || + (clientHash != null && receipt.client.sha256 != clientHash)) { + throw const ValidationError( + 'Publication identity or artifact changed from its receipt. Use a new pack release for changed artifacts; inspect the existing remote release before resuming.', + ); + } + final peerKeys = {}; + for (final peer in peers) { + final peerKey = jsonEncode([ + peer.target.name, + _serverKey(peer.serverUrl), + ]); + final hash = await fileDigest(peer.artifact, sha256); + final recorded = receipt.peers[peerKey]; + if (recorded != null && recorded.sha256 != hash) { + throw const ValidationError( + 'A peer-platform publication artifact changed from its receipt. Start a new release.', + ); + } + receipt.peers.putIfAbsent( + peerKey, + () => PublicationStepReceipt(sha256: hash), + ); + peerKeys[peer] = peerKey; + if (_unknown(receipt.peers[peerKey])) { + throw const ValidationError( + 'The peer-platform upload outcome is unknown. Inspect its remote release before repeating this publication; use a verified CurseForge server-only retry independently.', + ); + } + } + final explicit = plan.parentFileId; + if (plan.role == PublicationArtifactRole.server) { + if (explicit == null || explicit <= 0) { + throw const UsageError( + 'Server-only publishing requires an explicit positive parent file ID.', + ); + } + if (receipt.parentId != null && receipt.parentId != explicit) { + throw const ValidationError( + 'Explicit parent differs from the recorded publication parent.', + ); + } + await _verifyParent( + project, + explicit, + displayName, + releaseType as String, + ); + receipt.client.id = explicit.toString(); + receipt.client.state = PublicationStepState.complete; + } else if (_unknown(receipt.client)) { + throw const ValidationError( + 'The client upload outcome is unknown. Inspect remote files, then resume with --server-only and an explicitly verified --curseforge-parent-file ID; do not repeat the client POST.', + ); + } + if (_unknown(receipt.server)) { + final parent = receipt.parentId; + if (parent == null) { + throw const ValidationError( + 'The server upload outcome is unknown and its parent is unverified. Inspect remote files before retrying.', + ); + } + final List> remoteFiles; + try { + remoteFiles = await listFiles(project); + } on Object catch (e) { + throw ValidationError( + 'The server upload outcome is unknown and remote reconciliation failed. ${e is GitrinthException ? e.message : "Inspect the remote parent and artifact hash before retrying."} No duplicate POST was sent.', + ); + } + final matches = remoteFiles + .where( + (file) => + file['modId'] == project && + file['parentProjectFileId'] == parent && + _sha1(file) == serverSha1 && + file['id'] is int && + (file['id'] as int) > 0, + ) + .toList(); + if (matches.length != 1) { + throw const ValidationError( + 'The server upload outcome is unknown. Inspect remote files for the matching parent and artifact hash before retrying; no duplicate POST was sent.', + ); + } + receipt.server.id = matches.single['id'].toString(); + receipt.server.state = PublicationStepState.complete; + } + return PreparedCurseForgePublication( + root: plan, + server: server, + receipt: receipt, + peerKeys: peerKeys, + persistedSnapshot: persistedSnapshot, + ); + } + + bool peerComplete( + PreparedCurseForgePublication plan, + PreparedPublication peer, + ) => _peerStep(plan, peer).state == PublicationStepState.complete; + String? peerId( + PreparedCurseForgePublication plan, + PreparedPublication peer, + ) => _peerStep(plan, peer).id; + PublicationStepReceipt _peerStep( + PreparedCurseForgePublication plan, + PreparedPublication peer, + ) { + final key = plan.peerKeys[peer]; + final step = plan.receipt.peers[key]; + if (step == null) { + throw StateError( + 'Prepared peer publication is missing its receipt step.', + ); + } + return step; + } + + Future startPeer( + PreparedCurseForgePublication plan, + PreparedPublication peer, + ) async { + final step = _peerStep(plan, peer); + await _unchanged(peer.artifact, step.sha256); + step.state = PublicationStepState.inFlight; + _persist(plan); + } + + void completePeer( + PreparedCurseForgePublication plan, + PreparedPublication peer, + String? id, + ) { + final step = _peerStep(plan, peer); + if (id == null || id.isEmpty) { + step.state = PublicationStepState.unknown; + _persist(plan); + throw const UserError( + 'Peer-platform upload returned no version ID; its outcome is unknown. Inspect remote releases before retrying.', + ); + } + step.id = id; + step.state = PublicationStepState.complete; + _persist(plan); + } + + void failPeer( + PreparedCurseForgePublication plan, + PreparedPublication peer, + Object error, + ) { + final step = _peerStep(plan, peer); + step.state = _unknownFailure(error) + ? PublicationStepState.unknown + : PublicationStepState.failed; + _persist(plan); + } + + Future publish( + PreparedCurseForgePublication prepared, + ) async { + final receipt = prepared.receipt; + try { + _persist(prepared); + if (receipt.client.state != PublicationStepState.complete) { + await _upload( + prepared, + receipt.client, + prepared.root, + prepared.root.payload, + ); + } + final parent = receipt.parentId; + if (parent == null || parent <= 0) { + throw StateError('Completed client publication has no parent ID.'); + } + if (receipt.server.state != PublicationStepState.complete) { + final payload = {...prepared.server.payload, 'parentFileID': parent} + ..remove('gameVersions') + ..remove('gameVersionNames'); + await _upload(prepared, receipt.server, prepared.server, payload); + } + final child = receipt.childId; + if (child == null || child <= 0) { + throw StateError('Completed server publication has no child ID.'); + } + receipt.classification = 'pending'; + try { + final parentFile = await readFile(receipt.projectId, parent); + final childFile = await readFile(receipt.projectId, child); + if (parentFile['id'] == parent && + parentFile['modId'] == receipt.projectId && + parentFile['serverPackFileId'] == child && + childFile['id'] == child && + childFile['modId'] == receipt.projectId && + childFile['isServerPack'] == true && + childFile['parentProjectFileId'] == parent) { + receipt.classification = 'confirmed'; + } + } on Object { + // Moderation/read visibility can lag successful additional-file uploads. + } + _persist(prepared); + return CurseForgePublicationResult( + parent, + child, + receipt.classification == 'confirmed', + ); + } on Object catch (e) { + final message = e is GitrinthException + ? e.message + : 'CurseForge publication failed; inspect recorded remote outcomes before retrying.'; + throw CurseForgePublicationFailure( + message, + parentId: receipt.parentId, + childId: receipt.childId, + unknownOutcome: _unknown(receipt.client) || _unknown(receipt.server), + ); + } + } + + Future _upload( + PreparedCurseForgePublication prepared, + PublicationStepReceipt step, + PreparedPublication artifact, + Map metadata, + ) async { + await _unchanged(artifact.artifact, step.sha256); + step.state = PublicationStepState.inFlight; + _persist(prepared); + final CurseForgeUploadResult result; + try { + result = await uploadClient.upload( + projectId: prepared.receipt.projectId, + token: artifact.token, + archive: artifact.artifact, + metadata: metadata, + ); + } on Object catch (e) { + step.state = _unknownFailure(e) + ? PublicationStepState.unknown + : PublicationStepState.failed; + _persist(prepared); + rethrow; + } + step.id = result.fileId.toString(); + step.state = PublicationStepState.complete; + _persist(prepared); + } + + void _persist(PreparedCurseForgePublication prepared) { + prepared._persistedSnapshot = store.writeIfUnchanged( + prepared.receipt, + prepared._persistedSnapshot, + ); + } + + static Future _unchanged(File artifact, String? expected) async { + if (expected == null || await fileDigest(artifact, sha256) != expected) { + throw const ValidationError( + 'Prepared publication artifact changed before upload. Inspect the receipt and start a new release for changed bytes.', + ); + } + } + + static bool _unknown(PublicationStepReceipt? step) => + step != null && + const { + PublicationStepState.inFlight, + PublicationStepState.unknown, + }.contains(step.state); + static bool _unknownFailure(Object error) => switch (error) { + CurseForgeUploadFailure(:final unknownOutcome) => unknownOutcome, + AuthenticationError() => false, + DioException(:final response) => + response == null || (response.statusCode ?? 500) >= 500, + _ => true, + }; + Future _verifyParent( + int project, + int parent, + String displayName, + String releaseType, + ) async { + final file = await readFile(project, parent); + if (file['id'] != parent || file['modId'] != project) { + throw const ValidationError( + 'CurseForge parent file belongs to a different project.', + ); + } + if (file['displayName'] != displayName || + file['releaseType'] != + const {'release': 1, 'beta': 2, 'alpha': 3}[releaseType] || + file['isServerPack'] == true) { + throw const ValidationError( + 'CurseForge parent file does not match the intended client pack release and channel.', + ); + } + } + + static String? _sha1(Map file) { + final hashes = file['hashes']; + if (hashes is! List) return null; + for (final hash in hashes) { + if (hash is Map && hash['algo'] == 1 && hash['value'] is String) { + return (hash['value'] as String).toLowerCase(); + } + } + return null; + } + + static ServerDistributionManifest _validateDistribution( + File file, { + required String slug, + required String version, + required String minecraft, + required String loaderId, + required String? loaderVersion, + required String? pluginLoaderId, + required String? pluginLoaderVersion, + required bool requireLoaderVersionMatch, + required bool requirePluginVersionMatch, + }) { + final input = InputFileStream(file.path); + try { + final archive = ZipDecoder().decodeStream(input); + _validateArchiveEntries(archive, server: true); + final marker = archive.findFile(serverDistributionManifestName); + if (marker == null || !marker.isFile) { + throw const FormatException('Missing server distribution marker.'); + } + final raw = jsonDecode(utf8.decode(marker.content)); + if (raw is! Map) { + throw const FormatException('Invalid server distribution marker.'); + } + final manifest = ServerDistributionManifest.fromMap(raw); + if (manifest.slug != slug || + manifest.version != version || + manifest.minecraft != minecraft || + manifest.loaderId != loaderId || + (requireLoaderVersionMatch && + manifest.loaderVersion != loaderVersion) || + manifest.pluginLoaderId != pluginLoaderId || + (requirePluginVersionMatch && + manifest.pluginLoaderVersion != pluginLoaderVersion)) { + throw const FormatException( + 'Server distribution does not match this pack release, Minecraft version, or loader.', + ); + } + final actual = archive.files + .where( + (entry) => + entry.isFile && entry.name != serverDistributionManifestName, + ) + .toList(); + if (archive.files + .where( + (entry) => entry.name == serverDistributionManifestName, + ) + .length != + 1 || + actual.length != manifest.files.length) { + throw const FormatException( + 'Server distribution inventory does not match archive contents.', + ); + } + final entriesByPath = >{}; + for (final entry in actual) { + (entriesByPath[entry.name] ??= []).add(entry); + } + for (final expected in manifest.files) { + final entries = entriesByPath[expected.path] ?? const []; + if (entries.length != 1 || + entries.single.isSymbolicLink || + sha256.convert(entries.single.content).toString() != + expected.sha256) { + throw FormatException( + 'Server distribution inventory/hash mismatch for ${expected.path}.', + ); + } + } + return manifest; + } on Object catch (e) { + throw ValidationError( + 'Invalid server distribution ${file.path}: $e. Build it with `gitrinth pack --curseforge --server-distribution`; the legacy manifest-server ZIP is not a distribution.', + ); + } finally { + input.closeSync(); + } + } + + static void _validateArchiveEntries(Archive archive, {required bool server}) { + for (final entry in archive.files) { + final path = !entry.isFile && entry.name.endsWith('/') + ? entry.name.substring(0, entry.name.length - 1) + : entry.name; + if (entry.isSymbolicLink || + (entry.mode & 0xf000) == 0xa000 || + !isSafeServerDistributionPath(path) || + (server && + entry.name != serverDistributionManifestName && + isPrivateServerDistributionPath(path))) { + throw FormatException( + 'Unsafe/private path or symbolic link in archive: ${entry.name}', + ); + } + } + } + + static void _validateClient( + File file, { + required String displayName, + required String version, + required String minecraft, + required String? expectedLoader, + }) { + final input = InputFileStream(file.path); + try { + final archive = ZipDecoder().decodeStream(input); + _validateArchiveEntries(archive, server: false); + final manifests = archive.files + .where((entry) => entry.name == 'manifest.json' && entry.isFile) + .toList(); + if (manifests.length != 1) { + throw const FormatException( + 'Client ZIP requires exactly one manifest.json.', + ); + } + final raw = jsonDecode(utf8.decode(manifests.single.content)); + if (raw is! Map || + raw['manifestType'] != 'minecraftModpack' || + raw['manifestVersion'] != 1 || + raw['version'] != version || + raw['name'] is! String || + '${raw['name']} $version' != displayName || + raw['minecraft'] is! Map || + raw['files'] is! List || + raw['overrides'] is! String || + !isSafeServerDistributionPath(raw['overrides'] as String)) { + throw const FormatException( + 'Client manifest does not match the intended pack release or format.', + ); + } + final game = raw['minecraft'] as Map; + final loaders = game['modLoaders']; + if (game['version'] != minecraft || + loaders is! List || + loaders.any( + (loader) => + loader is! Map || + loader['id'] is! String || + loader['primary'] is! bool, + )) { + throw const FormatException( + 'Client manifest has incompatible Minecraft/loader metadata.', + ); + } + final primaries = loaders + .where((loader) => (loader as Map)['primary'] == true) + .toList(); + if ((expectedLoader == null && loaders.isNotEmpty) || + (expectedLoader != null && + (primaries.length != 1 || + (primaries.single as Map)['id'] != expectedLoader))) { + throw const FormatException( + 'Client and server distribution loader versions do not match.', + ); + } + for (final entry in raw['files'] as List) { + if (entry is! Map || + entry['projectID'] is! int || + (entry['projectID'] as int) <= 0 || + entry['fileID'] is! int || + (entry['fileID'] as int) <= 0 || + entry['required'] is! bool) { + throw const FormatException( + 'Invalid client manifest project/file reference.', + ); + } + } + } on Object catch (e) { + throw ValidationError( + 'Invalid client CurseForge archive ${file.path}: $e. Rebuild the matching client release with `gitrinth pack --curseforge`.', + ); + } finally { + input.closeSync(); + } + } +} diff --git a/lib/src/service/curseforge/curseforge_rate_limit_interceptor.dart b/lib/src/service/curseforge/curseforge_rate_limit_interceptor.dart new file mode 100644 index 0000000..dc41a5b --- /dev/null +++ b/lib/src/service/curseforge/curseforge_rate_limit_interceptor.dart @@ -0,0 +1,121 @@ +import 'dart:async'; +import 'dart:io' show HttpDate, HttpException; + +import 'package:dio/dio.dart'; + +import '../console.dart'; +import 'curseforge_url.dart'; + +/// Reactive 429 retry handler for the CurseForge read API. +/// +/// CurseForge does not publish budget headers, so unlike +/// `ModrinthRateLimitInterceptor` this is purely reactive: when a +/// request comes back 429, the interceptor sleeps (`Retry-After` if +/// present, as delay-seconds or an HTTP-date, otherwise an attempt-ramped backoff inside [_minSleep, +/// _maxSleep]) and retries up to [_maxRetries] times before propagating +/// the original error. +/// +/// Scoped to the resolved CurseForge read-API host so other upstreams +/// pass through. +class CurseForgeRateLimitInterceptor extends Interceptor { + final Dio _dio; + final Console? _console; + final Future Function(Duration) _sleep; + final DateTime Function() _now; + final String _cfHost; + + CurseForgeRateLimitInterceptor({ + required Dio dio, + String baseUrl = defaultCurseForgeBaseUrl, + Console? console, + Future Function(Duration)? sleep, + DateTime Function()? now, + }) : _dio = dio, + _console = console, + _cfHost = Uri.parse(baseUrl).host.toLowerCase(), + _sleep = sleep ?? Future.delayed, + _now = now ?? DateTime.now; + + @override + Future onError( + DioException err, + ErrorInterceptorHandler handler, + ) async { + final response = err.response; + if (response == null || + response.statusCode != 429 || + err.requestOptions.uri.host.toLowerCase() != _cfHost) { + handler.next(err); + return; + } + + final retries = (err.requestOptions.extra[_retryCountKey] as int?) ?? 0; + if (retries >= _maxRetries) { + handler.next(err); + return; + } + + final wait = _retryWait(response, retries); + _maybeLog(wait); + await _sleep(wait); + + final retryOptions = err.requestOptions.copyWith( + extra: {...err.requestOptions.extra, _retryCountKey: retries + 1}, + ); + + try { + final retried = await _dio.fetch(retryOptions); + handler.resolve(retried); + } on DioException catch (e) { + handler.next(e); + } + } + + Duration _retryWait(Response response, int retries) { + final retryAfter = _parseRetryAfter( + response.headers.value('retry-after'), + _now(), + ); + if (retryAfter != null) return _clampRetry(retryAfter); + // No header — ramp backoff with attempt count: 1s, 2s, 4s, 8s, 16s, + // capped at _maxSleep. Mirrors Modrinth's clamp envelope. + final ramp = Duration(seconds: 1 << retries); + return _clampRetry(ramp); + } + + Duration _clampRetry(Duration d) { + if (d < _minSleep) return _minSleep; + if (d > _maxSleep) return _maxSleep; + return d; + } + + void _maybeLog(Duration wait) { + if (wait < _verboseSleepThreshold) return; + _console?.io( + 'CurseForge rate limit: received HTTP 429, ' + 'waiting ~${wait.inSeconds}s before retry', + ); + } +} + +/// RFC 9110 `Retry-After`: delay-seconds or an HTTP-date. Anything else +/// is ignored so the attempt-ramped backoff applies. +Duration? _parseRetryAfter(String? raw, DateTime now) { + if (raw == null) return null; + final value = raw.trim(); + final seconds = int.tryParse(value); + if (seconds != null) return Duration(seconds: seconds); + try { + return HttpDate.parse(value).difference(now); + } on FormatException { + return null; + } on HttpException { + return null; + } +} + +const int _maxRetries = 5; +const Duration _minSleep = Duration(seconds: 1); +const Duration _maxSleep = Duration(seconds: 65); +const Duration _verboseSleepThreshold = Duration(seconds: 2); +const String _retryCountKey = 'gitrinth.curseforgeRateLimitRetries'; diff --git a/lib/src/service/curseforge/curseforge_upload_client.dart b/lib/src/service/curseforge/curseforge_upload_client.dart new file mode 100644 index 0000000..9bc9155 --- /dev/null +++ b/lib/src/service/curseforge/curseforge_upload_client.dart @@ -0,0 +1,89 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:path/path.dart' as p; + +import '../../cli/exceptions.dart'; +import 'curseforge_url.dart'; + +class CurseForgeUploadResult { + final int fileId; + const CurseForgeUploadResult(this.fileId); +} + +class CurseForgeUploadFailure extends UserError { + final bool unknownOutcome; + const CurseForgeUploadFailure(super.message, {required this.unknownOutcome}); +} + +/// Upload credentials are never read from the resolver's download key. +/// Protocol: https://support.curseforge.com/support/solutions/articles/9000197321 +class CurseForgeUploadClient { + final Dio dio; + final String baseUrl; + CurseForgeUploadClient( + this.dio, { + this.baseUrl = defaultCurseForgeUploadBaseUrl, + }); + + Future upload({ + required int projectId, + required String token, + required File archive, + required Map metadata, + }) async { + if (projectId <= 0) { + throw const UsageError('CurseForge project ID must be positive.'); + } + try { + final response = await dio.post( + '${baseUrl.replaceFirst(RegExp(r"/$"), "")}/api/projects/$projectId/upload-file', + data: FormData.fromMap({ + 'metadata': jsonEncode(metadata), + 'file': await MultipartFile.fromFile( + archive.path, + filename: p.basename(archive.path), + contentType: DioMediaType('application', 'zip'), + ), + }), + options: Options( + headers: {'X-Api-Token': token}, + followRedirects: false, + validateStatus: (_) => true, + sendTimeout: dio.options.sendTimeout ?? const Duration(minutes: 5), + receiveTimeout: + dio.options.receiveTimeout ?? const Duration(minutes: 2), + ), + ); + final status = response.statusCode ?? 0; + if (status < 200 || status >= 300) { + throw DioException.badResponse( + statusCode: status, + requestOptions: response.requestOptions, + response: response, + ); + } + final data = response.data; + final id = data is Map ? data['id'] : null; + if (id is! int || id <= 0) { + throw const CurseForgeUploadFailure( + 'CurseForge upload returned no positive file ID; the remote outcome is unknown. Inspect remote files before retrying.', + unknownOutcome: true, + ); + } + return CurseForgeUploadResult(id); + } on DioException catch (e) { + if (e.response?.statusCode == 401 || e.response?.statusCode == 403) { + throw const AuthenticationError( + 'CurseForge rejected the publish token.', + ); + } + final status = e.response?.statusCode; + throw CurseForgeUploadFailure( + 'CurseForge upload failed (HTTP ${e.response?.statusCode ?? "unavailable"}).', + unknownOutcome: status == null || status >= 500 || status == 408, + ); + } + } +} diff --git a/lib/src/service/curseforge/curseforge_url.dart b/lib/src/service/curseforge/curseforge_url.dart new file mode 100644 index 0000000..14ffbcd --- /dev/null +++ b/lib/src/service/curseforge/curseforge_url.dart @@ -0,0 +1,76 @@ +import 'dart:convert'; + +import '../modrinth/modrinth_url.dart'; + +/// Default CurseForge read-API base URL. The CurseForge API has a single +/// canonical host; per-call overrides exist for testing only. +const String defaultCurseForgeBaseUrl = 'https://api.curseforge.com'; + +/// Resolves the CurseForge read-API base URL. +/// +/// Mirrors [resolveModrinthBaseUrl] — `GITRINTH_CURSEFORGE_URL` overrides +/// the default and any trailing slash is stripped. Empty values fall back +/// to [defaultCurseForgeBaseUrl]. +String resolveCurseForgeBaseUrl(Map env) { + final fromEnv = env['GITRINTH_CURSEFORGE_URL']; + if (fromEnv != null && fromEnv.isNotEmpty) { + return fromEnv.endsWith('/') + ? fromEnv.substring(0, fromEnv.length - 1) + : fromEnv; + } + return defaultCurseForgeBaseUrl; +} + +/// Build-time default CurseForge download-API key, base64-encoded. +/// +/// Official builds inject this with +/// `--define=GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64=`. +/// User config tokens are intentionally not consulted for downloads; +/// `gitrinth token add curseforge.com` is reserved for the separate +/// CurseForge publish key. +const String kCurseForgeDefaultApiKeyB64 = String.fromEnvironment( + 'GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64', +); + +/// Returns the build-time default CurseForge download-API key. +String decodeDefaultCfApiKey() { + final raw = kCurseForgeDefaultApiKeyB64; + if (raw.isEmpty) return ''; + return utf8.decode(base64.decode(raw)); +} + +/// Default CurseForge upload-API base URL. The upload API is a separate +/// service from the read API and is only consumed by Part 8's publish +/// path; Part 3 declares the constant so the token store and CLI surface +/// have a stable shape to build against. +const String defaultCurseForgeUploadBaseUrl = + 'https://minecraft.curseforge.com'; + +/// Stable [UserConfig.tokens] key for the CurseForge publish API. +/// +/// The publish API itself lives at [defaultCurseForgeUploadBaseUrl], +/// but users manage that credential as `curseforge.com`. +final String curseForgeUploadTokenKey = normalizeServerKey( + 'https://curseforge.com', +); + +/// Resolves the per-developer CurseForge upload token. +/// +/// Returns null when neither the env var nor the user-config map carries +/// a value — Part 8 surfaces a hard error in that case pointing at +/// `https://authors-old.curseforge.com/account/api-tokens` and at +/// `gitrinth token add curseforge.com`. +/// +/// Personal upload tokens are intentionally *not* embeddable: they tie an +/// upload to a specific CurseForge developer account. There is no +/// `decodeDefaultCfUploadToken` counterpart and there must never be one. +String? resolveCurseForgeUploadToken({ + required Map env, + required Map userTokens, +}) { + final fromEnv = env['GITRINTH_CURSEFORGE_UPLOAD_TOKEN']; + if (fromEnv != null && fromEnv.isNotEmpty) return fromEnv; + final fromUser = userTokens[curseForgeUploadTokenKey]; + if (fromUser != null && fromUser.isNotEmpty) return fromUser; + return null; +} diff --git a/lib/src/service/curseforge/curseforge_version.dart b/lib/src/service/curseforge/curseforge_version.dart new file mode 100644 index 0000000..61bd73b --- /dev/null +++ b/lib/src/service/curseforge/curseforge_version.dart @@ -0,0 +1,36 @@ +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/resolver/constraint.dart'; + +/// CF has no version-number field. Prefer the display label, then filename. +/// Keep the extracted number shared with `add` so emitted constraints resolve +/// against the same value on subsequent runs. +String? curseForgeFileVersion(ModFile file) { + final pattern = RegExp(r'\d+\.\d+\.\d+(?:\.\d+)?(?:[-+][0-9A-Za-z.-]+)?'); + for (final label in [file.displayName, file.fileName]) { + var normalized = label; + for (final mc in file.gameVersions.where( + (v) => RegExp(r'^\d+\.\d+(?:\.\d+)?$').hasMatch(v), + )) { + normalized = normalized.replaceAllMapped( + RegExp( + '(^|[^0-9.])(?:mc|minecraft)?${RegExp.escape(mc)}(?=\$|[^0-9.])', + caseSensitive: false, + ), + (match) => match.group(1)!, + ); + } + final match = pattern.firstMatch(normalized); + if (match != null) { + return match.group(0)!.replaceFirst(RegExp(r'\.(jar|zip)$'), ''); + } + } + return null; +} + +bool curseForgeFileSatisfies(ModFile file, String? raw) { + if (raw == null || raw.trim().isEmpty) return true; + if (file.displayName == raw || file.fileName == raw) return true; + final version = curseForgeFileVersion(file); + if (version == null) return false; + return parseConstraint(raw).allows(parseModrinthVersionBestEffort(version)); +} diff --git a/lib/src/service/dependency_graph.dart b/lib/src/service/dependency_graph.dart new file mode 100644 index 0000000..d70ef20 --- /dev/null +++ b/lib/src/service/dependency_graph.dart @@ -0,0 +1,66 @@ +import '../model/modrinth/dependency.dart'; +import '../model/resolver/result.dart'; +import 'source_identity.dart'; + +/// Indexed dependency edges shared by live and cached resolution adapters. +final class DependencyGraph { + final _children = >{}; + final _parents = >{}; + + void addNode(T node) { + _children.putIfAbsent(node, () => {}); + _parents.putIfAbsent(node, () => {}); + } + + void addEdge(T parent, T child) { + addNode(parent); + addNode(child); + _children[parent]!.add(child); + _parents[child]!.add(parent); + } + + Set children(T node) => Set.unmodifiable(_children[node] ?? {}); + Set parents(T node) => Set.unmodifiable(_parents[node] ?? {}); + + Set reachable(Iterable seeds) { + final visited = {}; + final queue = seeds.toList(); + while (queue.isNotEmpty) { + final node = queue.removeLast(); + if (visited.add(node)) queue.addAll(_children[node] ?? {}); + } + return visited; + } +} + +DependencyGraph resolvedDependencyGraph( + ResolutionResult resolution, { + required String Function(LocalEntryKey node) hostFor, +}) { + final identity = SourceIdentity(); + final graph = DependencyGraph(); + for (final entry in resolution.entries) { + final node = LocalEntryKey(entry.section, entry.slug); + identity.register(node, host: hostFor(node)); + identity.bind( + node, + ModrinthProjectKey(hostFor(node), entry.version.projectId), + ); + graph.addNode(node); + } + for (final entry in resolution.entries) { + final parent = LocalEntryKey(entry.section, entry.slug); + for (final dependency in entry.version.dependencies) { + if (dependency.dependencyType != DependencyType.required || + dependency.projectId == null) { + continue; + } + for (final child in identity.aliases( + ModrinthProjectKey(hostFor(parent), dependency.projectId!), + )) { + graph.addEdge(parent, child); + } + } + } + return graph; +} diff --git a/lib/src/service/downloader.dart b/lib/src/service/downloader.dart index 5bdcd79..9b8c121 100644 --- a/lib/src/service/downloader.dart +++ b/lib/src/service/downloader.dart @@ -13,22 +13,31 @@ class Downloader { Downloader({required this.dio, required this.cache}); - /// Downloads [url] into the cache at [destinationPath], verifies sha512 - /// (when provided), then atomically renames the temp file into place. + /// Downloads [url], verifies each supplied hash, then atomically renames + /// the temporary file into [destinationPath]. Cached files are verified too. /// Returns the downloaded [File]. If the file already exists at the - /// destination, no download is performed; if [expectedSha512] is given the - /// existing file is verified. + /// destination and satisfies the supplied hashes, no download is performed. Future downloadTo({ required String url, required String destinationPath, String? expectedSha512, + String? expectedSha1, }) async { + Future verify(File file) async { + if (expectedSha512 != null) { + await GitrinthCache.verifyFileSha512(file, expectedSha512); + } + if (expectedSha1 != null) { + await GitrinthCache.verifyFileSha1(file, expectedSha1); + } + } + cache.ensureRoot(); final destFile = File(destinationPath); if (destFile.existsSync()) { - if (expectedSha512 != null) { + if (expectedSha512 != null || expectedSha1 != null) { try { - await GitrinthCache.verifyFileSha512(destFile, expectedSha512); + await verify(destFile); } on UserError { destFile.deleteSync(); // fall through and re-download @@ -51,9 +60,7 @@ class Downloader { tmpPath, options: Options(responseType: ResponseType.bytes), ); - if (expectedSha512 != null) { - await GitrinthCache.verifyFileSha512(tmpFile, expectedSha512); - } + await verify(tmpFile); if (destFile.existsSync()) destFile.deleteSync(); tmpFile.renameSync(destinationPath); return File(destinationPath); diff --git a/lib/src/service/file_digest.dart b/lib/src/service/file_digest.dart new file mode 100644 index 0000000..66a73ca --- /dev/null +++ b/lib/src/service/file_digest.dart @@ -0,0 +1,29 @@ +import 'dart:io'; + +import 'package:crypto/crypto.dart'; + +import '../cli/exceptions.dart'; + +/// Hashes a file without retaining its contents in memory. +Future fileDigest(File file, Hash algorithm) async => + (await algorithm.bind(file.openRead()).first).toString(); + +/// Computes digests in algorithm order from a single file stream. +Future> fileDigests(File file, List algorithms) { + final stream = file.openRead().asBroadcastStream(); + return Future.wait([ + for (final algorithm in algorithms) + algorithm.bind(stream).first.then((digest) => digest.toString()), + ]); +} + +Future verifyFileDigest( + File file, + Hash algorithm, + String expected, +) async { + final actual = await fileDigest(file, algorithm); + if (actual.toLowerCase() != expected.toLowerCase()) { + throw UserError('checksum mismatch: expected $expected, got $actual'); + } +} diff --git a/lib/src/service/java_runtime_fetcher.dart b/lib/src/service/java_runtime_fetcher.dart index 882eb4a..266f354 100644 --- a/lib/src/service/java_runtime_fetcher.dart +++ b/lib/src/service/java_runtime_fetcher.dart @@ -84,29 +84,22 @@ class JavaRuntimeFetcher { ), ); - // No sentinel means an incomplete install; start fresh. - if (dir.existsSync() && !_hasSentinel(dir)) { - dir.deleteSync(recursive: true); - } - if (dir.existsSync() && _hasSentinel(dir)) { final java = _findJavaBinary(dir, osKey); if (java != null) return java; - // Sentinel without binary (truncated install) — start over. - dir.deleteSync(recursive: true); } Directory(_cache.runtimesRoot).createSync(recursive: true); final lockFile = File(_cache.javaRuntimeLockPath(feature.toString())); final lockHandle = lockFile.openSync(mode: FileMode.write); - lockHandle.lockSync(FileLock.exclusive); try { + lockHandle.lockSync(FileLock.exclusive); // Re-check after locking in case another process just finished. if (dir.existsSync() && _hasSentinel(dir)) { final cached = _findJavaBinary(dir, osKey); if (cached != null) return cached; - dir.deleteSync(recursive: true); } + if (dir.existsSync()) dir.deleteSync(recursive: true); _console.io( 'Downloading Temurin $feature (~190 MB) to ${dir.path}; ' diff --git a/lib/src/service/loader_binary_fetcher.dart b/lib/src/service/loader_binary_fetcher.dart index bd89fcd..3b545de 100644 --- a/lib/src/service/loader_binary_fetcher.dart +++ b/lib/src/service/loader_binary_fetcher.dart @@ -1,9 +1,11 @@ +import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; import '../cli/exceptions.dart'; import '../model/manifest/mods_yaml.dart'; +import '../util/atomic_file_writer.dart'; import '../util/url_template.dart'; import 'cache.dart'; import 'dio_error_helpers.dart'; @@ -19,6 +21,7 @@ import 'downloader.dart'; /// takes optional URL templates with `{mc}`/`{v}` placeholders, falling back /// to environment variables, and finally to the real upstream URLs. class LoaderBinaryFetcher { + final LoaderRegistry _registry; final GitrinthCache _cache; final Downloader _downloader; final String _forgeInstallerUrlTemplate; @@ -27,8 +30,10 @@ class LoaderBinaryFetcher { final String _fabricServerJarUrlTemplate; final String _fabricInstallerUrlTemplate; final String _fabricInstallerVersion; + final String _quiltInstallerMetaUrl; LoaderBinaryFetcher({ + LoaderRegistry registry = LoaderRegistry.builtins, required GitrinthCache cache, required Downloader downloader, Map? environment, @@ -38,7 +43,12 @@ class LoaderBinaryFetcher { String? fabricServerJarUrlTemplate, String? fabricInstallerUrlTemplate, String? fabricInstallerVersion, - }) : _cache = cache, + }) : _quiltInstallerMetaUrl = + (environment ?? + Platform.environment)['GITRINTH_QUILT_INSTALLER_META_URL'] ?? + 'https://meta.quiltmc.org/v3/versions/installer', + _registry = registry, + _cache = cache, _downloader = downloader, _forgeInstallerUrlTemplate = forgeInstallerUrlTemplate ?? @@ -87,6 +97,11 @@ class LoaderBinaryFetcher { required String mcVersion, required String modLoaderVersion, }) async { + _validateVersion(mcVersion); + _validateVersion(modLoaderVersion); + if (_registry[loader]?.installer == InstallerFamily.quilt) { + return _fetchQuiltInstaller(loader, mcVersion, modLoaderVersion); + } final (url, filename) = _serverUrlAndFilename( loader: loader, mcVersion: mcVersion, @@ -120,20 +135,21 @@ class LoaderBinaryFetcher { required String mcVersion, required String modLoaderVersion, }) async { - switch (loader) { - case ModLoader.vanilla: + _validateVersion(mcVersion); + _validateVersion(modLoaderVersion); + switch (_registry[loader]?.installer) { + case InstallerFamily.vanilla: throw StateError( 'fetchClientInstaller called for vanilla; gate on ' 'LoaderConfig.hasModRuntime.', ); - case ModLoader.forge: - case ModLoader.neoforge: + case InstallerFamily.forge: return fetchServerArtifact( loader: loader, mcVersion: mcVersion, modLoaderVersion: modLoaderVersion, ); - case ModLoader.fabric: + case InstallerFamily.fabric: final url = _fabricInstallerUrlTemplate.replaceAll( '{installerVersion}', _fabricInstallerVersion, @@ -154,6 +170,12 @@ class LoaderBinaryFetcher { context: 'failed to download Fabric installer from $url', ); } + case InstallerFamily.quilt: + return _fetchQuiltInstaller(loader, mcVersion, modLoaderVersion); + default: + throw ValidationError( + 'Loader ${loader.name} has no installed adapter for this operation.', + ); } } @@ -162,19 +184,19 @@ class LoaderBinaryFetcher { required String mcVersion, required String modLoaderVersion, }) { - switch (loader) { - case ModLoader.vanilla: + switch (_registry[loader]?.versionSource) { + case null: throw StateError( '_serverUrlAndFilename called for vanilla; gate on ' 'LoaderConfig.hasModRuntime.', ); - case ModLoader.forge: + case VersionSourceFamily.forge: final url = fillUrlTemplate(_forgeInstallerUrlTemplate, { 'mc': mcVersion, 'v': modLoaderVersion, }); return (url, 'forge-$mcVersion-$modLoaderVersion-installer.jar'); - case ModLoader.neoforge: + case VersionSourceFamily.neoforge: if (mcVersion == '1.20.1') { final url = fillUrlTemplate(_neoforgeLegacyInstallerUrlTemplate, { 'mc': mcVersion, @@ -186,12 +208,116 @@ class LoaderBinaryFetcher { 'v': modLoaderVersion, }); return (url, 'neoforge-$modLoaderVersion-installer.jar'); - case ModLoader.fabric: + case VersionSourceFamily.fabric: final url = fillUrlTemplate(_fabricServerJarUrlTemplate, { 'mc': mcVersion, 'v': modLoaderVersion, }); return (url, 'fabric-server-launch.jar'); + default: + throw ValidationError( + 'Loader ${loader.name} has no installed adapter for this operation.', + ); + } + } + + static String serverArtifactFilename( + LoaderId loader, + String mc, + String version, { + LoaderRegistry registry = LoaderRegistry.builtins, + }) { + final descriptor = registry[loader]; + return switch (descriptor?.installer) { + InstallerFamily.fabric => 'fabric-server-launch.jar', + InstallerFamily.quilt => 'quilt-installer.jar', + InstallerFamily.forge => + descriptor?.versionSource == VersionSourceFamily.neoforge && + mc != '1.20.1' + ? 'neoforge-$version-installer.jar' + : 'forge-$mc-$version-installer.jar', + _ => throw ValidationError( + 'Loader ${loader.name} has no cached server runtime recipe.', + ), + }; + } + + static void _validateVersion(String value) { + if (!isSafeRuntimeToken(value)) { + throw ValidationError('Unsafe runtime version "$value".'); + } + } + + Future _fetchQuiltInstaller( + LoaderId loader, + String mc, + String version, + ) async { + final destination = _cache.loaderArtifactPath( + loader: loader, + mcVersion: mc, + modLoaderVersion: version, + filename: 'quilt-installer.jar', + ); + final metadataFile = File('$destination.metadata.json'); + Map? selected; + if (metadataFile.existsSync()) { + try { + final raw = jsonDecode(metadataFile.readAsStringSync()); + if (raw is Map) { + _quiltInstallerSource(raw); + selected = raw; + } + } on FormatException { + // Invalid cache metadata is refreshed through the guarded transport. + } on FileSystemException { + // A concurrent cache replacement can be recovered the same way. + } on ValidationError { + // Revalidate provider metadata rather than trusting a broken sidecar. + } + } + if (selected == null) { + final Response response; + try { + response = await _downloader.dio.get(_quiltInstallerMetaUrl); + } on DioException catch (e) { + unwrapOrThrow(e, context: 'failed to refresh Quilt installer metadata'); + } + final raw = response.data; + if (raw is! List || raw.isEmpty || raw.first is! Map) { + throw const ValidationError('Quilt installer metadata is empty.'); + } + selected = Map.from(raw.first as Map); + } + final (url, hash) = _quiltInstallerSource(selected); + final artifact = await _downloader.downloadTo( + url: url, + destinationPath: destination, + expectedSha512: hash, + ); + atomicReplaceString(metadataFile.path, jsonEncode(selected)); + return artifact; + } + + static (String, String) _quiltInstallerSource(Map selected) { + final installerVersion = selected['version']; + final hashes = selected['hashes']; + final hash = hashes is Map ? hashes['sha512'] : null; + if (installerVersion is! String || + hash is! String || + !RegExp(r'^[a-fA-F0-9]{128}$').hasMatch(hash)) { + throw const ValidationError( + 'Quilt installer metadata lacks a version or SHA512.', + ); + } + _validateVersion(installerVersion); + final url = + 'https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/$installerVersion/quilt-installer-$installerVersion.jar'; + if (selected['url'] != url) { + throw const ValidationError( + 'Quilt installer metadata has an untrusted artifact URL.', + ); } + return (url, hash); } } diff --git a/lib/src/service/loader_catalog_cache.dart b/lib/src/service/loader_catalog_cache.dart new file mode 100644 index 0000000..0666b06 --- /dev/null +++ b/lib/src/service/loader_catalog_cache.dart @@ -0,0 +1,54 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:path/path.dart' as p; + +import '../cli/exceptions.dart'; +import '../util/atomic_file_writer.dart'; +import 'cache.dart'; + +/// Provider/host-scoped raw catalogs. Remote metadata cannot carry recipes. +class LoaderCatalogCache { + final GitrinthCache cache; + const LoaderCatalogCache(this.cache); + String path(String provider, String host) => p.join( + cache.root, + 'loader-catalogs', + provider, + GitrinthCache.hostCacheSegment(host), + 'catalog.json', + ); + List>? read(String provider, String host) { + final file = File(path(provider, host)); + if (!file.existsSync()) return null; + try { + final raw = jsonDecode(file.readAsStringSync()); + if (raw is! Map || + raw['version'] != 1 || + raw['provider'] != provider || + raw['host'] != host || + raw['entries'] is! List) { + throw const FormatException('catalog envelope mismatch'); + } + return (raw['entries'] as List) + .map((entry) => Map.from(entry as Map)) + .toList(); + } on Object catch (e) { + throw ValidationError( + 'Invalid loader catalog ${file.path}: $e. Refresh it online.', + ); + } + } + + void write(String provider, String host, List> entries) { + atomicReplaceString( + path(provider, host), + jsonEncode({ + 'version': 1, + 'provider': provider, + 'host': host, + 'entries': entries, + }), + ); + } +} diff --git a/lib/src/service/loader_client_installer.dart b/lib/src/service/loader_client_installer.dart index 9ef5d53..6695221 100644 --- a/lib/src/service/loader_client_installer.dart +++ b/lib/src/service/loader_client_installer.dart @@ -15,17 +15,20 @@ import 'server_installer.dart' show ProcessRunner, spawnInstaller; /// libraries. Returns the `lastVersionId` string suitable for plumbing into /// `launcher_profiles.json`. class LoaderClientInstaller { + final LoaderRegistry _registry; final ProcessRunner? _runProcess; final Map _environment; final JavaRuntimeResolver? _resolver; final Console _console; LoaderClientInstaller({ + LoaderRegistry registry = LoaderRegistry.builtins, ProcessRunner? runProcess, Map? environment, JavaRuntimeResolver? resolver, Console? console, - }) : _runProcess = runProcess, + }) : _registry = registry, + _runProcess = runProcess, _environment = environment ?? Platform.environment, _resolver = resolver, _console = console ?? const Console(); @@ -41,8 +44,15 @@ class LoaderClientInstaller { bool allowManagedJava = true, bool verbose = false, }) async { + if (!isSafeRuntimeToken(mcVersion) || + !isSafeRuntimeToken(modLoaderVersion)) { + throw const ValidationError( + 'Unsafe runtime version; installer versions cannot contain paths, URLs, or options.', + ); + } final versionId = expectedClientVersionId( loader: loader, + registry: _registry, mcVersion: mcVersion, modLoaderVersion: modLoaderVersion, ); @@ -126,21 +136,28 @@ class LoaderClientInstaller { /// Must match what the installer writes to `/versions/`. static String expectedClientVersionId({ required ModLoader loader, + LoaderRegistry registry = LoaderRegistry.builtins, required String mcVersion, required String modLoaderVersion, }) { - switch (loader) { - case ModLoader.vanilla: + switch (registry[loader]?.installer) { + case InstallerFamily.vanilla: throw StateError( 'expectedClientVersionId called for vanilla; gate on ' 'LoaderConfig.hasModRuntime.', ); - case ModLoader.fabric: + case InstallerFamily.fabric: return 'fabric-loader-$modLoaderVersion-$mcVersion'; - case ModLoader.forge: - return '$mcVersion-forge-$modLoaderVersion'; - case ModLoader.neoforge: - return 'neoforge-$modLoaderVersion'; + case InstallerFamily.forge: + return registry[loader]?.versionSource == VersionSourceFamily.neoforge + ? 'neoforge-$modLoaderVersion' + : '$mcVersion-forge-$modLoaderVersion'; + case InstallerFamily.quilt: + return 'quilt-loader-$modLoaderVersion-$mcVersion'; + default: + throw ValidationError( + 'Loader ${loader.name} has no installed adapter for this operation.', + ); } } @@ -151,13 +168,13 @@ class LoaderClientInstaller { required File installerJar, required Directory dotMinecraftDir, }) { - switch (loader) { - case ModLoader.vanilla: + switch (_registry[loader]?.installer) { + case InstallerFamily.vanilla: throw StateError( '_installArgs called for vanilla; gate on ' 'LoaderConfig.hasModRuntime.', ); - case ModLoader.fabric: + case InstallerFamily.fabric: return [ '-jar', installerJar.path, @@ -169,14 +186,27 @@ class LoaderClientInstaller { '-loader', modLoaderVersion, ]; - case ModLoader.forge: - case ModLoader.neoforge: + case InstallerFamily.quilt: + return [ + '-jar', + installerJar.path, + 'install', + 'client', + mcVersion, + modLoaderVersion, + '--install-dir="${dotMinecraftDir.path}"', + ]; + case InstallerFamily.forge: return [ '-jar', installerJar.path, '--installClient', dotMinecraftDir.path, ]; + default: + throw ValidationError( + 'Loader ${loader.name} has no installed adapter for this operation.', + ); } } diff --git a/lib/src/service/loader_descriptor_config.dart b/lib/src/service/loader_descriptor_config.dart new file mode 100644 index 0000000..01b58c1 --- /dev/null +++ b/lib/src/service/loader_descriptor_config.dart @@ -0,0 +1,182 @@ +import 'dart:io'; + +import 'package:yaml/yaml.dart'; + +import '../cli/exceptions.dart'; +import '../model/loader/loader_descriptor.dart'; +import '../model/loader/loader_id.dart'; +import 'modrinth/modrinth_url.dart'; + +/// User-owned, declarative mappings. No executable fields are accepted. +class LoaderDescriptorConfig { + static LoaderRegistry read(String path, {bool required = false}) { + final file = File(path); + if (!file.existsSync()) { + if (required) { + throw ValidationError('Loader descriptor file not found: $path'); + } + return LoaderRegistry.builtins; + } + try { + return parse(file.readAsStringSync()); + } on GitrinthException { + rethrow; + } on Object catch (e) { + throw ValidationError('Invalid loader descriptors at $path: $e'); + } + } + + static LoaderRegistry parse(String yaml) { + final raw = loadYaml(yaml); + if (raw is! Map || raw['version'] != 1 || raw['loaders'] is! Map) { + throw const ValidationError( + 'Loader descriptors require version: 1 and a loaders mapping.', + ); + } + _keys(raw, const {'version', 'loaders'}); + final additions = []; + final tags = {}; + for (final entry in (raw['loaders'] as Map).entries) { + if (entry.key is! String || entry.value is! Map) { + throw const ValidationError( + 'Loader descriptors must map names to objects.', + ); + } + final id = LoaderId(entry.key as String); + if (LoaderRegistry.builtins[id] != null) { + throw ValidationError( + 'Loader ${id.name} cannot shadow a built-in identity or alias.', + ); + } + final data = entry.value as Map; + _keys(data, const { + 'role', + 'modrinth', + 'curseforge_type', + 'curseforge_plugins', + 'archive', + 'version_source', + 'installer_family', + 'compatible_mods', + 'server_launch_jar', + 'server_run_script', + }); + final role = _enum(LoaderRole.values, data['role'], 'role'); + final mr = {}; + final provider = data['modrinth']; + if (provider != null) { + if (provider is! Map) { + throw const ValidationError( + 'modrinth must map provider hosts to tags.', + ); + } + for (final mapping in provider.entries) { + if (mapping.key is! String || mapping.value is! String) { + throw const ValidationError('Modrinth mappings must be strings.'); + } + final host = normalizeServerKey(mapping.key as String); + final tag = LoaderId(mapping.value as String).name; + final key = '$host/$tag'; + if (tags.containsKey(key) || + LoaderRegistry.builtins.descriptors.values.any( + (d) => d.modrinthTag(host) == tag, + )) { + throw ValidationError('Modrinth alias collision for $key.'); + } + tags[key] = id.name; + mr[host] = tag; + } + } + final cf = data['curseforge_type']; + if (cf != null && (cf is! int || cf <= 0)) { + throw const ValidationError( + 'curseforge_type must be a positive integer.', + ); + } + if (cf != null && + [ + ...LoaderRegistry.builtins.descriptors.values, + ...additions, + ].any((d) => d.curseforgeType == cf)) { + throw ValidationError( + 'CurseForge type $cf already belongs to another loader.', + ); + } + final archive = data['archive']; + if (archive != null && archive is! Map) { + throw const ValidationError('archive must be a mapping.'); + } + final archiveMap = archive as Map? ?? const {}; + _keys(archiveMap, const {'modrinth', 'curseforge'}); + String? token(Object? value) => + value == null ? null : LoaderId(value as String).name; + final compatible = data['compatible_mods']; + if (compatible != null && compatible is! List) { + throw const ValidationError('compatible_mods must be a list.'); + } + final jar = data['server_launch_jar']; + if (jar != null && + (jar is! String || + !RegExp(r'^[a-zA-Z0-9_.-]+\.jar$').hasMatch(jar))) { + throw const ValidationError( + 'server_launch_jar must be a plain JAR filename.', + ); + } + for (final key in ['curseforge_plugins', 'server_run_script']) { + if (data[key] != null && data[key] is! bool) { + throw ValidationError('$key must be boolean.'); + } + } + additions.add( + LoaderDescriptor( + id: id, + role: role, + modrinthTags: mr, + curseforgeType: cf as int?, + curseforgePlugins: data['curseforge_plugins'] == true, + modrinthArchiveKey: token(archiveMap['modrinth']), + curseforgeArchiveToken: token(archiveMap['curseforge']), + versionSource: data['version_source'] == null + ? VersionSourceFamily.catalog + : _enum( + VersionSourceFamily.values, + data['version_source'], + 'version_source', + ), + installer: data['installer_family'] == null + ? null + : _enum( + InstallerFamily.values, + data['installer_family'], + 'installer_family', + ), + compatibleMods: [ + for (final value in compatible as List? ?? []) + LoaderId(value as String), + ], + serverLaunchJar: jar as String?, + serverRunScript: data['server_run_script'] == true, + ), + ); + } + return LoaderRegistry.builtins.merge(additions); + } + + static T _enum(List values, Object? raw, String key) { + final found = values.where((value) => value.name == raw).firstOrNull; + if (found == null) { + throw ValidationError( + 'Unknown $key "$raw"; use ${values.map((v) => v.name).join(', ')}.', + ); + } + return found; + } + + static void _keys(Map data, Set allowed) { + for (final key in data.keys) { + if (!allowed.contains(key)) { + throw ValidationError('Unknown loader descriptor key "$key".'); + } + } + } +} diff --git a/lib/src/service/loader_registry.dart b/lib/src/service/loader_registry.dart new file mode 100644 index 0000000..8553ff8 --- /dev/null +++ b/lib/src/service/loader_registry.dart @@ -0,0 +1,244 @@ +import 'package:dio/dio.dart'; + +import '../cli/exceptions.dart'; +import '../model/loader/loader_descriptor.dart'; +import '../model/loader/loader_id.dart'; +import 'loader_catalog_cache.dart'; +import 'modrinth/modrinth_url.dart'; + +export '../model/loader/loader_descriptor.dart'; +export '../model/loader/loader_id.dart'; + +/// Produces immutable snapshots; it never changes a process-wide registry. +class LoaderRegistryService { + final LoaderRegistry configured; + final LoaderCatalogCache cache; + final Dio dio; + const LoaderRegistryService({ + required this.configured, + required this.cache, + required this.dio, + }); + + Future snapshot({ + required String modrinthHost, + required String curseforgeHost, + required Iterable needed, + bool offline = false, + bool refresh = false, + bool discoverModrinth = true, + Future>> Function()? readCurseforge, + }) async { + final mrHost = normalizeServerKey(modrinthHost); + final cfHost = normalizeServerKey(curseforgeHost); + var registry = configured; + void mergeCached( + String provider, + String host, + LoaderRegistry Function(LoaderRegistry, List>) merge, + ) { + try { + final entries = cache.read(provider, host); + if (entries != null) registry = merge(registry, entries); + } on ValidationError { + if (offline) rethrow; + } + } + + if (discoverModrinth) { + mergeCached( + 'modrinth', + mrHost, + (current, entries) => mergeModrinth(current, mrHost, entries), + ); + } + if (readCurseforge != null) { + mergeCached('curseforge', cfHost, mergeCurseforge); + } + final missing = needed + .where( + (id) => + registry[id] == null || + (discoverModrinth && + registry[id]?.role != LoaderRole.vanilla && + registry[id]?.modrinthTag(mrHost) == null), + ) + .toList(); + if (offline) { + if (missing.isNotEmpty) { + throw ValidationError( + 'No offline loader capability snapshot for ${missing.join(', ')}. Run online once or supply loaders.yaml.', + ); + } + return registry; + } + if (!refresh && missing.isEmpty) return registry; + if (discoverModrinth) { + final response = await _request( + () => dio.get('$mrHost/tag/loader'), + mrHost, + ); + if (response.data is! List) { + throw const ValidationError( + 'Modrinth loader discovery returned no catalog.', + ); + } + final mr = (response.data as List) + .map((value) => Map.from(value as Map)) + .toList(); + registry = mergeModrinth(registry, mrHost, mr); + cache.write('modrinth', mrHost, mr); + } + if (readCurseforge != null) { + final cf = await _request(readCurseforge, cfHost); + registry = mergeCurseforge(registry, cf); + cache.write('curseforge', cfHost, cf); + } + for (final id in needed) { + if (registry[id] == null) { + throw ValidationError( + 'Loader $id was not discovered by the selected providers; supply an explicit loaders.yaml mapping.', + ); + } + } + return registry; + } + + static Future _request( + Future Function() request, + String host, + ) async { + try { + return await request(); + } on DioException catch (e) { + final cause = e.error; + if (cause is GitrinthException) throw cause; + throw UserError( + 'Loader discovery failed for $host (HTTP ${e.response?.statusCode ?? "unavailable"}).', + ); + } + } + + static LoaderRegistry mergeModrinth( + LoaderRegistry registry, + String host, + List> entries, + ) { + final additions = []; + final projectTypes = >{}; + for (final entry in entries) { + final rawName = entry['name']; + final rawTypes = entry['supported_project_types']; + if (rawName is! String || + rawTypes is! List || + rawTypes.any((value) => value is! String)) { + throw ValidationError('Invalid loader catalog fields from $host.'); + } + final LoaderId id; + try { + id = LoaderId(rawName); + } on FormatException catch (e) { + throw ValidationError( + 'Invalid loader catalog identity from $host: ${e.message}', + ); + } + (projectTypes[id] ??= {}).addAll(rawTypes.cast()); + } + for (final entry in projectTypes.entries) { + final id = entry.key; + final types = entry.value; + final existing = registry[id]; + if (existing != null) { + // Exact names are identities, never aliases for a different provider ID. + final mapped = existing.modrinthTag(host); + if (mapped != null && mapped != id.name) { + throw ValidationError('Loader alias collision for $host/${id.name}.'); + } + additions.add(existing.withProvider(host: host, tag: id.name)); + continue; + } + final role = types.contains('plugin') + ? LoaderRole.plugin + : types.contains('shader') + ? LoaderRole.shader + : types.contains('mod') + ? LoaderRole.mod + : null; + if (role == null) continue; + additions.add( + LoaderDescriptor( + id: id, + role: role, + modrinthTags: {host: id.name}, + versionSource: VersionSourceFamily.catalog, + ), + ); + } + return registry.merge(additions); + } + + static LoaderRegistry mergeCurseforge( + LoaderRegistry registry, + List> entries, + ) { + final byType = >{}; + for (final entry in entries) { + final type = entry['type']; + final name = entry['name']; + if (type is! int || type <= 0 || name is! String) continue; + (byType[type] ??= []).add( + LoaderCatalogVersion( + name: name, + gameVersion: entry['gameVersion'] is String + ? entry['gameVersion'] as String + : null, + latest: entry['latest'] == true, + recommended: entry['recommended'] == true, + ), + ); + } + final additions = []; + for (final entry in byType.entries) { + final existing = registry.descriptors.values + .where((d) => d.curseforgeType == entry.key) + .firstOrNull; + if (existing != null) { + additions.add( + existing.withProvider( + versions: entry.value.map((v) => v.name).toList(), + catalogVersions: entry.value, + ), + ); + } else { + additions.add( + LoaderDescriptor( + id: LoaderId('curseforge-type-${entry.key}'), + role: LoaderRole.mod, + curseforgeType: entry.key, + versions: entry.value.map((v) => v.name).toList(), + catalogVersions: entry.value, + versionSource: VersionSourceFamily.catalog, + ), + ); + } + } + return registry.merge(additions); + } +} + +/// Rejects hybrid combinations whose explicit compatibility excludes the mod runtime. +void validateLoaderCompatibility( + LoaderRegistry registry, + LoaderId mods, + LoaderId? plugins, +) { + final descriptor = registry[plugins]; + if (mods != LoaderId.vanilla && + descriptor != null && + descriptor.compatibleMods.isNotEmpty && + !descriptor.compatibleMods.contains(mods)) { + throw ValidationError( + 'Plugin loader ${descriptor.id.name} does not support mod loader ${mods.name}; compatible mod loaders: ${descriptor.compatibleMods.join(', ')}.', + ); + } +} diff --git a/lib/src/service/lock_builder.dart b/lib/src/service/lock_builder.dart deleted file mode 100644 index a42964f..0000000 --- a/lib/src/service/lock_builder.dart +++ /dev/null @@ -1,150 +0,0 @@ -part of 'resolve_and_sync.dart'; - -/// Builds the new `ModsLock` from the resolved Modrinth versions plus the -/// manifest's url:/path: entries (which never enter the resolver) and -/// `files:` entries (which are manifest-only). -ModsLock _buildLock( - ModsYaml manifest, - ResolutionResult resolution, - String? resolvedModLoaderVersion, - String? resolvedPluginLoaderVersion, -) { - final mods = {}; - final resourcePacks = {}; - final dataPacks = {}; - final shaders = {}; - final plugins = {}; - final byKind = >{ - Section.mods: mods, - Section.resourcePacks: resourcePacks, - Section.dataPacks: dataPacks, - Section.shaders: shaders, - Section.plugins: plugins, - }; - for (final r in resolution.entries) { - final sectionMap = byKind[r.section]; - if (sectionMap == null) { - throw StateError('lock_builder: unknown section ${r.section}'); - } - final entry = manifest.sectionEntries(r.section)[r.slug]; - sectionMap[r.slug] = LockedEntry( - slug: r.slug, - sourceKind: LockedSourceKind.modrinth, - version: r.version.versionNumber, - projectId: r.version.projectId, - versionId: r.version.id, - file: LockedFile( - name: r.file.filename, - url: r.file.url, - sha1: r.file.hashes['sha1'], - sha512: r.file.sha512, - size: r.file.size, - ), - client: r.client, - server: r.server, - dependency: r.dependency, - gameVersions: List.unmodifiable(r.version.gameVersions), - acceptsMc: List.unmodifiable(entry?.acceptsMc ?? const []), - ); - } - for (final section in Section.values) { - final sectionMap = byKind[section]; - if (sectionMap == null) { - throw StateError('lock_builder: unknown section $section'); - } - final entries = manifest.sectionEntries(section); - entries.forEach((slug, entry) { - final src = entry.source; - if (src is UrlEntrySource) { - sectionMap[slug] = LockedEntry( - slug: slug, - sourceKind: LockedSourceKind.url, - file: LockedFile(name: _filenameFromUrl(src.url), url: src.url), - client: entry.client, - server: entry.server, - ); - } else if (src is PathEntrySource) { - sectionMap[slug] = LockedEntry( - slug: slug, - sourceKind: LockedSourceKind.path, - path: src.path, - client: entry.client, - server: entry.server, - ); - } - }); - } - // Bake the resolved concrete loader version into the lock's LoaderConfig. - final lockedLoader = LoaderConfig( - mods: manifest.loader.mods, - modLoaderVersion: resolvedModLoaderVersion, - shaders: manifest.loader.shaders, - plugins: manifest.loader.plugins, - pluginLoaderVersion: resolvedPluginLoaderVersion, - ); - // Forward `files:` entries verbatim. `files:` is manifest-only — no - // pubgrub resolution, no Modrinth round-trip — so the lock entry is - // a 1:1 mirror of the FileEntry. Source-existence is verified later - // in the artifact-fetch loop (mirrors `path:` mod entries). - final lockedFiles = { - for (final e in manifest.files.entries) - e.key: LockedFileEntry( - destination: e.value.destination, - sourcePath: e.value.sourcePath, - client: e.value.client, - server: e.value.server, - preserve: e.value.preserve, - ), - }; - return ModsLock( - gitrinthVersion: packageVersion, - loader: lockedLoader, - mcVersion: manifest.mcVersion, - mods: mods, - resourcePacks: resourcePacks, - dataPacks: dataPacks, - shaders: shaders, - plugins: plugins, - files: lockedFiles, - ); -} - -/// Writes a `version.json` next to each resolved Modrinth artifact in -/// the cache, capturing the full Modrinth `Version` payload — its -/// `dependencies` array (consumed by `upgrade --unlock-transitive`) and -/// its `files` array (consumed by `cache repair` to verify and refetch -/// corrupt artifacts). Best effort: a write failure logs a warn but -/// doesn't abort the resolve, because the artifact download is the -/// user-visible contract. -void _persistVersionMetadata( - ResolutionResult resolution, - GitrinthCache cache, - Console console, -) { - for (final r in resolution.entries) { - final pid = r.version.projectId; - final vid = r.version.id; - if (pid.isEmpty || vid.isEmpty) continue; - final path = cache.modrinthVersionMetadataPath( - projectId: pid, - versionId: vid, - ); - try { - Directory(p.dirname(path)).createSync(recursive: true); - File(path).writeAsStringSync( - const JsonEncoder.withIndent(' ').convert(r.version.toMap()), - ); - } on Object catch (e) { - console.warn( - 'cache: failed to persist version metadata for ${r.slug} ' - '(${r.version.versionNumber}): $e', - ); - } - } -} - -String _filenameFromUrl(String url) { - final uri = Uri.parse(url); - if (uri.pathSegments.isEmpty) return 'artifact.jar'; - return uri.pathSegments.last; -} diff --git a/lib/src/service/lock_graph.dart b/lib/src/service/lock_graph.dart deleted file mode 100644 index b93dbfa..0000000 --- a/lib/src/service/lock_graph.dart +++ /dev/null @@ -1,108 +0,0 @@ -import 'dart:convert'; -import 'dart:io'; - -import '../model/manifest/mods_lock.dart'; -import 'cache.dart'; -import 'console.dart'; - -/// Reads the `dependencies` array out of the cached `version.json` -/// and returns the list of `project_id`s for entries whose -/// `dependency_type == "required"`. Returns null when the cache file -/// is missing or unparseable (cold cache); returns an empty list when -/// the version legitimately has no required deps. -List? readCachedRequiredChildren( - GitrinthCache cache, - String projectId, - String versionId, -) { - final path = cache.modrinthVersionMetadataPath( - projectId: projectId, - versionId: versionId, - ); - final file = File(path); - if (!file.existsSync()) return null; - final dynamic raw; - try { - raw = jsonDecode(file.readAsStringSync()); - } on Object { - return null; - } - if (raw is! Map) return null; - final deps = raw['dependencies']; - if (deps is! List) return const []; - final out = []; - for (final d in deps) { - if (d is! Map) continue; - if (d['dependency_type'] != 'required') continue; - final pid = d['project_id']; - if (pid is String && pid.isNotEmpty) out.add(pid); - } - return out; -} - -/// BFS over the dep graph to compute the transitive closure of -/// [seeds]. Edges are read from the artifact cache's per-version -/// `version.json` (mirrors dart pub's "graph in cache" architecture -/// — see [GitrinthCache.modrinthVersionMetadataPath]). -/// -/// Cold-cache entries (a slug whose `version.json` hasn't been written -/// yet) are reported via `console.detail` (gated on the optional -/// [verboseLabel] for caller-specific phrasing) and their children are -/// skipped; subsequent runs populate the cache. -/// -/// When [lock] is null, the closure is just [seeds]: there is no -/// graph to walk yet. -Set walkTransitiveClosure( - Set seeds, - ModsLock? lock, - GitrinthCache cache, { - Console? console, - String verboseLabel = 'walkTransitiveClosure', -}) { - if (lock == null) { - console?.io( - '$verboseLabel: no mods.lock found yet — ' - 'falling back to the named entries.', - ); - return seeds; - } - - final lookup = {}; - final projectIdToSlug = {}; - for (final entry in lock.allEntries) { - lookup[entry.key] = entry.value; - final pid = entry.value.projectId; - if (pid != null) projectIdToSlug[pid] = entry.key; - } - - final closure = {...seeds}; - final queue = [...seeds]; - while (queue.isNotEmpty) { - final slug = queue.removeLast(); - final entry = lookup[slug]; - if (entry == null) { - console?.io("$verboseLabel: '$slug' not in mods.lock; skipping."); - continue; - } - if (entry.sourceKind != LockedSourceKind.modrinth) continue; - final pid = entry.projectId; - final vid = entry.versionId; - if (pid == null || vid == null) continue; - - final children = readCachedRequiredChildren(cache, pid, vid); - if (children == null) { - console?.io( - "$verboseLabel: no cached version.json for " - "'$slug' ($pid/$vid); skipping its transitive children. " - 'Run `gitrinth get` to populate the cache.', - ); - continue; - } - for (final childPid in children) { - final childSlug = projectIdToSlug[childPid]; - if (childSlug == null) continue; - if (closure.add(childSlug)) queue.add(childSlug); - } - } - return closure; -} diff --git a/lib/src/service/modrinth_api.dart b/lib/src/service/modrinth/modrinth_api.dart similarity index 82% rename from lib/src/service/modrinth_api.dart rename to lib/src/service/modrinth/modrinth_api.dart index 1f4b91a..881e538 100644 --- a/lib/src/service/modrinth_api.dart +++ b/lib/src/service/modrinth/modrinth_api.dart @@ -3,9 +3,9 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:retrofit/retrofit.dart'; -import '../model/modrinth/game_version.dart'; -import '../model/modrinth/project.dart'; -import '../model/modrinth/version.dart'; +import '../../model/modrinth/game_version.dart'; +import '../../model/modrinth/project.dart'; +import '../../model/modrinth/version.dart'; import 'modrinth_auth_interceptor.dart'; part 'modrinth_api.g.dart'; @@ -17,6 +17,16 @@ abstract class ModrinthApi { @GET('/project/{slug}') Future getProject(@Path('slug') String slug); + @GET('/project/{slug}/members') + Future> getProjectMembers(@Path('slug') String slug); + + @GET('/search') + Future> searchProjects({ + @Query('query') required String query, + @Query('facets') required String facets, + @Query('limit') int limit = 20, + }); + /// Modrinth's project-validity endpoint. Returns 200 with `{"id": ...}` if a /// project with the given slug already exists, or 404 if the slug is free. /// Despite the published docs page describing the route as HEAD, the actual diff --git a/lib/src/service/modrinth_api.g.dart b/lib/src/service/modrinth/modrinth_api.g.dart similarity index 77% rename from lib/src/service/modrinth_api.g.dart rename to lib/src/service/modrinth/modrinth_api.g.dart index 5483ea8..b267a69 100644 --- a/lib/src/service/modrinth_api.g.dart +++ b/lib/src/service/modrinth/modrinth_api.g.dart @@ -46,6 +46,58 @@ class _ModrinthApi implements ModrinthApi { return _value; } + @override + Future> getProjectMembers(String slug) async { + final _extra = {}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType>( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/project/${slug}/members', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch(_options); + final _value = _result.data; + final httpResponse = HttpResponse(_value, _result); + return httpResponse; + } + + @override + Future> searchProjects({ + required String query, + required String facets, + int limit = 20, + }) async { + final _extra = {}; + final queryParameters = { + r'query': query, + r'facets': facets, + r'limit': limit, + }; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType>( + Options(method: 'GET', headers: _headers, extra: _extra) + .compose( + _dio.options, + '/search', + queryParameters: queryParameters, + data: _data, + ) + .copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)), + ); + final _result = await _dio.fetch(_options); + final _value = _result.data; + final httpResponse = HttpResponse(_value, _result); + return httpResponse; + } + @override Future> checkProjectValidity(String slug) async { final _extra = {}; diff --git a/lib/src/service/modrinth/modrinth_api_factory.dart b/lib/src/service/modrinth/modrinth_api_factory.dart new file mode 100644 index 0000000..b1ad0de --- /dev/null +++ b/lib/src/service/modrinth/modrinth_api_factory.dart @@ -0,0 +1,91 @@ +import 'package:dio/dio.dart'; + +import '../../version.dart'; +import '../console.dart'; +import '../offline_guard_interceptor.dart'; +import 'modrinth_api.dart'; +import 'modrinth_api_source.dart'; +import 'modrinth_auth_interceptor.dart'; +import 'modrinth_error_interceptor.dart'; +import 'modrinth_rate_limit_interceptor.dart'; +import 'modrinth_url.dart'; + +/// Builds and caches one [ModrinthApi] (with its own [Dio] + +/// [ModrinthRateLimitInterceptor]) per Modrinth-protocol host. +/// +/// Modrinth's rate-limit budget is per-IP-per-host, so each labrinth +/// deployment gets its own interceptor instance. Auth, offline, and +/// error behavior are wired the same way the global `dioProvider` +/// wires them — they're just one factory away now instead of one +/// shared singleton. +class ModrinthApiFactory implements ModrinthApiSource { + final Console _console; + final ModrinthAuthInterceptor _auth; + final bool Function() _offline; + @override + final String defaultBaseUrl; + + final Map _bundles = {}; + + ModrinthApiFactory({ + required Console console, + required ModrinthAuthInterceptor auth, + required bool Function() offline, + required this.defaultBaseUrl, + }) : _console = console, + _auth = auth, + _offline = offline; + + /// Returns the cached [ModrinthApi] for [hostUrl], building one if + /// missing. `null` / empty resolves to [_defaultBaseUrl]. + @override + ModrinthApi forHost(String? hostUrl) { + final raw = (hostUrl == null || hostUrl.trim().isEmpty) + ? defaultBaseUrl + : hostUrl.trim(); + final key = _normalize(raw); + final cached = _bundles[key]; + if (cached != null) return cached.api; + final dio = Dio(); + dio + ..interceptors.add(OfflineGuardInterceptor(_offline)) + ..interceptors.add(_auth) + ..interceptors.add( + ModrinthRateLimitInterceptor( + dio: dio, + modrinthBaseUrl: raw, + console: _console, + ), + ) + ..interceptors.add(ModrinthErrorInterceptor()) + ..options.headers['User-Agent'] = + 'gitrinth/$packageVersion (+github.com/getBoolean/gitrinth)'; + final api = ModrinthApi(dio, baseUrl: raw); + _bundles[key] = _HostBundle(dio: dio, api: api); + return api; + } + + /// Disposes every cached client. Riverpod's `onDispose` plumbing + /// calls this when the factory provider tears down. + void close() { + for (final bundle in _bundles.values) { + bundle.dio.close(force: true); + } + _bundles.clear(); + } + + static String _normalize(String url) { + try { + return normalizeServerKey(url); + } on FormatException { + return url; + } + } +} + +class _HostBundle { + final Dio dio; + final ModrinthApi api; + + _HostBundle({required this.dio, required this.api}); +} diff --git a/lib/src/service/modrinth/modrinth_api_source.dart b/lib/src/service/modrinth/modrinth_api_source.dart new file mode 100644 index 0000000..d0e2157 --- /dev/null +++ b/lib/src/service/modrinth/modrinth_api_source.dart @@ -0,0 +1,7 @@ +import 'modrinth_api.dart'; + +/// Host-addressed public API access, without ownership of the transport. +abstract interface class ModrinthApiSource { + String get defaultBaseUrl; + ModrinthApi forHost(String? hostUrl); +} diff --git a/lib/src/service/modrinth_auth_interceptor.dart b/lib/src/service/modrinth/modrinth_auth_interceptor.dart similarity index 98% rename from lib/src/service/modrinth_auth_interceptor.dart rename to lib/src/service/modrinth/modrinth_auth_interceptor.dart index 72c8532..407f8f1 100644 --- a/lib/src/service/modrinth_auth_interceptor.dart +++ b/lib/src/service/modrinth/modrinth_auth_interceptor.dart @@ -1,7 +1,7 @@ import 'package:dio/dio.dart'; -import '../cli/exceptions.dart'; -import 'dio_error_helpers.dart'; +import '../../cli/exceptions.dart'; +import '../dio_error_helpers.dart'; import 'modrinth_url.dart'; /// Marker key on `RequestOptions.extra` that opts a request into auth. diff --git a/lib/src/service/modrinth_error_interceptor.dart b/lib/src/service/modrinth/modrinth_error_interceptor.dart similarity index 97% rename from lib/src/service/modrinth_error_interceptor.dart rename to lib/src/service/modrinth/modrinth_error_interceptor.dart index a6c44e9..a387089 100644 --- a/lib/src/service/modrinth_error_interceptor.dart +++ b/lib/src/service/modrinth/modrinth_error_interceptor.dart @@ -2,8 +2,8 @@ import 'dart:convert'; import 'package:dio/dio.dart'; -import '../cli/exceptions.dart'; -import 'dio_error_helpers.dart'; +import '../../cli/exceptions.dart'; +import '../dio_error_helpers.dart'; class ModrinthErrorInterceptor extends Interceptor { @override diff --git a/lib/src/service/modrinth_project_url.dart b/lib/src/service/modrinth/modrinth_project_url.dart similarity index 100% rename from lib/src/service/modrinth_project_url.dart rename to lib/src/service/modrinth/modrinth_project_url.dart diff --git a/lib/src/service/modrinth_rate_limit_interceptor.dart b/lib/src/service/modrinth/modrinth_rate_limit_interceptor.dart similarity index 99% rename from lib/src/service/modrinth_rate_limit_interceptor.dart rename to lib/src/service/modrinth/modrinth_rate_limit_interceptor.dart index 78ca11e..8291cde 100644 --- a/lib/src/service/modrinth_rate_limit_interceptor.dart +++ b/lib/src/service/modrinth/modrinth_rate_limit_interceptor.dart @@ -2,7 +2,7 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'console.dart'; +import '../console.dart'; /// Coordinates outbound calls to the Modrinth API against the published /// 300 req/min/IP budget. Reads `X-Ratelimit-*` headers on every response, diff --git a/lib/src/service/modrinth/modrinth_resolution_snapshot.dart b/lib/src/service/modrinth/modrinth_resolution_snapshot.dart new file mode 100644 index 0000000..3f97bdb --- /dev/null +++ b/lib/src/service/modrinth/modrinth_resolution_snapshot.dart @@ -0,0 +1,109 @@ +import 'package:retrofit/retrofit.dart'; + +import '../../model/modrinth/game_version.dart'; +import '../../model/modrinth/project.dart'; +import '../../model/modrinth/version.dart'; +import 'modrinth_api.dart'; +import 'modrinth_api_source.dart'; +import 'modrinth_url.dart'; + +/// Public metadata shared by solver passes within a single resolution. +/// Construct a new snapshot for each preview or write; transport ownership +/// and authenticated requests remain with the underlying source. +final class ModrinthResolutionSnapshot implements ModrinthApiSource { + final ModrinthApiSource _source; + final _hosts = {}; + + ModrinthResolutionSnapshot(this._source); + + @override + String get defaultBaseUrl => _source.defaultBaseUrl; + + @override + ModrinthApi forHost(String? hostUrl) { + final raw = hostUrl == null || hostUrl.trim().isEmpty + ? defaultBaseUrl + : hostUrl; + final host = normalizeServerKey(raw); + return _hosts.putIfAbsent(host, () => _SnapshotApi(_source.forHost(host))); + } +} + +final class _SnapshotApi implements ModrinthApi { + final ModrinthApi _api; + final _projects = >{}; + final _members = >>{}; + final _versions = <(String, String?, String?), Future>>{}; + final _searches = <(String, String, int), Future>>{}; + + _SnapshotApi(this._api); + + @override + Future getProject(String slug) => _projects.putIfAbsent( + slug, + () => _api + .getProject(slug) + .then( + (project) => project.copyWith( + loaders: List.unmodifiable(project.loaders), + categories: List.unmodifiable(project.categories), + ), + ), + ); + + @override + Future> getProjectMembers(String slug) => + _members.putIfAbsent( + slug, + () => _api.getProjectMembers(slug).then(_immutableResponse), + ); + + @override + Future> listVersions( + String slug, { + String? loadersJson, + String? gameVersionsJson, + }) => _versions.putIfAbsent( + (slug, loadersJson, gameVersionsJson), + () => _api + .listVersions( + slug, + loadersJson: loadersJson, + gameVersionsJson: gameVersionsJson, + ) + .then((versions) => List.unmodifiable(versions)), + ); + + @override + Future> searchProjects({ + required String query, + required String facets, + int limit = 20, + }) => _searches.putIfAbsent( + (query, facets, limit), + () => _api + .searchProjects(query: query, facets: facets, limit: limit) + .then(_immutableResponse), + ); + + @override + Future> getGameVersions() => _api.getGameVersions(); + + @override + Future> checkProjectValidity(String slug) => + _api.checkProjectValidity(slug); + + @override + Future> getCurrentUser() => _api.getCurrentUser(); +} + +HttpResponse _immutableResponse(HttpResponse response) => + HttpResponse(_immutableJson(response.data), response.response); + +dynamic _immutableJson(dynamic value) => switch (value) { + List values => List.unmodifiable(values.map(_immutableJson)), + Map values => Map.unmodifiable( + values.map((key, value) => MapEntry(key, _immutableJson(value))), + ), + _ => value, +}; diff --git a/lib/src/service/modrinth_url.dart b/lib/src/service/modrinth/modrinth_url.dart similarity index 100% rename from lib/src/service/modrinth_url.dart rename to lib/src/service/modrinth/modrinth_url.dart diff --git a/lib/src/service/pack_archive_writer.dart b/lib/src/service/pack_archive_writer.dart new file mode 100644 index 0000000..784a52c --- /dev/null +++ b/lib/src/service/pack_archive_writer.dart @@ -0,0 +1,43 @@ +import 'dart:io'; +import 'dart:math'; + +import 'package:archive/archive_io.dart'; + +/// Writes manifest bytes followed by the supplied files, in iteration order. +void writeArchive( + File output, { + required String manifestName, + required List manifest, + required Iterable<({String path, File source})> entries, +}) { + final random = Random.secure(); + final suffix = List.generate( + 16, + (_) => random.nextInt(256), + ).map((byte) => byte.toRadixString(16).padLeft(2, '0')).join(); + final temporary = File('${output.path}.tmp-$suffix'); + OutputFileStream? stream; + try { + stream = OutputFileStream(temporary.path); + final encoder = ZipEncoder()..startEncode(stream); + encoder.add(ArchiveFile(manifestName, manifest.length, manifest)); + for (final entry in entries) { + final input = InputFileStream(entry.source.path); + try { + encoder.add(ArchiveFile.stream(entry.path, input), autoClose: false); + } finally { + input.closeSync(); + } + } + encoder.endEncode(); + stream.closeSync(); + stream = null; + temporary.renameSync(output.path); + } finally { + try { + stream?.closeSync(); + } finally { + if (temporary.existsSync()) temporary.deleteSync(); + } + } +} diff --git a/lib/src/service/paper_api_client.dart b/lib/src/service/paper_api_client.dart index 4a6e2be..b3e66ac 100644 --- a/lib/src/service/paper_api_client.dart +++ b/lib/src/service/paper_api_client.dart @@ -20,12 +20,12 @@ class PaperBuild { }); } -/// Small typed wrapper over the PaperMC v2 REST API. Mirrors the URL- +/// Small typed wrapper over the PaperMC Fill v3 REST API. Mirrors the URL- /// template + env-var override pattern used by [LoaderBinaryFetcher]. class PaperApiClient { final Dio _dio; final String _buildsUrlTemplate; - final String _downloadUrlTemplate; + final String? _downloadUrlTemplate; PaperApiClient({ required Dio dio, @@ -36,22 +36,19 @@ class PaperApiClient { _buildsUrlTemplate = buildsUrlTemplate ?? (environment ?? Platform.environment)['GITRINTH_PAPER_API_URL'] ?? - 'https://api.papermc.io/v2/projects/{project}/versions/{mc}/builds', + 'https://fill.papermc.io/v3/projects/{project}/versions/{mc}/builds', _downloadUrlTemplate = downloadUrlTemplate ?? - (environment ?? - Platform.environment)['GITRINTH_PAPER_DOWNLOAD_URL'] ?? - 'https://api.papermc.io/v2/projects/{project}/versions/{mc}/' - 'builds/{build}/downloads/{filename}'; + (environment ?? Platform.environment)['GITRINTH_PAPER_DOWNLOAD_URL']; - /// Returns the latest stable (`channel: "default"`) build of [project] + /// Returns the latest stable (`channel: "STABLE"`) build of [project] /// for Minecraft [mc]. Throws [UserError] when no stable build exists. Future latestStableBuild({ required String project, required String mc, }) async { final builds = await _listBuilds(project: project, mc: mc); - // PaperMC's `channel` is uppercase (`STABLE` / `EXPERIMENTAL`). Match + // PaperMC's `channel` is uppercase (`STABLE` / `ALPHA` / `BETA`). Match // case-insensitively to tolerate any future casing changes. Within // stable builds, pick the highest build number rather than relying on // API ordering. @@ -63,7 +60,7 @@ class PaperApiClient { if (channel is! String) continue; final normalized = channel.toLowerCase(); if (normalized != 'stable' && normalized != 'default') continue; - final build = raw['build']; + final build = raw['id'] ?? raw['build']; if (build is! int) continue; if (build > latestBuild) { latestBuild = build; @@ -90,7 +87,7 @@ class PaperApiClient { int latestBuild = -1; for (final raw in builds) { if (raw is! Map) continue; - final build = raw['build']; + final build = raw['id'] ?? raw['build']; if (build is! int) continue; if (build > latestBuild) { latestBuild = build; @@ -112,7 +109,7 @@ class PaperApiClient { final builds = await _listBuilds(project: project, mc: mc); for (final raw in builds) { if (raw is! Map) continue; - if (raw['build'] == build) { + if ((raw['id'] ?? raw['build']) == build) { return _buildFromEntry( project: project, mc: mc, @@ -135,18 +132,24 @@ class PaperApiClient { }); final Response response; try { - response = await _dio.get(url); + response = await _dio.get( + url, + options: Options( + headers: { + 'User-Agent': 'gitrinth (https://github.com/getBoolean/gitrinth)', + }, + ), + ); } on DioException catch (e) { unwrapOrThrow(e, context: 'failed to query PaperMC API at $url'); } final decoded = response.data is String ? jsonDecode(response.data as String) : response.data; - if (decoded is! Map) { + if (decoded is! Map && decoded is! List) { throw UserError('PaperMC API at $url returned unexpected payload.'); } - final data = decoded.cast(); - final builds = data['builds']; + final builds = decoded is List ? decoded : (decoded as Map)['builds']; if (builds is! List || builds.isEmpty) { throw UserError('PaperMC reported no $project builds for Minecraft $mc.'); } @@ -158,14 +161,14 @@ class PaperApiClient { required String mc, required Map entry, }) { - final build = entry['build']; + final build = entry['id'] ?? entry['build']; final downloads = entry['downloads']; if (build is! int || downloads is! Map) { throw UserError( 'PaperMC returned a malformed build entry for $project $mc.', ); } - final application = downloads['application']; + final application = downloads['server:default'] ?? downloads['application']; if (application is! Map) { throw UserError( 'PaperMC build $build for $project did not list an `application` ' @@ -178,14 +181,23 @@ class PaperApiClient { 'PaperMC build $build for $project did not name its application jar.', ); } - final downloadUrl = Uri.parse( - fillUrlTemplate(_downloadUrlTemplate, { - 'project': project, - 'mc': mc, - 'build': build.toString(), - 'filename': filename, - }), - ); + final template = _downloadUrlTemplate; + final rawUrl = template != null + ? fillUrlTemplate(template, { + 'project': project, + 'mc': mc, + 'build': build.toString(), + 'filename': filename, + }) + : application['url']; + final downloadUrl = rawUrl is String ? Uri.tryParse(rawUrl) : null; + if (downloadUrl == null || + !downloadUrl.hasAuthority || + !{'http', 'https'}.contains(downloadUrl.scheme)) { + throw UserError( + 'PaperMC build $build for $project has no valid download URL.', + ); + } return PaperBuild( build: build, filename: filename, diff --git a/lib/src/service/plugin_server_source.dart b/lib/src/service/plugin_server_source.dart index 70751a3..8a72c03 100644 --- a/lib/src/service/plugin_server_source.dart +++ b/lib/src/service/plugin_server_source.dart @@ -34,51 +34,57 @@ abstract class PluginServerSource { /// without consulting `loader.mods`. static PluginServerSource forLoader( PluginLoader loader, { + LoaderRegistry registry = LoaderRegistry.builtins, required PaperApiClient paperApi, required SpongeApiClient spongeApi, required BuildToolsRunner buildTools, required GitrinthCache cache, required Downloader downloader, }) { - switch (loader) { - case PluginLoader.paper: - case PluginLoader.folia: + final descriptor = registry[loader]; + switch (descriptor?.installer) { + case InstallerFamily.paper: return _PaperLikeSource( - loader: loader, + loader: LoaderId(descriptor?.runtimeArtifact ?? loader.name), paperApi: paperApi, cache: cache, downloader: downloader, ); - case PluginLoader.spongeforge: - return _SpongeSource( - modLoader: SpongeLoader.forge, - spongeApi: spongeApi, - cache: cache, - downloader: downloader, - ); - case PluginLoader.spongeneo: - return _SpongeSource( - modLoader: SpongeLoader.neoforge, - spongeApi: spongeApi, - cache: cache, - downloader: downloader, - ); - case PluginLoader.spongevanilla: + case InstallerFamily.sponge: + final artifact = descriptor?.runtimeArtifact; + final modLoader = switch (artifact) { + 'spongeforge' => SpongeLoader.forge, + 'spongeneo' => SpongeLoader.neoforge, + 'spongevanilla' => SpongeLoader.vanilla, + _ => null, + }; + if (modLoader == null) { + throw ValidationError( + 'Loader ${loader.name} has no trusted Sponge artifact recipe.', + ); + } return _SpongeSource( - modLoader: SpongeLoader.vanilla, + modLoader: modLoader, spongeApi: spongeApi, cache: cache, downloader: downloader, ); - case PluginLoader.spigot: + case InstallerFamily.buildtools: + final artifact = descriptor?.runtimeArtifact; + if (artifact != 'spigot' && artifact != 'craftbukkit') { + throw ValidationError( + 'Loader ${loader.name} has no trusted BuildTools artifact recipe.', + ); + } return _BuildToolsSource( - flavor: SpigotFlavor.spigot, + flavor: artifact == 'spigot' + ? SpigotFlavor.spigot + : SpigotFlavor.craftbukkit, buildTools: buildTools, ); - case PluginLoader.bukkit: - return _BuildToolsSource( - flavor: SpigotFlavor.craftbukkit, - buildTools: buildTools, + default: + throw ValidationError( + 'Loader ${loader.name} has no installed plugin server adapter.', ); } } diff --git a/lib/src/service/prepared_publication.dart b/lib/src/service/prepared_publication.dart new file mode 100644 index 0000000..e1b1e2f --- /dev/null +++ b/lib/src/service/prepared_publication.dart @@ -0,0 +1,29 @@ +import 'dart:io'; + +import '../model/manifest/mods_yaml.dart'; + +enum PublicationArtifactRole { client, server } + +class PreparedPublication { + final SourceKind target; + final String serverUrl; + final String token; + final File artifact; + final Map payload; + final int? curseforgeProjectId; + final PublicationArtifactRole role; + final PreparedPublication? dependentServer; + final int? parentFileId; + + PreparedPublication({ + required this.target, + required this.serverUrl, + required this.token, + required this.artifact, + required Map payload, + this.curseforgeProjectId, + this.role = PublicationArtifactRole.client, + this.dependentServer, + this.parentFileId, + }) : payload = Map.unmodifiable(payload); +} diff --git a/lib/src/service/resolve/catalog_loader_version.dart b/lib/src/service/resolve/catalog_loader_version.dart new file mode 100644 index 0000000..cea1f2e --- /dev/null +++ b/lib/src/service/resolve/catalog_loader_version.dart @@ -0,0 +1,37 @@ +import '../../cli/exceptions.dart'; +import '../../model/loader/loader_descriptor.dart'; + +/// Selects only releases with applicability and promotion metadata for this game. +String resolveCatalogLoaderVersion( + LoaderDescriptor descriptor, + String tag, + String mcVersion, +) { + final catalog = descriptor.catalogVersions; + if (catalog != null) { + final matching = catalog.where((v) => v.gameVersion == mcVersion); + final selected = matching + .where( + (v) => switch (tag) { + 'latest' => v.latest, + 'stable' => v.recommended, + _ => v.name == tag, + }, + ) + .firstOrNull; + if (selected != null) return selected.name; + } else { + if (tag != 'stable' && + tag != 'latest' && + (descriptor.versions.isEmpty || descriptor.versions.contains(tag))) { + return tag; + } + if ((tag == 'stable' || tag == 'latest') && + descriptor.versions.isNotEmpty) { + return descriptor.versions.first; + } + } + throw ValidationError( + 'Loader ${descriptor.id.name} has no discovered version matching $tag for Minecraft $mcVersion; supply a compatible concrete version or refresh its provider catalog.', + ); +} diff --git a/lib/src/service/conflict_disable.dart b/lib/src/service/resolve/conflict_disable.dart similarity index 85% rename from lib/src/service/conflict_disable.dart rename to lib/src/service/resolve/conflict_disable.dart index a636586..6285400 100644 --- a/lib/src/service/conflict_disable.dart +++ b/lib/src/service/resolve/conflict_disable.dart @@ -1,8 +1,8 @@ -import '../cli/exceptions.dart'; -import '../model/manifest/mods_yaml.dart'; -import '../model/resolver/constraint.dart'; -import '../model/resolver/pubgrub.dart'; -import 'console.dart'; +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/resolver/constraint.dart'; +import '../../model/resolver/pubgrub.dart'; +import '../console.dart'; import 'resolve_and_sync.dart'; /// Outcome of [resolveWithConflictAutoDisable]: the second-pass @@ -126,23 +126,17 @@ Future resolveWithConflictAutoDisable({ /// Returns [manifest] with each `(section, slug)` in [disabled] /// rewritten to carry `constraintRaw: disabledByConflictMarker`. -/// Mirrors the in-memory marker-application migrate has done since -/// the auto-disable feature shipped. ModsYaml applyDisableMarkers( ModsYaml manifest, Set<(Section, String)> disabled, ) { if (disabled.isEmpty) return manifest; - Map mark(Section section, Map m) => { - for (final e in m.entries) - e.key: disabled.contains((section, e.key)) - ? e.value.copyWith(constraintRaw: disabledByConflictMarker) - : e.value, - }; - return manifest.copyWith( - mods: mark(Section.mods, manifest.mods), - resourcePacks: mark(Section.resourcePacks, manifest.resourcePacks), - dataPacks: mark(Section.dataPacks, manifest.dataPacks), - shaders: mark(Section.shaders, manifest.shaders), + return manifest.mapSectionEntries( + (section, entries) => { + for (final e in entries.entries) + e.key: disabled.contains((section, e.key)) + ? e.value.copyWith(constraintRaw: disabledByConflictMarker) + : e.value, + }, ); } diff --git a/lib/src/service/resolve/cross_platform_search.dart b/lib/src/service/resolve/cross_platform_search.dart new file mode 100644 index 0000000..aaaa0d5 --- /dev/null +++ b/lib/src/service/resolve/cross_platform_search.dart @@ -0,0 +1,395 @@ +import 'dart:convert'; + +import '../../commands/version_picker.dart'; +import '../../model/curseforge/cf_constants.dart'; +import '../../model/curseforge/cf_mod.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/manifest/source_eligibility.dart'; +import '../../model/modrinth/project.dart'; +import '../../model/modrinth/version.dart' as mr; +import '../../model/resolver/constraint.dart'; +import '../../model/resolver/version_selection.dart'; +import '../console.dart'; +import '../curseforge/curseforge_api.dart'; +import '../curseforge/curseforge_filters.dart'; +import '../modrinth/modrinth_api.dart'; +import '../modrinth/modrinth_api_source.dart'; +import '../source_identity.dart'; +import 'curseforge_resolver.dart'; + +/// Session-scoped search caches retain host and all compatibility filters. +/// Only a unique project with an identical SHA1 can be selected automatically. +class CrossPlatformSearch { + final ModsYaml manifest; + final LoaderRegistry registry; + final ModrinthApiSource mrFactory; + final CurseForgeApi cfApi; + final Console console; + final bool disabled; + final Set modrinthSearchSlugs = {}; + final Map>> _cfQueries = {}; + final Map diagnostics = {}; + + CrossPlatformSearch({ + required this.manifest, + this.registry = LoaderRegistry.builtins, + required this.mrFactory, + required this.cfApi, + required this.console, + this.disabled = false, + }); + + bool enabled(ModEntry entry, Section section) => + !disabled && + !entry.noCrossPlatformSearch && + effectiveSources(manifest, entry, section, registry: registry).length == + 2 && + !(section == Section.mods && manifest.loader.mods == ModLoader.vanilla); + + Future findCurseforge({ + required String slug, + required Section section, + required ModEntry entry, + required List anchors, + int? excludeProject, + }) async { + if (!enabled(entry, section) || anchors.isEmpty) return null; + final query = entry.curseforgeSlug ?? slug; + final versions = {manifest.mcVersion, ...entry.acceptsMc}.toList(); + final key = jsonEncode([ + query, + section.name, + manifest.loader.toMap(), + versions, + ]); + final projects = await _cfQueries.putIfAbsent( + key, + () async => (await cfApi.searchMods( + gameId: kCurseForgeGameIdMinecraft, + classId: section.cfClassId, + searchFilter: query, + pageSize: 20, + )).data, + ); + final candidates = <_Candidate>[]; + final anchorHashes = _mrHashes(anchors, entry.hashScanDepth ?? 10); + Project? anchorProject; + final anchorAuthors = []; + final host = manifest.effectiveModrinthHost( + entry, + mrFactory.defaultBaseUrl, + ); + try { + anchorProject = await mrFactory + .forHost(host) + .getProject(anchors.first.projectId); + } on Object { + /* Hash evidence does not require project metadata. */ + } + try { + final members = + (await mrFactory + .forHost(host) + .getProjectMembers(anchors.first.projectId)) + .data; + if (members is List) { + for (final member in members) { + if (member is Map && member['user'] is Map) { + final name = member['user']['username']; + if (name is String) anchorAuthors.add(name); + } + } + } + } on Object { + /* Ranking remains useful without optional authors. */ + } + for (final project in projects) { + if (project.id == excludeProject || + project.classId != section.cfClassId) { + continue; + } + final files = await listCompatibleFiles( + registry: registry, + api: cfApi, + projectId: project.id, + section: section, + modLoader: section == Section.mods ? manifest.loader.mods : null, + pluginLoader: manifest.loader.plugins, + gameVersions: versions, + channel: entry.channel ?? Channel.alpha, + diagnoseUnavailable: false, + ); + if (files.isEmpty) continue; + final matches = files + .take((entry.hashScanDepth ?? 10) + 1) + .where((f) => f.sha1Hash != null && anchorHashes.contains(f.sha1Hash)) + .toList(); + candidates.add( + _Candidate( + value: CfResolvedEntry( + slug: slug, + section: section, + project: project, + file: matches.isEmpty ? files.first : matches.first, + candidates: files, + discoveredViaSearch: true, + ), + hashMatch: matches.isNotEmpty, + title: project.name, + slug: project.slug, + authors: project.authors + .map((a) => a['name']?.toString() ?? '') + .toList(), + categories: project.categories + .map((c) => c['name']?.toString() ?? '') + .toList(), + version: files.first.displayName, + ), + ); + } + final selected = _select( + LocalEntryKey(section, slug), + 'CurseForge', + candidates, + anchorProject?.title ?? slug, + anchorAuthors, + anchorProject?.categories ?? const [], + ); + if (selected != null) { + _notice(slug, 'CurseForge', selected.project.slug, 'curseforge'); + } + return selected; + } + + Future?> findModrinth({ + required String slug, + required Section section, + required ModEntry entry, + required CfResolvedEntry anchor, + String? excludeProject, + }) async { + if (!enabled(entry, section)) return null; + final apiHost = manifest.effectiveModrinthHost( + entry, + mrFactory.defaultBaseUrl, + ); + final api = mrFactory.forHost(apiHost); + final query = entry.modrinthSlug ?? slug; + final versions = {manifest.mcVersion, ...entry.acceptsMc}.toList(); + final loaders = filterLoadersForSection( + manifest.loader, + section, + registry: registry, + host: apiHost, + ); + final type = switch (section) { + Section.mods => 'mod', + Section.resourcePacks => 'resourcepack', + Section.shaders => 'shader', + Section.dataPacks => 'datapack', + Section.plugins => 'plugin', + }; + final facets = jsonEncode([ + [ + 'project_type:$type', + if (section == Section.dataPacks || section == Section.plugins) + 'project_type:mod', + ], + [for (final v in versions) 'versions:$v'], + if (loaders != null) [for (final l in loaders) 'categories:$l'], + ]); + final response = await api.searchProjects(query: query, facets: facets); + final data = response.data; + final hits = data is Map && data['hits'] is List + ? data['hits'] as List + : []; + final anchorHashes = anchor.candidates + .take((entry.hashScanDepth ?? 10) + 1) + .map((f) => f.sha1Hash) + .whereType() + .toSet(); + final candidates = <_Candidate>>[]; + for (final raw in hits) { + if (raw is! Map || + raw['project_id'] is! String || + raw['project_id'] == excludeProject) { + continue; + } + final legacyMod = + raw['project_type'] == 'mod' && + (section == Section.dataPacks || section == Section.plugins); + if (raw['project_type'] != type && !legacyMod) continue; + final list = await api.listVersions( + raw['project_id'] as String, + loadersJson: loaders == null ? null : encodeFilterArray(loaders), + gameVersionsJson: encodeFilterArray(versions), + ); + final compatible = list + .where( + (v) => + v.primaryFile != null && + v.gameVersions.any(versions.contains) && + (loaders == null || v.loaders.any(loaders.contains)), + ) + .toList(); + // Respect the manifest constraint and channel before considering a hash. + final eligible = eligibleModrinthVersions(compatible, entry); + if (eligible.isEmpty) continue; + final matches = eligible.take((entry.hashScanDepth ?? 10) + 1).where((v) { + final hash = v.primaryFile?.hashes['sha1']?.toLowerCase(); + return hash != null && anchorHashes.contains(hash); + }).toList(); + candidates.add( + _Candidate( + value: matches, + hashMatch: matches.isNotEmpty, + title: raw['title']?.toString() ?? '', + slug: raw['slug']?.toString() ?? '', + authors: [raw['author']?.toString() ?? ''], + categories: (raw['categories'] as List? ?? []) + .map((v) => v.toString()) + .toList(), + version: eligible.first.versionNumber, + ), + ); + } + final selected = _select( + LocalEntryKey(section, slug), + 'Modrinth', + candidates, + anchor.project.name, + anchor.project.authors.map((a) => a['name']?.toString() ?? '').toList(), + anchor.project.categories + .map((c) => c['name']?.toString() ?? '') + .toList(), + ); + if (selected != null) { + modrinthSearchSlugs.add(LocalEntryKey(section, slug)); + final project = candidates.firstWhere( + (c) => identical(c.value, selected), + ); + _notice(slug, 'Modrinth', project.slug, 'modrinth'); + } + return selected; + } + + Set _mrHashes(List versions, int depth) => versions + .take(depth + 1) + .map((v) => v.primaryFile?.hashes['sha1']?.toLowerCase()) + .whereType() + .toSet(); + + T? _select( + LocalEntryKey node, + String platform, + List<_Candidate> candidates, + String title, + List authors, + List categories, + ) { + candidates.sort((a, b) { + final hash = (b.hashMatch ? 1 : 0).compareTo(a.hashMatch ? 1 : 0); + if (hash != 0) return hash; + final name = _similarity( + b.title, + title, + ).compareTo(_similarity(a.title, title)); + if (name != 0) return name; + final author = _overlap( + b.authors, + authors, + ).compareTo(_overlap(a.authors, authors)); + if (author != 0) return author; + final category = _overlap( + b.categories, + categories, + ).compareTo(_overlap(a.categories, categories)); + return category != 0 ? category : a.slug.compareTo(b.slug); + }); + final matches = candidates.where((c) => c.hashMatch).toList(); + if (matches.length == 1) return matches.single.value; + final reason = matches.length > 1 + ? 'found ambiguous hash matches' + : 'found no unique hash match'; + final details = candidates + .take(5) + .map((c) { + final authors = c.authors.where((a) => a.isNotEmpty).join(', '); + return ' - ${c.title} (${c.slug}), author: $authors, version: ${c.version}'; + }) + .join('\n'); + final message = + '$platform search $reason. ' + '${candidates.isEmpty ? "No candidates." : "Candidates:\n$details"}'; + diagnostics.update( + node, + (previous) => '$previous\n$message', + ifAbsent: () => message, + ); + return null; + } + + void _notice( + String slug, + String platform, + String peer, + String field, + ) => console.message( + '$slug: resolved on $platform via search as \'$peer\' (SHA1 match). ' + 'Consider adding `$field: $peer` to mods.yaml to skip search on future runs.', + ); +} + +/// The hash window contains only versions admissible for this manifest entry. +List eligibleModrinthVersions( + List versions, + ModEntry? entry, +) { + final constraint = parseConstraint(entry?.constraintRaw); + final channel = entry?.channel ?? Channel.alpha; + return versions + .where((v) => pickHighestMatching([v], constraint, channel) != null) + .toList() + ..sort( + (a, b) => compareModrinthSelectionOrder( + a, + parseModrinthVersionBestEffort(a.versionNumber), + b, + parseModrinthVersionBestEffort(b.versionNumber), + ), + ); +} + +class _Candidate { + final T value; + final bool hashMatch; + final String title, slug, version; + final List authors, categories; + const _Candidate({ + required this.value, + required this.hashMatch, + required this.title, + required this.slug, + required this.version, + required this.authors, + required this.categories, + }); +} + +Set _words(String s) => s + .toLowerCase() + .split(RegExp(r'[^a-z0-9]+')) + .where((s) => s.isNotEmpty) + .toSet(); +double _similarity(String a, String b) { + final left = _words(a), right = _words(b); + final union = left.union(right); + return union.isEmpty ? 0 : left.intersection(right).length / union.length; +} + +int _overlap(List a, List b) => a + .map((v) => v.toLowerCase()) + .toSet() + .intersection(b.map((v) => v.toLowerCase()).toSet()) + .where((v) => v.isNotEmpty) + .length; diff --git a/lib/src/service/resolve/curseforge_relationships.dart b/lib/src/service/resolve/curseforge_relationships.dart new file mode 100644 index 0000000..8ceae1d --- /dev/null +++ b/lib/src/service/resolve/curseforge_relationships.dart @@ -0,0 +1,83 @@ +import '../../cli/exceptions.dart'; +import '../../model/curseforge/cf_file_relation.dart'; +import '../../model/curseforge/cf_mod_file.dart'; +import '../source_identity.dart'; +import 'curseforge_resolver.dart'; +import 'hash_scan.dart'; + +/// Select the same final files as lock construction, after hash matching settles. +Map selectedCurseforgeFiles( + CfResolution resolution, + Map hashOutcomes, +) { + final selected = {}; + for (final entry in resolution.resolved.entries) { + final file = switch (hashOutcomes[entry.key]) { + HashMatched(:final curseforgeFile) => curseforgeFile, + HashAcceptedMismatch(:final curseforgeFile) => curseforgeFile, + _ => entry.value.file, + }; + if (file.modId != entry.value.project.id) { + throw ValidationError( + 'CurseForge file ${file.id} belongs to project ${file.modId}, not selected project ${entry.value.project.id} for ${entry.key.localKey}.', + ); + } + selected[entry.key] = file; + } + return selected; +} + +/// Validate by numeric provider identity so local aliases cannot hide edges. +void validateCurseforgeRelationships( + Map files, { + Set overriddenSlugs = const {}, +}) { + final aliases = >{}; + final overriddenProjects = {}; + for (final entry in files.entries) { + (aliases[entry.value.modId] ??= []).add(entry.key); + if (overriddenSlugs.contains(entry.key.localKey)) { + overriddenProjects.add(entry.value.modId); + } + } + String describe(int projectId) { + final nodes = aliases[projectId] ?? const []; + final names = + nodes.map((node) => '${node.section.name}/${node.localKey}').toList() + ..sort(); + final ids = nodes.map((node) => files[node]?.id).nonNulls.toSet().toList() + ..sort(); + return '${names.join(', ')} (project $projectId, files ${ids.join(', ')})'; + } + + final conflicts = {}; + for (final file in files.values) { + if (!file.isAvailable) { + throw ValidationError( + 'Selected CurseForge project ${file.modId}, file ${file.id} is unavailable. Run `gitrinth get` online to refresh it.', + ); + } + if (overriddenProjects.contains(file.modId)) continue; + for (final relation in file.dependencies) { + final other = relation.modId; + if (relation.relationType != RelationType.incompatible || + !aliases.containsKey(other) || + overriddenProjects.contains(other)) { + continue; + } + final pair = [file.modId, other]..sort(); + conflicts['${pair.first}:${pair.last}'] = + '${describe(pair.first)} is incompatible with ${describe(pair.last)}.'; + } + } + if (conflicts.isNotEmpty) { + final keys = conflicts.keys.toList()..sort(); + throw ValidationError( + [ + 'Selected CurseForge files cannot coexist:', + for (final key in keys) ' ${conflicts[key]}', + 'Choose an explicit compatible pin in mods.yaml and run `gitrinth get` again.', + ].join('\n'), + ); + } +} diff --git a/lib/src/service/resolve/curseforge_resolution_api.dart b/lib/src/service/resolve/curseforge_resolution_api.dart new file mode 100644 index 0000000..8506386 --- /dev/null +++ b/lib/src/service/resolve/curseforge_resolution_api.dart @@ -0,0 +1,64 @@ +import '../../model/curseforge/cf_search_response.dart'; +import '../curseforge/curseforge_api.dart'; + +/// A graph solve uses one metadata snapshot while selected dependency edges +/// change. Cache request futures only for that solve, including pagination. +class CurseForgeResolutionApi implements CurseForgeApi { + final CurseForgeApi delegate; + final _projects = >{}; + final _files = <(int, int), Future>{}; + final _listings = + <(int, String?, int?, int?, int?), Future>{}; + final _searches = + <(int, int?, String?, String?, int?, int?), Future>{}; + + CurseForgeResolutionApi(this.delegate); + + @override + Future getMod(int id) => + _projects.putIfAbsent(id, () => delegate.getMod(id)); + + @override + Future getFile(int modId, int fileId) => _files.putIfAbsent(( + modId, + fileId, + ), () => delegate.getFile(modId, fileId)); + + @override + Future listFiles( + int modId, { + String? gameVersion, + int? modLoaderType, + int? index, + int? pageSize, + }) => _listings.putIfAbsent( + (modId, gameVersion, modLoaderType, index, pageSize), + () => delegate.listFiles( + modId, + gameVersion: gameVersion, + modLoaderType: modLoaderType, + index: index, + pageSize: pageSize, + ), + ); + + @override + Future searchMods({ + required int gameId, + int? classId, + String? slug, + String? searchFilter, + int? index, + int? pageSize, + }) => _searches.putIfAbsent( + (gameId, classId, slug, searchFilter, index, pageSize), + () => delegate.searchMods( + gameId: gameId, + classId: classId, + slug: slug, + searchFilter: searchFilter, + index: index, + pageSize: pageSize, + ), + ); +} diff --git a/lib/src/service/resolve/curseforge_resolver.dart b/lib/src/service/resolve/curseforge_resolver.dart new file mode 100644 index 0000000..4c1413a --- /dev/null +++ b/lib/src/service/resolve/curseforge_resolver.dart @@ -0,0 +1,365 @@ +import 'package:dio/dio.dart'; + +import '../../cli/exceptions.dart'; +import '../../model/curseforge/cf_mod.dart'; +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/curseforge/cf_constants.dart'; +import '../../model/curseforge/cf_file_relation.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/manifest/source_eligibility.dart'; +import '../../model/resolver/constraint.dart'; +import '../console.dart'; +import '../source_identity.dart'; +import '../curseforge/curseforge_api.dart'; +import '../curseforge/curseforge_filters.dart'; +import '../curseforge/curseforge_version.dart'; + +/// One CF entry that successfully resolved. The lock builder uses +/// these to populate per-entry [LockedCurseforgeSource] blocks. +/// +/// [candidates] is the full newest-first list of compatible files +/// returned by [listCompatibleFiles] for this entry. The hash scan +/// step needs more than just the picked [file] when scanning older +/// pairs for a SHA1 match. +class CfResolvedEntry { + final String slug; + final Section section; + final Mod project; + final ModFile file; + final List candidates; + final bool discoveredViaSearch; + final List requiredBy; + final bool transitive; + const CfResolvedEntry({ + required this.slug, + required this.section, + required this.project, + required this.file, + required this.candidates, + this.discoveredViaSearch = false, + this.requiredBy = const [], + this.transitive = false, + }); +} + +/// One CF entry that failed to resolve. The aggregator turns these +/// into a single [ValidationError] after collecting both Modrinth and +/// CurseForge failures across the manifest. +class CfNotFound { + final String slug; + final Section section; + + /// Slug used to query CurseForge — entry's `curseforge:` peer slug + /// override or the section-map key. + final String slugUsed; + + /// `'slug-not-found'` (CF returned no project) or + /// `'no-compatible-files'` (project found but no file matched the + /// loader/MC/channel filter). + final String reason; + + const CfNotFound({ + required this.slug, + required this.section, + required this.slugUsed, + required this.reason, + }); +} + +/// Aggregate result of a CurseForge resolution pass: per-entry +/// resolved blocks and per-entry not-found markers. +class CfResolution { + final Map resolved; + final List notFound; + const CfResolution({this.resolved = const {}, this.notFound = const []}); + + /// Empty resolution — used when the CurseForge resolver is bypassed + /// (offline mode, no entries request CurseForge, etc.). + static const empty = CfResolution(); +} + +/// Walks the merged manifest and resolves every CurseForge-eligible +/// entry against [api]. Returns a [CfResolution] aggregating the +/// per-entry content misses. Service failures retain their actionable causes +/// rather than becoming [CfNotFound] markers. +/// +/// Eligibility per entry follows [effectiveSources] — entries excluded +/// from CurseForge by `sources:` / matrix-ineligibility / pack-default +/// are silently skipped. Vanilla `mods:` entries are short-circuited +/// before any HTTP call (their `cfModLoaderType` would throw). +/// +/// Inputs are kept boundary-pure: the manifest is the only spec, [api] +/// is the only HTTP surface. +Future resolveCurseforgeEntries({ + required ModsYaml manifest, + LoaderRegistry registry = LoaderRegistry.builtins, + required CurseForgeApi api, + required Console console, + Map selectedFiles = const {}, +}) async { + final resolved = {}; + final notFound = []; + final mcVersions = {manifest.mcVersion}; + + for (final section in Section.values) { + final entries = manifest.sectionEntries(section); + for (final mapEntry in entries.entries) { + final slug = mapEntry.key; + final entry = mapEntry.value; + // url:/path: sources never enter CF resolution. + if (entry.source is! ModrinthEntrySource) continue; + if (isAnyGitrinthMarker(entry.constraintRaw)) continue; + // Honor entry- and pack-level source restrictions before any + // network call. Section/loader matrix-ineligibility silently + // drops CF here; the `sources: curseforge` explicit-error path is + // already enforced by the parser. + final effective = effectiveSources( + manifest, + entry, + section, + registry: registry, + ); + if (section == Section.plugins && + !(registry[manifest.loader.plugins]?.curseforgePlugins ?? false)) { + if ((entry.sources?.contains(SourceKind.curseforge) ?? false) || + entry.curseforgeSlug != null) { + throw ValidationError( + 'Loader ${manifest.loader.plugins?.name} has no CurseForge plugin capability; use Modrinth or supply an explicit provider mapping.', + ); + } + continue; + } + if (!effective.contains(SourceKind.curseforge)) continue; + // Vanilla packs have no mod-loader on the wire — CF would throw + // a `StateError` from `cfModLoaderType`. Section.mods + vanilla + // is incoherent with CurseForge anyway; short-circuit. + if (section == Section.mods && + manifest.loader.mods == ModLoader.vanilla) { + continue; + } + + final slugUsed = entry.curseforgeSlug ?? slug; + final gameVersions = {...mcVersions, ...entry.acceptsMc}.toList(); + final channel = entry.channel ?? Channel.alpha; + + Mod? project; + // Numeric `curseforge: 32274` short-form: skip the slug search + // and resolve directly via getMod. + final asInt = int.tryParse(slugUsed); + try { + if (asInt != null) { + final envelope = await api.getMod(asInt); + project = envelope.data; + } else { + project = await findModBySlug(api, slug: slugUsed, section: section); + } + } on Object catch (e) { + if (asInt == null || + e is! DioException || + e.response?.statusCode != 404) { + final cause = e is DioException ? e.error : e; + if (cause is GitrinthException) throw cause; + throw UserError( + 'CurseForge lookup for "$slug" ($slugUsed) failed: $e', + ); + } + } + if (project == null) { + notFound.add( + CfNotFound( + slug: slug, + section: section, + slugUsed: slugUsed, + reason: 'slug-not-found', + ), + ); + continue; + } + if (project.classId == null) { + throw ValidationError( + 'Cannot infer content type for CurseForge ${project.slug} (project ${project.id}); the API returned no classId.', + ); + } + if (project.classId != section.cfClassId) { + throw ValidationError( + 'CurseForge ${project.slug} has a different content type from ${section.name}; declare it in its intended section.', + ); + } + + List files; + try { + files = await listCompatibleFiles( + api: api, + registry: registry, + projectId: project.id, + section: section, + modLoader: section == Section.mods ? manifest.loader.mods : null, + pluginLoader: manifest.loader.plugins, + gameVersions: gameVersions, + channel: channel, + ); + } on Object catch (e) { + final cause = e is DioException ? e.error : e; + if (cause is GitrinthException) throw cause; + throw UserError( + 'CurseForge file listing for "$slug" (project ${project.id}) failed: $e', + ); + } + + if (files.isEmpty) { + notFound.add( + CfNotFound( + slug: slug, + section: section, + slugUsed: slugUsed, + reason: 'no-compatible-files', + ), + ); + continue; + } + + // For dual-source roots the Modrinth version constraint anchors the + // hash scan; CF labels need not share Modrinth's numbering scheme. + if (!effective.contains(SourceKind.modrinth)) { + files = files + .where((f) => curseForgeFileSatisfies(f, entry.constraintRaw)) + .toList(); + if (files.isEmpty) { + notFound.add( + CfNotFound( + slug: slug, + section: section, + slugUsed: slugUsed, + reason: 'no-compatible-files', + ), + ); + continue; + } + } + resolved[LocalEntryKey(section, slug)] = CfResolvedEntry( + slug: slug, + section: section, + project: project, + file: + files + .where( + (f) => f.id == selectedFiles[LocalEntryKey(section, slug)], + ) + .firstOrNull ?? + files.first, + candidates: files, + ); + } + } + + final aliasesByProject = >{}; + for (final entry in resolved.entries) { + (aliasesByProject[entry.value.project.id] ??= {}).add(entry.key); + } + final expanded = {}; + final queue = resolved.keys.toList(); + final parents = >{}; + while (queue.isNotEmpty) { + final parentKey = queue.removeLast(); + final parent = resolved[parentKey]!; + if (!expanded.add(parentKey)) continue; + for (final dep in parent.file.dependencies) { + if (dep.relationType != RelationType.requiredDependency) continue; + final project = (await api.getMod(dep.modId)).data; + final sections = Section.values.where( + (s) => s.cfClassId == project.classId, + ); + if (sections.isEmpty) { + throw ValidationError( + 'Cannot infer content type for CurseForge dependency ${project.slug}; declare a top-level entry in its intended section.', + ); + } + final section = sections.single; + String? key; + for (final e in manifest.sectionEntries(section).entries) { + if (e.value.source is! ModrinthEntrySource) continue; + if (e.value.curseforgeSlug == project.slug || + e.value.curseforgeSlug == project.id.toString() || + (e.value.curseforgeSlug == null && e.key == project.slug)) { + if (!effectiveSources( + manifest, + e.value, + section, + registry: registry, + ).contains(SourceKind.curseforge) || + isAnyGitrinthMarker(e.value.constraintRaw)) { + throw ValidationError( + '${parent.slug} requires CurseForge ${project.slug}, but top-level ${e.key} excludes that source.', + ); + } + key = e.key; + break; + } + } + key ??= project.slug; + final aliases = aliasesByProject[project.id] ?? const {}; + if (aliases.isNotEmpty) { + for (final alias in aliases) { + (parents[alias] ??= {}).add(parentKey); + } + continue; + } + final node = LocalEntryKey(section, key); + if (resolved.containsKey(node)) { + throw ValidationError( + 'Dependency identity collision for $key; declare a top-level entry with explicit peer slugs and section.', + ); + } + (parents[node] ??= {}).add(parentKey); + final declared = manifest.sectionEntries(section)[key]; + final files = await listCompatibleFiles( + api: api, + registry: registry, + projectId: project.id, + section: section, + modLoader: section == Section.mods ? manifest.loader.mods : null, + pluginLoader: manifest.loader.plugins, + gameVersions: [manifest.mcVersion, ...?declared?.acceptsMc], + channel: declared?.channel ?? Channel.alpha, + ); + if (files.isEmpty) { + throw ValidationError( + '${parent.slug} requires ${project.slug}, which has no compatible CurseForge files.', + ); + } + resolved[node] = CfResolvedEntry( + slug: key, + section: section, + project: project, + file: + files.where((f) => f.id == selectedFiles[node]).firstOrNull ?? + files.first, + candidates: files, + transitive: declared == null, + ); + (aliasesByProject[project.id] ??= {}).add(node); + queue.add(node); + } + } + return CfResolution( + resolved: { + for (final e in resolved.entries) + e.key: CfResolvedEntry( + slug: e.value.slug, + section: e.value.section, + project: e.value.project, + file: e.value.file, + candidates: e.value.candidates, + discoveredViaSearch: e.value.discoveredViaSearch, + transitive: e.value.transitive, + requiredBy: + (parents[e.key] ?? {}) + .map((node) => node.localKey) + .toSet() + .toList() + ..sort(), + ), + }, + notFound: notFound, + ); +} diff --git a/lib/src/service/resolve/download_artifacts.dart b/lib/src/service/resolve/download_artifacts.dart new file mode 100644 index 0000000..2d937c3 --- /dev/null +++ b/lib/src/service/resolve/download_artifacts.dart @@ -0,0 +1,243 @@ +import 'dart:io'; + +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_lock.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../cache.dart'; +import '../artifact_locator.dart'; +import '../console.dart'; +import '../downloader.dart'; +import '../manifest_io.dart'; + +/// Aggregate result of [downloadArtifacts]: per-section counts of +/// cache hits and downloads, plus any per-entry fetch errors that the +/// caller should surface as a single [ValidationError]. +class DownloadArtifactsResult { + final int downloaded; + final int hits; + final List errors; + const DownloadArtifactsResult({ + required this.downloaded, + required this.hits, + required this.errors, + }); +} + +/// Walks every entry in [lock] and ensures its artifact(s) are present +/// in the cache. Per-platform source blocks fetch independently: +/// - `LockedEntryKind.resolved` with a Modrinth source → cached at +/// [GitrinthCache.modrinthPath] keyed by `(host, projectId, +/// versionId)`. +/// - `LockedEntryKind.resolved` with a CurseForge source → cached +/// at [GitrinthCache.curseforgePath] keyed by `(projectId, fileId)`. +/// A missing URL requires manually cached bytes with a matching +/// locked hash, regardless of the project's distribution setting. +/// - `LockedEntryKind.url` → cached at [GitrinthCache.urlPath] keyed +/// by sha512, or under [GitrinthCache.unverifiedUrlPath] when no +/// sha512 is recorded. +/// - `LockedEntryKind.path` → existence check against the project +/// directory (no fetch). +/// +/// Pure plumbing: no policy, no manifest reads. The host for Modrinth +/// entries uses the locked source host or [defaultModrinthHost]. +Future downloadArtifacts({ + required ModsLock lock, + required GitrinthCache cache, + required Downloader downloader, + required Console console, + required ManifestIo io, + required String defaultModrinthHost, +}) async { + final locator = ArtifactLocator( + cache: cache, + projectDir: io.directory.path, + defaultModrinthHost: defaultModrinthHost, + ); + var downloaded = 0; + var hits = 0; + final errors = []; + + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).entries) { + final locked = entry.value; + try { + switch (locked.kind) { + case LockedEntryKind.resolved: + final mr = locked.modrinth; + if (mr != null) { + final delta = await _fetchModrinth( + downloader: downloader, + console: console, + slug: locked.slug, + source: mr, + locator: locator, + ); + downloaded += delta.downloaded; + hits += delta.hits; + } + final cf = locked.curseforge; + if (cf != null) { + final delta = await _fetchCurseforge( + locator: locator, + downloader: downloader, + console: console, + slug: locked.slug, + source: cf, + ); + downloaded += delta.downloaded; + hits += delta.hits; + } + case LockedEntryKind.url: + final delta = await _fetchUrl( + locator: locator, + downloader: downloader, + slug: locked.slug, + file: locked.urlFile, + ); + downloaded += delta.downloaded; + hits += delta.hits; + case LockedEntryKind.path: + _verifyPath(locator: locator, slug: locked.slug, raw: locked.path); + } + } on GitrinthException catch (e) { + errors.add(e.message); + } + } + } + + return DownloadArtifactsResult( + downloaded: downloaded, + hits: hits, + errors: errors, + ); +} + +class _Counts { + final int downloaded; + final int hits; + const _Counts({this.downloaded = 0, this.hits = 0}); +} + +Future<_Counts> _fetchModrinth({ + required Downloader downloader, + required Console console, + required String slug, + required LockedModrinthSource source, + required ArtifactLocator locator, +}) async { + final file = source.file; + final url = file.url; + if (url == null) return const _Counts(); + final dest = locator.modrinth(source); + final existed = File(dest).existsSync(); + await downloader.downloadTo( + url: url, + destinationPath: dest, + expectedSha512: file.sha512, + ); + if (existed) { + console.io('cache hit: $slug -> $dest'); + return const _Counts(hits: 1); + } + console.io('downloaded: $slug -> $dest'); + return const _Counts(downloaded: 1); +} + +Future<_Counts> _fetchCurseforge({ + required ArtifactLocator locator, + required Downloader downloader, + required Console console, + required String slug, + required LockedCurseforgeSource source, +}) async { + final file = source.file; + final dest = locator.curseforge(source); + final existed = File(dest).existsSync(); + final url = file.url; + if (url == null) { + final identity = + '$slug: CurseForge project ${source.projectId}, file ${source.fileId} ' + '(${file.name})'; + final sha1 = file.sha1; + final sha512 = file.sha512; + if (sha1 == null && sha512 == null) { + throw ValidationError( + '$identity has no download URL and no verifiable hash. ' + 'Cached bytes at $dest cannot be verified; use a path: source ' + 'for a manually managed artifact.', + ); + } + final recovery = + 'download manually from the CurseForge project page for ' + 'project ${source.projectId}, file ${source.fileId}, ' + 'save ${file.name} at $dest, then rerun the command.'; + if (!existed) { + throw ValidationError('$identity is missing from the cache; $recovery'); + } + try { + if (sha1 != null) { + await GitrinthCache.verifyFileSha1(File(dest), sha1); + } + if (sha512 != null) { + await GitrinthCache.verifyFileSha512(File(dest), sha512); + } + } on UserError catch (e) { + throw ValidationError('$identity: ${e.message}; $recovery'); + } + console.io('cache hit: $slug (cf) -> $dest'); + return const _Counts(hits: 1); + } + await downloader.downloadTo( + url: url, + destinationPath: dest, + expectedSha1: file.sha1, + expectedSha512: file.sha512, + ); + if (existed) { + console.io('cache hit: $slug (cf) -> $dest'); + return const _Counts(hits: 1); + } + console.io('downloaded: $slug (cf) -> $dest'); + return const _Counts(downloaded: 1); +} + +Future<_Counts> _fetchUrl({ + required ArtifactLocator locator, + required Downloader downloader, + required String slug, + required LockedFile? file, +}) async { + if (file == null) return const _Counts(); + final url = file.url; + if (url == null) return const _Counts(); + final sha512 = file.sha512; + final dest = locator.url(slug, file); + final existed = File(dest).existsSync(); + await downloader.downloadTo( + url: url, + destinationPath: dest, + expectedSha512: sha512, + ); + if (existed) return const _Counts(hits: 1); + return const _Counts(downloaded: 1); +} + +void _verifyPath({ + required ArtifactLocator locator, + required String slug, + required String? raw, +}) { + if (raw == null) { + throw FormatException( + 'lock entry "$slug" with kind=path has no path — mods.lock is ' + 'malformed.', + ); + } + final resolved = locator.path(raw); + if (FileSystemEntity.typeSync(resolved) == FileSystemEntityType.notFound) { + throw ValidationError( + 'path source for "$slug" points to a missing file: ' + '$raw (looked in $resolved)', + ); + } +} diff --git a/lib/src/service/resolve/hash_scan.dart b/lib/src/service/resolve/hash_scan.dart new file mode 100644 index 0000000..5e1f516 --- /dev/null +++ b/lib/src/service/resolve/hash_scan.dart @@ -0,0 +1,147 @@ +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/modrinth/version.dart' as modrinth; + +/// Outcome of [scanForMatchingPair]. Sealed so callers must handle every +/// case at the switch boundary; the lock-builder differentiates between +/// a true SHA1 match, an accepted mismatch (locked at the newest pair), +/// and a depth-exhausted failure (which the caller turns into a +/// [ValidationError]). +sealed class HashOutcome { + const HashOutcome(); +} + +/// SHA1s matched — either the newest pair or an older pair found via +/// the depth scan. The lock builder uses [modrinthVersion] + +/// [curseforgeFile] verbatim. +class HashMatched extends HashOutcome { + final modrinth.Version modrinthVersion; + final ModFile curseforgeFile; + const HashMatched({ + required this.modrinthVersion, + required this.curseforgeFile, + }); +} + +/// SHA1s differed but the user opted in via `allow_hash_mismatch: true`. +/// Lock the newest pair anyway and emit a console warning. Carrier of +/// the same fields as [HashMatched] for caller convenience. +class HashAcceptedMismatch extends HashOutcome { + final modrinth.Version modrinthVersion; + final ModFile curseforgeFile; + const HashAcceptedMismatch({ + required this.modrinthVersion, + required this.curseforgeFile, + }); +} + +/// Scan window exhausted with no SHA1 match and `allow_hash_mismatch` +/// not set. The caller throws a [ValidationError] naming the newest +/// pair and the depth examined. +class HashExhausted extends HashOutcome { + final modrinth.Version newestModrinth; + final ModFile newestCurseforge; + final int depthExamined; + const HashExhausted({ + required this.newestModrinth, + required this.newestCurseforge, + required this.depthExamined, + }); +} + +/// Only one platform produced a candidate. There's nothing to compare; +/// the lock builder uses whichever block exists. Hash scan does not run +/// in this case (callers short-circuit), so this case exists for +/// completeness — callers may pre-filter and never see it. +class HashSingleSided extends HashOutcome { + final modrinth.Version? modrinthVersion; + final ModFile? curseforgeFile; + const HashSingleSided({this.modrinthVersion, this.curseforgeFile}); +} + +/// Compares the newest Modrinth/CurseForge candidates' SHA1s and +/// optionally scans `[hashScanDepth]` additional pairs looking for a +/// match. Pure: no I/O, no Console, no async — the caller threads +/// candidates in newest-first order and decides what to do with the +/// returned [HashOutcome]. +/// +/// Algorithm (`hash_scan_depth: N` counts **additional** pairs beyond +/// the newest pair; default 10; `0` disables): +/// 1. If either side empty, return [HashSingleSided]. +/// 2. Compare newest Modrinth SHA1 vs newest CurseForge SHA1. +/// 3. Match → [HashMatched] on the newest pair. +/// 4. Mismatch + [allowHashMismatch] → [HashAcceptedMismatch] on +/// the newest pair. +/// 5. Mismatch + depth > 0: +/// cfWindow = take(depth + 1) of [cfCandidates] +/// cfBySha = { f.sha1.lower() → f for f in cfWindow if f.sha1 != null } +/// for mr in take(depth + 1) of [mrCandidates]: +/// if mr.sha1 in cfBySha: return matched(mr, cfBySha[mr.sha1]) +/// return [HashExhausted]. +/// 6. Mismatch + depth == 0 → [HashExhausted] (depthExamined == 0). +HashOutcome scanForMatchingPair({ + required List mrCandidates, + required List cfCandidates, + required int hashScanDepth, + required bool allowHashMismatch, +}) { + if (mrCandidates.isEmpty || cfCandidates.isEmpty) { + return HashSingleSided( + modrinthVersion: mrCandidates.isEmpty ? null : mrCandidates.first, + curseforgeFile: cfCandidates.isEmpty ? null : cfCandidates.first, + ); + } + + final newestMr = mrCandidates.first; + final newestCf = cfCandidates.first; + final newestMrSha = _modrinthSha1(newestMr); + final newestCfSha = newestCf.sha1Hash; + + if (newestMrSha != null && + newestCfSha != null && + newestMrSha == newestCfSha) { + return HashMatched(modrinthVersion: newestMr, curseforgeFile: newestCf); + } + + if (hashScanDepth > 0) { + // Index CF candidates within the window by their lowercase SHA1. + final windowSize = hashScanDepth + 1; + final cfWindow = cfCandidates.take(windowSize); + final cfBySha = {}; + for (final f in cfWindow) { + final sha = f.sha1Hash; + if (sha != null) cfBySha[sha] = f; + } + // Walk Modrinth newest→older within the same window for hits. + for (final mr in mrCandidates.take(windowSize)) { + final sha = _modrinthSha1(mr); + if (sha == null) continue; + final cfHit = cfBySha[sha]; + if (cfHit != null) { + return HashMatched(modrinthVersion: mr, curseforgeFile: cfHit); + } + } + } + + if (allowHashMismatch) { + return HashAcceptedMismatch( + modrinthVersion: newestMr, + curseforgeFile: newestCf, + ); + } + + return HashExhausted( + newestModrinth: newestMr, + newestCurseforge: newestCf, + depthExamined: hashScanDepth, + ); +} + +/// Reads the SHA1 (lowercase) from the primary file of a Modrinth +/// version. Modrinth stores hashes per-file in the `hashes` map; the +/// solver-level resolver always picks the primary file. +String? _modrinthSha1(modrinth.Version v) { + final f = v.primaryFile; + if (f == null) return null; + final sha = f.hashes['sha1']; + return sha?.toLowerCase(); +} diff --git a/lib/src/service/resolve/lock_builder.dart b/lib/src/service/resolve/lock_builder.dart new file mode 100644 index 0000000..a0defdc --- /dev/null +++ b/lib/src/service/resolve/lock_builder.dart @@ -0,0 +1,390 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:path/path.dart' as p; + +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/manifest/mods_lock.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/manifest/source_eligibility.dart'; +import '../../model/modrinth/version.dart' as modrinth; +import '../dependency_graph.dart'; +import '../source_identity.dart'; +import '../modrinth/modrinth_url.dart'; +import '../../model/resolver/result.dart'; +import '../../model/resolver/constraint.dart'; +import '../../version.dart'; +import '../cache.dart'; +import '../console.dart'; +import 'curseforge_resolver.dart'; +import 'hash_scan.dart'; + +/// Builds the new `ModsLock` from the resolved Modrinth versions plus +/// CurseForge resolution + hash-scan outcomes plus the manifest's +/// url:/path: entries (which never enter the resolver) and `files:` +/// entries (which are manifest-only). +/// +/// For dual-source slugs (both [resolution] and [cfResolution] resolved +/// the entry), the [hashOutcomes] map carries the per-entry +/// [HashOutcome] result. When the outcome is [HashMatched] / +/// [HashAcceptedMismatch], both source blocks are populated; the +/// Modrinth block uses the matched/accepted Modrinth version and the +/// CurseForge block uses the matched/accepted CurseForge file. +ModsLock buildLock({ + required ModsYaml manifest, + required ResolutionResult resolution, + required CfResolution cfResolution, + required Map hashOutcomes, + required String? resolvedModLoaderVersion, + required String? resolvedPluginLoaderVersion, + Set modrinthSearchSlugs = const {}, + String? Function(Section section, String slug)? hostFor, + String defaultHost = defaultModrinthBaseUrl, +}) { + final mods = {}; + final resourcePacks = {}; + final dataPacks = {}; + final shaders = {}; + final plugins = {}; + final byKind = >{ + Section.mods: mods, + Section.resourcePacks: resourcePacks, + Section.dataPacks: dataPacks, + Section.shaders: shaders, + Section.plugins: plugins, + }; + + final mrNodes = { + for (final r in resolution.entries) LocalEntryKey(r.section, r.slug), + }; + final requiredBy = >{}; + final graph = resolvedDependencyGraph( + resolution, + hostFor: (node) => + hostFor?.call(node.section, node.localKey) ?? defaultHost, + ); + for (final child in resolution.entries) { + final node = LocalEntryKey(child.section, child.slug); + requiredBy[node] = { + for (final parent in graph.parents(node)) parent.localKey, + }; + } + for (final child in cfResolution.resolved.values) { + (requiredBy[LocalEntryKey(child.section, child.slug)] ??= {}).addAll( + child.requiredBy, + ); + } + + // Walk every Modrinth-resolved slug, applying any matching CF block + // and hash outcome. Modrinth-only entries pass through unchanged. + for (final r in resolution.entries) { + final sectionMap = byKind[r.section]; + if (sectionMap == null) { + throw StateError('lock_builder: unknown section ${r.section}'); + } + final entry = manifest.sectionEntries(r.section)[r.slug]; + final cf = cfResolution.resolved[LocalEntryKey(r.section, r.slug)]; + final outcome = hashOutcomes[LocalEntryKey(r.section, r.slug)]; + sectionMap[r.slug] = + _buildResolvedEntry( + slug: r.slug, + manifestEntry: entry, + mrEntry: r, + cfEntry: cf, + hashOutcome: outcome, + modrinthDiscoveredViaSearch: modrinthSearchSlugs.contains( + LocalEntryKey(r.section, r.slug), + ), + ).copyWith( + requiredBy: + (requiredBy[LocalEntryKey(r.section, r.slug)] ?? {}).toList() + ..sort(), + ); + // Persist the source host for dependency lookups and cache addressing. + final built = sectionMap[r.slug]!; + sectionMap[r.slug] = built.copyWith( + modrinth: built.modrinth?.copyWith( + host: hostFor?.call(r.section, r.slug), + ), + ); + } + + // CF-only entries: a slug that resolved on CF but not on Modrinth + // (for example `sources: [curseforge]`). These have no Modrinth + // ResolvedEntry, so they wouldn't appear in the loop above. + for (final cf in cfResolution.resolved.values) { + if (mrNodes.contains(LocalEntryKey(cf.section, cf.slug))) continue; + final sectionMap = byKind[cf.section]; + if (sectionMap == null) continue; + final entry = manifest.sectionEntries(cf.section)[cf.slug]; + sectionMap[cf.slug] = _buildResolvedEntry( + slug: cf.slug, + manifestEntry: entry, + mrEntry: null, + cfEntry: cf, + hashOutcome: null, + ); + } + + for (final section in Section.values) { + final sectionMap = byKind[section]; + if (sectionMap == null) { + throw StateError('lock_builder: unknown section $section'); + } + final entries = manifest.sectionEntries(section); + entries.forEach((slug, entry) { + final src = entry.source; + if (src is UrlEntrySource) { + sectionMap[slug] = LockedEntry( + slug: slug, + kind: LockedEntryKind.url, + url: src.url, + urlFile: LockedFile(name: filenameFromUrl(src.url), url: src.url), + client: entry.client, + server: entry.server, + ); + } else if (src is PathEntrySource) { + sectionMap[slug] = LockedEntry( + slug: slug, + kind: LockedEntryKind.path, + path: src.path, + client: entry.client, + server: entry.server, + ); + } + }); + } + // Bake the resolved concrete loader version into the lock's LoaderConfig. + final lockedLoader = LoaderConfig( + mods: manifest.loader.mods, + modLoaderVersion: resolvedModLoaderVersion, + shaders: manifest.loader.shaders, + plugins: manifest.loader.plugins, + pluginLoaderVersion: resolvedPluginLoaderVersion, + ); + // Forward `files:` entries verbatim. `files:` is manifest-only — no + // pubgrub resolution, no Modrinth round-trip — so the lock entry is + // a 1:1 mirror of the FileEntry. Source-existence is verified later + // in the artifact-fetch loop (mirrors `path:` mod entries). + final lockedFiles = { + for (final e in manifest.files.entries) + e.key: LockedFileEntry( + destination: e.value.destination, + sourcePath: e.value.sourcePath, + client: e.value.client, + server: e.value.server, + preserve: e.value.preserve, + ), + }; + return ModsLock( + gitrinthVersion: packageVersion, + loader: lockedLoader, + mcVersion: manifest.mcVersion, + mods: mods, + resourcePacks: resourcePacks, + dataPacks: dataPacks, + shaders: shaders, + plugins: plugins, + files: lockedFiles, + ); +} + +LockedEntry _buildResolvedEntry({ + required String slug, + required ModEntry? manifestEntry, + required ResolvedEntry? mrEntry, + required CfResolvedEntry? cfEntry, + required HashOutcome? hashOutcome, + bool modrinthDiscoveredViaSearch = false, +}) { + // For dual-source entries, prefer the (mrPicked, cfPicked) returned + // by the hash scan when it found a match (or accepted a mismatch). + // For single-source entries, the resolver's pick is the only one. + modrinth.Version? finalMr; + ModFile? finalCf; + if (hashOutcome is HashMatched) { + finalMr = hashOutcome.modrinthVersion; + finalCf = hashOutcome.curseforgeFile; + } else if (hashOutcome is HashAcceptedMismatch) { + finalMr = hashOutcome.modrinthVersion; + finalCf = hashOutcome.curseforgeFile; + } else { + // No outcome (single-source entry) or HashSingleSided / unhandled. + finalMr = mrEntry?.version; + finalCf = cfEntry?.file; + } + + LockedModrinthSource? mrSource; + if (finalMr != null && mrEntry != null) { + final file = finalMr.primaryFile!; + mrSource = LockedModrinthSource( + projectId: finalMr.projectId, + versionId: finalMr.id, + versionNumber: finalMr.versionNumber, + discoveredViaSearch: modrinthDiscoveredViaSearch, + file: LockedFile( + name: file.filename, + url: file.url, + sha1: file.hashes['sha1']?.toLowerCase(), + sha512: file.sha512, + size: file.size, + ), + gameVersions: List.unmodifiable(finalMr.gameVersions), + ); + } + + LockedCurseforgeSource? cfSource; + if (finalCf != null && cfEntry != null) { + cfSource = LockedCurseforgeSource( + projectId: cfEntry.project.id, + fileId: finalCf.id, + slug: cfEntry.project.slug, + file: LockedFile( + name: finalCf.fileName, + url: finalCf.downloadUrl, + sha1: finalCf.sha1Hash, + size: null, + ), + gameVersions: List.unmodifiable(finalCf.gameVersions), + allowModDistribution: cfEntry.project.allowModDistribution, + discoveredViaSearch: cfEntry.discoveredViaSearch, + ); + } + + final jointLabel = + mrSource?.versionNumber ?? + cfEntry?.file.displayName ?? + cfSource?.file.name; + + // Sides + dependency follow Modrinth's resolved entry when present, + // else fall back to manifest defaults (CF-only entries don't go + // through PubGrub). + final defaults = defaultSidesFor( + mrEntry?.section ?? cfEntry?.section ?? Section.mods, + ); + final client = mrEntry?.client ?? manifestEntry?.client ?? defaults.client; + final server = mrEntry?.server ?? manifestEntry?.server ?? defaults.server; + final dependency = manifestEntry != null + ? LockedDependencyKind.direct + : LockedDependencyKind.transitive; + + return LockedEntry( + slug: slug, + kind: LockedEntryKind.resolved, + version: jointLabel, + modrinth: mrSource, + curseforge: cfSource, + client: client, + server: server, + dependency: dependency, + requiredBy: cfEntry?.requiredBy ?? const [], + acceptsMc: List.unmodifiable(manifestEntry?.acceptsMc ?? const []), + ); +} + +/// Aggregates Modrinth + CurseForge "missing on a requested platform" +/// outcomes into a single [Map] keyed by slug. Used by the caller to +/// produce one [ValidationError] per resolve, not one per entry. +/// +/// `mrNotFoundSlugs` is the set of Modrinth slugs that returned no +/// versions. `cfResolution` carries the per-slug CurseForge failures. +/// The aggregation walks the merged manifest's effective-source set +/// per entry: a slug fails when ANY of its requested eligible sources +/// is missing. +String? formatCrossPlatformMissingError({ + LoaderRegistry registry = LoaderRegistry.builtins, + required ModsYaml manifest, + required Set mrNotFoundSlugs, + required CfResolution cfResolution, + Set allowedMissingSlugs = const {}, +}) { + final cfNotFound = { + for (final f in cfResolution.notFound) LocalEntryKey(f.section, f.slug): f, + }; + final lines = []; + + for (final section in Section.values) { + final entries = manifest.sectionEntries(section); + for (final mapEntry in entries.entries) { + final slug = mapEntry.key; + if (allowedMissingSlugs.contains(LocalEntryKey(section, slug))) continue; + final entry = mapEntry.value; + if (entry.source is! ModrinthEntrySource) continue; + if (isAnyGitrinthMarker(entry.constraintRaw)) continue; + final effective = effectiveSources( + manifest, + entry, + section, + registry: registry, + ); + final missing = []; + if (effective.contains(SourceKind.modrinth) && + mrNotFoundSlugs.contains(LocalEntryKey(section, slug))) { + missing.add('Modrinth'); + } + if (effective.contains(SourceKind.curseforge) && + cfNotFound.containsKey(LocalEntryKey(section, slug))) { + final cf = cfNotFound[LocalEntryKey(section, slug)]!; + missing.add('CurseForge (${cf.reason}, queried "${cf.slugUsed}")'); + } + if (missing.isNotEmpty) { + lines.add(' $slug: not found on ${missing.join(', ')}'); + } + } + } + + if (lines.isEmpty) return null; + return [ + 'The following entries are missing on one or more requested platforms:', + ...lines, + 'Resolution options:', + ' - If the slug differs across platforms, add `modrinth:` / ' + '`curseforge:` peer slug overrides', + ' - If the entry only exists on one platform, restrict via ' + '`sources: [modrinth]` or `sources: [curseforge]`', + ' - Run `gitrinth migrate ` to mark the entry not-found in ' + 'the manifest if you intentionally moved off a platform', + ].join('\n'); +} + +/// Writes a `version.json` next to each resolved Modrinth artifact in +/// the cache, capturing the full Modrinth `Version` payload — its +/// `dependencies` array (consumed by `upgrade --unlock-transitive`) and +/// its `files` array (consumed by `cache repair` to verify and refetch +/// corrupt artifacts). Best effort: a write failure logs a warn but +/// doesn't abort the resolve, because the artifact download is the +/// user-visible contract. +void persistVersionMetadata( + ResolutionResult resolution, + GitrinthCache cache, + Console console, + String Function(ResolvedEntry) hostFor, +) { + for (final r in resolution.entries) { + final pid = r.version.projectId; + final vid = r.version.id; + if (pid.isEmpty || vid.isEmpty) continue; + final path = cache.modrinthVersionMetadataPath( + host: hostFor(r), + projectId: pid, + versionId: vid, + ); + try { + Directory(p.dirname(path)).createSync(recursive: true); + File(path).writeAsStringSync( + const JsonEncoder.withIndent(' ').convert(r.version.toMap()), + ); + } on Object catch (e) { + console.warn( + 'cache: failed to persist version metadata for ${r.slug} ' + '(${r.version.versionNumber}): $e', + ); + } + } +} + +String filenameFromUrl(String url) { + final uri = Uri.parse(url); + if (uri.pathSegments.isEmpty) return 'artifact.jar'; + return uri.pathSegments.last; +} diff --git a/lib/src/service/resolve/lock_graph.dart b/lib/src/service/resolve/lock_graph.dart new file mode 100644 index 0000000..9106e8c --- /dev/null +++ b/lib/src/service/resolve/lock_graph.dart @@ -0,0 +1,172 @@ +import 'dart:convert'; +import 'dart:io'; + +import '../../model/manifest/mods_lock.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../cli/exceptions.dart'; +import '../dependency_graph.dart'; +import '../source_identity.dart'; +import '../cache.dart'; +import '../console.dart'; + +/// Reads the `dependencies` array out of the cached `version.json` +/// and returns the list of `project_id`s for entries whose +/// `dependency_type == "required"`. Returns null when the cache file +/// is missing or unparseable (cold cache); returns an empty list when +/// the version legitimately has no required deps. +List? readCachedRequiredChildren( + GitrinthCache cache, + String host, + String projectId, + String versionId, +) { + final path = cache.modrinthVersionMetadataPath( + host: host, + projectId: projectId, + versionId: versionId, + ); + final file = File(path); + if (!file.existsSync()) return null; + final dynamic raw; + try { + raw = jsonDecode(file.readAsStringSync()); + } on Object { + return null; + } + if (raw is! Map) return null; + final deps = raw['dependencies']; + if (deps is! List) return const []; + final out = []; + for (final d in deps) { + if (d is! Map) continue; + if (d['dependency_type'] != 'required') continue; + final pid = d['project_id']; + if (pid is String && pid.isNotEmpty) out.add(pid); + } + return out; +} + +/// Lock edges preserve section identity; the legacy bare required_by list is +/// used only when its parent can be identified without guessing. +final class LockedDependencyGraph { + final graph = DependencyGraph(); + final identity = SourceIdentity(); + final entries = {}; + final diagnostics = []; + var coldCacheCount = 0; + + LockedDependencyGraph( + ModsLock lock, + GitrinthCache cache, { + required String defaultHost, + }) { + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).entries) { + final node = LocalEntryKey(section, entry.key); + final host = entry.value.modrinth?.host ?? defaultHost; + entries[node] = entry.value; + identity.register(node, host: host); + graph.addNode(node); + final mr = entry.value.modrinth; + if (mr != null) { + identity.bind(node, ModrinthProjectKey(host, mr.projectId)); + } + } + } + for (final item in entries.entries) { + final mr = item.value.modrinth; + if (mr == null || mr.projectId.isEmpty || mr.versionId.isEmpty) continue; + final children = readCachedRequiredChildren( + cache, + identity.host(item.key), + mr.projectId, + mr.versionId, + ); + if (children == null) { + coldCacheCount++; + continue; + } + for (final project in children) { + for (final child in identity.aliases( + ModrinthProjectKey(identity.host(item.key), project), + )) { + graph.addEdge(item.key, child); + } + } + } + for (final item in entries.entries) { + for (final parentName in item.value.requiredBy) { + final candidates = identity + .nodesNamed(parentName) + .where( + (node) => canRequireLockedSource( + entries[node]!, + item.value, + defaultModrinthHost: defaultHost, + ), + ) + .toList(); + if (candidates.length > 1) { + final knownParents = graph.parents(item.key); + final metadataParents = candidates + .where(knownParents.contains) + .toList(); + if (metadataParents.isNotEmpty) { + continue; + } + } + if (candidates.length == 1) { + graph.addEdge(candidates.single, item.key); + } else if (candidates.length > 1) { + diagnostics.add( + "Ambiguous required_by parent '$parentName' for ${item.key}; omitting that edge.", + ); + } + } + } + } +} + +Set walkTransitiveClosure( + Set seeds, + ModsLock? lock, + GitrinthCache cache, { + required String defaultHost, + Console? console, + String verboseLabel = 'walkTransitiveClosure', +}) { + if (lock == null) { + console?.io( + '$verboseLabel: no mods.lock found yet — falling back to the named entries.', + ); + return seeds; + } + final indexed = LockedDependencyGraph(lock, cache, defaultHost: defaultHost); + if (indexed.diagnostics.isNotEmpty) { + throw UserError(indexed.diagnostics.join('\n')); + } + final nodes = []; + for (final seed in seeds) { + final matches = indexed.identity.nodesNamed(seed); + if (matches.length > 1) { + throw UserError( + "Ambiguous entry '$seed' in mods.lock; cannot unlock its transitive closure.", + ); + } + nodes.addAll(matches); + } + if (indexed.coldCacheCount > 0) { + console?.io( + '$verboseLabel: no cached version.json for ${indexed.coldCacheCount} entries. Run `gitrinth get` to populate the cache.', + ); + } + final reachable = indexed.graph.reachable(nodes); + for (final node in reachable) { + if (indexed.identity.nodesNamed(node.localKey).length > 1) { + throw UserError( + "Ambiguous entry '${node.localKey}' in mods.lock; cannot unlock its transitive closure.", + ); + } + } + return {...seeds, for (final node in reachable) node.localKey}; +} diff --git a/lib/src/service/mod_loader_version_resolver.dart b/lib/src/service/resolve/mod_loader_version_resolver.dart similarity index 88% rename from lib/src/service/mod_loader_version_resolver.dart rename to lib/src/service/resolve/mod_loader_version_resolver.dart index ff50807..52b79e4 100644 --- a/lib/src/service/mod_loader_version_resolver.dart +++ b/lib/src/service/resolve/mod_loader_version_resolver.dart @@ -3,9 +3,11 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:pub_semver/pub_semver.dart'; -import '../cli/exceptions.dart'; -import '../model/manifest/mods_yaml.dart'; -import '../util/mc_version.dart'; +import 'catalog_loader_version.dart'; + +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../util/mc_version.dart'; /// Resolves the docker-style `:` syntax in `mods.yaml` to a /// concrete loader version. @@ -24,6 +26,7 @@ import '../util/mc_version.dart'; class ModLoaderVersionResolver { final Dio _dio; final String _fabricMetaUrl; + final String _quiltMetaUrl; final String _forgePromotionsUrl; final String _forgeVersionsUrl; final String _neoforgeVersionsUrl; @@ -38,6 +41,9 @@ class ModLoaderVersionResolver { String? neoforgeVersionsUrl, String? neoforgeLegacyVersionsUrl, }) : _dio = dio, + _quiltMetaUrl = + (environment ?? Platform.environment)['GITRINTH_QUILT_META_URL'] ?? + 'https://meta.quiltmc.org/v3/versions/loader', _fabricMetaUrl = fabricMetaUrl ?? (environment ?? Platform.environment)['GITRINTH_FABRIC_META_URL'] ?? @@ -68,26 +74,52 @@ class ModLoaderVersionResolver { /// per-Minecraft-version. Fabric ignores it. Future resolve({ required ModLoader loader, + LoaderRegistry registry = LoaderRegistry.builtins, required String tag, required String mcVersion, }) async { - switch (loader) { - case ModLoader.fabric: + final descriptor = registry[loader]; + if (descriptor == null) { + throw ValidationError( + 'Loader ${loader.name} has no capability snapshot for version discovery.', + ); + } + switch (descriptor.versionSource) { + case VersionSourceFamily.fabric: return _resolveFabric(tag); - case ModLoader.forge: + case VersionSourceFamily.quilt: + return _resolveQuilt(tag); + case VersionSourceFamily.forge: return _resolveForge(tag, mcVersion); - case ModLoader.neoforge: + case VersionSourceFamily.neoforge: return mcVersion == '1.20.1' ? _resolveNeoforgeLegacy(tag) : _resolveNeoforge(tag, mcVersion); - case ModLoader.vanilla: - throw StateError( - 'loader-version resolution is not defined for vanilla; ' - 'callers must guard with LoaderConfig.hasModRuntime.', + case VersionSourceFamily.catalog: + return resolveCatalogLoaderVersion(descriptor, tag, mcVersion); + default: + throw ValidationError( + 'Loader ${loader.name} has no version-discovery capability for mods.', ); } } + Future _resolveQuilt(String tag) async { + final body = await _fetchJson(_quiltMetaUrl, 'meta.quiltmc.org'); + if (body is List) { + for (final entry in body) { + final version = entry is Map ? entry['version'] : null; + if (version is! String) continue; + if (tag == version || + tag == 'latest' || + (tag == 'stable' && !version.contains('-'))) { + return version; + } + } + } + throw UserError('No Quilt loader version matched $tag.'); + } + Future _resolveFabric(String tag) async { final body = await _fetchJson(_fabricMetaUrl, 'meta.fabricmc.net'); if (body is! List || body.isEmpty) { diff --git a/lib/src/service/plugin_loader_version_resolver.dart b/lib/src/service/resolve/plugin_loader_version_resolver.dart similarity index 71% rename from lib/src/service/plugin_loader_version_resolver.dart rename to lib/src/service/resolve/plugin_loader_version_resolver.dart index 0e93273..f7d5bac 100644 --- a/lib/src/service/plugin_loader_version_resolver.dart +++ b/lib/src/service/resolve/plugin_loader_version_resolver.dart @@ -2,12 +2,14 @@ import 'dart:io'; import 'package:dio/dio.dart'; -import '../cli/exceptions.dart'; -import '../model/manifest/mods_yaml.dart'; -import '../util/url_template.dart'; -import 'dio_error_helpers.dart'; -import 'paper_api_client.dart'; -import 'sponge_api_client.dart'; +import 'catalog_loader_version.dart'; + +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../util/url_template.dart'; +import '../dio_error_helpers.dart'; +import '../paper_api_client.dart'; +import '../sponge_api_client.dart'; class PluginLoaderVersionResolver { final Dio _dio; @@ -41,20 +43,37 @@ class PluginLoaderVersionResolver { Future resolve({ required PluginLoader loader, + LoaderRegistry registry = LoaderRegistry.builtins, required String tag, required String mcVersion, }) async { - switch (loader) { - case PluginLoader.paper: - case PluginLoader.folia: - return _resolvePaperLike(loader.name, tag, mcVersion); - case PluginLoader.spongeforge: - case PluginLoader.spongeneo: - case PluginLoader.spongevanilla: - return _resolveSponge(loader, tag, mcVersion); - case PluginLoader.spigot: - case PluginLoader.bukkit: + final descriptor = registry[loader]; + if (descriptor == null) { + throw ValidationError( + 'Loader ${loader.name} has no capability snapshot for plugin version discovery.', + ); + } + switch (descriptor.versionSource) { + case VersionSourceFamily.paper: + return _resolvePaperLike( + descriptor.runtimeArtifact ?? loader.name, + tag, + mcVersion, + ); + case VersionSourceFamily.sponge: + return _resolveSponge( + descriptor.runtimeArtifact ?? loader.name, + tag, + mcVersion, + ); + case VersionSourceFamily.buildtools: return _resolveBuildTools(tag); + case VersionSourceFamily.catalog: + return resolveCatalogLoaderVersion(descriptor, tag, mcVersion); + default: + throw ValidationError( + 'Loader ${loader.name} has no plugin version-discovery capability.', + ); } } @@ -79,16 +98,17 @@ class PluginLoaderVersionResolver { } Future _resolveSponge( - PluginLoader loader, + String artifact, String tag, String mcVersion, ) async { - final artifact = switch (loader) { - PluginLoader.spongeforge => 'spongeforge', - PluginLoader.spongeneo => 'spongeneo', - PluginLoader.spongevanilla => 'spongevanilla', - _ => throw StateError('_resolveSponge called for ${loader.name}'), - }; + if (!const { + 'spongeforge', + 'spongeneo', + 'spongevanilla', + }.contains(artifact)) { + throw ValidationError('No trusted Sponge version recipe for $artifact.'); + } final build = switch (tag) { 'stable' => await _spongeApi.latestRecommendedBuild( artifact: artifact, diff --git a/lib/src/service/resolve/resolve_and_sync.dart b/lib/src/service/resolve/resolve_and_sync.dart new file mode 100644 index 0000000..bbf298e --- /dev/null +++ b/lib/src/service/resolve/resolve_and_sync.dart @@ -0,0 +1,1819 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:path/path.dart' as p; + +import '../../cli/exceptions.dart'; +import '../../cli/exit_codes.dart'; +import '../../commands/version_picker.dart'; +import '../../model/curseforge/cf_constants.dart'; +import '../../model/curseforge/cf_file_hash.dart'; +import '../../model/curseforge/cf_mod.dart'; +import '../../model/curseforge/cf_mod_file.dart'; +import '../../model/manifest/emitter.dart'; +import '../../model/manifest/mods_lock.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/manifest/overrides_merger.dart'; +import '../../model/modrinth/project.dart'; +import '../../model/modrinth/version.dart' as modrinth; +import '../../model/resolver/constraint.dart'; +import '../../model/resolver/modrinth_solver_adapter.dart'; +import '../../model/resolver/pubgrub.dart'; +import '../../model/resolver/resolver.dart'; +import '../../model/resolver/version_selection.dart'; +import '../../model/manifest/source_eligibility.dart'; +import '../cache.dart'; +import '../source_identity.dart'; +import '../dependency_graph.dart'; +import '../console.dart'; +import '../curseforge/curseforge_api_factory.dart'; +import '../curseforge/curseforge_metadata_store.dart'; +import '../curseforge/curseforge_version.dart'; +import '../downloader.dart'; +import '../manifest_io.dart'; +import '../loader_registry.dart' show validateLoaderCompatibility; +import '../modrinth/modrinth_api.dart'; +import '../modrinth/modrinth_api_factory.dart'; +import '../modrinth/modrinth_url.dart'; +import '../modrinth/modrinth_resolution_snapshot.dart'; +import 'curseforge_resolver.dart'; +import 'curseforge_relationships.dart'; +import 'curseforge_resolution_api.dart'; +import 'cross_platform_search.dart'; +import 'download_artifacts.dart'; +import 'hash_scan.dart'; +import 'lock_builder.dart'; +import 'mod_loader_version_resolver.dart'; +import 'plugin_loader_version_resolver.dart'; +import 'section_inference.dart'; +import 'solve_report.dart'; + +/// Returns true when [v] is admissible under both filters used during +/// resolve: the caller's loader filter (`null` means "any loader is OK" +/// — used for resource_packs / data_packs) and the game-version set. +bool _matchesLoaderAndMc( + modrinth.Version v, + List? loaderFilter, + List gameVersions, +) { + final loaderOk = loaderFilter == null || v.loaders.any(loaderFilter.contains); + final mcOk = v.gameVersions.any(gameVersions.contains); + return loaderOk && mcOk; +} + +/// Result of [resolveAndSync]: exit code plus enough detail for the caller +/// to print its own `Got dependencies!` / `Changed N…` summary. +class ResolveSyncResult { + final ModsLock? newLock; + final List diff; + final int changeCount; + final int outdated; + final int exitCode; + + const ResolveSyncResult({ + required this.newLock, + required this.diff, + required this.changeCount, + required this.outdated, + required this.exitCode, + }); +} + +/// Re-resolves `mods.yaml`, writes `mods.lock`, and downloads/verifies every +/// artifact. Shared between `gitrinth get` and `gitrinth add` so both commands +/// produce identical `+ slug version` / `Changed N dependencies!` output. +/// +/// When [dryRun] is true, the lockfile and cache are not written; the result +/// carries [exitValidationError] if the lock would have changed, otherwise +/// [exitOk]. When [enforce] is true, any lock change throws a +/// [ValidationError] (same semantics as `gitrinth get --enforce-lockfile`). +/// +/// [freshSlugs] holds slugs whose `mods.lock` entries should NOT be passed to +/// the resolver as soft pins, so the resolver picks newest-within-constraint +/// instead of preserving the existing lock. [relaxConstraints] holds slugs +/// whose version constraint should be treated as `any` for resolution. Both +/// power `gitrinth upgrade`; `get` leaves them empty. The diff and the +/// rebuilt lock still come from the *original* manifest and lock, so per-entry +/// metadata (env/optional/channel/etc.) is preserved. +/// +/// [manifestOverride] replaces the on-disk `mods.yaml` for resolution. +/// `gitrinth migrate` uses this to resolve against a mutated target without +/// first writing `mods.yaml`. +Future resolveAndSync({ + required ManifestIo io, + required Console console, + required ModrinthApiFactory apiFactory, + required CurseForgeApiFactory cfFactory, + required GitrinthCache cache, + required Downloader downloader, + required ModLoaderVersionResolver modLoaderResolver, + required PluginLoaderVersionResolver pluginLoaderResolver, + LoaderRegistry loaderRegistry = LoaderRegistry.builtins, + bool offline = false, + bool dryRun = false, + bool enforce = false, + bool noSearch = false, + Set allowMissingSourcesForSlugs = const {}, + Set freshSlugs = const {}, + Set relaxConstraints = const {}, + ModsYaml? manifestOverride, + SolveType solveType = SolveType.get, +}) async { + final reporter = SolveReporter(console); + + final rawManifest = manifestOverride ?? io.readModsYaml(); + validateLoaderCompatibility( + loaderRegistry, + rawManifest.loader.mods, + rawManifest.loader.plugins, + ); + final manifest = rawManifest.copyWith( + mods: coerceModsForPluginLoader( + rawManifest.mods, + rawManifest.loader.plugins, + registry: loaderRegistry, + ), + ); + final defaultBaseUrl = normalizeServerKey(apiFactory.defaultBaseUrl); + // Effective host for entries that don't override their own. Used for + // the pack-level slug → projectType lookups (project_overrides + // section inference) and for the one-shot `mc_version` validation. + final packHost = normalizeServerKey(manifest.modrinthHost ?? defaultBaseUrl); + final apiReads = ModrinthResolutionSnapshot(apiFactory); + final api = apiReads.forHost(packHost); + final projectOverrides = io.readProjectOverrides(); + final mergedResult = await applyOverrides( + manifest, + projectOverrides, + inferSectionForTransitive: (slug) async { + if (offline) { + // Without network, we can't infer the project type. Fall back + // to mods — the override author can write a `type:` hint or + // declare the slug under the right section explicitly to + // avoid this fallback. + return Section.mods; + } + try { + final proj = await api.getProject(slug); + return inferSectionFromProject( + projectType: proj.projectType, + loaders: proj.loaders, + ); + } on DioException catch (e) { + final err = e.error; + if (err is GitrinthException) throw err; + throw UserError( + 'project_overrides: failed to look up Modrinth project ' + '"$slug" to infer its section: ${e.message}', + ); + } + }, + ); + final merged = mergedResult.manifest; + final overriddenSlugs = mergedResult.overriddenSlugs; + final existingLock = io.readModsLock(); + + if (enforce) { + _checkUserEntriesPresentInLock(merged, existingLock); + } + + final lockForResolution = _stripLockPins(existingLock, freshSlugs); + var manifestForResolution = _relaxManifestConstraints( + merged, + relaxConstraints, + ); + + final loaderConfig = merged.loader; + final mc = merged.mcVersion; + + // One-shot mc-version validation: only call /tag/game_version when the + // mc-version differs from what mods.lock already records. The pairing + // was already validated when it was first locked, so a stable lock is + // proof enough. Skipped under --offline since we can't reach the tag + // endpoint; trust mods.yaml. + if (existingLock?.mcVersion != mc && !offline) { + await _validateGameVersion(api: api, mcVersion: mc); + } + + // Loader-tag resolution. Concrete tags pass through unchanged with no + // network call; `stable` / `latest` always re-resolve (those tags exist + // precisely to drift). For concrete tags that already match the lock, + // skip even the passthrough — the lock is the cache. Under --offline, + // `stable`/`latest` falls back to the lock's concrete version. + final String? resolvedModLoaderVersion; + final modLoaderVersion = loaderConfig.modLoaderVersion; + if (loaderConfig.hasModRuntime && modLoaderVersion != null) { + resolvedModLoaderVersion = await _resolveModLoaderVersion( + modLoaderResolver: modLoaderResolver, + registry: loaderRegistry, + modLoader: loaderConfig.mods, + tag: modLoaderVersion, + mcVersion: mc, + existingLock: existingLock, + offline: offline, + console: console, + ); + } else { + resolvedModLoaderVersion = null; + } + + final String? resolvedPluginLoaderVersion; + final pluginLoader = loaderConfig.plugins; + final pluginLoaderVersion = loaderConfig.pluginLoaderVersion; + if (pluginLoader != null && pluginLoaderVersion != null) { + resolvedPluginLoaderVersion = await _resolvePluginLoaderVersion( + pluginLoaderResolver: pluginLoaderResolver, + registry: loaderRegistry, + pluginLoader: pluginLoader, + tag: pluginLoaderVersion, + mcVersion: mc, + existingLock: existingLock, + offline: offline, + console: console, + ); + } else { + resolvedPluginLoaderVersion = null; + } + + final identities = SourceIdentity(); + for (final section in Section.values) { + for (final e in merged.sectionEntries(section).entries) { + identities.register( + LocalEntryKey(section, e.key), + host: manifest.effectiveModrinthHost(e.value, defaultBaseUrl), + projectSlug: e.value.modrinthSlug, + ); + } + } + final versionsPerSlug = >{}; + Section sectionFor(String key) => identities.nodeForSolverKey(key).section; + String keyFor(Section section, String slug) { + final node = LocalEntryKey(section, slug); + if (!identities.nodes.contains(node)) { + final locked = existingLock?.sectionFor(section)[slug]?.modrinth; + final host = locked == null ? packHost : locked.host ?? defaultBaseUrl; + identities.register(node, host: host, projectSlug: locked?.projectId); + if (locked != null) { + identities.bind(node, ModrinthProjectKey(host, locked.projectId)); + } + } + return identities.solverKey(node); + } + + String entryHost(Section section, String slug, ModEntry? declaredEntry) { + final node = LocalEntryKey(section, slug); + if (identities.nodes.contains(node)) return identities.host(node); + final entry = declaredEntry ?? merged.sectionEntries(section)[slug]; + return entry == null + ? packHost + : manifest.effectiveModrinthHost(entry, defaultBaseUrl); + } + + // CurseForge resolution pass. Under --offline we skip the network + // walk and synthesize a CfResolution from the existing lock so the + // builder re-emits every prior CF source block byte-for-byte. The + // cache supplies the actual artifacts. + final cfApi = CurseForgeResolutionApi(cfFactory.api); + CfResolution cfResolution; + if (offline) { + console.warn( + 'Offline: using locked CurseForge files and cached relationship metadata.', + ); + cfResolution = existingLock == null + ? CfResolution.empty + : synthesizeCfResolutionFromLock( + existingLock, + cache: cache, + registry: loaderRegistry, + manifest: merged, + defaultModrinthHost: defaultBaseUrl, + ); + final missingCf = []; + for (final section in Section.values) { + for (final entry in merged.sectionEntries(section).values) { + if (entry.source is! ModrinthEntrySource || + isAnyGitrinthMarker(entry.constraintRaw)) { + continue; + } + if (!effectiveSources( + merged, + entry, + section, + registry: loaderRegistry, + ).contains(SourceKind.curseforge)) { + continue; + } + final prior = cfResolution.resolved[LocalEntryKey(section, entry.slug)]; + if (prior == null || + prior.project.id <= 0 || + prior.file.id <= 0 || + prior.file.fileName.isEmpty) { + missingCf.add('${section.name}/${entry.slug}'); + continue; + } + final wantedMc = {mc, ...entry.acceptsMc}; + final matchesMc = prior.file.gameVersions.isEmpty + ? wantedMc.contains(existingLock?.mcVersion) + : prior.file.gameVersions.any(wantedMc.contains); + final peer = entry.curseforgeSlug; + final matchesPeer = + peer == null || + peer == prior.project.slug || + int.tryParse(peer) == prior.project.id; + final matchesLoader = switch (section) { + Section.mods => existingLock?.loader.mods == loaderConfig.mods, + Section.plugins => + existingLock?.loader.plugins == loaderConfig.plugins, + _ => true, + }; + final cfOnly = !effectiveSources( + merged, + entry, + section, + registry: loaderRegistry, + ).contains(SourceKind.modrinth); + if (!matchesMc || + !matchesPeer || + !matchesLoader || + (cfOnly && + !curseForgeFileSatisfies(prior.file, entry.constraintRaw))) { + throw ValidationError( + 'Cannot resolve ${entry.slug} while offline: its locked CurseForge file ' + 'does not satisfy the current version, Minecraft version, loader, or peer. ' + 'Run `gitrinth get` online first.', + ); + } + } + } + if (missingCf.isNotEmpty) { + throw ValidationError( + 'Cannot resolve CurseForge entries while offline: ${missingCf.join(', ')} ' + 'have no usable CurseForge block in mods.lock. Run `gitrinth get` online first.', + ); + } + } else { + cfResolution = await resolveCurseforgeEntries( + registry: loaderRegistry, + manifest: merged, + api: cfApi, + console: console, + ); + } + + for (final cf in cfResolution.resolved.values) { + final node = LocalEntryKey(cf.section, cf.slug); + if (!identities.nodes.contains(node)) { + identities.register(node, host: packHost); + } + identities.bind(node, CurseforgeProjectKey(cf.project.id)); + } + + final search = CrossPlatformSearch( + registry: loaderRegistry, + manifest: manifestForResolution, + mrFactory: apiReads, + cfApi: cfApi, + console: console, + disabled: offline || noSearch, + ); + final searchedMrVersions = >{}; + final hashVersionIds = {}; + + final resolver = Resolver( + sectionForSlug: sectionFor, + nodeKeyFor: keyFor, + localNameFor: (key) => identities.nodeForSolverKey(key).localKey, + formatError: identities.displayMessage, + resolveDependencySlug: (projectId, parentSlug) async { + final parent = identities.nodeForSolverKey(parentSlug); + final host = identities.host(parent); + final aliases = identities.aliases(ModrinthProjectKey(host, projectId)); + final currentAliases = offline + ? aliases + : aliases + .where( + (node) => manifestForResolution + .sectionEntries(node.section) + .containsKey(node.localKey), + ) + .toSet(); + final sameSection = currentAliases.where( + (node) => node.section == parent.section, + ); + if (sameSection.length == 1) { + return identities.solverKey(sameSection.single); + } + if (currentAliases.length == 1) { + return identities.solverKey(currentAliases.single); + } + if (offline) { + for (final section in Section.values) { + for (final e + in existingLock?.sectionFor(section).entries ?? + >[]) { + if (e.value.modrinth?.projectId != projectId) continue; + if ((e.value.modrinth?.host ?? defaultBaseUrl) != host) continue; + final node = LocalEntryKey(section, e.key); + identities.register(node, host: host, projectSlug: projectId); + identities.bind(node, ModrinthProjectKey(host, projectId)); + return identities.solverKey(node); + } + } + throw ValidationError( + 'Cannot resolve dependency $projectId required by ${parent.localKey} while offline: missing from mods.lock.', + ); + } + final project = await apiReads.forHost(host).getProject(projectId); + final declaredSections =
{ + for (final section in Section.values) + for (final e in manifestForResolution.sectionEntries(section).entries) + if (e.value.source is ModrinthEntrySource && + entryHost(section, e.key, e.value) == host && + ((e.value.modrinthSlug ?? e.key) == project.slug || + e.value.modrinthSlug == project.id)) + section, + }; + if (declaredSections.isEmpty && aliases.length == 1) { + return identities.solverKey(aliases.single); + } + final section = + (declaredSections.contains(parent.section) + ? parent.section + : declaredSections.firstOrNull) ?? + inferSectionFromProject( + projectType: project.projectType, + loaders: project.loaders, + ); + var key = project.slug; + for (final e in manifestForResolution.sectionEntries(section).entries) { + if (e.value.source is! ModrinthEntrySource) continue; + if (entryHost(section, e.key, e.value) != host) continue; + if ((e.value.modrinthSlug ?? e.key) != project.slug && + e.value.modrinthSlug != project.id) { + continue; + } + if (!effectiveSources( + manifestForResolution, + e.value, + section, + registry: loaderRegistry, + ).contains(SourceKind.modrinth) || + isAnyGitrinthMarker(e.value.constraintRaw)) { + throw ValidationError( + '${parent.localKey} requires Modrinth ${project.slug}, but top-level ${e.key} excludes that source.', + ); + } + key = e.key; + break; + } + final declaredSameKey = manifestForResolution.sectionEntries( + section, + )[key]; + if ((declaredSameKey != null && + entryHost(section, key, declaredSameKey) != host) || + (identities.find(key, section) != null && + identities.host(LocalEntryKey(section, key)) != host)) { + key = '${project.slug}@${Uri.encodeComponent(host)}'; + } + final node = LocalEntryKey(section, key); + identities.register(node, host: host, projectSlug: project.slug); + identities.bind(node, ModrinthProjectKey(host, projectId)); + return identities.solverKey(node); + }, + listVersions: (key) async { + final node = identities.nodeForSolverKey(key); + final slug = node.localKey; + final section = node.section; + final entry = manifestForResolution.sectionEntries(section)[slug]; + final gameVersions = {mc, ...?entry?.acceptsMc}.toList(); + final host = entryHost(section, slug, entry); + final loaderFilter = filterLoadersForSection( + loaderConfig, + section, + registry: loaderRegistry, + host: host, + ); + + final searched = searchedMrVersions[LocalEntryKey(section, slug)]; + if (searched != null) { + for (final version in searched) { + identities.bind(node, ModrinthProjectKey(host, version.projectId)); + } + versionsPerSlug[LocalEntryKey(section, slug)] = searched; + return searched.map(ModrinthSolverVersion.new).toList(); + } + if (offline) { + final lockedProjectId = existingLock + ?.sectionFor(section)[slug] + ?.modrinth + ?.projectId; + if (lockedProjectId == null) { + throw UserError( + 'cannot resolve "$slug" while offline: not present in mods.lock ' + 'and never cached. Try again without --offline, or run ' + '`gitrinth get` once with the network available.', + ); + } + final cached = cache + .listCachedVersions(host, lockedProjectId) + .where((v) => _matchesLoaderAndMc(v, loaderFilter, gameVersions)) + .toList(); + for (final version in cached) { + identities.bind(node, ModrinthProjectKey(host, version.projectId)); + } + versionsPerSlug[LocalEntryKey(section, slug)] = cached; + return cached.map(ModrinthSolverVersion.new).toList(); + } + + try { + final list = await apiReads + .forHost(host) + .listVersions( + entry?.modrinthSlug ?? identities.projectSlug(node), + loadersJson: loaderFilter == null + ? null + : encodeFilterArray(loaderFilter), + gameVersionsJson: encodeFilterArray(gameVersions), + ); + for (final version in list) { + identities.bind(node, ModrinthProjectKey(host, version.projectId)); + } + versionsPerSlug[LocalEntryKey(section, slug)] = list; + return list.map(ModrinthSolverVersion.new).toList(); + } on Object catch (e) { + final anchor = cfResolution.resolved[LocalEntryKey(section, slug)]; + if (e is DioException && + e.response?.statusCode == 404 && + entry != null && + anchor != null) { + final found = await search.findModrinth( + slug: slug, + section: section, + entry: entry, + anchor: anchor, + ); + if (found != null) { + searchedMrVersions[LocalEntryKey(section, slug)] = found; + for (final version in found) { + identities.bind( + node, + ModrinthProjectKey(host, version.projectId), + ); + } + versionsPerSlug[LocalEntryKey(section, slug)] = found; + return found.map(ModrinthSolverVersion.new).toList(); + } + final detail = search.diagnostics[LocalEntryKey(section, slug)]; + if (detail != null) { + throw ValidationError( + '$slug: $detail\nSet a peer slug override or restrict `sources:`.', + ); + } + } + if (allowMissingSourcesForSlugs.contains(slug) && + e is DioException && + e.response?.statusCode == 404 && + cfResolution.resolved.containsKey(LocalEntryKey(section, slug))) { + versionsPerSlug[LocalEntryKey(section, slug)] = []; + return []; + } + final err = (e is DioException) ? e.error : e; + if (err is GitrinthException) throw err; + throw UserError('failed to list versions for $slug: $e'); + } + }, + resolveSlugForProjectId: (projectId) async { + try { + final Project proj = await apiReads + .forHost(packHost) + .getProject(projectId); + return proj.slug; + } on Object { + return null; + } + }, + solveType: solveType, + ); + + // Promote each Modrinth-source override entry to a concrete + // Modrinth Version. This is the sticky pre-decision the resolver + // honors: the slug never enters the candidate-search loop. URL/path + // overrides take the lock builder's url/path branches and bypass + // the resolver entirely. + final overridePins = {}; + for (final (section, slug, entry) in [ + for (final section in Section.values) + for (final entry in merged.sectionEntries(section).entries) + if (mergedResult.overrideEntries.containsKey(entry.key)) + (section, entry.key, entry.value), + ]) { + if (entry.source is! ModrinthEntrySource) continue; + if (!effectiveSources( + merged, + entry, + section, + registry: loaderRegistry, + ).contains(SourceKind.modrinth)) { + continue; + } + final gameVersions = {mc, ...entry.acceptsMc}.toList(); + final overrideHost = entryHost(section, slug, entry); + final loaderFilter = filterLoadersForSection( + loaderConfig, + section, + registry: loaderRegistry, + host: overrideHost, + ); + final List candidates; + if (offline) { + final lockedProjectId = existingLock + ?.sectionFor(section)[slug] + ?.modrinth + ?.projectId; + if (lockedProjectId == null) { + throw UserError( + 'cannot apply project_overrides for "$slug" while offline: ' + 'not present in mods.lock and never cached.', + ); + } + candidates = cache + .listCachedVersions(overrideHost, lockedProjectId) + .where((v) => _matchesLoaderAndMc(v, loaderFilter, gameVersions)) + .toList(); + } else { + try { + candidates = await apiReads + .forHost(overrideHost) + .listVersions( + entry.modrinthSlug ?? slug, + loadersJson: loaderFilter == null + ? null + : encodeFilterArray(loaderFilter), + gameVersionsJson: encodeFilterArray(gameVersions), + ); + } on Object catch (err) { + final e = (err is DioException) ? err.error : err; + if (e is GitrinthException) throw e; + throw UserError( + 'project_overrides: failed to list versions for "$slug": $err', + ); + } + } + final picked = pickHighestMatching( + candidates, + parseConstraint(entry.constraintRaw), + entry.channel ?? Channel.alpha, + solveType: solveType, + ); + if (picked == null) { + throw ValidationError( + "project_overrides: '$slug' has no published version matching " + "${entry.constraintRaw ?? 'any'} on ${manifest.loader.mods.name} " + "$mc", + ); + } + overridePins[keyFor(section, slug)] = OverridePin( + keyFor(section, slug), + ModrinthSolverVersion(picked), + ); + versionsPerSlug[LocalEntryKey(section, slug)] = candidates; + } + + console.message('Resolving dependencies...'); + console.solver( + 'Resolving with loader.mods=${loaderConfig.mods.name} mc=$mc...', + ); + + // Dispatch gate: compute the set of slugs that Modrinth participates + // in. Entries whose effective sources exclude Modrinth (e.g. + // `sources: [curseforge]`) are passed to the CF resolver only. + final modrinthSlugs = {}; + for (final section in Section.values) { + merged.sectionEntries(section).forEach((slug, entry) { + if (entry.source is! ModrinthEntrySource) return; + if (effectiveSources( + merged, + entry, + section, + registry: loaderRegistry, + ).contains(SourceKind.modrinth)) { + modrinthSlugs.add(keyFor(section, slug)); + } + }); + } + + // CF-discovered synthetics may also be present on Modrinth. A missing + // peer is permitted for synthetics; declared roots remain strict. + final promotedCfRoots = {}; + Future promoteCfSynthetics() async { + var changed = false; + if (offline || !merged.gitrinth.isPlatformEnabled(SourceKind.modrinth)) { + return false; + } + for (final node in promotedCfRoots.toList()) { + if (cfResolution.resolved.containsKey(node)) continue; + modrinthSlugs.remove(keyFor(node.section, node.localKey)); + searchedMrVersions.remove(node); + manifestForResolution = _withoutResolutionEntry( + manifestForResolution, + node.section, + node.localKey, + ); + promotedCfRoots.remove(node); + changed = true; + } + for (final cf in cfResolution.resolved.values.where((e) => e.transitive)) { + if (merged.sectionEntries(cf.section).containsKey(cf.slug) || + modrinthSlugs.contains(keyFor(cf.section, cf.slug))) { + continue; + } + final synthetic = ModEntry( + slug: cf.slug, + sources: const {SourceKind.modrinth, SourceKind.curseforge}, + ); + List candidates = []; + var promotedSlug = cf.slug; + try { + final peer = await apiReads + .forHost(packHost) + .getProject(cf.project.slug); + final peerSection = inferSectionFromProject( + projectType: peer.projectType, + loaders: peer.loaders, + ); + if (peerSection != cf.section) { + throw ValidationError( + 'Transitive ${cf.slug} resolves to different platform content types; declare its intended section explicitly.', + ); + } + final peerLoaders = filterLoadersForSection( + loaderConfig, + cf.section, + registry: loaderRegistry, + host: packHost, + ); + candidates = await apiReads + .forHost(packHost) + .listVersions( + cf.project.slug, + loadersJson: peerLoaders == null + ? null + : encodeFilterArray(peerLoaders), + gameVersionsJson: encodeFilterArray([mc]), + ); + } on DioException catch (e) { + if (e.response?.statusCode != 404) rethrow; + } + if (candidates.isEmpty) { + candidates = + await search.findModrinth( + slug: cf.slug, + section: cf.section, + entry: synthetic, + anchor: cf, + ) ?? + []; + if (candidates.isNotEmpty) { + promotedSlug = + (await apiReads + .forHost(packHost) + .getProject(candidates.first.projectId)) + .slug; + } + } + if (candidates.isEmpty) continue; + if (modrinthSlugs.contains(keyFor(cf.section, promotedSlug))) continue; + searchedMrVersions[LocalEntryKey(cf.section, promotedSlug)] = candidates; + identities.register( + LocalEntryKey(cf.section, promotedSlug), + host: packHost, + ); + modrinthSlugs.add(keyFor(cf.section, promotedSlug)); + promotedCfRoots.add(LocalEntryKey(cf.section, promotedSlug)); + changed = true; + manifestForResolution = _withResolutionEntry( + manifestForResolution, + cf.section, + synthetic.copyWith(slug: promotedSlug), + ); + } + return changed; + } + + await promoteCfSynthetics(); + + if (offline && merged.gitrinth.isPlatformEnabled(SourceKind.modrinth)) { + for (final cf in cfResolution.resolved.values.where((e) => e.transitive)) { + final prior = existingLock?.sectionFor(cf.section)[cf.slug]?.modrinth; + if (prior == null) continue; + modrinthSlugs.add(keyFor(cf.section, cf.slug)); + identities.register( + LocalEntryKey(cf.section, cf.slug), + host: prior.host ?? defaultBaseUrl, + ); + manifestForResolution = _withResolutionEntry( + manifestForResolution, + cf.section, + ModEntry( + slug: cf.slug, + source: ModrinthEntrySource(host: prior.host ?? defaultBaseUrl), + sources: const {SourceKind.modrinth}, + ), + ); + } + } + + for (final key in modrinthSlugs.toList()) { + final node = identities.nodeForSolverKey(key); + final slug = node.localKey; + if (!allowMissingSourcesForSlugs.contains(slug)) continue; + if (!cfResolution.resolved.containsKey(node)) continue; + final candidates = await resolver.listVersions(key); + if (candidates.isEmpty) modrinthSlugs.remove(key); + } + var resolution = await resolver.resolve( + manifestForResolution, + existingLock: lockForResolution, + overridePins: overridePins, + modrinthSlugs: modrinthSlugs, + requiredVersionIds: hashVersionIds, + ); + + final selectedCfFiles = {}; + Future refreshCfPeers() async { + if (offline) return; + var syntheticManifest = merged; + final nativeMr = + resolvedDependencyGraph( + resolution, + hostFor: (node) => entryHost(node.section, node.localKey, null), + ).reachable({ + for (final r in resolution.entries) + if (merged.sectionEntries(r.section).containsKey(r.slug)) + LocalEntryKey(r.section, r.slug), + }); + for (final section in Section.values) { + for (final e in merged.sectionEntries(section).entries) { + final prior = cfResolution.resolved[LocalEntryKey(section, e.key)]; + if (prior?.discoveredViaSearch == true) { + syntheticManifest = _withResolutionEntry( + syntheticManifest, + section, + e.value.copyWith(curseforgeSlug: prior!.project.id.toString()), + ); + } + } + } + for (final r in resolution.entries) { + if (merged.sectionEntries(r.section).containsKey(r.slug) || + !merged.gitrinth.isPlatformEnabled(SourceKind.curseforge) || + !nativeMr.contains(LocalEntryKey(r.section, r.slug))) { + continue; + } + syntheticManifest = _withResolutionEntry( + syntheticManifest, + r.section, + ModEntry( + slug: r.slug, + curseforgeSlug: cfResolution + .resolved[LocalEntryKey(r.section, r.slug)] + ?.project + .id + .toString(), + sources: const {SourceKind.curseforge}, + ), + ); + } + var peers = await resolveCurseforgeEntries( + registry: loaderRegistry, + manifest: syntheticManifest, + api: cfApi, + console: console, + selectedFiles: selectedCfFiles, + ); + final discovered = {}; + for (final missing in peers.notFound) { + if (merged.sectionEntries(missing.section).containsKey(missing.slug)) { + continue; + } + final mrPeer = resolution.entries + .where((r) => r.section == missing.section && r.slug == missing.slug) + .firstOrNull; + if (mrPeer == null || missing.reason != 'slug-not-found') continue; + final entry = ModEntry( + slug: missing.slug, + source: ModrinthEntrySource( + host: entryHost(missing.section, missing.slug, null), + ), + sources: const {SourceKind.modrinth, SourceKind.curseforge}, + ); + final found = await search.findCurseforge( + slug: missing.slug, + section: missing.section, + entry: entry, + anchors: [mrPeer.version], + ); + if (found == null) continue; + discovered.add(LocalEntryKey(missing.section, missing.slug)); + syntheticManifest = _withResolutionEntry( + syntheticManifest, + missing.section, + entry.copyWith( + curseforgeSlug: found.project.id.toString(), + sources: const {SourceKind.curseforge}, + ), + ); + } + if (discovered.isNotEmpty) { + peers = await resolveCurseforgeEntries( + registry: loaderRegistry, + manifest: syntheticManifest, + api: cfApi, + console: console, + selectedFiles: selectedCfFiles, + ); + } + cfResolution = CfResolution( + resolved: { + for (final e in peers.resolved.entries) + e.key: CfResolvedEntry( + slug: e.value.slug, + section: e.value.section, + project: e.value.project, + file: e.value.file, + candidates: e.value.candidates, + requiredBy: e.value.requiredBy, + transitive: e.value.transitive, + discoveredViaSearch: + discovered.contains(e.key) || + (cfResolution.resolved[e.key]?.discoveredViaSearch ?? false), + ), + }, + notFound: cfResolution.notFound, + ); + } + + await refreshCfPeers(); + + // Different slugs are one identity only when an actual SHA1 pair proves + // it. Re-key CF to Modrinth before the shared hash/solver convergence loop. + final reportedSyntheticPairs = {}; + void mergeSyntheticIdentities() { + final cfEntries = Map.from( + cfResolution.resolved, + ); + final renamed = {}; + for (final r in resolution.entries) { + if (merged.sectionEntries(r.section).containsKey(r.slug) || + cfEntries.containsKey(LocalEntryKey(r.section, r.slug))) { + continue; + } + for (final cf in cfEntries.values.toList()) { + if (merged.sectionEntries(cf.section).containsKey(cf.slug) || + resolution.entries.any( + (e) => e.section == cf.section && e.slug == cf.slug, + )) { + continue; + } + final outcome = scanForMatchingPair( + mrCandidates: + versionsPerSlug[LocalEntryKey(r.section, r.slug)] ?? [r.version], + cfCandidates: cf.candidates, + hashScanDepth: 10, + allowHashMismatch: false, + ); + if (outcome is! HashMatched) { + if (r.section == cf.section && + reportedSyntheticPairs.add('${r.slug}/${cf.slug}')) { + console.message( + "Transitive dependencies '${r.slug}' (Modrinth) and '${cf.slug}' (CurseForge) have no hash match within hash_scan_depth. If they are the same project, declare a top-level entry with modrinth: ${r.slug} and curseforge: ${cf.project.slug}; otherwise they remain separate.", + ); + } + continue; + } + if (r.section != cf.section) { + throw ValidationError( + 'Hash-identical transitive dependencies ${r.slug} and ${cf.slug} resolve to different content types; declare a top-level entry in its intended section.', + ); + } + cfEntries.remove(LocalEntryKey(cf.section, cf.slug)); + renamed[LocalEntryKey(cf.section, cf.slug)] = r.slug; + cfEntries[LocalEntryKey(r.section, r.slug)] = CfResolvedEntry( + slug: r.slug, + section: cf.section, + project: cf.project, + file: cf.file, + candidates: cf.candidates, + transitive: true, + requiredBy: cf.requiredBy, + ); + break; + } + } + cfResolution = CfResolution( + resolved: { + for (final e in cfEntries.entries) + e.key: CfResolvedEntry( + slug: e.value.slug, + section: e.value.section, + project: e.value.project, + file: e.value.file, + candidates: e.value.candidates, + discoveredViaSearch: e.value.discoveredViaSearch, + transitive: e.value.transitive, + requiredBy: { + for (final parent in e.value.requiredBy) + renamed[LocalEntryKey(e.value.section, parent)] ?? parent, + }.toList()..sort(), + ), + }, + notFound: cfResolution.notFound, + ); + } + + if (!offline) mergeSyntheticIdentities(); + + // Slug fallback requires a resolved peer as the hash anchor. + if (!offline) { + final resolvedCf = Map.from( + cfResolution.resolved, + ); + final remaining = []; + for (final missing in cfResolution.notFound) { + final entry = manifestForResolution.sectionEntries( + missing.section, + )[missing.slug]; + final mrEntries = resolution.entries + .where((r) => r.section == missing.section && r.slug == missing.slug) + .toList(); + CfResolvedEntry? found; + if (missing.reason == 'slug-not-found' && + entry != null && + mrEntries.isNotEmpty) { + found = await search.findCurseforge( + slug: missing.slug, + section: missing.section, + entry: entry, + anchors: [ + mrEntries.single.version, + if (!overridePins.containsKey( + keyFor(missing.section, missing.slug), + )) + ...eligibleModrinthVersions( + versionsPerSlug[LocalEntryKey(missing.section, missing.slug)] ?? + [], + entry, + ).where((v) => v.id != mrEntries.single.version.id), + ], + ); + } + if (found == null) { + remaining.add(missing); + } else { + resolvedCf[LocalEntryKey(missing.section, missing.slug)] = found; + } + } + cfResolution = CfResolution(resolved: resolvedCf, notFound: remaining); + } + + final mrNotFoundSlugs = {}; + for (final key in modrinthSlugs) { + final node = identities.nodeForSolverKey(key); + final slug = node.localKey; + final section = node.section; + if (resolution.entries.any((r) => r.section == section && r.slug == slug)) { + continue; + } + if (overridePins.containsKey(key)) continue; + final entry = merged.sectionEntries(section)[slug]; + if (entry == null) continue; + if (isAnyGitrinthMarker(entry.constraintRaw)) continue; + final mrCandidates = versionsPerSlug[LocalEntryKey(section, slug)]; + if (mrCandidates == null || mrCandidates.isEmpty) { + mrNotFoundSlugs.add(node); + } + } + final missingMessage = formatCrossPlatformMissingError( + registry: loaderRegistry, + manifest: merged, + mrNotFoundSlugs: mrNotFoundSlugs, + cfResolution: cfResolution, + allowedMissingSlugs: { + for (final section in Section.values) + for (final slug in allowMissingSourcesForSlugs) + if (resolution.entries.any( + (r) => r.section == section && r.slug == slug, + ) || + cfResolution.resolved.containsKey(LocalEntryKey(section, slug))) + LocalEntryKey(section, slug), + }, + ); + if (missingMessage != null) { + throw ValidationError( + [missingMessage, ...search.diagnostics.values].join('\n'), + ); + } + + // Hash-scan dual-source entries. For each slug with both Modrinth + // and CurseForge resolution, compare SHA1s and optionally scan + // older pairs per `hash_scan_depth`. Skipped under --offline: the + // synthesized CF resolution carries a single locked candidate per + // slug, so a scan would either be a no-op or falsely report + // HashSingleSided. + final hashOutcomes = {}; + + if (!offline) { + var passes = 0; + while (true) { + if (++passes > 100) { + throw const ValidationError( + 'Cross-platform hash selection did not converge; pin peer versions explicitly.', + ); + } + await refreshCfPeers(); + mergeSyntheticIdentities(); + if (await promoteCfSynthetics()) { + resolution = await resolver.resolve( + manifestForResolution, + existingLock: lockForResolution, + overridePins: overridePins, + modrinthSlugs: modrinthSlugs, + requiredVersionIds: hashVersionIds, + ); + continue; + } + mergeSyntheticIdentities(); + hashOutcomes.clear(); + var needsResolve = false; + for (final r in resolution.entries) { + var cf = cfResolution.resolved[LocalEntryKey(r.section, r.slug)]; + if (cf == null) continue; + if (r.section != cf.section) { + throw ValidationError( + 'Transitive ${r.slug} resolves to different content types across platforms; declare a top-level entry in its intended section.', + ); + } + final entry = manifestForResolution.sectionEntries(r.section)[r.slug]; + final depth = entry?.hashScanDepth ?? 10; + final allow = entry?.allowHashMismatch ?? false; + final allowed = eligibleModrinthVersions( + versionsPerSlug[LocalEntryKey(r.section, r.slug)] ?? [r.version], + entry, + ); + final mrCandidates = [ + r.version, + if (!overridePins.containsKey(keyFor(r.section, r.slug))) + ...allowed.where((v) => v.id != r.version.id), + ]; + var outcome = scanForMatchingPair( + mrCandidates: mrCandidates, + cfCandidates: cf.candidates, + hashScanDepth: depth, + allowHashMismatch: allow, + ); + if (outcome is HashExhausted && entry != null) { + final foundCf = await search.findCurseforge( + slug: r.slug, + section: r.section, + entry: entry, + anchors: mrCandidates, + excludeProject: cf.project.id, + ); + if (foundCf != null) { + cf = foundCf; + cfResolution = CfResolution( + resolved: { + ...cfResolution.resolved, + LocalEntryKey(r.section, r.slug): cf, + }, + notFound: cfResolution.notFound, + ); + } else if (!overridePins.containsKey(keyFor(r.section, r.slug))) { + final foundMr = await search.findModrinth( + slug: r.slug, + section: r.section, + entry: entry, + anchor: cf, + excludeProject: r.version.projectId, + ); + if (foundMr != null) { + searchedMrVersions[LocalEntryKey(r.section, r.slug)] = foundMr; + } + } + outcome = scanForMatchingPair( + mrCandidates: + searchedMrVersions[LocalEntryKey(r.section, r.slug)] ?? + mrCandidates, + cfCandidates: cf.candidates, + hashScanDepth: depth, + allowHashMismatch: allow, + ); + } + final matchedMr = switch (outcome) { + HashMatched(:final modrinthVersion) => modrinthVersion, + HashAcceptedMismatch(:final modrinthVersion) => modrinthVersion, + _ => null, + }; + final matchedCf = switch (outcome) { + HashMatched(:final curseforgeFile) => curseforgeFile, + HashAcceptedMismatch(:final curseforgeFile) => curseforgeFile, + _ => null, + }; + if (matchedCf != null) { + selectedCfFiles[LocalEntryKey(cf.section, cf.slug)] = matchedCf.id; + } + if (matchedMr != null && matchedMr.id != r.version.id) { + hashVersionIds[keyFor(r.section, r.slug)] = matchedMr.id; + needsResolve = true; + } + hashOutcomes[LocalEntryKey(r.section, r.slug)] = outcome; + if (outcome is HashAcceptedMismatch) { + console.warn( + '${r.slug}: SHA1 mismatch between Modrinth (${outcome.modrinthVersion.versionNumber}) ' + 'and CurseForge (file ${outcome.curseforgeFile.fileName}); ' + 'accepting because allow_hash_mismatch: true.', + ); + } else if (outcome is HashExhausted) { + throw ValidationError( + '${r.slug}: SHA1 mismatch between Modrinth and CurseForge.\n' + ' Modrinth (project ${outcome.newestModrinth.projectId}, version ' + '${outcome.newestModrinth.versionNumber}): ' + '${outcome.newestModrinth.primaryFile?.hashes['sha1']}\n' + ' CurseForge (project ${cf.project.id}, file ' + '${outcome.newestCurseforge.id}): ' + '${outcome.newestCurseforge.sha1Hash}\n' + ' Scanned ${outcome.depthExamined} older versions on both ' + 'platforms; no match found.\n' + 'Resolution options:\n' + ' - Set `allow_hash_mismatch: true` on this entry to accept ' + 'the divergence\n' + ' - Restrict to a single platform: `sources: [modrinth]` or ' + '`sources: [curseforge]`\n' + ' - Pin per-platform versions explicitly in mods.yaml\n' + '${search.diagnostics[LocalEntryKey(r.section, r.slug)] ?? ''}', + ); + } + } + // Rewalk selected CF files on the next pass before accepting a graph. + if (cfResolution.resolved.values.any( + (cf) => + selectedCfFiles.containsKey(LocalEntryKey(cf.section, cf.slug)) && + selectedCfFiles[LocalEntryKey(cf.section, cf.slug)] != cf.file.id, + )) { + needsResolve = true; + } + if (!needsResolve) break; + resolution = await resolver.resolve( + manifestForResolution, + existingLock: lockForResolution, + overridePins: overridePins, + modrinthSlugs: modrinthSlugs, + requiredVersionIds: hashVersionIds, + ); + } + } + + if (offline && existingLock != null) { + var graphManifest = merged; + if (merged.gitrinth.isPlatformEnabled(SourceKind.curseforge)) { + for (final r in resolution.entries) { + if (merged.sectionEntries(r.section).containsKey(r.slug)) continue; + graphManifest = _withResolutionEntry( + graphManifest, + r.section, + ModEntry(slug: r.slug, sources: const {SourceKind.curseforge}), + ); + } + } + cfResolution = synthesizeCfResolutionFromLock( + existingLock, + cache: cache, + registry: loaderRegistry, + manifest: graphManifest, + defaultModrinthHost: defaultBaseUrl, + ); + } + + final finalCfFiles = selectedCurseforgeFiles(cfResolution, hashOutcomes); + validateCurseforgeRelationships( + finalCfFiles, + overriddenSlugs: overriddenSlugs, + ); + + final newLock = buildLock( + defaultHost: defaultBaseUrl, + hostFor: (section, slug) => offline + ? existingLock?.sectionFor(section)[slug]?.modrinth?.host + : (entryHost(section, slug, null) == defaultBaseUrl + ? null + : entryHost(section, slug, null)), + manifest: merged, + resolution: resolution, + cfResolution: cfResolution, + hashOutcomes: hashOutcomes, + modrinthSearchSlugs: offline + ? { + for (final section in Section.values) + for (final e + in existingLock?.sectionFor(section).entries ?? + >[]) + if (e.value.modrinth?.discoveredViaSearch ?? false) + LocalEntryKey(section, e.key), + } + : search.modrinthSearchSlugs, + resolvedModLoaderVersion: resolvedModLoaderVersion, + resolvedPluginLoaderVersion: resolvedPluginLoaderVersion, + ); + final diff = diffLocks(existingLock, newLock); + + reporter.printSimpleDiff(diff); + + if (enforce && diff.isNotEmpty) { + throw ValidationError( + 'mods.lock is out of date (--enforce-lockfile). ' + '${diff.length} change(s) would be applied.', + ); + } + + if (dryRun) { + reporter.printSimpleDiff(diff, force: true); + return ResolveSyncResult( + newLock: newLock, + diff: diff, + changeCount: diff.where((d) => d.kind != DiffKind.unchanged).length, + outdated: countOutdated(newLock, versionsPerSlug), + exitCode: diff.isNotEmpty ? exitValidationError : exitOk, + ); + } + + final newLockText = emitModsLock(newLock); + if (!offline) { + final metadata = CurseForgeMetadataStore(cache); + for (final file in finalCfFiles.values) { + metadata.write(file); + } + } + io.writeModsLock(newLockText); + + reporter.printReport( + newLock: newLock, + diff: diff, + versionsPerSlug: versionsPerSlug, + overriddenSlugs: overriddenSlugs, + ); + + // Persist each resolved Modrinth version's dependency list to the + // artifact cache, sibling to the .jar. Mirrors dart pub's + // "graph-in-cache" architecture: `mods.lock` no longer carries + // forward edges; future `gitrinth upgrade --unlock-transitive` + // recomputes the closure from these JSON files. Failure here warns + // but doesn't abort — the artifact download is the contract. + persistVersionMetadata( + resolution, + cache, + console, + (r) => entryHost(r.section, r.slug, null), + ); + + final fetchResult = await downloadArtifacts( + lock: newLock, + cache: cache, + downloader: downloader, + console: console, + io: io, + defaultModrinthHost: defaultBaseUrl, + ); + final downloaded = fetchResult.downloaded; + final hits = fetchResult.hits; + final fetchErrors = fetchResult.errors; + // Verify `files:` source paths exist; mirrors the `path:` mod-entry + // existence check above. Build-time resolution would also catch + // this, but failing fast at `get` time matches user expectations. + for (final entry in newLock.files.values) { + final resolved = p.isAbsolute(entry.sourcePath) + ? entry.sourcePath + : p.normalize(p.join(io.directory.path, entry.sourcePath)); + if (FileSystemEntity.typeSync(resolved) == FileSystemEntityType.notFound) { + fetchErrors.add( + 'files: entry "${entry.destination}" points to a missing source: ' + '${entry.sourcePath} (looked in $resolved)', + ); + } + } + if (fetchErrors.isNotEmpty) { + throw ValidationError( + fetchErrors.length == 1 + ? fetchErrors.first + : 'failed to fetch ${fetchErrors.length} dependencies:\n' + '${fetchErrors.map((e) => ' - $e').join('\n')}', + ); + } + + final changeCount = diff.where((d) => d.kind != DiffKind.unchanged).length; + final outdated = countOutdated(newLock, versionsPerSlug); + console.io( + 'Locked $changeCount change(s); $downloaded downloaded, $hits cache hit(s).', + ); + + final markerCount = _countNotFoundMarkers(merged); + if (markerCount > 0) { + console.message( + '$markerCount ${markerCount == 1 ? 'entry' : 'entries'} marked ' + '$notFoundMarker — run `gitrinth migrate ` to retry.', + ); + } + final disabledCount = _countDisabledByConflictMarkers(merged); + if (disabledCount > 0) { + console.message( + '$disabledCount ${disabledCount == 1 ? 'entry' : 'entries'} marked ' + '$disabledByConflictMarker — run `gitrinth migrate ` or ' + '`gitrinth upgrade --major-versions` to retry.', + ); + } + + return ResolveSyncResult( + newLock: newLock, + diff: diff, + changeCount: changeCount, + outdated: outdated, + exitCode: exitOk, + ); +} + +/// Rebuilds a [CfResolution] from the CurseForge source blocks already +/// recorded in [lock]. Used by the offline path so [buildLock] re-emits +/// every prior `curseforge:` block byte-for-byte without a network walk. +/// +/// The synthesized [CfResolvedEntry.project] / [CfResolvedEntry.file] +/// only populate the fields the lock builder actually reads +/// (`project.id`, `project.slug`, `project.allowModDistribution`, +/// `file.id`, `file.fileName`, `file.downloadUrl`, `file.sha1Hash`, +/// `file.gameVersions`); other fields carry placeholder values. +/// When [cache] is supplied, selected-file sidecars are mandatory and provide +/// validated availability and relations. Omitting it supports read-only lock +/// inspection; actual offline resolution always supplies the cache. +/// `candidates` holds the single locked file — the offline path skips +/// the hash-scan loop, so the list is never walked beyond `.first`. +CfResolution synthesizeCfResolutionFromLock( + ModsLock lock, { + GitrinthCache? cache, + LoaderRegistry registry = LoaderRegistry.builtins, + ModsYaml? manifest, + String defaultModrinthHost = defaultModrinthBaseUrl, +}) { + final resolved = {}; + final retained = {}; + final lockedNodes = { + for (final section in Section.values) + for (final slug in lock.sectionFor(section).keys) + LocalEntryKey(section, slug), + }; + LocalEntryKey? parentNode(LocalEntryKey child, String name) { + final childEntry = lock.sectionFor(child.section)[child.localKey]!; + final candidates = lockedNodes + .where( + (node) => + node.localKey == name && + canRequireLockedSource( + lock.sectionFor(node.section)[node.localKey]!, + childEntry, + defaultModrinthHost: defaultModrinthHost, + ), + ) + .toList(); + if (candidates.length <= 1) return candidates.firstOrNull; + throw ValidationError( + "Ambiguous required_by parent '$name' for $child while offline. Run `gitrinth get` online first.", + ); + } + + if (manifest != null) { + for (final section in Section.values) { + for (final e in manifest.sectionEntries(section).entries) { + if (e.value.source is ModrinthEntrySource && + !isAnyGitrinthMarker(e.value.constraintRaw) && + effectiveSources( + manifest, + e.value, + section, + registry: registry, + ).contains(SourceKind.curseforge)) { + retained.add(LocalEntryKey(section, e.key)); + } + } + } + var changed = true; + while (changed) { + changed = false; + for (final section in Section.values) { + for (final e in lock.sectionFor(section).entries) { + if (e.value.dependency != LockedDependencyKind.transitive || + e.value.curseforge == null) { + continue; + } + final node = LocalEntryKey(section, e.key); + if (e.value.requiredBy.any( + (name) => retained.contains(parentNode(node, name)), + ) && + retained.add(node)) { + changed = true; + } + } + } + } + } + for (final section in Section.values) { + for (final entry in lock.sectionFor(section).entries) { + final slug = entry.key; + if (manifest != null) { + final declared = manifest.sectionEntries(section)[slug]; + if (!retained.contains(LocalEntryKey(section, slug)) || + (declared != null && + (declared.source is! ModrinthEntrySource || + isAnyGitrinthMarker(declared.constraintRaw) || + !effectiveSources( + manifest, + declared, + section, + registry: registry, + ).contains(SourceKind.curseforge)))) { + continue; + } + } + final cf = entry.value.curseforge; + if (cf == null) continue; + final sha1 = cf.file.sha1; + final metadata = cache == null + ? null + : CurseForgeMetadataStore(cache).read( + projectId: cf.projectId, + fileId: cf.fileId, + expectedSha1: sha1, + ); + final synthFile = ModFile( + id: cf.fileId, + modId: cf.projectId, + displayName: entry.value.modrinth == null + ? entry.value.version ?? cf.file.name + : cf.file.name, + fileName: cf.file.name, + releaseType: 1, + fileDate: '', + gameVersions: List.from(cf.gameVersions), + hashes: sha1 == null ? const [] : [FileHash(value: sha1, algoCode: 1)], + dependencies: metadata?.dependencies ?? const [], + isAvailable: metadata?.isAvailable ?? true, + downloadUrl: cf.file.url, + ); + final synthMod = Mod( + id: cf.projectId, + gameId: kCurseForgeGameIdMinecraft, + name: cf.slug ?? slug, + slug: cf.slug ?? slug, + classId: 0, + latestFiles: [synthFile], + allowModDistribution: cf.allowModDistribution, + ); + resolved[LocalEntryKey(section, slug)] = CfResolvedEntry( + slug: slug, + section: section, + project: synthMod, + file: synthFile, + candidates: [synthFile], + discoveredViaSearch: cf.discoveredViaSearch, + requiredBy: entry.value.requiredBy + .where( + (p) => + manifest == null || + retained.contains( + parentNode(LocalEntryKey(section, slug), p), + ), + ) + .toList(), + transitive: entry.value.dependency == LockedDependencyKind.transitive, + ); + } + } + return CfResolution(resolved: resolved, notFound: const []); +} + +int _countNotFoundMarkers(ModsYaml manifest) { + var count = 0; + for (final section in Section.values) { + for (final entry in manifest.sectionEntries(section).values) { + if (isNotFoundMarker(entry.constraintRaw)) count++; + } + } + return count; +} + +int _countDisabledByConflictMarkers(ModsYaml manifest) { + var count = 0; + for (final section in Section.values) { + for (final entry in manifest.sectionEntries(section).values) { + if (isDisabledByConflictMarker(entry.constraintRaw)) count++; + } + } + return count; +} + +void _checkUserEntriesPresentInLock(ModsYaml manifest, ModsLock? lock) { + if (lock == null) { + throw const ValidationError('mods.lock is missing (--enforce-lockfile).'); + } + final missing = []; + for (final section in Section.values) { + final entries = manifest.sectionEntries(section); + final lockSection = lock.sectionFor(section); + for (final entry in entries.entries) { + // Marker entries are absent from the lock by design. + if (isAnyGitrinthMarker(entry.value.constraintRaw)) continue; + if (!lockSection.containsKey(entry.key)) missing.add(entry.key); + } + } + if (missing.isNotEmpty) { + throw ValidationError( + 'mods.lock is missing user-declared entries (--enforce-lockfile): ' + '${missing.join(', ')}', + ); + } +} + +/// Hits `/v2/tag/game_version` and throws [ValidationError] if [mcVersion] +/// is not in the list. Called only on first lock or when the user changed +/// `mc-version` in `mods.yaml`. +Future _validateGameVersion({ + required ModrinthApi api, + required String mcVersion, +}) async { + final List known; + try { + final list = await api.getGameVersions(); + known = list.map((v) => v.version).toList(); + } on Object catch (e) { + final err = (e is DioException) ? e.error : e; + if (err is GitrinthException) throw err; + throw UserError( + 'failed to fetch Minecraft game versions from Modrinth: $e', + ); + } + if (!known.contains(mcVersion)) { + throw ValidationError( + 'mc-version "$mcVersion" is not a known Minecraft release on ' + 'Modrinth. Pick one from ' + 'https://api.modrinth.com/v2/tag/game_version (e.g. ' + '${known.take(3).join(", ")}).', + ); + } +} + +/// Returns the concrete loader version to bake into the lock. +/// +/// Skips re-resolution when the user typed a concrete tag (anything other +/// than `stable`/`latest`) and the existing lock already records that +/// same loader+version pair. `stable`/`latest` always re-resolve. +/// +/// Concrete tags hit the network for upstream validation on first use; +/// under `--offline` the validation is skipped (we trust the user's pin). +/// +/// Caller is expected to gate on [LoaderConfig.hasModRuntime] and pass +/// the populated [tag]; vanilla packs skip this resolution entirely. +Future _resolveModLoaderVersion({ + required ModLoaderVersionResolver modLoaderResolver, + required LoaderRegistry registry, + required ModLoader modLoader, + required String tag, + required String mcVersion, + required ModsLock? existingLock, + required bool offline, + required Console console, +}) async { + final lockedSameLoader = existingLock?.loader.mods == modLoader; + final lockedVersion = existingLock?.loader.modLoaderVersion; + final tagIsConcrete = tag != 'stable' && tag != 'latest'; + if (tagIsConcrete && lockedSameLoader && lockedVersion == tag) { + return tag; + } + if (tagIsConcrete && offline) { + if (lockedSameLoader && lockedVersion != null && lockedVersion != tag) { + console.warn( + 'using unvalidated loader pin `${modLoader.name}:' + '$tag` while offline (mods.lock had `$lockedVersion`).', + ); + } + return tag; + } + if (!tagIsConcrete && offline) { + if (lockedSameLoader && lockedVersion != null) { + return lockedVersion; + } + throw UserError( + 'cannot resolve loader tag "$tag" while offline: no concrete ' + 'version recorded in mods.lock. Try again without --offline, or ' + 'pin a concrete tag like `${modLoader.name}:` in ' + 'mods.yaml.', + ); + } + return modLoaderResolver.resolve( + loader: modLoader, + registry: registry, + tag: tag, + mcVersion: mcVersion, + ); +} + +Future _resolvePluginLoaderVersion({ + required PluginLoaderVersionResolver pluginLoaderResolver, + required LoaderRegistry registry, + required PluginLoader pluginLoader, + required String tag, + required String mcVersion, + required ModsLock? existingLock, + required bool offline, + required Console console, +}) async { + final lockedSameLoader = existingLock?.loader.plugins == pluginLoader; + final lockedVersion = existingLock?.loader.pluginLoaderVersion; + final tagIsConcrete = tag != 'stable' && tag != 'latest'; + if (tagIsConcrete && lockedSameLoader && lockedVersion == tag) { + return tag; + } + if (tagIsConcrete && offline) { + if (lockedSameLoader && lockedVersion != null && lockedVersion != tag) { + console.warn( + 'using unvalidated plugin loader pin `${pluginLoader.name}:' + '$tag` while offline (mods.lock had `$lockedVersion`).', + ); + } + return tag; + } + if (!tagIsConcrete && offline) { + if (lockedSameLoader && lockedVersion != null) { + return lockedVersion; + } + throw UserError( + 'cannot resolve plugin loader tag "$tag" while offline: no concrete ' + 'version recorded in mods.lock. Try again without --offline, or pin ' + 'a concrete tag like `${pluginLoader.name}:` in mods.yaml.', + ); + } + return pluginLoaderResolver.resolve( + loader: pluginLoader, + registry: registry, + tag: tag, + mcVersion: mcVersion, + ); +} + +/// Returns [existingLock] with each section's map filtered to drop any slug +/// in [freshSlugs]. Used by `gitrinth upgrade` to force the resolver to pick +/// a new version for the named entries instead of preserving the soft pin. +/// When [freshSlugs] is empty (the `get` path), returns [existingLock] +/// unchanged. +ModsLock? _stripLockPins(ModsLock? existingLock, Set freshSlugs) { + if (existingLock == null || freshSlugs.isEmpty) return existingLock; + Map strip(Map m) => { + for (final e in m.entries) + if (!freshSlugs.contains(e.key)) e.key: e.value, + }; + return existingLock.copyWith( + mods: strip(existingLock.mods), + resourcePacks: strip(existingLock.resourcePacks), + dataPacks: strip(existingLock.dataPacks), + shaders: strip(existingLock.shaders), + plugins: strip(existingLock.plugins), + ); +} + +ModsYaml _withResolutionEntry( + ModsYaml manifest, + Section section, + ModEntry entry, +) { + final entries = {...manifest.sectionEntries(section), entry.slug: entry}; + return manifest.withSectionEntries(section, entries); +} + +ModsYaml _withoutResolutionEntry( + ModsYaml manifest, + Section section, + String slug, +) { + final entries = {...manifest.sectionEntries(section)}..remove(slug); + return manifest.withSectionEntries(section, entries); +} + +/// Returns [merged] with each section entry whose slug is in [relaxConstraints] +/// rewritten to have `constraintRaw: null` (parsed as `VersionConstraint.any`). +/// Channel/env/optional/source are preserved. Used by +/// `gitrinth upgrade --major-versions` to bypass caret ceilings. +ModsYaml _relaxManifestConstraints(ModsYaml merged, Set relax) { + if (relax.isEmpty) return merged; + Map relaxSection(Map m) => { + for (final e in m.entries) + e.key: relax.contains(e.key) + ? e.value.copyWith(constraintRaw: null) + : e.value, + }; + return merged.mapSectionEntries((_, entries) => relaxSection(entries)); +} diff --git a/lib/src/service/section_inference.dart b/lib/src/service/resolve/section_inference.dart similarity index 98% rename from lib/src/service/section_inference.dart rename to lib/src/service/resolve/section_inference.dart index 1a592a3..56e4d39 100644 --- a/lib/src/service/section_inference.dart +++ b/lib/src/service/resolve/section_inference.dart @@ -1,8 +1,8 @@ import 'package:mime/mime.dart'; import 'package:path/path.dart' as p; -import '../cli/exceptions.dart'; -import '../model/manifest/mods_yaml.dart'; +import '../../cli/exceptions.dart'; +import '../../model/manifest/mods_yaml.dart'; /// Infers the `mods.yaml` [Section] from a Modrinth project response. /// diff --git a/lib/src/service/solve_report.dart b/lib/src/service/resolve/solve_report.dart similarity index 83% rename from lib/src/service/solve_report.dart rename to lib/src/service/resolve/solve_report.dart index 1e23124..0c2e564 100644 --- a/lib/src/service/solve_report.dart +++ b/lib/src/service/resolve/solve_report.dart @@ -1,12 +1,13 @@ import 'package:intl/intl.dart'; import 'package:pub_semver/pub_semver.dart' as semver; -import '../model/manifest/mods_lock.dart'; -import '../model/manifest/mods_yaml.dart'; -import '../model/modrinth/version.dart' as modrinth; -import '../model/resolver/constraint.dart'; -import '../model/resolver/version_selection.dart'; -import 'console.dart'; +import '../../model/manifest/mods_lock.dart'; +import '../../model/manifest/mods_yaml.dart'; +import '../../model/modrinth/version.dart' as modrinth; +import '../../model/resolver/constraint.dart'; +import '../../model/resolver/version_selection.dart'; +import '../console.dart'; +import '../source_identity.dart'; enum DiffKind { added, removed, updated, unchanged } @@ -72,18 +73,52 @@ List diffLocks(ModsLock? oldLock, ModsLock newLock) { } bool _equalLocked(LockedEntry a, LockedEntry b) { - return a.sourceKind == b.sourceKind && - a.version == b.version && - a.projectId == b.projectId && + if (a.kind != b.kind || + a.version != b.version || + a.path != b.path || + a.url != b.url || + a.client != b.client || + a.server != b.server || + a.dependency != b.dependency) { + return false; + } + if (!_equalFile(a.urlFile, b.urlFile)) return false; + if (!_equalModrinthSource(a.modrinth, b.modrinth)) return false; + if (!_equalCurseforgeSource(a.curseforge, b.curseforge)) return false; + return true; +} + +bool _equalFile(LockedFile? a, LockedFile? b) { + if (a == null && b == null) return true; + if (a == null || b == null) return false; + return a.name == b.name && + a.url == b.url && + a.sha1 == b.sha1 && + a.sha512 == b.sha512 && + a.size == b.size; +} + +bool _equalModrinthSource(LockedModrinthSource? a, LockedModrinthSource? b) { + if (a == null && b == null) return true; + if (a == null || b == null) return false; + return a.projectId == b.projectId && a.versionId == b.versionId && - a.path == b.path && - a.client == b.client && - a.server == b.server && - a.dependency == b.dependency && - a.file?.name == b.file?.name && - a.file?.url == b.file?.url && - a.file?.sha512 == b.file?.sha512 && - a.file?.size == b.file?.size; + a.versionNumber == b.versionNumber && + a.host == b.host && + _equalFile(a.file, b.file); +} + +bool _equalCurseforgeSource( + LockedCurseforgeSource? a, + LockedCurseforgeSource? b, +) { + if (a == null && b == null) return true; + if (a == null || b == null) return false; + return a.projectId == b.projectId && + a.fileId == b.fileId && + a.slug == b.slug && + a.allowModDistribution == b.allowModDistribution && + _equalFile(a.file, b.file); } /// Returns the raw `version_number` of the newest entry in [candidates] @@ -167,17 +202,17 @@ String? newerAvailableThan( /// sources are ignored — there's no "latest" concept for them. int countOutdated( ModsLock newLock, - Map> versionsPerSlug, + Map> versionsPerSlug, ) { var n = 0; for (final section in Section.values) { final sectionMap = newLock.sectionFor(section); for (final entry in sectionMap.entries) { final locked = entry.value; - if (locked.sourceKind != LockedSourceKind.modrinth) continue; + if (locked.modrinth == null) continue; final nr = newerAvailableThan( locked.version ?? '', - versionsPerSlug[entry.key], + versionsPerSlug[LocalEntryKey(section, entry.key)], ); if (nr != null) n++; } @@ -209,7 +244,7 @@ String? formatReportLine({ if (kind == DiffKind.updated && diff != null) { final before = diff.before; final after = diff.after; - final sourceChanged = before?.sourceKind != after?.sourceKind; + final sourceChanged = before?.kind != after?.kind; if (sourceChanged) { icon = '* '; } else { @@ -245,20 +280,17 @@ String? formatReportLine({ if (icon == null) return null; final buf = StringBuffer(icon)..write(locked.slug); - switch (locked.sourceKind) { - case LockedSourceKind.modrinth: + switch (locked.kind) { + case LockedEntryKind.resolved: if (locked.version != null) { buf.write(' '); buf.write(locked.version); } - break; - case LockedSourceKind.url: - final url = locked.file?.url; + case LockedEntryKind.url: + final url = locked.urlFile?.url ?? locked.url; buf.write(' from url ${url ?? ''}'.trimRight()); - break; - case LockedSourceKind.path: + case LockedEntryKind.path: buf.write(' from path ${locked.path}'); - break; } if (wasVersion != null) { buf.write(' (was $wasVersion)'); @@ -290,7 +322,7 @@ class SolveReporter { void printReport({ required ModsLock newLock, required List diff, - required Map> versionsPerSlug, + required Map> versionsPerSlug, required Set overriddenSlugs, }) { final diffByKey = { @@ -303,8 +335,11 @@ class SolveReporter { for (final entry in sectionMap.entries) { final slug = entry.key; final locked = entry.value; - final newerRaw = locked.sourceKind == LockedSourceKind.modrinth - ? newerAvailableThan(locked.version ?? '', versionsPerSlug[slug]) + final newerRaw = locked.modrinth != null + ? newerAvailableThan( + locked.version ?? '', + versionsPerSlug[LocalEntryKey(section, slug)], + ) : null; final isOverridden = overriddenSlugs.contains(slug); final d = diffByKey['${section.name}/$slug']; diff --git a/lib/src/service/resolve_and_sync.dart b/lib/src/service/resolve_and_sync.dart deleted file mode 100644 index 9957032..0000000 --- a/lib/src/service/resolve_and_sync.dart +++ /dev/null @@ -1,740 +0,0 @@ -library; - -import 'dart:convert'; -import 'dart:io'; - -import 'package:dio/dio.dart'; -import 'package:path/path.dart' as p; - -import '../cli/exceptions.dart'; -import '../cli/exit_codes.dart'; -import '../model/manifest/emitter.dart'; -import '../model/manifest/mods_lock.dart'; -import '../model/manifest/mods_yaml.dart'; -import '../model/manifest/overrides_merger.dart'; -import '../model/modrinth/project.dart'; -import '../model/modrinth/version.dart' as modrinth; -import '../model/resolver/constraint.dart'; -import '../model/resolver/pubgrub.dart'; -import '../model/resolver/resolver.dart'; -import '../model/resolver/result.dart'; -import '../model/resolver/version_selection.dart'; -import '../version.dart'; -import '../commands/version_picker.dart'; -import 'cache.dart'; -import 'console.dart'; -import 'downloader.dart'; -import 'mod_loader_version_resolver.dart'; -import 'manifest_io.dart'; -import 'modrinth_api.dart'; -import 'plugin_loader_version_resolver.dart'; -import 'section_inference.dart'; -import 'solve_report.dart'; - -part 'lock_builder.dart'; - -/// Returns true when [v] is admissible under both filters used during -/// resolve: the caller's loader filter (`null` means "any loader is OK" -/// — used for resource_packs / data_packs) and the game-version set. -bool _matchesLoaderAndMc( - modrinth.Version v, - List? loaderFilter, - List gameVersions, -) { - final loaderOk = loaderFilter == null || v.loaders.any(loaderFilter.contains); - final mcOk = v.gameVersions.any(gameVersions.contains); - return loaderOk && mcOk; -} - -/// Result of [resolveAndSync]: exit code plus enough detail for the caller -/// to print its own `Got dependencies!` / `Changed N…` summary. -class ResolveSyncResult { - final ModsLock? newLock; - final List diff; - final int changeCount; - final int outdated; - final int exitCode; - - const ResolveSyncResult({ - required this.newLock, - required this.diff, - required this.changeCount, - required this.outdated, - required this.exitCode, - }); -} - -/// Re-resolves `mods.yaml`, writes `mods.lock`, and downloads/verifies every -/// artifact. Shared between `gitrinth get` and `gitrinth add` so both commands -/// produce identical `+ slug version` / `Changed N dependencies!` output. -/// -/// When [dryRun] is true, the lockfile and cache are not written; the result -/// carries [exitValidationError] if the lock would have changed, otherwise -/// [exitOk]. When [enforce] is true, any lock change throws a -/// [ValidationError] (same semantics as `gitrinth get --enforce-lockfile`). -/// -/// [freshSlugs] holds slugs whose `mods.lock` entries should NOT be passed to -/// the resolver as soft pins, so the resolver picks newest-within-constraint -/// instead of preserving the existing lock. [relaxConstraints] holds slugs -/// whose version constraint should be treated as `any` for resolution. Both -/// power `gitrinth upgrade`; `get` leaves them empty. The diff and the -/// rebuilt lock still come from the *original* manifest and lock, so per-entry -/// metadata (env/optional/channel/etc.) is preserved. -/// -/// [manifestOverride] replaces the on-disk `mods.yaml` for resolution. -/// `gitrinth migrate` uses this to resolve against a mutated target without -/// first writing `mods.yaml`. -Future resolveAndSync({ - required ManifestIo io, - required Console console, - required ModrinthApi api, - required GitrinthCache cache, - required Downloader downloader, - required ModLoaderVersionResolver modLoaderResolver, - required PluginLoaderVersionResolver pluginLoaderResolver, - bool offline = false, - bool dryRun = false, - bool enforce = false, - Set freshSlugs = const {}, - Set relaxConstraints = const {}, - ModsYaml? manifestOverride, - SolveType solveType = SolveType.get, -}) async { - final reporter = SolveReporter(console); - - final manifest = manifestOverride ?? io.readModsYaml(); - final projectOverrides = io.readProjectOverrides(); - final slugCache = {}; - // The slug → project_type lookup feeds two things: section - // inference for purely-transitive overrides (here, in - // applyOverrides) and reverse lookup of transitive dep project_ids - // (later, in the Resolver). Cache project lookups so we don't - // double-fetch the same slug for both purposes when the network - // is reachable. - final projectCache = {}; - Future getProjectCached(String slug) async { - final cached = projectCache[slug]; - if (cached != null) return cached; - final p = await api.getProject(slug); - projectCache[slug] = p; - return p; - } - - final mergedResult = await applyOverrides( - manifest, - projectOverrides, - inferSectionForTransitive: (slug) async { - if (offline) { - // Without network, we can't infer the project type. Fall back - // to mods — the override author can write a `type:` hint or - // declare the slug under the right section explicitly to - // avoid this fallback. - return Section.mods; - } - try { - final proj = await getProjectCached(slug); - return inferSectionFromProject( - projectType: proj.projectType, - loaders: proj.loaders, - ); - } on DioException catch (e) { - final err = e.error; - if (err is GitrinthException) throw err; - throw UserError( - 'project_overrides: failed to look up Modrinth project ' - '"$slug" to infer its section: ${e.message}', - ); - } - }, - ); - final merged = mergedResult.manifest; - final overriddenSlugs = mergedResult.overriddenSlugs; - final existingLock = io.readModsLock(); - - if (enforce) { - _checkUserEntriesPresentInLock(merged, existingLock); - } - - final lockForResolution = _stripLockPins(existingLock, freshSlugs); - final manifestForResolution = _relaxManifestConstraints( - merged, - relaxConstraints, - ); - - final loaderConfig = merged.loader; - final mc = merged.mcVersion; - - // One-shot mc-version validation: only call /tag/game_version when the - // mc-version differs from what mods.lock already records. The pairing - // was already validated when it was first locked, so a stable lock is - // proof enough. Skipped under --offline since we can't reach the tag - // endpoint; trust mods.yaml. - if (existingLock?.mcVersion != mc && !offline) { - await _validateGameVersion(api: api, mcVersion: mc); - } - - // Loader-tag resolution. Concrete tags pass through unchanged with no - // network call; `stable` / `latest` always re-resolve (those tags exist - // precisely to drift). For concrete tags that already match the lock, - // skip even the passthrough — the lock is the cache. Under --offline, - // `stable`/`latest` falls back to the lock's concrete version. - final String? resolvedModLoaderVersion; - final modLoaderVersion = loaderConfig.modLoaderVersion; - if (loaderConfig.hasModRuntime && modLoaderVersion != null) { - resolvedModLoaderVersion = await _resolveModLoaderVersion( - modLoaderResolver: modLoaderResolver, - modLoader: loaderConfig.mods, - tag: modLoaderVersion, - mcVersion: mc, - existingLock: existingLock, - offline: offline, - console: console, - ); - } else { - resolvedModLoaderVersion = null; - } - - final String? resolvedPluginLoaderVersion; - final pluginLoader = loaderConfig.plugins; - final pluginLoaderVersion = loaderConfig.pluginLoaderVersion; - if (pluginLoader != null && pluginLoaderVersion != null) { - resolvedPluginLoaderVersion = await _resolvePluginLoaderVersion( - pluginLoaderResolver: pluginLoaderResolver, - pluginLoader: pluginLoader, - tag: pluginLoaderVersion, - mcVersion: mc, - existingLock: existingLock, - offline: offline, - console: console, - ); - } else { - resolvedPluginLoaderVersion = null; - } - - final slugToSection = {}; - for (final section in Section.values) { - for (final slug in merged.sectionEntries(section).keys) { - slugToSection[slug] = section; - } - } - - final versionsPerSlug = >{}; - - final resolver = Resolver( - listVersions: (slug) async { - final section = slugToSection[slug] ?? Section.mods; - final loaderFilter = filterLoadersForSection(loaderConfig, section); - final entry = merged.sectionEntries(section)[slug]; - final gameVersions = {mc, ...?entry?.acceptsMc}.toList(); - - if (offline) { - final lockedProjectId = existingLock - ?.sectionFor(section)[slug] - ?.projectId; - if (lockedProjectId == null) { - throw UserError( - 'cannot resolve "$slug" while offline: not present in mods.lock ' - 'and never cached. Try again without --offline, or run ' - '`gitrinth get` once with the network available.', - ); - } - final cached = cache - .listCachedVersions(lockedProjectId) - .where((v) => _matchesLoaderAndMc(v, loaderFilter, gameVersions)) - .toList(); - versionsPerSlug[slug] = cached; - return cached; - } - - try { - final list = await api.listVersions( - slug, - loadersJson: loaderFilter == null - ? null - : encodeFilterArray(loaderFilter), - gameVersionsJson: encodeFilterArray(gameVersions), - ); - versionsPerSlug[slug] = list; - return list; - } on Object catch (e) { - final err = (e is DioException) ? e.error : e; - if (err is GitrinthException) throw err; - throw UserError('failed to list versions for $slug: $e'); - } - }, - resolveSlugForProjectId: (projectId) async { - if (slugCache.containsKey(projectId)) return slugCache[projectId]; - try { - final Project proj = await api.getProject(projectId); - slugCache[projectId] = proj.slug; - return proj.slug; - } on Object { - slugCache[projectId] = null; - return null; - } - }, - solveType: solveType, - ); - - // Promote each Modrinth-source override entry to a concrete - // Modrinth Version. This is the sticky pre-decision the resolver - // honors: the slug never enters the candidate-search loop. URL/path - // overrides take the lock builder's url/path branches and bypass - // the resolver entirely. - final overridePins = {}; - for (final e in mergedResult.overrideEntries.entries) { - final slug = e.key; - final entry = e.value; - if (entry.source is! ModrinthEntrySource) continue; - final section = slugToSection[slug] ?? Section.mods; - final loaderFilter = filterLoadersForSection(loaderConfig, section); - final gameVersions = {mc, ...entry.acceptsMc}.toList(); - final List candidates; - if (offline) { - final lockedProjectId = existingLock - ?.sectionFor(section)[slug] - ?.projectId; - if (lockedProjectId == null) { - throw UserError( - 'cannot apply project_overrides for "$slug" while offline: ' - 'not present in mods.lock and never cached.', - ); - } - candidates = cache - .listCachedVersions(lockedProjectId) - .where((v) => _matchesLoaderAndMc(v, loaderFilter, gameVersions)) - .toList(); - } else { - try { - candidates = await api.listVersions( - slug, - loadersJson: loaderFilter == null - ? null - : encodeFilterArray(loaderFilter), - gameVersionsJson: encodeFilterArray(gameVersions), - ); - } on Object catch (err) { - final e = (err is DioException) ? err.error : err; - if (e is GitrinthException) throw e; - throw UserError( - 'project_overrides: failed to list versions for "$slug": $err', - ); - } - } - final picked = pickHighestMatching( - candidates, - parseConstraint(entry.constraintRaw), - entry.channel ?? Channel.alpha, - solveType: solveType, - ); - if (picked == null) { - throw ValidationError( - "project_overrides: '$slug' has no published version matching " - "${entry.constraintRaw ?? 'any'} on ${manifest.loader.mods.name} " - "$mc", - ); - } - overridePins[slug] = OverridePin(slug, picked); - versionsPerSlug[slug] = candidates; - } - - console.message('Resolving dependencies...'); - console.solver( - 'Resolving with loader.mods=${loaderConfig.mods.name} mc=$mc...', - ); - final resolution = await resolver.resolve( - manifestForResolution, - existingLock: lockForResolution, - overridePins: overridePins, - ); - - final newLock = _buildLock( - merged, - resolution, - resolvedModLoaderVersion, - resolvedPluginLoaderVersion, - ); - final diff = diffLocks(existingLock, newLock); - - reporter.printSimpleDiff(diff); - - if (enforce && diff.isNotEmpty) { - throw ValidationError( - 'mods.lock is out of date (--enforce-lockfile). ' - '${diff.length} change(s) would be applied.', - ); - } - - if (dryRun) { - reporter.printSimpleDiff(diff, force: true); - return ResolveSyncResult( - newLock: newLock, - diff: diff, - changeCount: diff.where((d) => d.kind != DiffKind.unchanged).length, - outdated: countOutdated(newLock, versionsPerSlug), - exitCode: diff.isNotEmpty ? exitValidationError : exitOk, - ); - } - - final newLockText = emitModsLock(newLock); - io.writeModsLock(newLockText); - - reporter.printReport( - newLock: newLock, - diff: diff, - versionsPerSlug: versionsPerSlug, - overriddenSlugs: overriddenSlugs, - ); - - // Persist each resolved Modrinth version's dependency list to the - // artifact cache, sibling to the .jar. Mirrors dart pub's - // "graph-in-cache" architecture: `mods.lock` no longer carries - // forward edges; future `gitrinth upgrade --unlock-transitive` - // recomputes the closure from these JSON files. Failure here warns - // but doesn't abort — the artifact download is the contract. - _persistVersionMetadata(resolution, cache, console); - - int downloaded = 0; - int hits = 0; - final fetchErrors = []; - for (final section in Section.values) { - final sectionMap = newLock.sectionFor(section); - for (final entry in sectionMap.entries) { - final locked = entry.value; - try { - switch (locked.sourceKind) { - case LockedSourceKind.modrinth: - final file = locked.file; - final projectId = locked.projectId; - final versionId = locked.versionId; - if (file == null || projectId == null || versionId == null) { - throw FormatException( - 'lock entry "${locked.slug}" with kind=modrinth is missing ' - 'required fields — mods.lock is malformed.', - ); - } - final url = file.url; - if (url == null) continue; - final dest = cache.modrinthPath( - projectId: projectId, - versionId: versionId, - filename: file.name, - ); - final existed = File(dest).existsSync(); - await downloader.downloadTo( - url: url, - destinationPath: dest, - expectedSha512: file.sha512, - ); - if (existed) { - hits++; - console.io('cache hit: ${locked.slug} -> $dest'); - } else { - downloaded++; - console.io('downloaded: ${locked.slug} -> $dest'); - } - break; - case LockedSourceKind.url: - final file = locked.file; - if (file == null) continue; - final url = file.url; - if (url == null) continue; - final sha512 = file.sha512; - final dest = sha512 != null - ? cache.urlPath(sha512: sha512, filename: file.name) - : cache.unverifiedUrlPath(locked.slug, file.name); - final existed = File(dest).existsSync(); - await downloader.downloadTo( - url: url, - destinationPath: dest, - expectedSha512: sha512, - ); - if (existed) { - hits++; - } else { - downloaded++; - } - break; - case LockedSourceKind.path: - final rawPath = locked.path; - if (rawPath == null) { - throw FormatException( - 'lock entry "${locked.slug}" with kind=path has no path ' - '— mods.lock is malformed.', - ); - } - final resolved = p.isAbsolute(rawPath) - ? rawPath - : p.normalize(p.join(io.directory.path, rawPath)); - if (FileSystemEntity.typeSync(resolved) == - FileSystemEntityType.notFound) { - throw ValidationError( - 'path source for "${locked.slug}" points to a missing file: ' - '$rawPath (looked in $resolved)', - ); - } - break; - } - } on GitrinthException catch (e) { - fetchErrors.add(e.message); - } - } - } - // Verify `files:` source paths exist; mirrors the `path:` mod-entry - // existence check above. Build-time resolution would also catch - // this, but failing fast at `get` time matches user expectations. - for (final entry in newLock.files.values) { - final resolved = p.isAbsolute(entry.sourcePath) - ? entry.sourcePath - : p.normalize(p.join(io.directory.path, entry.sourcePath)); - if (FileSystemEntity.typeSync(resolved) == FileSystemEntityType.notFound) { - fetchErrors.add( - 'files: entry "${entry.destination}" points to a missing source: ' - '${entry.sourcePath} (looked in $resolved)', - ); - } - } - if (fetchErrors.isNotEmpty) { - throw ValidationError( - fetchErrors.length == 1 - ? fetchErrors.first - : 'failed to fetch ${fetchErrors.length} dependencies:\n' - '${fetchErrors.map((e) => ' - $e').join('\n')}', - ); - } - - final changeCount = diff.where((d) => d.kind != DiffKind.unchanged).length; - final outdated = countOutdated(newLock, versionsPerSlug); - console.io( - 'Locked $changeCount change(s); $downloaded downloaded, $hits cache hit(s).', - ); - - final markerCount = _countNotFoundMarkers(merged); - if (markerCount > 0) { - console.message( - '$markerCount ${markerCount == 1 ? 'entry' : 'entries'} marked ' - '$notFoundMarker — run `gitrinth migrate ` to retry.', - ); - } - final disabledCount = _countDisabledByConflictMarkers(merged); - if (disabledCount > 0) { - console.message( - '$disabledCount ${disabledCount == 1 ? 'entry' : 'entries'} marked ' - '$disabledByConflictMarker — run `gitrinth migrate ` or ' - '`gitrinth upgrade --major-versions` to retry.', - ); - } - - return ResolveSyncResult( - newLock: newLock, - diff: diff, - changeCount: changeCount, - outdated: outdated, - exitCode: exitOk, - ); -} - -int _countNotFoundMarkers(ModsYaml manifest) { - var count = 0; - for (final section in Section.values) { - for (final entry in manifest.sectionEntries(section).values) { - if (isNotFoundMarker(entry.constraintRaw)) count++; - } - } - return count; -} - -int _countDisabledByConflictMarkers(ModsYaml manifest) { - var count = 0; - for (final section in Section.values) { - for (final entry in manifest.sectionEntries(section).values) { - if (isDisabledByConflictMarker(entry.constraintRaw)) count++; - } - } - return count; -} - -void _checkUserEntriesPresentInLock(ModsYaml manifest, ModsLock? lock) { - if (lock == null) { - throw const ValidationError('mods.lock is missing (--enforce-lockfile).'); - } - final missing = []; - for (final section in Section.values) { - final entries = manifest.sectionEntries(section); - final lockSection = lock.sectionFor(section); - for (final entry in entries.entries) { - // Marker entries are absent from the lock by design. - if (isAnyGitrinthMarker(entry.value.constraintRaw)) continue; - if (!lockSection.containsKey(entry.key)) missing.add(entry.key); - } - } - if (missing.isNotEmpty) { - throw ValidationError( - 'mods.lock is missing user-declared entries (--enforce-lockfile): ' - '${missing.join(', ')}', - ); - } -} - -/// Hits `/v2/tag/game_version` and throws [ValidationError] if [mcVersion] -/// is not in the list. Called only on first lock or when the user changed -/// `mc-version` in `mods.yaml`. -Future _validateGameVersion({ - required ModrinthApi api, - required String mcVersion, -}) async { - final List known; - try { - final list = await api.getGameVersions(); - known = list.map((v) => v.version).toList(); - } on Object catch (e) { - final err = (e is DioException) ? e.error : e; - if (err is GitrinthException) throw err; - throw UserError( - 'failed to fetch Minecraft game versions from Modrinth: $e', - ); - } - if (!known.contains(mcVersion)) { - throw ValidationError( - 'mc-version "$mcVersion" is not a known Minecraft release on ' - 'Modrinth. Pick one from ' - 'https://api.modrinth.com/v2/tag/game_version (e.g. ' - '${known.take(3).join(", ")}).', - ); - } -} - -/// Returns the concrete loader version to bake into the lock. -/// -/// Skips re-resolution when the user typed a concrete tag (anything other -/// than `stable`/`latest`) and the existing lock already records that -/// same loader+version pair. `stable`/`latest` always re-resolve. -/// -/// Concrete tags hit the network for upstream validation on first use; -/// under `--offline` the validation is skipped (we trust the user's pin). -/// -/// Caller is expected to gate on [LoaderConfig.hasModRuntime] and pass -/// the populated [tag]; vanilla packs skip this resolution entirely. -Future _resolveModLoaderVersion({ - required ModLoaderVersionResolver modLoaderResolver, - required ModLoader modLoader, - required String tag, - required String mcVersion, - required ModsLock? existingLock, - required bool offline, - required Console console, -}) async { - final lockedSameLoader = existingLock?.loader.mods == modLoader; - final lockedVersion = existingLock?.loader.modLoaderVersion; - final tagIsConcrete = tag != 'stable' && tag != 'latest'; - if (tagIsConcrete && lockedSameLoader && lockedVersion == tag) { - return tag; - } - if (tagIsConcrete && offline) { - if (lockedSameLoader && lockedVersion != null && lockedVersion != tag) { - console.warn( - 'using unvalidated loader pin `${modLoader.name}:' - '$tag` while offline (mods.lock had `$lockedVersion`).', - ); - } - return tag; - } - if (!tagIsConcrete && offline) { - if (lockedSameLoader && lockedVersion != null) { - return lockedVersion; - } - throw UserError( - 'cannot resolve loader tag "$tag" while offline: no concrete ' - 'version recorded in mods.lock. Try again without --offline, or ' - 'pin a concrete tag like `${modLoader.name}:` in ' - 'mods.yaml.', - ); - } - return modLoaderResolver.resolve( - loader: modLoader, - tag: tag, - mcVersion: mcVersion, - ); -} - -Future _resolvePluginLoaderVersion({ - required PluginLoaderVersionResolver pluginLoaderResolver, - required PluginLoader pluginLoader, - required String tag, - required String mcVersion, - required ModsLock? existingLock, - required bool offline, - required Console console, -}) async { - final lockedSameLoader = existingLock?.loader.plugins == pluginLoader; - final lockedVersion = existingLock?.loader.pluginLoaderVersion; - final tagIsConcrete = tag != 'stable' && tag != 'latest'; - if (tagIsConcrete && lockedSameLoader && lockedVersion == tag) { - return tag; - } - if (tagIsConcrete && offline) { - if (lockedSameLoader && lockedVersion != null && lockedVersion != tag) { - console.warn( - 'using unvalidated plugin loader pin `${pluginLoader.name}:' - '$tag` while offline (mods.lock had `$lockedVersion`).', - ); - } - return tag; - } - if (!tagIsConcrete && offline) { - if (lockedSameLoader && lockedVersion != null) { - return lockedVersion; - } - throw UserError( - 'cannot resolve plugin loader tag "$tag" while offline: no concrete ' - 'version recorded in mods.lock. Try again without --offline, or pin ' - 'a concrete tag like `${pluginLoader.name}:` in mods.yaml.', - ); - } - return pluginLoaderResolver.resolve( - loader: pluginLoader, - tag: tag, - mcVersion: mcVersion, - ); -} - -/// Returns [existingLock] with each section's map filtered to drop any slug -/// in [freshSlugs]. Used by `gitrinth upgrade` to force the resolver to pick -/// a new version for the named entries instead of preserving the soft pin. -/// When [freshSlugs] is empty (the `get` path), returns [existingLock] -/// unchanged. -ModsLock? _stripLockPins(ModsLock? existingLock, Set freshSlugs) { - if (existingLock == null || freshSlugs.isEmpty) return existingLock; - Map strip(Map m) => { - for (final e in m.entries) - if (!freshSlugs.contains(e.key)) e.key: e.value, - }; - return existingLock.copyWith( - mods: strip(existingLock.mods), - resourcePacks: strip(existingLock.resourcePacks), - dataPacks: strip(existingLock.dataPacks), - shaders: strip(existingLock.shaders), - plugins: strip(existingLock.plugins), - ); -} - -/// Returns [merged] with each section entry whose slug is in [relaxConstraints] -/// rewritten to have `constraintRaw: null` (parsed as `VersionConstraint.any`). -/// Channel/env/optional/source are preserved. Used by -/// `gitrinth upgrade --major-versions` to bypass caret ceilings. -ModsYaml _relaxManifestConstraints(ModsYaml merged, Set relax) { - if (relax.isEmpty) return merged; - Map relaxSection(Map m) => { - for (final e in m.entries) - e.key: relax.contains(e.key) - ? e.value.copyWith(constraintRaw: null) - : e.value, - }; - return merged.copyWith( - mods: relaxSection(merged.mods), - resourcePacks: relaxSection(merged.resourcePacks), - dataPacks: relaxSection(merged.dataPacks), - shaders: relaxSection(merged.shaders), - plugins: relaxSection(merged.plugins), - ); -} diff --git a/lib/src/service/server_installer.dart b/lib/src/service/server_installer.dart index 4133365..4a28f1c 100644 --- a/lib/src/service/server_installer.dart +++ b/lib/src/service/server_installer.dart @@ -31,17 +31,20 @@ typedef ProcessRunner = /// Idempotent via a sentinel marker file so re-runs of `gitrinth build` are /// fast. class ServerInstaller { + final LoaderRegistry _registry; final ProcessRunner? _runProcess; final Map _environment; final JavaRuntimeResolver? _resolver; final Console _console; ServerInstaller({ + LoaderRegistry registry = LoaderRegistry.builtins, ProcessRunner? runProcess, Map? environment, JavaRuntimeResolver? resolver, Console? console, - }) : _runProcess = runProcess, + }) : _registry = registry, + _runProcess = runProcess, _environment = environment ?? Platform.environment, _resolver = resolver, _console = console ?? const Console(); @@ -59,6 +62,12 @@ class ServerInstaller { File? pluginServerJar, String? pluginInstallMarker, }) async { + if (!isSafeRuntimeToken(mcVersion) || + (modLoaderVersion != null && !isSafeRuntimeToken(modLoaderVersion))) { + throw const ValidationError( + 'Unsafe runtime version; installer versions cannot contain paths, URLs, or options.', + ); + } // Plugin-loader installs don't use [loader] / [modLoaderVersion]; the // mod-loader path requires both. Caller must guard with // LoaderConfig.hasModRuntime — these throws are the last line of @@ -77,6 +86,17 @@ class ServerInstaller { ); } } + final family = _registry[loader]?.installer; + if (pluginServerJar == null && + !const { + InstallerFamily.fabric, + InstallerFamily.forge, + InstallerFamily.quilt, + }.contains(family)) { + throw ValidationError( + 'Loader ${loader.name} has no installed server adapter. Metadata resolution and packing remain available.', + ); + } outputDir.createSync(recursive: true); if (pluginServerJar != null && pluginInstallMarker != null) { @@ -91,6 +111,9 @@ class ServerInstaller { return; } + if (modLoaderVersion == null) { + throw StateError('Mod-loader installation requires a concrete version.'); + } final marker = File( p.join( outputDir.path, @@ -99,19 +122,19 @@ class ServerInstaller { ); if (marker.existsSync()) return; - switch (loader) { - case ModLoader.vanilla: + switch (family) { + case InstallerFamily.vanilla: throw StateError( 'installServer: mod-loader path entered with vanilla loader; ' 'caller must dispatch through the plugin-server path or guard ' 'on hasModRuntime.', ); - case ModLoader.fabric: + case InstallerFamily.fabric: installerOrServerJar.copySync( p.join(outputDir.path, 'fabric-server-launch.jar'), ); - case ModLoader.forge: - case ModLoader.neoforge: + case InstallerFamily.forge: + case InstallerFamily.quilt: if (offline) { throw UserError( '${loader.name} server install requires running its installer ' @@ -132,8 +155,17 @@ class ServerInstaller { final args = [ '-jar', installerOrServerJar.path, - '--installServer', - outputDir.path, + if (family == InstallerFamily.quilt) ...[ + 'install', + 'server', + mcVersion, + modLoaderVersion, + '--install-dir="${outputDir.path}"', + '--download-server', + ] else ...[ + '--installServer', + outputDir.path, + ], ]; final environment = _spawnEnvironment(java.path); final injected = _runProcess; @@ -157,8 +189,20 @@ class ServerInstaller { '(see installer output above).', ); } + default: + throw ValidationError( + 'Loader ${loader.name} has no server installer adapter.', + ); + } + if (family == InstallerFamily.quilt && + (!File( + p.join(outputDir.path, 'quilt-server-launch.jar'), + ).existsSync() || + !File(p.join(outputDir.path, 'server.jar')).existsSync())) { + throw const UserError( + 'Quilt installer did not produce its server launcher and server.jar; installation is incomplete.', + ); } - marker.writeAsStringSync(DateTime.now().toIso8601String()); } diff --git a/lib/src/service/source_identity.dart b/lib/src/service/source_identity.dart new file mode 100644 index 0000000..14f34aa --- /dev/null +++ b/lib/src/service/source_identity.dart @@ -0,0 +1,125 @@ +import '../model/manifest/mods_yaml.dart'; +import '../model/manifest/mods_lock.dart'; +import 'modrinth/modrinth_url.dart'; + +/// Whether a parent edge is possible on a shared platform and source host. +bool canRequireLockedSource( + LockedEntry parent, + LockedEntry child, { + required String defaultModrinthHost, +}) { + if (parent.curseforge != null && child.curseforge != null) return true; + final parentMr = parent.modrinth; + final childMr = child.modrinth; + return parentMr != null && + childMr != null && + normalizeServerKey(parentMr.host ?? defaultModrinthHost) == + normalizeServerKey(childMr.host ?? defaultModrinthHost); +} + +/// The manifest's local name is scoped to its content section. +final class LocalEntryKey { + final Section section; + final String localKey; + const LocalEntryKey(this.section, this.localKey); + @override + bool operator ==(Object other) => + other is LocalEntryKey && + other.section == section && + other.localKey == localKey; + @override + int get hashCode => Object.hash(section, localKey); + @override + String toString() => '${section.name}/$localKey'; +} + +/// Platform identity, independent of any number of local aliases. +sealed class RemoteProjectKey { + const RemoteProjectKey(); +} + +final class ModrinthProjectKey extends RemoteProjectKey { + final String host; + final String projectId; + ModrinthProjectKey(String host, this.projectId) + : host = normalizeServerKey(host); + @override + bool operator ==(Object other) => + other is ModrinthProjectKey && + other.host == host && + other.projectId == projectId; + @override + int get hashCode => Object.hash(host, projectId); +} + +final class CurseforgeProjectKey extends RemoteProjectKey { + final int projectId; + const CurseforgeProjectKey(this.projectId); + @override + bool operator ==(Object other) => + other is CurseforgeProjectKey && other.projectId == projectId; + @override + int get hashCode => projectId.hashCode; +} + +/// One resolution's local nodes and their source identities. Reverse lookups +/// deliberately preserve every alias instead of selecting a last writer. +final class SourceIdentity { + final _nodes = {}; + final _remote = >{}; + final _names = >{}; + final _solverKeys = {}; + final _keys = {}; + + Iterable get nodes => _nodes.keys; + void register( + LocalEntryKey node, { + required String host, + String? projectSlug, + }) { + (_names[node.localKey] ??= {}).add(node); + _nodes[node] = ( + host: normalizeServerKey(host), + projectSlug: projectSlug ?? node.localKey, + ); + _keys.putIfAbsent(node, () { + final key = _solverKeys.containsKey(node.localKey) + ? '\u0000${node.section.name}/${node.localKey}\u0000' + : node.localKey; + _solverKeys[key] = node; + return key; + }); + } + + void bind(LocalEntryKey node, RemoteProjectKey remote) => + _remote.putIfAbsent(remote, () => {}).add(node); + Set aliases(RemoteProjectKey remote) => + Set.unmodifiable(_remote[remote] ?? const {}); + String host(LocalEntryKey node) => _nodes[node]!.host; + String projectSlug(LocalEntryKey node) => _nodes[node]!.projectSlug; + String solverKey(LocalEntryKey node) => _keys[node]!; + LocalEntryKey nodeForSolverKey(String key) => _solverKeys[key]!; + + /// Opaque keys are delimited at both ends, so this cannot replace a + /// fragment of another key or a user's ordinary local name. + String displayMessage(String message) { + for (final entry in _solverKeys.entries) { + if (entry.key.startsWith('\u0000')) { + message = message.replaceAll(entry.key, entry.value.localKey); + } + } + return message; + } + + Set nodesNamed(String localKey) => + Set.unmodifiable(_names[localKey] ?? const {}); + + LocalEntryKey? find(String localKey, [Section? section]) { + if (section != null) { + final node = LocalEntryKey(section, localKey); + return _nodes.containsKey(node) ? node : null; + } + final matches = nodesNamed(localKey); + return matches.length == 1 ? matches.single : null; + } +} diff --git a/lib/src/service/user_config.dart b/lib/src/service/user_config.dart index 1325dfc..2d7e5e0 100644 --- a/lib/src/service/user_config.dart +++ b/lib/src/service/user_config.dart @@ -7,7 +7,7 @@ import '../app/env.dart'; import '../app/runner_settings.dart'; import '../cli/exceptions.dart'; import '../util/safe_yaml.dart'; -import 'modrinth_url.dart'; +import 'modrinth/modrinth_url.dart'; /// User-level config. Holds per-host Modrinth tokens keyed by /// [normalizeServerKey]; mutate via [withToken] / [withoutToken]. diff --git a/lib/src/util/atomic_file_writer.dart b/lib/src/util/atomic_file_writer.dart index c2caad2..51447a4 100644 --- a/lib/src/util/atomic_file_writer.dart +++ b/lib/src/util/atomic_file_writer.dart @@ -1,4 +1,40 @@ import 'dart:io'; +import 'dart:math'; + +/// Replaces a string file through a unique sibling, preserving the destination +/// until rename and keeping concurrent writers' staging files independent. +/// A persistent `$path.lock` sibling serializes Windows destination replacement. +File atomicReplaceString(String path, String contents) { + final random = Random.secure(); + final suffix = List.generate( + 16, + (_) => random.nextInt(256).toRadixString(16).padLeft(2, '0'), + ).join(); + final temporary = File('$path.tmp-$suffix'); + try { + File(path).parent.createSync(recursive: true); + final lock = File('$path.lock').openSync(mode: FileMode.append); + try { + lock.lockSync(FileLock.blockingExclusive); + try { + temporary.writeAsStringSync(contents, flush: true); + return temporary.renameSync(path); + } finally { + lock.unlockSync(); + } + } finally { + lock.closeSync(); + } + } finally { + if (temporary.existsSync()) { + try { + temporary.deleteSync(); + } on FileSystemException { + // Cleanup must not replace the original write/rename failure. + } + } + } +} /// Writes [bytes] atomically to [path] by writing to `.tmp` and renaming. /// On failure, removes the orphan temp file. diff --git a/test/commands/add_command_test.dart b/test/commands/add_command_test.dart index 74bf19e..990794f 100644 --- a/test/commands/add_command_test.dart +++ b/test/commands/add_command_test.dart @@ -33,6 +33,8 @@ void main() { Map env() => { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, + 'GITRINTH_PAPER_API_URL': modrinth.paperBuildsUrl, }; String readYaml() => @@ -51,7 +53,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -94,7 +96,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -121,7 +123,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -149,7 +151,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -173,7 +175,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -201,7 +203,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -233,7 +235,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -268,7 +270,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -301,7 +303,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -326,7 +328,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: release '''); @@ -357,7 +359,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''; await writeManifest(before); @@ -384,7 +386,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); // Serve one jar so the download succeeds. @@ -419,7 +421,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -450,7 +452,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -479,7 +481,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -501,7 +503,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -529,7 +531,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: # comment above jei jei: ^1.0.0 # inline comment on jei @@ -567,7 +569,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -595,7 +597,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -630,7 +632,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: '''); @@ -651,7 +653,7 @@ mods: expect(yaml, contains('appleskin:')); expect(yaml, contains('version: ^3.0.9')); // yaml_edit may quote numeric-looking scalars. - expect(yaml, contains(RegExp(r'''accepts-mc:\s*['"]?1\.21['"]?'''))); + expect(yaml, contains(RegExp(r'''accepts_mc:\s*['"]?1\.21['"]?'''))); }, ); @@ -669,7 +671,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: '''); @@ -692,7 +694,7 @@ mods: yaml, contains( RegExp( - r'''accepts-mc:\s*\n\s+-\s*['"]?1\.21['"]?\s*\n\s+-\s*['"]?1\.20\.1['"]?''', + r'''accepts_mc:\s*\n\s+-\s*['"]?1\.21['"]?\s*\n\s+-\s*['"]?1\.20\.1['"]?''', ), ), ); @@ -712,7 +714,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: '''); @@ -740,7 +742,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: '''); @@ -764,7 +766,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: '''); @@ -797,7 +799,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -824,7 +826,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -846,7 +848,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -868,7 +870,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -891,7 +893,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -914,7 +916,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final bytes = Uint8List.fromList([1, 2, 3, 4]); @@ -946,7 +948,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -976,7 +978,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ '-C', @@ -1012,7 +1014,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -1050,7 +1052,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -1080,7 +1082,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -1118,7 +1120,7 @@ description: x loader: mods: "neoforge:21.1.50" plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -1149,7 +1151,7 @@ version: 0.1.0 description: x loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ diff --git a/test/commands/bridge_cli_test.dart b/test/commands/bridge_cli_test.dart new file mode 100644 index 0000000..1c0a470 --- /dev/null +++ b/test/commands/bridge_cli_test.dart @@ -0,0 +1,361 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; +import '../helpers/fake_modrinth.dart'; + +void main() { + late Directory root; + late FakeModrinth mr; + late HttpServer cf; + late List requests; + var cfPresent = true; + var cfHash = 'same'; + var classId = 6; + setUp(() async { + root = Directory.systemTemp.createTempSync('gitrinth_bridge_cli_'); + mr = FakeModrinth(); + await mr.start(); + requests = []; + cfPresent = true; + cfHash = 'same'; + classId = 6; + cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + cf.listen((request) async { + requests.add(request.uri); + final project = { + 'id': 1, + 'gameId': 432, + 'name': 'Example', + 'slug': 'example', + 'classId': classId, + }; + final file = { + 'id': 10, + 'modId': 1, + 'displayName': '1.2.3', + 'fileName': 'example-1.2.3.jar', + 'releaseType': 1, + 'fileDate': '2026-01-01', + 'gameVersions': ['1.21.1'], + 'hashes': [ + {'value': cfHash, 'algo': 1}, + ], + 'dependencies': [], + 'downloadUrl': '${mr.downloadBaseUrl}/example/example-1.2.3.jar', + }; + final path = request.uri.path; + final list = path.endsWith('/search') + ? cfPresent && (request.uri.queryParameters['classId'] == '$classId') + ? [project] + : [] + : [file]; + final body = path.endsWith('/files/10') + ? {'data': file} + : path == '/v1/mods/1' + ? {'data': project} + : { + 'data': list, + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': list.length, + 'totalCount': list.length, + }, + }; + request.response.headers.contentType = ContentType.json; + request.response.write(jsonEncode(body)); + await request.response.close(); + }); + }); + tearDown(() async { + await cf.close(force: true); + await mr.stop(); + root.deleteSync(recursive: true); + }); + File getYaml() => File(p.join(root.path, 'mods.yaml')); + void manifest({ + String platforms = '', + String loader = 'mods: "neoforge:21.1.50"', + }) { + getYaml().writeAsStringSync( + 'slug: pack\nname: Pack\nversion: 1.0.0\ndescription: x\nmc_version: 1.21.1\nloader: {$loader}\n$platforms', + ); + } + + Map env() => { + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_CURSEFORGE_URL': 'http://127.0.0.1:${cf.port}', + 'GITRINTH_CURSEFORGE_TOKEN': 'test', + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_NEOFORGE_VERSIONS_URL': mr.neoforgeVersionsUrl, + 'GITRINTH_PAPER_API_URL': mr.paperBuildsUrl, + }; + Future add(List args) => + runCli(['-C', root.path, 'add', ...args], environment: env()); + void registerMr({String hash = 'same'}) { + mr.registerVersion( + slug: 'example', + versionNumber: '1.2.3', + versionType: 'release', + ); + (mr.versions['example']!.first['files'] as List).first['hashes']['sha1'] = + hash; + } + + test('toggles preserve version, comments and entries without HTTP', () async { + manifest( + platforms: + 'gitrinth:\n version: ^0.1.0 # retained\nmods:\n existing: release\n', + ); + for (final platform in ['curseforge', 'modrinth']) { + for (final action in ['enable', 'disable']) { + final result = await runCli([ + '-C', + root.path, + platform, + action, + ], environment: env()); + expect(result.exitCode, 0, reason: result.stderr); + expect( + getYaml().readAsStringSync(), + contains('version: ^0.1.0 # retained'), + ); + } + } + expect(requests, isEmpty); + }); + test('top-level token stores CF publish credential without HTTP', () async { + final result = await runCli([ + 'token', + 'add', + 'curseforge.com', + '--token', + 'secret-publish-value', + ], environment: env()); + expect(result.exitCode, 0, reason: result.stderr); + final list = await runCli(['token', 'list'], environment: env()); + expect(list.stdout, contains('https://curseforge.com')); + expect(list.stdout, isNot(contains('secret-publish-value'))); + final removed = await runCli([ + 'token', + 'remove', + 'curseforge.com', + ], environment: env()); + expect(removed.exitCode, 0); + expect(requests, isEmpty); + }); + for (final type in { + 'mod': 6, + 'resourcepack': 12, + 'datapack': 6945, + 'shader': 6552, + 'plugin': 5, + }.entries) { + test('cf sugar supports ${type.key} without Modrinth queries', () async { + classId = type.value; + manifest( + loader: type.key == 'plugin' + ? 'plugins: "paper:100"' + : type.key == 'shader' + ? 'mods: "neoforge:21.1.50", shaders: iris' + : 'mods: "neoforge:21.1.50"', + ); + final result = await add([ + 'cf:example', + '--type', + type.key, + '--dry-run', + '--no-search', + ]); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('sources: curseforge')); + expect(result.stdout, contains('version: ^1.2.3')); + expect( + mr.requestCounts.keys.where((x) => x.contains('/project/')), + isEmpty, + ); + expect(getYaml().readAsStringSync(), isNot(contains('example'))); + }); + } + test('Folia CF request fails before network', () async { + manifest(loader: 'plugins: "folia:42"'); + final result = await add(['cf:example', '--type', 'plugin', '--dry-run']); + expect(result.exitCode, isNot(0)); + expect(requests, isEmpty); + expect(result.stderr, contains('CurseForge plugins require')); + }); + test('platform modifiers reject manual sources before HTTP', () async { + manifest(); + for (final flag in [ + '--curseforge', + '--no-modrinth', + '--allow-hash-mismatch', + ]) { + final result = await add([ + 'example', + '--url', + 'https://example.com/a.jar', + flag, + ]); + expect(result.exitCode, isNot(0)); + expect(result.stderr, contains('Platform options cannot be combined')); + } + expect(requests, isEmpty); + expect(mr.requestCounts, isEmpty); + }); + test('CF sugar enforces explicit version constraints atomically', () async { + manifest(); + final before = getYaml().readAsStringSync(); + final rejected = await add([ + 'cf:example@^2.0.0', + '--type', + 'mod', + '--dry-run', + ]); + expect(rejected.exitCode, isNot(0)); + expect(getYaml().readAsStringSync(), before); + final accepted = await add([ + 'cf:example@^1.2.0', + '--type', + 'mod', + '--dry-run', + ]); + expect( + accepted.exitCode, + 0, + reason: '${accepted.stderr}\n${accepted.stdout}', + ); + expect(accepted.stdout, contains('version: ^1.2.0')); + }); + test('dual matches write shorthand on dry run', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + registerMr(); + final result = await add(['example', '--dry-run', '--no-search']); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('example: ^1.2.3')); + expect(result.stdout, isNot(contains('sources:'))); + }); + test('CF-only add writes its source and a usable lock', () async { + manifest(); + registerMr(); + cfHash = sha1.convert(mr.artifacts.values.first).toString(); + final result = await add(['cf:example', '--type', 'mod', '--no-search']); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + final text = getYaml().readAsStringSync(); + expect(text, contains('sources: curseforge')); + expect(text, contains('version: ^1.2.3')); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + contains('file_id: 10'), + ); + expect( + mr.requestCounts.keys.where((x) => x.contains('/project/')), + isEmpty, + ); + }); + test('older matching hash pair determines the written constraint', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + registerMr(); + mr.registerVersion( + slug: 'example', + versionNumber: '2.0.0', + versionType: 'release', + ); + (mr.versions['example']!.last['files'] as List).first['hashes']['sha1'] = + 'newer-different'; + final result = await add(['example', '--dry-run', '--no-search']); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('example: ^1.2.3')); + expect(result.stdout, contains('2.0.0')); + expect( + requests.where((uri) => uri.queryParameters.containsKey('searchFilter')), + isEmpty, + ); + }); + test('missing CF restricts automatic add', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + registerMr(); + cfPresent = false; + final result = await add(['example', '--dry-run', '--no-search']); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('sources: [modrinth]')); + }); + test('missing Modrinth restricts automatic add to CF', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + final result = await add([ + 'example', + '--type', + 'mod', + '--dry-run', + '--no-search', + ]); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('sources: [curseforge]')); + }); + test('neither platform available leaves manifest unchanged', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + cfPresent = false; + final before = getYaml().readAsStringSync(); + final result = await add(['example', '--type', 'mod', '--no-search']); + expect(result.exitCode, isNot(0)); + expect(getYaml().readAsStringSync(), before); + }); + test('hosted Modrinth uses its host for matching and constraint', () async { + final hosted = FakeModrinth(); + await hosted.start(); + try { + hosted.registerVersion( + slug: 'example', + versionNumber: '2.0.0', + versionType: 'release', + ); + (hosted.versions['example']!.first['files'] as List) + .first['hashes']['sha1'] = + 'same'; + manifest( + platforms: + 'gitrinth: {curseforge: enabled}\nmodrinth_host: ${hosted.baseUrl}\n', + ); + final result = await add(['example', '--dry-run', '--no-search']); + expect(result.exitCode, 0, reason: '${result.stderr}\n${result.stdout}'); + expect(result.stdout, contains('example: ^2.0.0')); + expect( + mr.requestCounts.keys.where((x) => x.contains('/project/')), + isEmpty, + ); + } finally { + await hosted.stop(); + } + }); + test('mismatch is atomic and opt-in records modifier', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + registerMr(); + cfHash = 'different'; + final before = getYaml().readAsStringSync(); + final failed = await add(['example', '--no-search']); + expect(failed.exitCode, isNot(0)); + expect(getYaml().readAsStringSync(), before); + final allowed = await add([ + 'example', + '--allow-hash-mismatch', + '--dry-run', + '--no-search', + ]); + expect(allowed.exitCode, 0, reason: '${allowed.stderr}\n${allowed.stdout}'); + expect(allowed.stdout, contains('allow_hash_mismatch: true')); + }); + test('explicit no-CF preserves a visible one-sided restriction', () async { + manifest(platforms: 'gitrinth: {curseforge: enabled}\n'); + registerMr(); + final result = await add(['example', '--no-curseforge', '--dry-run']); + expect(result.exitCode, 0, reason: result.stderr); + expect(result.stdout, contains('sources: [modrinth]')); + expect(requests, isEmpty); + }); +} diff --git a/test/commands/build_assembler_test.dart b/test/commands/build_assembler_test.dart index e9688fb..e0129a3 100644 --- a/test/commands/build_assembler_test.dart +++ b/test/commands/build_assembler_test.dart @@ -13,14 +13,16 @@ LockedEntry _modrinth( SideEnv server = SideEnv.required, }) => LockedEntry( slug: slug, - sourceKind: LockedSourceKind.modrinth, - projectId: 'PRJ', - versionId: 'VER', - file: LockedFile( - name: '$slug.jar', - url: 'https://example.invalid/$slug.jar', - sha512: 'a' * 128, - size: 0, + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: 'PRJ', + versionId: 'VER', + file: LockedFile( + name: '$slug.jar', + url: 'https://example.invalid/$slug.jar', + sha512: 'a' * 128, + size: 0, + ), ), client: client, server: server, @@ -32,8 +34,9 @@ LockedEntry _url( SideEnv server = SideEnv.required, }) => LockedEntry( slug: slug, - sourceKind: LockedSourceKind.url, - file: LockedFile( + kind: LockedEntryKind.url, + url: 'https://example.invalid/$slug.jar', + urlFile: LockedFile( name: '$slug.jar', url: 'https://example.invalid/$slug.jar', sha512: 'b' * 128, @@ -49,7 +52,7 @@ LockedEntry _path( SideEnv server = SideEnv.required, }) => LockedEntry( slug: slug, - sourceKind: LockedSourceKind.path, + kind: LockedEntryKind.path, path: path, client: client, server: server, @@ -184,16 +187,19 @@ void main() { group('resolveSourcePath', () { final cache = GitrinthCache(root: p.join('tmpcache', 'root')); + const host = 'https://api.modrinth.com/v2'; test('modrinth source resolves via cache.modrinthPath', () { final path = resolveSourcePath( cache, _modrinth('jei'), projectDir: p.join('some', 'proj'), + modrinthHost: host, ); expect( path, cache.modrinthPath( + host: host, projectId: 'PRJ', versionId: 'VER', filename: 'jei.jar', @@ -207,10 +213,11 @@ void main() { cache, entry, projectDir: p.join('some', 'proj'), + modrinthHost: host, ); expect( path, - cache.urlPath(sha512: entry.file!.sha512!, filename: 'custom.jar'), + cache.urlPath(sha512: entry.urlFile!.sha512!, filename: 'custom.jar'), ); }); @@ -220,6 +227,7 @@ void main() { cache, _path('local', p.join('mods', 'local.jar')), projectDir: projectDir, + modrinthHost: host, ); expect(path, p.normalize(p.join(projectDir, 'mods', 'local.jar'))); }); @@ -230,18 +238,23 @@ void main() { cache, _path('abs', abs), projectDir: p.join(p.separator, 'elsewhere'), + modrinthHost: host, ); expect(path, abs); }); - test('modrinth entry missing projectId throws ValidationError', () { + test('resolved entry without source blocks throws ValidationError', () { final broken = LockedEntry( slug: 'broken', - sourceKind: LockedSourceKind.modrinth, - file: const LockedFile(name: 'broken.jar'), + kind: LockedEntryKind.resolved, ); expect( - () => resolveSourcePath(cache, broken, projectDir: '.'), + () => resolveSourcePath( + cache, + broken, + projectDir: '.', + modrinthHost: host, + ), throwsA(isA()), ); }); @@ -251,10 +264,16 @@ void main() { () { final entry = LockedEntry( slug: 'unhashed', - sourceKind: LockedSourceKind.url, - file: const LockedFile(name: 'unhashed.jar'), + kind: LockedEntryKind.url, + url: 'https://example.invalid/unhashed.jar', + urlFile: const LockedFile(name: 'unhashed.jar'), + ); + final path = resolveSourcePath( + cache, + entry, + projectDir: '.', + modrinthHost: host, ); - final path = resolveSourcePath(cache, entry, projectDir: '.'); expect(path, contains('_unverified')); expect(path, contains('unhashed')); expect(path, endsWith('unhashed.jar')); diff --git a/test/commands/build_orchestrator_test.dart b/test/commands/build_orchestrator_test.dart index 3a16bbc..5d5b698 100644 --- a/test/commands/build_orchestrator_test.dart +++ b/test/commands/build_orchestrator_test.dart @@ -5,6 +5,7 @@ import 'package:gitrinth/src/commands/build_assembler.dart'; import 'package:gitrinth/src/commands/build_orchestrator.dart'; import 'package:gitrinth/src/commands/build_pruner.dart'; import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/cache.dart'; import 'package:gitrinth/src/service/manifest_io.dart'; import 'package:path/path.dart' as p; import 'package:riverpod/riverpod.dart'; @@ -23,7 +24,7 @@ void _writeFixture( 'name: Test Pack\n' 'version: 0.1.0\n' 'description: integration test fixture\n' - 'mc-version: 1.21.1\n' + 'mc_version: 1.21.1\n' 'loader:\n' ' mods: fabric:0.17.3\n', ); @@ -36,7 +37,7 @@ void _writeFixture( for (final pth in modPaths) { final slug = p.basenameWithoutExtension(pth); modBlocks.writeln(' $slug:'); - modBlocks.writeln(' source: path'); + modBlocks.writeln(' kind: path'); modBlocks.writeln(' path: $pth'); modBlocks.writeln(' client: required'); modBlocks.writeln(' server: required'); @@ -63,7 +64,7 @@ void _writeFixture( 'gitrinth-version: 0.1.0\n' 'loader:\n' ' mods: fabric:0.17.3\n' - 'mc-version: 1.21.1\n' + 'mc_version: 1.21.1\n' '$modBlocks' 'resource_packs: {}\n' 'data_packs: {}\n' @@ -115,6 +116,51 @@ void main() { String clientPath(String relPath) => p.join(packDir.path, 'build', 'client', relPath); + test( + 'custom pack builds default-host declared and synthetic locked entries', + () async { + _writeFixture(packDir, modPaths: [], files: const {}); + final manifest = File(p.join(packDir.path, 'mods.yaml')); + manifest.writeAsStringSync( + '${manifest.readAsStringSync()}modrinth_host: https://hosted.example/v2\nmods:\n declared:\n modrinth_host: https://api.modrinth.com/v2\n', + ); + final lockFile = File(p.join(packDir.path, 'mods.lock')); + var lock = lockFile.readAsStringSync(); + final entries = StringBuffer('mods:\n'); + final cache = GitrinthCache(root: cacheRoot.path); + for (final slug in ['declared', 'synthetic']) { + entries.write( + ' $slug:\n kind: resolved\n modrinth:\n project_id: $slug\n version_id: V\n file:\n name: $slug.jar\n', + ); + final artifact = File( + cache.modrinthPath( + host: 'https://api.modrinth.com/v2', + projectId: slug, + versionId: 'V', + filename: '$slug.jar', + ), + ); + artifact.parent.createSync(recursive: true); + artifact.writeAsStringSync(slug); + } + lockFile.writeAsStringSync( + lock.replaceFirst('mods: {}', entries.toString().trimRight()), + ); + expect( + await runBuild( + options: const BuildOptions(envFlag: 'client', skipDownload: true), + container: container, + console: const Console(), + io: io, + ), + 0, + ); + for (final slug in ['declared', 'synthetic']) { + expect(File(clientPath('mods/$slug.jar')).readAsStringSync(), slug); + } + }, + ); + test('first build writes ledger and copies files: entries', () async { File(p.join(packDir.path, 'sodium.jar')).writeAsStringSync('SODIUM'); File( diff --git a/test/commands/cache_command_test.dart b/test/commands/cache_command_test.dart index 1c86f47..68be724 100644 --- a/test/commands/cache_command_test.dart +++ b/test/commands/cache_command_test.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/service/cache.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -24,6 +25,10 @@ void main() { String sha512Hex(List bytes) => sha512.convert(bytes).toString(); + // Default Modrinth host, used for the host-segmented cache layout. + const seedHost = 'https://api.modrinth.com/v2'; + final seedHostSegment = GitrinthCache.hostCacheSegment(seedHost); + void seedModrinthArtifact({ required String projectId, required String versionId, @@ -32,7 +37,7 @@ void main() { String? url, }) { final dir = Directory( - p.join(cacheRoot.path, 'modrinth', projectId, versionId), + p.join(cacheRoot.path, 'modrinth', seedHostSegment, projectId, versionId), ); dir.createSync(recursive: true); final jarBytes = utf8.encode(body); @@ -163,7 +168,14 @@ void main() { expect(out.stderr, contains('--force')); expect( File( - p.join(cacheRoot.path, 'modrinth', 'PROJ1', 'VER1', 'mod.jar'), + p.join( + cacheRoot.path, + 'modrinth', + seedHostSegment, + 'PROJ1', + 'VER1', + 'mod.jar', + ), ).existsSync(), isTrue, reason: 'a refused clean must not delete anything', @@ -233,8 +245,9 @@ void main() { test( 'cache repair on modrinth jar with missing version.json warns and skips', () async { - final dir = Directory(p.join(cacheRoot.path, 'modrinth', 'X', 'Y')) - ..createSync(recursive: true); + final dir = Directory( + p.join(cacheRoot.path, 'modrinth', seedHostSegment, 'X', 'Y'), + )..createSync(recursive: true); File(p.join(dir.path, 'orphan.jar')).writeAsStringSync('garbage'); // No version.json sibling. diff --git a/test/commands/create_command_test.dart b/test/commands/create_command_test.dart index 8eccc0c..ba791d8 100644 --- a/test/commands/create_command_test.dart +++ b/test/commands/create_command_test.dart @@ -40,8 +40,8 @@ void main() { expect(mods, contains('name: example_modpack')); expect(mods, contains('loader:')); expect(mods, contains('mods: neoforge')); - expect(mods, contains('mc-version: 1.21.1')); - expect(mods, contains('tooling:')); + expect(mods, contains('mc_version: 1.21.1')); + expect(mods, contains('gitrinth:')); expect(File(p.join(target, 'README.md')).existsSync(), isTrue); expect(File(p.join(target, '.gitignore')).existsSync(), isTrue); @@ -127,7 +127,7 @@ void main() { final mods = File(p.join(target, 'mods.yaml')).readAsStringSync(); expect(mods, contains('mods: fabric')); - expect(mods, contains('mc-version: 1.20.1')); + expect(mods, contains('mc_version: 1.20.1')); }); test( diff --git a/test/commands/curseforge_publish_test.dart b/test/commands/curseforge_publish_test.dart new file mode 100644 index 0000000..b112eb3 --- /dev/null +++ b/test/commands/curseforge_publish_test.dart @@ -0,0 +1,479 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/commands/curseforge_pack_assembler.dart'; +import 'package:gitrinth/src/commands/pack_assembler.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_upload_client.dart'; +import 'package:test/test.dart'; +import '../helpers/capture.dart'; +import '../helpers/multipart.dart'; + +const base = '''slug: test +name: Test +version: 1.0.0 +description: Test +mc_version: 1.21.1 +loader: + mods: fabric:0.16.0 +'''; +ModsYaml parse(String tail) => + parseModsYaml('$base$tail', filePath: 'mods.yaml'); + +void main() { + test( + 'both-target publication uses credentials and changelog captured before uploads', + () async { + final temp = Directory.systemTemp.createTempSync('publish_prepared_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() async { + await server.close(force: true); + temp.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}'; + final config = File('${temp.path}/config.yaml') + ..writeAsStringSync( + 'tokens:\n $host: captured-mr-token\n https://curseforge.com: captured-cf-token\n', + ); + final changelog = File('${temp.path}/release.md') + ..writeAsStringSync('Original release notes'); + File( + '${temp.path}/mods.yaml', + ).writeAsStringSync('${base}publish_to: [modrinth, curseforge]\n'); + Directory('${temp.path}/build').createSync(); + File('${temp.path}/build/test-1.0.0.mrpack').writeAsBytesSync([1]); + File('${temp.path}/build/test-1.0.0.zip').writeAsBytesSync([2]); + final uploads = []; + server.listen((request) async { + uploads.add(request.uri.path); + if (request.uri.path == '/project/test/version') { + final body = await utf8.decoder.bind(request).join(); + expect(body, contains('Original release notes')); + expect(request.headers.value('Authorization'), 'captured-mr-token'); + config.writeAsStringSync('tokens: {}\n'); + changelog.writeAsStringSync('Changed after first upload'); + } else { + expect(request.uri.path, '/api/projects/42/upload-file'); + expect(request.headers.value('X-Api-Token'), 'captured-cf-token'); + final parts = await readMultipart(request); + expect(parts.keys, unorderedEquals(['metadata', 'file'])); + expect(parts['metadata']!.text, contains('Original release notes')); + final upload = parts['file']!; + expect(upload.filename, 'test-1.0.0.zip'); + expect(upload.contentType, 'application/zip'); + expect(upload.bytes, [2], reason: 'the captured CF archive'); + } + request.response.headers.contentType = ContentType.json; + request.response.write('{"id":123}'); + await request.response.close(); + }); + final result = await runCli( + [ + '-C', + temp.path, + 'publish', + '--force', + '--curseforge-project', + '42', + '--changelog', + changelog.path, + ], + environment: { + 'GITRINTH_CONFIG': config.path, + 'GITRINTH_MODRINTH_URL': host, + 'GITRINTH_CURSEFORGE_UPLOAD_URL': host, + }, + ); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + expect(uploads, [ + '/project/test/version', + '/api/projects/42/upload-file', + ]); + }, + ); + test( + 'upload honors a caller receive timeout without retrying a stalled response', + () async { + final temp = Directory.systemTemp.createTempSync('cf_upload_timeout_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio( + BaseOptions(receiveTimeout: const Duration(milliseconds: 50)), + ); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + temp.deleteSync(recursive: true); + }); + final archive = File('${temp.path}/pack.zip') + ..writeAsBytesSync([1, 2, 3]); + var requests = 0; + server.listen((request) async { + requests++; + await request.drain(); + }); + await expectLater( + CurseForgeUploadClient( + dio, + baseUrl: 'http://127.0.0.1:${server.port}', + ).upload( + projectId: 42, + token: 'test-token', + archive: archive, + metadata: {'releaseType': 'release'}, + ), + throwsA(isA()), + ); + expect(requests, 1); + }, + ); + test( + 'second-target failure preserves first-target success without retrying', + () async { + final temp = Directory.systemTemp.createTempSync('publish_partial_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() async { + await server.close(force: true); + temp.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}'; + final config = File('${temp.path}/config.yaml') + ..writeAsStringSync( + 'tokens:\n $host: mr-token\n https://curseforge.com: cf-token\n', + ); + File( + '${temp.path}/mods.yaml', + ).writeAsStringSync('${base}publish_to: [modrinth, curseforge]\n'); + Directory('${temp.path}/build').createSync(); + File('${temp.path}/build/test-1.0.0.mrpack').writeAsBytesSync([1]); + File('${temp.path}/build/test-1.0.0.zip').writeAsBytesSync([2]); + final uploads = []; + server.listen((request) async { + await request.drain(); + uploads.add(request.uri.path); + request.response.headers.contentType = ContentType.json; + request.response.statusCode = uploads.length == 1 ? 201 : 500; + request.response.write( + uploads.length == 1 ? '{"id":123}' : '{"error":"rejected"}', + ); + await request.response.close(); + }); + final result = await runCli( + ['-C', temp.path, 'publish', '--force', '--curseforge-project', '42'], + environment: { + 'GITRINTH_CONFIG': config.path, + 'GITRINTH_MODRINTH_URL': host, + 'GITRINTH_CURSEFORGE_UPLOAD_URL': host, + }, + ); + expect(result.exitCode, isNot(0)); + expect(result.stdout, contains('Published test v1.0.0 to $host')); + expect(result.stdout, isNot(contains('to CurseForge'))); + expect(result.stderr, contains('upload')); + expect(uploads, [ + '/project/test/version', + '/api/projects/42/upload-file', + ]); + }, + ); + test( + 'publish selects both platforms and rejects a download key as upload authentication', + () async { + final temp = Directory.systemTemp.createTempSync('cf_publish_cli_'); + addTearDown(() => temp.deleteSync(recursive: true)); + File( + '${temp.path}/mods.yaml', + ).writeAsStringSync('${base}publish_to: [modrinth, curseforge]\n'); + Directory('${temp.path}/build').createSync(); + File('${temp.path}/build/test-1.0.0.mrpack').writeAsBytesSync([1]); + File('${temp.path}/build/test-1.0.0.zip').writeAsBytesSync([2]); + final args = [ + '-C', + temp.path, + 'publish', + '--dry-run', + '--curseforge-project', + '42', + ]; + final env = { + 'GITRINTH_CONFIG': '${temp.path}/config.yaml', + 'GITRINTH_TOKEN': 'mr-token', + 'GITRINTH_CURSEFORGE_TOKEN': 'download-token', + }; + final denied = await runCli(args, environment: env); + expect(denied.exitCode, 4); + expect(denied.stderr, contains('publish token')); + // Preflight prevents even a first-target upload/dry-run with invalid CF auth. + expect(denied.stdout, isNot(contains('would publish'))); + final out = await runCli( + args, + environment: { + ...env, + 'GITRINTH_CURSEFORGE_UPLOAD_TOKEN': 'upload-token', + }, + ); + expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); + expect( + out.stdout, + contains('would publish to https://api.modrinth.com/v2'), + ); + expect(out.stdout, contains('would publish to CurseForge')); + expect(out.stdout, contains('"gameVersionNames"')); + }, + ); + + test('publish_to.modrinth host is independent from source host', () async { + final temp = Directory.systemTemp.createTempSync('host_publish_cli_'); + addTearDown(() => temp.deleteSync(recursive: true)); + File('${temp.path}/mods.yaml').writeAsStringSync( + '${base}modrinth_host: https://source.example\npublish_to: {modrinth: https://publish.example/v2}\n', + ); + Directory('${temp.path}/build').createSync(); + File('${temp.path}/build/test-1.0.0.mrpack').writeAsBytesSync([1]); + File('${temp.path}/config.yaml').writeAsStringSync( + 'tokens:\n https://publish.example/v2: hosted-token\n', + ); + final out = await runCli( + ['-C', temp.path, 'publish', '--dry-run'], + environment: {'GITRINTH_CONFIG': '${temp.path}/config.yaml'}, + ); + expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); + expect(out.stdout, contains('would publish to https://publish.example/v2')); + expect( + out.stdout, + isNot(contains('would publish to https://source.example')), + ); + }); + + test('publish selectors normalize and defaults follow enabled platforms', () { + expect(parse('').publishTargets.keys, [SourceKind.modrinth]); + expect(parse('gitrinth: {curseforge: enabled}').publishTargets.length, 2); + expect(parse('publish_to: none').publishTargets, isEmpty); + for (final selector in [ + 'curseforge', + '[curseforge]', + '{curseforge: null}', + '{curseforge: {}}', + ]) { + expect(parse('publish_to: $selector').publishTargets, { + SourceKind.curseforge: null, + }); + } + expect( + parse( + 'publish_to: {modrinth: https://example.org, curseforge: null}', + ).publishTargets, + {SourceKind.modrinth: 'https://example.org', SourceKind.curseforge: null}, + ); + for (final selector in [ + '[]', + '{}', + '[modrinth, modrinth]', + '[none]', + '{none: null}', + 'https://example.org', + 'bad', + '{curseforge: true}', + '{modrinth: invalid}', + ]) { + expect( + () => parse('publish_to: $selector'), + throwsA(isA()), + reason: selector, + ); + } + }); + + test( + '--publishable rejects explicit hash-mismatch permission for the selected platform', + () { + final yaml = parse('mods:\n divergent: {allow_hash_mismatch: true}\n'); + const entry = LockedEntry( + slug: 'divergent', + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: 'mr', + versionId: 'v', + file: LockedFile(name: 'mr.jar', sha1: 'a', sha512: 'b', size: 1), + ), + curseforge: LockedCurseforgeSource( + projectId: 42, + fileId: 99, + file: LockedFile(name: 'cf.jar'), + ), + ); + const lock = ModsLock( + gitrinthVersion: 'test', + mcVersion: '1.21.1', + loader: LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + ), + mods: {'divergent': entry}, + ); + expect( + () => buildIndex( + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: true, + ), + throwsA(isA()), + ); + expect( + () => buildCurseForgeManifest( + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: true, + ), + throwsA(isA()), + ); + expect( + buildCurseForgeManifest( + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: false, + )['files'], + hasLength(1), + ); + }, + ); + + test( + 'CurseForge archive contains references and server-side plugin override bytes', + () { + final temp = Directory.systemTemp.createTempSync('cf_archive_'); + addTearDown(() => temp.deleteSync(recursive: true)); + final plugin = File('${temp.path}/plugin.jar') + ..writeAsBytesSync([1, 2, 3]); + final yaml = parse(''); + final lock = ModsLock( + gitrinthVersion: 'test', + mcVersion: '1.21.1', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + plugins: PluginLoader.folia, + ), + mods: { + 'cf': const LockedEntry( + slug: 'cf', + kind: LockedEntryKind.resolved, + curseforge: LockedCurseforgeSource( + projectId: 42, + fileId: 99, + file: LockedFile(name: 'cf.jar'), + ), + ), + }, + plugins: { + 'plugin': LockedEntry( + slug: 'plugin', + kind: LockedEntryKind.path, + path: plugin.path, + client: SideEnv.unsupported, + ), + }, + ); + final overrides = curseForgeOverrides( + lock: lock, + cache: GitrinthCache(root: '${temp.path}/cache'), + projectDir: temp.path, + target: PackTarget.server, + ); + final manifest = buildCurseForgeManifest( + yaml: yaml, + lock: lock, + target: PackTarget.server, + publishable: false, + ); + final output = File('${temp.path}/pack.zip'); + writePackArchive( + output, + manifestName: 'manifest.json', + manifest: manifest, + overrides: overrides.entries, + ); + final zip = ZipDecoder().decodeBytes(output.readAsBytesSync()); + final decoded = jsonDecode( + utf8.decode(zip.findFile('manifest.json')!.content), + ); + expect(decoded['files'], [ + {'projectID': 42, 'fileID': 99, 'required': true}, + ]); + expect(decoded['minecraft']['modLoaders'], [ + {'id': 'fabric-0.16.0', 'primary': true}, + ]); + expect(zip.findFile('overrides/plugins/plugin.jar')!.content, [1, 2, 3]); + expect(zip.findFile('modrinth.index.json'), isNull); + expect( + () => buildCurseForgeManifest( + yaml: yaml, + lock: lock, + target: PackTarget.server, + publishable: true, + ), + throwsA(isA()), + ); + expect( + () => buildCurseForgeManifest( + yaml: yaml, + lock: lock, + target: PackTarget.client, + publishable: true, + ), + returnsNormally, + ); + }, + ); + + test( + 'upload sends personal token and multipart metadata to upload endpoint', + () async { + final temp = Directory.systemTemp.createTempSync('cf_upload_'); + addTearDown(() => temp.deleteSync(recursive: true)); + // Every byte value, so encoding or truncation bugs cannot hide. + final payload = [for (var i = 0; i < 4096; i++) i % 256]; + final file = File('${temp.path}/pack.zip')..writeAsBytesSync(payload); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => server.close(force: true)); + final requestFuture = server.first.then((request) async { + expect(request.method, 'POST'); + expect(request.uri.path, '/api/projects/42/upload-file'); + expect(request.headers.value('X-Api-Token'), 'upload-secret'); + expect(request.headers.value('x-api-key'), isNull); + final parts = await readMultipart(request); + expect(parts.keys, unorderedEquals(['metadata', 'file'])); + expect(parts['metadata']!.json, {'releaseType': 'release'}); + final upload = parts['file']!; + expect(upload.filename, 'pack.zip'); + expect(upload.contentType, 'application/zip'); + expect(upload.bytes, payload); + request.response.headers.contentType = ContentType.json; + request.response.write('{"id":123}'); + await request.response.close(); + }); + final dio = Dio(); + addTearDown(dio.close); + final result = + await CurseForgeUploadClient( + dio, + baseUrl: 'http://127.0.0.1:${server.port}', + ).upload( + projectId: 42, + token: 'upload-secret', + archive: file, + metadata: {'releaseType': 'release'}, + ); + await requestFuture; + expect(result.fileId, 123); + }, + ); +} diff --git a/test/commands/curseforge_server_publication_test.dart b/test/commands/curseforge_server_publication_test.dart new file mode 100644 index 0000000..44a3987 --- /dev/null +++ b/test/commands/curseforge_server_publication_test.dart @@ -0,0 +1,720 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; +import '../helpers/multipart.dart'; + +void main() { + List clientArchive({ + String version = '1.0.0', + bool changed = false, + String? loader = 'fabric-0.16.0', + }) { + final manifest = utf8.encode( + jsonEncode({ + 'manifestType': 'minecraftModpack', + 'manifestVersion': 1, + 'name': 'Test', + 'version': version, + 'minecraft': { + 'version': '1.21.1', + 'modLoaders': [ + if (loader != null) {'id': loader, 'primary': true}, + ], + }, + 'files': [], + 'overrides': 'overrides', + }), + ); + final archive = Archive() + ..addFile(ArchiveFile('manifest.json', manifest.length, manifest)); + if (changed) { + archive.addFile(ArchiveFile('overrides/config/options.txt', 1, [9])); + } + return ZipEncoder().encode(archive); + } + + late Directory root; + late HttpServer server; + late String host; + late List> uploads; + late List uploadedFiles; + late List paths; + late bool rejectChild; + late bool rejectParent; + late bool invalidClientId; + late bool classified; + late bool rejectPeer; + late bool unknownPeer; + late int parentProject; + late String parentRelease; + + setUp(() async { + root = Directory.systemTemp.createTempSync('cf_server_publish_'); + server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + host = 'http://127.0.0.1:${server.port}'; + uploads = []; + uploadedFiles = []; + paths = []; + rejectChild = false; + rejectParent = false; + invalidClientId = false; + classified = false; + rejectPeer = false; + unknownPeer = false; + parentProject = 42; + parentRelease = 'Test 1.0.0'; + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: test +name: Test +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'fabric:0.16.0'} +publish_to: curseforge +'''); + final build = Directory(p.join(root.path, 'build'))..createSync(); + File( + p.join(build.path, 'test-1.0.0.zip'), + ).writeAsBytesSync(clientArchive()); + final content = utf8.encode('actual server bytes'); + final marker = { + 'formatVersion': 1, + 'artifactType': 'gitrinth-server-distribution', + 'slug': 'test', + 'version': '1.0.0', + 'minecraft': '1.21.1', + 'loader': {'id': 'fabric', 'version': '0.16.0'}, + 'runtimeBundled': false, + 'files': [ + { + 'path': 'mods/server.jar', + 'sha256': sha256.convert(content).toString(), + }, + ], + }; + final markerBytes = utf8.encode(jsonEncode(marker)); + final archive = Archive() + ..addFile( + ArchiveFile( + 'gitrinth-server-distribution.json', + markerBytes.length, + markerBytes, + ), + ) + ..addFile(ArchiveFile('mods/server.jar', content.length, content)); + File( + p.join(build.path, 'test-1.0.0-server-distribution.zip'), + ).writeAsBytesSync(ZipEncoder().encode(archive)); + server.listen((request) async { + paths.add(request.uri.path); + request.response.headers.contentType = ContentType.json; + if (request.method == 'POST') { + if (request.uri.path == '/project/test/version') { + expect(request.headers.value('Authorization'), 'private-mr-token'); + await request.drain(); + if (rejectPeer) request.response.statusCode = 401; + request.response.write(unknownPeer ? '{}' : '{"id":"mr-version"}'); + await request.response.close(); + return; + } + expect(request.headers.value('X-Api-Token'), 'private-upload-token'); + final parts = await readMultipart(request); + expect(parts.keys, unorderedEquals(['metadata', 'file'])); + final metadata = parts['metadata']!.json; + uploads.add(metadata); + uploadedFiles.add(parts['file']!); + final child = metadata.containsKey('parentFileID'); + if ((child && rejectChild) || (!child && rejectParent)) { + request.response.statusCode = 400; + request.response.write('{"error":"rejected"}'); + } else { + request.response.write( + jsonEncode({'id': child ? 200 : (invalidClientId ? 0 : 100)}), + ); + } + } else if (request.uri.path.endsWith('/100')) { + request.response.write( + jsonEncode({ + 'data': { + 'id': 100, + 'modId': parentProject, + 'displayName': parentRelease, + 'releaseType': 1, + 'isServerPack': false, + 'serverPackFileId': classified ? 200 : null, + }, + }), + ); + } else if (request.uri.path.endsWith('/200')) { + request.response.write( + jsonEncode({ + 'data': { + 'id': 200, + 'modId': 42, + 'isServerPack': classified, + 'parentProjectFileId': 100, + }, + }), + ); + } else { + request.response.write( + jsonEncode({ + 'data': [], + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': 0, + 'totalCount': 0, + }, + }), + ); + } + await request.response.close(); + }); + }); + tearDown(() async { + await server.close(force: true); + root.deleteSync(recursive: true); + }); + Future publish(List args, {bool forced = false}) => + runCli( + [ + '-C', + root.path, + if (forced) 'curseforge', + 'publish', + '--force', + '--curseforge-project', + '42', + ...args, + ], + environment: { + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_TOKEN': 'private-mr-token', + 'GITRINTH_MODRINTH_URL': host, + 'GITRINTH_CURSEFORGE_UPLOAD_TOKEN': 'private-upload-token', + 'GITRINTH_CURSEFORGE_TOKEN': 'private-read-token', + 'GITRINTH_CURSEFORGE_UPLOAD_URL': host, + 'GITRINTH_CURSEFORGE_URL': host, + }, + ); + List receipts() { + final dir = Directory( + p.join(root.path, '.gitrinth', 'publications', 'curseforge', '42'), + ); + return dir.existsSync() + ? dir + .listSync() + .whereType() + .where((file) => file.path.endsWith('.json')) + .toList() + : []; + } + + test( + 'uploads parent then child and reports manual classification pending', + () async { + final result = await publish(['--with-server', '--draft']); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + expect(uploads, hasLength(2)); + expect(uploads.first.containsKey('parentFileID'), isFalse); + expect(uploads.last['parentFileID'], 100); + expect(uploads.last.containsKey('gameVersions'), isFalse); + expect(uploads.last.containsKey('gameVersionNames'), isFalse); + expect(uploads.last['isMarkedForManualRelease'], isTrue); + final build = p.join(root.path, 'build'); + final client = File(p.join(build, 'test-1.0.0.zip')); + final server = File(p.join(build, 'test-1.0.0-server-distribution.zip')); + expect(uploadedFiles, hasLength(2)); + final (parentFile, childFile) = (uploadedFiles.first, uploadedFiles.last); + for (final (part, artifact) in [ + (parentFile, client), + (childFile, server), + ]) { + expect(part.filename, p.basename(artifact.path)); + expect(part.contentType, 'application/zip'); + expect(part.bytes, artifact.readAsBytesSync()); + } + expect(parentFile.zipEntries.keys, contains('manifest.json')); + expect( + parentFile.zipEntries.keys, + isNot(contains('gitrinth-server-distribution.json')), + ); + expect( + childFile.zipEntries.keys, + containsAll(['gitrinth-server-distribution.json', 'mods/server.jar']), + ); + expect( + utf8.decode(childFile.zipEntries['mods/server.jar']!), + 'actual server bytes', + ); + expect( + result.stdout, + allOf( + contains('100'), + contains('200'), + contains('pending'), + contains('author'), + ), + ); + final receipt = receipts().single.readAsStringSync(); + expect(receipt, isNot(contains('private-upload-token'))); + expect(receipt, isNot(contains('private-read-token'))); + }, + ); + test( + 'server-only retry reuses verified parent after a definite child rejection', + () async { + rejectChild = true; + final failed = await publish(['--with-server']); + expect(failed.exitCode, isNot(0)); + expect( + failed.stderr, + allOf( + contains('100'), + contains('--server-only'), + contains('--curseforge-parent-file 100'), + ), + ); + rejectChild = false; + final retried = await publish([ + '--server-only', + '--curseforge-parent-file', + '100', + ]); + expect( + retried.exitCode, + 0, + reason: '${retried.stdout}\n${retried.stderr}', + ); + expect(uploads, hasLength(3)); + expect( + uploads.where((m) => !m.containsKey('parentFileID')), + hasLength(1), + ); + }, + ); + test('missing server artifact fails before parent upload', () async { + File( + p.join(root.path, 'build', 'test-1.0.0-server-distribution.zip'), + ).deleteSync(); + final result = await publish(['--with-server']); + expect(result.exitCode, isNot(0)); + expect(result.stderr, contains('test-1.0.0-server-distribution.zip')); + expect(uploads, isEmpty); + }); + test( + 'invalid successful parent response is unknown and never retried automatically', + () async { + invalidClientId = true; + final first = await publish(['--with-server']); + expect(first.exitCode, isNot(0)); + expect(uploads, hasLength(1)); + final second = await publish(['--with-server']); + expect(second.exitCode, isNot(0)); + expect(second.stderr, contains('unknown')); + expect(uploads, hasLength(1)); + }, + ); + for (final wrong in ['project', 'release']) { + test( + 'rejects existing parent from wrong $wrong before child upload', + () async { + if (wrong == 'project') parentProject = 99; + if (wrong == 'release') parentRelease = 'Test 2.0.0'; + final result = await publish([ + '--server-only', + '--curseforge-parent-file', + '100', + ]); + expect(result.exitCode, isNot(0)); + expect( + result.stderr, + allOf(startsWith('error:'), contains('parent'), contains(wrong)), + ); + expect(uploads, isEmpty); + }, + ); + } + test( + 'dry run shows dependent steps without receipts or remote writes', + () async { + final result = await publish(['--with-server', '--dry-run']); + expect(result.exitCode, 0, reason: result.stderr); + expect( + result.stdout, + allOf(contains('client'), contains('server'), contains('parentFileID')), + ); + expect(uploads, isEmpty); + expect(receipts(), isEmpty); + }, + ); + test( + 'confirmed server relation is distinct from generic association', + () async { + classified = true; + final result = await publish(['--with-server']); + expect(result.exitCode, 0, reason: result.stderr); + expect(result.stdout, contains('confirmed')); + expect(result.stdout, isNot(contains('pending'))); + final repeated = await publish(['--with-server']); + expect(repeated.exitCode, 0, reason: repeated.stderr); + expect(uploads, hasLength(2)); + }, + ); + test( + 'changed artifact cannot reuse a completed publication receipt', + () async { + expect((await publish(['--with-server'])).exitCode, 0); + File( + p.join(root.path, 'build', 'test-1.0.0.zip'), + ).writeAsBytesSync(clientArchive(changed: true)); + final result = await publish(['--with-server']); + expect(result.exitCode, isNot(0)); + expect(result.stderr, contains('changed')); + expect(uploads, hasLength(2)); + }, + ); + test( + 'failed parent prevents child and records a retryable definite rejection', + () async { + rejectParent = true; + final result = await publish(['--with-server']); + expect(result.exitCode, isNot(0)); + expect(uploads, hasLength(1)); + expect(uploads.single.containsKey('parentFileID'), isFalse); + final receipt = jsonDecode(receipts().single.readAsStringSync()); + expect(receipt['client']['state'], 'failed'); + expect(receipt['server']['state'], 'pending'); + }, + ); + test( + 'both-platform retry reuses completed Modrinth and client steps', + () async { + final yaml = File(p.join(root.path, 'mods.yaml')); + yaml.writeAsStringSync( + yaml.readAsStringSync().replaceFirst( + 'publish_to: curseforge', + 'publish_to: [modrinth, curseforge]', + ), + ); + File( + p.join(root.path, 'build', 'test-1.0.0.mrpack'), + ).writeAsBytesSync([4]); + rejectChild = true; + final first = await publish(['--with-server']); + expect(first.exitCode, isNot(0)); + expect(first.stderr, contains('Modrinth version mr-version')); + rejectChild = false; + final second = await publish(['--with-server']); + expect(second.exitCode, 0, reason: '${second.stdout}\n${second.stderr}'); + expect( + paths.where((path) => path == '/project/test/version'), + hasLength(1), + ); + expect( + uploads.where((metadata) => !metadata.containsKey('parentFileID')), + hasLength(1), + ); + final serverOnly = await publish([ + '--server-only', + '--curseforge-parent-file', + '100', + ]); + expect(serverOnly.exitCode, 64); + final forced = await publish([ + '--server-only', + '--curseforge-parent-file', + '100', + ], forced: true); + expect(forced.exitCode, 0, reason: forced.stderr); + expect( + paths.where((path) => path == '/project/test/version'), + hasLength(1), + ); + }, + ); + for (final unknown in [false, true]) { + test( + 'CurseForge-first survives peer failure (unknown: $unknown)', + () async { + final yaml = File(p.join(root.path, 'mods.yaml')); + yaml.writeAsStringSync( + yaml.readAsStringSync().replaceFirst( + 'publish_to: curseforge', + 'publish_to: [curseforge, modrinth]', + ), + ); + File( + p.join(root.path, 'build', 'test-1.0.0.mrpack'), + ).writeAsBytesSync([4]); + rejectPeer = !unknown; + unknownPeer = unknown; + final first = await publish(['--with-server']); + expect(first.exitCode, isNot(0)); + expect(first.stdout, contains('client file 100')); + final receipt = jsonDecode(receipts().single.readAsStringSync()); + expect(receipt['client']['id'], '100'); + expect(receipt['server']['id'], '200'); + rejectPeer = false; + unknownPeer = false; + final second = await publish(['--with-server']); + expect(second.exitCode, unknown ? isNot(0) : 0, reason: second.stderr); + expect(uploads, hasLength(2)); + expect( + paths.where((path) => path == '/project/test/version'), + hasLength(unknown ? 1 : 2), + ); + }, + ); + } + for (final change in ['tampered', 'unlisted', 'missing']) { + test('server inventory rejects $change payload before any POST', () async { + final file = File( + p.join(root.path, 'build', 'test-1.0.0-server-distribution.zip'), + ); + final old = ZipDecoder().decodeBytes(file.readAsBytesSync()); + final marker = old.findFile('gitrinth-server-distribution.json'); + final payload = old.findFile('mods/server.jar'); + if (marker == null || payload == null) { + throw StateError('missing fixture'); + } + final archive = Archive() + ..addFile( + ArchiveFile(marker.name, marker.content.length, marker.content), + ); + if (change != 'missing') { + final bytes = change == 'tampered' + ? utf8.encode('changed bytes') + : payload.content; + archive.addFile(ArchiveFile(payload.name, bytes.length, bytes)); + } + if (change == 'unlisted') { + archive.addFile(ArchiveFile('mods/extra.jar', 1, [1])); + } + file.writeAsBytesSync(ZipEncoder().encode(archive)); + final result = await publish(['--with-server']); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('inventory')); + expect(uploads, isEmpty); + }); + } + test( + 'in-flight parent requires an explicit verified ID instead of another POST', + () async { + invalidClientId = true; + expect((await publish(['--with-server'])).exitCode, isNot(0)); + final file = receipts().single; + final receipt = jsonDecode(file.readAsStringSync()); + receipt['client']['state'] = 'inFlight'; + file.writeAsStringSync(jsonEncode(receipt)); + invalidClientId = false; + final resumed = await publish([ + '--server-only', + '--curseforge-parent-file', + '100', + ]); + expect(resumed.exitCode, 0, reason: resumed.stderr); + expect(uploads, hasLength(2)); + expect(uploads.last['parentFileID'], 100); + }, + ); + test( + 'legacy manifest-server archive is rejected before any upload', + () async { + final marker = utf8.encode('{"manifestType":"minecraftModpack"}'); + final archive = Archive() + ..addFile(ArchiveFile('manifest.json', marker.length, marker)); + File( + p.join(root.path, 'build', 'test-1.0.0-server-distribution.zip'), + ).writeAsBytesSync(ZipEncoder().encode(archive)); + final result = await publish(['--with-server']); + expect(result.exitCode, 2); + expect(result.stderr, contains('legacy manifest-server')); + expect(uploads, isEmpty); + }, + ); + for (final directory in [ + '../../escape/', + '.gitrinth/private/', + 'safe-link/', + ]) { + test( + 'rejects unsafe/private or symlink directory $directory before upload', + () async { + final file = File( + p.join(root.path, 'build', 'test-1.0.0-server-distribution.zip'), + ); + final archive = ZipDecoder().decodeBytes(file.readAsBytesSync()); + final entry = ArchiveFile.directory(directory); + if (directory == 'safe-link/') { + entry.symbolicLink = '../../outside'; + entry.mode = 0xa1ff; + } + archive.addFile(entry); + file.writeAsBytesSync(ZipEncoder().encode(archive)); + final result = await publish(['--with-server']); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('distribution')); + expect(uploads, isEmpty); + }, + ); + } + for (final wrong in ['corrupt', 'release']) { + test('rejects $wrong client archive before the first POST', () async { + File(p.join(root.path, 'build', 'test-1.0.0.zip')).writeAsBytesSync( + wrong == 'corrupt' ? [9] : clientArchive(version: '2.0.0'), + ); + final result = await publish(['--with-server']); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('client')); + expect(uploads, isEmpty); + }); + } + test( + 'floating manifest loader can publish matching concrete archives without a lock', + () async { + final yaml = File(p.join(root.path, 'mods.yaml')); + yaml.writeAsStringSync( + yaml.readAsStringSync().replaceFirst('fabric:0.16.0', 'fabric:stable'), + ); + final result = await publish(['--with-server']); + expect(result.exitCode, 0, reason: result.stderr); + expect(uploads, hasLength(2)); + }, + ); + test( + 'server distribution preserves and validates plugin runtime identity', + () async { + final yaml = File(p.join(root.path, 'mods.yaml')); + yaml.writeAsStringSync( + yaml.readAsStringSync().replaceFirst( + "loader: {mods: 'fabric:0.16.0'}", + "loader: {mods: vanilla, plugins: 'paper:100'}", + ), + ); + File( + p.join(root.path, 'build', 'test-1.0.0.zip'), + ).writeAsBytesSync(clientArchive(loader: null)); + final file = File( + p.join(root.path, 'build', 'test-1.0.0-server-distribution.zip'), + ); + final oldArchive = ZipDecoder().decodeBytes(file.readAsBytesSync()); + final oldMarker = jsonDecode( + utf8.decode( + oldArchive.findFile('gitrinth-server-distribution.json')!.content, + ), + ); + oldMarker['loader'] = { + 'id': 'vanilla', + 'version': null, + 'plugins': {'id': 'paper', 'version': '100'}, + }; + oldMarker['files'].single['path'] = 'plugins/server.jar'; + final marker = utf8.encode(jsonEncode(oldMarker)); + final content = oldArchive.findFile('mods/server.jar')!.content; + final archive = Archive() + ..addFile( + ArchiveFile( + 'gitrinth-server-distribution.json', + marker.length, + marker, + ), + ) + ..addFile(ArchiveFile('plugins/server.jar', content.length, content)); + file.writeAsBytesSync(ZipEncoder().encode(archive)); + final result = await publish(['--with-server']); + expect(result.exitCode, 0, reason: result.stderr); + expect(uploads, hasLength(2)); + yaml.writeAsStringSync( + yaml.readAsStringSync().replaceFirst('paper:100', 'paper:101'), + ); + final wrong = await publish(['--with-server']); + expect(wrong.exitCode, 2); + expect(wrong.stderr, contains('loader')); + expect(uploads, hasLength(2)); + }, + ); + test( + 'printed retry runs from the original external directory with explicit selections', + () async { + final outside = Directory(p.join(root.path, 'original directory')) + ..createSync(); + final config = File(p.join(root.path, 'release config.yaml')) + ..writeAsStringSync( + 'tokens:\n https://curseforge.com: private-upload-token\n', + ); + final descriptors = File(p.join(root.path, 'release loaders.yaml')) + ..writeAsStringSync('version: 1\nloaders: {}\n'); + final environment = { + 'GITRINTH_CURSEFORGE_TOKEN': 'private-read-token', + 'GITRINTH_CURSEFORGE_URL': host, + 'GITRINTH_CURSEFORGE_UPLOAD_URL': host, + }; + rejectChild = true; + final first = await runCli( + [ + '-C', + root.path, + '--config', + config.path, + 'curseforge', + 'publish', + '--loader-descriptors', + descriptors.path, + '--with-server', + '--curseforge-project', + '42', + '--force', + ], + workingDirectory: outside.path, + environment: environment, + ); + expect(first.exitCode, isNot(0)); + final command = RegExp( + r'Retry only the server: (gitrinth [^\r\n]+)', + ).firstMatch(first.stderr)?.group(1); + expect(command, isNotNull, reason: first.stderr); + final arguments = RegExp(r"'(?:[^']|'')*'|[^\s]+") + .allMatches(command!) + .map((match) { + final token = match.group(0)!; + return token.startsWith("'") + ? token.substring(1, token.length - 1).replaceAll("''", "'") + : token; + }) + .skip(1) + .toList(); + rejectChild = false; + final retried = await runCli( + arguments, + workingDirectory: outside.path, + environment: environment, + ); + expect( + retried.exitCode, + 0, + reason: '${retried.stdout}\n${retried.stderr}', + ); + expect( + arguments, + containsAll([ + '-C', + root.path, + '--config', + config.path, + '--loader-descriptors', + descriptors.path, + ]), + ); + expect(command, isNot(contains('private-upload-token'))); + expect(uploads, hasLength(3)); + }, + ); +} diff --git a/test/commands/curseforge_workflow_test.dart b/test/commands/curseforge_workflow_test.dart new file mode 100644 index 0000000..efff81a --- /dev/null +++ b/test/commands/curseforge_workflow_test.dart @@ -0,0 +1,144 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; +import '../helpers/fake_modrinth.dart'; + +void main() { + test( + 'accepted Minecraft file is verified and archived by its CF identity', + () async { + final root = Directory.systemTemp.createTempSync('cf_workflow_'); + final mr = FakeModrinth(); + await mr.start(); + final cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() async { + await cf.close(force: true); + await mr.stop(); + root.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${cf.port}'; + final bytes = utf8.encode('verified resource pack'); + final queries = []; + var downloads = 0; + cf.listen((request) async { + request.response.headers.contentType = ContentType.json; + final path = request.uri.path; + if (path == '/artifact.zip') { + downloads++; + request.response.add(bytes); + } else if (path == '/v1/mods/42') { + request.response.write( + jsonEncode({ + 'data': { + 'id': 42, + 'gameId': 432, + 'name': 'Textures', + 'slug': 'textures', + 'classId': 12, + }, + }), + ); + } else if (path == '/v1/mods/42/files') { + final version = request.uri.queryParameters['gameVersion']; + queries.add(version ?? 'missing'); + final files = version == '1.21' + ? [ + { + 'id': 420, + 'modId': 42, + 'displayName': '1.0.0', + 'fileName': 'textures.zip', + 'releaseType': 1, + 'fileDate': '2026-01-01', + 'gameVersions': ['1.21'], + 'downloadUrl': '$host/artifact.zip', + 'dependencies': [], + 'hashes': [ + {'algo': 1, 'value': sha1.convert(bytes).toString()}, + ], + }, + ] + : []; + request.response.write( + jsonEncode({ + 'data': files, + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': files.length, + 'totalCount': files.length, + }, + }), + ); + } else { + request.response.statusCode = 404; + } + await request.response.close(); + }); + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: cf-workflow +name: CF Workflow +version: 1.0.0 +description: test +mc_version: 1.21.1 +gitrinth: {modrinth: disabled, curseforge: enabled} +resource_packs: + local-textures: {curseforge: '42', accepts_mc: ['1.21']} +'''); + final environment = { + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_CURSEFORGE_URL': host, + 'GITRINTH_CURSEFORGE_TOKEN': 'fixture-key', + }; + final get = await runCli([ + '-C', + root.path, + 'get', + ], environment: environment); + expect(get.exitCode, 0, reason: '${get.stdout}\n${get.stderr}'); + expect(queries, ['1.21.1', '1.21']); + expect(downloads, 1); + final lockFile = File(p.join(root.path, 'mods.lock')); + final lock = parseModsLock( + lockFile.readAsStringSync(), + filePath: lockFile.path, + ); + final source = lock.resourcePacks['local-textures']!.curseforge!; + expect((source.projectId, source.fileId), (42, 420)); + final cached = GitrinthCache( + root: environment['GITRINTH_CACHE']!, + ).curseforgePath(projectId: 42, fileId: 420, filename: 'textures.zip'); + expect(File(cached).readAsBytesSync(), bytes); + final pack = await runCli([ + '-C', + root.path, + 'pack', + '--curseforge', + '--offline', + ], environment: environment); + expect(pack.exitCode, 0, reason: '${pack.stdout}\n${pack.stderr}'); + expect(downloads, 1); + final zip = ZipDecoder().decodeBytes( + File( + p.join(root.path, 'build', 'cf-workflow-1.0.0.zip'), + ).readAsBytesSync(), + ); + final manifest = jsonDecode( + utf8.decode(zip.findFile('manifest.json')!.content), + ); + expect(manifest['files'], [ + {'projectID': 42, 'fileID': 420, 'required': true}, + ]); + }, + ); +} diff --git a/test/commands/deps_command_test.dart b/test/commands/deps_command_test.dart index ab5304b..8644b80 100644 --- a/test/commands/deps_command_test.dart +++ b/test/commands/deps_command_test.dart @@ -1,3 +1,6 @@ +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'dart:convert'; import 'dart:io'; @@ -41,6 +44,56 @@ void main() { Future runDeps(List extra) => runCli(['-C', packDir.path, 'deps', ...extra], environment: env); + test('reports retain same local name in separate sections', () async { + await writeManifest(''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +mods: + same: 'path:mod.jar' +resource_packs: + same: 'path:resource.zip' +'''); + File(p.join(packDir.path, 'mods.lock')).writeAsStringSync( + emitModsLock( + ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig(mods: ModLoader.fabric), + mcVersion: '1.21.1', + mods: { + 'same': const LockedEntry( + slug: 'same', + kind: LockedEntryKind.path, + path: 'mod.jar', + ), + }, + resourcePacks: { + 'same': const LockedEntry( + slug: 'same', + kind: LockedEntryKind.path, + path: 'resource.zip', + ), + }, + ), + ), + ); + final json = await runDeps(['--json']); + expect(json.exitCode, 0, reason: json.stderr); + final packages = (jsonDecode(json.stdout) as Map)['packages'] as List; + expect( + packages.map((entry) => (entry as Map)['section']), + unorderedEquals(['mods', 'resourcePacks']), + ); + final list = await runDeps(['--style', 'list']); + expect(list.stdout, contains('- mods/same')); + expect(list.stdout, contains('- resourcePacks/same')); + expect(list.stdout, isNot(contains('mods/mods/same'))); + }); + test('tree style shows direct + transitive structure', () async { modrinth ..registerVersion( @@ -56,7 +109,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -86,7 +139,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -115,7 +168,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -141,7 +194,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -169,7 +222,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -193,7 +246,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); diff --git a/test/commands/downgrade_command_test.dart b/test/commands/downgrade_command_test.dart index e73dc9a..ba2bbdb 100644 --- a/test/commands/downgrade_command_test.dart +++ b/test/commands/downgrade_command_test.dart @@ -55,7 +55,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -82,7 +82,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^2.0.0 @@ -109,7 +109,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -133,7 +133,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); diff --git a/test/commands/entry_version_editor_test.dart b/test/commands/entry_version_editor_test.dart new file mode 100644 index 0000000..ec7d2b3 --- /dev/null +++ b/test/commands/entry_version_editor_test.dart @@ -0,0 +1,72 @@ +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/commands/add_command_editor.dart'; +import 'package:gitrinth/src/commands/migrate_editor.dart'; +import 'package:gitrinth/src/commands/pin_editor.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:test/test.dart'; + +void main() { + final rewrites = { + 'pin': (text, section, version) => updateEntryConstraint( + text, + section: section, + slug: 'entry', + newConstraint: version, + ), + 'migrate': (text, section, version) => setEntryVersion( + text, + section: section, + slug: 'entry', + newVersion: version, + ), + }; + for (final operation in rewrites.entries) { + for (final section in Section.values) { + test( + '${operation.key} preserves long-form metadata in ${section.name}', + () { + final before = + ''' +# retained header +${sectionKeyFor(section)}: + entry: + version: null # retained version comment + sources: curseforge + curseforge: remote-entry + client: unsupported +'''; + expect( + operation.value(before, section, 'gitrinth:not-found'), + before.replaceFirst('version: null', 'version: gitrinth:not-found'), + ); + }, + ); + } + test('${operation.key} retains its no-version diagnostic', () { + final expectedAction = operation.key == 'pin' + ? 'cannot (un)pin.' + : 'cannot rewrite version.'; + expect( + () => operation.value( + 'mods:\n entry: {url: https://example.test/a.jar}\n', + Section.mods, + '1.0.0', + ), + throwsA( + isA().having( + (error) => error.message, + 'message', + "'entry' has no `version:` field — likely a url/path source; $expectedAction", + ), + ), + ); + }); + test('${operation.key} preserves shorthand null comments', () { + const before = 'mods:\n entry: null # retained\n other: release\n'; + expect( + operation.value(before, Section.mods, '^2.0.0'), + before.replaceFirst('entry: null', 'entry: ^2.0.0'), + ); + }); + } +} diff --git a/test/commands/get_command_test.dart b/test/commands/get_command_test.dart index d1e5ecb..355d8fc 100644 --- a/test/commands/get_command_test.dart +++ b/test/commands/get_command_test.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; +import 'package:gitrinth/src/service/cache.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -69,7 +70,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -79,6 +80,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -89,8 +91,16 @@ mods: expect(lockText, contains('jei:')); expect(lockText, contains('version: 1.0.0')); + final hostSeg = GitrinthCache.hostCacheSegment(modrinth.baseUrl); final cachedJar = File( - p.join(cacheDir.path, 'modrinth', 'JEI_ID', 'JEI_V1', 'jei-1.0.0.jar'), + p.join( + cacheDir.path, + 'modrinth', + hostSeg, + 'JEI_ID', + 'JEI_V1', + 'jei-1.0.0.jar', + ), ); expect(cachedJar.existsSync(), isTrue); expect(cachedJar.readAsBytesSync(), jarBytes); @@ -127,7 +137,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: '''); @@ -137,6 +147,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 2, reason: out.stderr); @@ -157,7 +168,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -166,6 +177,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 2, reason: out.stderr); @@ -190,7 +202,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: '''); @@ -200,6 +212,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -234,7 +247,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: beta '''); @@ -244,6 +257,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -285,7 +299,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: channel: release @@ -297,6 +311,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -315,7 +330,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: does-not-exist: ^1.0.0 '''); @@ -325,6 +340,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, isNot(0), reason: out.stderr); @@ -365,7 +381,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: b: ^1.0.0 '''); @@ -373,6 +389,7 @@ mods: final env = { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; final first = await runCli(['-C', packDir.path, 'get'], environment: env); expect(first.exitCode, 0, reason: first.stderr); @@ -423,7 +440,7 @@ description: x loader: mods: "neoforge:21.1.50" shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: complementary-reimagined: ^r5.7.1 '''); @@ -433,6 +450,7 @@ shaders: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -486,7 +504,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 resource_packs: faithful-32x: ^1.21.1-december-2025 '''); @@ -496,6 +514,7 @@ resource_packs: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -548,7 +567,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 data_packs: terralith: ^2.5.8 '''); @@ -558,6 +577,7 @@ data_packs: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -619,7 +639,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 local-a: @@ -633,6 +653,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, isNot(0), reason: out.stdout); @@ -646,11 +667,13 @@ mods: ); // The modrinth download still has to have happened — proving the error // surfaced AFTER the loop completed rather than short-circuiting it. + final pathHostSeg = GitrinthCache.hostCacheSegment(modrinth.baseUrl); expect( File( p.join( cacheDir.path, 'modrinth', + pathHostSeg, 'JEI_ID', 'JEI_V1', 'jei-1.0.0.jar', @@ -674,7 +697,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -684,6 +707,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -701,7 +725,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.99.99 +mc_version: 1.99.99 mods: {} '''); @@ -710,6 +734,7 @@ mods: {} environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 2, reason: out.stdout); @@ -729,7 +754,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -737,6 +762,7 @@ mods: final env = { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; final first = await runCli([ '-C', @@ -767,7 +793,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -775,6 +801,7 @@ mods: final env = { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; await runCli(['-C', packDir.path, 'get'], environment: env); expect(modrinth.requestCounts['/v2/tag/game_version'], 1); @@ -792,7 +819,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.20.1 +mc_version: 1.20.1 mods: jei: ^2.0.0 '''); @@ -817,7 +844,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -828,6 +855,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -854,7 +882,7 @@ version: 0.1.0 description: x loader: mods: "fabric:stable" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -865,6 +893,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -896,7 +925,7 @@ version: 0.1.0 description: x loader: mods: "fabric:latest" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -907,6 +936,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -932,7 +962,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -941,6 +971,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; await runCli(['-C', packDir.path, 'get'], environment: env); await runCli(['-C', packDir.path, 'get'], environment: env); @@ -963,7 +994,7 @@ version: 0.1.0 description: x loader: mods: "fabric:stable" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -972,6 +1003,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; await runCli(['-C', packDir.path, 'get'], environment: env); await runCli(['-C', packDir.path, 'get'], environment: env); @@ -999,7 +1031,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:stable" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -1038,6 +1070,7 @@ mods: 'GITRINTH_FORGE_VERSIONS_URL': modrinth.forgeVersionsUrl, 'GITRINTH_FORGE_PROMOTIONS_URL': modrinth.forgePromotionsUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; await writeManifest(''' @@ -1047,7 +1080,7 @@ version: 0.1.0 description: x loader: mods: "forge:47.2.0" -mc-version: 1.20.1 +mc_version: 1.20.1 mods: jei: ^1.0.0 '''); @@ -1066,7 +1099,7 @@ version: 0.1.0 description: x loader: mods: "forge:47.4.10" -mc-version: 1.20.1 +mc_version: 1.20.1 mods: jei: ^1.0.0 '''); @@ -1100,6 +1133,7 @@ mods: 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; await writeManifest(''' @@ -1109,7 +1143,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -1130,7 +1164,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.99.99-unvalidated" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -1167,7 +1201,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -1177,6 +1211,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -1200,11 +1235,11 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: [1.21] + accepts_mc: [1.21] '''); final out = await runCli( @@ -1212,6 +1247,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -1224,8 +1260,8 @@ mods: p.join(packDir.path, 'mods.lock'), ).readAsStringSync(); expect(lockText, contains('appleskin:')); - expect(lockText, contains('game-versions: ["1.21"]')); - expect(lockText, contains('accepts-mc: ["1.21"]')); + expect(lockText, contains('game_versions: ["1.21"]')); + expect(lockText, contains('accepts_mc: ["1.21"]')); }, ); @@ -1251,11 +1287,11 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: [1.21] + accepts_mc: [1.21] '''); final out = await runCli( @@ -1263,6 +1299,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -1270,8 +1307,8 @@ mods: final lockText = File( p.join(packDir.path, 'mods.lock'), ).readAsStringSync(); - expect(lockText, contains('game-versions: ["1.21", 1.21.1]')); - expect(lockText, contains('accepts-mc: ["1.21"]')); + expect(lockText, contains('game_versions: ["1.21", 1.21.1]')); + expect(lockText, contains('accepts_mc: ["1.21"]')); }, ); @@ -1292,7 +1329,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: ^3.0.9 '''); @@ -1302,12 +1339,13 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); final lockText = File(p.join(packDir.path, 'mods.lock')).readAsStringSync(); - expect(lockText, isNot(contains('accepts-mc:'))); + expect(lockText, isNot(contains('accepts_mc:'))); }); // Regression: a long-form entry with `version:` set to a channel @@ -1339,7 +1377,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: distanthorizons: version: beta @@ -1352,6 +1390,7 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); @@ -1363,7 +1402,7 @@ mods: // Channel `beta` admits both `release` and `beta` versions; the // higher version (3.0.1-b, the beta) wins under semver ordering. expect(lockText, contains('version: 3.0.1-b')); - expect(lockText, contains('version-id: $betaId')); + expect(lockText, contains('version_id: $betaId')); expect(lockText, contains('client: optional')); expect(lockText, contains('server: optional')); }, @@ -1389,7 +1428,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -1399,13 +1438,16 @@ mods: environment: { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }, ); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); + final hostSeg = GitrinthCache.hostCacheSegment(modrinth.baseUrl); final metadataPath = p.join( cacheDir.path, 'modrinth', + hostSeg, 'create_ID', 'create_6_0_10', 'version.json', diff --git a/test/commands/get_offline_test.dart b/test/commands/get_offline_test.dart index 7dfcb8a..9234e34 100644 --- a/test/commands/get_offline_test.dart +++ b/test/commands/get_offline_test.dart @@ -78,7 +78,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); @@ -118,7 +118,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); diff --git a/test/commands/launch_client_command_test.dart b/test/commands/launch_client_command_test.dart index 57e0727..016317d 100644 --- a/test/commands/launch_client_command_test.dart +++ b/test/commands/launch_client_command_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:gitrinth/src/app/env.dart'; import 'package:gitrinth/src/cli/exceptions.dart'; import 'package:gitrinth/src/commands/build_orchestrator.dart'; import 'package:gitrinth/src/commands/launch_command.dart'; @@ -122,7 +123,7 @@ void main() { 'name: Pack\n' 'version: 0.1.0\n' 'description: a pack\n' - 'mc-version: 1.21.1\n' + 'mc_version: 1.21.1\n' 'loader:\n' ' mods: fabric:0.17.3\n', ); @@ -134,6 +135,61 @@ void main() { if (tempRoot.existsSync()) tempRoot.deleteSync(recursive: true); }); + test( + 'metadata-only no-build launch leaves directories untouched', + () async { + container.dispose(); + container = ProviderContainer( + overrides: [ + environmentProvider.overrideWithValue({ + 'GITRINTH_CONFIG': p.join(tempRoot.path, 'config.yaml'), + }), + ], + ); + File(io.modsLockPath).writeAsStringSync( + emitModsLock(_lock(loader: LoaderId('future-runtime'))), + ); + final fakeFetcher = _FakeFetcher(installerJar); + final fakeInstaller = _FakeClientInstaller('future-profile'); + Object? failure; + try { + await runLaunchClient( + options: const LaunchClientOptions( + autoBuild: false, + offline: false, + verbose: false, + ), + container: container, + console: const Console(), + io: io, + fetcher: fakeFetcher, + clientInstaller: fakeInstaller, + locator: _FakeLocator(launcherExecutable: launcherExe), + cache: cache, + runProcess: + ( + exe, + args, { + Directory? workingDirectory, + bool runInShell = false, + Map? environment, + ProcessStartMode mode = ProcessStartMode.inheritStdio, + }) async => (pid: 9001, exitCode: null), + ); + } catch (e) { + failure = e; + } + expect( + Directory(cache.launcherWorkDir(slug: 'pack')).existsSync(), + isFalse, + ); + expect(Directory(p.join(clientDir.path, 'mods')).existsSync(), isFalse); + expect(fakeFetcher.called, isFalse); + expect(fakeInstaller.called, isFalse); + expect(failure, isA()); + }, + ); + test('--offline refuses to launch up front', () async { await expectLater( runLaunchClient( diff --git a/test/commands/launch_server_command_test.dart b/test/commands/launch_server_command_test.dart index b0b9f9f..4ea46f9 100644 --- a/test/commands/launch_server_command_test.dart +++ b/test/commands/launch_server_command_test.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:gitrinth/src/app/env.dart'; import 'package:gitrinth/src/cli/exceptions.dart'; import 'package:gitrinth/src/commands/build_orchestrator.dart'; import 'package:gitrinth/src/commands/launch_command.dart'; @@ -158,6 +159,44 @@ void main() { File(io.modsLockPath).writeAsStringSync(emitModsLock(lock)); } + test('metadata-only no-build launch has no side effects', () async { + container.dispose(); + container = ProviderContainer( + overrides: [ + environmentProvider.overrideWithValue({ + 'GITRINTH_CONFIG': p.join(tempRoot.path, 'config.yaml'), + }), + ], + ); + writeLock(_lock(loader: LoaderId('future-runtime'))); + final fake = _FakeRunProcess(); + await expectLater( + runLaunchServer( + options: const LaunchServerOptions( + autoBuild: false, + acceptEula: true, + memoryMax: '2G', + memoryMin: '2G', + offline: false, + verbose: false, + extraArgs: [], + headless: false, + detach: false, + force: false, + ), + container: container, + console: const Console(), + io: io, + runProcess: fake.call, + resolver: fakeResolver, + ), + throwsA(isA()), + ); + expect(fakeResolver.lastMcVersion, isNull); + expect(File(p.join(serverDir.path, 'eula.txt')).existsSync(), isFalse); + expect(fake.executable, isNull); + }); + test('--accept-eula writes eula.txt with eula=true', () async { writeLock(_lock()); File( @@ -649,44 +688,40 @@ void main() { skip: Platform.isWindows ? 'POSIX-only' : null, ); - test( - 'on Windows, Forge launch shells out to run.bat', - () async { - writeLock(_lock(loader: ModLoader.forge)); - File( - p.join(serverDir.path, 'run.bat'), - ).writeAsStringSync('@echo off\n'); - final fake = _FakeRunProcess(); - await runLaunchServer( - options: const LaunchServerOptions( - acceptEula: false, - autoBuild: false, - memoryMax: '6G', - memoryMin: '6G', - offline: false, - verbose: false, - extraArgs: [], - headless: false, - detach: false, - force: false, - ), - container: container, - console: const Console(), - io: io, - runProcess: fake.call, - resolver: fakeResolver, - ); - expect(fake.executable, endsWith('run.bat')); - expect(fake.runInShell, isTrue); - // The chosen JDK's bin/ is prepended to PATH and JAVA_HOME - // points at its parent so the unmodified run.bat picks it up. - expect(fake.environment, isNotNull); - final fakeJavaBinDir = p.dirname(fakeResolver.javaPath); - expect(fake.environment!['PATH'], startsWith(fakeJavaBinDir)); - expect(fake.environment!['JAVA_HOME'], p.dirname(fakeJavaBinDir)); - }, - skip: !Platform.isWindows ? 'Windows-only' : null, - ); + test('on Windows, Forge launch shells out to run.bat', () async { + writeLock(_lock(loader: ModLoader.forge)); + File( + p.join(serverDir.path, 'run.bat'), + ).writeAsStringSync('@echo off\n'); + final fake = _FakeRunProcess(); + await runLaunchServer( + options: const LaunchServerOptions( + acceptEula: false, + autoBuild: false, + memoryMax: '6G', + memoryMin: '6G', + offline: false, + verbose: false, + extraArgs: [], + headless: false, + detach: false, + force: false, + ), + container: container, + console: const Console(), + io: io, + runProcess: fake.call, + resolver: fakeResolver, + ); + expect(fake.executable, endsWith('run.bat')); + expect(fake.runInShell, isTrue); + // The chosen JDK's bin/ is prepended to PATH and JAVA_HOME + // points at its parent so the unmodified run.bat picks it up. + expect(fake.environment, isNotNull); + final fakeJavaBinDir = p.dirname(fakeResolver.javaPath); + expect(fake.environment!['PATH'], startsWith(fakeJavaBinDir)); + expect(fake.environment!['JAVA_HOME'], p.dirname(fakeJavaBinDir)); + }, skip: !Platform.isWindows ? 'Windows-only' : null); test( 'rewrites user_jvm_args.txt with managed GC and heap flags', @@ -1058,39 +1093,35 @@ void main() { skip: Platform.isWindows ? 'POSIX-only gate' : null, ); - test( - 'Windows: --detach --headless without --force is allowed', - () async { - writeLock(_lock(loader: ModLoader.fabric)); - File( - p.join(serverDir.path, 'fabric-server-launch.jar'), - ).writeAsStringSync('FAB'); - final fake = _FakeRunProcess(); - final code = await runLaunchServer( - options: const LaunchServerOptions( - acceptEula: false, - autoBuild: false, - memoryMax: '2G', - memoryMin: '2G', - offline: false, - verbose: false, - extraArgs: [], - headless: true, - detach: true, - force: false, - ), - container: container, - console: const Console(), - io: io, - runProcess: fake.call, - resolver: fakeResolver, - ); - expect(code, 0); - expect(fake.mode, ProcessStartMode.detached); - expect(fake.args, contains('nogui')); - }, - skip: !Platform.isWindows ? 'Windows-only behavior' : null, - ); + test('Windows: --detach --headless without --force is allowed', () async { + writeLock(_lock(loader: ModLoader.fabric)); + File( + p.join(serverDir.path, 'fabric-server-launch.jar'), + ).writeAsStringSync('FAB'); + final fake = _FakeRunProcess(); + final code = await runLaunchServer( + options: const LaunchServerOptions( + acceptEula: false, + autoBuild: false, + memoryMax: '2G', + memoryMin: '2G', + offline: false, + verbose: false, + extraArgs: [], + headless: true, + detach: true, + force: false, + ), + container: container, + console: const Console(), + io: io, + runProcess: fake.call, + resolver: fakeResolver, + ); + expect(code, 0); + expect(fake.mode, ProcessStartMode.detached); + expect(fake.args, contains('nogui')); + }, skip: !Platform.isWindows ? 'Windows-only behavior' : null); test( '--detach --headless --force spawns detached and bypasses validation', diff --git a/test/commands/loader_capability_workflow_test.dart b/test/commands/loader_capability_workflow_test.dart new file mode 100644 index 0000000..2e6c99a --- /dev/null +++ b/test/commands/loader_capability_workflow_test.dart @@ -0,0 +1,171 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; +import '../helpers/fake_modrinth.dart'; + +void main() { + test('provider Purpur plugin metadata reaches concrete resolution', () async { + final root = Directory.systemTemp.createTempSync('purpur_loader_'); + final api = FakeModrinth(); + await api.start(); + addTearDown(() async { + await api.stop(); + root.deleteSync(recursive: true); + }); + api.loaderTags.add({ + 'name': 'purpur', + 'supported_project_types': ['plugin', 'project', 'mod'], + }); + api.registerVersion(slug: 'demo', versionNumber: '1.0.0', loader: 'purpur'); + api.projects['demo']?['project_type'] = 'plugin'; + final file = (api.versions['demo']?.single['files'] as List).single as Map; + (file['hashes'] as Map)['sha1'] = sha1 + .convert(api.artifacts.values.single) + .toString(); + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: demo +name: Demo +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: vanilla, plugins: 'purpur:1234'} +mods: {} +plugins: {demo: null} +publish_to: modrinth +'''); + final result = await runCli( + ['-C', root.path, 'get'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_MODRINTH_URL': api.baseUrl, + }, + ); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + expect(api.lastVersionQuery['demo']?['loaders'], '["purpur"]'); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + contains('purpur'), + ); + }); + + test( + 'a newly discovered loader resolves and packs while runtime remains explicit', + () async { + final root = Directory.systemTemp.createTempSync('future_loader_'); + final api = FakeModrinth(); + await api.start(); + addTearDown(() async { + await api.stop(); + root.deleteSync(recursive: true); + }); + api.registerVersion( + slug: 'demo', + versionNumber: '1.0.0', + loader: 'future-runtime', + ); + final version = api.versions['demo']!.single; + final file = (version['files'] as List).single as Map; + (file['hashes'] as Map)['sha1'] = sha1 + .convert(api.artifacts.values.single) + .toString(); + api.loaderTags.add({ + 'name': 'future-runtime', + 'supported_project_types': ['mod', 'modpack'], + 'icon': '', + }); + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: future +name: Future +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'future-runtime:1.2.3'} +mods: {demo: null} +publish_to: modrinth +'''); + final environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_MODRINTH_URL': api.baseUrl, + 'GITRINTH_TOKEN': 'fixture-token', + }; + final get = await runCli([ + '-C', + root.path, + 'get', + ], environment: environment); + expect(get.exitCode, 0, reason: '${get.stdout}\n${get.stderr}'); + expect(api.lastVersionQuery['demo']?['loaders'], '["future-runtime"]'); + final unsupported = await runCli([ + '-C', + root.path, + 'pack', + '--offline', + ], environment: environment); + expect(unsupported.exitCode, 2); + expect(unsupported.stderr, contains('archive metadata capability')); + final descriptors = File(p.join(root.path, 'user', 'loaders.yaml')); + descriptors.parent.createSync(recursive: true); + descriptors.writeAsStringSync( + "version: 1\nloaders:\n future-runtime:\n role: mod\n modrinth: {${api.baseUrl}: future-runtime}\n archive: {modrinth: future-runtime}\n", + ); + final packed = await runCli([ + '-C', + root.path, + 'pack', + '--offline', + '--loader-descriptors', + descriptors.path, + ], environment: environment); + expect(packed.exitCode, 0, reason: '${packed.stdout}\n${packed.stderr}'); + final archive = ZipDecoder().decodeBytes( + File( + p.join(root.path, 'build', 'future-1.0.0.mrpack'), + ).readAsBytesSync(), + ); + final index = jsonDecode( + utf8.decode(archive.findFile('modrinth.index.json')!.content), + ); + expect(index['dependencies'], { + 'minecraft': '1.21.1', + 'future-runtime': '1.2.3', + }); + final publish = await runCli([ + '-C', + root.path, + 'publish', + '--dry-run', + '--loader-descriptors', + descriptors.path, + ], environment: environment); + expect( + publish.exitCode, + 0, + reason: '${publish.stdout}\n${publish.stderr}', + ); + expect(publish.stdout, contains('future-runtime')); + final untouched = Directory(p.join(root.path, 'runtime-output')); + final build = await runCli([ + '-C', + root.path, + 'build', + 'server', + '--output', + untouched.path, + '--offline', + '--loader-descriptors', + descriptors.path, + ], environment: environment); + expect(build.exitCode, 2, reason: build.stderr); + expect(build.stderr, contains('adapter')); + expect(untouched.existsSync(), isFalse); + }, + ); +} diff --git a/test/commands/migrate_command_test.dart b/test/commands/migrate_command_test.dart index 4db6b3b..2e372eb 100644 --- a/test/commands/migrate_command_test.dart +++ b/test/commands/migrate_command_test.dart @@ -34,6 +34,7 @@ void main() { env = { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_PAPER_API_URL': modrinth.paperBuildsUrl, 'GITRINTH_FABRIC_META_URL': modrinth.fabricMetaUrl, }; }); @@ -88,7 +89,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -97,10 +98,10 @@ mods: final out = await runMigrateMc('1.21.4'); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); final manifest = readManifest(); - expect(manifest, contains('mc-version: 1.21.4')); + expect(manifest, contains('mc_version: 1.21.4')); expect(manifest, contains('a: ^2.0.0')); final lock = readLock(); - expect(lock, contains('mc-version: 1.21.4')); + expect(lock, contains('mc_version: 1.21.4')); expect(lock, contains('version: 2.0.0')); }); @@ -123,7 +124,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -165,7 +166,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: @@ -215,7 +216,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -258,7 +259,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -306,7 +307,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -343,7 +344,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: @@ -409,7 +410,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -445,7 +446,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -463,7 +464,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} '''); final out = await runCli([ @@ -517,7 +518,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -574,7 +575,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -624,7 +625,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -684,7 +685,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^1.0.0 @@ -730,7 +731,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: $disabledByConflictMarker @@ -785,7 +786,7 @@ version: 0.1.0 description: x loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} '''); expect((await runGet()).exitCode, 0); @@ -808,7 +809,7 @@ slug: pack name: Pack version: 0.1.0 description: x -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} '''); expect((await runGet()).exitCode, 0); @@ -831,7 +832,7 @@ description: x loader: mods: forge plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} '''); // Forge install isn't actually run during migrate (no mods to @@ -864,7 +865,7 @@ description: x loader: mods: neoforge plugins: bukkit:187 -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} '''); diff --git a/test/commands/modrinth_publish_command_test.dart b/test/commands/modrinth_publish_command_test.dart index b71c7af..2094001 100644 --- a/test/commands/modrinth_publish_command_test.dart +++ b/test/commands/modrinth_publish_command_test.dart @@ -37,7 +37,7 @@ void main() { ..writeln('name: Test Pack') ..writeln('version: $version') ..writeln('description: x') - ..writeln('mc-version: 1.21.1') + ..writeln('mc_version: 1.21.1') ..writeln('loader:') ..writeln(' mods: neoforge') ..writeln('mods: {}'); diff --git a/test/commands/outdated_command_test.dart b/test/commands/outdated_command_test.dart index dee5882..3acb3e1 100644 --- a/test/commands/outdated_command_test.dart +++ b/test/commands/outdated_command_test.dart @@ -1,6 +1,9 @@ import 'dart:convert'; import 'dart:io'; +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -50,7 +53,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -77,7 +80,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -103,7 +106,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -123,7 +126,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -141,6 +144,82 @@ mods: expect(entry['latest']['version'], '1.5.0'); expect(entry['kind'], 'direct'); expect(entry['source'], 'modrinth'); + expect(entry['has_modrinth_source'], isTrue); + }); + + test('--json preserves source labels for all artifact kinds', () async { + await writeManifest(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: + mods: "neoforge:21.1.50" +mods: {} +'''); + const mr = LockedModrinthSource( + projectId: 'p', + versionId: 'v', + file: LockedFile(name: 'mr.jar'), + ); + const cf = LockedCurseforgeSource( + projectId: 42, + fileId: 7, + file: LockedFile(name: 'cf.jar'), + ); + final lock = ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.neoforge, + modLoaderVersion: '21.1.50', + ), + mcVersion: '1.21.1', + mods: const { + 'mr': LockedEntry( + slug: 'mr', + kind: LockedEntryKind.resolved, + modrinth: mr, + ), + 'cf': LockedEntry( + slug: 'cf', + kind: LockedEntryKind.resolved, + curseforge: cf, + ), + 'dual': LockedEntry( + slug: 'dual', + kind: LockedEntryKind.resolved, + modrinth: mr, + curseforge: cf, + ), + 'url': LockedEntry( + slug: 'url', + kind: LockedEntryKind.url, + url: 'https://example.com/mod.jar', + ), + 'path': LockedEntry( + slug: 'path', + kind: LockedEntryKind.path, + path: './mod.jar', + ), + }, + ); + File( + p.join(packDir.path, 'mods.lock'), + ).writeAsStringSync(emitModsLock(lock)); + final out = await runOutdated(['--json', '--offline']); + expect(out.exitCode, 0, reason: out.stderr); + final rows = (jsonDecode(out.stdout) as Map)['packages'] as List; + expect( + {for (final row in rows) row['slug']: row['source']}, + { + 'mr': 'modrinth', + 'cf': 'curseforge', + 'dual': 'modrinth', + 'url': 'url', + 'path': 'path', + }, + ); }); test('errors out when mods.lock is missing', () async { @@ -151,7 +230,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); diff --git a/test/commands/override_command_test.dart b/test/commands/override_command_test.dart index 0915ec2..a4a19dc 100644 --- a/test/commands/override_command_test.dart +++ b/test/commands/override_command_test.dart @@ -32,6 +32,7 @@ void main() { Map env() => { 'GITRINTH_MODRINTH_URL': modrinth.baseUrl, 'GITRINTH_CACHE': cacheDir.path, + 'GITRINTH_NEOFORGE_VERSIONS_URL': modrinth.neoforgeVersionsUrl, }; String readYaml() => @@ -47,7 +48,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.0.0 '''); @@ -196,7 +197,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.0.0 project_overrides: @@ -272,7 +273,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: other: ^1.0.0 mod_a: ^1.0.0 diff --git a/test/commands/pack_assembler_test.dart b/test/commands/pack_assembler_test.dart index 026abb4..e3f45ac 100644 --- a/test/commands/pack_assembler_test.dart +++ b/test/commands/pack_assembler_test.dart @@ -55,16 +55,19 @@ LockedEntry _modrinth({ }) { return LockedEntry( slug: slug, - sourceKind: LockedSourceKind.modrinth, + kind: LockedEntryKind.resolved, version: '1.0.0', - projectId: projectId, - versionId: versionId, - file: LockedFile( - name: name, - url: url, - sha1: sha1, - sha512: sha512, - size: size, + modrinth: LockedModrinthSource( + projectId: projectId, + versionId: versionId, + versionNumber: '1.0.0', + file: LockedFile( + name: name, + url: url, + sha1: sha1, + sha512: sha512, + size: size, + ), ), client: client, server: server, @@ -81,8 +84,9 @@ LockedEntry _url({ }) { return LockedEntry( slug: slug, - sourceKind: LockedSourceKind.url, - file: LockedFile(name: filename, url: url, sha512: sha512), + kind: LockedEntryKind.url, + url: url, + urlFile: LockedFile(name: filename, url: url, sha512: sha512), client: client, server: server, ); @@ -96,7 +100,7 @@ LockedEntry _path({ }) { return LockedEntry( slug: slug, - sourceKind: LockedSourceKind.path, + kind: LockedEntryKind.path, path: path, client: client, server: server, @@ -356,24 +360,23 @@ void main() { }, ); - test( - '--publishable allows url-source RESOURCE PACK (different policy)', - () { - final lock = _lock( - mods: {'sodium': _modrinth(slug: 'sodium')}, - resourcePacks: {'custom-rp': _url(slug: 'custom-rp')}, - ); - final idx = buildIndex( + test('--publishable rejects target-missing RESOURCE PACK', () { + final lock = _lock( + mods: {'sodium': _modrinth(slug: 'sodium')}, + resourcePacks: {'custom-rp': _url(slug: 'custom-rp')}, + ); + expect( + () => buildIndex( yaml: _yaml(), lock: lock, target: PackTarget.combined, publishable: true, - ); - expect(idx.files, hasLength(1)); - }, - ); + ), + throwsA(isA()), + ); + }); - test('--publishable allows url-source DATA PACK and SHADER too', () { + test('--publishable rejects target-missing DATA PACK and SHADER too', () { final lock = _lock( dataPacks: {'custom-dp': _url(slug: 'custom-dp')}, shaders: { @@ -384,13 +387,15 @@ void main() { ), }, ); - final idx = buildIndex( - yaml: _yaml(), - lock: lock, - target: PackTarget.combined, - publishable: true, + expect( + () => buildIndex( + yaml: _yaml(), + lock: lock, + target: PackTarget.combined, + publishable: true, + ), + throwsA(isA()), ); - expect(idx.files, isEmpty); }); test( @@ -418,13 +423,15 @@ void main() { test('missing sha1 on a modrinth entry is rejected with a re-run hint', () { final entryWithoutSha1 = LockedEntry( slug: 'sodium', - sourceKind: LockedSourceKind.modrinth, - projectId: 'PID', - versionId: 'VID', - file: const LockedFile( - name: 'sodium-0.6.0.jar', - sha512: 'deadbeef', - size: 1024, + kind: LockedEntryKind.resolved, + modrinth: const LockedModrinthSource( + projectId: 'PID', + versionId: 'VID', + file: LockedFile( + name: 'sodium-0.6.0.jar', + sha512: 'deadbeef', + size: 1024, + ), ), ); final lock = _lock(mods: {'sodium': entryWithoutSha1}); diff --git a/test/commands/pack_command_test.dart b/test/commands/pack_command_test.dart index aaa1341..c5de417 100644 --- a/test/commands/pack_command_test.dart +++ b/test/commands/pack_command_test.dart @@ -53,6 +53,51 @@ void main() { 'GITRINTH_CACHE': cacheDir.path, }; + test( + '--curseforge emits split ZIPs with target-missing cached Modrinth overrides', + () async { + modrinth.registerVersion( + slug: 'sodium', + versionNumber: '0.6.0', + loader: 'fabric', + ); + await writeManifest(''' +slug: cfpack +name: CF Pack +version: 1.0.0 +description: test +loader: + mods: fabric:0.17.3 +mc_version: 1.21.1 +mods: + sodium: ^0.6.0 +'''); + final out = await runCli([ + '-C', + packDir.path, + 'pack', + '--curseforge', + ], environment: defaultEnv()); + expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); + for (final suffix in ['', '-server']) { + final file = File( + p.join(packDir.path, 'build', 'cfpack-1.0.0$suffix.zip'), + ); + final archive = ZipDecoder().decodeBytes(file.readAsBytesSync()); + final manifest = jsonDecode( + utf8.decode(archive.findFile('manifest.json')!.content), + ); + expect(manifest['manifestType'], 'minecraftModpack'); + expect(manifest['files'], isEmpty); + expect(archive.findFile('overrides/mods/sodium-0.6.0.jar'), isNotNull); + } + expect( + out.stdout, + isNot(contains('Install the server pack with mrpack-install')), + ); + }, + ); + test( 'default: writes both client and server .mrpack with valid indexes; mentions mrpack-install', () async { @@ -73,7 +118,7 @@ version: 1.2.3 description: a test pack loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -146,7 +191,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -196,7 +241,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 '''); @@ -240,7 +285,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: local-mod: path: ${p.relative(localJar.path, from: packDir.path).replaceAll(r'\', '/')} @@ -258,7 +303,7 @@ mods: }, ); - test('--publishable allows a url-source resource pack (not a mod)', () async { + test('--publishable refuses a target-missing resource pack', () async { modrinth.registerVersion( slug: 'sodium', versionNumber: '0.6.0', @@ -283,7 +328,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 resource_packs: @@ -297,20 +342,8 @@ resource_packs: 'pack', '--publishable', ], environment: defaultEnv()); - expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); - - final mrpack = File(p.join(packDir.path, 'build', 'pack-0.1.0.mrpack')); - expect(mrpack.existsSync(), isTrue); - - // RP went to overrides/, not files[]. Resource packs default to - // `client: optional, server: unsupported`, so the override lands in - // the client-only root. - final paths = _zipPaths(mrpack); - expect(paths, contains('client-overrides/resourcepacks/custom-pack.zip')); - final fileEntries = (_readIndex(mrpack)['files'] as List) - .cast>(); - expect(fileEntries, hasLength(1)); - expect(fileEntries.single['path'], 'mods/sodium-0.6.0.jar'); + expect(out.exitCode, isNot(0)); + expect(out.stderr, contains('custom-pack')); }); test( @@ -334,7 +367,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: zeta-mod: url: ${modrinth.downloadBaseUrl}/remote-b/remote-b.jar @@ -398,7 +431,7 @@ description: x loader: mods: "fabric:0.17.3" shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 mods: both-mod: url: ${modrinth.downloadBaseUrl}/srv/srv-mod.jar @@ -459,12 +492,12 @@ shaders: expect(out.stdout, contains('- both-mod (url)')); expect(out.stdout, contains('- client-mod (url)')); expect(out.stdout, contains('- server-mod (url)')); - expect(out.stdout, isNot(contains('- custom-shader'))); + expect(out.stdout, contains('- custom-shader')); }, ); test( - 'non-publishable run with only a url-source RESOURCE PACK: no permissions warning', + 'non-publishable run warns about loose RESOURCE PACK permission', () async { modrinth.registerVersion( slug: 'sodium', @@ -488,7 +521,7 @@ version: 0.1.0 description: x loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: ^0.6.0 resource_packs: @@ -502,11 +535,7 @@ resource_packs: 'pack', ], environment: defaultEnv()); expect(out.exitCode, 0, reason: '${out.stderr}\n${out.stdout}'); - expect( - out.stdout, - isNot(contains('permission from each mod author')), - reason: 'RP overrides do not require Modrinth-style mod permissions', - ); + expect(out.stdout, contains('permission from each mod author')); }, ); } diff --git a/test/commands/pack_plugins_test.dart b/test/commands/pack_plugins_test.dart index a8435b9..a3d87a5 100644 --- a/test/commands/pack_plugins_test.dart +++ b/test/commands/pack_plugins_test.dart @@ -24,16 +24,19 @@ void main() { plugins: { 'luckperms': LockedEntry( slug: 'luckperms', - sourceKind: LockedSourceKind.modrinth, + kind: LockedEntryKind.resolved, version: '5.4.0', - projectId: 'luckperms_ID', - versionId: 'luckperms_5_4_0', - file: LockedFile( - name: 'luckperms-5.4.0.jar', - url: 'https://example.com/luckperms-5.4.0.jar', - sha1: sha1, - sha512: sha512, - size: 1024, + modrinth: LockedModrinthSource( + projectId: 'luckperms_ID', + versionId: 'luckperms_5_4_0', + versionNumber: '5.4.0', + file: LockedFile( + name: 'luckperms-5.4.0.jar', + url: 'https://example.com/luckperms-5.4.0.jar', + sha1: sha1, + sha512: sha512, + size: 1024, + ), ), client: SideEnv.unsupported, server: SideEnv.required, diff --git a/test/commands/pin_command_test.dart b/test/commands/pin_command_test.dart index 8c94715..33d7ee2 100644 --- a/test/commands/pin_command_test.dart +++ b/test/commands/pin_command_test.dart @@ -36,7 +36,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.27.0.340 '''; @@ -45,19 +45,19 @@ mods: gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: - source: modrinth + kind: resolved version: "19.27.0.340" - project-id: jei-id - version-id: jei-version - file: - name: jei.jar - url: https://example.com/jei.jar - sha1: "0000000000000000000000000000000000000000" - size: 100 - env: both + modrinth: + project_id: jei-id + version_id: jei-version + file: + name: jei.jar + url: https://example.com/jei.jar + sha1: "0000000000000000000000000000000000000000" + size: 100 resource_packs: {} data_packs: {} shaders: {} @@ -86,7 +86,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^19.27.0.340 @@ -111,7 +111,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10 '''); @@ -119,19 +119,19 @@ mods: gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: - source: modrinth + kind: resolved version: "6.0.10+mc1.21.1" - project-id: c - version-id: v - file: - name: create.jar - url: https://example.com/create.jar - sha1: "0000000000000000000000000000000000000000" - size: 1 - env: both + modrinth: + project_id: c + version_id: v + file: + name: create.jar + url: https://example.com/create.jar + sha1: "0000000000000000000000000000000000000000" + size: 1 resource_packs: {} data_packs: {} shaders: {} @@ -169,7 +169,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: custom: url: https://example.com/custom.jar @@ -178,16 +178,16 @@ mods: gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: custom: - source: url + kind: url + url: https://example.com/custom.jar file: name: custom.jar url: https://example.com/custom.jar sha1: "0000000000000000000000000000000000000000" size: 1 - env: both resource_packs: {} data_packs: {} shaders: {} @@ -207,7 +207,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: terralith: ^2.5.8 data_packs: @@ -217,32 +217,32 @@ data_packs: gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: terralith: - source: modrinth + kind: resolved version: "2.5.8" - project-id: t - version-id: v - file: - name: t.jar - url: https://example.com/t.jar - sha1: "0000000000000000000000000000000000000000" - size: 1 - env: both + modrinth: + project_id: t + version_id: v + file: + name: t.jar + url: https://example.com/t.jar + sha1: "0000000000000000000000000000000000000000" + size: 1 resource_packs: {} data_packs: terralith: - source: modrinth + kind: resolved version: "2.5.8" - project-id: t - version-id: v - file: - name: t.zip - url: https://example.com/t.zip - sha1: "0000000000000000000000000000000000000000" - size: 1 - env: both + modrinth: + project_id: t + version_id: v + file: + name: t.zip + url: https://example.com/t.zip + sha1: "0000000000000000000000000000000000000000" + size: 1 shaders: {} '''); diff --git a/test/commands/remove_command_test.dart b/test/commands/remove_command_test.dart index afc1755..9b02754 100644 --- a/test/commands/remove_command_test.dart +++ b/test/commands/remove_command_test.dart @@ -55,7 +55,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.27.0.340 sodium: release @@ -93,7 +93,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 resource_packs: faithful-32x: ^1.21.0 '''); @@ -120,7 +120,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.27.0.340 '''); @@ -144,7 +144,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 '''); final out = await runCli([ @@ -163,7 +163,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: release '''); @@ -186,7 +186,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: release '''; diff --git a/test/commands/search_fallback_test.dart b/test/commands/search_fallback_test.dart new file mode 100644 index 0000000..4e85356 --- /dev/null +++ b/test/commands/search_fallback_test.dart @@ -0,0 +1,888 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; +import '../helpers/fake_modrinth.dart'; + +void main() { + late Directory root; + late FakeModrinth mr; + late HttpServer cf; + late String cfBaseUrl; + late List> candidates; + late Map>> files; + late List requests; + late List> exactProjects; + + setUp(() async { + root = Directory.systemTemp.createTempSync('gitrinth_search_'); + mr = FakeModrinth(); + await mr.start(); + mr.registerVersion(slug: 'anchor', versionNumber: '1.0.0'); + cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + cfBaseUrl = 'http://127.0.0.1:${cf.port}'; + requests = []; + exactProjects = []; + candidates = [ + { + 'id': 42, + 'gameId': 432, + 'classId': 6, + 'slug': 'different-slug', + 'name': 'Anchor', + }, + ]; + final hashes = + (mr.versions['anchor']!.single['files'] as List).single['hashes'] + as Map; + hashes['sha1'] = sha1.convert(mr.artifacts.values.single).toString(); + final hash = hashes['sha1']; + files = { + 42: [ + { + 'id': 7, + 'modId': 42, + 'displayName': '1.0.0', + 'fileName': 'anchor.jar', + 'releaseType': 1, + 'fileDate': '2026-01-01T00:00:00Z', + 'gameVersions': ['1.21.1'], + 'hashes': [ + {'value': hash, 'algo': 1}, + ], + 'dependencies': [], + 'downloadUrl': '${mr.downloadBaseUrl}/anchor/anchor-1.0.0.jar', + }, + ], + }; + cf.listen((req) async { + requests.add(req.uri); + if (RegExp(r'^/v1/mods/\d+$').hasMatch(req.uri.path)) { + final id = int.parse(req.uri.pathSegments.last); + final project = [ + ...exactProjects, + ...candidates, + ].where((p) => p['id'] == id).firstOrNull; + req.response.headers.contentType = ContentType.json; + if (project == null) req.response.statusCode = 404; + req.response.write(jsonEncode({'data': project})); + await req.response.close(); + return; + } + final data = req.uri.path.endsWith('/search') + ? (req.uri.queryParameters.containsKey('searchFilter') + ? ([ + 'anchor', + '999', + ].contains(req.uri.queryParameters['searchFilter']) + ? candidates + : []) + : (req.uri.queryParameters['slug'] == 'anchor' + ? exactProjects + : [])) + : files[int.tryParse(req.uri.path.split('/')[3])] ?? []; + req.response.headers.contentType = ContentType.json; + req.response.write( + jsonEncode({ + 'data': data, + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': data.length, + 'totalCount': data.length, + }, + }), + ); + await req.response.close(); + }); + }); + tearDown(() async { + await mr.stop(); + await cf.close(force: true); + root.deleteSync(recursive: true); + }); + + Future run({ + String extra = '', + List flags = const [], + bool dryRun = true, + String constraint = 'null', + String section = 'mods', + String pluginLoader = '', + String command = 'get', + }) async { + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: + mods: "neoforge:21.1.50" +$pluginLoader +gitrinth: + curseforge: enabled +$section: + anchor: + version: $constraint +$extra +'''); + return runCli( + ['-C', root.path, command, if (dryRun) '--dry-run', ...flags], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_NEOFORGE_VERSIONS_URL': mr.neoforgeVersionsUrl, + 'GITRINTH_PAPER_API_URL': mr.paperBuildsUrl, + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_CURSEFORGE_URL': cfBaseUrl, + 'GITRINTH_CURSEFORGE_TOKEN': 'test-key', + }, + ); + } + + test('missing CF slug discovers its hash-identical peer', () async { + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + expect(out.stderr, isNot(contains('not found'))); + expect( + out.stdout, + contains('different-slug'), + reason: '${out.exitCode}: ${out.stderr}', + ); + expect( + requests.where((u) => u.queryParameters.containsKey('searchFilter')), + hasLength(1), + ); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('discovered_via_search: true')); + expect(lock, contains('slug: different-slug')); + // Project metadata is shared between dependency resolution and peer ranking. + expect(mr.requestCounts['/v2/project/anchor_ID'], 1); + final offline = await run(flags: ['--offline'], dryRun: false); + expect(offline.exitCode, 0, reason: offline.stderr); + expect(File(p.join(root.path, 'mods.lock')).readAsStringSync(), lock); + }); + + test( + 'unavailable search hit does not hide a later hash-identical peer', + () async { + final available = candidates.single; + candidates = [ + {...available, 'id': 41, 'slug': 'unavailable-peer'}, + available, + ]; + files[41] = [ + {...files[42]!.single, 'modId': 41, 'id': 6, 'isAvailable': false}, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('project_id: 42')); + expect(lock, isNot(contains('project_id: 41'))); + }, + ); + + test( + 'missing numeric CF project retains hash-verified search fallback', + () async { + final out = await run(extra: ' curseforge: "999"', dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('project_id: 42')); + expect(lock, contains('discovered_via_search: true')); + }, + ); + + test('CF-only get writes a complete lock and reuses it offline', () async { + exactProjects = candidates; + final online = await run(extra: ' sources: curseforge', dryRun: false); + expect(online.exitCode, 0, reason: online.stderr); + final lock = File(p.join(root.path, 'mods.lock')); + final before = lock.readAsStringSync(); + expect(before, contains('project_id: 42')); + expect(before, isNot(contains(' modrinth:'))); + expect( + mr.requestCounts.keys.where( + (k) => k.startsWith('/v2/project/') || k == '/v2/search', + ), + isEmpty, + ); + await cf.close(force: true); + final offline = await run( + extra: ' sources: curseforge', + flags: ['--offline'], + dryRun: false, + ); + expect(offline.exitCode, 0, reason: offline.stderr); + expect(lock.readAsStringSync(), before); + }); + + test('allow_hash_mismatch locks both verified artifacts and warns', () async { + exactProjects = candidates; + mr.registerVersion(slug: 'cf-artifact', versionNumber: '2.0.0'); + final cfSha = sha1 + .convert(mr.artifacts['cf-artifact/cf-artifact-2.0.0.jar']!) + .toString(); + files[42]!.single['hashes'] = [ + {'algo': 1, 'value': cfSha}, + ]; + files[42]!.single['downloadUrl'] = + '${mr.downloadBaseUrl}/cf-artifact/cf-artifact-2.0.0.jar'; + final out = await run( + extra: ' allow_hash_mismatch: true', + dryRun: false, + ); + expect(out.exitCode, 0, reason: out.stderr); + expect(out.stderr, contains('accepting because allow_hash_mismatch: true')); + final text = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(text, contains(' modrinth:')); + expect(text, contains(' curseforge:')); + expect(text, contains('sha1: $cfSha')); + expect( + requests.where((u) => u.queryParameters.containsKey('searchFilter')), + isEmpty, + ); + }); + + for (final marker in [ + 'gitrinth:not-found', + 'gitrinth:disabled-by-conflict', + ]) { + test( + '$marker is excluded from CF resolution, online and offline', + () async { + final initial = await run(dryRun: false); + expect(initial.exitCode, 0, reason: initial.stderr); + requests.clear(); + exactProjects = candidates; + final offline = await run( + constraint: marker, + flags: ['--offline'], + dryRun: false, + ); + expect(offline.exitCode, 0, reason: offline.stderr); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + isNot(contains(' anchor:')), + ); + final online = await run(constraint: marker, dryRun: false); + expect(online.exitCode, 0, reason: online.stderr); + expect(requests, isEmpty); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + isNot(contains(' anchor:')), + ); + }, + ); + } + + test('offline rejects a CF-only entry without a prior lock', () async { + final out = await run( + extra: ' sources: curseforge', + flags: ['--offline'], + dryRun: false, + ); + expect(out.exitCode, 2); + expect(out.stderr, contains('anchor')); + expect(out.stderr, contains('offline')); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + }); + + test( + 'offline rejects a newly added CF-only entry without replacing the lock', + () async { + final initial = await run(dryRun: false); + expect(initial.exitCode, 0, reason: initial.stderr); + final lock = File(p.join(root.path, 'mods.lock')); + final before = lock.readAsStringSync(); + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' + additional: + sources: curseforge + curseforge: "99" +''', mode: FileMode.append); + final out = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + }, + ); + expect(out.exitCode, 2); + expect(out.stderr, contains('additional')); + expect(lock.readAsStringSync(), before); + }, + ); + + test('global and per-entry opt-outs suppress fallback requests', () async { + for (final flags in [ + ['--no-search'], + [], + ]) { + requests.clear(); + final out = await run( + flags: flags, + extra: flags.isEmpty ? ' no_cross_platform_search: true' : '', + ); + expect(out.exitCode, 2); + expect(out.stdout, isNot(contains('via search'))); + expect( + requests.where((u) => u.queryParameters.containsKey('searchFilter')), + isEmpty, + ); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + } + }); + + test('offline source restriction drops the old CF block', () async { + final first = await run(dryRun: false); + expect(first.exitCode, 0, reason: first.stderr); + final restricted = await run( + extra: ' sources: modrinth', + flags: ['--offline'], + dryRun: false, + ); + expect(restricted.exitCode, 0, reason: restricted.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, isNot(contains(' curseforge:'))); + expect(lock, contains(' modrinth:')); + }); + + test('offline get does not resurrect removed entries', () async { + final first = await run(dryRun: false); + expect(first.exitCode, 0, reason: first.stderr); + final manifest = File(p.join(root.path, 'mods.yaml')); + manifest.writeAsStringSync( + manifest.readAsStringSync().replaceAll( + ' anchor:\n version: null\n', + '', + ), + ); + final out = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + }, + ); + expect(out.exitCode, 0, reason: out.stderr); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + isNot(contains(' anchor:')), + ); + }); + + test( + 'upgrade --major-versions relaxes constraints for discovered MR peers', + () async { + exactProjects = candidates; + mr.versions.remove('anchor'); + mr.registerVersion(slug: 'other', versionNumber: '2.0.0'); + mr.projects['other'] = { + 'id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'project_type': 'mod', + }; + final hashes = + (mr.versions['other']!.single['files'] as List).single['hashes'] + as Map; + hashes['sha1'] = 'new-hash'; + files[42]!.single['hashes'] = [ + {'algo': 1, 'value': 'new-hash'}, + ]; + mr.searchHits = [ + { + 'project_id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'author': 'Author', + 'project_type': 'mod', + 'categories': ['neoforge'], + }, + ]; + final out = await run( + command: 'upgrade', + constraint: '^1.0.0', + flags: ['--major-versions'], + ); + expect(out.stderr, isEmpty); + expect(out.stdout, contains('resolved on Modrinth via search')); + }, + ); + + test('single-source entry never searches the excluded platform', () async { + final out = await run(extra: ' sources: modrinth'); + expect(out.stderr, isEmpty); + expect(requests, isEmpty); + }); + + for (final change in ['version', 'minecraft', 'peer']) { + test( + 'offline rejects changed CF-only $change before writing lock', + () async { + exactProjects = candidates; + final online = await run( + extra: ' sources: curseforge', + dryRun: false, + ); + expect(online.exitCode, 0, reason: online.stderr); + final lock = File(p.join(root.path, 'mods.lock')); + final before = lock.readAsStringSync(); + final manifest = File(p.join(root.path, 'mods.yaml')); + var text = manifest.readAsStringSync(); + text = switch (change) { + 'version' => text.replaceFirst('version: null', 'version: ^2.0.0'), + 'minecraft' => text.replaceFirst( + 'mc_version: 1.21.1', + 'mc_version: 1.21.4', + ), + _ => '$text curseforge: another-project\n', + }; + manifest.writeAsStringSync(text); + final beforeRequests = requests.length; + final offline = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + }, + ); + expect(offline.exitCode, 2); + expect(offline.stderr, contains('does not satisfy')); + expect(lock.readAsStringSync(), before); + expect(requests.length, beforeRequests); + }, + ); + } + + test( + 'Modrinth fallback uses the entry host and leaves the default host alone', + () async { + final hosted = FakeModrinth(); + await hosted.start(); + addTearDown(hosted.stop); + hosted.registerVersion(slug: 'other', versionNumber: '1.0.0'); + hosted.projects['other'] = { + 'id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'project_type': 'mod', + }; + (hosted.versions['other']!.single['files'] as List) + .single['hashes']['sha1'] = + files[42]!.single['hashes'][0]['value']; + hosted.searchHits = [ + { + 'project_id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'author': 'Author', + 'project_type': 'mod', + 'categories': ['neoforge'], + }, + ]; + exactProjects = candidates; + final out = await run(extra: ' modrinth_host: ${hosted.baseUrl}'); + expect(out.stderr, isEmpty); + expect(out.stdout, contains('resolved on Modrinth via search')); + expect(hosted.requestCounts['/v2/search'], 1); + expect(mr.requestCounts['/v2/search'], isNull); + }, + ); + + test('fuzzy-only results are diagnostics and do not write a lock', () async { + files[42]!.single['hashes'] = [ + {'algo': 1, 'value': 'different'}, + ]; + candidates.single['authors'] = [ + {'name': 'ExampleAuthor'}, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 2); + expect(out.stderr, contains('Anchor (different-slug)')); + expect(out.stderr, contains('ExampleAuthor')); + expect(out.stdout, isNot(contains('via search'))); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + }); + + test('multiple hash-matching projects remain ambiguous', () async { + candidates.add({...candidates.single, 'id': 43, 'slug': 'another-peer'}); + files[43] = [ + {...files[42]!.single, 'id': 8, 'modId': 43}, + ]; + final out = await run(); + expect(out.stderr, contains('ambiguous hash matches')); + expect(out.stdout, isNot(contains('via search'))); + }); + + test( + 'no compatible files on the declared project does not trigger search', + () async { + exactProjects = candidates; + files.clear(); + final out = await run(); + expect(out.exitCode, 2); + expect( + requests.where((u) => u.queryParameters.containsKey('searchFilter')), + isEmpty, + ); + }, + ); + + test('hash mismatch searches alternative CF projects', () async { + exactProjects = [ + {...candidates.single, 'id': 99, 'slug': 'anchor'}, + ]; + files[99] = [ + { + ...files[42]!.single, + 'modId': 99, + 'hashes': [ + {'algo': 1, 'value': 'wrong'}, + ], + }, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('project_id: 42')); + expect(lock, contains('discovered_via_search: true')); + }); + + test('older CF file is searched only inside hash_scan_depth', () async { + files[42] = [ + { + ...files[42]!.single, + 'id': 8, + 'fileDate': '2026-02-01T00:00:00Z', + 'hashes': [ + {'algo': 1, 'value': 'newer'}, + ], + }, + files[42]!.single, + ]; + final excluded = await run(extra: ' hash_scan_depth: 0'); + expect(excluded.stderr, contains('no unique hash match')); + final included = await run(extra: ' hash_scan_depth: 1', dryRun: false); + expect(included.exitCode, 0, reason: included.stderr); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + contains('file_id: 7'), + ); + }); + + for (final section in ['data_packs', 'plugins']) { + test( + '$section accepts legacy Modrinth mod hits with the right loader', + () async { + final loader = section == 'data_packs' ? 'datapack' : 'paper'; + exactProjects = [ + {...candidates.single, 'classId': section == 'data_packs' ? 6945 : 5}, + ]; + mr.versions.remove('anchor'); + mr.registerVersion( + slug: 'other', + versionNumber: '1.0.0', + loader: loader, + ); + mr.projects['other'] = { + 'id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'project_type': 'mod', + }; + (mr.versions['other']!.single['files'] as List) + .single['hashes']['sha1'] = + files[42]!.single['hashes'][0]['value']; + mr.searchHits = [ + { + 'project_id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'author': 'Author', + 'project_type': 'mod', + 'categories': [loader], + }, + ]; + final out = await run( + section: section, + pluginLoader: section == 'plugins' ? ' plugins: paper' : '', + dryRun: false, + ); + expect(out.exitCode, 0, reason: out.stderr); + expect(mr.lastSearchQuery!['facets'], contains('project_type:mod')); + expect(mr.lastSearchQuery!['facets'], contains('categories:$loader')); + expect( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + contains('project_id: other_ID'), + ); + expect( + requests + .where((u) => u.path.endsWith('/files')) + .every((u) => !u.queryParameters.containsKey('modLoaderType')), + isTrue, + ); + }, + ); + } + + test( + 'CF fallback filters by section and accepts additional MC versions', + () async { + files[42]!.single['gameVersions'] = ['1.21']; + candidates.add({ + ...candidates.single, + 'id': 43, + 'classId': 12, + 'slug': 'wrong-section', + }); + files[43] = [ + {...files[42]!.single, 'modId': 43}, + ]; + final out = await run(extra: ' accepts_mc: ["1.21"]'); + expect(out.stderr, isEmpty); + final search = requests.singleWhere( + (u) => u.queryParameters.containsKey('searchFilter'), + ); + expect(search.queryParameters['classId'], '6'); + final fileRequests = requests.where((u) => u.path.endsWith('/42/files')); + expect(fileRequests, isNotEmpty); + expect( + fileRequests.every((u) => u.queryParameters['modLoaderType'] == '6'), + isTrue, + ); + expect(requests.where((u) => u.path.endsWith('/43/files')), isEmpty); + }, + ); + + test('Folia entries never attempt CurseForge search', () async { + mr.versions['anchor']!.single['loaders'] = ['folia']; + final out = await run( + section: 'plugins', + pluginLoader: ' plugins: "folia:100"', + ); + expect(out.stderr, isEmpty); + expect(requests, isEmpty); + }); + + test('hash-selected older MR version re-solves its dependencies', () async { + mr.registerVersion(slug: 'anchor', versionNumber: '2.0.0'); + mr.registerVersion(slug: 'old-child', versionNumber: '1.0.0'); + mr.projects['old-child'] = { + 'id': 'old-child_ID', + 'slug': 'old-child', + 'title': 'Child', + 'project_type': 'mod', + }; + mr.versions['anchor']!.first['dependencies'] = [ + {'project_id': 'old-child_ID', 'dependency_type': 'required'}, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('version_id: anchor_1_0_0')); + expect(lock, contains(' old-child:')); + expect(lock, contains('name: anchor-1.0.0.jar')); + expect(lock, isNot(contains('anchor-2.0.0.jar'))); + }); + + test('hash-selected older CF file replaces its dependency graph', () async { + final rootFile = files[42]!.single; + exactProjects = [ + ...candidates, + {...candidates.single, 'id': 43, 'slug': 'old-cf-child'}, + {...candidates.single, 'id': 44, 'slug': 'new-cf-child'}, + ]; + files[43] = [ + {...rootFile, 'id': 43, 'modId': 43, 'fileName': 'old-child.jar'}, + ]; + files[44] = [ + {...rootFile, 'id': 44, 'modId': 44, 'fileName': 'new-child.jar'}, + ]; + files[42] = [ + { + ...rootFile, + 'id': 8, + 'fileDate': '2027-01-01T00:00:00Z', + 'hashes': [ + {'algo': 1, 'value': 'different'}, + ], + 'dependencies': [ + {'modId': 44, 'relationType': 3}, + ], + }, + { + ...rootFile, + 'dependencies': [ + {'modId': 43, 'relationType': 3}, + ], + }, + ]; + final out = await run(dryRun: false, flags: ['--no-search']); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains(' old-cf-child:')); + expect(lock, isNot(contains(' new-cf-child:'))); + expect(lock, contains('file_id: 7')); + }); + + test('hash scan cannot escape the manifest version constraint', () async { + mr.registerVersion(slug: 'anchor', versionNumber: '2.0.0'); + final out = await run(constraint: '^2.0.0', dryRun: false); + expect(out.exitCode, 2); + expect(out.stdout, isNot(contains('via search'))); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + }); + + test('hash selection preserves dependency version_id minimums', () async { + mr.registerVersion(slug: 'child', versionNumber: '1.0.0'); + mr.registerVersion(slug: 'child', versionNumber: '2.0.0'); + mr.versions['anchor']!.single['dependencies'] = [ + { + 'project_id': 'child_ID', + 'version_id': 'child_2_0_0', + 'dependency_type': 'required', + }, + ]; + exactProjects = [ + ...candidates, + {...candidates.single, 'id': 43, 'slug': 'child'}, + ]; + files[42]!.single['dependencies'] = [ + {'modId': 43, 'relationType': 3}, + ]; + files[43] = [ + { + ...files[42]!.single, + 'id': 43, + 'modId': 43, + 'dependencies': [], + 'displayName': '1.0.0', + 'hashes': [ + { + 'algo': 1, + 'value': sha1 + .convert(mr.artifacts['child/child-1.0.0.jar']!) + .toString(), + }, + ], + 'downloadUrl': '${mr.downloadBaseUrl}/child/child-1.0.0.jar', + }, + ]; + final out = await run(dryRun: false, flags: ['--no-search']); + expect(out.exitCode, 2, reason: '${out.stdout}\n${out.stderr}'); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + }); + + test('hash scan cannot select a beta under channel release', () async { + mr.versions['anchor']!.single['version_type'] = 'beta'; + mr.registerVersion( + slug: 'anchor', + versionNumber: '2.0.0', + versionType: 'release', + ); + final out = await run(extra: ' channel: release', dryRun: false); + expect(out.exitCode, 2); + expect(out.stdout, isNot(contains('via search'))); + expect(File(p.join(root.path, 'mods.lock')).existsSync(), isFalse); + }); + + test('hash mismatch also searches a different Modrinth project', () async { + exactProjects = candidates; + candidates = []; + mr.registerVersion( + slug: 'other', + versionNumber: '2.0.0', + requiredDeps: ['child'], + ); + mr.registerVersion(slug: 'child', versionNumber: '1.0.0'); + mr.projects['other'] = { + 'id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'project_type': 'mod', + }; + mr.projects['child'] = { + 'id': 'child_ID', + 'slug': 'child', + 'title': 'Child', + 'project_type': 'mod', + }; + final hashes = + (mr.versions['other']!.single['files'] as List).single['hashes'] as Map; + hashes['sha1'] = sha1 + .convert(mr.artifacts['other/other-2.0.0.jar']!) + .toString(); + files[42]!.single['hashes'] = [ + {'algo': 1, 'value': hashes['sha1']}, + ]; + files[42]!.single['downloadUrl'] = + '${mr.downloadBaseUrl}/other/other-2.0.0.jar'; + mr.searchHits = [ + { + 'project_id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'author': 'Author', + 'project_type': 'mod', + 'categories': ['neoforge'], + }, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('project_id: other_ID')); + expect(lock, contains(' child:')); + expect(mr.requestCounts['/v2/search'], 1); + }); + + test( + 'Modrinth slug fallback runs candidates through dependency solving', + () async { + exactProjects = candidates; + mr.versions.remove('anchor'); + mr.registerVersion( + slug: 'other', + versionNumber: '1.0.0', + requiredDeps: ['child'], + ); + mr.registerVersion(slug: 'child', versionNumber: '1.0.0'); + mr.projects['other'] = { + 'id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'project_type': 'mod', + }; + mr.projects['child'] = { + 'id': 'child_ID', + 'slug': 'child', + 'title': 'Child', + 'project_type': 'mod', + }; + (mr.versions['other']!.single['files'] as List).single['hashes']['sha1'] = + files[42]!.single['hashes'][0]['value']; + mr.searchHits = [ + { + 'project_id': 'other_ID', + 'slug': 'other', + 'title': 'Anchor', + 'author': 'Author', + 'project_type': 'mod', + 'categories': ['neoforge'], + }, + ]; + final out = await run(dryRun: false); + expect(out.exitCode, 0, reason: out.stderr); + final lock = File(p.join(root.path, 'mods.lock')).readAsStringSync(); + expect(lock, contains('project_id: other_ID')); + expect(lock, contains('discovered_via_search: true')); + expect(lock, contains(' child:')); + expect(mr.lastSearchQuery!['facets'], contains('versions:1.21.1')); + expect(mr.lastSearchQuery!['facets'], contains('categories:neoforge')); + }, + ); +} diff --git a/test/commands/server_distribution_manifest_test.dart b/test/commands/server_distribution_manifest_test.dart new file mode 100644 index 0000000..e3f729a --- /dev/null +++ b/test/commands/server_distribution_manifest_test.dart @@ -0,0 +1,96 @@ +import 'package:gitrinth/src/model/manifest/server_distribution.dart'; +import 'package:test/test.dart'; + +Map fixture() => { + 'formatVersion': 1, + 'artifactType': 'gitrinth-server-distribution', + 'slug': 'pack', + 'version': '1.0.0', + 'minecraft': '1.21.1', + 'loader': {'id': 'future-runtime', 'version': '1.2.3'}, + 'runtimeBundled': false, + 'files': [ + {'path': 'mods/server.jar', 'sha256': 'a' * 64}, + ], +}; + +void main() { + test( + 'physical distribution marker round trips independent archive identities', + () { + final raw = fixture(); + final parsed = ServerDistributionManifest.fromMap(raw); + expect(parsed.toMap(), raw); + expect(parsed.loaderId, 'future-runtime'); + expect(parsed.files.single.path, 'mods/server.jar'); + expect(() => parsed.files.clear(), throwsUnsupportedError); + }, + ); + + test('inventory rejects unsafe and private archive paths', () { + for (final path in [ + '../escape.jar', + '/absolute.jar', + r'C:\secret.jar', + 'mods/../escape.jar', + '.gitrinth/publications/receipt.json', + 'config/credentials.json', + '.env', + 'cache/data.json', + 'eula.txt', + 'gitrinth-server-distribution.json', + ]) { + final raw = fixture(); + raw['files'] = [ + {'path': path, 'sha256': 'a' * 64}, + ]; + expect( + () => ServerDistributionManifest.fromMap(raw), + throwsFormatException, + reason: path, + ); + } + final raw = fixture(); + raw['files'] = [ + {'path': 'mods/tokens.jar', 'sha256': 'a' * 64}, + ]; + expect( + ServerDistributionManifest.fromMap(raw).files.single.path, + 'mods/tokens.jar', + ); + }); + + test('inventory rejects duplicates and malformed hashes', () { + final duplicate = fixture(); + duplicate['files'] = [ + {'path': 'mods/server.jar', 'sha256': 'a' * 64}, + {'path': 'mods/SERVER.jar', 'sha256': 'b' * 64}, + ]; + expect( + () => ServerDistributionManifest.fromMap(duplicate), + throwsFormatException, + ); + final invalidHash = fixture(); + invalidHash['files'] = [ + {'path': 'mods/server.jar', 'sha256': 'not-a-sha256'}, + ]; + expect( + () => ServerDistributionManifest.fromMap(invalidHash), + throwsFormatException, + ); + }); + + test('marker cannot claim a bundled runtime or a manifest-only archive', () { + for (final change in [ + {'runtimeBundled': true}, + {'artifactType': 'minecraftModpack'}, + {'files': []}, + {'formatVersion': 2}, + ]) { + expect( + () => ServerDistributionManifest.fromMap({...fixture(), ...change}), + throwsFormatException, + ); + } + }); +} diff --git a/test/commands/server_distribution_test.dart b/test/commands/server_distribution_test.dart new file mode 100644 index 0000000..11d8c7a --- /dev/null +++ b/test/commands/server_distribution_test.dart @@ -0,0 +1,326 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/model/curseforge/cf_file_hash.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_metadata_store.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../helpers/capture.dart'; + +void main() { + late Directory root; + late Map environment; + setUp(() { + root = Directory.systemTemp.createTempSync('server_distribution_'); + environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + }; + for (final entry in { + 'server.jar': 'server-side mod', + 'client.jar': 'client-side mod', + 'settings.json': '{"setting":true}', + 'secret.txt': 'private fixture', + 'eula.txt': 'eula=true', + '.env': 'private environment fixture', + }.entries) { + File(p.join(root.path, entry.key)).writeAsStringSync(entry.value); + } + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'fabric:0.17.3'} +mods: + server-mod: {path: server.jar, client: unsupported} + client-mod: {path: client.jar, server: unsupported} +files: + config/settings.json: {path: settings.json} + config/client.json: {path: settings.json, server: unsupported} + .env: {path: secret.txt} + cache/token.json: {path: secret.txt} + eula.txt: {path: eula.txt} + config/renamed-auth.json: {path: .env} +'''); + }); + tearDown(() => root.deleteSync(recursive: true)); + Future pack(List flags) => runCli([ + '-C', + root.path, + 'pack', + '--offline', + ...flags, + ], environment: environment); + Archive archive(String name) => ZipDecoder().decodeBytes( + File(p.join(root.path, 'build', name)).readAsBytesSync(), + ); + + for (final target in ['.env', '.gitrinth/private.json']) { + test('private source alias $target is excluded', () async { + final private = File(p.join(root.path, target)); + private.parent.createSync(recursive: true); + private.writeAsStringSync('private source bytes'); + File(p.join(root.path, 'settings.json')).deleteSync(); + Link(p.join(root.path, 'settings.json')).createSync(private.path); + final result = await pack(['--curseforge', '--server-distribution']); + expect(result.exitCode, 0, reason: result.stderr); + final zip = archive('pack-1.0.0-server-distribution.zip'); + expect(zip.findFile('config/settings.json'), isNull); + }); + } + + test('nonprivate source aliases preserve selected config bytes', () async { + File( + p.join(root.path, 'settings.json'), + ).renameSync(p.join(root.path, 'settings-original.json')); + Link( + p.join(root.path, 'settings.json'), + ).createSync(p.join(root.path, 'settings-original.json')); + final result = await pack(['--curseforge', '--server-distribution']); + expect(result.exitCode, 0, reason: result.stderr); + expect( + utf8.decode( + archive( + 'pack-1.0.0-server-distribution.zip', + ).findFile('config/settings.json')!.content, + ), + '{"setting":true}', + ); + }); + test('legacy server ZIP remains a side-filtered manifest archive', () async { + final result = await pack(['--curseforge']); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final zip = archive('pack-1.0.0-server.zip'); + expect(zip.findFile('manifest.json'), isNotNull); + expect(zip.findFile('overrides/mods/server.jar'), isNotNull); + expect(zip.findFile('overrides/mods/client.jar'), isNull); + expect(zip.findFile('gitrinth-server-distribution.json'), isNull); + }); + + test( + 'server distribution contains physical server bytes and no private state', + () async { + final result = await pack(['--curseforge', '--server-distribution']); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final zip = archive('pack-1.0.0-server-distribution.zip'); + expect( + utf8.decode(zip.findFile('mods/server.jar')!.content), + 'server-side mod', + ); + expect( + utf8.decode(zip.findFile('config/settings.json')!.content), + '{"setting":true}', + ); + for (final excluded in [ + 'mods/client.jar', + 'config/client.json', + '.env', + 'cache/token.json', + 'eula.txt', + 'config/renamed-auth.json', + 'manifest.json', + 'overrides/mods/server.jar', + ]) { + expect(zip.findFile(excluded), isNull, reason: excluded); + } + final marker = jsonDecode( + utf8.decode(zip.findFile('gitrinth-server-distribution.json')!.content), + ); + expect(marker['artifactType'], 'gitrinth-server-distribution'); + expect(marker['formatVersion'], 1); + expect(marker['slug'], 'pack'); + expect(marker['version'], '1.0.0'); + expect(marker['minecraft'], '1.21.1'); + expect(marker['runtimeBundled'], isFalse); + expect( + marker['files'], + contains( + equals({ + 'path': 'mods/server.jar', + 'sha256': sha256.convert(utf8.encode('server-side mod')).toString(), + }), + ), + ); + expect(result.stdout, contains('runtime is not bundled')); + expect( + File(p.join(root.path, 'build', 'pack-1.0.0.zip')).existsSync(), + isFalse, + ); + }, + ); + + test( + 'distribution flag requires CurseForge and a single server artifact', + () async { + for (final flags in [ + ['--server-distribution'], + ['--curseforge', '--server-distribution', '--combined'], + ]) { + final result = await pack(flags); + expect(result.exitCode, 64, reason: result.stderr); + expect( + result.stderr, + contains( + flags.length == 1 + ? '--server-distribution requires --curseforge' + : '--server-distribution cannot be combined with --combined', + ), + ); + expect(Directory(p.join(root.path, 'build')).existsSync(), isFalse); + } + }, + ); + + test( + 'metadata-only loader can distribute bytes without an archive token or runtime', + () async { + final manifest = File(p.join(root.path, 'mods.yaml')); + manifest.writeAsStringSync( + manifest + .readAsStringSync() + .replaceFirst('fabric:0.17.3', 'future-runtime:1.2.3') + .replaceFirst( + 'mc_version: 1.21.1', + 'mc_version: 1.21.1\ngitrinth: {modrinth: disabled, curseforge: disabled}', + ), + ); + final descriptors = File(p.join(root.path, 'loaders.yaml')) + ..writeAsStringSync( + 'version: 1\nloaders:\n future-runtime:\n role: mod\n', + ); + final result = await pack([ + '--curseforge', + '--server-distribution', + '--loader-descriptors', + descriptors.path, + '--output', + p.join('dist', 'server.zip'), + ]); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final zip = ZipDecoder().decodeBytes( + File(p.join(root.path, 'dist', 'server.zip')).readAsBytesSync(), + ); + final marker = jsonDecode( + utf8.decode(zip.findFile('gitrinth-server-distribution.json')!.content), + ); + expect(marker['loader'], {'id': 'future-runtime', 'version': '1.2.3'}); + expect(marker['runtimeBundled'], isFalse); + expect(zip.findFile('mods/server.jar'), isNotNull); + expect(result.stdout, contains('runtime is not bundled')); + }, + ); + + test( + 'explicit CurseForge redistribution denial blocks physical bytes', + () async { + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'fabric:0.17.3'} +gitrinth: {modrinth: disabled, curseforge: enabled} +mods: {restricted: {curseforge: '42'}} +'''); + final bytes = utf8.encode('restricted mod'); + final hash = sha1.convert(bytes).toString(); + final cache = GitrinthCache(root: environment['GITRINTH_CACHE']!); + final cached = File( + cache.curseforgePath( + projectId: 42, + fileId: 420, + filename: 'restricted.jar', + ), + ); + cached.parent.createSync(recursive: true); + cached.writeAsBytesSync(bytes); + CurseForgeMetadataStore(cache).write( + ModFile( + id: 420, + modId: 42, + displayName: '1.0.0', + fileName: 'restricted.jar', + releaseType: 1, + fileDate: '', + gameVersions: ['1.21.1'], + hashes: [FileHash(value: hash, algoCode: 1)], + dependencies: [], + ), + ); + File(p.join(root.path, 'mods.lock')).writeAsStringSync( + emitModsLock( + ModsLock( + gitrinthVersion: '0.1.0', + mcVersion: '1.21.1', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.17.3', + ), + mods: { + 'restricted': LockedEntry( + slug: 'restricted', + kind: LockedEntryKind.resolved, + version: '1.0.0', + curseforge: LockedCurseforgeSource( + projectId: 42, + fileId: 420, + allowModDistribution: false, + file: LockedFile(name: 'restricted.jar', sha1: hash), + gameVersions: ['1.21.1'], + ), + ), + }, + ), + ), + ); + final result = await pack(['--curseforge', '--server-distribution']); + expect(result.exitCode, 2, reason: '${result.stdout}\n${result.stderr}'); + expect(result.stderr, allOf(contains('42'), contains('redistribution'))); + expect( + File( + p.join(root.path, 'build', 'pack-1.0.0-server-distribution.zip'), + ).existsSync(), + isFalse, + ); + }, + ); + + test('plugin-server distribution retains its runtime identity', () async { + final manifest = File(p.join(root.path, 'mods.yaml')); + manifest.writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {plugins: 'paper:100'} +plugins: + server-plugin: {path: server.jar} +files: + config/settings.json: {path: settings.json} +'''); + final result = await pack(['--curseforge', '--server-distribution']); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final zip = archive('pack-1.0.0-server-distribution.zip'); + final marker = jsonDecode( + utf8.decode(zip.findFile('gitrinth-server-distribution.json')!.content), + ); + expect(marker['loader'], { + 'id': 'vanilla', + 'version': null, + 'plugins': {'id': 'paper', 'version': '100'}, + }); + expect(zip.findFile('plugins/server.jar'), isNotNull); + }); +} diff --git a/test/commands/unpin_command_test.dart b/test/commands/unpin_command_test.dart index a921899..f89cb59 100644 --- a/test/commands/unpin_command_test.dart +++ b/test/commands/unpin_command_test.dart @@ -33,7 +33,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: 19.27.0.340 '''); @@ -52,7 +52,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: iris: version: 1.8.12 @@ -75,7 +75,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^19.27.0.340 '''); @@ -92,7 +92,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: release '''); @@ -109,7 +109,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: 6.0.10+mc1.21.1 '''); @@ -127,7 +127,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: 19.27.0.340 '''); @@ -148,7 +148,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: terralith: 2.5.8 data_packs: diff --git a/test/commands/upgrade_command_test.dart b/test/commands/upgrade_command_test.dart index d3d742a..c22dcfc 100644 --- a/test/commands/upgrade_command_test.dart +++ b/test/commands/upgrade_command_test.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:gitrinth/src/service/cache.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -57,7 +58,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^2.0.0 @@ -86,7 +87,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 b: ^2.0.0 @@ -116,7 +117,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 '''); @@ -143,7 +144,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 '''); @@ -170,7 +171,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 '''); @@ -193,7 +194,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 '''); @@ -216,7 +217,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 '''); @@ -247,7 +248,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: release '''); @@ -274,7 +275,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^6.0.10 b: ^1.0.0 @@ -304,7 +305,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -334,7 +335,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -353,7 +354,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 '''); @@ -373,7 +374,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: ^1.0.0 custom: @@ -417,7 +418,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: version: ^1.0.0 @@ -441,7 +442,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: version: ^1.0.0 @@ -495,7 +496,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: resource_packs: faithful-32x: ^1.21.1 @@ -536,7 +537,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: resource_packs: faithful-32x: ^1.21.1-april-2025 @@ -581,7 +582,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 '''); @@ -601,14 +602,12 @@ mods: expect(lock, contains('version: 1.5.0')); expect( lock, - contains(RegExp(r'bar:\s+source: modrinth\s+version: 1\.0\.0\b')), + contains(RegExp(r'bar:\s+kind: resolved\s+version: 1\.0\.0\b')), reason: 'bar must stay at 1.0.0', ); expect( lock, - isNot( - contains(RegExp(r'bar:\s+source: modrinth\s+version: 1\.5\.0')), - ), + isNot(contains(RegExp(r'bar:\s+kind: resolved\s+version: 1\.5\.0'))), reason: 'bar must stay at 1.0.0', ); }, @@ -630,7 +629,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 baz: ^1.0.0 @@ -652,15 +651,15 @@ mods: // foo and bar both move; baz (root, unrelated) keeps its pin. expect( lock, - contains(RegExp(r'foo:\s+source: modrinth\s+version: 1\.5\.0')), + contains(RegExp(r'foo:\s+kind: resolved\s+version: 1\.5\.0')), ); expect( lock, - contains(RegExp(r'bar:\s+source: modrinth\s+version: 1\.5\.0')), + contains(RegExp(r'bar:\s+kind: resolved\s+version: 1\.5\.0')), ); expect( lock, - contains(RegExp(r'baz:\s+source: modrinth\s+version: 1\.0\.0')), + contains(RegExp(r'baz:\s+kind: resolved\s+version: 1\.0\.0')), ); }); @@ -681,7 +680,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 '''); @@ -700,11 +699,11 @@ mods: final lock = readLock(); expect( lock, - contains(RegExp(r'foo:\s+source: modrinth\s+version: 2\.0\.0')), + contains(RegExp(r'foo:\s+kind: resolved\s+version: 2\.0\.0')), ); expect( lock, - contains(RegExp(r'bar:\s+source: modrinth\s+version: 1\.0\.0')), + contains(RegExp(r'bar:\s+kind: resolved\s+version: 1\.0\.0')), reason: 'bar must stay locked at 1.0.0', ); expect(readManifest(), contains('foo: ^2.0.0')); @@ -729,7 +728,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 baz: ^1.0.0 @@ -754,15 +753,15 @@ mods: final lock = readLock(); expect( lock, - contains(RegExp(r'foo:\s+source: modrinth\s+version: 2\.0\.0')), + contains(RegExp(r'foo:\s+kind: resolved\s+version: 2\.0\.0')), ); expect( lock, - contains(RegExp(r'bar:\s+source: modrinth\s+version: 1\.5\.0')), + contains(RegExp(r'bar:\s+kind: resolved\s+version: 1\.5\.0')), ); expect( lock, - contains(RegExp(r'baz:\s+source: modrinth\s+version: 1\.0\.0')), + contains(RegExp(r'baz:\s+kind: resolved\s+version: 1\.0\.0')), reason: 'baz is not in foo\'s closure; must stay locked', ); }, @@ -791,7 +790,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 bar: ^0.1.0 @@ -821,7 +820,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 bar: ^1.0.0 @@ -853,7 +852,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 bar: ^1.0.0 @@ -891,7 +890,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 bar: ^1.0.0 @@ -902,11 +901,13 @@ mods: // cycle. The resolver would not normally produce one (Modrinth // deps are acyclic in practice), but a corrupt cache or a // hand-edited file can — the closure walker must terminate. + final cycleHostSeg = GitrinthCache.hostCacheSegment(modrinth.baseUrl); void writeCachedDeps(String slug, String depProjectId) { final pid = '${slug}_ID'; final vid = '${slug}_1_0_0'; - final dir = Directory(p.join(cacheDir.path, 'modrinth', pid, vid)) - ..createSync(recursive: true); + final dir = Directory( + p.join(cacheDir.path, 'modrinth', cycleHostSeg, pid, vid), + )..createSync(recursive: true); File(p.join(dir.path, 'version.json')).writeAsStringSync( '{"dependencies":[{"project_id":"$depProjectId",' '"dependency_type":"required"}]}', @@ -944,7 +945,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: foo: ^1.0.0 bar: ^1.0.0 @@ -982,7 +983,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: gitrinth:disabled-by-conflict b: ^1.0.0 @@ -1008,7 +1009,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: a: gitrinth:disabled-by-conflict b: ^1.0.0 diff --git a/test/commands/upgrade_offline_test.dart b/test/commands/upgrade_offline_test.dart index 9e103c1..72a2b57 100644 --- a/test/commands/upgrade_offline_test.dart +++ b/test/commands/upgrade_offline_test.dart @@ -71,7 +71,7 @@ version: 0.1.0 description: x loader: mods: "neoforge:21.1.50" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.0.0 '''); diff --git a/test/fixtures/curseforge/mod_distribution_disabled.json b/test/fixtures/curseforge/mod_distribution_disabled.json new file mode 100644 index 0000000..beab0cd --- /dev/null +++ b/test/fixtures/curseforge/mod_distribution_disabled.json @@ -0,0 +1,62 @@ +{ + "data": [ + { + "id": 555555, + "gameId": 432, + "name": "Closed Source Mod", + "slug": "closed-source-mod", + "summary": "A mod whose author opted out of API distribution.", + "status": 4, + "downloadCount": 12345, + "isFeatured": false, + "primaryCategoryId": 423, + "categories": [], + "classId": 6, + "authors": [{"id": 99, "name": "someauthor", "url": "https://example.com/someauthor"}], + "logo": null, + "screenshots": [], + "mainFileId": 4000000, + "latestFiles": [ + { + "id": 4000000, + "gameId": 432, + "modId": 555555, + "isAvailable": true, + "displayName": "closed-source-mod-1.0.0.jar", + "fileName": "closed-source-mod-1.0.0.jar", + "releaseType": 1, + "fileStatus": 4, + "hashes": [ + {"value": "feedfeedfeedfeedfeedfeedfeedfeedfeedfeed", "algo": 1} + ], + "fileDate": "2025-09-01T00:00:00Z", + "fileLength": 1000000, + "downloadCount": 100, + "downloadUrl": null, + "gameVersions": ["1.21.1", "Fabric"], + "sortableGameVersions": [], + "dependencies": [], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 0, + "modules": [] + } + ], + "latestFilesIndexes": [], + "latestEarlyAccessFilesIndexes": [], + "dateCreated": "2024-01-01T00:00:00Z", + "dateModified": "2025-09-01T00:00:00Z", + "dateReleased": "2025-09-01T00:00:00Z", + "allowModDistribution": false, + "gamePopularityRank": 1000, + "isAvailable": true, + "thumbsUpCount": 0 + } + ], + "pagination": { + "index": 0, + "pageSize": 50, + "resultCount": 1, + "totalCount": 1 + } +} diff --git a/test/fixtures/curseforge/mod_files_response.json b/test/fixtures/curseforge/mod_files_response.json new file mode 100644 index 0000000..5a5b9f2 --- /dev/null +++ b/test/fixtures/curseforge/mod_files_response.json @@ -0,0 +1,134 @@ +{ + "data": [ + { + "id": 5814200, + "gameId": 432, + "modId": 238222, + "isAvailable": true, + "displayName": "jei-1.21.1-fabric-19.27.0.340.jar", + "fileName": "jei-1.21.1-fabric-19.27.0.340.jar", + "releaseType": 1, + "fileStatus": 4, + "hashes": [ + {"value": "abcd1234abcd1234abcd1234abcd1234abcd1234", "algo": 1}, + {"value": "1234abcd1234abcd1234abcd1234abcd", "algo": 2} + ], + "fileDate": "2025-08-15T12:00:00Z", + "fileLength": 2098765, + "downloadCount": 12345, + "downloadUrl": "https://edge.forgecdn.net/files/5814/200/jei-1.21.1-fabric-19.27.0.340.jar", + "gameVersions": ["1.21.1", "Fabric"], + "sortableGameVersions": [], + "dependencies": [ + {"modId": 306612, "relationType": 3}, + {"modId": 999999, "relationType": 2} + ], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 12345678, + "modules": [] + }, + { + "id": 5814100, + "gameId": 432, + "modId": 238222, + "isAvailable": true, + "displayName": "jei-1.21.1-forge-19.26.0.340.jar", + "fileName": "jei-1.21.1-forge-19.26.0.340.jar", + "releaseType": 1, + "fileStatus": 4, + "hashes": [ + {"value": "0011223344556677889900112233445566778899", "algo": 1} + ], + "fileDate": "2025-08-10T10:00:00Z", + "fileLength": 2090000, + "downloadCount": 9876, + "downloadUrl": "https://edge.forgecdn.net/files/5814/100/jei-1.21.1-forge-19.26.0.340.jar", + "gameVersions": ["1.21.1", "Forge"], + "sortableGameVersions": [], + "dependencies": [], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 12345679, + "modules": [] + }, + { + "id": 5814050, + "gameId": 432, + "modId": 238222, + "isAvailable": true, + "displayName": "jei-1.21.1-fabric-19.27.0.340-beta.jar", + "fileName": "jei-1.21.1-fabric-19.27.0.340-beta.jar", + "releaseType": 2, + "fileStatus": 4, + "hashes": [ + {"value": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", "algo": 1} + ], + "fileDate": "2025-08-05T08:00:00Z", + "fileLength": 2080000, + "downloadCount": 1234, + "downloadUrl": "https://edge.forgecdn.net/files/5814/050/jei-1.21.1-fabric-19.27.0.340-beta.jar", + "gameVersions": ["1.21.1", "Fabric"], + "sortableGameVersions": [], + "dependencies": [], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 12345680, + "modules": [] + }, + { + "id": 5810000, + "gameId": 432, + "modId": 238222, + "isAvailable": true, + "displayName": "jei-1.21-fabric-19.20.0.0.jar", + "fileName": "jei-1.21-fabric-19.20.0.0.jar", + "releaseType": 1, + "fileStatus": 4, + "hashes": [ + {"value": "cafebabecafebabecafebabecafebabecafebabe", "algo": 1} + ], + "fileDate": "2025-07-01T08:00:00Z", + "fileLength": 2050000, + "downloadCount": 50000, + "downloadUrl": "https://edge.forgecdn.net/files/5810/000/jei-1.21-fabric-19.20.0.0.jar", + "gameVersions": ["1.21", "Fabric"], + "sortableGameVersions": [], + "dependencies": [], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 12345681, + "modules": [] + }, + { + "id": 5809000, + "gameId": 432, + "modId": 238222, + "isAvailable": true, + "displayName": "jei-1.21.1-fabric-19.19.0.0-alpha.jar", + "fileName": "jei-1.21.1-fabric-19.19.0.0-alpha.jar", + "releaseType": 3, + "fileStatus": 4, + "hashes": [ + {"value": "facefeedfacefeedfacefeedfacefeedfacefeed", "algo": 1} + ], + "fileDate": "2025-06-15T08:00:00Z", + "fileLength": 2040000, + "downloadCount": 500, + "downloadUrl": "https://edge.forgecdn.net/files/5809/000/jei-1.21.1-fabric-19.19.0.0-alpha.jar", + "gameVersions": ["1.21.1", "Fabric"], + "sortableGameVersions": [], + "dependencies": [], + "alternateFileId": 0, + "isServerPack": false, + "fileFingerprint": 12345682, + "modules": [] + } + ], + "pagination": { + "index": 0, + "pageSize": 50, + "resultCount": 5, + "totalCount": 5 + } +} diff --git a/test/fixtures/curseforge/mod_search_response.json b/test/fixtures/curseforge/mod_search_response.json new file mode 100644 index 0000000..f7f3cba --- /dev/null +++ b/test/fixtures/curseforge/mod_search_response.json @@ -0,0 +1,37 @@ +{ + "data": [ + { + "id": 238222, + "gameId": 432, + "name": "Just Enough Items (JEI)", + "slug": "jei", + "summary": "JEI is an item and recipe viewing mod for Minecraft, built from the ground up for stability and performance.", + "status": 4, + "downloadCount": 999999999, + "isFeatured": false, + "primaryCategoryId": 423, + "categories": [], + "classId": 6, + "authors": [{"id": 1, "name": "mezz", "url": "https://example.com/mezz"}], + "logo": null, + "screenshots": [], + "mainFileId": 5814200, + "latestFiles": [], + "latestFilesIndexes": [], + "latestEarlyAccessFilesIndexes": [], + "dateCreated": "2014-09-04T00:00:00Z", + "dateModified": "2025-08-15T12:00:00Z", + "dateReleased": "2025-08-15T12:00:00Z", + "allowModDistribution": true, + "gamePopularityRank": 1, + "isAvailable": true, + "thumbsUpCount": 1234567 + } + ], + "pagination": { + "index": 0, + "pageSize": 50, + "resultCount": 1, + "totalCount": 1 + } +} diff --git a/test/helpers/fake_modrinth.dart b/test/helpers/fake_modrinth.dart index f0552b6..e753812 100644 --- a/test/helpers/fake_modrinth.dart +++ b/test/helpers/fake_modrinth.dart @@ -25,6 +25,14 @@ class FakeModrinth { /// Extra slugs treated as taken by `/v2/project//check`. final Set _takenSlugs = {}; + final List> loaderTags = [ + { + 'name': 'neoforge', + 'supported_project_types': ['mod', 'modpack'], + 'icon': '', + }, + ]; + /// Versions returned by `/v2/tag/game_version`. List> gameVersions = [ { @@ -58,6 +66,8 @@ class FakeModrinth { /// Request count by route. final Map requestCounts = {}; + List> searchHits = []; + Map? lastSearchQuery; FakeModrinth({ Map>? projects, @@ -70,6 +80,9 @@ class FakeModrinth { String get baseUrl => 'http://127.0.0.1:${_server.port}/v2'; String get downloadBaseUrl => 'http://127.0.0.1:${_server.port}/downloads'; + String get paperBuildsUrl => + 'http://127.0.0.1:${_server.port}/paper/{project}/{mc}/builds'; + /// Fake fabric-meta loader-list URL. String get fabricMetaUrl => 'http://127.0.0.1:${_server.port}/fabric/v2/versions/loader'; @@ -311,9 +324,32 @@ class FakeModrinth { jsonEncode({'id': 'fake-user-id', 'username': username}), ); } + } else if (path == '/v2/search') { + lastSearchQuery = req.uri.queryParameters; + req.response.headers.contentType = ContentType.json; + req.response.write(jsonEncode({'hits': searchHits})); + } else if (path == '/v2/tag/loader') { + req.response.headers.contentType = ContentType.json; + req.response.write(jsonEncode(loaderTags)); } else if (path == '/v2/tag/game_version') { req.response.headers.contentType = ContentType.json; req.response.write(jsonEncode(gameVersions)); + } else if (path.startsWith('/paper/') && path.endsWith('/builds')) { + req.response.headers.contentType = ContentType.json; + req.response.write( + jsonEncode([ + { + 'id': 100, + 'channel': 'STABLE', + 'downloads': { + 'server:default': { + 'name': 'paper-100.jar', + 'url': '$downloadBaseUrl/paper/paper-100.jar', + }, + }, + }, + ]), + ); } else if (path == '/fabric/v2/versions/loader') { req.response.headers.contentType = ContentType.json; req.response.write(jsonEncode(fabricLoaderVersions)); @@ -469,7 +505,12 @@ class FakeModrinth { lastVersionQuery[slug] = Map.from( req.uri.queryParameters, ); - final list = versions[slug]; + final list = + versions[slug] ?? + versions[projects.values + .where((p) => p['id'] == slug) + .map((p) => p['slug']) + .firstOrNull]; if (list == null) { req.response.statusCode = 404; req.response.write(jsonEncode({'error': 'unknown slug $slug'})); diff --git a/test/helpers/java_runtime_contender.dart b/test/helpers/java_runtime_contender.dart new file mode 100644 index 0000000..456eefb --- /dev/null +++ b/test/helpers/java_runtime_contender.dart @@ -0,0 +1,41 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/downloader.dart'; +import 'package:gitrinth/src/service/java_runtime_fetcher.dart'; +import 'package:gitrinth/src/util/host_platform.dart'; + +Future main(List args) async { + debugHostPlatformOverride = const HostPlatform(os: 'windows', arch: 'x64'); + final cache = GitrinthCache(root: args.single); + final dio = Dio(); + dio.interceptors.add( + InterceptorsWrapper( + onRequest: (request, handler) { + handler.reject( + DioException( + requestOptions: request, + error: StateError( + 'A locked contender must not access the network.', + ), + ), + ); + }, + ), + ); + try { + await JavaRuntimeFetcher( + cache: cache, + downloader: Downloader(dio: dio, cache: cache), + ).ensureRuntime(21); + throw StateError('The incomplete runtime is owned by another process.'); + } on FileSystemException catch (error) { + stdout.writeln( + jsonEncode({'status': 'lock-refused', 'message': error.message}), + ); + } finally { + dio.close(force: true); + } +} diff --git a/test/helpers/multipart.dart b/test/helpers/multipart.dart new file mode 100644 index 0000000..a64a81c --- /dev/null +++ b/test/helpers/multipart.dart @@ -0,0 +1,69 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:archive/archive.dart'; +import 'package:mime/mime.dart'; + +/// One decoded `multipart/form-data` part as the server received it. +class ReceivedPart { + final String name; + final String? filename; + final String? contentType; + final Uint8List bytes; + + const ReceivedPart({ + required this.name, + required this.filename, + required this.contentType, + required this.bytes, + }); + + String get text => utf8.decode(bytes); + + Map get json => jsonDecode(text) as Map; + + /// Decodes the part as a ZIP archive, keyed by entry name. + Map> get zipEntries { + final archive = ZipDecoder().decodeBytes(bytes); + return { + for (final file in archive.files) + if (file.isFile) file.name: file.content as List, + }; + } +} + +/// Parses a real multipart request body, keyed by form field name. +/// +/// Fails loudly on a non-multipart request or a duplicated field so a +/// receiver cannot silently accept a malformed upload. +Future> readMultipart(HttpRequest request) async { + final contentType = request.headers.contentType; + final boundary = contentType?.parameters['boundary']; + if (contentType?.mimeType != 'multipart/form-data' || boundary == null) { + throw StateError('Expected multipart/form-data, got $contentType.'); + } + final parts = {}; + await for (final part in MimeMultipartTransformer(boundary).bind(request)) { + final disposition = HeaderValue.parse( + part.headers['content-disposition'] ?? '', + preserveBackslash: true, + ); + final name = disposition.parameters['name']; + if (name == null) throw StateError('Multipart part without a name.'); + if (parts.containsKey(name)) { + throw StateError('Duplicate multipart field "$name".'); + } + final builder = BytesBuilder(copy: false); + await for (final chunk in part) { + builder.add(chunk); + } + parts[name] = ReceivedPart( + name: name, + filename: disposition.parameters['filename'], + contentType: part.headers['content-type'], + bytes: builder.takeBytes(), + ); + } + return parts; +} diff --git a/test/manifest/declared_plugin_loader_resolution_test.dart b/test/manifest/declared_plugin_loader_resolution_test.dart index bc8290b..2bcdfe1 100644 --- a/test/manifest/declared_plugin_loader_resolution_test.dart +++ b/test/manifest/declared_plugin_loader_resolution_test.dart @@ -12,7 +12,7 @@ description: x loader: mods: $mods plugins: $plugins -mc-version: 1.21.1 +mc_version: 1.21.1 '''; String yamlNoMods({required String plugins}) => @@ -23,7 +23,7 @@ version: 0.1.0 description: x loader: plugins: $plugins -mc-version: 1.21.1 +mc_version: 1.21.1 '''; group('DeclaredPluginLoader resolution truth table', () { @@ -82,7 +82,7 @@ description: x loader: mods: fabric plugins: sponge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: version: ^1.0.0 @@ -98,7 +98,7 @@ mods: group('DeclaredPluginLoaderResolution.resolveWith', () { test('non-sponge declared loaders pass through every mod loader', () { - const passthrough = { + final passthrough = { DeclaredPluginLoader.bukkit: PluginLoader.bukkit, DeclaredPluginLoader.folia: PluginLoader.folia, DeclaredPluginLoader.paper: PluginLoader.paper, diff --git a/test/manifest/loader_mods_optional_test.dart b/test/manifest/loader_mods_optional_test.dart index 99d903f..600e45d 100644 --- a/test/manifest/loader_mods_optional_test.dart +++ b/test/manifest/loader_mods_optional_test.dart @@ -15,7 +15,7 @@ version: 0.1.0 description: x loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.loader.mods, ModLoader.vanilla); @@ -30,7 +30,7 @@ slug: pack name: Pack version: 0.1.0 description: x -mc-version: 1.21.1 +mc_version: 1.21.1 '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.loader.mods, ModLoader.vanilla); @@ -45,7 +45,7 @@ version: 0.1.0 description: x loader: mods: vanilla -mc-version: 1.21.1 +mc_version: 1.21.1 '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.loader.mods, ModLoader.vanilla); @@ -60,7 +60,7 @@ version: 0.1.0 description: x loader: mods: vanilla:1.0 -mc-version: 1.21.1 +mc_version: 1.21.1 '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -82,7 +82,7 @@ version: 0.1.0 description: x loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: release '''; @@ -106,7 +106,7 @@ version: 0.1.0 description: x loader: plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: plugins: luckperms: release diff --git a/test/manifest/loader_plugins_parser_test.dart b/test/manifest/loader_plugins_parser_test.dart index a348f81..0e55f88 100644 --- a/test/manifest/loader_plugins_parser_test.dart +++ b/test/manifest/loader_plugins_parser_test.dart @@ -16,7 +16,7 @@ description: x loader: mods: $mods plugins: $pluginsValue -mc-version: 1.21.1 +mc_version: 1.21.1 '''; group('loader.plugins parsing', () { @@ -36,17 +36,20 @@ mc-version: 1.21.1 expect(manifest.loader.plugins, PluginLoader.spongeforge); }); - test('rejects an unknown value with a message listing every loader', () { - try { - parseModsYaml(yamlWithLoaderPlugins('bogus'), filePath: 'mods.yaml'); - fail('expected a parse error'); - } on ValidationError catch (e) { - expect(e.message, contains('bukkit')); - expect(e.message, contains('folia')); - expect(e.message, contains('paper')); - expect(e.message, contains('spigot')); - expect(e.message, contains('sponge')); - } + test('rejects a malformed plugin loader identity', () { + expect( + () => parseModsYaml( + yamlWithLoaderPlugins('bad/loader'), + filePath: 'mods.yaml', + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('Invalid lowercase loader identity'), + ), + ), + ); }); test('rejects a non-empty plugins: section with no loader.plugins', () { @@ -57,7 +60,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 plugins: luckperms: '''; @@ -121,7 +124,7 @@ description: x loader: mods: neoforge plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: version: ^1.0.0 @@ -143,7 +146,7 @@ description: x loader: mods: forge plugins: sponge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: version: ^1.0.0 @@ -163,7 +166,7 @@ description: x loader: mods: neoforge plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 plugins: luckperms: version: ^5.0.0 diff --git a/test/manifest/loader_ref_test.dart b/test/manifest/loader_ref_test.dart index a4b91cb..86dfca5 100644 --- a/test/manifest/loader_ref_test.dart +++ b/test/manifest/loader_ref_test.dart @@ -82,10 +82,10 @@ void main() { expect(msg, contains('more than one')); }); - test('rejects an unknown loader name', () { - final msg = _captureError('quilt'); - expect(msg, contains('quilt')); - expect(msg, contains('not a recognized loader')); + test('rejects a malformed loader identity', () { + final msg = _captureError('bad/loader'); + expect(msg, contains('bad/loader')); + expect(msg, contains('Invalid lowercase loader identity')); }); }); } diff --git a/test/manifest/open_loader_ids_test.dart b/test/manifest/open_loader_ids_test.dart new file mode 100644 index 0000000..641addb --- /dev/null +++ b/test/manifest/open_loader_ids_test.dart @@ -0,0 +1,31 @@ +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:test/test.dart'; + +void main() { + test('future loader identities survive manifest and lock parsing', () { + const manifest = ''' +slug: future +name: Future +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'future-loader:1.2.3', shaders: future-shader} +'''; + final yaml = parseModsYaml(manifest, filePath: 'mods.yaml'); + expect(yaml.loader.mods.name, 'future-loader'); + expect(yaml.loader.shaders?.name, 'future-shader'); + const lock = ''' +gitrinth-version: 0.1.0 +mc_version: 1.21.1 +loader: {mods: 'future-loader:1.2.3', plugins: 'future-plugin:2.0.0'} +'''; + final parsed = parseModsLock(lock, filePath: 'mods.lock'); + final emitted = emitModsLock(parsed); + final roundtrip = parseModsLock(emitted, filePath: 'mods.lock'); + expect(roundtrip.loader.mods.name, 'future-loader'); + expect(roundtrip.loader.plugins?.name, 'future-plugin'); + expect(roundtrip.loader.modLoaderVersion, '1.2.3'); + expect(roundtrip.loader.pluginLoaderVersion, '2.0.0'); + }); +} diff --git a/test/model/curseforge/cf_mod_file_mapper_test.dart b/test/model/curseforge/cf_mod_file_mapper_test.dart new file mode 100644 index 0000000..6ddedc4 --- /dev/null +++ b/test/model/curseforge/cf_mod_file_mapper_test.dart @@ -0,0 +1,174 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:gitrinth/src/model/curseforge/cf_file_hash.dart'; +import 'package:gitrinth/src/model/curseforge/cf_file_relation.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/model/curseforge/cf_search_response.dart'; +import 'package:test/test.dart'; + +const String _filesFixturePath = + 'test/fixtures/curseforge/mod_files_response.json'; +const String _searchFixturePath = + 'test/fixtures/curseforge/mod_search_response.json'; +const String _disabledFixturePath = + 'test/fixtures/curseforge/mod_distribution_disabled.json'; + +Map _readJson(String path) { + final raw = File(path).readAsStringSync(); + return jsonDecode(raw) as Map; +} + +void main() { + group('ModFile round-trip', () { + test('fixture deserializes and re-serializes without loss', () { + final raw = _readJson(_filesFixturePath); + final response = ModFileSearchResponseMapper.fromMap(raw); + expect(response.data, hasLength(5)); + + final encoded = response.toMap(); + // Round-trip should preserve the data array shape. + expect(encoded['data'], isA()); + expect((encoded['data'] as List), hasLength(5)); + + // Re-decode and re-encode to verify stability of the mapping. + final reparsed = ModFileSearchResponseMapper.fromMap(encoded); + expect(reparsed.data.first.id, response.data.first.id); + expect(reparsed.data.first.fileName, response.data.first.fileName); + expect( + reparsed.data.first.dependencies.length, + response.data.first.dependencies.length, + ); + }); + }); + + group('ModFile.sha1Hash', () { + test('returns the lowercased sha1 hash from the hashes array', () { + final raw = _readJson(_filesFixturePath); + final response = ModFileSearchResponseMapper.fromMap(raw); + final firstFile = response.data.first; + expect(firstFile.sha1Hash, 'abcd1234abcd1234abcd1234abcd1234abcd1234'); + }); + + test('returns null when no sha1 entry is present', () { + const file = ModFile( + id: 1, + modId: 1, + displayName: 'x', + fileName: 'x', + releaseType: 1, + fileDate: '2025-01-01T00:00:00Z', + gameVersions: [], + hashes: [FileHash(value: 'aaa', algoCode: 2)], + dependencies: [], + ); + expect(file.sha1Hash, isNull); + }); + + test('lowercases mixed-case digests', () { + const file = ModFile( + id: 1, + modId: 1, + displayName: 'x', + fileName: 'x', + releaseType: 1, + fileDate: '2025-01-01T00:00:00Z', + gameVersions: [], + hashes: [FileHash(value: 'ABCDEF', algoCode: 1)], + dependencies: [], + ); + expect(file.sha1Hash, 'abcdef'); + }); + }); + + group('ModFile.requiredDependencies', () { + test('filters dependencies to relationType == requiredDependency', () { + final raw = _readJson(_filesFixturePath); + final response = ModFileSearchResponseMapper.fromMap(raw); + final firstFile = response.data.first; + final required = firstFile.requiredDependencies.toList(); + expect(required, hasLength(1)); + expect(required.single.modId, 306612); + expect(required.single.relationType, RelationType.requiredDependency); + }); + + test('relationType decodes unknown codes as RelationType.unknown', () { + const rel = FileRelation(modId: 1, relationTypeCode: 99); + expect(rel.relationType, RelationType.unknown); + }); + }); + + group('Mod / ModSearchResponse', () { + test('search-response fixture deserializes one Mod', () { + final raw = _readJson(_searchFixturePath); + final response = ModSearchResponseMapper.fromMap(raw); + expect(response.data, hasLength(1)); + final mod = response.data.single; + expect(mod.id, 238222); + expect(mod.slug, 'jei'); + expect(mod.classId, 6); + expect(mod.allowModDistribution, isTrue); + }); + + test('nullable content type decodes without inventing a section', () { + final mod = ModMapper.fromMap({ + 'id': 1, + 'gameId': 432, + 'name': 'Unknown', + 'slug': 'unknown', + 'classId': null, + }); + expect(mod.classId, isNull); + }); + + test('nullable distribution flag preserves the legacy default', () { + for (final flag in [null, true, false]) { + final mod = ModMapper.fromMap({ + 'id': 1, + 'gameId': 432, + 'name': 'Example', + 'slug': 'example', + 'classId': 6, + 'allowModDistribution': flag, + }); + expect(mod.allowModDistribution, flag ?? true); + } + }); + + test('parses allowModDistribution: false', () { + final raw = _readJson(_disabledFixturePath); + final response = ModSearchResponseMapper.fromMap(raw); + expect(response.data.single.allowModDistribution, isFalse); + expect(response.data.single.latestFiles.single.downloadUrl, isNull); + }); + + test('defaults allowModDistribution to true when the field is missing', () { + final mod = ModMapper.fromMap({ + 'id': 1, + 'gameId': 432, + 'name': 'No-distribution-flag mod', + 'slug': 'noflag', + 'classId': 6, + }); + expect(mod.allowModDistribution, isTrue); + }); + }); + + group('HashAlgo decoding', () { + test('algoCode 1 → sha1', () { + const hash = FileHash(value: 'abc', algoCode: 1); + expect(hash.algo, HashAlgo.sha1); + }); + + test('algoCode 2 → md5', () { + const hash = FileHash(value: 'abc', algoCode: 2); + expect(hash.algo, HashAlgo.md5); + }); + + test('unknown algoCode → unknown', () { + const hash = FileHash(value: 'abc', algoCode: 99); + expect(hash.algo, HashAlgo.unknown); + }); + }); +} diff --git a/test/model/manifest/emitter_test.dart b/test/model/manifest/emitter_test.dart index f3415be..732cdf9 100644 --- a/test/model/manifest/emitter_test.dart +++ b/test/model/manifest/emitter_test.dart @@ -4,37 +4,69 @@ import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:gitrinth/src/model/manifest/parser.dart'; import 'package:test/test.dart'; +LockedEntry _modrinthOnly({ + required String slug, + required String version, + required String projectId, + required String versionId, + required String fileName, + String? url, + String? sha1, + String? sha512, + int? size, + List gameVersions = const [], + List acceptsMc = const [], + LockedDependencyKind dependency = LockedDependencyKind.direct, + SideEnv client = SideEnv.required, + SideEnv server = SideEnv.required, +}) => LockedEntry( + slug: slug, + kind: LockedEntryKind.resolved, + version: version, + modrinth: LockedModrinthSource( + projectId: projectId, + versionId: versionId, + versionNumber: version, + file: LockedFile( + name: fileName, + url: url, + sha1: sha1, + sha512: sha512, + size: size, + ), + gameVersions: gameVersions, + ), + client: client, + server: server, + dependency: dependency, + acceptsMc: acceptsMc, +); + void main() { - ModsLock sample() => const ModsLock( + ModsLock sample() => ModsLock( gitrinthVersion: '0.1.0', - loader: LoaderConfig(mods: ModLoader.neoforge), + loader: const LoaderConfig(mods: ModLoader.neoforge), mcVersion: '1.21.1', mods: { - 'create': LockedEntry( + 'create': _modrinthOnly( slug: 'create', - sourceKind: LockedSourceKind.modrinth, version: '6.0.10+mc1.21.1', projectId: 'LNytGWDc', versionId: 'abc123', - file: LockedFile( - name: 'create-1.21.1-6.0.10.jar', - url: 'https://cdn.modrinth.com/data/create.jar', - sha512: 'AABBCC', - size: 12345, - ), + fileName: 'create-1.21.1-6.0.10.jar', + url: 'https://cdn.modrinth.com/data/create.jar', + sha512: 'AABBCC', + size: 12345, ), - 'flywheel': LockedEntry( + 'flywheel': _modrinthOnly( slug: 'flywheel', - sourceKind: LockedSourceKind.modrinth, version: '1.0.0', projectId: 'PROJ2', versionId: 'VER2', - file: LockedFile( - name: 'flywheel.jar', - url: 'https://cdn.modrinth.com/data/flywheel.jar', - sha512: '112233', - size: 10000, - ), + fileName: 'flywheel.jar', + url: 'https://cdn.modrinth.com/data/flywheel.jar', + sha512: '112233', + size: 10000, dependency: LockedDependencyKind.transitive, ), }, @@ -74,23 +106,20 @@ void main() { }); test('game-versions round-trips through emit -> parse -> emit', () { - const lock = ModsLock( + final lock = ModsLock( gitrinthVersion: '0.1.0', - loader: LoaderConfig(mods: ModLoader.neoforge), + loader: const LoaderConfig(mods: ModLoader.neoforge), mcVersion: '1.21.1', mods: { - 'appleskin': LockedEntry( + 'appleskin': _modrinthOnly( slug: 'appleskin', - sourceKind: LockedSourceKind.modrinth, version: '3.0.9+mc1.21', projectId: 'P1', versionId: 'V1', - file: LockedFile( - name: 'appleskin.jar', - url: 'https://cdn.modrinth.com/data/appleskin.jar', - sha512: 'ff00', - size: 1, - ), + fileName: 'appleskin.jar', + url: 'https://cdn.modrinth.com/data/appleskin.jar', + sha512: 'ff00', + size: 1, gameVersions: ['1.21'], ), }, @@ -98,42 +127,39 @@ void main() { final once = emitModsLock(lock); // `1.21` matches the numeric regex in `_needsQuotes`, so the // emitter quotes it to preserve string-typed round-trip. - expect(once, contains('game-versions: ["1.21"]')); + expect(once, contains('game_versions: ["1.21"]')); final parsed = parseModsLock(once, filePath: 'mods.lock'); - expect(parsed.mods['appleskin']!.gameVersions, ['1.21']); + expect(parsed.mods['appleskin']!.modrinth!.gameVersions, ['1.21']); expect(emitModsLock(parsed), once); }); test('empty gameVersions omits the field', () { final out = emitModsLock(sample()); - expect(out, isNot(contains('game-versions'))); + expect(out, isNot(contains('game_versions'))); }); test('accepts-mc round-trips through emit -> parse -> emit', () { - const lock = ModsLock( + final lock = ModsLock( gitrinthVersion: '0.1.0', - loader: LoaderConfig(mods: ModLoader.neoforge), + loader: const LoaderConfig(mods: ModLoader.neoforge), mcVersion: '1.21.1', mods: { - 'appleskin': LockedEntry( + 'appleskin': _modrinthOnly( slug: 'appleskin', - sourceKind: LockedSourceKind.modrinth, version: '3.0.9+mc1.21', projectId: 'P1', versionId: 'V1', - file: LockedFile( - name: 'appleskin.jar', - url: 'https://cdn.modrinth.com/data/appleskin.jar', - sha512: 'ff00', - size: 1, - ), - gameVersions: ['1.21', '1.21.1'], - acceptsMc: ['1.21'], + fileName: 'appleskin.jar', + url: 'https://cdn.modrinth.com/data/appleskin.jar', + sha512: 'ff00', + size: 1, + gameVersions: const ['1.21', '1.21.1'], + acceptsMc: const ['1.21'], ), }, ); final once = emitModsLock(lock); - expect(once, contains('accepts-mc: ["1.21"]')); + expect(once, contains('accepts_mc: ["1.21"]')); final parsed = parseModsLock(once, filePath: 'mods.lock'); expect(parsed.mods['appleskin']!.acceptsMc, ['1.21']); expect(emitModsLock(parsed), once); @@ -141,27 +167,27 @@ void main() { test('empty acceptsMc omits the field', () { final out = emitModsLock(sample()); - expect(out, isNot(contains('accepts-mc'))); + expect(out, isNot(contains('accepts_mc'))); }); test('emits client/server lines and round-trips through parse', () { - const lock = ModsLock( + final lock = ModsLock( gitrinthVersion: '0.1.0', - loader: LoaderConfig(mods: ModLoader.fabric, modLoaderVersion: '0.17.3'), + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.17.3', + ), mcVersion: '1.21.1', mods: { - 'distanthorizons': LockedEntry( + 'distanthorizons': _modrinthOnly( slug: 'distanthorizons', - sourceKind: LockedSourceKind.modrinth, version: '2.3.0-b', projectId: 'uCdwusMi', versionId: 'abc123', - file: LockedFile( - name: 'distanthorizons-2.3.0-b.jar', - sha1: 'deadbeef', - sha512: 'cafebabe', - size: 12345, - ), + fileName: 'distanthorizons-2.3.0-b.jar', + sha1: 'deadbeef', + sha512: 'cafebabe', + size: 12345, client: SideEnv.optional, server: SideEnv.optional, ), @@ -186,26 +212,28 @@ void main() { }); test('dependency: enum round-trips through emit -> parse -> emit', () { - const lock = ModsLock( + final lock = ModsLock( gitrinthVersion: '0.1.0', - loader: LoaderConfig(mods: ModLoader.neoforge), + loader: const LoaderConfig(mods: ModLoader.neoforge), mcVersion: '1.21.1', mods: { - 'create': LockedEntry( + 'create': _modrinthOnly( slug: 'create', - sourceKind: LockedSourceKind.modrinth, version: '6.0.10', projectId: 'CR', versionId: 'CRV', - file: LockedFile(name: 'create.jar', sha512: 'aa', size: 1), + fileName: 'create.jar', + sha512: 'aa', + size: 1, ), - 'flywheel': LockedEntry( + 'flywheel': _modrinthOnly( slug: 'flywheel', - sourceKind: LockedSourceKind.modrinth, version: '1.0.0', projectId: 'FL', versionId: 'FLV', - file: LockedFile(name: 'flywheel.jar', sha512: 'bb', size: 1), + fileName: 'flywheel.jar', + sha512: 'bb', + size: 1, dependency: LockedDependencyKind.transitive, ), }, @@ -274,17 +302,18 @@ void main() { gitrinth-version: 0.1.0 loader: mods: neoforge:stable -mc-version: 1.21.1 +mc_version: 1.21.1 mods: flywheel: - source: modrinth + kind: resolved version: 1.0.0 - project-id: FL - version-id: FLV - file: - name: flywheel.jar - sha512: bb - size: 1 + modrinth: + project_id: FL + version_id: FLV + file: + name: flywheel.jar + sha512: bb + size: 1 client: required server: required auto: true diff --git a/test/model/manifest/lock_emitter_dual_source_test.dart b/test/model/manifest/lock_emitter_dual_source_test.dart new file mode 100644 index 0000000..4be241c --- /dev/null +++ b/test/model/manifest/lock_emitter_dual_source_test.dart @@ -0,0 +1,206 @@ +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:test/test.dart'; + +const _modrinthBlock = LockedModrinthSource( + projectId: 'mr-pid', + versionId: 'mr-vid', + versionNumber: '21.3.0', + file: LockedFile( + name: 'jei-21.3.0.jar', + url: 'https://cdn.modrinth.com/data/mr-pid/versions/mr-vid/jei-21.3.0.jar', + sha1: 'aabb', + sha512: 'cafebabe', + size: 1234567, + ), + gameVersions: ['1.21.4', '1.21.5'], +); + +const _curseforgeBlock = LockedCurseforgeSource( + projectId: 238222, + fileId: 5512345, + slug: 'jei', + file: LockedFile( + name: 'jei-21.3.0.jar', + url: 'https://edge.forgecdn.net/files/5512/345/jei-21.3.0.jar', + sha1: 'aabb', + size: 1234567, + ), + gameVersions: ['1.21.4', '1.21.5'], +); + +ModsLock _lockWith({ + Map mods = const {}, + Map plugins = const {}, +}) => ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + ), + mcVersion: '1.21.5', + mods: mods, + plugins: plugins, +); + +void main() { + group('emit + parse round-trip with per-platform source blocks', () { + test('dual-source entry: both modrinth and curseforge populate', () { + final lock = _lockWith( + mods: { + 'jei': const LockedEntry( + slug: 'jei', + kind: LockedEntryKind.resolved, + version: '21.3.0', + modrinth: _modrinthBlock, + curseforge: _curseforgeBlock, + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains(' jei:')); + expect(out, contains(' modrinth:')); + expect(out, contains(' project_id: mr-pid')); + expect(out, contains(' curseforge:')); + expect(out, contains(' project_id: 238222')); + expect(out, contains(' file_id: 5512345')); + // SHA1 round-trips lowercased. + expect(out, contains('sha1: aabb')); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + final entry = parsed.mods['jei']!; + expect(entry.modrinth!.projectId, 'mr-pid'); + expect(entry.curseforge!.projectId, 238222); + expect(entry.curseforge!.fileId, 5512345); + expect(emitModsLock(parsed), out); + }); + + test('modrinth-only entry: curseforge field omitted', () { + final lock = _lockWith( + mods: { + 'modrinth-only': const LockedEntry( + slug: 'modrinth-only', + kind: LockedEntryKind.resolved, + version: '21.3.0', + modrinth: _modrinthBlock, + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains(' modrinth:')); + expect(out, isNot(contains(' curseforge:'))); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + expect(parsed.mods['modrinth-only']!.curseforge, isNull); + expect(parsed.mods['modrinth-only']!.modrinth, isNotNull); + }); + + test('curseforge-only entry: modrinth field omitted', () { + final lock = _lockWith( + mods: { + 'cf-only': const LockedEntry( + slug: 'cf-only', + kind: LockedEntryKind.resolved, + version: '21.3.0', + curseforge: _curseforgeBlock, + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains(' curseforge:')); + expect(out, isNot(contains(' modrinth:'))); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + expect(parsed.mods['cf-only']!.modrinth, isNull); + expect(parsed.mods['cf-only']!.curseforge, isNotNull); + }); + + test('url entry: kind=url with urlFile, no platform blocks', () { + final lock = _lockWith( + mods: { + 'manual-jar': const LockedEntry( + slug: 'manual-jar', + kind: LockedEntryKind.url, + url: 'https://example.com/foo-1.0.jar', + urlFile: LockedFile(name: 'foo-1.0.jar'), + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains(' kind: url')); + expect(out, contains(' url: "https://example.com/foo-1.0.jar"')); + expect(out, isNot(contains(' modrinth:'))); + expect(out, isNot(contains(' curseforge:'))); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + expect(parsed.mods['manual-jar']!.kind, LockedEntryKind.url); + expect(parsed.mods['manual-jar']!.url, 'https://example.com/foo-1.0.jar'); + }); + + test('plugins section emits per-platform blocks the same way', () { + final lock = ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + plugins: PluginLoader.paper, + pluginLoaderVersion: '1.21.5', + ), + mcVersion: '1.21.5', + plugins: const { + 'luckperms': LockedEntry( + slug: 'luckperms', + kind: LockedEntryKind.resolved, + version: '5.4.0', + modrinth: _modrinthBlock, + curseforge: _curseforgeBlock, + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains('plugins:')); + expect(out, contains(' luckperms:')); + expect(out, contains(' modrinth:')); + expect(out, contains(' curseforge:')); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + expect(parsed.plugins['luckperms']!.modrinth, isNotNull); + expect(parsed.plugins['luckperms']!.curseforge, isNotNull); + }); + + test( + 'allow_mod_distribution: false survives round-trip and omits file.url', + () { + final lock = _lockWith( + mods: { + 'opaque-cf': const LockedEntry( + slug: 'opaque-cf', + kind: LockedEntryKind.resolved, + version: 'x', + curseforge: LockedCurseforgeSource( + projectId: 9999, + fileId: 100, + file: LockedFile(name: 'opaque.jar'), + allowModDistribution: false, + ), + ), + }, + ); + final out = emitModsLock(lock); + expect(out, contains('allow_mod_distribution: false')); + // file.url is null, so the emitter omits the url line. + final cfBlock = out.substring(out.indexOf(' curseforge:')); + expect(cfBlock, isNot(contains(' url:'))); + + final parsed = parseModsLock(out, filePath: 'mods.lock'); + expect( + parsed.mods['opaque-cf']!.curseforge!.allowModDistribution, + isFalse, + ); + expect(parsed.mods['opaque-cf']!.curseforge!.file.url, isNull); + }, + ); + }); +} diff --git a/test/model/manifest/overrides_merger_test.dart b/test/model/manifest/overrides_merger_test.dart index b7ea2f1..86ce475 100644 --- a/test/model/manifest/overrides_merger_test.dart +++ b/test/model/manifest/overrides_merger_test.dart @@ -19,6 +19,43 @@ void main() { Future
stubInferSection(String slug) async => Section.mods; + test( + 'version-only overrides preserve platform participation and peer identity', + () async { + final m = base().copyWith( + gitrinth: const GitrinthConfig( + curseforge: GitrinthPlatformState.enabled, + ), + modrinthHost: 'https://labrinth.example/v2', + mods: { + 'jei': const ModEntry( + slug: 'jei', + source: ModrinthEntrySource(host: 'https://entry.example/v2'), + sources: {SourceKind.curseforge}, + curseforgeSlug: 'just-enough-items', + noCrossPlatformSearch: true, + ), + }, + ); + final out = await applyOverrides( + m, + const ProjectOverrides( + entries: {'jei': ModEntry(slug: 'jei', constraintRaw: '19.0.0')}, + ), + inferSectionForTransitive: stubInferSection, + ); + expect(out.manifest.gitrinth.curseforge, GitrinthPlatformState.enabled); + expect(out.manifest.modrinthHost, m.modrinthHost); + expect(out.manifest.mods['jei']!.sources, {SourceKind.curseforge}); + expect(out.manifest.mods['jei']!.curseforgeSlug, 'just-enough-items'); + expect(out.manifest.mods['jei']!.noCrossPlatformSearch, isTrue); + expect( + (out.manifest.mods['jei']!.source as ModrinthEntrySource).host, + 'https://entry.example/v2', + ); + }, + ); + test('overrides win over matching entries (in-file overrides)', () async { final m = base(); final withOv = ModsYaml( diff --git a/test/model/manifest/parser_test.dart b/test/model/manifest/parser_test.dart index fe77dd4..a3e6ed6 100644 --- a/test/model/manifest/parser_test.dart +++ b/test/model/manifest/parser_test.dart @@ -13,7 +13,7 @@ version: 0.1.0 description: an example loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: create: ^6.0.10+mc1.21.1 jei: 19.27.0.340 @@ -40,7 +40,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.20.1 +mc_version: 1.20.1 mods: iris: version: ^1.8.12 @@ -71,7 +71,7 @@ version: 0.1.0 description: x loader: mods: forge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: bad: url: https://example.com/x.jar @@ -83,7 +83,7 @@ mods: ); }); - test('rejects hosted: source as deferred', () { + test('parses modrinth_host: into ModrinthEntrySource.host', () { final yaml = ''' slug: pack name: Pack @@ -91,14 +91,62 @@ version: 0.1.0 description: x loader: mods: forge -mc-version: 1.21.1 +mc_version: 1.21.1 +mods: + hosted: + modrinth_host: https://modrinth.example.com + version: ^1.0.0 +'''; + final m = parseModsYaml(yaml, filePath: 'mods.yaml'); + final source = m.mods['hosted']!.source; + expect(source, isA()); + expect( + (source as ModrinthEntrySource).host, + 'https://modrinth.example.com', + ); + }); + + test('pack-level modrinth_host: parses into ModsYaml.modrinthHost', () { + final yaml = ''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +modrinth_host: https://modrinth.example.com +mods: + jei: ^1.0 +'''; + final m = parseModsYaml(yaml, filePath: 'mods.yaml'); + expect(m.modrinthHost, 'https://modrinth.example.com'); + // Per-entry host stays null and falls back to the pack default. + final source = m.mods['jei']!.source as ModrinthEntrySource; + expect(source.host, isNull); + expect( + m.effectiveModrinthHost(m.mods['jei']!, 'https://api.modrinth.com/v2'), + 'https://modrinth.example.com', + ); + }); + + test('rejects modrinth_host: combined with url:', () { + final yaml = ''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: forge +mc_version: 1.21.1 mods: bad: - hosted: https://example.com + modrinth_host: https://example.com + url: https://example.com/x.jar '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), - throwsA(isA()), + throwsA(isA()), ); }); @@ -107,7 +155,7 @@ mods: slug: pack loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -123,7 +171,7 @@ version: 0.1.0 description: x loader: mods: bukkit -mc-version: 1.21.1 +mc_version: 1.21.1 '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -138,7 +186,7 @@ name: Pack version: 0.1.0 description: x loader: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -163,7 +211,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: my-shader: ^1.0.0 '''; @@ -191,7 +239,7 @@ description: x loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: my-shader: ^1.0.0 '''; @@ -210,7 +258,7 @@ description: x loader: mods: neoforge plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.loader.plugins, PluginLoader.paper); @@ -226,7 +274,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: beta '''; @@ -244,7 +292,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: ^1.8 '''; @@ -261,7 +309,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^1.8 @@ -280,7 +328,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^1.8 @@ -307,7 +355,7 @@ description: x loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: my-shader: version: ^1.0.0 @@ -328,7 +376,7 @@ description: x loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: my-shader: version: ^1.0.0 @@ -348,7 +396,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: iris: version: ^1.0 @@ -374,7 +422,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^1.0 @@ -400,7 +448,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: ghost: version: ^1.0 @@ -421,7 +469,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^1.0 @@ -440,7 +488,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 resource_packs: faithful: ^1.0 '''; @@ -457,7 +505,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 data_packs: terralith: ^1.0 '''; @@ -476,7 +524,7 @@ description: x loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: comp: r5.7.1 '''; @@ -493,11 +541,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: [1.21, 1.20.1, 1.21] + accepts_mc: [1.21, 1.20.1, 1.21] '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.mods['appleskin']!.acceptsMc, ['1.21', '1.20.1']); @@ -511,7 +559,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: ^1.8 @@ -528,11 +576,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: 1.21 + accepts_mc: 1.21 '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.mods['appleskin']!.acceptsMc, ['1.21']); @@ -546,11 +594,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: true + accepts_mc: true '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -566,11 +614,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: [1.21, "24w10a", "1.21-pre1", "b1.7.3"] + accepts_mc: [1.21, "24w10a", "1.21-pre1", "b1.7.3"] '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.mods['appleskin']!.acceptsMc, [ @@ -589,11 +637,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: ["1.21 snapshot"] + accepts_mc: ["1.21 snapshot"] '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -609,11 +657,11 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: appleskin: version: ^3.0.9 - accepts-mc: [1.21, true] + accepts_mc: [1.21, true] '''; expect( () => parseModsYaml(yaml, filePath: 'mods.yaml'), @@ -630,11 +678,11 @@ description: x loader: mods: neoforge shaders: iris -mc-version: 1.21.1 +mc_version: 1.21.1 shaders: complementary-reimagined: version: ^5.7.1 - accepts-mc: [1.21] + accepts_mc: [1.21] '''; final m = parseModsYaml(yaml, filePath: 'mods.yaml'); expect(m.shaders['complementary-reimagined']!.acceptsMc, ['1.21']); @@ -648,7 +696,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 mods: distanthorizons: version: beta @@ -675,7 +723,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 mods: one: version: release @@ -703,7 +751,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 mods: conflicted: version: beta @@ -724,7 +772,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 mods: jei: version: 19.27.0.340 @@ -737,6 +785,378 @@ mods: }); }); + group('cross-platform source grammar', () { + String wrap(String body, {String mods = 'fabric'}) => + ''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: $mods +mc_version: 1.21.1 +$body +'''; + + test('scalar sources: curseforge normalizes to {curseforge}', () { + final m = parseModsYaml( + wrap(''' +mods: + ae2: + sources: curseforge + version: ^1.0.0 +'''), + filePath: 'mods.yaml', + ); + expect(m.mods['ae2']!.sources, equals({SourceKind.curseforge})); + }); + + test('list sources: [modrinth, curseforge] normalizes to a set', () { + final m = parseModsYaml( + wrap(''' +mods: + jei: + sources: [modrinth, curseforge] + version: ^1.0.0 +'''), + filePath: 'mods.yaml', + ); + expect( + m.mods['jei']!.sources, + equals({SourceKind.modrinth, SourceKind.curseforge}), + ); + }); + + test('rejects empty sources: list', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + bad: + sources: [] +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects unknown source name', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + bad: + sources: bogus +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects duplicate sources entries', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + bad: + sources: [modrinth, modrinth] +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('parses modrinth: and curseforge: peer slug fields', () { + final m = parseModsYaml( + wrap(''' +mods: + fabric-api: + modrinth: fabric-api + curseforge: fabric-api-cf-slug + version: ^0.102.0 +'''), + filePath: 'mods.yaml', + ); + expect(m.mods['fabric-api']!.modrinthSlug, 'fabric-api'); + expect(m.mods['fabric-api']!.curseforgeSlug, 'fabric-api-cf-slug'); + }); + + test('parses hash_scan_depth and allow_hash_mismatch', () { + final m = parseModsYaml( + wrap(''' +mods: + jei: + version: ^19.0.0 + hash_scan_depth: 5 + allow_hash_mismatch: true +'''), + filePath: 'mods.yaml', + ); + expect(m.mods['jei']!.hashScanDepth, 5); + expect(m.mods['jei']!.allowHashMismatch, isTrue); + }); + + test('hash_scan_depth: 0 (disable) parses to 0, not null', () { + final m = parseModsYaml( + wrap(''' +mods: + jei: + version: ^19.0.0 + hash_scan_depth: 0 +'''), + filePath: 'mods.yaml', + ); + expect(m.mods['jei']!.hashScanDepth, 0); + }); + + test('rejects negative hash_scan_depth', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + jei: + hash_scan_depth: -1 +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects non-bool allow_hash_mismatch', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + jei: + allow_hash_mismatch: "yes" +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects hash_scan_depth paired with single-element sources list', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + jei: + sources: [modrinth] + hash_scan_depth: 5 +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects hash_scan_depth paired with scalar sources string', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + jei: + sources: modrinth + hash_scan_depth: 5 +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test('rejects allow_hash_mismatch paired with single-element sources', () { + expect( + () => parseModsYaml( + wrap(''' +mods: + jei: + sources: curseforge + allow_hash_mismatch: true +'''), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + + test( + 'allow_hash_mismatch with sources: [modrinth, curseforge] is allowed', + () { + final m = parseModsYaml( + wrap(''' +mods: + jei: + sources: [modrinth, curseforge] + allow_hash_mismatch: true +'''), + filePath: 'mods.yaml', + ); + expect(m.mods['jei']!.allowHashMismatch, isTrue); + }, + ); + }); + + group('gitrinth: block', () { + test('omitted block uses defaults (modrinth enabled, cf disabled)', () { + final m = parseModsYaml(''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +''', filePath: 'mods.yaml'); + expect(m.gitrinth.modrinth, GitrinthPlatformState.enabled); + expect(m.gitrinth.curseforge, GitrinthPlatformState.disabled); + expect(m.gitrinth.version, isNull); + }); + + test('parses version + platform toggles', () { + final m = parseModsYaml(''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +gitrinth: + version: ^1.0.0 + modrinth: enabled + curseforge: enabled +''', filePath: 'mods.yaml'); + expect(m.gitrinth.version, '^1.0.0'); + expect(m.gitrinth.curseforge, GitrinthPlatformState.enabled); + }); + + test('rejects unknown gitrinth.* fields', () { + expect( + () => parseModsYaml(''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +gitrinth: + bogus: nope +''', filePath: 'mods.yaml'), + throwsA(isA()), + ); + }); + + test('rejects unknown enum values', () { + expect( + () => parseModsYaml(''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: fabric +mc_version: 1.21.1 +gitrinth: + modrinth: bogus +''', filePath: 'mods.yaml'), + throwsA(isA()), + ); + }); + }); + + group('plugin source eligibility', () { + String pluginYaml(String pluginsLoader, {String body = ''}) => + ''' +slug: pack +name: Pack +version: 0.1.0 +description: x +loader: + mods: neoforge + plugins: $pluginsLoader +mc_version: 1.21.1 +plugins: +$body +'''; + + test('paper + sources: curseforge is allowed', () { + final m = parseModsYaml( + pluginYaml( + 'paper', + body: ''' + worldedit: + sources: curseforge + version: ^7.0.0 +''', + ), + filePath: 'mods.yaml', + ); + expect(m.plugins['worldedit']!.sources, equals({SourceKind.curseforge})); + }); + + test('bukkit + curseforge: peer is allowed', () { + final m = parseModsYaml( + pluginYaml( + 'bukkit', + body: ''' + worldedit: + curseforge: worldedit-bukkit + version: ^7.0.0 +''', + ), + filePath: 'mods.yaml', + ); + expect(m.plugins['worldedit']!.curseforgeSlug, 'worldedit-bukkit'); + }); + + test('folia + sources: curseforge throws a manifest error', () { + expect( + () => parseModsYaml( + pluginYaml( + 'folia', + body: ''' + bad: + sources: curseforge + version: ^1.0.0 +''', + ), + filePath: 'mods.yaml', + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf(contains('CurseForge'), contains('eligibility-matrix')), + ), + ), + ); + }); + + test('sponge + curseforge: peer throws a manifest error', () { + expect( + () => parseModsYaml( + pluginYaml( + 'sponge', + body: ''' + bad: + curseforge: bukkit-only-thing + version: ^1.0.0 +''', + ), + filePath: 'mods.yaml', + ), + throwsA(isA()), + ); + }); + }); + group('parseProjectOverrides', () { test('empty file returns empty entries', () { final o = parseProjectOverrides('', filePath: 'project_overrides.yaml'); @@ -783,7 +1203,7 @@ version: 0.1.0 description: x loader: mods: fabric -mc-version: 1.21.1 +mc_version: 1.21.1 overrides: jei: version: 19.27.0.340 @@ -809,7 +1229,7 @@ version: 0.1.0 description: x loader: mods: $mods -mc-version: 1.21.1 +mc_version: 1.21.1 ''', filePath: 'mods.yaml'); test('bare loader name defaults the version tag to "stable"', () { @@ -873,14 +1293,14 @@ mc-version: 1.21.1 ); }); - test('unknown loader name is rejected', () { + test('malformed loader identity is rejected', () { expect( - () => parse('quilt:1.2.3'), + () => parse('bad/loader:1.2.3'), throwsA( isA().having( (e) => e.message, 'message', - contains('not a recognized loader'), + contains('Invalid lowercase loader identity'), ), ), ); @@ -893,7 +1313,7 @@ mc-version: 1.21.1 gitrinth-version: 0.1.0 loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} resource_packs: {} data_packs: {} @@ -909,7 +1329,7 @@ shaders: {} gitrinth-version: 0.1.0 loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} resource_packs: {} data_packs: {} @@ -926,7 +1346,7 @@ gitrinth-version: 0.1.0 loader: mods: "neoforge:21.1.50" plugins: paper -mc-version: 1.21.1 +mc_version: 1.21.1 mods: {} resource_packs: {} data_packs: {} @@ -950,25 +1370,25 @@ plugins: {} gitrinth-version: 0.1.0 loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: sodium: - source: modrinth - project-id: AANobbMI - version-id: abcdef - file: - name: sodium-0.6.0.jar - url: https://cdn.modrinth.com/data/AANobbMI/versions/abcdef/sodium-0.6.0.jar - sha1: 0123456789abcdef - sha512: deadbeef - size: 1024 - env: both + kind: resolved + modrinth: + project_id: AANobbMI + version_id: abcdef + file: + name: sodium-0.6.0.jar + url: https://cdn.modrinth.com/data/AANobbMI/versions/abcdef/sodium-0.6.0.jar + sha1: 0123456789abcdef + sha512: deadbeef + size: 1024 resource_packs: {} data_packs: {} shaders: {} '''; final l = parseModsLock(lock, filePath: 'mods.lock'); - final f = l.mods['sodium']!.file!; + final f = l.mods['sodium']!.modrinth!.file; expect(f.sha1, '0123456789abcdef'); expect(f.sha512, 'deadbeef'); }); @@ -978,18 +1398,19 @@ shaders: {} gitrinth-version: 0.1.0 loader: mods: "fabric:0.17.3" -mc-version: 1.21.1 +mc_version: 1.21.1 mods: distanthorizons: - source: modrinth + kind: resolved version: "2.3.0-b" - project-id: uCdwusMi - version-id: abc123 - file: - name: distanthorizons-2.3.0-b.jar - sha1: deadbeef - sha512: cafebabe - size: 12345 + modrinth: + project_id: uCdwusMi + version_id: abc123 + file: + name: distanthorizons-2.3.0-b.jar + sha1: deadbeef + sha512: cafebabe + size: 12345 client: optional server: optional resource_packs: {} @@ -1011,7 +1432,7 @@ version: 0.1.0 description: x loader: mods: neoforge -mc-version: 1.21.1 +mc_version: 1.21.1 $filesBlock '''; diff --git a/test/model/manifest/section_transform_test.dart b/test/model/manifest/section_transform_test.dart new file mode 100644 index 0000000..0b5e1ad --- /dev/null +++ b/test/model/manifest/section_transform_test.dart @@ -0,0 +1,123 @@ +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/file_entry.dart'; +import 'package:gitrinth/src/model/resolver/constraint.dart'; +import 'package:gitrinth/src/service/resolve/conflict_disable.dart'; +import 'package:test/test.dart'; + +void main() { + const entries = { + 'target': ModEntry(slug: 'remote-name', constraintRaw: '^1.0.0'), + 'keep': ModEntry(slug: 'keep', constraintRaw: '^2.0.0'), + }; + const manifest = ModsYaml( + slug: 'pack', + name: 'Pack', + version: '1.2.3', + description: 'All settings survive section changes', + mcVersion: '1.21.1', + loader: LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + plugins: PluginLoader.paper, + pluginLoaderVersion: '100', + ), + modrinthHost: 'https://source.example/v2', + gitrinth: GitrinthConfig( + version: '^0.1.0', + curseforge: GitrinthPlatformState.enabled, + ), + mods: entries, + resourcePacks: entries, + dataPacks: entries, + shaders: entries, + plugins: entries, + projectOverrides: {'override': ModEntry(slug: 'override')}, + files: {'config/a': FileEntry(destination: 'config/a', sourcePath: 'a')}, + publishTo: {SourceKind.modrinth: 'https://publish.example/v2'}, + ); + + void expectPackSettingsPreserved(ModsYaml result) { + expect(result.slug, manifest.slug); + expect(result.name, manifest.name); + expect(result.version, manifest.version); + expect(result.description, manifest.description); + expect(result.mcVersion, manifest.mcVersion); + expect(result.loader, manifest.loader); + expect(result.modrinthHost, manifest.modrinthHost); + expect(result.gitrinth, manifest.gitrinth); + expect(result.projectOverrides, manifest.projectOverrides); + expect(result.files, manifest.files); + expect(result.publishTo, manifest.publishTo); + } + + test( + 'section mapping transforms every section and preserves pack settings', + () { + final transformed = manifest.mapSectionEntries( + (section, values) => { + for (final entry in values.entries) + entry.key: entry.value.copyWith(constraintRaw: section.name), + }, + ); + + for (final section in Section.values) { + expect( + transformed.sectionEntries(section)['target']!.constraintRaw, + section.name, + ); + expect( + transformed.sectionEntries(section)['keep']!.constraintRaw, + section.name, + ); + expect( + manifest.sectionEntries(section)['target']!.constraintRaw, + '^1.0.0', + ); + } + expectPackSettingsPreserved(transformed); + }, + ); + + test('section replacement changes only the selected section', () { + for (final selected in Section.values) { + final replaced = manifest.withSectionEntries(selected, const {}); + for (final section in Section.values) { + expect( + replaced.sectionEntries(section), + section == selected ? isEmpty : entries, + ); + } + expectPackSettingsPreserved(replaced); + } + }); + + test('removal uses local map keys in every section', () { + final stripped = manifest.withoutEntrySlugs({'target'}); + for (final section in Section.values) { + expect(stripped.sectionEntries(section), {'keep': entries['keep']}); + } + expectPackSettingsPreserved(stripped); + }); + + test('conflict markers apply to plugins without changing other sections', () { + const manifest = ModsYaml( + slug: 'pack', + name: 'Pack', + version: '1.0.0', + description: '', + mcVersion: '1.21.1', + loader: LoaderConfig( + mods: ModLoader.vanilla, + plugins: PluginLoader.paper, + ), + mods: {'same': ModEntry(slug: 'same', constraintRaw: '^1.0.0')}, + plugins: {'same': ModEntry(slug: 'same', constraintRaw: '^2.0.0')}, + ); + + final marked = applyDisableMarkers(manifest, {(Section.plugins, 'same')}); + + expect(marked.plugins['same']!.constraintRaw, disabledByConflictMarker); + expect(marked.mods['same']!.constraintRaw, '^1.0.0'); + expect(manifest.plugins['same']!.constraintRaw, '^2.0.0'); + }); +} diff --git a/test/model/manifest/source_eligibility_test.dart b/test/model/manifest/source_eligibility_test.dart new file mode 100644 index 0000000..774812e --- /dev/null +++ b/test/model/manifest/source_eligibility_test.dart @@ -0,0 +1,149 @@ +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/source_eligibility.dart'; +import 'package:test/test.dart'; + +ModsYaml _pack({ + required PluginLoader? plugins, + GitrinthPlatformState modrinth = GitrinthPlatformState.enabled, + GitrinthPlatformState curseforge = GitrinthPlatformState.disabled, + Map mods = const {}, + Map pluginsMap = const {}, + Map resourcePacks = const {}, +}) { + return ModsYaml( + slug: 'pack', + name: 'pack', + version: '0.0.1', + description: '', + loader: LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + plugins: plugins, + ), + mcVersion: '1.21.5', + gitrinth: GitrinthConfig(modrinth: modrinth, curseforge: curseforge), + mods: mods, + resourcePacks: resourcePacks, + plugins: pluginsMap, + ); +} + +ModEntry _entry({Set? sources, String? curseforgeSlug}) { + return ModEntry( + slug: 'jei', + sources: sources, + curseforgeSlug: curseforgeSlug, + ); +} + +void main() { + group('effectiveSources', () { + test('falls back to pack defaults when entry.sources is null', () { + final manifest = _pack(plugins: null); + expect( + effectiveSources(manifest, _entry(), Section.mods), + equals({SourceKind.modrinth}), + ); + }); + + test('pack default with both platforms enabled returns both', () { + final manifest = _pack( + plugins: null, + modrinth: GitrinthPlatformState.enabled, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources(manifest, _entry(), Section.mods), + equals({SourceKind.modrinth, SourceKind.curseforge}), + ); + }); + + test('pack default empty when both disabled and no entry override', () { + final manifest = _pack( + plugins: null, + modrinth: GitrinthPlatformState.disabled, + curseforge: GitrinthPlatformState.disabled, + ); + expect(effectiveSources(manifest, _entry(), Section.mods), isEmpty); + }); + + test('entry sources: [curseforge] overrides pack-level disable', () { + final manifest = _pack( + plugins: null, + curseforge: GitrinthPlatformState.disabled, + ); + expect( + effectiveSources( + manifest, + _entry(sources: {SourceKind.curseforge}), + Section.mods, + ), + equals({SourceKind.curseforge}), + ); + }); + + test('Folia plugins section silently drops curseforge', () { + final manifest = _pack( + plugins: PluginLoader.folia, + modrinth: GitrinthPlatformState.enabled, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources(manifest, _entry(), Section.plugins), + equals({SourceKind.modrinth}), + ); + }); + + test('Sponge plugins section silently drops curseforge', () { + final manifest = _pack( + plugins: PluginLoader.spongeforge, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources(manifest, _entry(), Section.plugins), + equals({SourceKind.modrinth}), + ); + }); + + test('paper plugins section keeps curseforge', () { + final manifest = _pack( + plugins: PluginLoader.paper, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources(manifest, _entry(), Section.plugins), + equals({SourceKind.modrinth, SourceKind.curseforge}), + ); + }); + + test('non-plugins section never filters curseforge by loader', () { + final manifest = _pack( + plugins: PluginLoader.folia, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources(manifest, _entry(), Section.mods), + equals({SourceKind.modrinth, SourceKind.curseforge}), + ); + expect( + effectiveSources(manifest, _entry(), Section.resourcePacks), + equals({SourceKind.modrinth, SourceKind.curseforge}), + ); + }); + + test('entry sources: [curseforge] in Folia plugins drops to empty set', () { + final manifest = _pack( + plugins: PluginLoader.folia, + curseforge: GitrinthPlatformState.enabled, + ); + expect( + effectiveSources( + manifest, + _entry(sources: {SourceKind.curseforge}), + Section.plugins, + ), + isEmpty, + ); + }); + }); +} diff --git a/test/model/resolver/error_labels_test.dart b/test/model/resolver/error_labels_test.dart new file mode 100644 index 0000000..a263d1d --- /dev/null +++ b/test/model/resolver/error_labels_test.dart @@ -0,0 +1,62 @@ +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart'; +import 'package:gitrinth/src/model/resolver/modrinth_solver_adapter.dart'; +import 'package:gitrinth/src/model/resolver/resolver.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; +import 'package:test/test.dart'; + +void main() { + test( + 'missing-file error displays the local name for a scoped solver key', + () async { + final identities = SourceIdentity(); + for (final section in [Section.mods, Section.resourcePacks]) { + identities.register( + LocalEntryKey(section, 'same'), + host: 'https://example.com/v2', + ); + } + final resolver = Resolver( + nodeKeyFor: (section, slug) => + identities.solverKey(LocalEntryKey(section, slug)), + localNameFor: (key) => identities.nodeForSolverKey(key).localKey, + formatError: identities.displayMessage, + resolveSlugForProjectId: (_) async => null, + listVersions: (_) async => [ + ModrinthSolverVersion( + const Version( + id: 'v1', + projectId: 'p1', + versionNumber: '1.0.0', + files: [], + dependencies: [], + loaders: [], + gameVersions: [], + ), + ), + ], + ); + final manifest = ModsYaml( + slug: 'pack', + name: 'Pack', + version: '1', + description: '', + mcVersion: '1.21.1', + loader: const LoaderConfig(mods: ModLoader.fabric), + mods: {'same': const ModEntry(slug: 'same')}, + resourcePacks: {'same': const ModEntry(slug: 'same')}, + ); + await expectLater( + resolver.resolve(manifest), + throwsA( + isA().having( + (error) => error.message, + 'message', + 'Resolved same version 1.0.0 has no downloadable file.', + ), + ), + ); + }, + ); +} diff --git a/test/model/resolver/modrinth_solver_adapter_test.dart b/test/model/resolver/modrinth_solver_adapter_test.dart new file mode 100644 index 0000000..3229dd3 --- /dev/null +++ b/test/model/resolver/modrinth_solver_adapter_test.dart @@ -0,0 +1,173 @@ +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/modrinth/dependency.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; +import 'package:gitrinth/src/model/modrinth/version_file.dart'; +import 'package:gitrinth/src/model/resolver/modrinth_solver_adapter.dart'; +import 'package:gitrinth/src/model/resolver/version_selection.dart'; +import 'package:gitrinth/src/model/resolver/version_solver_types.dart'; +import 'package:test/test.dart'; + +modrinth.Version _v({ + required String id, + required String versionNumber, + String? versionType, + String? datePublished, + List dependencies = const [], +}) => modrinth.Version( + id: id, + projectId: 'p', + versionNumber: versionNumber, + files: [ + VersionFile( + url: 'https://example.com/$id.jar', + filename: '$id.jar', + hashes: const {'sha512': 'deadbeef'}, + size: 1, + primary: true, + ), + ], + dependencies: dependencies, + loaders: const ['fabric'], + gameVersions: const ['1.21.1'], + versionType: versionType, + datePublished: datePublished, +); + +void main() { + group('ModrinthSolverVersion', () { + test('passes versionNumber and id through', () { + final v = ModrinthSolverVersion(_v(id: 'x', versionNumber: '1.2.3')); + expect(v.id, 'x'); + expect(v.versionNumber, '1.2.3'); + }); + + test('parses parsedVersion via parseModrinthVersionBestEffort', () { + // Four-segment numeric → pub_semver-friendly form via the + // best-effort parser. + final v = ModrinthSolverVersion( + _v(id: 'x', versionNumber: '19.27.0.340'), + ); + expect(v.parsedVersion.major, 19); + expect(v.parsedVersion.minor, 27); + expect(v.parsedVersion.patch, 0); + }); + + test('versionType release/beta/alpha → Channel', () { + expect( + ModrinthSolverVersion( + _v(id: 'r', versionNumber: '1.0.0', versionType: 'release'), + ).channel, + Channel.release, + ); + expect( + ModrinthSolverVersion( + _v(id: 'b', versionNumber: '1.0.0', versionType: 'beta'), + ).channel, + Channel.beta, + ); + expect( + ModrinthSolverVersion( + _v(id: 'a', versionNumber: '1.0.0', versionType: 'alpha'), + ).channel, + Channel.alpha, + ); + }); + + test('null/unknown versionType defaults to release', () { + expect( + ModrinthSolverVersion(_v(id: 'n', versionNumber: '1.0.0')).channel, + Channel.release, + ); + }); + + test('drops optional and embedded deps; keeps required + incompatible', () { + final v = ModrinthSolverVersion( + _v( + id: 'x', + versionNumber: '1.0.0', + dependencies: const [ + Dependency( + projectId: 'r-pid', + dependencyType: DependencyType.required, + ), + Dependency( + projectId: 'o-pid', + dependencyType: DependencyType.optional, + ), + Dependency( + projectId: 'i-pid', + dependencyType: DependencyType.incompatible, + ), + Dependency( + projectId: 'e-pid', + dependencyType: DependencyType.embedded, + ), + ], + ), + ); + expect(v.dependencies, hasLength(2)); + expect(v.dependencies.map((d) => d.targetProjectId).toSet(), { + 'r-pid', + 'i-pid', + }); + expect( + v.dependencies.where((d) => d.type == SolverDependencyType.required), + hasLength(1), + ); + expect( + v.dependencies.where( + (d) => d.type == SolverDependencyType.incompatible, + ), + hasLength(1), + ); + }); + + test('preserves projectId and versionId pass-through', () { + final v = ModrinthSolverVersion( + _v( + id: 'x', + versionNumber: '1.0.0', + dependencies: const [ + Dependency( + projectId: 'r-pid', + versionId: 'r-vid', + dependencyType: DependencyType.required, + ), + ], + ), + ); + expect(v.dependencies.single.targetProjectId, 'r-pid'); + expect(v.dependencies.single.targetVersionId, 'r-vid'); + }); + }); + + group('compareModrinthSolverOrder', () { + test('matches the legacy compareModrinthSelectionOrder ordering', () { + // Two versions with date_published — newer date wins regardless of + // parsed semver. + final older = _v( + id: 'a', + versionNumber: '2.0.0', + datePublished: '2025-01-01T00:00:00Z', + ); + final newer = _v( + id: 'b', + versionNumber: '1.0.0', + datePublished: '2025-06-01T00:00:00Z', + ); + final solverOlder = ModrinthSolverVersion(older); + final solverNewer = ModrinthSolverVersion(newer); + // newer first under both comparators. + expect(compareModrinthSolverOrder(solverNewer, solverOlder), lessThan(0)); + expect( + compareModrinthSelectionOrder( + newer, + solverNewer.parsedVersion, + older, + solverOlder.parsedVersion, + ), + lessThan(0), + ); + }); + }); +} diff --git a/test/model/resolver/pubgrub_agnostic_test.dart b/test/model/resolver/pubgrub_agnostic_test.dart new file mode 100644 index 0000000..06fcdb9 --- /dev/null +++ b/test/model/resolver/pubgrub_agnostic_test.dart @@ -0,0 +1,205 @@ +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/resolver/pubgrub.dart'; +import 'package:gitrinth/src/model/resolver/version_selection.dart'; +import 'package:gitrinth/src/model/resolver/version_solver_types.dart'; +import 'package:pub_semver/pub_semver.dart'; +import 'package:test/test.dart'; + +/// Pure-test [SolverVersion] implementation that doesn't depend on +/// any platform-specific type. Drives a small CSP through +/// [PubGrubSolver] to confirm the solver doesn't downcast. +class _FakeSolverVersion implements SolverVersion { + @override + final String id; + @override + final String versionNumber; + @override + final Version parsedVersion; + @override + final Channel channel; + @override + final List dependencies; + + _FakeSolverVersion({ + required this.id, + required this.versionNumber, + String? parsed, + this.dependencies = const [], + }) : parsedVersion = Version.parse(parsed ?? versionNumber), + channel = Channel.release; +} + +int _fakeOrder(SolverVersion a, SolverVersion b) => + b.parsedVersion.compareTo(a.parsedVersion); + +void main() { + group('PubGrub on a non-Modrinth SolverVersion', () { + test('decides a single user-root constraint', () async { + final db = { + 'foo': [ + _FakeSolverVersion(id: 'foo-1', versionNumber: '1.0.0'), + _FakeSolverVersion(id: 'foo-2', versionNumber: '2.0.0'), + ], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (_) async => null, + selectionOrder: _fakeOrder, + ); + final out = await solver.solve([ + RootConstraint( + slug: 'foo', + constraint: VersionConstraint.parse('^1.0.0'), + isUserDeclared: true, + ), + ]); + expect(out.decisions['foo']!.versionNumber, '1.0.0'); + }); + + test('promotes a lock-suggestion when in range', () async { + final db = { + 'foo': [ + _FakeSolverVersion(id: 'foo-1', versionNumber: '1.0.0'), + _FakeSolverVersion(id: 'foo-2', versionNumber: '1.5.0'), + _FakeSolverVersion(id: 'foo-3', versionNumber: '1.9.0'), + ], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (_) async => null, + selectionOrder: _fakeOrder, + lockSuggestions: const [LockSuggestion('foo', '1.5.0')], + ); + final out = await solver.solve([ + RootConstraint( + slug: 'foo', + constraint: VersionConstraint.parse('^1.0.0'), + isUserDeclared: true, + ), + ]); + expect(out.decisions['foo']!.versionNumber, '1.5.0'); + }); + + test('honors a sticky override-pin decision', () async { + final pinned = _FakeSolverVersion(id: 'foo-7', versionNumber: '7.0.0'); + final db = { + 'foo': [ + _FakeSolverVersion(id: 'foo-1', versionNumber: '1.0.0'), + _FakeSolverVersion(id: 'foo-2', versionNumber: '2.0.0'), + pinned, + ], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (_) async => null, + selectionOrder: _fakeOrder, + overridePins: [OverridePin('foo', pinned)], + ); + final out = await solver.solve(const []); + expect(out.decisions['foo']!.versionNumber, '7.0.0'); + }); + + test( + 'follows a required dep and resolves slug via injected callback', + () async { + final db = { + 'parent': [ + _FakeSolverVersion( + id: 'parent-1', + versionNumber: '1.0.0', + dependencies: const [ + SolverDependency( + targetProjectId: 'child-pid', + type: SolverDependencyType.required, + ), + ], + ), + ], + 'child': [_FakeSolverVersion(id: 'child-1', versionNumber: '1.0.0')], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (pid) async => + pid == 'child-pid' ? 'child' : null, + selectionOrder: _fakeOrder, + ); + final out = await solver.solve([ + RootConstraint( + slug: 'parent', + constraint: VersionConstraint.any, + isUserDeclared: true, + ), + ]); + expect(out.decisions.keys.toSet(), {'parent', 'child'}); + expect(out.auto['child'], isTrue); + expect(out.auto['parent'], isFalse); + }, + ); + + test( + 'detects an incompatible dep against an already-decided slug', + () async { + final db = { + 'parent': [ + _FakeSolverVersion( + id: 'parent-1', + versionNumber: '1.0.0', + dependencies: const [ + SolverDependency( + targetProjectId: 'foe-pid', + type: SolverDependencyType.incompatible, + ), + ], + ), + ], + 'foe': [_FakeSolverVersion(id: 'foe-1', versionNumber: '1.0.0')], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (pid) async => + pid == 'foe-pid' ? 'foe' : null, + selectionOrder: _fakeOrder, + ); + await expectLater( + solver.solve([ + RootConstraint( + slug: 'parent', + constraint: VersionConstraint.any, + isUserDeclared: true, + ), + RootConstraint( + slug: 'foe', + constraint: VersionConstraint.any, + isUserDeclared: true, + ), + ]), + throwsA(isA()), + ); + }, + ); + + test('downgrade picks the lowest matching candidate', () async { + final db = { + 'foo': [ + _FakeSolverVersion(id: 'foo-1', versionNumber: '1.0.0'), + _FakeSolverVersion(id: 'foo-2', versionNumber: '1.5.0'), + _FakeSolverVersion(id: 'foo-3', versionNumber: '1.9.0'), + ], + }; + final solver = PubGrubSolver( + listVersions: (slug) async => db[slug] ?? const [], + resolveSlugForProjectId: (_) async => null, + selectionOrder: _fakeOrder, + solveType: SolveType.downgrade, + ); + final out = await solver.solve([ + RootConstraint( + slug: 'foo', + constraint: VersionConstraint.parse('^1.0.0'), + isUserDeclared: true, + ), + ]); + expect(out.decisions['foo']!.versionNumber, '1.0.0'); + }); + }); +} diff --git a/test/model/resolver/pubgrub_test.dart b/test/model/resolver/pubgrub_test.dart index fc33444..465bdc0 100644 --- a/test/model/resolver/pubgrub_test.dart +++ b/test/model/resolver/pubgrub_test.dart @@ -3,10 +3,17 @@ import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:gitrinth/src/model/modrinth/dependency.dart'; import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; import 'package:gitrinth/src/model/modrinth/version_file.dart'; +import 'package:gitrinth/src/model/resolver/modrinth_solver_adapter.dart'; import 'package:gitrinth/src/model/resolver/pubgrub.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; +/// Wraps a Modrinth-typed version DB into the PubGrub agnostic surface. +ListVersions _adapt(Map> db) => + (slug) async => (db[slug] ?? const []) + .map(ModrinthSolverVersion.new) + .toList(); + modrinth.Version v({ required String slug, required String number, @@ -43,8 +50,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -73,8 +81,9 @@ void main() { 'flywheel': [v(slug: 'flywheel', number: '1.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -93,8 +102,9 @@ void main() { 'create': [v(slug: 'create', number: '5.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -119,8 +129,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, lockSuggestions: const [LockSuggestion('create', '6.0.10')], ); final out = await solver.solve([ @@ -142,8 +153,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -165,8 +177,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -188,8 +201,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -212,8 +226,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -238,8 +253,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -260,8 +276,9 @@ void main() { 'jei': [v(slug: 'jei', number: '1.0.0-alpha', versionType: 'alpha')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -303,8 +320,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -333,8 +351,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -360,8 +379,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, lockSuggestions: const [LockSuggestion('jei', '1.0.1-beta')], ); final out = await solver.solve([ @@ -411,8 +431,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -440,8 +461,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -461,8 +483,9 @@ void main() { 'create': [v(slug: 'create', number: '5.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -501,8 +524,9 @@ void main() { 'flywheel': [], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -543,8 +567,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -610,8 +635,9 @@ void main() { 'flywheel': [], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -650,8 +676,9 @@ void main() { 'a': [v(slug: 'a', number: '1.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -711,8 +738,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -765,8 +793,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); final out = await solver.solve([ RootConstraint( @@ -808,8 +837,9 @@ void main() { 'b': [v(slug: 'b', number: '1.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -852,8 +882,9 @@ void main() { 'flywheel': [], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -877,8 +908,9 @@ void main() { 'create': [v(slug: 'create', number: '5.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => null, + selectionOrder: compareModrinthSolverOrder, ); try { await solver.solve([ @@ -906,9 +938,12 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('jei', db['jei']![2])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin('jei', ModrinthSolverVersion(db['jei']![2])), + ], ); // The user-declared root would normally cap us at 19.0.0; the // override pin pre-decides 19.27.0 and the root is dropped from @@ -928,9 +963,12 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('flywheel', db['flywheel']![1])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin('flywheel', ModrinthSolverVersion(db['flywheel']![1])), + ], ); final out = await solver.solve(const []); expect(out.decisions['flywheel']!.versionNumber, '2.0.0'); @@ -954,9 +992,15 @@ void main() { 'forge_compat': [v(slug: 'forge_compat', number: '1.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('forge_compat', db['forge_compat']![0])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin( + 'forge_compat', + ModrinthSolverVersion(db['forge_compat']![0]), + ), + ], ); final out = await solver.solve([ RootConstraint( @@ -996,9 +1040,12 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('mod_b', db['mod_b']![0])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin('mod_b', ModrinthSolverVersion(db['mod_b']![0])), + ], ); final out = await solver.solve([ RootConstraint( @@ -1035,9 +1082,12 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('mod_b', db['mod_b']![0])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin('mod_b', ModrinthSolverVersion(db['mod_b']![0])), + ], ); final out = await solver.solve([ RootConstraint( @@ -1073,10 +1123,14 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, overridePins: [ - OverridePin('create_incompatible', db['create_incompatible']![0]), + OverridePin( + 'create_incompatible', + ModrinthSolverVersion(db['create_incompatible']![0]), + ), ], ); final out = await solver.solve([ @@ -1102,9 +1156,12 @@ void main() { 'create': [v(slug: 'create', number: '5.0.0')], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, - overridePins: [OverridePin('jei', db['jei']![0])], + selectionOrder: compareModrinthSolverOrder, + overridePins: [ + OverridePin('jei', ModrinthSolverVersion(db['jei']![0])), + ], ); try { await solver.solve([ @@ -1153,10 +1210,14 @@ void main() { ], 'mod_c': [v(slug: 'mod_c', number: '1.0.0')], }; - final overridePin = OverridePin('mod_c', db['mod_c']![0]); + final overridePin = OverridePin( + 'mod_c', + ModrinthSolverVersion(db['mod_c']![0]), + ); final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (id) async => id, + selectionOrder: compareModrinthSolverOrder, overridePins: [overridePin], ); final out = await solver.solve([ diff --git a/test/model/resolver/solve_type_test.dart b/test/model/resolver/solve_type_test.dart index f444402..4ecffe5 100644 --- a/test/model/resolver/solve_type_test.dart +++ b/test/model/resolver/solve_type_test.dart @@ -2,11 +2,18 @@ import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:gitrinth/src/model/modrinth/dependency.dart'; import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; import 'package:gitrinth/src/model/modrinth/version_file.dart'; +import 'package:gitrinth/src/model/resolver/modrinth_solver_adapter.dart'; import 'package:gitrinth/src/model/resolver/pubgrub.dart'; import 'package:gitrinth/src/model/resolver/version_selection.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; +/// Wraps a Modrinth-typed version DB into the PubGrub agnostic surface. +ListVersions _adapt(Map> db) => + (slug) async => (db[slug] ?? const []) + .map(ModrinthSolverVersion.new) + .toList(); + modrinth.Version v({ required String slug, required String number, @@ -39,8 +46,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (_) async => null, + selectionOrder: compareModrinthSolverOrder, // SolveType.get is the default; spelled out for clarity. solveType: SolveType.get, ); @@ -63,8 +71,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (_) async => null, + selectionOrder: compareModrinthSolverOrder, solveType: SolveType.downgrade, ); final out = await solver.solve([ @@ -88,8 +97,9 @@ void main() { ], }; final solver = PubGrubSolver( - listVersions: (slug) async => db[slug] ?? [], + listVersions: _adapt(db), resolveSlugForProjectId: (_) async => null, + selectionOrder: compareModrinthSolverOrder, lockSuggestions: const [LockSuggestion('create', '6.0.11')], solveType: SolveType.downgrade, ); diff --git a/test/runner_test.dart b/test/runner_test.dart index 21c2fa1..c5d6a68 100644 --- a/test/runner_test.dart +++ b/test/runner_test.dart @@ -164,6 +164,7 @@ void main() { test('pack', () async { final out = await runCli(['pack', '--help']); + expect(out.stdout, contains('--server-distribution')); expect(out.exitCode, 0); expect(out.stdout, contains('--output')); }); diff --git a/test/service/cache_inspector_test.dart b/test/service/cache_inspector_test.dart index 2a259f7..624ce15 100644 --- a/test/service/cache_inspector_test.dart +++ b/test/service/cache_inspector_test.dart @@ -57,6 +57,9 @@ void main() { String sha512Hex(List bytes) => sha512.convert(bytes).toString(); + const seedHost = 'https://api.modrinth.com/v2'; + final seedHostSegment = GitrinthCache.hostCacheSegment(seedHost); + void seedModrinth({ required String projectId, required String versionId, @@ -65,7 +68,9 @@ void main() { String? url, String? declaredSha512, }) { - final dir = Directory(p.join(cache.modrinthRoot, projectId, versionId)); + final dir = Directory( + p.join(cache.modrinthRoot, seedHostSegment, projectId, versionId), + ); dir.createSync(recursive: true); final bytes = utf8.encode(body); File(p.join(dir.path, filename)).writeAsBytesSync(bytes); @@ -91,6 +96,7 @@ void main() { }; File( cache.modrinthVersionMetadataPath( + host: seedHost, projectId: projectId, versionId: versionId, ), @@ -192,7 +198,11 @@ void main() { // Sanity: version.json declares goodSha, not the tampered body's hash. expect( File( - cache.modrinthVersionMetadataPath(projectId: 'P', versionId: 'V'), + cache.modrinthVersionMetadataPath( + host: seedHost, + projectId: 'P', + versionId: 'V', + ), ).readAsStringSync(), contains(goodSha), ); @@ -211,6 +221,7 @@ void main() { expect(outcome.stillCorrupt, isEmpty); // The on-disk file is now the good content. final destPath = cache.modrinthPath( + host: seedHost, projectId: 'P', versionId: 'V', filename: 'mod.jar', @@ -274,8 +285,9 @@ void main() { ); test('skips modrinth jar with missing version.json (orphan)', () async { - final dir = Directory(p.join(cache.modrinthRoot, 'P', 'V')) - ..createSync(recursive: true); + final dir = Directory( + p.join(cache.modrinthRoot, seedHostSegment, 'P', 'V'), + )..createSync(recursive: true); File(p.join(dir.path, 'orphan.jar')).writeAsStringSync('garbage'); final downloader = buildDownloader({}); final inspector = CacheInspector(cache); @@ -290,8 +302,9 @@ void main() { }); test('skips modrinth jar when version.json lacks files array', () async { - final dir = Directory(p.join(cache.modrinthRoot, 'P', 'V')) - ..createSync(recursive: true); + final dir = Directory( + p.join(cache.modrinthRoot, seedHostSegment, 'P', 'V'), + )..createSync(recursive: true); File(p.join(dir.path, 'jar.jar')).writeAsStringSync('garbage'); File( p.join(dir.path, 'version.json'), diff --git a/test/service/cache_listed_versions_test.dart b/test/service/cache_listed_versions_test.dart index 3fa6e63..3f6f4c1 100644 --- a/test/service/cache_listed_versions_test.dart +++ b/test/service/cache_listed_versions_test.dart @@ -16,12 +16,15 @@ void main() { }); tearDown(() => tempRoot.deleteSync(recursive: true)); + const host = 'https://api.modrinth.com/v2'; + void writeSidecar( String projectId, String versionId, Map v, ) { final path = cache.modrinthVersionMetadataPath( + host: host, projectId: projectId, versionId: versionId, ); @@ -29,7 +32,7 @@ void main() { File(path).writeAsStringSync(jsonEncode(v)); } - test('returns every parsed version under /', () { + test('returns every parsed version under //', () { writeSidecar( 'p1', 'v1', @@ -46,12 +49,12 @@ void main() { _versionPayload(id: 'vx', projectId: 'p2', version: '0.1.0'), ); - final got = cache.listCachedVersions('p1').toList(); + final got = cache.listCachedVersions(host, 'p1').toList(); expect(got.map((v) => v.id), unorderedEquals(['v1', 'v2'])); }); test('returns an empty iterable when projectId never cached', () { - expect(cache.listCachedVersions('never-seen'), isEmpty); + expect(cache.listCachedVersions(host, 'never-seen'), isEmpty); }); test('skips malformed sidecars (logs warn but does not throw)', () { @@ -61,15 +64,24 @@ void main() { _versionPayload(id: 'good', projectId: 'p1', version: '1.0.0'), ); final badPath = cache.modrinthVersionMetadataPath( + host: host, projectId: 'p1', versionId: 'bad', ); Directory(p.dirname(badPath)).createSync(recursive: true); File(badPath).writeAsStringSync('{not json'); - final got = cache.listCachedVersions('p1').toList(); + final got = cache.listCachedVersions(host, 'p1').toList(); expect(got.map((v) => v.id), equals(['good'])); }); + + test('default host and custom host map to different cache segments', () { + const customHost = 'https://modrinth.example.com'; + expect( + GitrinthCache.hostCacheSegment(host), + isNot(equals(GitrinthCache.hostCacheSegment(customHost))), + ); + }); }); } diff --git a/test/service/cache_test.dart b/test/service/cache_test.dart index 1461239..3c1d48b 100644 --- a/test/service/cache_test.dart +++ b/test/service/cache_test.dart @@ -21,13 +21,39 @@ void main() { if (tempRoot.existsSync()) tempRoot.deleteSync(recursive: true); }); - test('layout uses modrinth///', () { - final path = cache.modrinthPath( - projectId: 'P1', - versionId: 'V1', - filename: 'a.jar', + test( + 'layout uses modrinth////', + () { + const host = 'https://api.modrinth.com/v2'; + final segment = GitrinthCache.hostCacheSegment(host); + final path = cache.modrinthPath( + host: host, + projectId: 'P1', + versionId: 'V1', + filename: 'a.jar', + ); + expect( + path, + p.join(tempRoot.path, 'modrinth', segment, 'P1', 'V1', 'a.jar'), + ); + }, + ); + + test( + 'default host renders to a stable cache segment (api.modrinth.com_v2)', + () { + expect( + GitrinthCache.hostCacheSegment('https://api.modrinth.com/v2'), + 'api.modrinth.com_v2', + ); + }, + ); + + test('different hosts map to different cache segments', () { + expect( + GitrinthCache.hostCacheSegment('https://api.modrinth.com/v2'), + isNot(GitrinthCache.hostCacheSegment('https://modrinth.example.com')), ); - expect(path, p.join(tempRoot.path, 'modrinth', 'P1', 'V1', 'a.jar')); }); test('verifySha512 accepts a matching hash and rejects a wrong one', () { diff --git a/test/service/curseforge_api_factory_test.dart b/test/service/curseforge_api_factory_test.dart new file mode 100644 index 0000000..b479469 --- /dev/null +++ b/test/service/curseforge_api_factory_test.dart @@ -0,0 +1,400 @@ +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api_factory.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_auth_interceptor.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_error_interceptor.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_rate_limit_interceptor.dart'; +import 'package:gitrinth/src/service/offline_guard_interceptor.dart'; +import 'package:gitrinth/src/version.dart'; +import 'package:test/test.dart'; + +class _RecordingAdapter implements HttpClientAdapter { + final int status; + final String body; + final List requests = []; + + _RecordingAdapter({this.status = 200, this.body = _emptyDataEnvelope}); + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + requests.add(options); + return ResponseBody.fromString( + body, + status, + headers: { + Headers.contentTypeHeader: ['application/json'], + }, + ); + } + + @override + void close({bool force = false}) {} +} + +const String _emptyDataEnvelope = + '{"data":{"id":1,"gameId":432,"name":"x","slug":"x","classId":6,"latestFiles":[],"allowModDistribution":true}}'; +const String _emptySearchEnvelope = + '{"data":[],"pagination":{"index":0,"pageSize":50,"resultCount":0,"totalCount":0}}'; +const String _baseUrl = 'https://api.curseforge.com'; + +Dio _buildDio({int responseStatus = 200, String body = _emptyDataEnvelope}) { + final dio = Dio(); + dio.httpClientAdapter = _RecordingAdapter(status: responseStatus, body: body); + return dio; +} + +_RecordingAdapter _adapterOf(Dio dio) => + dio.httpClientAdapter as _RecordingAdapter; + +class _SlowPublicationAdapter implements HttpClientAdapter { + @override + Future fetch( + RequestOptions options, + Stream? stream, + Future? cancelFuture, + ) async { + await Future.delayed(const Duration(milliseconds: 150)); + return ResponseBody.fromString( + '{"data":{"id":100}}', + 200, + headers: { + Headers.contentTypeHeader: ['application/json'], + }, + ); + } + + @override + void close({bool force = false}) {} +} + +void main() { + test('publication read deadline also bounds a stalled transport', () async { + final factory = CurseForgeApiFactory( + console: const Console(), + auth: CurseForgeAuthInterceptor(envTokenLookup: () => 'fixture'), + offline: () => false, + baseUrl: _baseUrl, + ); + addTearDown(factory.close); + factory.api; + final client = factory.dio; + if (client == null) throw StateError('missing client'); + client.options.receiveTimeout = const Duration(milliseconds: 20); + client.httpClientAdapter = _SlowPublicationAdapter(); + await expectLater( + factory.publicationFile(42, 100), + throwsA(isA()), + ); + }); + group('CurseForgeApi retrofit interface', () { + test('getMod issues GET /v1/mods/{id} with cf_auth extra', () async { + final dio = _buildDio(); + final api = CurseForgeApi(dio, baseUrl: _baseUrl); + + await api.getMod(238222); + + final req = _adapterOf(dio).requests.single; + expect(req.method, 'GET'); + expect(req.uri.toString(), '$_baseUrl/v1/mods/238222'); + expect(req.extra[kCurseForgeAuthRequired], isTrue); + }); + + test( + 'searchMods issues GET /v1/mods/search with the queried filters', + () async { + final dio = _buildDio(body: _emptySearchEnvelope); + final api = CurseForgeApi(dio, baseUrl: _baseUrl); + + await api.searchMods(gameId: 432, slug: 'jei', classId: 6); + + final req = _adapterOf(dio).requests.single; + expect(req.method, 'GET'); + final uri = req.uri; + expect(uri.path, '/v1/mods/search'); + expect(uri.queryParameters['gameId'], '432'); + expect(uri.queryParameters['slug'], 'jei'); + expect(uri.queryParameters['classId'], '6'); + expect(req.extra[kCurseForgeAuthRequired], isTrue); + }, + ); + + test('searchMods omits null query params from the wire request', () async { + final dio = _buildDio(body: _emptySearchEnvelope); + final api = CurseForgeApi(dio, baseUrl: _baseUrl); + + await api.searchMods(gameId: 432, slug: 'jei'); + + final req = _adapterOf(dio).requests.single; + expect(req.uri.queryParameters.containsKey('classId'), isFalse); + expect(req.uri.queryParameters.containsKey('searchFilter'), isFalse); + }); + + test( + 'listFiles issues GET /v1/mods/{id}/files with the queried filters', + () async { + final dio = _buildDio(body: _emptySearchEnvelope); + final api = CurseForgeApi(dio, baseUrl: _baseUrl); + + await api.listFiles( + 238222, + gameVersion: '1.21.1', + modLoaderType: 4, + pageSize: 200, + ); + + final req = _adapterOf(dio).requests.single; + expect(req.method, 'GET'); + final uri = req.uri; + expect(uri.path, '/v1/mods/238222/files'); + expect(uri.queryParameters['gameVersion'], '1.21.1'); + expect(uri.queryParameters['modLoaderType'], '4'); + expect(uri.queryParameters['pageSize'], '200'); + expect(req.extra[kCurseForgeAuthRequired], isTrue); + }, + ); + + test('getFile issues GET /v1/mods/{modId}/files/{fileId}', () async { + final dio = _buildDio( + body: + '{"data":{"id":1,"modId":2,"displayName":"a","fileName":"a","releaseType":1,"fileDate":"2025-01-01T00:00:00Z","gameVersions":[],"hashes":[],"dependencies":[],"downloadUrl":null}}', + ); + final api = CurseForgeApi(dio, baseUrl: _baseUrl); + + await api.getFile(238222, 4567); + + final req = _adapterOf(dio).requests.single; + expect(req.method, 'GET'); + expect(req.uri.toString(), '$_baseUrl/v1/mods/238222/files/4567'); + expect(req.extra[kCurseForgeAuthRequired], isTrue); + }); + }); + + group('CurseForgeApiFactory', () { + CurseForgeApiFactory factoryFor({ + bool offline = false, + String baseUrl = _baseUrl, + }) { + return CurseForgeApiFactory( + console: const Console(), + auth: CurseForgeAuthInterceptor( + envTokenLookup: () => null, + baseUrl: baseUrl, + defaultKeyResolver: () => 'TEST', + ), + offline: () => offline, + baseUrl: baseUrl, + ); + } + + test('api returns the same instance across calls', () { + final factory = factoryFor(); + final a = factory.api; + final b = factory.api; + expect(identical(a, b), isTrue); + factory.close(); + }); + + test( + 'Dio has interceptors in [OfflineGuard, Auth, RateLimit, Error] order', + () { + final factory = factoryFor(); + // Force the bundle to build. + factory.api; + // Filter out anything Dio prepends internally (e.g. its + // ImplyContentTypeInterceptor); assert only the four we added. + final ours = factory.dio!.interceptors + .where( + (i) => + i is OfflineGuardInterceptor || + i is CurseForgeAuthInterceptor || + i is CurseForgeRateLimitInterceptor || + i is CurseForgeErrorInterceptor, + ) + .toList(growable: false); + expect(ours, hasLength(4)); + expect(ours[0], isA()); + expect(ours[1], isA()); + expect(ours[2], isA()); + expect(ours[3], isA()); + factory.close(); + }, + ); + + test('User-Agent header is gitrinth/', () { + final factory = factoryFor(); + factory.api; + expect( + factory.dio!.options.headers['User-Agent'], + 'gitrinth/$packageVersion (+github.com/getBoolean/gitrinth)', + ); + factory.close(); + }); + + test('configured baseUrl receives CurseForge auth', () async { + final factory = factoryFor(baseUrl: 'https://cf-proxy.example.test'); + final adapter = _RecordingAdapter(); + factory.api; + factory.dio!.httpClientAdapter = adapter; + + await factory.api.getMod(1); + + final req = adapter.requests.single; + expect(req.uri.toString(), 'https://cf-proxy.example.test/v1/mods/1'); + expect(req.headers['x-api-key'], 'TEST'); + factory.close(); + }); + + test('close() disposes the bundle so the next api access rebuilds', () { + final factory = factoryFor(); + final first = factory.api; + factory.close(); + expect(factory.dio, isNull); + final second = factory.api; + expect(identical(first, second), isFalse); + factory.close(); + }); + + test('OfflineGuardInterceptor blocks calls when offline=true', () async { + final factory = factoryFor(offline: true); + // Replace the adapter with a recorder so a leak through the guard + // would be visible. + final adapter = _RecordingAdapter(); + factory.api; + factory.dio!.httpClientAdapter = adapter; + + await expectLater( + factory.api.getMod(1), + throwsA( + isA() + .having((e) => e.error, 'error', isA()) + .having( + (e) => (e.error as UserError).message, + 'message', + contains('offline'), + ), + ), + ); + expect(adapter.requests, isEmpty); + factory.close(); + }); + }); + + group('CurseForgeErrorInterceptor', () { + Dio withErrorInterceptor({required int status, String body = '{}'}) { + final dio = Dio(); + dio.httpClientAdapter = _RecordingAdapter(status: status, body: body); + dio.interceptors.add(CurseForgeErrorInterceptor()); + return dio; + } + + test('401 maps to AuthenticationError', () async { + final dio = withErrorInterceptor(status: 401); + await expectLater( + dio.get('$_baseUrl/v1/mods/1'), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA(), + ), + ), + ); + }); + + test('403 maps to AuthenticationError', () async { + final dio = withErrorInterceptor(status: 403); + await expectLater( + dio.get('$_baseUrl/v1/mods/1'), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA(), + ), + ), + ); + }); + + test('404 on /v1/mods/ names the mod identifier', () async { + final dio = withErrorInterceptor(status: 404); + await expectLater( + dio.get('$_baseUrl/v1/mods/238222'), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + contains("'238222' not found"), + ), + ), + ), + ); + }); + + test('5xx falls through to a generic UserError', () async { + final dio = withErrorInterceptor( + status: 503, + body: '{"error":"upstream unavailable"}', + ); + await expectLater( + dio.get('$_baseUrl/v1/mods/1'), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + contains('upstream unavailable'), + ), + ), + ), + ); + }); + + test( + 'does not double-wrap a GitrinthException already on the error', + () async { + // Build a chain where the auth interceptor produces an + // AuthenticationError first, then the error interceptor sees it. + final dio = Dio(); + dio.httpClientAdapter = _RecordingAdapter(status: 401); + dio.interceptors.add( + CurseForgeAuthInterceptor( + envTokenLookup: () => null, + defaultKeyResolver: () => 'TEST', + ), + ); + dio.interceptors.add(CurseForgeErrorInterceptor()); + + await expectLater( + dio.get( + '$_baseUrl/v1/mods/1', + options: Options(extra: {kCurseForgeAuthRequired: true}), + ), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + contains('GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64'), + ), + ), + ), + ); + }, + ); + }); +} diff --git a/test/service/curseforge_auth_interceptor_test.dart b/test/service/curseforge_auth_interceptor_test.dart new file mode 100644 index 0000000..0ab6e54 --- /dev/null +++ b/test/service/curseforge_auth_interceptor_test.dart @@ -0,0 +1,334 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_auth_interceptor.dart'; +import 'package:test/test.dart'; + +class _RecordingAdapter implements HttpClientAdapter { + final int status; + final List requests = []; + + _RecordingAdapter({this.status = 200}); + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + requests.add(options); + return ResponseBody.fromString( + '{}', + status, + headers: { + Headers.contentTypeHeader: ['application/json'], + }, + ); + } + + @override + void close({bool force = false}) {} +} + +const String _cfBase = 'https://api.curseforge.com'; + +Dio _buildDio({ + String? envToken, + String? defaultKey, + String baseUrl = _cfBase, + int responseStatus = 200, +}) { + final dio = Dio(); + dio.httpClientAdapter = _RecordingAdapter(status: responseStatus); + dio.interceptors.add( + CurseForgeAuthInterceptor( + envTokenLookup: () => envToken, + baseUrl: baseUrl, + defaultKeyResolver: defaultKey == null ? null : () => defaultKey, + ), + ); + return dio; +} + +_RecordingAdapter _adapterOf(Dio dio) => + dio.httpClientAdapter as _RecordingAdapter; + +Options _authed([Map? extra]) => + Options(extra: {kCurseForgeAuthRequired: true, ...?extra}); + +void main() { + group('CurseForgeAuthInterceptor.onRequest', () { + test('env value wins over the build-time default', () async { + final dio = _buildDio(envToken: 'ENV', defaultKey: 'DEF'); + + await dio.get('$_cfBase/v1/mods/1', options: _authed()); + + expect(_adapterOf(dio).requests.single.headers['x-api-key'], 'ENV'); + }); + + test( + 'falls back to the build-time default key when env is absent', + () async { + final dio = _buildDio(defaultKey: 'DEF'); + + await dio.get('$_cfBase/v1/mods/1', options: _authed()); + + expect(_adapterOf(dio).requests.single.headers['x-api-key'], 'DEF'); + }, + ); + + test('does not attach x-api-key for non-CF hosts', () async { + final dio = _buildDio(envToken: 'ENV'); + + await dio.get('https://example.com/api/x', options: _authed()); + + final headers = _adapterOf(dio).requests.single.headers; + expect(headers.containsKey('x-api-key'), isFalse); + }); + + test('attaches x-api-key for the configured CF host override', () async { + final dio = _buildDio( + envToken: 'ENV', + baseUrl: 'https://cf-proxy.example.test', + ); + + await dio.get( + 'https://cf-proxy.example.test/v1/mods/1', + options: _authed(), + ); + + expect(_adapterOf(dio).requests.single.headers['x-api-key'], 'ENV'); + }); + + test('does not attach x-api-key when the marker extra is absent', () async { + final dio = _buildDio(envToken: 'ENV'); + + await dio.get('$_cfBase/v1/mods/1'); + + final headers = _adapterOf(dio).requests.single.headers; + expect(headers.containsKey('x-api-key'), isFalse); + }); + + test( + 'rejects auth-gated request when the embedded default decodes empty', + () async { + final dio = _buildDio(defaultKey: ''); + + await expectLater( + dio.get('$_cfBase/v1/mods/1', options: _authed()), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + contains('GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64'), + ), + ), + ), + ); + + expect( + _adapterOf(dio).requests, + isEmpty, + reason: 'request must not reach the network without a key', + ); + }, + ); + }); + + group('CurseForgeAuthInterceptor.onError', () { + test( + 'wraps a 401 response in AuthenticationError pointing at token add', + () async { + final dio = _buildDio(envToken: 'ENV', responseStatus: 401); + + await expectLater( + dio.get('$_cfBase/v1/mods/1', options: _authed()), + throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + contains('GITRINTH_CURSEFORGE_DEFAULT_API_KEY_B64'), + ), + ), + ), + ); + }, + ); + + test('non-401 errors pass through untouched', () async { + final dio = _buildDio(envToken: 'ENV', responseStatus: 500); + + await expectLater( + dio.get('$_cfBase/v1/mods/1', options: _authed()), + throwsA( + isA().having( + (e) => e.error, + 'error', + isNot(isA()), + ), + ), + ); + }); + + test('401 from non-CF host is passed through unchanged', () async { + final dio = _buildDio(responseStatus: 401); + + await expectLater( + dio.get('https://example.com/api/x'), + throwsA( + isA().having( + (e) => e.error, + 'error', + isNot(isA()), + ), + ), + ); + }); + }); + + group('CurseForgeAuthInterceptor origin check', () { + test('does not attach x-api-key when only the port differs', () async { + final dio = _buildDio( + envToken: 'ENV', + baseUrl: 'https://cf-proxy.example.test:8443', + ); + + await dio.get( + 'https://cf-proxy.example.test/v1/mods/1', + options: _authed(), + ); + + expect( + _adapterOf(dio).requests.single.headers.containsKey('x-api-key'), + isFalse, + ); + }); + + test('does not attach x-api-key when only the scheme differs', () async { + final dio = _buildDio(envToken: 'ENV'); + + await dio.get('http://api.curseforge.com/v1/mods/1', options: _authed()); + + expect( + _adapterOf(dio).requests.single.headers.containsKey('x-api-key'), + isFalse, + ); + }); + }); + + group('CurseForgeAuthInterceptor redirects over real HTTP', () { + late HttpServer source; + late HttpServer target; + late List targetKeys; + late Dio dio; + late String base; + + setUp(() async { + source = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + target = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + targetKeys = []; + target.listen((request) async { + targetKeys.add(request.headers.value('x-api-key')); + request.response.headers.contentType = ContentType.json; + request.response.write('{}'); + await request.response.close(); + }); + base = 'http://127.0.0.1:${source.port}'; + dio = Dio() + ..interceptors.add( + CurseForgeAuthInterceptor( + envTokenLookup: () => 'fixture-only', + baseUrl: base, + ), + ); + }); + + tearDown(() async { + dio.close(force: true); + await source.close(force: true); + await target.close(force: true); + }); + + void redirectTo(String location, {int status = 302}) { + source.listen((request) async { + request.response.statusCode = status; + request.response.headers.set('location', location); + await request.response.close(); + }); + } + + Matcher refusedRedirect() => throwsA( + isA().having( + (e) => e.error, + 'error', + isA().having( + (e) => e.message, + 'message', + allOf(contains('redirected'), contains('GITRINTH_CURSEFORGE_URL')), + ), + ), + ); + + for (final (label, location) in [ + ('host', (int port) => 'http://localhost:$port/capture'), + ('port', (int port) => 'http://127.0.0.1:$port/capture'), + ]) { + test('refuses a cross-origin redirect that changes the $label', () async { + redirectTo(location(target.port)); + + await expectLater( + dio.get('$base/v1/mods/1', options: _authed()), + refusedRedirect(), + ); + + expect(targetKeys, isEmpty, reason: 'the key must not be replayed'); + }); + } + + test('refuses a redirect that changes the scheme', () async { + redirectTo('https://127.0.0.1:${target.port}/capture', status: 307); + + await expectLater( + dio.get('$base/v1/mods/1', options: _authed()), + refusedRedirect(), + ); + + expect(targetKeys, isEmpty); + }); + + test( + 'refuses redirects even when the caller accepts every status', + () async { + redirectTo('http://localhost:${target.port}/capture'); + + await expectLater( + dio.get( + '$base/v1/mods/1', + options: _authed()..validateStatus = (_) => true, + ), + refusedRedirect(), + ); + + expect(targetKeys, isEmpty); + }, + ); + + test('unauthenticated requests keep default redirect handling', () async { + redirectTo('http://localhost:${target.port}/capture'); + + final response = await dio.get('$base/v1/mods/1'); + + expect(response.statusCode, 200); + expect(targetKeys, [null]); + }); + }); +} diff --git a/test/service/curseforge_cache_path_test.dart b/test/service/curseforge_cache_path_test.dart new file mode 100644 index 0000000..5e862b9 --- /dev/null +++ b/test/service/curseforge_cache_path_test.dart @@ -0,0 +1,58 @@ +import 'dart:io'; + +import 'package:gitrinth/src/service/cache.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + late Directory tempRoot; + late GitrinthCache cache; + + setUp(() { + tempRoot = Directory.systemTemp.createTempSync('gitrinth_cf_cache_'); + cache = GitrinthCache(root: tempRoot.path); + cache.ensureRoot(); + }); + + tearDown(() { + if (tempRoot.existsSync()) tempRoot.deleteSync(recursive: true); + }); + + test('curseforgeRoot is `/curseforge`', () { + expect(cache.curseforgeRoot, p.join(tempRoot.path, 'curseforge')); + }); + + test('curseforgePath uses curseforge///', () { + final path = cache.curseforgePath( + projectId: 238222, + fileId: 4567, + filename: 'jei-1.0.0.jar', + ); + expect( + path, + p.join(tempRoot.path, 'curseforge', '238222', '4567', 'jei-1.0.0.jar'), + ); + }); + + test('curseforge cache layout is disjoint from modrinth layout', () { + const host = 'https://api.modrinth.com/v2'; + final mPath = cache.modrinthPath( + host: host, + projectId: 'P1', + versionId: 'V1', + filename: 'a.jar', + ); + final cfPath = cache.curseforgePath( + projectId: 1, + fileId: 2, + filename: 'a.jar', + ); + expect(p.split(mPath), contains('modrinth')); + expect(p.split(cfPath), contains('curseforge')); + expect( + p.split(cfPath).any((s) => s == 'modrinth'), + isFalse, + reason: 'curseforge layout must not include the modrinth segment', + ); + }); +} diff --git a/test/service/curseforge_filters_test.dart b/test/service/curseforge_filters_test.dart new file mode 100644 index 0000000..a03e9ad --- /dev/null +++ b/test/service/curseforge_filters_test.dart @@ -0,0 +1,586 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/curseforge/cf_constants.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_filters.dart'; +import 'package:test/test.dart'; + +void main() { + group('SectionCfMapping.cfClassId', () { + test('mods → 6', () { + expect(Section.mods.cfClassId, 6); + }); + + test('resourcePacks → 12', () { + expect(Section.resourcePacks.cfClassId, 12); + }); + + test('dataPacks → 6945', () { + expect(Section.dataPacks.cfClassId, 6945); + }); + + test('shaders → 6552', () { + expect(Section.shaders.cfClassId, 6552); + }); + + test('plugins → 5', () { + expect(Section.plugins.cfClassId, 5); + }); + }); + + group('ModLoaderCfMapping.cfModLoaderType', () { + test('forge → 1', () { + expect(ModLoader.forge.cfModLoaderType, 1); + }); + + test('fabric → 4', () { + expect(ModLoader.fabric.cfModLoaderType, 4); + }); + + test('quilt uses the official numeric type 5', () { + expect(ModLoader.quilt.cfModLoaderType, 5); + }); + + test('neoforge → 6', () { + expect(ModLoader.neoforge.cfModLoaderType, 6); + }); + }); + + group('cfReleaseTypesFor', () { + test('release → {1}', () { + expect(cfReleaseTypesFor(Channel.release), {1}); + }); + + test('beta → {1, 2}', () { + expect(cfReleaseTypesFor(Channel.beta), {1, 2}); + }); + + test('alpha → {1, 2, 3}', () { + expect(cfReleaseTypesFor(Channel.alpha), {1, 2, 3}); + }); + }); + + group('pluginLoaderEligibleForCurseforge', () { + test('bukkit is eligible', () { + expect(pluginLoaderEligibleForCurseforge(PluginLoader.bukkit), isTrue); + }); + + test('spigot is eligible', () { + expect(pluginLoaderEligibleForCurseforge(PluginLoader.spigot), isTrue); + }); + + test('paper is eligible', () { + expect(pluginLoaderEligibleForCurseforge(PluginLoader.paper), isTrue); + }); + + test('folia is not eligible', () { + expect(pluginLoaderEligibleForCurseforge(PluginLoader.folia), isFalse); + }); + + test('spongeforge is not eligible', () { + expect( + pluginLoaderEligibleForCurseforge(PluginLoader.spongeforge), + isFalse, + ); + }); + + test('spongeneo is not eligible', () { + expect( + pluginLoaderEligibleForCurseforge(PluginLoader.spongeneo), + isFalse, + ); + }); + + test('spongevanilla is not eligible', () { + expect( + pluginLoaderEligibleForCurseforge(PluginLoader.spongevanilla), + isFalse, + ); + }); + }); + + group('kCurseForgeGameIdMinecraft', () { + test('is 432', () { + expect(kCurseForgeGameIdMinecraft, 432); + }); + }); + + group('listCompatibleFiles', () { + test('Any cannot act as a concrete loader filter', () async { + final adapter = _ScriptedAdapter([]); + final loader = LoaderId('future'); + await expectLater( + listCompatibleFiles( + api: _apiWith(adapter), + registry: LoaderRegistry.builtins.merge([ + LoaderDescriptor( + id: loader, + role: LoaderRole.mod, + curseforgeType: 0, + ), + ]), + projectId: 1, + section: Section.mods, + modLoader: loader, + gameVersions: ['1.21.1'], + channel: Channel.release, + ), + throwsA(isA()), + ); + expect(adapter.requests, isEmpty); + }); + + final filesFixture = File( + 'test/fixtures/curseforge/mod_files_response.json', + ).readAsStringSync(); + + test('drops files whose gameVersions does not intersect the requested ' + 'list and respects the channel floor', () async { + final adapter = _ScriptedAdapter([_resp(200, filesFixture)]); + final api = _apiWith(adapter); + + final files = await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.forge, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + + // Release channel admits releaseType==1 only; 4 of the 5 fixture + // files match `1.21.1`, but only the two release-type ones survive + // and the 1.21-only file is dropped. + expect(files, hasLength(2)); + // Newest-first by fileDate. + expect(files.first.id, 5814200); + expect(files.last.id, 5814100); + }); + + test( + 'explicitly unavailable newest file is excluded from candidates', + () async { + final fixture = jsonDecode(filesFixture) as Map; + for (final file in fixture['data'] as List) { + if (file['id'] == 5814200) file['isAvailable'] = false; + } + final api = _apiWith( + _ScriptedAdapter([_resp(200, jsonEncode(fixture))]), + ); + final files = await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.forge, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + expect(files.map((f) => f.id), [5814100]); + }, + ); + + test('all compatible files unavailable explains availability', () async { + final fixture = jsonDecode(filesFixture) as Map; + for (final file in fixture['data'] as List) { + file['isAvailable'] = false; + } + final api = _apiWith(_ScriptedAdapter([_resp(200, jsonEncode(fixture))])); + await expectLater( + listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.forge, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ), + throwsA( + isA().having( + (e) => e.toString(), + 'message', + contains('unavailable'), + ), + ), + ); + }); + + test('beta channel admits releaseType ∈ {1, 2}', () async { + final adapter = _ScriptedAdapter([_resp(200, filesFixture)]); + final api = _apiWith(adapter); + + final files = await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.beta, + ); + + expect( + files.map((f) => f.id), + containsAll([5814200, 5814100, 5814050]), + ); + expect(files.any((f) => f.releaseType == 3), isFalse); + }); + + test('accepts_mc unions: 1.21 and 1.21.1 both match', () async { + final adapter = _ScriptedAdapter([ + _resp(200, filesFixture), + _resp(200, filesFixture), + ]); + final api = _apiWith(adapter); + + final files = await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21', '1.21.1'], + channel: Channel.release, + ); + + expect( + files.map((f) => f.id), + containsAll([5810000, 5814200, 5814100]), + ); + expect(files.map((f) => f.id).toSet(), hasLength(files.length)); + expect( + adapter.requests.map((r) => r.uri.queryParameters['gameVersion']), + ['1.21', '1.21.1'], + ); + expect( + adapter.requests.map((r) => r.uri.queryParameters['modLoaderType']), + ['4', '4'], + ); + }); + + test('exactly 10000 results ends inside the request window', () async { + final adapter = _ScriptedAdapter([ + for (var index = 0; index < 10000; index += 50) + _resp(200, _page(index: index, total: 10000, count: 50)), + ]); + final files = await listCompatibleFiles( + api: _apiWith(adapter), + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + expect(files, hasLength(10000)); + expect(adapter.requests, hasLength(200)); + for (final request in adapter.requests) { + final query = request.uri.queryParameters; + expect( + int.parse(query['index']!) + int.parse(query['pageSize']!), + lessThanOrEqualTo(10000), + ); + expect(query['gameVersion'], '1.21.1'); + } + }); + + test( + 'filtered history beyond the window fails before paging further', + () async { + final adapter = _ScriptedAdapter([ + _resp(200, _page(index: 0, total: 10001, count: 50)), + ]); + await expectLater( + listCompatibleFiles( + api: _apiWith(adapter), + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf(contains('238222'), contains('1.21.1'), contains('10,000')), + ), + ), + ); + expect(adapter.requests, hasLength(1)); + }, + ); + + test('empty history ends after one version-scoped request', () async { + final adapter = _ScriptedAdapter([ + _resp(200, _page(index: 0, total: 0, count: 0)), + ]); + final files = await listCompatibleFiles( + api: _apiWith(adapter), + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1', '1.21.1'], + channel: Channel.release, + ); + expect(files, isEmpty); + expect( + adapter.requests.single.uri.queryParameters['gameVersion'], + '1.21.1', + ); + }); + + test('premature empty page rejects incomplete history', () async { + final adapter = _ScriptedAdapter([ + _resp(200, _page(index: 0, total: 3, count: 2)), + _resp(200, _page(index: 2, total: 3, count: 0)), + ]); + await expectLater( + listCompatibleFiles( + api: _apiWith(adapter), + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('incomplete'), + ), + ), + ); + expect(adapter.requests, hasLength(2)); + }); + + test( + 'overlapping short pages deduplicate and retain newest ordering', + () async { + final adapter = _ScriptedAdapter([ + _resp(200, _page(index: 0, total: 4, count: 2)), + _resp( + 200, + jsonEncode({ + 'data': [ + _miniFile(id: 1, fileDate: '2025-01-01'), + _miniFile(id: 3, fileDate: '2025-02-01'), + ], + 'pagination': { + 'index': 2, + 'pageSize': 50, + 'resultCount': 2, + 'totalCount': 4, + }, + }), + ), + ]); + final files = await listCompatibleFiles( + api: _apiWith(adapter), + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + expect(files.map((f) => f.id), [3, 0, 1]); + expect(adapter.requests[1].uri.queryParameters['index'], '2'); + }, + ); + + test('plugins+bukkit passes classId=5 and no modLoaderType', () async { + final adapter = _ScriptedAdapter([_resp(200, filesFixture)]); + final api = _apiWith(adapter); + + await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.plugins, + pluginLoader: PluginLoader.bukkit, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + + final query = adapter.requests.single.uri.queryParameters; + expect(query.containsKey('modLoaderType'), isFalse); + }); + + test( + 'plugins+folia throws ArgumentError before any HTTP request', + () async { + final adapter = _ScriptedAdapter([_resp(200, filesFixture)]); + final api = _apiWith(adapter); + + await expectLater( + listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.plugins, + pluginLoader: PluginLoader.folia, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ), + throwsA(isA()), + ); + expect(adapter.requests, isEmpty); + }, + ); + + test('pages additional requests until the totalCount is covered', () async { + // Synthesize a two-page response: 2 files with totalCount=3 on + // page 1, then 1 file on page 2. listCompatibleFiles must keep + // paging. + final page1 = jsonEncode({ + 'data': [ + _miniFile(id: 1, fileDate: '2025-01-03T00:00:00Z'), + _miniFile(id: 2, fileDate: '2025-01-02T00:00:00Z'), + ], + 'pagination': { + 'index': 0, + 'pageSize': 2, + 'resultCount': 2, + 'totalCount': 3, + }, + }); + final page2 = jsonEncode({ + 'data': [_miniFile(id: 3, fileDate: '2025-01-01T00:00:00Z')], + 'pagination': { + 'index': 2, + 'pageSize': 2, + 'resultCount': 1, + 'totalCount': 3, + }, + }); + final adapter = _ScriptedAdapter([_resp(200, page1), _resp(200, page2)]); + final api = _apiWith(adapter); + + final files = await listCompatibleFiles( + api: api, + projectId: 238222, + section: Section.mods, + modLoader: ModLoader.fabric, + gameVersions: const ['1.21.1'], + channel: Channel.release, + ); + + expect(files, hasLength(3)); + expect(adapter.requests, hasLength(2)); + // Second page index should advance. + expect(adapter.requests[1].uri.queryParameters['index'], '2'); + }); + }); + + group('findModBySlug', () { + test('returns the first mod when search yields one result', () async { + final body = File( + 'test/fixtures/curseforge/mod_search_response.json', + ).readAsStringSync(); + final adapter = _ScriptedAdapter([_resp(200, body)]); + final api = _apiWith(adapter); + + final mod = await findModBySlug(api, slug: 'jei', section: Section.mods); + + expect(mod, isNotNull); + expect(mod!.slug, 'jei'); + final query = adapter.requests.single.uri.queryParameters; + expect(query['gameId'], '432'); + expect(query['classId'], '6'); + expect(query['slug'], 'jei'); + }); + + test('returns null when search yields no results', () async { + final body = jsonEncode({ + 'data': >[], + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': 0, + 'totalCount': 0, + }, + }); + final adapter = _ScriptedAdapter([_resp(200, body)]); + final api = _apiWith(adapter); + + final mod = await findModBySlug(api, slug: 'nope', section: Section.mods); + + expect(mod, isNull); + }); + }); +} + +String _page({required int index, required int total, required int count}) => + jsonEncode({ + 'data': [ + for (var i = 0; i < count; i++) + _miniFile(id: index + i, fileDate: '2025-01-01'), + ], + 'pagination': { + 'index': index, + 'pageSize': 50, + 'resultCount': count, + 'totalCount': total, + }, + }); + +CurseForgeApi _apiWith(_ScriptedAdapter adapter) { + final dio = Dio()..httpClientAdapter = adapter; + return CurseForgeApi(dio, baseUrl: 'https://api.curseforge.com'); +} + +typedef _ScriptedResponse = ({int status, List bytes}); + +_ScriptedResponse _resp(int status, String body) => + (status: status, bytes: utf8.encode(body)); + +class _ScriptedAdapter implements HttpClientAdapter { + final List<_ScriptedResponse> queue; + final List requests = []; + + _ScriptedAdapter(this.queue); + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + requests.add(options); + if (queue.isEmpty) { + return ResponseBody.fromString('queue exhausted', 500); + } + final r = queue.removeAt(0); + return ResponseBody.fromBytes( + r.bytes, + r.status, + headers: { + Headers.contentTypeHeader: ['application/json'], + Headers.contentLengthHeader: ['${r.bytes.length}'], + }, + ); + } + + @override + void close({bool force = false}) {} +} + +Map _miniFile({ + required int id, + required String fileDate, + int releaseType = 1, + List gameVersions = const ['1.21.1', 'Fabric'], +}) { + return { + 'id': id, + 'modId': 1, + 'displayName': 'mini-$id', + 'fileName': 'mini-$id.jar', + 'releaseType': releaseType, + 'fileDate': fileDate, + 'gameVersions': gameVersions, + 'hashes': >[], + 'dependencies': >[], + 'downloadUrl': null, + }; +} diff --git a/test/service/curseforge_loader_discovery_test.dart b/test/service/curseforge_loader_discovery_test.dart new file mode 100644 index 0000000..e1a6d0f --- /dev/null +++ b/test/service/curseforge_loader_discovery_test.dart @@ -0,0 +1,194 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:gitrinth/src/app/env.dart'; +import 'package:gitrinth/src/app/loader_registry_provider.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/loader_catalog_cache.dart'; +import 'package:gitrinth/src/service/loader_registry.dart'; +import 'package:path/path.dart' as p; +import 'package:riverpod/riverpod.dart'; +import 'package:test/test.dart'; + +/// Drives loader discovery end to end: `prepareLoaderRegistry` → +/// `CurseForgeApiFactory.listMinecraftLoaders` (with the production +/// auth/offline interceptors) → the persisted catalog cache → snapshot. +void main() { + late Directory root; + late HttpServer server; + late String host; + late List paths; + late List apiKeys; + late Object Function(HttpRequest request) cfBody; + + setUp(() async { + root = Directory.systemTemp.createTempSync('cf_loader_discovery_'); + server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + host = 'http://127.0.0.1:${server.port}'; + paths = []; + apiKeys = []; + cfBody = (_) => { + 'data': [ + { + 'name': 'future-loader-2.0.0', + 'gameVersion': '1.21.1', + 'latest': true, + 'recommended': true, + 'type': 42, + }, + { + 'name': 'future-loader-1.0.0', + 'gameVersion': '1.21.1', + 'latest': false, + 'recommended': false, + 'type': 42, + }, + ], + }; + server.listen((request) async { + paths.add(request.uri.path); + apiKeys.add(request.headers.value('x-api-key')); + request.response.headers.contentType = ContentType.json; + final body = request.uri.path == '/v1/minecraft/modloader' + ? cfBody(request) + : request.uri.path == '/tag/loader' + ? [ + { + 'name': 'future', + 'supported_project_types': ['mod'], + }, + ] + : (request.response.statusCode = 404); + request.response.write(jsonEncode(body)); + await request.response.close(); + }); + }); + + tearDown(() async { + await server.close(force: true); + root.deleteSync(recursive: true); + }); + + ProviderContainer container() { + final c = ProviderContainer( + overrides: [ + environmentProvider.overrideWithValue({ + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CURSEFORGE_URL': host, + 'GITRINTH_CURSEFORGE_TOKEN': 'read-key', + 'GITRINTH_MODRINTH_URL': host, + }), + ], + ); + addTearDown(c.dispose); + return c; + } + + LoaderCatalogCache catalogs() => + LoaderCatalogCache(GitrinthCache(root: p.join(root.path, 'cache'))); + + String manifest({ + required String loader, + String modrinth = 'disabled', + String curseforge = 'enabled', + }) => + ''' +slug: demo +name: Demo +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: '$loader'} +gitrinth: {modrinth: $modrinth, curseforge: $curseforge} +mods: {} +'''; + + Future prepare(String yaml, {bool offline = false}) => + prepareLoaderRegistry( + container(), + parseModsYaml(yaml, filePath: 'mods.yaml'), + offline: offline, + ); + + final unknown = LoaderId('curseforge-type-42'); + + test( + 'discovers an unknown numeric loader type and reuses it offline', + () async { + final registry = await prepare( + manifest(loader: 'curseforge-type-42:2.0.0'), + ); + + expect(paths, ['/v1/minecraft/modloader']); + expect(apiKeys, ['read-key']); + final discovered = registry[unknown]; + expect(discovered?.curseforgeType, 42); + expect(discovered?.role, LoaderRole.mod); + expect(discovered?.versions, [ + 'future-loader-2.0.0', + 'future-loader-1.0.0', + ]); + expect(catalogs().read('curseforge', host)?.map((e) => e['name']), [ + 'future-loader-2.0.0', + 'future-loader-1.0.0', + ]); + + await server.close(force: true); + final offline = await prepare( + manifest(loader: 'curseforge-type-42:2.0.0'), + offline: true, + ); + + expect(offline[unknown]?.curseforgeType, 42); + expect(offline[unknown]?.versions, discovered?.versions); + expect(paths, hasLength(1), reason: 'offline reuse sends no request'); + }, + ); + + for (final (label, body) in [ + ('a missing data list', (HttpRequest _) => {'data': 'nope'}), + ( + 'a non-object entry', + (HttpRequest _) => { + 'data': ['future-loader-2.0.0'], + }, + ), + ('a bare list envelope', (HttpRequest _) => []), + ]) { + test('rejects $label without caching it', () async { + cfBody = body; + + await expectLater( + prepare(manifest(loader: 'curseforge-type-42:2.0.0')), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('CurseForge loader discovery'), + ), + ), + ); + + expect(paths, ['/v1/minecraft/modloader']); + expect(File(catalogs().path('curseforge', host)).existsSync(), isFalse); + }); + } + + test('a disabled CurseForge provider sends no CurseForge request', () async { + final registry = await prepare( + manifest( + loader: 'future:1.0.0', + modrinth: 'enabled', + curseforge: 'disabled', + ), + ); + + expect(registry[LoaderId('future')]?.modrinthTag(host), 'future'); + expect(paths, ['/tag/loader']); + expect(apiKeys, [null], reason: 'Modrinth discovery never gets the CF key'); + expect(File(catalogs().path('curseforge', host)).existsSync(), isFalse); + }); +} diff --git a/test/service/curseforge_metadata_concurrency_test.dart b/test/service/curseforge_metadata_concurrency_test.dart new file mode 100644 index 0000000..38a903c --- /dev/null +++ b/test/service/curseforge_metadata_concurrency_test.dart @@ -0,0 +1,68 @@ +import 'dart:io'; +import 'dart:isolate'; + +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_metadata_store.dart'; +import 'package:test/test.dart'; + +void _writeMetadata(List args) { + final result = args[0] as SendPort; + final store = CurseForgeMetadataStore(GitrinthCache(root: args[1] as String)); + try { + for (var i = 0; i < 100; i++) { + store.write( + ModFile( + id: 10, + modId: 1, + displayName: '${args[2]}-$i', + fileName: 'artifact.jar', + releaseType: 1, + fileDate: '2026-01-01', + gameVersions: ['1.21.1'], + hashes: [], + dependencies: [], + ), + ); + } + result.send(null); + } on Object catch (error) { + result.send(error.toString()); + } +} + +void main() { + test( + 'concurrent selected-metadata writers never collide on staging files', + () async { + final root = Directory.systemTemp.createTempSync( + 'cf_metadata_concurrency_', + ); + addTearDown(() => root.deleteSync(recursive: true)); + final results = ReceivePort(); + addTearDown(results.close); + await Future.wait([ + for (var i = 0; i < 4; i++) + Isolate.spawn(_writeMetadata, [ + results.sendPort, + root.path, + i, + ]), + ]); + final errors = await results.take(4).toList(); + expect(errors, everyElement(isNull)); + final file = CurseForgeMetadataStore( + GitrinthCache(root: root.path), + ).read(projectId: 1, fileId: 10); + expect(file.fileName, 'artifact.jar'); + expect( + Directory(root.path) + .listSync(recursive: true) + .whereType() + .map((file) => file.path) + .where((path) => path.contains('.tmp')), + isEmpty, + ); + }, + ); +} diff --git a/test/service/curseforge_publication_service_test.dart b/test/service/curseforge_publication_service_test.dart new file mode 100644 index 0000000..ef181bc --- /dev/null +++ b/test/service/curseforge_publication_service_test.dart @@ -0,0 +1,402 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api_factory.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_auth_interceptor.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_publication_receipt.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_publication_service.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_upload_client.dart'; +import 'package:gitrinth/src/service/prepared_publication.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +/// A release whose client upload completed and whose dependent server +/// upload timed out, leaving `receipt.server` in the unknown state. +class _UnknownChild { + final CurseForgePublicationService service; + final CurseForgePublicationReceiptStore store; + final Future Function() prepare; + final int Function() uploadRequests; + final String serverSha1; + + _UnknownChild({ + required this.service, + required this.store, + required this.prepare, + required this.uploadRequests, + required this.serverSha1, + }); + + PublicationStepState? get serverState => + store.read(42, '1.0.0')?.server.state; + + /// A remote record that satisfies every reconciliation condition. + Map matchingRecord({int id = 200}) => { + 'id': id, + 'modId': 42, + 'parentProjectFileId': 100, + 'hashes': [ + {'algo': 1, 'value': serverSha1}, + ], + }; +} + +Future<_UnknownChild> _unknownChild({ + required Future>> Function(int project) listFiles, +}) async { + final root = Directory.systemTemp.createTempSync('cf_unknown_child_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio( + BaseOptions(receiveTimeout: const Duration(milliseconds: 150)), + ); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + root.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}'; + final clientManifest = utf8.encode( + jsonEncode({ + 'manifestType': 'minecraftModpack', + 'manifestVersion': 1, + 'name': 'Test', + 'version': '1.0.0', + 'minecraft': { + 'version': '1.21.1', + 'modLoaders': [ + {'id': 'fabric-0.16.0', 'primary': true}, + ], + }, + 'files': [], + 'overrides': 'overrides', + }), + ); + final client = File(p.join(root.path, 'client.zip')) + ..writeAsBytesSync( + ZipEncoder().encode( + Archive()..addFile( + ArchiveFile('manifest.json', clientManifest.length, clientManifest), + ), + ), + ); + final payload = utf8.encode('server mod'); + final marker = utf8.encode( + jsonEncode({ + 'formatVersion': 1, + 'artifactType': 'gitrinth-server-distribution', + 'slug': 'test', + 'version': '1.0.0', + 'minecraft': '1.21.1', + 'loader': {'id': 'fabric', 'version': '0.16.0'}, + 'runtimeBundled': false, + 'files': [ + { + 'path': 'mods/server.jar', + 'sha256': sha256.convert(payload).toString(), + }, + ], + }), + ); + final archive = Archive() + ..addFile( + ArchiveFile('gitrinth-server-distribution.json', marker.length, marker), + ) + ..addFile(ArchiveFile('mods/server.jar', payload.length, payload)); + final distribution = File(p.join(root.path, 'server.zip')) + ..writeAsBytesSync(ZipEncoder().encode(archive)); + final metadata = { + 'displayName': 'Test 1.0.0', + 'releaseType': 'release', + 'isMarkedForManualRelease': false, + }; + final plan = PreparedPublication( + target: SourceKind.curseforge, + serverUrl: host, + token: 'never-store-this-token', + artifact: client, + curseforgeProjectId: 42, + payload: metadata, + dependentServer: PreparedPublication( + target: SourceKind.curseforge, + serverUrl: host, + token: 'never-store-this-token', + artifact: distribution, + curseforgeProjectId: 42, + payload: metadata, + role: PublicationArtifactRole.server, + ), + ); + var requests = 0; + server.listen((request) async { + requests++; + await request.drain(); + // The parent upload answers; the child upload stalls into a timeout. + if (requests == 1) { + request.response.headers.contentType = ContentType.json; + request.response.write('{"id":100}'); + await request.response.close(); + } + }); + final store = CurseForgePublicationReceiptStore(root.path); + final service = CurseForgePublicationService( + uploadClient: CurseForgeUploadClient(dio, baseUrl: host), + store: store, + readFile: (project, id) async => { + 'id': id, + 'modId': project, + 'displayName': 'Test 1.0.0', + 'releaseType': 1, + 'isServerPack': false, + }, + listFiles: listFiles, + ); + Future prepare() => service.prepare( + plan: plan, + slug: 'test', + version: '1.0.0', + displayName: 'Test 1.0.0', + minecraft: '1.21.1', + loaderId: 'fabric', + loaderVersion: '0.16.0', + clientLoaderToken: 'fabric', + ); + await expectLater( + service.publish(await prepare()), + throwsA( + isA() + .having((e) => e.parentId, 'parent ID', 100) + .having((e) => e.unknownOutcome, 'unknown outcome', isTrue), + ), + ); + expect(requests, 2); + expect(store.read(42, '1.0.0')?.client.state, PublicationStepState.complete); + expect(store.read(42, '1.0.0')?.server.state, PublicationStepState.unknown); + return _UnknownChild( + service: service, + store: store, + prepare: prepare, + uploadRequests: () => requests, + serverSha1: sha1.convert(distribution.readAsBytesSync()).toString(), + ); +} + +void main() { + test( + 'timed-out child is unknown until remote hash and parent reconcile it', + () async { + final remote = >[]; + final fixture = await _unknownChild(listFiles: (_) async => remote); + expect( + File(fixture.store.path(42, '1.0.0')).readAsStringSync(), + isNot(contains('never-store-this-token')), + ); + await expectLater( + fixture.prepare(), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('unknown'), + ), + ), + ); + expect(fixture.uploadRequests(), 2); + remote.add(fixture.matchingRecord()); + final recovered = await fixture.service.publish(await fixture.prepare()); + expect(recovered.childId, 200); + expect(fixture.uploadRequests(), 2); + expect(fixture.serverState, PublicationStepState.complete); + }, + ); + + group('unknown child stays unknown without exactly one exact match', () { + final cases = > Function(_UnknownChild)>{ + 'wrong parent': (f) => [ + {...f.matchingRecord(), 'parentProjectFileId': 101}, + ], + 'missing parent': (f) => [ + {...f.matchingRecord()}..remove('parentProjectFileId'), + ], + 'wrong project': (f) => [ + {...f.matchingRecord(), 'modId': 43}, + ], + 'wrong hash': (f) => [ + { + ...f.matchingRecord(), + 'hashes': [ + {'algo': 1, 'value': 'f' * 40}, + ], + }, + ], + 'hash of another algorithm': (f) => [ + { + ...f.matchingRecord(), + 'hashes': [ + {'algo': 2, 'value': f.serverSha1}, + ], + }, + ], + 'missing hashes': (f) => [ + {...f.matchingRecord()}..remove('hashes'), + ], + 'zero ID': (f) => [f.matchingRecord(id: 0)], + 'negative ID': (f) => [f.matchingRecord(id: -200)], + 'non-integer ID': (f) => [ + {...f.matchingRecord(), 'id': '200'}, + ], + 'two matching records': (f) => [ + f.matchingRecord(), + f.matchingRecord(id: 201), + ], + }; + for (final MapEntry(key: label, value: records) in cases.entries) { + test(label, () async { + final remote = >[]; + final fixture = await _unknownChild(listFiles: (_) async => remote); + remote.addAll(records(fixture)); + + await expectLater( + fixture.prepare(), + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf(contains('unknown'), contains('no duplicate POST')), + ), + ), + ); + + expect(fixture.uploadRequests(), 2, reason: 'no further POST'); + expect(fixture.serverState, PublicationStepState.unknown); + }); + } + }); + + test( + 'reconciliation walks the full remote history through the read API', + () async { + final read = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => read.close(force: true)); + final readHost = 'http://127.0.0.1:${read.port}'; + final factory = CurseForgeApiFactory( + console: const Console(), + auth: CurseForgeAuthInterceptor( + envTokenLookup: () => 'read-key', + baseUrl: readHost, + ), + offline: () => false, + baseUrl: readHost, + ); + addTearDown(factory.close); + final fixture = await _unknownChild(listFiles: factory.publicationFiles); + final pages = >[]; + // Page one is full of unrelated history; the match is on page two. + final history = [ + for (var i = 0; i < 50; i++) + { + 'id': 1000 + i, + 'modId': 42, + 'parentProjectFileId': 7, + 'hashes': [], + }, + fixture.matchingRecord(), + ]; + read.listen((request) async { + expect(request.uri.path, '/v1/mods/42/files'); + expect(request.headers.value('x-api-key'), 'read-key'); + pages.add(request.uri.queryParameters); + final index = int.parse(request.uri.queryParameters['index']!); + final size = int.parse(request.uri.queryParameters['pageSize']!); + final page = history.skip(index).take(size).toList(); + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'data': page, + 'pagination': { + 'index': index, + 'pageSize': size, + 'resultCount': page.length, + 'totalCount': history.length, + }, + }), + ); + await request.response.close(); + }); + + final recovered = await fixture.service.publish(await fixture.prepare()); + + expect(recovered.childId, 200); + expect(pages.map((q) => q['index']), ['0', '50']); + expect(fixture.uploadRequests(), 2); + expect(fixture.serverState, PublicationStepState.complete); + }, + ); + + test( + 'upload HTTP rejection does not invoke generic retry interceptors', + () async { + final root = Directory.systemTemp.createTempSync('cf_post_rejection_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio(); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + root.deleteSync(recursive: true); + }); + var requests = 0; + var retries = 0; + dio.interceptors.add( + InterceptorsWrapper( + onError: (error, handler) { + retries++; + handler.next(error); + }, + ), + ); + server.listen((request) async { + requests++; + await request.drain(); + request.response.statusCode = 429; + await request.response.close(); + }); + final artifact = File(p.join(root.path, 'client.zip')) + ..writeAsBytesSync([1]); + await expectLater( + CurseForgeUploadClient( + dio, + baseUrl: 'http://127.0.0.1:${server.port}', + ).upload( + projectId: 42, + token: 'token', + archive: artifact, + metadata: {}, + ), + throwsA( + isA().having( + (e) => e.unknownOutcome, + 'unknown', + isFalse, + ), + ), + ); + expect(requests, 1); + expect(retries, 0); + }, + ); + + test('release keys cannot escape the project receipt directory', () { + const store = CurseForgePublicationReceiptStore('C:/project'); + final path = store.path(42, '../../other-release'); + expect(p.basename(path), matches(RegExp(r'^[a-f0-9]{64}\.json$'))); + expect( + p.dirname(path), + p.join('C:/project', '.gitrinth', 'publications', 'curseforge', '42'), + ); + }); +} diff --git a/test/service/curseforge_publication_state_test.dart b/test/service/curseforge_publication_state_test.dart new file mode 100644 index 0000000..ee103ef --- /dev/null +++ b/test/service/curseforge_publication_state_test.dart @@ -0,0 +1,309 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api_factory.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_auth_interceptor.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_publication_receipt.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_publication_service.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_upload_client.dart'; +import 'package:gitrinth/src/service/prepared_publication.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +class _Fixture { + final Directory root; + final CurseForgePublicationService service; + final Future Function( + List, + ) + _prepare; + Future prepare([ + List peers = const [], + ]) => _prepare(peers); + final CurseForgePublicationReceiptStore store; + final int Function() requests; + _Fixture(this.root, this.service, this._prepare, this.store, this.requests); +} + +Future<_Fixture> _fixture({ + bool checkDurability = false, + bool stallChild = false, + bool stallReadback = false, +}) async { + final root = Directory.systemTemp.createTempSync('cf_unknown_child_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio( + BaseOptions(receiveTimeout: const Duration(milliseconds: 150)), + ); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + root.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}'; + final clientManifest = utf8.encode( + jsonEncode({ + 'manifestType': 'minecraftModpack', + 'manifestVersion': 1, + 'name': 'Test', + 'version': '1.0.0', + 'minecraft': { + 'version': '1.21.1', + 'modLoaders': [ + {'id': 'fabric-0.16.0', 'primary': true}, + ], + }, + 'files': [], + 'overrides': 'overrides', + }), + ); + final client = File(p.join(root.path, 'client.zip')) + ..writeAsBytesSync( + ZipEncoder().encode( + Archive()..addFile( + ArchiveFile('manifest.json', clientManifest.length, clientManifest), + ), + ), + ); + final payload = utf8.encode('server mod'); + final marker = utf8.encode( + jsonEncode({ + 'formatVersion': 1, + 'artifactType': 'gitrinth-server-distribution', + 'slug': 'test', + 'version': '1.0.0', + 'minecraft': '1.21.1', + 'loader': {'id': 'fabric', 'version': '0.16.0'}, + 'runtimeBundled': false, + 'files': [ + { + 'path': 'mods/server.jar', + 'sha256': sha256.convert(payload).toString(), + }, + ], + }), + ); + final archive = Archive() + ..addFile( + ArchiveFile('gitrinth-server-distribution.json', marker.length, marker), + ) + ..addFile(ArchiveFile('mods/server.jar', payload.length, payload)); + final distribution = File(p.join(root.path, 'server.zip')) + ..writeAsBytesSync(ZipEncoder().encode(archive)); + final metadata = { + 'displayName': 'Test 1.0.0', + 'releaseType': 'release', + 'isMarkedForManualRelease': false, + }; + final plan = PreparedPublication( + target: SourceKind.curseforge, + serverUrl: host, + token: 'never-store-this-token', + artifact: client, + curseforgeProjectId: 42, + payload: metadata, + dependentServer: PreparedPublication( + target: SourceKind.curseforge, + serverUrl: host, + token: 'never-store-this-token', + artifact: distribution, + curseforgeProjectId: 42, + payload: metadata, + role: PublicationArtifactRole.server, + ), + ); + var requests = 0; + late final CurseForgePublicationReceiptStore store; + server.listen((request) async { + if (request.method == 'GET') return; + requests++; + if (checkDurability) { + final recorded = store.read(42, '1.0.0'); + expect( + recorded?.client.state, + requests == 1 + ? PublicationStepState.inFlight + : PublicationStepState.complete, + ); + expect( + recorded?.server.state, + requests == 1 + ? PublicationStepState.pending + : PublicationStepState.inFlight, + ); + } + await request.drain(); + if (stallChild && requests == 2) return; + request.response.headers.contentType = ContentType.json; + request.response.write('{"id":${100 + requests}}'); + await request.response.close(); + }); + final remote = >[]; + store = CurseForgePublicationReceiptStore(root.path); + final readFactory = CurseForgeApiFactory( + console: const Console(), + auth: CurseForgeAuthInterceptor( + envTokenLookup: () => 'fixture', + baseUrl: host, + ), + offline: () => false, + baseUrl: host, + ); + addTearDown(readFactory.close); + readFactory.api; + final readClient = readFactory.dio; + if (readClient == null) throw StateError('missing client'); + readClient.options.receiveTimeout = const Duration(milliseconds: 25); + final service = CurseForgePublicationService( + uploadClient: CurseForgeUploadClient(dio, baseUrl: host), + store: store, + readFile: (project, id) async { + if (stallReadback) return readFactory.publicationFile(project, id); + return { + 'id': id, + 'modId': project, + 'displayName': 'Test 1.0.0', + 'releaseType': 1, + 'isServerPack': false, + }; + }, + listFiles: (_) async => remote, + ); + Future prepare( + List peers, + ) => service.prepare( + plan: plan, + slug: 'test', + version: '1.0.0', + displayName: 'Test 1.0.0', + minecraft: '1.21.1', + loaderId: 'fabric', + loaderVersion: '0.16.0', + clientLoaderToken: 'fabric', + peers: peers, + ); + return _Fixture(root, service, prepare, store, () => requests); +} + +void main() { + for (final existingPending in [false, true]) { + test( + 'stale prepared receipt cannot duplicate uploads (existing: $existingPending)', + () async { + final fixture = await _fixture(); + if (existingPending) { + fixture.store.writeIfUnchanged( + (await fixture.prepare()).receipt, + null, + ); + } + final first = await fixture.prepare(); + final stale = await fixture.prepare(); + await fixture.service.publish(first); + await expectLater( + fixture.service.publish(stale), + throwsA(isA()), + ); + expect(fixture.requests(), 2); + expect(fixture.store.read(42, '1.0.0')?.childId, 102); + }, + ); + } + test( + 'overlapping pending publications allow only one upload sequence', + () async { + final fixture = await _fixture(); + fixture.store.writeIfUnchanged((await fixture.prepare()).receipt, null); + final first = await fixture.prepare(); + final second = await fixture.prepare(); + Future publish(PreparedCurseForgePublication plan) async { + try { + return await fixture.service.publish(plan); + } catch (e) { + return e; + } + } + + final results = await Future.wait([publish(first), publish(second)]); + expect(results.whereType(), hasLength(1)); + expect(results.whereType(), hasLength(1)); + expect(fixture.requests(), 2); + expect(fixture.store.read(42, '1.0.0')?.childId, 102); + }, + ); + test( + 'parent and child are durably in flight before their POST begins', + () async { + final fixture = await _fixture(checkDurability: true); + await fixture.service.publish(await fixture.prepare()); + expect(fixture.requests(), 2); + }, + ); + test('receipt write failure sends no upload', () async { + final fixture = await _fixture(); + final plan = await fixture.prepare(); + File( + p.join(fixture.root.path, '.gitrinth'), + ).writeAsStringSync('obstruction'); + await expectLater( + fixture.service.publish(plan), + throwsA(isA()), + ); + expect(fixture.requests(), 0); + }); + test('stale preparation cannot overwrite an unknown child outcome', () async { + final fixture = await _fixture(stallChild: true); + final first = await fixture.prepare(); + final stale = await fixture.prepare(); + await expectLater( + fixture.service.publish(first), + throwsA(isA()), + ); + await expectLater( + fixture.service.publish(stale), + throwsA(isA()), + ); + expect(fixture.requests(), 2); + final saved = fixture.store.read(42, '1.0.0'); + expect(saved?.parentId, 101); + expect(saved?.server.state, PublicationStepState.unknown); + }); + test('stalled readback returns pending and reuses both upload IDs', () async { + final fixture = await _fixture(stallReadback: true); + final first = await fixture.service.publish(await fixture.prepare()); + expect(first.classificationConfirmed, isFalse); + final second = await fixture.service.publish(await fixture.prepare()); + expect(second.parentId, first.parentId); + expect(second.childId, first.childId); + expect(fixture.requests(), 2); + }); + test('stale peer state cannot erase a completed peer upload', () async { + final fixture = await _fixture(); + final peer = PreparedPublication( + target: SourceKind.modrinth, + serverUrl: 'https://api.modrinth.com/v2', + token: 'fixture', + artifact: File(p.join(fixture.root.path, 'client.zip')), + payload: {}, + ); + final first = await fixture.prepare([peer]); + final stale = await fixture.prepare([peer]); + await fixture.service.startPeer(first, peer); + fixture.service.completePeer(first, peer, 'mr-version'); + await expectLater( + fixture.service.startPeer(stale, peer), + throwsA(isA()), + ); + expect( + fixture.store.read(42, '1.0.0')?.peers.values.single.id, + 'mr-version', + ); + expect(fixture.requests(), 0); + }); +} diff --git a/test/service/curseforge_rate_limit_interceptor_test.dart b/test/service/curseforge_rate_limit_interceptor_test.dart new file mode 100644 index 0000000..9460636 --- /dev/null +++ b/test/service/curseforge_rate_limit_interceptor_test.dart @@ -0,0 +1,317 @@ +import 'dart:convert'; +import 'dart:io' show HttpDate; +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_rate_limit_interceptor.dart'; +import 'package:test/test.dart'; + +typedef _ScriptedResponse = ({ + int status, + Map> headers, + List bytes, +}); + +class _ScriptedAdapter implements HttpClientAdapter { + final List<_ScriptedResponse> queue; + final List requests = []; + + _ScriptedAdapter(this.queue); + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + requests.add(options); + if (queue.isEmpty) { + return ResponseBody.fromString('queue exhausted', 500); + } + final r = queue.removeAt(0); + return ResponseBody.fromBytes( + r.bytes, + r.status, + headers: { + ...r.headers, + Headers.contentLengthHeader: ['${r.bytes.length}'], + }, + ); + } + + @override + void close({bool force = false}) {} +} + +const String _cfBase = 'https://api.curseforge.com'; + +_ScriptedResponse _ok({String body = '{}'}) => + (status: 200, headers: const {}, bytes: utf8.encode(body)); + +_ScriptedResponse _rateLimited({ + Map> headers = const {}, +}) => (status: 429, headers: headers, bytes: utf8.encode('{"error":"rate"}')); + +void main() { + group('CurseForgeRateLimitInterceptor', () { + test('429 with Retry-After: 2 sleeps ~2s and retries', () async { + final adapter = _ScriptedAdapter([ + _rateLimited( + headers: { + 'retry-after': ['2'], + }, + ), + _ok(body: '{"ok":true}'), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + ), + ); + + final response = await dio.get('$_cfBase/v1/mods/1'); + + expect(response.statusCode, 200); + expect(sleeps, [const Duration(seconds: 2)]); + expect(adapter.requests, hasLength(2)); + }); + + test('429 without Retry-After doubles the backoff on each attempt ' + '(1, 2, 4, 8, 16s)', () async { + final adapter = _ScriptedAdapter([ + _rateLimited(), + _rateLimited(), + _rateLimited(), + _rateLimited(), + _rateLimited(), + _rateLimited(), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + ), + ); + + await expectLater( + dio.get('$_cfBase/v1/mods/1'), + throwsA( + isA().having( + (e) => e.response?.statusCode, + 'status', + 429, + ), + ), + ); + + // 5 retries → 5 sleeps, doubling per attempt. Five attempts stay + // below the 65s cap; the cap is covered by the Retry-After cases. + expect(sleeps, [ + for (final s in [1, 2, 4, 8, 16]) Duration(seconds: s), + ]); + expect(adapter.requests, hasLength(6)); + }); + + final now = DateTime.utc(2026, 11, 4, 12); + for (final (label, header, expected) in [ + ('below the minimum (0)', '0', 1), + ('negative', '-5', 1), + ('above the maximum', '120', 65), + ('padded with whitespace', ' 7 ', 7), + ('an HTTP-date 30s ahead', HttpDate.format(now.add(_s(30))), 30), + ('an HTTP-date far ahead', HttpDate.format(now.add(_s(600))), 65), + ('an HTTP-date in the past', HttpDate.format(now.subtract(_s(30))), 1), + // Malformed values fall back to the first backoff step. + ('malformed (word)', 'soon', 1), + ('malformed (decimal)', '2.5', 1), + ('malformed (empty)', '', 1), + ]) { + test('Retry-After $label sleeps ${expected}s', () async { + final adapter = _ScriptedAdapter([ + _rateLimited( + headers: { + 'retry-after': [header], + }, + ), + _ok(), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + now: () => now, + ), + ); + + final response = await dio.get('$_cfBase/v1/mods/1'); + + expect(response.statusCode, 200); + expect(sleeps, [Duration(seconds: expected)]); + }); + } + + test('malformed Retry-After keeps ramping on later attempts', () async { + final adapter = _ScriptedAdapter([ + for (var i = 0; i < 3; i++) + _rateLimited( + headers: { + 'retry-after': ['soon'], + }, + ), + _ok(), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + ), + ); + + await dio.get('$_cfBase/v1/mods/1'); + + expect(sleeps, [ + for (final s in [1, 2, 4]) Duration(seconds: s), + ]); + }); + + test('429 from a non-CF host passes through unchanged', () async { + final adapter = _ScriptedAdapter([ + _rateLimited( + headers: { + 'retry-after': ['10'], + }, + ), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + ), + ); + + await expectLater( + dio.get('https://example.com/api'), + throwsA(isA()), + ); + + expect(sleeps, isEmpty); + expect(adapter.requests, hasLength(1)); + }); + + test('429 from the configured CF host override is retried', () async { + final adapter = _ScriptedAdapter([ + _rateLimited( + headers: { + 'retry-after': ['2'], + }, + ), + _ok(), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + baseUrl: 'https://cf-proxy.example.test', + sleep: (d) async => sleeps.add(d), + ), + ); + + final response = await dio.get( + 'https://cf-proxy.example.test/v1/mods/1', + ); + + expect(response.statusCode, 200); + expect(sleeps, [const Duration(seconds: 2)]); + expect(adapter.requests, hasLength(2)); + }); + + test( + 'on exhausted retries, propagates the original DioException', + () async { + final adapter = _ScriptedAdapter([ + for (var i = 0; i < 6; i++) + _rateLimited( + headers: { + 'retry-after': ['1'], + }, + ), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final sleeps = []; + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + sleep: (d) async => sleeps.add(d), + ), + ); + + await expectLater( + dio.get('$_cfBase/v1/mods/1'), + throwsA( + isA().having( + (e) => e.response?.statusCode, + 'status', + 429, + ), + ), + ); + + expect(sleeps, hasLength(5)); + expect(adapter.requests, hasLength(6)); + }, + ); + + test('Console.io fires when the sleep is at or above the verbose ' + 'threshold', () async { + final adapter = _ScriptedAdapter([ + _rateLimited( + headers: { + 'retry-after': ['3'], + }, + ), + _ok(), + ]); + final dio = Dio()..httpClientAdapter = adapter; + final console = _CapturingConsole(); + dio.interceptors.add( + CurseForgeRateLimitInterceptor( + dio: dio, + console: console, + sleep: (_) async {}, + ), + ); + + await dio.get('$_cfBase/v1/mods/1'); + + expect(console.details, hasLength(1)); + expect(console.details.single, contains('CurseForge rate limit')); + expect(console.details.single, contains('~3s')); + }); + }); +} + +class _CapturingConsole extends Console { + final List details = []; + + _CapturingConsole() : super(level: LogLevel.io); + + @override + void io(String message) { + details.add(message); + } +} + +Duration _s(int seconds) => Duration(seconds: seconds); diff --git a/test/service/curseforge_url_test.dart b/test/service/curseforge_url_test.dart new file mode 100644 index 0000000..c437f8e --- /dev/null +++ b/test/service/curseforge_url_test.dart @@ -0,0 +1,124 @@ +import 'package:gitrinth/src/service/curseforge/curseforge_url.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_url.dart'; +import 'package:test/test.dart'; + +void main() { + group('defaultCurseForgeBaseUrl', () { + test('is api.curseforge.com without a trailing slash', () { + expect(defaultCurseForgeBaseUrl, 'https://api.curseforge.com'); + expect(defaultCurseForgeBaseUrl.endsWith('/'), isFalse); + }); + }); + + group('resolveCurseForgeBaseUrl', () { + test('strips trailing slash from GITRINTH_CURSEFORGE_URL', () { + expect( + resolveCurseForgeBaseUrl({'GITRINTH_CURSEFORGE_URL': 'http://x/'}), + 'http://x', + ); + }); + + test( + 'returns the env value verbatim when no trailing slash is present', + () { + expect( + resolveCurseForgeBaseUrl({ + 'GITRINTH_CURSEFORGE_URL': 'https://cf.example.com', + }), + 'https://cf.example.com', + ); + }, + ); + + test('falls back to defaultCurseForgeBaseUrl when env is unset', () { + expect(resolveCurseForgeBaseUrl(const {}), defaultCurseForgeBaseUrl); + }); + + test('falls back when env value is empty', () { + expect( + resolveCurseForgeBaseUrl({'GITRINTH_CURSEFORGE_URL': ''}), + defaultCurseForgeBaseUrl, + ); + }); + }); + + group('decodeDefaultCfApiKey', () { + test('returns empty when no build-time key is embedded', () { + expect(decodeDefaultCfApiKey(), isEmpty); + }); + }); + + group('upload-API constants', () { + test('defaultCurseForgeUploadBaseUrl is minecraft.curseforge.com without a ' + 'trailing slash', () { + expect( + defaultCurseForgeUploadBaseUrl, + 'https://minecraft.curseforge.com', + ); + expect(defaultCurseForgeUploadBaseUrl.endsWith('/'), isFalse); + }); + + test('curseForgeUploadTokenKey is managed as curseforge.com', () { + expect( + curseForgeUploadTokenKey, + normalizeServerKey('https://curseforge.com'), + ); + }); + + test('curseForgeUploadTokenKey is distinct from the read API host', () { + expect( + curseForgeUploadTokenKey, + isNot(equals(normalizeServerKey(defaultCurseForgeBaseUrl))), + ); + }); + }); + + group('resolveCurseForgeUploadToken', () { + test('returns the env value when present', () { + expect( + resolveCurseForgeUploadToken( + env: const {'GITRINTH_CURSEFORGE_UPLOAD_TOKEN': 'env-upload'}, + userTokens: const {}, + ), + 'env-upload', + ); + }); + + test('falls back to userTokens[curseForgeUploadTokenKey]', () { + expect( + resolveCurseForgeUploadToken( + env: const {}, + userTokens: {curseForgeUploadTokenKey: 'user-upload'}, + ), + 'user-upload', + ); + }); + + test('env wins over userTokens', () { + expect( + resolveCurseForgeUploadToken( + env: const {'GITRINTH_CURSEFORGE_UPLOAD_TOKEN': 'env-upload'}, + userTokens: {curseForgeUploadTokenKey: 'user-upload'}, + ), + 'env-upload', + ); + }); + + test('returns null when neither env nor userTokens has a value', () { + expect( + resolveCurseForgeUploadToken(env: const {}, userTokens: const {}), + isNull, + ); + }); + + test('treats empty strings as missing', () { + expect( + resolveCurseForgeUploadToken( + env: const {'GITRINTH_CURSEFORGE_UPLOAD_TOKEN': ''}, + userTokens: {curseForgeUploadTokenKey: ''}, + ), + isNull, + ); + }); + }); +} diff --git a/test/service/curseforge_version_test.dart b/test/service/curseforge_version_test.dart new file mode 100644 index 0000000..55eb7d9 --- /dev/null +++ b/test/service/curseforge_version_test.dart @@ -0,0 +1,25 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:gitrinth/src/model/curseforge/cf_search_response.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_version.dart'; +import 'package:test/test.dart'; + +void main() { + test( + 'JEI fixture extracts mod version after Minecraft and loader labels', + () { + final response = ModFileSearchResponseMapper.fromMap( + jsonDecode( + File( + 'test/fixtures/curseforge/mod_files_response.json', + ).readAsStringSync(), + ) + as Map, + ); + expect(curseForgeFileVersion(response.data.first), '19.27.0.340'); + expect(curseForgeFileSatisfies(response.data.first, '^19.0.15'), isTrue); + expect(curseForgeFileSatisfies(response.data.first, '^1.21.1'), isFalse); + }, + ); +} diff --git a/test/service/file_digest_test.dart b/test/service/file_digest_test.dart new file mode 100644 index 0000000..508372a --- /dev/null +++ b/test/service/file_digest_test.dart @@ -0,0 +1,54 @@ +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/file_digest.dart'; +import 'package:test/test.dart'; + +// Only stream reads are available, so a regression to buffering fails directly. +class _StreamOnlyFile implements File { + final File source; + _StreamOnlyFile(this.source); + + @override + Stream> openRead([int? start, int? end]) => + source.openRead(start, end); + + @override + dynamic noSuchMethod(Invocation invocation) => + throw UnsupportedError('${invocation.memberName}'); +} + +void main() { + test( + 'all cache file hashes stream, accept uppercase, and retain mismatch text', + () async { + final dir = Directory.systemTemp.createTempSync('file-digest-'); + addTearDown(() => dir.deleteSync(recursive: true)); + final bytes = List.generate(200000, (index) => index % 251); + final file = _StreamOnlyFile( + File('${dir.path}/artifact')..writeAsBytesSync(bytes), + ); + for (final (algorithm, verify) in [ + (sha1, GitrinthCache.verifyFileSha1), + (sha256, GitrinthCache.verifyFileSha256), + (sha512, GitrinthCache.verifyFileSha512), + ]) { + final expected = algorithm.convert(bytes).toString(); + expect(await fileDigest(file, algorithm), expected); + await verify(file, expected.toUpperCase()); + await expectLater( + verify(file, 'INVALID'), + throwsA( + isA().having( + (error) => error.message, + 'message', + 'checksum mismatch: expected INVALID, got $expected', + ), + ), + ); + } + }, + ); +} diff --git a/test/service/file_digests_test.dart b/test/service/file_digests_test.dart new file mode 100644 index 0000000..478e0a8 --- /dev/null +++ b/test/service/file_digests_test.dart @@ -0,0 +1,36 @@ +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/service/file_digest.dart'; +import 'package:test/test.dart'; + +class _SingleReadFile implements File { + final List bytes; + var reads = 0; + _SingleReadFile(this.bytes); + + @override + Stream> openRead([int? start, int? end]) { + if (++reads > 1) throw StateError('File was read twice'); + return Stream.fromIterable([bytes.sublist(0, 100), bytes.sublist(100)]); + } + + @override + dynamic noSuchMethod(Invocation invocation) => + throw UnsupportedError('${invocation.memberName}'); +} + +void main() { + test( + 'multiple digests preserve algorithm order with one stream read', + () async { + final bytes = List.generate(200000, (index) => index % 251); + final file = _SingleReadFile(bytes); + expect(await fileDigests(file, [sha1, sha512]), [ + sha1.convert(bytes).toString(), + sha512.convert(bytes).toString(), + ]); + expect(file.reads, 1); + }, + ); +} diff --git a/test/service/java_runtime_fetcher_test.dart b/test/service/java_runtime_fetcher_test.dart index 4683146..1f64183 100644 --- a/test/service/java_runtime_fetcher_test.dart +++ b/test/service/java_runtime_fetcher_test.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; @@ -383,6 +384,47 @@ void main() { }, ); + test( + 'a lock contender cannot delete an in-progress installation', + () async { + final runtime = Directory( + cache.javaRuntimeDir( + vendor: 'temurin', + feature: 21, + osKey: 'windows', + archKey: 'x64', + ), + )..createSync(recursive: true); + final marker = File(p.join(runtime.path, 'owner-in-progress')) + ..writeAsStringSync('owned'); + final handle = File( + cache.javaRuntimeLockPath('21'), + ).openSync(mode: FileMode.write); + handle.lockSync(FileLock.exclusive); + Process? contender; + try { + contender = await Process.start(Platform.resolvedExecutable, [ + '--packages=${p.absolute('.dart_tool/package_config.json')}', + p.absolute('test/helpers/java_runtime_contender.dart'), + tempCacheRoot.path, + ]); + final output = contender.stdout.transform(utf8.decoder).join(); + final errors = contender.stderr.transform(utf8.decoder).join(); + final exitCode = await contender.exitCode.timeout( + const Duration(seconds: 15), + ); + expect(exitCode, 0, reason: await errors); + expect(jsonDecode(await output)['status'], 'lock-refused'); + expect(runtime.existsSync(), isTrue); + expect(marker.readAsStringSync(), 'owned'); + } finally { + contender?.kill(); + handle.unlockSync(); + handle.closeSync(); + } + }, + ); + test('cachedRuntime returns null when nothing cached', () { final fetcher = JavaRuntimeFetcher( cache: cache, diff --git a/test/service/jvm_gc_test.dart b/test/service/jvm_gc_test.dart index d4c9d51..8dc9fba 100644 --- a/test/service/jvm_gc_test.dart +++ b/test/service/jvm_gc_test.dart @@ -10,21 +10,18 @@ void main() { test('Java 8-14 → unlock experimental + Shenandoah', () { for (final major in const [8, 11, 14]) { - expect( - gcFlagsForJavaMajor(major), - const ['-XX:+UnlockExperimentalVMOptions', '-XX:+UseShenandoahGC'], - reason: 'JDK $major needs unlocked Shenandoah', - ); + expect(gcFlagsForJavaMajor(major), const [ + '-XX:+UnlockExperimentalVMOptions', + '-XX:+UseShenandoahGC', + ], reason: 'JDK $major needs unlocked Shenandoah'); } }); test('Java 15-20 -> Shenandoah only', () { for (final major in const [15, 16, 17, 18, 19, 20]) { - expect( - gcFlagsForJavaMajor(major), - const ['-XX:+UseShenandoahGC'], - reason: 'JDK $major does not need the unlock flag', - ); + expect(gcFlagsForJavaMajor(major), const [ + '-XX:+UseShenandoahGC', + ], reason: 'JDK $major does not need the unlock flag'); } }); diff --git a/test/service/loader_binary_fetcher_test.dart b/test/service/loader_binary_fetcher_test.dart index 59c5262..bcccdb4 100644 --- a/test/service/loader_binary_fetcher_test.dart +++ b/test/service/loader_binary_fetcher_test.dart @@ -1,4 +1,7 @@ +import 'dart:convert'; import 'dart:io'; + +import 'package:crypto/crypto.dart'; import 'dart:typed_data'; import 'package:dio/dio.dart'; @@ -52,6 +55,62 @@ void main() { } }); + test( + 'online Quilt fetch repairs truncated metadata after verifying cached bytes', + () async { + final bytes = utf8.encode('verified quilt installer'); + final digest = sha512.convert(bytes).toString(); + final destination = cache.loaderArtifactPath( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + filename: 'quilt-installer.jar', + ); + File(destination) + ..parent.createSync(recursive: true) + ..writeAsBytesSync(bytes); + final metadata = File('$destination.metadata.json') + ..writeAsStringSync('{"version":'); + var calls = 0; + dio.interceptors.add( + InterceptorsWrapper( + onRequest: (options, handler) { + calls++; + handler.resolve( + Response( + requestOptions: options, + data: [ + { + 'version': '1.0.0', + 'hashes': {'sha512': digest}, + 'url': + 'https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/1.0.0/quilt-installer-1.0.0.jar', + }, + ], + ), + ); + }, + ), + ); + final quilt = LoaderBinaryFetcher( + cache: cache, + downloader: downloader, + environment: {}, + ); + final artifact = await quilt.fetchServerArtifact( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + ); + expect(artifact.readAsBytesSync(), bytes); + expect(calls, 1); + expect( + (jsonDecode(metadata.readAsStringSync()) as Map)['version'], + '1.0.0', + ); + }, + ); + test('fetches Forge installer to the loaders cache', () async { final bytes = Uint8List.fromList(List.generate(64, (i) => i)); fake.forgeInstallerBytes['1.21.1-52.1.5'] = bytes; diff --git a/test/service/loader_catalog_contract_test.dart b/test/service/loader_catalog_contract_test.dart new file mode 100644 index 0000000..959e2c2 --- /dev/null +++ b/test/service/loader_catalog_contract_test.dart @@ -0,0 +1,321 @@ +import 'dart:io'; +import 'dart:isolate'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/app/env.dart'; +import 'package:gitrinth/src/app/loader_registry_provider.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/loader_catalog_cache.dart'; +import 'package:gitrinth/src/service/loader_descriptor_config.dart'; +import 'package:gitrinth/src/service/loader_registry.dart'; +import 'package:gitrinth/src/service/paper_api_client.dart'; +import 'package:gitrinth/src/service/resolve/mod_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/resolve/plugin_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/sponge_api_client.dart'; +import 'package:riverpod/riverpod.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void _writeCatalog(List args) { + final result = args[0] as SendPort; + final cache = LoaderCatalogCache(GitrinthCache(root: args[1] as String)); + try { + for (var i = 0; i < 100; i++) { + cache.write('modrinth', 'https://example.test/v2', [ + { + 'name': '${args[2]}-$i', + 'supported_project_types': ['mod'], + }, + ]); + } + result.send(null); + } on Object catch (error) { + result.send(error.toString()); + } +} + +void main() { + late Directory root; + late Dio dio; + setUp(() { + root = Directory.systemTemp.createTempSync('loader_contract_'); + dio = Dio(); + }); + tearDown(() { + dio.close(force: true); + root.deleteSync(recursive: true); + }); + + test( + 'concurrent provider refreshes keep independent staging files', + () async { + final results = ReceivePort(); + addTearDown(results.close); + await Future.wait([ + for (var i = 0; i < 4; i++) + Isolate.spawn(_writeCatalog, [ + results.sendPort, + root.path, + i, + ]), + ]); + expect(await results.take(4).toList(), everyElement(isNull)); + final cache = LoaderCatalogCache(GitrinthCache(root: root.path)); + expect( + cache.read('modrinth', 'https://example.test/v2')?.single['name'], + isA(), + ); + expect( + root + .listSync(recursive: true) + .whereType() + .where((f) => f.path.contains('.tmp')), + isEmpty, + ); + }, + ); + + test( + 'plugin applicability wins over legacy mod markers in any entry order', + () { + for (final types in [ + [ + ['plugin', 'project', 'mod'], + ], + [ + ['plugin'], + ['mod'], + ], + [ + ['mod'], + ['plugin'], + ], + ]) { + final registry = LoaderRegistryService.mergeModrinth( + LoaderRegistry.builtins, + 'https://example.test/v2', + [ + for (final entry in types) + {'name': 'purpur', 'supported_project_types': entry}, + ], + ); + expect(registry[LoaderId('purpur')]?.role, LoaderRole.plugin); + } + }, + ); + + test( + 'hybrid plugin compatibility accepts Forge and rejects Fabric', + () async { + final registry = LoaderDescriptorConfig.parse(''' +version: 1 +loaders: + hybrid: + role: plugin + modrinth: {https://api.modrinth.com/v2: hybrid} + compatible_mods: [forge] +'''); + for (final loader in ['forge', 'fabric']) { + final container = ProviderContainer( + overrides: [ + environmentProvider.overrideWithValue({ + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + }), + ], + ); + addTearDown(container.dispose); + container.read(loaderRegistryProvider.notifier).replace(registry); + final manifest = parseModsYaml(''' +slug: demo +name: Demo +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: '$loader:1.0', plugins: 'hybrid:1.0'} +mods: {} +''', filePath: 'mods.yaml'); + final prepared = prepareLoaderRegistry( + container, + manifest, + offline: true, + ); + if (loader == 'forge') { + await prepared; + } else { + await expectLater( + prepared, + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf( + contains('hybrid'), + contains('fabric'), + contains('forge'), + ), + ), + ), + ); + } + } + }, + ); + + test( + 'offline selected provider ignores a corrupt disabled catalog', + () async { + const host = 'https://example.test/v2'; + const cfHost = 'https://api.curseforge.com'; + final cache = LoaderCatalogCache(GitrinthCache(root: root.path)); + cache.write('modrinth', host, [ + { + 'name': 'future', + 'supported_project_types': ['mod'], + }, + ]); + final corrupt = File(cache.path('curseforge', cfHost)); + corrupt.parent.createSync(recursive: true); + corrupt.writeAsStringSync('{'); + var requests = 0; + dio.interceptors.add( + InterceptorsWrapper( + onRequest: (options, handler) { + requests++; + handler.reject(DioException(requestOptions: options)); + }, + ), + ); + final service = LoaderRegistryService( + configured: LoaderRegistry.builtins, + cache: cache, + dio: dio, + ); + final registry = await service.snapshot( + modrinthHost: host, + curseforgeHost: cfHost, + needed: [LoaderId('future')], + offline: true, + ); + expect(registry[LoaderId('future')]?.modrinthTag(host), 'future'); + expect(requests, 0); + await expectLater( + service.snapshot( + modrinthHost: host, + curseforgeHost: cfHost, + needed: [LoaderId('future')], + offline: true, + readCurseforge: () async => [], + ), + throwsA(isA()), + ); + expect(requests, 0); + }, + ); + + test('CurseForge Any is not a filterable loader identity', () { + final registry = LoaderRegistryService.mergeCurseforge( + LoaderRegistry.builtins, + [ + {'type': 0, 'name': 'any'}, + {'type': -1, 'name': 'invalid'}, + {'type': 42, 'name': 'future'}, + ], + ); + expect(registry[LoaderId('curseforge-type-0')], isNull); + expect(registry[LoaderId('curseforge-type-42')]?.curseforgeType, 42); + }); + + test( + 'catalog aliases and concrete versions stay within Minecraft releases', + () async { + final id = LoaderId('future'); + final configured = LoaderRegistry.builtins.merge([ + LoaderDescriptor( + id: id, + role: LoaderRole.mod, + curseforgeType: 42, + versionSource: VersionSourceFamily.catalog, + ), + ]); + final registry = LoaderRegistryService.mergeCurseforge(configured, [ + {'type': 42, 'name': 'unscoped', 'latest': true, 'recommended': true}, + { + 'type': 42, + 'name': 'old-latest', + 'gameVersion': '1.20.1', + 'latest': true, + 'recommended': true, + }, + { + 'type': 42, + 'name': 'new-beta', + 'gameVersion': '1.21.1', + 'latest': true, + 'recommended': false, + }, + { + 'type': 42, + 'name': 'new-stable', + 'gameVersion': '1.21.1', + 'latest': false, + 'recommended': true, + }, + ]); + final mods = ModLoaderVersionResolver(dio: dio); + final plugins = PluginLoaderVersionResolver( + dio: dio, + paperApi: PaperApiClient(dio: dio), + spongeApi: SpongeApiClient(dio: dio), + ); + for (final resolve in [mods.resolve, plugins.resolve]) { + expect( + await resolve( + loader: id, + registry: registry, + tag: 'latest', + mcVersion: '1.21.1', + ), + 'new-beta', + ); + expect( + await resolve( + loader: id, + registry: registry, + tag: 'stable', + mcVersion: '1.21.1', + ), + 'new-stable', + ); + expect( + await resolve( + loader: id, + registry: registry, + tag: 'new-stable', + mcVersion: '1.21.1', + ), + 'new-stable', + ); + await expectLater( + resolve( + loader: id, + registry: registry, + tag: 'old-latest', + mcVersion: '1.21.1', + ), + throwsA(isA()), + ); + await expectLater( + resolve( + loader: id, + registry: registry, + tag: 'latest', + mcVersion: '1.19.4', + ), + throwsA(isA()), + ); + } + }, + ); +} diff --git a/test/service/loader_provider_contract_test.dart b/test/service/loader_provider_contract_test.dart new file mode 100644 index 0000000..562d793 --- /dev/null +++ b/test/service/loader_provider_contract_test.dart @@ -0,0 +1,134 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/commands/curseforge_pack_assembler.dart'; +import 'package:gitrinth/src/commands/pack_assembler.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_filters.dart'; +import 'package:gitrinth/src/service/loader_descriptor_config.dart'; +import 'package:test/test.dart'; + +void main() { + test( + 'explicit descriptor type and archive token flow without an enum case', + () async { + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio(); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + }); + final registry = LoaderDescriptorConfig.parse(''' +version: 1 +loaders: + future: + role: mod + curseforge_type: 42 + archive: {curseforge: official-future} +'''); + var requests = 0; + server.listen((request) async { + requests++; + expect(request.uri.path, '/v1/mods/7/files'); + expect(request.uri.queryParameters['modLoaderType'], '42'); + expect(request.uri.queryParameters['gameVersion'], '1.21.1'); + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'data': [ + { + 'id': 70, + 'modId': 7, + 'displayName': '1.0.0', + 'fileName': 'mod.jar', + 'releaseType': 1, + 'fileDate': '2026-01-01', + 'gameVersions': ['1.21.1'], + 'hashes': [], + 'dependencies': [], + }, + ], + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': 1, + 'totalCount': 1, + }, + }), + ); + await request.response.close(); + }); + final api = CurseForgeApi( + dio, + baseUrl: 'http://127.0.0.1:${server.port}', + ); + final files = await listCompatibleFiles( + api: api, + registry: registry, + projectId: 7, + section: Section.mods, + modLoader: LoaderId('future'), + gameVersions: ['1.21.1'], + channel: Channel.release, + ); + expect(files.single.id, 70); + expect(requests, 1); + final loader = LoaderConfig( + mods: LoaderId('future'), + modLoaderVersion: '1.0.0', + ); + final manifest = buildCurseForgeManifest( + yaml: ModsYaml( + slug: 'test', + name: 'Test', + version: '1.0.0', + description: '', + mcVersion: '1.21.1', + loader: loader, + ), + lock: ModsLock( + gitrinthVersion: '0.1.0', + mcVersion: '1.21.1', + loader: loader, + ), + target: PackTarget.client, + publishable: false, + registry: registry, + ); + expect((manifest['minecraft'] as Map)['modLoaders'], [ + {'id': 'official-future-1.0.0', 'primary': true}, + ]); + + final mrOnly = LoaderRegistry([ + LoaderDescriptor( + id: LoaderId('mr-only'), + role: LoaderRole.mod, + modrinthTags: {'*': 'mr-only'}, + ), + ]); + await expectLater( + listCompatibleFiles( + api: api, + registry: mrOnly, + projectId: 7, + section: Section.mods, + modLoader: LoaderId('mr-only'), + gameVersions: ['1.21.1'], + channel: Channel.release, + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('no CurseForge numeric filter mapping'), + ), + ), + ); + expect(requests, 1); + }, + ); +} diff --git a/test/service/loader_registry_runtime_test.dart b/test/service/loader_registry_runtime_test.dart new file mode 100644 index 0000000..d8a3ec1 --- /dev/null +++ b/test/service/loader_registry_runtime_test.dart @@ -0,0 +1,214 @@ +import 'dart:io'; + +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/loader_client_installer.dart'; +import 'package:gitrinth/src/service/server_installer.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + test('Quilt client recipe uses its documented profile ID and CLI', () async { + final root = Directory.systemTemp.createTempSync('quilt_client_recipe_'); + addTearDown(() => root.deleteSync(recursive: true)); + final output = Directory(p.join(root.path, 'client with spaces')); + final installer = File(p.join(root.path, 'installer.jar')) + ..writeAsStringSync('fixture'); + final calls = >[]; + final service = LoaderClientInstaller( + runProcess: + ( + exe, + args, { + workingDirectory, + runInShell = false, + environment, + }) async { + calls.add(args); + return 0; + }, + ); + final id = await service.installClient( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + dotMinecraftDir: output, + installerJar: installer, + offline: false, + ); + expect(id, 'quilt-loader-0.30.2-1.21.1'); + expect(calls.single, [ + '-jar', + installer.path, + 'install', + 'client', + '1.21.1', + '0.30.2', + '--install-dir="${output.path}"', + ]); + }); + for (final version in [ + '../escape', + '--install-dir', + 'http://evil.test', + '..', + '1.0;echo', + ]) { + test( + 'runtime version $version fails before filesystem or process work', + () async { + final root = Directory.systemTemp.createTempSync('unsafe_recipe_'); + addTearDown(() => root.deleteSync(recursive: true)); + final output = Directory(p.join(root.path, 'untouched')); + var calls = 0; + final service = ServerInstaller( + runProcess: + ( + exe, + args, { + workingDirectory, + runInShell = false, + environment, + }) async { + calls++; + return 0; + }, + ); + await expectLater( + service.installServer( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: version, + outputDir: output, + installerOrServerJar: File('unused'), + offline: false, + ), + throwsA(isA()), + ); + expect(calls, 0); + expect(output.existsSync(), isFalse); + }, + ); + } + test( + 'metadata-only loader refuses installation before creating directories', + () async { + final root = Directory.systemTemp.createTempSync('loader_capability_'); + addTearDown(() => root.deleteSync(recursive: true)); + final output = Directory(p.join(root.path, 'untouched')); + final installer = File(p.join(root.path, 'installer.jar')) + ..writeAsStringSync('fixture'); + await expectLater( + ServerInstaller().installServer( + loader: LoaderId('future-loader'), + mcVersion: '1.21.1', + modLoaderVersion: '1.0.0', + outputDir: output, + installerOrServerJar: installer, + offline: false, + ), + throwsA(isA()), + ); + expect(output.existsSync(), isFalse); + }, + ); + + test( + 'Quilt server recipe uses official CLI arguments and validates launcher output', + () async { + final root = Directory.systemTemp.createTempSync('quilt_recipe_'); + addTearDown(() => root.deleteSync(recursive: true)); + final output = Directory(p.join(root.path, 'server with spaces')); + final installer = File(p.join(root.path, 'installer.jar')) + ..writeAsStringSync('fixture'); + final calls = >[]; + final service = ServerInstaller( + runProcess: + ( + exe, + args, { + workingDirectory, + runInShell = false, + environment, + }) async { + calls.add(args); + File( + p.join(output.path, 'quilt-server-launch.jar'), + ).writeAsStringSync('launcher'); + File( + p.join(output.path, 'server.jar'), + ).writeAsStringSync('server'); + return 0; + }, + ); + await service.installServer( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + outputDir: output, + installerOrServerJar: installer, + offline: false, + ); + expect(calls.single, [ + '-jar', + installer.path, + 'install', + 'server', + '1.21.1', + '0.30.2', + '--install-dir="${output.path}"', + '--download-server', + ]); + }, + ); + for (final outputs in [ + [], + ['quilt-server-launch.jar'], + ['server.jar'], + ]) { + test( + 'Quilt exit-zero with incomplete outputs $outputs is not installed', + () async { + final root = Directory.systemTemp.createTempSync('quilt_incomplete_'); + addTearDown(() => root.deleteSync(recursive: true)); + final output = Directory(p.join(root.path, 'server')); + final installer = File(p.join(root.path, 'installer.jar')) + ..writeAsStringSync('fixture'); + final service = ServerInstaller( + runProcess: + ( + exe, + args, { + workingDirectory, + runInShell = false, + environment, + }) async { + for (final filename in outputs) { + File( + p.join(output.path, filename), + ).writeAsStringSync('fixture'); + } + return 0; + }, + ); + await expectLater( + service.installServer( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + outputDir: output, + installerOrServerJar: installer, + offline: false, + ), + throwsA(isA()), + ); + expect( + File( + p.join(output.path, '.gitrinth-installed-quilt-0.30.2'), + ).existsSync(), + isFalse, + ); + }, + ); + } +} diff --git a/test/service/loader_registry_test.dart b/test/service/loader_registry_test.dart new file mode 100644 index 0000000..e027a36 --- /dev/null +++ b/test/service/loader_registry_test.dart @@ -0,0 +1,255 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/loader_catalog_cache.dart'; +import 'package:gitrinth/src/service/loader_descriptor_config.dart'; +import 'package:gitrinth/src/service/loader_registry.dart'; +import 'package:test/test.dart'; + +void main() { + test( + 'online discovery repairs corrupt catalogs while offline stays network-free', + () async { + final root = Directory.systemTemp.createTempSync('loader_recovery_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio(); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + root.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}/v2'; + final cache = LoaderCatalogCache(GitrinthCache(root: root.path)); + final sidecar = File(cache.path('modrinth', host)); + sidecar.parent.createSync(recursive: true); + sidecar.writeAsStringSync('{'); + var requests = 0; + server.listen((request) async { + requests++; + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode([ + { + 'name': 'future', + 'supported_project_types': ['mod'], + }, + ]), + ); + await request.response.close(); + }); + final service = LoaderRegistryService( + configured: LoaderRegistry.builtins, + cache: cache, + dio: dio, + ); + await expectLater( + service.snapshot( + modrinthHost: host, + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId('future')], + offline: true, + ), + throwsA(isA()), + ); + expect(requests, 0); + final repaired = await service.snapshot( + modrinthHost: host, + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId('future')], + refresh: true, + ); + expect(repaired[LoaderId('future')]?.modrinthTag(host), 'future'); + expect(requests, 1); + expect(cache.read('modrinth', host)?.single['name'], 'future'); + }, + ); + + test('descriptors and snapshots defensively copy caller collections', () { + final tags = {'https://example.test/v2': 'future'}; + final versions = ['1.0.0']; + final descriptor = LoaderDescriptor( + id: LoaderId('future'), + role: LoaderRole.mod, + modrinthTags: tags, + versions: versions, + ); + final registry = LoaderRegistry([descriptor]); + tags.clear(); + versions.clear(); + expect( + registry[LoaderId('future')]?.modrinthTag('https://example.test/v2'), + 'future', + ); + expect(descriptor.versions, ['1.0.0']); + expect(() => descriptor.modrinthTags.clear(), throwsUnsupportedError); + expect(() => registry.descriptors.clear(), throwsUnsupportedError); + }); + + test( + 'unknown CF numeric types preserve versions without inventing aliases or recipes', + () { + final registry = LoaderRegistryService.mergeCurseforge( + LoaderRegistry.builtins, + [ + {'type': 42, 'name': 'unmapped-loader-1.0.0'}, + ], + ); + final descriptor = registry[LoaderId('curseforge-type-42')]!; + expect(descriptor.curseforgeType, 42); + expect(descriptor.versions, ['unmapped-loader-1.0.0']); + expect(descriptor.modrinthTags, isEmpty); + expect(descriptor.installer, isNull); + expect(descriptor.curseforgeArchiveToken, isNull); + }, + ); + + test( + 'host catalog mappings remain scoped and remote data grants no runtime', + () { + const entry = { + 'name': 'future', + 'supported_project_types': ['mod', 'modpack'], + 'installer_family': 'forge', + }; + final first = LoaderRegistryService.mergeModrinth( + LoaderRegistry.builtins, + 'https://a.test/v2', + [entry], + ); + final next = LoaderRegistryService.mergeModrinth( + first, + 'https://b.test/v2', + [entry], + ); + final descriptor = next[LoaderId('future')]!; + expect(descriptor.modrinthTag('https://a.test/v2'), 'future'); + expect(descriptor.modrinthTag('https://b.test/v2'), 'future'); + expect(descriptor.modrinthTag('https://c.test/v2'), isNull); + expect(descriptor.installer, isNull); + }, + ); + + for (final bad in [ + '../escape', + '--install-dir', + 'http://evil.test', + 'evil;echo', + ]) { + test( + 'remote loader identity $bad cannot become paths or executable metadata', + () { + expect( + () => LoaderRegistryService.mergeModrinth( + LoaderRegistry.builtins, + 'https://a.test/v2', + [ + { + 'name': bad, + 'supported_project_types': ['mod'], + }, + ], + ), + throwsA(isA()), + ); + }, + ); + } + + test( + 'descriptor mappings accept data and reject unknown keys and collisions', + () { + final registry = LoaderDescriptorConfig.parse(''' +version: 1 +loaders: + future: + role: mod + modrinth: {https://example.test/v2: future-tag} + curseforge_type: 42 + archive: {modrinth: future-loader, curseforge: future} + version_source: catalog +'''); + expect(registry[LoaderId('future')]?.curseforgeType, 42); + expect(registry[LoaderId('future')]?.modrinthArchiveKey, 'future-loader'); + for (final text in [ + 'version: 1\nloaders: {future: {role: mod, command: "echo unsafe"}}', + 'version: 1\nloaders: {forge: {role: mod}}', + 'version: 1\nloaders: {future: {role: mod, modrinth: {https://example.test/v2: fabric}}}', + 'version: 1\nloaders: {future: {role: mod, installer_family: "sh script"}}', + 'version: 1\nloaders: {future: {role: mod, curseforge_type: 4}}', + ]) { + expect( + () => LoaderDescriptorConfig.parse(text), + throwsA(isA()), + ); + } + }, + ); + + test( + 'refresh discovers a provider addition and offline reuses the exact host snapshot', + () async { + final root = Directory.systemTemp.createTempSync('loader_catalog_'); + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final dio = Dio(); + addTearDown(() async { + dio.close(force: true); + await server.close(force: true); + root.deleteSync(recursive: true); + }); + final host = 'http://127.0.0.1:${server.port}/v2'; + final entries = >[]; + var requests = 0; + server.listen((request) async { + requests++; + expect(request.uri.path, '/v2/tag/loader'); + request.response.headers.contentType = ContentType.json; + request.response.write(jsonEncode(entries)); + await request.response.close(); + }); + final service = LoaderRegistryService( + configured: LoaderRegistry.builtins, + cache: LoaderCatalogCache(GitrinthCache(root: root.path)), + dio: dio, + ); + final first = await service.snapshot( + modrinthHost: host, + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId.fabric], + refresh: true, + ); + expect(first[LoaderId('future')], isNull); + entries.add({ + 'name': 'future', + 'supported_project_types': ['mod', 'modpack'], + }); + final next = await service.snapshot( + modrinthHost: host, + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId('future')], + refresh: true, + ); + expect(next[LoaderId('future')]?.modrinthArchiveKey, isNull); + final offline = await service.snapshot( + modrinthHost: host, + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId('future')], + offline: true, + ); + expect(offline[LoaderId('future')]?.modrinthTag(host), 'future'); + expect(requests, 2); + await expectLater( + service.snapshot( + modrinthHost: 'https://other.test/v2', + curseforgeHost: 'https://api.curseforge.com', + needed: [LoaderId('future')], + offline: true, + ), + throwsA(isA()), + ); + expect(requests, 2); + }, + ); +} diff --git a/test/service/mod_loader_version_resolver_test.dart b/test/service/mod_loader_version_resolver_test.dart index 492cc15..d77b2e5 100644 --- a/test/service/mod_loader_version_resolver_test.dart +++ b/test/service/mod_loader_version_resolver_test.dart @@ -1,7 +1,7 @@ import 'package:dio/dio.dart'; import 'package:gitrinth/src/cli/exceptions.dart'; import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; -import 'package:gitrinth/src/service/mod_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/resolve/mod_loader_version_resolver.dart'; import 'package:test/test.dart'; import '../helpers/fake_modrinth.dart'; diff --git a/test/service/modrinth_auth_interceptor_test.dart b/test/service/modrinth_auth_interceptor_test.dart index 02b610b..5c84d77 100644 --- a/test/service/modrinth_auth_interceptor_test.dart +++ b/test/service/modrinth_auth_interceptor_test.dart @@ -2,7 +2,7 @@ import 'dart:typed_data'; import 'package:dio/dio.dart'; import 'package:gitrinth/src/cli/exceptions.dart'; -import 'package:gitrinth/src/service/modrinth_auth_interceptor.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_auth_interceptor.dart'; import 'package:test/test.dart'; class _RecordingAdapter implements HttpClientAdapter { diff --git a/test/service/modrinth_project_url_test.dart b/test/service/modrinth_project_url_test.dart index 392b5c6..b34e3d0 100644 --- a/test/service/modrinth_project_url_test.dart +++ b/test/service/modrinth_project_url_test.dart @@ -1,6 +1,6 @@ import 'package:test/test.dart'; -import 'package:gitrinth/src/service/modrinth_project_url.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_project_url.dart'; void main() { group('parseModrinthProjectUrl', () { diff --git a/test/service/modrinth_rate_limit_interceptor_test.dart b/test/service/modrinth_rate_limit_interceptor_test.dart index 648bd79..3b7184e 100644 --- a/test/service/modrinth_rate_limit_interceptor_test.dart +++ b/test/service/modrinth_rate_limit_interceptor_test.dart @@ -3,7 +3,7 @@ import 'dart:typed_data'; import 'package:dio/dio.dart'; import 'package:gitrinth/src/service/console.dart'; -import 'package:gitrinth/src/service/modrinth_rate_limit_interceptor.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_rate_limit_interceptor.dart'; import 'package:test/test.dart'; typedef _ScriptedResponse = ({ diff --git a/test/service/modrinth_resolution_snapshot_test.dart b/test/service/modrinth_resolution_snapshot_test.dart new file mode 100644 index 0000000..c49f74b --- /dev/null +++ b/test/service/modrinth_resolution_snapshot_test.dart @@ -0,0 +1,208 @@ +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/model/modrinth/game_version.dart'; +import 'package:gitrinth/src/model/modrinth/project.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_api.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_api_source.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_resolution_snapshot.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_url.dart'; +import 'package:retrofit/retrofit.dart'; +import 'package:test/test.dart'; + +void main() { + late _Source transport; + late ModrinthApiSource reads; + setUp(() { + transport = _Source(); + reads = ModrinthResolutionSnapshot(transport); + }); + + test( + 'concurrent public reads retain the first response for this resolve', + () async { + final api = reads.forHost(null); + final results = await Future.wait([ + api.getProject('a'), + api.getProject('a'), + ]); + expect(results.map((p) => p.title), everyElement('first')); + transport.forHost(null).title = 'changed'; + expect((await api.getProject('a')).title, 'first'); + expect(transport.forHost(null).calls, {'project:a': 1}); + }, + ); + + test( + 'full host keys normalize case and slash but retain port and path', + () async { + await reads.forHost('https://EXAMPLE.test:8443/api/v2/').getProject('a'); + await reads.forHost('https://example.test:8443/api/v2').getProject('a'); + await reads.forHost('https://example.test:9443/api/v2').getProject('a'); + await reads.forHost('https://example.test:8443/other/v2').getProject('a'); + expect(transport.apis.length, 3); + expect( + transport.apis.values.map((a) => a.calls['project:a']), + everyElement(1), + ); + }, + ); + + test( + 'version filters stay separate and selected versions cannot truncate raw lists', + () async { + final api = reads.forHost(null); + final all = await api.listVersions('a'); + final selected = all.where((v) => v.id == 'old').toList(); + expect(selected.single.id, 'old'); + expect(() => all.removeLast(), throwsUnsupportedError); + expect((await api.listVersions('a')).map((v) => v.id), ['new', 'old']); + await api.listVersions('a', loadersJson: '["fabric"]'); + await api.listVersions('a', loadersJson: '["forge"]'); + await api.listVersions( + 'a', + loadersJson: '["fabric"]', + gameVersionsJson: '["1.21"]', + ); + await api.listVersions( + 'a', + loadersJson: '["fabric"]', + gameVersionsJson: '["1.20"]', + ); + await api.listVersions( + 'a', + loadersJson: '["fabric"]', + gameVersionsJson: '["1.20"]', + ); + expect(transport.forHost(null).calls.values, everyElement(1)); + expect(transport.forHost(null).calls.length, 5); + }, + ); + + test( + 'member and search data remain immutable and search arguments are isolated', + () async { + final api = reads.forHost(null); + final members = await api.getProjectMembers('a'); + expect( + () => members.data[0]['user']['username'] = 'changed', + throwsUnsupportedError, + ); + await api.getProjectMembers('a'); + final hits = await api.searchProjects(query: 'a', facets: '[]'); + expect(() => hits.data['hits'].clear(), throwsUnsupportedError); + await api.searchProjects(query: 'a', facets: '[]'); + await api.searchProjects(query: 'b', facets: '[]'); + await api.searchProjects(query: 'a', facets: '[[]]'); + await api.searchProjects(query: 'a', facets: '[]', limit: 40); + expect(transport.forHost(null).calls.values, everyElement(1)); + expect(transport.forHost(null).calls.length, 5); + }, + ); + + test('auth and non-resolution endpoints are never retained', () async { + final api = reads.forHost(null); + for (var i = 0; i < 2; i++) { + await api.getCurrentUser(); + await api.checkProjectValidity('a'); + await api.getGameVersions(); + } + expect(transport.forHost(null).calls.values, everyElement(2)); + }); + + test( + 'a new resolution observes fresh metadata from the same transport', + () async { + expect((await reads.forHost(null).getProject('a')).title, 'first'); + transport.forHost(null).title = 'second'; + final next = ModrinthResolutionSnapshot(transport); + expect((await next.forHost(null).getProject('a')).title, 'second'); + expect((await reads.forHost(null).getProject('a')).title, 'first'); + expect(transport.forHost(null).calls['project:a'], 2); + }, + ); +} + +class _Source implements ModrinthApiSource { + @override + final String defaultBaseUrl = 'https://example.test/v2'; + final apis = {}; + @override + _Api forHost(String? hostUrl) => + apis.putIfAbsent(normalizeServerKey(hostUrl ?? defaultBaseUrl), _Api.new); +} + +class _Api implements ModrinthApi { + final calls = {}; + String title = 'first'; + void record(String key) => calls.update(key, (n) => n + 1, ifAbsent: () => 1); + HttpResponse response(dynamic body) => + HttpResponse(body, Response(requestOptions: RequestOptions(path: '/'))); + @override + Future getProject(String slug) async { + record('project:$slug'); + return Project(id: slug, slug: slug, title: title, projectType: 'mod'); + } + + @override + Future> listVersions( + String slug, { + String? loadersJson, + String? gameVersionsJson, + }) async { + record('versions:$slug:$loadersJson:$gameVersionsJson'); + return [ + for (final id in ['new', 'old']) + Version( + id: id, + projectId: slug, + versionNumber: '1.0', + files: [], + dependencies: [], + loaders: [], + gameVersions: [], + ), + ]; + } + + @override + Future> getProjectMembers(String slug) async { + record('members:$slug'); + return response([ + { + 'user': {'username': 'author'}, + }, + ]); + } + + @override + Future> searchProjects({ + required String query, + required String facets, + int limit = 20, + }) async { + record('search:$query:$facets:$limit'); + return response({ + 'hits': [ + {'project_id': 'a'}, + ], + }); + } + + @override + Future> getCurrentUser() async { + record('user'); + return response({'username': 'author'}); + } + + @override + Future> checkProjectValidity(String slug) async { + record('check:$slug'); + return response({'id': slug}); + } + + @override + Future> getGameVersions() async { + record('game-versions'); + return []; + } +} diff --git a/test/service/pack_archive_writer_test.dart b/test/service/pack_archive_writer_test.dart new file mode 100644 index 0000000..145237f --- /dev/null +++ b/test/service/pack_archive_writer_test.dart @@ -0,0 +1,92 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:archive/archive.dart'; +import 'package:gitrinth/src/service/pack_archive_writer.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + late Directory dir; + setUp(() => dir = Directory.systemTemp.createTempSync('pack-writer-')); + tearDown(() => dir.deleteSync(recursive: true)); + + test( + 'encoding failure preserves the destination and removes temporary output', + () { + final output = File('${dir.path}/pack.zip') + ..writeAsStringSync('original'); + final source = File('${dir.path}/source')..writeAsStringSync('contents'); + Iterable<({String path, File source})> entries() sync* { + yield (path: 'overrides/source', source: source); + throw StateError('source discovery failed'); + } + + expect( + () => writeArchive( + output, + manifestName: 'manifest.json', + manifest: utf8.encode('{}'), + entries: entries(), + ), + throwsStateError, + ); + expect(output.readAsStringSync(), 'original'); + expect( + dir.listSync().map((file) => p.basename(file.path)), + unorderedEquals(['pack.zip', 'source']), + ); + }, + ); + + test( + 'streams entries in order with existing compression and mode defaults', + () { + final output = File('${dir.path}/pack.zip') + ..writeAsStringSync('previous'); + final source = File('${dir.path}/source')..writeAsStringSync('contents'); + writeArchive( + output, + manifestName: 'manifest.json', + manifest: utf8.encode('{}'), + entries: [(path: 'overrides/source', source: source)], + ); + final archive = ZipDecoder().decodeBytes(output.readAsBytesSync()); + expect(archive.map((entry) => entry.name), [ + 'manifest.json', + 'overrides/source', + ]); + expect(utf8.decode(archive.first.content), '{}'); + expect(utf8.decode(archive.last.content), 'contents'); + expect(archive.map((entry) => entry.mode & 0x1ff), everyElement(0x1a4)); + expect( + archive.map((entry) => entry.compression), + everyElement(CompressionType.deflate), + ); + // ZIP stores local wall time; archive decodes those fields as UTC. + final now = DateTime.now(); + expect( + archive.map((entry) => entry.lastModDateTime.millisecondsSinceEpoch), + everyElement( + closeTo(now.add(now.timeZoneOffset).millisecondsSinceEpoch, 5000), + ), + ); + }, + ); + + test('failed destination replacement removes temporary output', () { + final existing = Directory('${dir.path}/pack.zip')..createSync(); + File('${existing.path}/keep').writeAsStringSync('original'); + expect( + () => writeArchive( + File(existing.path), + manifestName: 'manifest.json', + manifest: utf8.encode('{}'), + entries: [], + ), + throwsA(isA()), + ); + expect(File('${existing.path}/keep').readAsStringSync(), 'original'); + expect(dir.listSync().map((entry) => p.basename(entry.path)), ['pack.zip']); + }); +} diff --git a/test/service/plugin_loader_version_resolver_test.dart b/test/service/plugin_loader_version_resolver_test.dart index c186753..7846991 100644 --- a/test/service/plugin_loader_version_resolver_test.dart +++ b/test/service/plugin_loader_version_resolver_test.dart @@ -1,9 +1,10 @@ import 'dart:io'; import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:gitrinth/src/service/paper_api_client.dart'; -import 'package:gitrinth/src/service/plugin_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/resolve/plugin_loader_version_resolver.dart'; import 'package:gitrinth/src/service/sponge_api_client.dart'; import 'package:test/test.dart'; @@ -46,6 +47,48 @@ void main() { ); } + test('catalog plugin versions preserve explicit concrete requests', () async { + final loader = LoaderId('future-plugin'); + final registry = LoaderRegistry.builtins.merge([ + LoaderDescriptor( + id: loader, + role: LoaderRole.plugin, + versionSource: VersionSourceFamily.catalog, + versions: ['3.0.0', '2.0.0'], + ), + ]); + final r = resolver(); + for (final tag in ['stable', 'latest']) { + expect( + await r.resolve( + loader: loader, + registry: registry, + tag: tag, + mcVersion: '1.21.1', + ), + '3.0.0', + ); + } + expect( + await r.resolve( + loader: loader, + registry: registry, + tag: '2.0.0', + mcVersion: '1.21.1', + ), + '2.0.0', + ); + await expectLater( + r.resolve( + loader: loader, + registry: registry, + tag: '1.0.0', + mcVersion: '1.21.1', + ), + throwsA(isA()), + ); + }); + test( 'Paper stable filters STABLE while latest accepts any channel', () async { @@ -93,6 +136,60 @@ void main() { }, ); + test('Fill v3 uses ids and the supplied object download URL', () async { + stub.listen((req) async { + expect( + req.headers.value('user-agent'), + contains('github.com/getBoolean/gitrinth'), + ); + req.response.headers.contentType = ContentType.json; + req.response.write(''' +[ + {"id": 12, "channel": "ALPHA", "downloads": {"server:default": {"name": "paper-12.jar", "url": "https://fill-data.papermc.io/objects/12/paper.jar"}}}, + {"id": 10, "channel": "STABLE", "downloads": {"server:default": {"name": "paper-10.jar", "url": "https://fill-data.papermc.io/objects/10/paper.jar"}}}, + {"id": 11, "channel": "STABLE", "downloads": {"server:default": {"name": "paper-11.jar", "url": "https://fill-data.papermc.io/objects/11/paper.jar"}}} +] +'''); + await req.response.close(); + }); + final api = PaperApiClient( + dio: dio, + buildsUrlTemplate: 'http://127.0.0.1:${stub.port}/builds', + ); + final stable = await api.latestStableBuild(project: 'paper', mc: '1.21.1'); + expect(stable.build, 11); + expect( + stable.downloadUrl.toString(), + 'https://fill-data.papermc.io/objects/11/paper.jar', + ); + expect((await api.latestBuild(project: 'paper', mc: '1.21.1')).build, 12); + expect( + (await api.buildByNumber( + project: 'paper', + mc: '1.21.1', + build: 10, + )).filename, + 'paper-10.jar', + ); + }); + + test('Fill v3 rejects a build without a valid download URL', () async { + stub.listen((req) async { + req.response.write( + '[{"id":1,"channel":"STABLE","downloads":{"server:default":{"name":"paper.jar"}}}]', + ); + await req.response.close(); + }); + final api = PaperApiClient( + dio: dio, + buildsUrlTemplate: 'http://127.0.0.1:${stub.port}/builds', + ); + await expectLater( + api.latestStableBuild(project: 'paper', mc: '1.21.1'), + throwsA(isA()), + ); + }); + test('Sponge stable uses recommended builds while latest uses any', () async { stub.listen((req) async { if (req.uri.path == '/sponge/artifacts/spongeforge/versions' && diff --git a/test/service/plugin_server_e2e_test.dart b/test/service/plugin_server_e2e_test.dart index c3cea29..21112b7 100644 --- a/test/service/plugin_server_e2e_test.dart +++ b/test/service/plugin_server_e2e_test.dart @@ -94,7 +94,7 @@ void main() { expect(build.build, greaterThan(0)); expect(build.filename, contains('paper-1.21.1-')); expect(build.filename, endsWith('.jar')); - expect(build.downloadUrl.host, 'api.papermc.io'); + expect(build.downloadUrl.host, 'fill-data.papermc.io'); await headOk(build.downloadUrl); }); diff --git a/test/service/quilt_installer_metadata_test.dart b/test/service/quilt_installer_metadata_test.dart new file mode 100644 index 0000000..1e66c1b --- /dev/null +++ b/test/service/quilt_installer_metadata_test.dart @@ -0,0 +1,143 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/downloader.dart'; +import 'package:gitrinth/src/service/loader_binary_fetcher.dart'; +import 'package:gitrinth/src/service/offline_guard_interceptor.dart'; +import 'package:test/test.dart'; + +class _Adapter implements HttpClientAdapter { + final Object metadata; + final List bytes; + int calls = 0; + _Adapter(this.metadata, this.bytes); + + @override + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { + calls++; + if (options.uri.host == 'meta.quiltmc.org') { + return ResponseBody.fromString( + jsonEncode(metadata), + 200, + headers: { + Headers.contentTypeHeader: ['application/json'], + }, + ); + } + return ResponseBody.fromBytes(bytes, 200); + } + + @override + void close({bool force = false}) {} +} + +void main() { + late Directory root; + late GitrinthCache cache; + late Dio dio; + late LoaderBinaryFetcher fetcher; + late String destination; + final bytes = utf8.encode('quilt installer'); + const url = + 'https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/1.0.0/quilt-installer-1.0.0.jar'; + Map metadata() => { + 'version': '1.0.0', + 'url': url, + 'hashes': {'sha512': sha512.convert(bytes).toString()}, + }; + Future fetch() => fetcher.fetchServerArtifact( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + ); + + setUp(() { + root = Directory.systemTemp.createTempSync('quilt_metadata_'); + cache = GitrinthCache(root: root.path); + dio = Dio(); + fetcher = LoaderBinaryFetcher( + cache: cache, + downloader: Downloader(dio: dio, cache: cache), + environment: {}, + ); + destination = cache.loaderArtifactPath( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + filename: 'quilt-installer.jar', + ); + }); + tearDown(() { + dio.close(force: true); + root.deleteSync(recursive: true); + }); + + test('corrupt offline metadata makes no transport calls', () async { + File('$destination.metadata.json') + ..parent.createSync(recursive: true) + ..writeAsStringSync('{'); + final adapter = _Adapter([metadata()], bytes); + dio.httpClientAdapter = adapter; + dio.interceptors.add(OfflineGuardInterceptor(() => true)); + await expectLater( + fetch(), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('offline'), + ), + ), + ); + expect(adapter.calls, 0); + expect(File('$destination.metadata.json').readAsStringSync(), '{'); + }); + + for (final invalid in ['url', 'missing hash', 'malformed hash']) { + test( + 'rejects provider metadata with $invalid before artifact transport', + () async { + final value = metadata(); + if (invalid == 'url') { + value['url'] = 'https://example.com/installer.jar'; + } + if (invalid == 'missing hash') value.remove('hashes'); + if (invalid == 'malformed hash') value['hashes'] = {'sha512': 'bad'}; + final adapter = _Adapter([value], bytes); + dio.httpClientAdapter = adapter; + await expectLater(fetch(), throwsA(isA())); + expect(adapter.calls, 1); + expect(File(destination).existsSync(), isFalse); + expect(File('$destination.metadata.json').existsSync(), isFalse); + }, + ); + } + + test('mismatching artifact never persists installer or metadata', () async { + dio.httpClientAdapter = _Adapter([metadata()], utf8.encode('tampered')); + await expectLater(fetch(), throwsA(isA())); + expect(File(destination).existsSync(), isFalse); + expect(File('$destination.metadata.json').existsSync(), isFalse); + }); + + test('verified download persists reusable metadata and bytes', () async { + final adapter = _Adapter([metadata()], bytes); + dio.httpClientAdapter = adapter; + expect((await fetch()).readAsBytesSync(), bytes); + expect(adapter.calls, 2); + expect( + jsonDecode(File('$destination.metadata.json').readAsStringSync()), + metadata(), + ); + }); +} diff --git a/test/service/quilt_runtime_smoke_test.dart b/test/service/quilt_runtime_smoke_test.dart new file mode 100644 index 0000000..c92b269 --- /dev/null +++ b/test/service/quilt_runtime_smoke_test.dart @@ -0,0 +1,70 @@ +@Tags(['network']) +library; + +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/downloader.dart'; +import 'package:gitrinth/src/service/loader_binary_fetcher.dart'; +import 'package:gitrinth/src/service/server_installer.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +void main() { + if (Platform.environment['GITRINTH_NETWORK_TESTS'] != '1') { + test( + 'Quilt runtime smoke', + () {}, + skip: 'set GITRINTH_NETWORK_TESTS=1 with Java 21 available', + ); + return; + } + test( + 'official Quilt installer creates its server runtime in an isolated directory', + () async { + final root = Directory.systemTemp.createTempSync('quilt_runtime_smoke_'); + final dio = Dio( + BaseOptions( + connectTimeout: const Duration(seconds: 30), + receiveTimeout: const Duration(minutes: 2), + ), + ); + addTearDown(() { + dio.close(force: true); + root.deleteSync(recursive: true); + }); + final cache = GitrinthCache(root: p.join(root.path, 'cache')); + final fetcher = LoaderBinaryFetcher( + cache: cache, + downloader: Downloader(dio: dio, cache: cache), + ); + final installer = await fetcher.fetchServerArtifact( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + ); + final output = Directory(p.join(root.path, 'server with spaces')); + await ServerInstaller().installServer( + loader: LoaderId.quilt, + mcVersion: '1.21.1', + modLoaderVersion: '0.30.2', + outputDir: output, + installerOrServerJar: installer, + offline: false, + ); + expect( + File(p.join(output.path, 'quilt-server-launch.jar')).lengthSync(), + greaterThan(0), + ); + expect( + File(p.join(output.path, 'server.jar')).lengthSync(), + greaterThan(0), + ); + expect(Directory(p.join(output.path, 'libraries')).existsSync(), isTrue); + expect(File(p.join(output.path, 'eula.txt')).existsSync(), isFalse); + }, + timeout: const Timeout(Duration(minutes: 5)), + ); +} diff --git a/test/service/resolve/curseforge_relationships_test.dart b/test/service/resolve/curseforge_relationships_test.dart new file mode 100644 index 0000000..2e47dad --- /dev/null +++ b/test/service/resolve/curseforge_relationships_test.dart @@ -0,0 +1,395 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:archive/archive.dart'; +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../../helpers/capture.dart'; +import '../../helpers/fake_modrinth.dart'; + +void main() { + late Directory root; + late FakeModrinth mr; + late HttpServer cf; + late Map>> files; + late Map environment; + final bytes = [1, 2, 3]; + final digest = sha1.convert(bytes).toString(); + setUp(() async { + root = Directory.systemTemp.createTempSync('cf_relationships_'); + mr = FakeModrinth(); + await mr.start(); + cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + final host = 'http://127.0.0.1:${cf.port}'; + files = { + for (final id in [1, 2, 3]) + id: [ + { + 'id': id * 10, + 'modId': id, + 'displayName': '1.0.0', + 'fileName': '$id.jar', + 'releaseType': 1, + 'fileDate': '2026-01-01', + 'gameVersions': ['1.21.1'], + 'hashes': [ + {'value': digest, 'algo': 1}, + ], + 'dependencies': >[], + 'downloadUrl': '$host/artifact/$id', + }, + ], + }; + cf.listen((request) async { + final parts = request.uri.pathSegments; + if (parts.first == 'artifact') { + request.response.add(bytes); + } else { + final id = int.parse(parts[2]); + final dynamic data = parts.length == 3 + ? { + 'id': id, + 'gameId': 432, + 'classId': 6, + 'slug': 'project-$id', + 'name': 'Project $id', + } + : files[id]; + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'data': data, + if (data is List) + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': data.length, + 'totalCount': data.length, + }, + }), + ); + } + await request.response.close(); + }); + environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_CONFIG': p.join(root.path, 'config.yaml'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_NEOFORGE_VERSIONS_URL': mr.neoforgeVersionsUrl, + 'GITRINTH_CURSEFORGE_URL': host, + 'GITRINTH_CURSEFORGE_TOKEN': 'test-key', + }; + }); + tearDown(() async { + await cf.close(force: true); + await mr.stop(); + root.deleteSync(recursive: true); + }); + void manifest(String mods, {String overrides = ''}) => + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'neoforge:21.1.50'} +gitrinth: {modrinth: disabled, curseforge: enabled} +mods: +$mods +$overrides +'''); + Future get({bool offline = false}) => runCli([ + '-C', + root.path, + 'get', + '--no-search', + if (offline) '--offline', + ], environment: environment); + File lock() => File(p.join(root.path, 'mods.lock')); + File metadata(int project, int file) => File( + p.join( + root.path, + 'cache', + 'curseforge', + '$project', + '$file', + 'metadata.json', + ), + ); + void incompatible(int a, int b) => files[a]!.first['dependencies'] = [ + {'modId': b, 'relationType': 5}, + ]; + + for (final reversed in [false, true]) { + test( + 'incompatibility rejects selected aliases before lock replacement, reversed=$reversed', + () async { + final entries = [ + ' alpha: {curseforge: "1"}', + ' beta: {curseforge: "2"}', + ]; + manifest((reversed ? entries.reversed : entries).join('\n')); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + final before = lock().readAsStringSync(); + incompatible(1, 2); + final result = await get(); + expect( + result.exitCode, + 2, + reason: '${result.stdout}\n${result.stderr}', + ); + expect( + result.stderr, + allOf( + contains('alpha'), + contains('beta'), + contains('10'), + contains('20'), + contains('compatible pin'), + ), + ); + expect(lock().readAsStringSync(), before); + }, + ); + } + test( + 'required transitive project conflicts with final selected file', + () async { + manifest(' alpha: {curseforge: "1"}'); + files[1]!.first['dependencies'] = [ + {'modId': 2, 'relationType': 3}, + {'modId': 2, 'relationType': 5}, + ]; + final result = await get(); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, allOf(contains('alpha'), contains('project-2'))); + expect(lock().existsSync(), isFalse); + }, + ); + test('aliases produce one project conflict diagnostic', () async { + manifest( + ' alpha: {curseforge: "1"}\n alpha-copy: {curseforge: "1"}\n beta: {curseforge: "2"}', + ); + incompatible(1, 2); + final result = await get(); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('alpha-copy')); + expect('incompatible with'.allMatches(result.stderr), hasLength(1)); + }); + for (final overridden in ['alpha', 'beta']) { + test( + 'project override bypasses either incompatibility endpoint $overridden', + () async { + manifest( + ' alpha: {curseforge: "1"}\n beta: {curseforge: "2"}', + overrides: 'project_overrides:\n $overridden: 1.0.0', + ); + incompatible(1, 2); + final result = await get(); + expect(result.exitCode, 0, reason: result.stderr); + }, + ); + } + test( + 'selected metadata persists raw relations and offline rejects conflicts', + () async { + manifest(' alpha: {curseforge: "1"}\n beta: {curseforge: "2"}'); + final ignored = [ + for (final code in [1, 2, 4, 6, 99]) {'modId': 3, 'relationType': code}, + ]; + files[1]!.first['dependencies'] = ignored; + final result = await get(); + expect(result.exitCode, 0, reason: result.stderr); + expect( + parseModsLock( + lock().readAsStringSync(), + filePath: lock().path, + ).mods.keys, + unorderedEquals(['alpha', 'beta']), + ); + final saved = + jsonDecode(metadata(1, 10).readAsStringSync()) + as Map; + expect(saved['schemaVersion'], 1); + expect(saved['projectId'], 1); + expect(saved['fileId'], 10); + expect(saved['file']['dependencies'], ignored); + saved['file']['dependencies'] = [ + {'modId': 2, 'relationType': 5}, + ]; + metadata(1, 10).writeAsStringSync(jsonEncode(saved)); + final before = lock().readAsStringSync(); + final offline = await get(offline: true); + expect(offline.exitCode, 2, reason: offline.stderr); + expect(offline.stderr, contains('incompatible with')); + expect(lock().readAsStringSync(), before); + }, + ); + for (final problem in [ + 'missing', + 'corrupt', + 'identity', + 'unavailable', + 'changed-hash', + 'missing-hash', + ]) { + test('offline $problem metadata fails before lock writes', () async { + manifest(' alpha: {curseforge: "1"}'); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + final before = lock().readAsStringSync(); + final sidecar = metadata(1, 10); + if (problem == 'missing') { + if (sidecar.existsSync()) sidecar.deleteSync(); + } else if (problem == 'corrupt') { + sidecar.writeAsStringSync('{'); + } else { + final saved = + jsonDecode(sidecar.readAsStringSync()) as Map; + if (problem == 'identity') saved['projectId'] = 999; + if (problem == 'unavailable') saved['file']['isAvailable'] = false; + if (problem == 'changed-hash') { + saved['file']['hashes'] = [ + {'algo': 1, 'value': 'different-sha1'}, + ]; + } + if (problem == 'missing-hash') saved['file']['hashes'] = []; + sidecar.writeAsStringSync(jsonEncode(saved)); + } + final result = await get(offline: true); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('online')); + expect(lock().readAsStringSync(), before); + }); + } + test('metadata write failure preserves previous lock', () async { + manifest(' alpha: {curseforge: "1"}'); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + final before = lock().readAsStringSync(); + files[1]!.first['id'] = 11; + final obstruction = File( + p.join(root.path, 'cache', 'curseforge', '1', '11'), + ); + obstruction.parent.createSync(recursive: true); + obstruction.writeAsStringSync('blocks metadata directory'); + final result = await get(); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('metadata')); + expect(lock().readAsStringSync(), before); + }); + + test('artifact cannot replace its selected metadata sidecar', () async { + manifest(' alpha: {curseforge: "1"}'); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + final before = lock().readAsStringSync(); + files[1]!.first['fileName'] = 'metadata.json'; + final result = await get(); + expect(result.exitCode, 2, reason: result.stderr); + expect(result.stderr, contains('metadata.json')); + expect(lock().readAsStringSync(), before); + }); + + test('artifact cannot occupy the metadata writer lock path', () async { + manifest(' alpha: {curseforge: "1"}'); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + final before = lock().readAsStringSync(); + files[1]!.first['fileName'] = 'metadata.json.lock'; + final result = await get(); + expect(result.exitCode, 2, reason: result.stderr); + expect(lock().readAsStringSync(), before); + }); + for (final selectedConflicts in [false, true]) { + test( + 'hash-selected older file owns final relations, conflicts=$selectedConflicts', + () async { + final hosted = FakeModrinth(); + await hosted.start(); + addTearDown(hosted.stop); + hosted.registerVersion(slug: 'alpha', versionNumber: '1.0.0'); + hosted.artifacts['alpha/alpha-1.0.0.jar'] = Uint8List.fromList(bytes); + final mrFile = + (hosted.versions['alpha']!.single['files'] as List).single + as Map; + (mrFile['hashes'] as Map)['sha1'] = digest; + (mrFile['hashes'] as Map)['sha512'] = sha512.convert(bytes).toString(); + manifest( + ' alpha: {curseforge: "1", modrinth_host: "${hosted.baseUrl}", sources: [modrinth, curseforge]}\n beta: {curseforge: "2"}', + ); + final older = {...files[1]!.first, 'id': 9, 'fileDate': '2025-01-01'}; + files[1]!.first['hashes'] = [ + { + 'value': sha1.convert([9]).toString(), + 'algo': 1, + }, + ]; + final conflict = [ + {'modId': 2, 'relationType': 5}, + ]; + files[1]!.first['dependencies'] = selectedConflicts ? [] : conflict; + older['dependencies'] = selectedConflicts ? conflict : []; + files[1]!.add(older); + final result = await get(); + expect( + result.exitCode, + selectedConflicts ? 2 : 0, + reason: '${result.stdout}\n${result.stderr}', + ); + if (selectedConflicts) { + expect( + result.stderr, + allOf(contains('files 9'), contains('incompatible with')), + ); + expect(lock().existsSync(), isFalse); + } else { + final saved = + jsonDecode(metadata(1, 9).readAsStringSync()) + as Map; + expect(saved['file']['dependencies'], isEmpty); + expect(metadata(1, 10).existsSync(), isFalse); + final beforePack = lock().readAsStringSync(); + final selected = parseModsLock( + beforePack, + filePath: lock().path, + ).mods['alpha']!; + expect(selected.modrinth!.host, hosted.baseUrl); + expect(selected.curseforge!.fileId, 9); + final requestsBeforePack = Map.of(hosted.requestCounts); + final packed = await runCli([ + '-C', + root.path, + 'pack', + '--curseforge', + '--offline', + ], environment: environment); + expect( + packed.exitCode, + 0, + reason: '${packed.stdout}\n${packed.stderr}', + ); + final archive = ZipDecoder().decodeBytes( + File( + p.join(root.path, 'build', 'pack-1.0.0.zip'), + ).readAsBytesSync(), + ); + final packedManifest = jsonDecode( + utf8.decode(archive.findFile('manifest.json')!.content), + ); + expect( + packedManifest['files'], + contains(equals({'projectID': 1, 'fileID': 9, 'required': true})), + ); + expect(lock().readAsStringSync(), beforePack); + expect(hosted.requestCounts, requestsBeforePack); + } + }, + ); + } +} diff --git a/test/service/resolve/curseforge_resolver_test.dart b/test/service/resolve/curseforge_resolver_test.dart new file mode 100644 index 0000000..0a464f3 --- /dev/null +++ b/test/service/resolve/curseforge_resolver_test.dart @@ -0,0 +1,620 @@ +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/model/curseforge/cf_constants.dart'; +import 'package:gitrinth/src/model/curseforge/cf_file_hash.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/model/curseforge/cf_search_response.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/resolve/curseforge_resolver.dart'; +import 'package:test/test.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; +import 'package:gitrinth/src/service/resolve/lock_builder.dart'; +import 'package:gitrinth/src/service/resolve/resolve_and_sync.dart'; +import 'package:gitrinth/src/model/resolver/result.dart'; + +class _FakeCfApi implements CurseForgeApi { + /// slug → Mod (or null to simulate not-found) + final Map projectsBySlug; + + /// projectId → list of ModFiles (paginated transparently) + final Map> filesByProject; + + /// id → Mod for getMod() + final Map modsById; + final Object? lookupError; + final Object? listingError; + + final List calls = []; + final List requestedGameVersions = []; + + _FakeCfApi({ + this.projectsBySlug = const {}, + this.filesByProject = const {}, + this.modsById = const {}, + this.lookupError, + this.listingError, + }); + + @override + Future searchMods({ + required int gameId, + int? classId, + String? slug, + String? searchFilter, + int? index, + int? pageSize, + }) async { + calls.add('searchMods($slug)'); + final error = lookupError; + if (error != null) throw error; + final found = projectsBySlug[slug]; + return ModSearchResponse( + data: found == null ? const [] : [found], + pagination: Pagination( + index: 0, + pageSize: pageSize ?? 0, + resultCount: found == null ? 0 : 1, + totalCount: found == null ? 0 : 1, + ), + ); + } + + @override + Future getMod(int id) async { + calls.add('getMod($id)'); + final m = modsById[id]; + if (m == null) throw StateError('no fake mod for id=$id'); + return ModEnvelope(data: m); + } + + @override + Future listFiles( + int modId, { + String? gameVersion, + int? modLoaderType, + int? index, + int? pageSize, + }) async { + calls.add('listFiles($modId)'); + requestedGameVersions.add(gameVersion); + final error = listingError; + if (error != null) throw error; + final files = filesByProject[modId] ?? const []; + return ModFileSearchResponse( + data: files, + pagination: Pagination( + index: 0, + pageSize: pageSize ?? 50, + resultCount: files.length, + totalCount: files.length, + ), + ); + } + + @override + Future getFile(int modId, int fileId) async { + throw UnimplementedError(); + } +} + +ModFile _file({ + required int id, + required int modId, + required String fileName, + required String fileDate, + int releaseType = 1, // release + List gameVersions = const ['1.21.5'], + String? sha1, + String? downloadUrl = 'https://example.com/x.jar', +}) => ModFile( + id: id, + modId: modId, + displayName: fileName, + fileName: fileName, + releaseType: releaseType, + fileDate: fileDate, + gameVersions: gameVersions, + hashes: sha1 == null ? const [] : [FileHash(value: sha1, algoCode: 1)], + dependencies: const [], + downloadUrl: downloadUrl, +); + +ModsYaml _pack({ + ModLoader mods = ModLoader.fabric, + PluginLoader? plugins, + GitrinthPlatformState curseforge = GitrinthPlatformState.enabled, + Map modsMap = const {}, + Map pluginsMap = const {}, +}) => ModsYaml( + slug: 'pack', + name: 'pack', + version: '0.0.1', + description: '', + loader: LoaderConfig( + mods: mods, + modLoaderVersion: '0.16.0', + plugins: plugins, + ), + mcVersion: '1.21.5', + gitrinth: GitrinthConfig(curseforge: curseforge), + mods: modsMap, + plugins: pluginsMap, +); + +void main() { + // The wrapper test adapter above is a bit indirect; verify it builds + // a ModFile that compiles (compile-time check only). + test('test harness builds a ModFile', () { + final f = _file( + id: 1, + modId: 100, + fileName: 'x.jar', + fileDate: '2025-01-01T00:00:00Z', + ); + expect(f.id, 1); + }); + + group('resolveCurseforgeEntries', () { + test( + 'accepts_mc queries both versions and selects the newest file', + () async { + final api = _FakeCfApi( + projectsBySlug: { + 'jei': const Mod( + id: 238222, + gameId: 432, + name: 'JEI', + slug: 'jei', + classId: 6, + ), + }, + filesByProject: { + 238222: [ + _file( + id: 1, + modId: 238222, + fileName: 'current.jar', + fileDate: '2025-01-01', + ), + _file( + id: 2, + modId: 238222, + fileName: 'accepted.jar', + fileDate: '2025-02-01', + gameVersions: ['1.21.4'], + ), + ], + }, + ); + final result = await resolveCurseforgeEntries( + manifest: _pack( + modsMap: { + 'jei': const ModEntry(slug: 'jei', acceptsMc: ['1.21.4']), + }, + ), + api: api, + console: const Console(level: LogLevel.error), + ); + expect(api.requestedGameVersions, ['1.21.5', '1.21.4']); + expect(result.resolved.values.single.file.id, 2); + expect(result.resolved.values.single.candidates.map((f) => f.id), [ + 2, + 1, + ]); + }, + ); + + test( + 'lookup authentication failure preserves its actionable cause', + () async { + final api = _FakeCfApi( + lookupError: DioException( + requestOptions: RequestOptions(path: '/v1/mods/search'), + error: const AuthenticationError('CurseForge rejected the API key'), + ), + ); + await expectLater( + resolveCurseforgeEntries( + manifest: _pack(modsMap: {'jei': const ModEntry(slug: 'jei')}), + api: api, + console: const Console(level: LogLevel.error), + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('API key'), + ), + ), + ); + }, + ); + + test('listing network failure is not a missing compatible file', () async { + final api = _FakeCfApi( + projectsBySlug: { + 'jei': const Mod( + id: 238222, + gameId: 432, + name: 'JEI', + slug: 'jei', + classId: 6, + ), + }, + listingError: DioException( + requestOptions: RequestOptions(path: '/v1/mods/238222/files'), + type: DioExceptionType.connectionError, + message: 'connection refused', + ), + ); + await expectLater( + resolveCurseforgeEntries( + manifest: _pack(modsMap: {'jei': const ModEntry(slug: 'jei')}), + api: api, + console: const Console(level: LogLevel.error), + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf(contains('238222'), contains('connection refused')), + ), + ), + ); + }); + + test( + 'nullable content type reports project identity before file selection', + () async { + final project = ModMapper.fromMap({ + 'id': 42, + 'gameId': 432, + 'name': 'Unknown', + 'slug': 'unknown', + 'classId': null, + }); + final api = _FakeCfApi(modsById: {42: project}); + final manifest = _pack( + modsMap: { + 'alias': const ModEntry( + slug: 'alias', + curseforgeSlug: '42', + sources: {SourceKind.curseforge}, + ), + }, + ); + await expectLater( + resolveCurseforgeEntries( + manifest: manifest, + api: api, + console: const Console(level: LogLevel.error), + ), + throwsA( + isA().having( + (e) => e.toString(), + 'message', + allOf( + contains('unknown'), + contains('42'), + contains('content type'), + ), + ), + ), + ); + expect(api.calls, ['getMod(42)']); + }, + ); + + test('same local key retains both CurseForge sections', () async { + final api = _FakeCfApi( + modsById: { + 1: Mod( + id: 1, + gameId: kCurseForgeGameIdMinecraft, + name: 'Mod', + slug: 'mod-peer', + classId: 6, + ), + 2: Mod( + id: 2, + gameId: kCurseForgeGameIdMinecraft, + name: 'Resources', + slug: 'resource-peer', + classId: 12, + ), + }, + filesByProject: { + 1: [ + _file( + id: 11, + modId: 1, + fileName: 'mod.jar', + fileDate: '2025-01-01', + ), + ], + 2: [ + _file( + id: 22, + modId: 2, + fileName: 'resources.zip', + fileDate: '2025-01-01', + ), + ], + }, + ); + final manifest = + _pack( + modsMap: { + 'same': const ModEntry( + slug: 'same', + curseforgeSlug: '1', + sources: {SourceKind.curseforge}, + ), + }, + ).withSectionEntries(Section.resourcePacks, { + 'same': const ModEntry( + slug: 'same', + curseforgeSlug: '2', + sources: {SourceKind.curseforge}, + ), + }); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: api, + console: const Console(level: LogLevel.error), + ); + expect(out.notFound, isEmpty); + expect( + out.resolved.values.map((e) => (e.section, e.project.id)), + unorderedEquals([(Section.mods, 1), (Section.resourcePacks, 2)]), + ); + final lock = buildLock( + manifest: manifest, + resolution: const ResolutionResult([]), + cfResolution: out, + hashOutcomes: {}, + resolvedModLoaderVersion: '0.16.0', + resolvedPluginLoaderVersion: null, + ); + expect(lock.mods['same']!.curseforge!.projectId, 1); + expect(lock.resourcePacks['same']!.curseforge!.projectId, 2); + final offline = synthesizeCfResolutionFromLock(lock, manifest: manifest); + expect(offline.resolved.keys, out.resolved.keys); + expect(offline.resolved.values.map((e) => e.project.id), [1, 2]); + }); + + test('newest pick wins (sorted by fileDate descending)', () async { + final cfApi = _FakeCfApi( + projectsBySlug: { + 'jei': Mod( + id: 238222, + gameId: kCurseForgeGameIdMinecraft, + name: 'JEI', + slug: 'jei', + classId: 6, + ), + }, + filesByProject: { + 238222: [ + _file( + id: 1000, + modId: 238222, + fileName: 'jei-old.jar', + fileDate: '2025-01-01T00:00:00Z', + ), + _file( + id: 2000, + modId: 238222, + fileName: 'jei-new.jar', + fileDate: '2025-06-01T00:00:00Z', + ), + ], + }, + ); + final manifest = _pack(modsMap: {'jei': const ModEntry(slug: 'jei')}); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.notFound, isEmpty); + expect( + out.resolved[const LocalEntryKey(Section.mods, 'jei')]!.file.id, + 2000, + ); + expect( + out.resolved[const LocalEntryKey(Section.mods, 'jei')]!.candidates, + hasLength(2), + ); + }); + + test('slug not found surfaces CfNotFound(slug-not-found)', () async { + final cfApi = _FakeCfApi(projectsBySlug: const {'jei': null}); + final manifest = _pack(modsMap: {'jei': const ModEntry(slug: 'jei')}); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(out.notFound, hasLength(1)); + expect(out.notFound.single.reason, 'slug-not-found'); + expect(out.notFound.single.slug, 'jei'); + }); + + test('empty files surfaces CfNotFound(no-compatible-files)', () async { + final cfApi = _FakeCfApi( + projectsBySlug: { + 'jei': Mod( + id: 238222, + gameId: kCurseForgeGameIdMinecraft, + name: 'JEI', + slug: 'jei', + classId: 6, + ), + }, + filesByProject: const {238222: []}, + ); + final manifest = _pack(modsMap: {'jei': const ModEntry(slug: 'jei')}); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(out.notFound.single.reason, 'no-compatible-files'); + }); + + test( + 'peer-slug override is used when entry has curseforge: ', + () async { + final cfApi = _FakeCfApi( + projectsBySlug: { + 'jei-peer': Mod( + id: 999, + gameId: kCurseForgeGameIdMinecraft, + name: 'JEI', + slug: 'jei-peer', + classId: 6, + ), + }, + filesByProject: { + 999: [ + _file( + id: 5000, + modId: 999, + fileName: 'jei.jar', + fileDate: '2025-06-01T00:00:00Z', + ), + ], + }, + ); + final manifest = _pack( + modsMap: { + 'jei': const ModEntry(slug: 'jei', curseforgeSlug: 'jei-peer'), + }, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect( + out.resolved[const LocalEntryKey(Section.mods, 'jei')]!.project.slug, + 'jei-peer', + ); + expect(cfApi.calls, contains('searchMods(jei-peer)')); + }, + ); + + test('numeric `curseforge: 32274` short-circuits via getMod', () async { + final cfApi = _FakeCfApi( + modsById: { + 32274: Mod( + id: 32274, + gameId: kCurseForgeGameIdMinecraft, + name: 'JEI', + slug: 'jei', + classId: 6, + ), + }, + filesByProject: { + 32274: [ + _file( + id: 1, + modId: 32274, + fileName: 'jei.jar', + fileDate: '2025-06-01T00:00:00Z', + ), + ], + }, + ); + final manifest = _pack( + modsMap: {'jei': const ModEntry(slug: 'jei', curseforgeSlug: '32274')}, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect( + out.resolved[const LocalEntryKey(Section.mods, 'jei')]!.project.id, + 32274, + ); + expect(cfApi.calls, contains('getMod(32274)')); + // searchMods should NOT have been called. + expect(cfApi.calls.where((c) => c.startsWith('searchMods')), isEmpty); + }); + + test('CF disabled at pack level skips silently', () async { + final cfApi = _FakeCfApi(); // no projects registered + final manifest = _pack( + curseforge: GitrinthPlatformState.disabled, + modsMap: {'jei': const ModEntry(slug: 'jei')}, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(out.notFound, isEmpty); + expect(cfApi.calls, isEmpty); + }); + + test('vanilla mods: section short-circuits before any HTTP call', () async { + final cfApi = _FakeCfApi(); + final manifest = _pack( + mods: ModLoader.vanilla, + modsMap: {'jei': const ModEntry(slug: 'jei')}, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(cfApi.calls, isEmpty); + }); + + test('Folia plugins entry silently skipped (matrix ineligible)', () async { + final cfApi = _FakeCfApi(); + final manifest = _pack( + mods: ModLoader.fabric, + plugins: PluginLoader.folia, + pluginsMap: {'plug': const ModEntry(slug: 'plug')}, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(out.notFound, isEmpty); + expect(cfApi.calls, isEmpty); + }); + + test('url:/path: source entries are skipped (no CF resolution)', () async { + final cfApi = _FakeCfApi(); + final manifest = _pack( + modsMap: { + 'urlmod': const ModEntry( + slug: 'urlmod', + source: UrlEntrySource(url: 'https://example.com/x.jar'), + ), + }, + ); + final out = await resolveCurseforgeEntries( + manifest: manifest, + api: cfApi, + console: const Console(level: LogLevel.error), + ); + expect(out.resolved, isEmpty); + expect(out.notFound, isEmpty); + expect(cfApi.calls, isEmpty); + }); + }); +} diff --git a/test/service/resolve/download_artifacts_test.dart b/test/service/resolve/download_artifacts_test.dart new file mode 100644 index 0000000..07b4324 --- /dev/null +++ b/test/service/resolve/download_artifacts_test.dart @@ -0,0 +1,332 @@ +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/downloader.dart'; +import 'package:gitrinth/src/service/manifest_io.dart'; +import 'package:gitrinth/src/service/offline_guard_interceptor.dart'; +import 'package:gitrinth/src/service/resolve/download_artifacts.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +ModsLock _lockWith(Map mods) => ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + ), + mcVersion: '1.21.5', + mods: mods, +); + +void main() { + late Directory tempDir; + late Directory cacheDir; + late GitrinthCache cache; + late Downloader downloader; + late ManifestIo io; + + setUp(() { + tempDir = Directory.systemTemp.createTempSync('gitrinth_dl_'); + cacheDir = Directory.systemTemp.createTempSync('gitrinth_dl_cache_'); + cache = GitrinthCache(root: cacheDir.path); + cache.ensureRoot(); + final dio = Dio(); + downloader = Downloader(dio: dio, cache: cache); + io = ManifestIo(directory: tempDir); + }); + + tearDown(() { + if (tempDir.existsSync()) tempDir.deleteSync(recursive: true); + if (cacheDir.existsSync()) cacheDir.deleteSync(recursive: true); + }); + + group('downloadArtifacts', () { + for (final allowed in [true, false]) { + for (final scenario in ['missing', 'valid', 'corrupt', 'unverifiable']) { + test( + 'CurseForge manual cache $scenario, distribution=$allowed', + () async { + final good = [1, 2, 3, 4]; + final dest = File( + cache.curseforgePath( + projectId: 99, + fileId: 1, + filename: 'opaque.jar', + ), + ); + if (scenario != 'missing') { + dest.parent.createSync(recursive: true); + dest.writeAsBytesSync(scenario == 'corrupt' ? [0] : good); + } + var requests = 0; + downloader.dio.interceptors.add( + InterceptorsWrapper( + onRequest: (options, handler) { + requests++; + handler.next(options); + }, + ), + ); + downloader.dio.interceptors.add( + OfflineGuardInterceptor(() => true), + ); + final lock = _lockWith({ + 'opaque': LockedEntry( + slug: 'opaque', + kind: LockedEntryKind.resolved, + curseforge: LockedCurseforgeSource( + projectId: 99, + fileId: 1, + allowModDistribution: allowed, + file: LockedFile( + name: 'opaque.jar', + sha1: scenario == 'unverifiable' + ? null + : sha1.convert(good).toString(), + ), + ), + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'unused', + ); + expect(requests, 0); + expect(result.downloaded, 0); + if (scenario == 'valid') { + expect(result.errors, isEmpty); + expect(result.hits, 1); + expect(dest.readAsBytesSync(), good); + } else { + expect(result.hits, 0); + expect(result.errors, hasLength(1)); + expect(result.errors.single, contains('project 99')); + expect(result.errors.single, contains('file 1')); + expect(result.errors.single, contains(dest.path)); + expect(result.errors.single, contains('opaque.jar')); + switch (scenario) { + case 'missing': + expect(result.errors.single, contains('missing')); + expect(result.errors.single, contains('download manually')); + case 'corrupt': + expect(result.errors.single, contains('checksum mismatch')); + expect(result.errors.single, contains('download manually')); + case 'unverifiable': + expect(result.errors.single, contains('no verifiable hash')); + expect(result.errors.single, contains('path:')); + } + } + }, + ); + } + } + + test( + 'a cached Modrinth peer does not satisfy a missing CurseForge artifact', + () async { + final bytes = [1, 2, 3, 4]; + final mrDest = File( + cache.modrinthPath( + host: 'unused', + projectId: 'mr-project', + versionId: 'mr-version', + filename: 'peer.jar', + ), + ); + mrDest.parent.createSync(recursive: true); + mrDest.writeAsBytesSync(bytes); + final result = await downloadArtifacts( + lock: _lockWith({ + 'dual': LockedEntry( + slug: 'dual', + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: 'mr-project', + versionId: 'mr-version', + file: LockedFile( + name: 'peer.jar', + url: 'https://invalid.example/peer.jar', + sha512: sha512.convert(bytes).toString(), + ), + ), + curseforge: LockedCurseforgeSource( + projectId: 99, + fileId: 1, + file: LockedFile( + name: 'opaque.jar', + sha1: sha1.convert(bytes).toString(), + ), + ), + ), + }), + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'unused', + ); + expect(result.hits, 1); + expect(result.downloaded, 0); + expect(result.errors, hasLength(1)); + expect(result.errors.single, contains('project 99')); + }, + ); + + for (final scenario in [ + 'valid cache', + 'corrupt cache', + 'corrupt download', + ]) { + test('CurseForge SHA1 verification: $scenario', () async { + final good = [1, 2, 3, 4]; + final dest = File( + cache.curseforgePath(projectId: 42, fileId: 7, filename: 'mod.jar'), + ); + if (scenario != 'corrupt download') { + dest.parent.createSync(recursive: true); + dest.writeAsBytesSync(scenario == 'valid cache' ? good : [0]); + } + var requests = 0; + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => server.close(force: true)); + server.listen((req) async { + requests++; + req.response.add(scenario == 'corrupt download' ? [0] : good); + await req.response.close(); + }); + final lock = _lockWith({ + 'mod': LockedEntry( + slug: 'mod', + kind: LockedEntryKind.resolved, + curseforge: LockedCurseforgeSource( + projectId: 42, + fileId: 7, + file: LockedFile( + name: 'mod.jar', + url: 'http://127.0.0.1:${server.port}/mod.jar', + sha1: sha1.convert(good).toString(), + ), + ), + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'unused', + ); + if (scenario == 'corrupt download') { + expect(result.errors, hasLength(1)); + expect(result.errors.single, contains('checksum mismatch')); + expect(dest.existsSync(), isFalse); + } else { + expect(result.errors, isEmpty); + expect(dest.readAsBytesSync(), good); + expect(requests, scenario == 'valid cache' ? 0 : 1); + } + }); + } + + test('kind:path entry pointing to existing file passes', () async { + final localJar = File(p.join(tempDir.path, 'mods', 'local.jar')) + ..createSync(recursive: true) + ..writeAsBytesSync([0]); + addTearDown(() { + if (localJar.existsSync()) localJar.deleteSync(); + }); + final lock = _lockWith({ + 'local': LockedEntry( + slug: 'local', + kind: LockedEntryKind.path, + path: p.join('mods', 'local.jar'), + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'https://api.modrinth.com/v2', + ); + expect(result.errors, isEmpty); + }); + + test( + 'kind:path entry pointing to a missing file aggregates an error', + () async { + final lock = _lockWith({ + 'missing': const LockedEntry( + slug: 'missing', + kind: LockedEntryKind.path, + path: 'mods/does-not-exist.jar', + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'https://api.modrinth.com/v2', + ); + expect(result.errors, hasLength(1)); + expect(result.errors.first, contains('does-not-exist.jar')); + }, + ); + + test('resolved entry with no source blocks (defensive) is a no-op, ' + 'not an error', () async { + final lock = _lockWith({ + 'empty': const LockedEntry( + slug: 'empty', + kind: LockedEntryKind.resolved, + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'https://api.modrinth.com/v2', + ); + expect(result.errors, isEmpty); + expect(result.downloaded, 0); + expect(result.hits, 0); + }); + + test( + 'kind:url entry without urlFile is a no-op (existing behavior)', + () async { + final lock = _lockWith({ + 'url-no-file': const LockedEntry( + slug: 'url-no-file', + kind: LockedEntryKind.url, + url: 'https://example.com/x.jar', + ), + }); + final result = await downloadArtifacts( + lock: lock, + cache: cache, + downloader: downloader, + console: const Console(level: LogLevel.error), + io: io, + defaultModrinthHost: 'https://api.modrinth.com/v2', + ); + expect(result.errors, isEmpty); + }, + ); + }); +} diff --git a/test/service/resolve/hash_scan_test.dart b/test/service/resolve/hash_scan_test.dart new file mode 100644 index 0000000..df2d9da --- /dev/null +++ b/test/service/resolve/hash_scan_test.dart @@ -0,0 +1,195 @@ +import 'package:gitrinth/src/model/curseforge/cf_file_hash.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; +import 'package:gitrinth/src/model/modrinth/version_file.dart'; +import 'package:gitrinth/src/service/resolve/hash_scan.dart'; +import 'package:test/test.dart'; + +modrinth.Version _mr({required String number, String? sha1}) => + modrinth.Version( + id: 'mr-$number', + projectId: 'p', + versionNumber: number, + files: [ + VersionFile( + url: 'https://example.com/mr-$number.jar', + filename: 'mr-$number.jar', + hashes: sha1 == null ? const {} : {'sha1': sha1}, + size: 1, + primary: true, + ), + ], + dependencies: const [], + loaders: const [], + gameVersions: const [], + ); + +ModFile _cf({required int id, String? sha1}) => ModFile( + id: id, + modId: 1, + displayName: 'cf-$id', + fileName: 'cf-$id.jar', + releaseType: 1, + fileDate: '2025-01-01T00:00:00Z', + gameVersions: const [], + hashes: sha1 == null ? const [] : [FileHash(value: sha1, algoCode: 1)], + dependencies: const [], + downloadUrl: 'https://example.com/cf-$id.jar', +); + +void main() { + group('scanForMatchingPair', () { + test('newest pair SHA1 matches → HashMatched', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'aabb')], + cfCandidates: [_cf(id: 1, sha1: 'aabb')], + hashScanDepth: 0, + allowHashMismatch: false, + ); + expect(out, isA()); + final m = out as HashMatched; + expect(m.modrinthVersion.versionNumber, '1.0.0'); + expect(m.curseforgeFile.id, 1); + }); + + test('depth=0 mismatch with allow=false → HashExhausted', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'aabb')], + cfCandidates: [_cf(id: 1, sha1: 'ccdd')], + hashScanDepth: 0, + allowHashMismatch: false, + ); + expect(out, isA()); + final ex = out as HashExhausted; + expect(ex.depthExamined, 0); + }); + + test('depth=0 mismatch with allow=true → HashAcceptedMismatch', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'aabb')], + cfCandidates: [_cf(id: 1, sha1: 'ccdd')], + hashScanDepth: 0, + allowHashMismatch: true, + ); + expect(out, isA()); + }); + + test( + 'an older matching pair takes precedence over accepting a mismatch', + () { + final out = scanForMatchingPair( + mrCandidates: [ + _mr(number: '2.0.0', sha1: 'new-mr'), + _mr(number: '1.0.0', sha1: 'same'), + ], + cfCandidates: [ + _cf(id: 2, sha1: 'new-cf'), + _cf(id: 1, sha1: 'same'), + ], + hashScanDepth: 1, + allowHashMismatch: true, + ); + expect(out, isA()); + expect((out as HashMatched).modrinthVersion.versionNumber, '1.0.0'); + expect(out.curseforgeFile.id, 1); + }, + ); + + test('depth=10 finds a match at index 3 → HashMatched', () { + final mr = [ + for (var i = 0; i < 6; i++) _mr(number: '1.0.$i', sha1: 'mr$i'), + ]; + final cf = [for (var i = 0; i < 6; i++) _cf(id: 100 + i, sha1: 'cf$i')]; + // Plant the match at the index-3 pair: mr3 SHA matches cf3 SHA. + mr[3] = _mr(number: '1.0.3', sha1: 'matchHash'); + cf[3] = _cf(id: 103, sha1: 'matchHash'); + final out = scanForMatchingPair( + mrCandidates: mr, + cfCandidates: cf, + hashScanDepth: 10, + allowHashMismatch: false, + ); + expect(out, isA()); + final m = out as HashMatched; + expect(m.modrinthVersion.versionNumber, '1.0.3'); + expect(m.curseforgeFile.id, 103); + }); + + test('depth=2 and match outside window → HashExhausted', () { + final mr = [ + _mr(number: '1.0.0', sha1: 'a'), + _mr(number: '1.0.1', sha1: 'b'), + _mr(number: '1.0.2', sha1: 'c'), + _mr(number: '1.0.3', sha1: 'matchHash'), + ]; + final cf = [ + _cf(id: 100, sha1: 'x'), + _cf(id: 101, sha1: 'y'), + _cf(id: 102, sha1: 'z'), + _cf(id: 103, sha1: 'matchHash'), + ]; + final out = scanForMatchingPair( + mrCandidates: mr, + cfCandidates: cf, + hashScanDepth: 2, // window = 3 + allowHashMismatch: false, + ); + expect(out, isA()); + }); + + test('asymmetric windows: short MR, long CF — no crash', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'aa')], + cfCandidates: [ + _cf(id: 1, sha1: 'ff'), + _cf(id: 2, sha1: 'ee'), + _cf(id: 3, sha1: 'aa'), + ], + hashScanDepth: 5, + allowHashMismatch: false, + ); + expect(out, isA()); + final m = out as HashMatched; + expect(m.curseforgeFile.id, 3); + }); + + test('candidates with missing SHA1 are skipped, not crashed', () { + final out = scanForMatchingPair( + mrCandidates: [ + _mr(number: '1.0.0'), // no sha1 + _mr(number: '0.9.0', sha1: 'aabb'), + ], + cfCandidates: [ + _cf(id: 1), // no sha1 + _cf(id: 2, sha1: 'aabb'), + ], + hashScanDepth: 5, + allowHashMismatch: false, + ); + expect(out, isA()); + final m = out as HashMatched; + expect(m.modrinthVersion.versionNumber, '0.9.0'); + expect(m.curseforgeFile.id, 2); + }); + + test('one side empty → HashSingleSided', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'aa')], + cfCandidates: const [], + hashScanDepth: 5, + allowHashMismatch: false, + ); + expect(out, isA()); + }); + + test('case-insensitive SHA1 match (lowercase normalization)', () { + final out = scanForMatchingPair( + mrCandidates: [_mr(number: '1.0.0', sha1: 'AABB')], + cfCandidates: [_cf(id: 1, sha1: 'aabb')], + hashScanDepth: 0, + allowHashMismatch: false, + ); + expect(out, isA()); + }); + }); +} diff --git a/test/service/resolve/identity_reentry_test.dart b/test/service/resolve/identity_reentry_test.dart new file mode 100644 index 0000000..9b10947 --- /dev/null +++ b/test/service/resolve/identity_reentry_test.dart @@ -0,0 +1,96 @@ +import 'dart:io'; + +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +import '../../helpers/capture.dart'; +import '../../helpers/fake_modrinth.dart'; + +void main() { + late Directory root; + late FakeModrinth defaultApi; + late FakeModrinth hostedApi; + late Map environment; + setUp(() async { + root = Directory.systemTemp.createTempSync('identity_reentry_'); + defaultApi = FakeModrinth(); + hostedApi = FakeModrinth(); + await defaultApi.start(); + await hostedApi.start(); + for (final api in [defaultApi, hostedApi]) { + api.registerVersion( + slug: 'anchor', + versionNumber: '1.0.0', + requiredDeps: ['library'], + ); + api.registerVersion(slug: 'library', versionNumber: '2.0.0'); + } + environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': defaultApi.baseUrl, + 'GITRINTH_NEOFORGE_VERSIONS_URL': defaultApi.neoforgeVersionsUrl, + }; + }); + tearDown(() async { + await defaultApi.stop(); + await hostedApi.stop(); + root.deleteSync(recursive: true); + }); + void manifest(String entries) => + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +modrinth_host: ${hostedApi.baseUrl} +loader: + mods: "neoforge:21.1.50" +mods: + default-anchor: + modrinth: anchor + modrinth_host: ${defaultApi.baseUrl} +$entries +'''); + Future get() => + runCli(['-C', root.path, 'get'], environment: environment); + + test( + 'second online get reuses host-qualified lock entries by remote project ID', + () async { + manifest(' hosted-anchor: {modrinth: anchor}'); + final first = await get(); + expect(first.exitCode, 0, reason: '${first.stdout}\n${first.stderr}'); + final lock = File(p.join(root.path, 'mods.lock')); + final before = lock.readAsStringSync(); + expect( + parseModsLock( + before, + filePath: lock.path, + ).mods.keys.any((key) => key.contains('@')), + isTrue, + ); + final second = await get(); + expect(second.exitCode, 0, reason: '${second.stdout}\n${second.stderr}'); + expect(lock.readAsStringSync(), before); + }, + ); + + test('renamed declared peer wins over the old lock-only alias', () async { + String alias(String key) => + ' $key: {modrinth: library, modrinth_host: ${defaultApi.baseUrl}}'; + manifest(alias('old-name')); + final first = await get(); + expect(first.exitCode, 0, reason: first.stderr); + manifest(alias('new-name')); + final second = await get(); + expect(second.exitCode, 0, reason: '${second.stdout}\n${second.stderr}'); + final lock = parseModsLock( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + filePath: 'mods.lock', + ); + expect(lock.mods.keys, unorderedEquals(['default-anchor', 'new-name'])); + expect(lock.mods['new-name']!.requiredBy, ['default-anchor']); + }); +} diff --git a/test/service/resolve/lock_graph_test.dart b/test/service/resolve/lock_graph_test.dart new file mode 100644 index 0000000..746bff9 --- /dev/null +++ b/test/service/resolve/lock_graph_test.dart @@ -0,0 +1,208 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/resolve/lock_graph.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; +import 'package:gitrinth/src/cli/exceptions.dart'; +import 'package:test/test.dart'; + +void main() { + late Directory root; + late GitrinthCache cache; + const defaultHost = 'https://default.example/v2'; + const hostedHost = 'https://hosted.example:8443/api/v2'; + setUp(() { + root = Directory.systemTemp.createTempSync('gitrinth_graph_'); + cache = GitrinthCache(root: root.path); + }); + tearDown(() => root.deleteSync(recursive: true)); + LockedEntry entry( + String name, + String project, { + String? host, + List parents = const [], + }) => LockedEntry( + slug: name, + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: project, + versionId: 'v1', + host: host, + file: LockedFile(name: '$name.jar'), + ), + requiredBy: parents, + ); + ModsLock lock(Map entries) => ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig(mods: ModLoader.fabric), + mcVersion: '1.21.1', + mods: entries, + ); + void metadata(String host, String project, List children) { + final file = File( + cache.modrinthVersionMetadataPath( + host: host, + projectId: project, + versionId: 'v1', + ), + ); + file.parent.createSync(recursive: true); + file.writeAsStringSync( + jsonEncode({ + 'dependencies': [ + for (final child in children) + {'project_id': child, 'dependency_type': 'required'}, + ], + }), + ); + } + + test('closure does not follow an identical project ID on another host', () { + final locked = lock({ + 'root-a': entry('root-a', 'root'), + 'child-a': entry('child-a', 'library', parents: ['root-a']), + 'root-b': entry('root-b', 'root', host: hostedHost), + 'child-b': entry( + 'child-b', + 'library', + host: hostedHost, + parents: ['root-b'], + ), + }); + metadata(defaultHost, 'root', ['library']); + metadata(hostedHost, 'root', ['library']); + final closure = walkTransitiveClosure( + {'root-a'}, + locked, + cache, + defaultHost: defaultHost, + ); + expect(closure, unorderedEquals(['root-a', 'child-a'])); + }); + + test( + 'metadata retains multiple local aliases for a project on the same host', + () { + final locked = lock({ + 'root': entry('root', 'root'), + 'alias-a': entry('alias-a', 'library'), + 'alias-b': entry('alias-b', 'library'), + }); + metadata(defaultHost, 'root', ['library']); + final closure = walkTransitiveClosure( + {'root'}, + locked, + cache, + defaultHost: defaultHost, + ); + expect(closure, unorderedEquals(['root', 'alias-a', 'alias-b'])); + }, + ); + + test('parent edges retain cold-cache dependencies and terminate cycles', () { + final locked = lock({ + 'root': entry('root', 'root', parents: ['child']), + 'child': entry('child', 'library', parents: ['root']), + }); + expect( + walkTransitiveClosure({'root'}, locked, cache, defaultHost: defaultHost), + unorderedEquals(['root', 'child']), + ); + }); + + test('section-qualified nodes retain duplicate local names', () { + final locked = lock({ + 'root': entry('root', 'mod-root'), + }).copyWith(plugins: {'root': entry('root', 'plugin-root')}); + final indexed = LockedDependencyGraph( + locked, + cache, + defaultHost: defaultHost, + ); + expect( + indexed.entries.keys, + unorderedEquals([ + const LocalEntryKey(Section.mods, 'root'), + const LocalEntryKey(Section.plugins, 'root'), + ]), + ); + expect( + () => walkTransitiveClosure( + {'root'}, + locked, + cache, + defaultHost: defaultHost, + ), + throwsA(isA()), + ); + }); + + test( + 'a matching child section does not resolve ambiguous parent identity', + () { + final locked = lock({ + 'root': entry('root', 'mod-root'), + 'child': entry('child', 'child', parents: ['root']), + }).copyWith(plugins: {'root': entry('root', 'plugin-root')}); + final indexed = LockedDependencyGraph( + locked, + cache, + defaultHost: defaultHost, + ); + expect(indexed.diagnostics.single, contains('Ambiguous required_by')); + expect( + indexed.graph.parents(const LocalEntryKey(Section.mods, 'child')), + isEmpty, + ); + }, + ); + + test('cold-cache parents match source host before section', () { + final locked = lock({ + 'root': entry('root', 'root-a'), + 'child': entry('child', 'child-b', host: hostedHost, parents: ['root']), + }).copyWith(plugins: {'root': entry('root', 'root-b', host: hostedHost)}); + final indexed = LockedDependencyGraph( + locked, + cache, + defaultHost: defaultHost, + ); + expect(indexed.graph.parents(const LocalEntryKey(Section.mods, 'child')), { + const LocalEntryKey(Section.plugins, 'root'), + }); + }); + + test( + 'ambiguous bare parents are reported and omitted instead of guessed', + () { + final locked = lock({'root': entry('root', 'mod-root')}).copyWith( + plugins: {'root': entry('root', 'plugin-root')}, + dataPacks: { + 'child': entry('child', 'child', parents: ['root']), + }, + ); + final indexed = LockedDependencyGraph( + locked, + cache, + defaultHost: defaultHost, + ); + expect(indexed.diagnostics.single, contains('Ambiguous required_by')); + expect( + indexed.graph.parents(const LocalEntryKey(Section.dataPacks, 'child')), + isEmpty, + ); + expect( + () => walkTransitiveClosure( + {'child'}, + locked, + cache, + defaultHost: defaultHost, + ), + throwsA(isA()), + ); + }, + ); +} diff --git a/test/service/resolve/offline_parent_ambiguity_test.dart b/test/service/resolve/offline_parent_ambiguity_test.dart new file mode 100644 index 0000000..193d964 --- /dev/null +++ b/test/service/resolve/offline_parent_ambiguity_test.dart @@ -0,0 +1,74 @@ +import 'dart:io'; + +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:test/test.dart'; + +import '../../helpers/capture.dart'; + +void main() { + test( + 'offline removal cannot guess a colliding parent or overwrite the lock', + () async { + final root = Directory.systemTemp.createTempSync( + 'offline_parent_ambiguity_', + ); + addTearDown(() => root.deleteSync(recursive: true)); + File('${root.path}/mods.yaml').writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'fabric:0.16.0'} +gitrinth: {modrinth: disabled, curseforge: enabled} +resource_packs: + root: +'''); + LockedEntry entry(String slug, int id, {bool transitive = false}) => + LockedEntry( + slug: slug, + kind: LockedEntryKind.resolved, + version: '1.0.0', + dependency: transitive + ? LockedDependencyKind.transitive + : LockedDependencyKind.direct, + requiredBy: transitive ? ['root'] : [], + curseforge: LockedCurseforgeSource( + projectId: id, + fileId: id, + file: LockedFile(name: '$id.jar'), + gameVersions: ['1.21.1'], + ), + ); + final lock = File('${root.path}/mods.lock'); + final before = emitModsLock( + ModsLock( + gitrinthVersion: '0.1.0', + mcVersion: '1.21.1', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + ), + mods: { + 'root': entry('root', 1), + 'library': entry('library', 3, transitive: true), + }, + resourcePacks: {'root': entry('root', 2)}, + ), + ); + lock.writeAsStringSync(before); + final result = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': '${root.path}/cache', + 'GITRINTH_CONFIG': '${root.path}/config.yaml', + }, + ); + expect(result.exitCode, isNot(0)); + expect(lock.readAsStringSync(), before); + expect(result.stderr, contains('Ambiguous required_by')); + }, + ); +} diff --git a/test/service/resolve/offline_preserves_curseforge_test.dart b/test/service/resolve/offline_preserves_curseforge_test.dart new file mode 100644 index 0000000..f615ec5 --- /dev/null +++ b/test/service/resolve/offline_preserves_curseforge_test.dart @@ -0,0 +1,374 @@ +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:crypto/crypto.dart'; +import 'package:dio/dio.dart'; +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; +import 'package:gitrinth/src/model/modrinth/version_file.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api_factory.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_metadata_store.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_auth_interceptor.dart'; +import 'package:gitrinth/src/service/downloader.dart'; +import 'package:gitrinth/src/service/manifest_io.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_api_factory.dart'; +import 'package:gitrinth/src/service/modrinth/modrinth_auth_interceptor.dart'; +import 'package:gitrinth/src/service/paper_api_client.dart'; +import 'package:gitrinth/src/service/resolve/mod_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/resolve/plugin_loader_version_resolver.dart'; +import 'package:gitrinth/src/service/resolve/resolve_and_sync.dart'; +import 'package:gitrinth/src/service/resolve/solve_report.dart'; +import 'package:gitrinth/src/service/sponge_api_client.dart'; +import 'package:gitrinth/src/version.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; + +const _console = Console(level: LogLevel.error); + +/// Synthetic Modrinth artifact bytes; their sha512 anchors the lock so +/// the Downloader's existence-check accepts the cached file. +final Uint8List _modrinthBytes = Uint8List.fromList( + List.generate(64, (i) => i & 0xff), +); +final String _modrinthSha512 = sha512.convert(_modrinthBytes).toString(); + +ModsLock _dualSourceLock({String? gitrinthVersion}) => ModsLock( + gitrinthVersion: gitrinthVersion ?? '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.neoforge, + modLoaderVersion: '21.1.50', + ), + mcVersion: '1.21.1', + mods: { + 'jei': LockedEntry( + slug: 'jei', + kind: LockedEntryKind.resolved, + version: '15.3.0.4', + modrinth: LockedModrinthSource( + projectId: 'JEI_PID', + versionId: 'JEI_VID', + versionNumber: '15.3.0.4', + file: LockedFile( + name: 'jei-15.3.0.4.jar', + url: + 'https://cdn.modrinth.com/data/JEI_PID/versions/JEI_VID/jei-15.3.0.4.jar', + sha1: 'aabb1122', + sha512: _modrinthSha512, + size: _modrinthBytes.length, + ), + gameVersions: const ['1.21.1'], + ), + curseforge: const LockedCurseforgeSource( + projectId: 238222, + fileId: 5512345, + slug: 'jei', + // CurseForge's lock-builder branch never records `size`; mirror + // that here so the prior lock and the rebuilt lock match. + file: LockedFile( + name: 'jei-15.3.0.4.jar', + url: 'https://edge.forgecdn.net/files/5512/345/jei-15.3.0.4.jar', + sha1: 'aabb1122', + ), + gameVersions: ['1.21.1'], + ), + ), + }, +); + +void main() { + group('synthesizeCfResolutionFromLock', () { + test('rebuilds a CfResolvedEntry from a dual-source lock', () { + final out = synthesizeCfResolutionFromLock(_dualSourceLock()); + + expect(out.notFound, isEmpty); + expect(out.resolved.keys.map((node) => node.localKey), ['jei']); + + final entry = out.resolved[const LocalEntryKey(Section.mods, 'jei')]!; + expect(entry.slug, 'jei'); + expect(entry.section, Section.mods); + expect(entry.project.id, 238222); + expect(entry.project.slug, 'jei'); + expect(entry.project.allowModDistribution, isTrue); + expect(entry.file.id, 5512345); + expect(entry.file.modId, 238222); + expect(entry.file.fileName, 'jei-15.3.0.4.jar'); + expect( + entry.file.downloadUrl, + 'https://edge.forgecdn.net/files/5512/345/jei-15.3.0.4.jar', + ); + expect(entry.file.sha1Hash, 'aabb1122'); + expect(entry.file.gameVersions, ['1.21.1']); + expect(entry.candidates, hasLength(1)); + expect(entry.candidates.single.id, entry.file.id); + }); + + test('skips entries without a curseforge block', () { + final lock = ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.neoforge, + modLoaderVersion: '21.1.50', + ), + mcVersion: '1.21.1', + mods: const { + 'mr-only': LockedEntry( + slug: 'mr-only', + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: 'MR', + versionId: 'V', + file: LockedFile(name: 'mr-only.jar'), + ), + ), + }, + ); + final out = synthesizeCfResolutionFromLock(lock); + expect(out.resolved, isEmpty); + }); + + test('preserves allowModDistribution=false and a null download URL', () { + final lock = ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.neoforge, + modLoaderVersion: '21.1.50', + ), + mcVersion: '1.21.1', + mods: const { + 'opaque': LockedEntry( + slug: 'opaque', + kind: LockedEntryKind.resolved, + curseforge: LockedCurseforgeSource( + projectId: 99, + fileId: 1, + slug: 'opaque', + file: LockedFile(name: 'opaque.jar'), + allowModDistribution: false, + ), + ), + }, + ); + final out = synthesizeCfResolutionFromLock(lock); + final entry = out.resolved[const LocalEntryKey(Section.mods, 'opaque')]!; + expect(entry.project.allowModDistribution, isFalse); + expect(entry.file.downloadUrl, isNull); + expect(entry.file.sha1Hash, isNull); + }); + }); + + group('resolveAndSync(offline: true) preserves CurseForge blocks', () { + late Directory tempDir; + late Directory cacheDir; + late GitrinthCache cache; + late ManifestIo io; + late Downloader downloader; + late ModrinthApiFactory mrFactory; + late CurseForgeApiFactory cfFactory; + late ModLoaderVersionResolver modLoaderResolver; + late PluginLoaderVersionResolver pluginLoaderResolver; + const unreachable = 'http://127.0.0.1:1'; + + setUp(() { + tempDir = Directory.systemTemp.createTempSync('gitrinth_off_cf_'); + cacheDir = Directory.systemTemp.createTempSync('gitrinth_off_cf_cache_'); + cache = GitrinthCache(root: cacheDir.path); + cache.ensureRoot(); + io = ManifestIo(directory: tempDir); + downloader = Downloader(dio: Dio(), cache: cache); + mrFactory = ModrinthApiFactory( + console: _console, + auth: ModrinthAuthInterceptor( + tokensProvider: () => const {}, + envTokenLookup: () => null, + defaultBaseUrl: unreachable, + ), + offline: () => true, + defaultBaseUrl: unreachable, + ); + cfFactory = CurseForgeApiFactory( + console: _console, + auth: CurseForgeAuthInterceptor( + envTokenLookup: () => null, + baseUrl: unreachable, + defaultKeyResolver: () => '', + ), + offline: () => true, + baseUrl: unreachable, + ); + modLoaderResolver = ModLoaderVersionResolver(dio: Dio()); + final pluginDio = Dio(); + pluginLoaderResolver = PluginLoaderVersionResolver( + dio: pluginDio, + paperApi: PaperApiClient(dio: pluginDio), + spongeApi: SpongeApiClient(dio: pluginDio), + ); + }); + + tearDown(() { + mrFactory.close(); + cfFactory.close(); + if (tempDir.existsSync()) tempDir.deleteSync(recursive: true); + if (cacheDir.existsSync()) cacheDir.deleteSync(recursive: true); + }); + + test( + 'CF block is re-emitted byte-for-byte and the artifact cache hits', + () async { + // 1. Hand-write mods.yaml. `gitrinth.curseforge: enabled` makes + // CF an effective source for jei. + File(p.join(tempDir.path, 'mods.yaml')).writeAsStringSync(''' +slug: testpack +name: TestPack +version: 0.1.0 +description: x +loader: + mods: "neoforge:21.1.50" +mc_version: 1.21.1 +gitrinth: + curseforge: enabled +mods: + jei: ^15.0.0 +'''); + + // 2. Hand-write a mods.lock containing both source blocks. Use + // the running packageVersion so the prior lock matches what + // buildLock will produce — otherwise the diff would be + // non-empty on a gitrinth-version line. + final template = _dualSourceLock(gitrinthVersion: packageVersion); + final cf = template.mods['jei']!.curseforge!; + final priorLock = template.copyWith( + mods: { + 'jei': template.mods['jei']!.copyWith( + curseforge: cf.copyWith( + file: cf.file.copyWith( + sha1: sha1.convert(const [0]).toString(), + ), + ), + ), + }, + ); + final priorLockText = emitModsLock(priorLock); + File( + p.join(tempDir.path, 'mods.lock'), + ).writeAsStringSync(priorLockText); + + // 3. Pre-populate the cache. With files present the Downloader + // returns early without HTTP; without the version.json + // sidecar the offline Modrinth resolver finds no candidates. + final mrEntry = priorLock.mods['jei']!.modrinth!; + File( + cache.modrinthPath( + host: unreachable, + projectId: mrEntry.projectId, + versionId: mrEntry.versionId, + filename: mrEntry.file.name, + ), + ) + ..createSync(recursive: true) + ..writeAsBytesSync(_modrinthBytes); + + final cachedVersion = modrinth.Version( + id: mrEntry.versionId, + projectId: mrEntry.projectId, + versionNumber: mrEntry.versionNumber!, + files: [ + VersionFile( + url: mrEntry.file.url!, + filename: mrEntry.file.name, + hashes: { + if (mrEntry.file.sha1 != null) 'sha1': mrEntry.file.sha1!, + 'sha512': mrEntry.file.sha512!, + }, + size: mrEntry.file.size!, + primary: true, + ), + ], + dependencies: const [], + loaders: const ['neoforge'], + gameVersions: const ['1.21.1'], + versionType: 'release', + ); + File( + cache.modrinthVersionMetadataPath( + host: unreachable, + projectId: mrEntry.projectId, + versionId: mrEntry.versionId, + ), + ) + ..createSync(recursive: true) + ..writeAsStringSync(jsonEncode(cachedVersion.toMap())); + + final cfEntry = priorLock.mods['jei']!.curseforge!; + for (final entry in synthesizeCfResolutionFromLock( + priorLock, + ).resolved.values) { + CurseForgeMetadataStore(cache).write(entry.file); + } + File( + cache.curseforgePath( + projectId: cfEntry.projectId, + fileId: cfEntry.fileId, + filename: cfEntry.file.name, + ), + ) + ..createSync(recursive: true) + ..writeAsBytesSync(const [0]); + + // 4. Run the offline resolve. + final result = await resolveAndSync( + io: io, + console: _console, + apiFactory: mrFactory, + cfFactory: cfFactory, + cache: cache, + downloader: downloader, + modLoaderResolver: modLoaderResolver, + pluginLoaderResolver: pluginLoaderResolver, + offline: true, + ); + + // 5. The CF source block survives byte-for-byte. + final newLock = result.newLock!; + final jei = newLock.mods['jei']!; + expect(jei.curseforge, isNotNull); + expect(jei.curseforge!.projectId, cfEntry.projectId); + expect(jei.curseforge!.fileId, cfEntry.fileId); + expect(jei.curseforge!.file.sha1, cfEntry.file.sha1); + expect(jei.curseforge!.file.name, cfEntry.file.name); + expect(jei.curseforge!.file.url, cfEntry.file.url); + expect(jei.curseforge!.gameVersions, cfEntry.gameVersions); + expect(jei.curseforge!.allowModDistribution, isTrue); + + // The Modrinth block also survives. + expect(jei.modrinth, isNotNull); + expect(jei.modrinth!.projectId, mrEntry.projectId); + expect(jei.modrinth!.versionId, mrEntry.versionId); + + // 6. Diff is empty — the lock is byte-stable across the + // round-trip. + final changes = result.diff + .where((d) => d.kind != DiffKind.unchanged) + .toList(); + expect( + changes, + isEmpty, + reason: + 'offline resolve should not change a complete lock; got: ' + '${changes.map((c) => "${c.kind.name} ${c.section.name}/${c.slug}").join(", ")}', + ); + + // 7. mods.lock on disk is byte-identical. + final reReadText = File( + p.join(tempDir.path, 'mods.lock'), + ).readAsStringSync(); + expect(reReadText, priorLockText); + }, + ); + }); +} diff --git a/test/service/resolve/override_synthetic_collision_test.dart b/test/service/resolve/override_synthetic_collision_test.dart new file mode 100644 index 0000000..f82ab2c --- /dev/null +++ b/test/service/resolve/override_synthetic_collision_test.dart @@ -0,0 +1,145 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:test/test.dart'; + +import '../../helpers/capture.dart'; +import '../../helpers/fake_modrinth.dart'; + +void main() { + for (final declaredResource in [false, true]) { + test( + 'override targets declared entries with ${declaredResource ? 'declared' : 'synthetic'} same-name CF dependency', + () async { + final root = Directory.systemTemp.createTempSync('override_collision_'); + final mr = FakeModrinth(); + await mr.start(); + final cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() async { + await cf.close(force: true); + await mr.stop(); + root.deleteSync(recursive: true); + }); + mr.registerVersion(slug: 'library', versionNumber: '1.0.0'); + mr.registerVersion(slug: 'library', versionNumber: '2.0.0'); + final host = 'http://127.0.0.1:${cf.port}'; + final projects = { + 1: { + 'id': 1, + 'gameId': 432, + 'classId': 6, + 'slug': 'anchor', + 'name': 'Anchor', + }, + 2: { + 'id': 2, + 'gameId': 432, + 'classId': 12, + 'slug': 'library', + 'name': 'Library resource pack', + }, + }; + Map file( + int project, + int id, + String version, + List children, + ) => { + 'id': id, + 'modId': project, + 'displayName': version, + 'fileName': '$id.jar', + 'releaseType': 1, + 'fileDate': '2026-${version.startsWith('3') ? '03' : '02'}-01', + 'gameVersions': ['1.21.1'], + 'hashes': [], + 'dependencies': [ + for (final child in children) {'modId': child, 'relationType': 3}, + ], + 'downloadUrl': '$host/artifact/$id', + }; + final files = { + 1: [ + file(1, 11, '1.0.0', [2]), + ], + 2: [file(2, 32, '3.0.0', []), file(2, 22, '2.0.0', [])], + }; + cf.listen((request) async { + if (request.uri.path.startsWith('/artifact/')) { + request.response.add([1, 2, 3]); + } else { + final parts = request.uri.pathSegments; + final dynamic data; + if (parts.last == 'search') { + data = projects.values + .where( + (project) => + project['slug'] == request.uri.queryParameters['slug'], + ) + .toList(); + } else { + final id = int.parse(parts[2]); + data = parts.length == 3 ? projects[id] : files[id]; + } + request.response.headers.contentType = ContentType.json; + request.response.write( + jsonEncode({ + 'data': data, + if (data is List) + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': data.length, + 'totalCount': data.length, + }, + }), + ); + } + await request.response.close(); + }); + File('${root.path}/mods.yaml').writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'neoforge:21.1.50'} +gitrinth: {modrinth: disabled, curseforge: enabled} +mods: + library: {version: 1.0.0, sources: modrinth} + anchor: {sources: curseforge} +${declaredResource ? 'resource_packs:\n library: {version: 1.0.0, sources: curseforge}' : ''} +project_overrides: + library: 2.0.0 +'''); + final result = await runCli( + ['-C', root.path, 'get', '--no-search'], + environment: { + 'GITRINTH_CACHE': '${root.path}/cache', + 'GITRINTH_CONFIG': '${root.path}/config.yaml', + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_NEOFORGE_VERSIONS_URL': mr.neoforgeVersionsUrl, + 'GITRINTH_CURSEFORGE_URL': host, + 'GITRINTH_CURSEFORGE_TOKEN': 'test-key', + }, + ); + expect( + result.exitCode, + 0, + reason: '${result.stdout}\n${result.stderr}', + ); + final locked = parseModsLock( + File('${root.path}/mods.lock').readAsStringSync(), + filePath: 'mods.lock', + ); + expect(locked.mods['library']!.modrinth!.versionNumber, '2.0.0'); + expect( + locked.resourcePacks['library']!.curseforge!.fileId, + declaredResource ? 22 : 32, + ); + expect(locked.resourcePacks['library']!.requiredBy, ['anchor']); + }, + ); + } +} diff --git a/test/service/resolve/source_qualified_parent_test.dart b/test/service/resolve/source_qualified_parent_test.dart new file mode 100644 index 0000000..9e3753c --- /dev/null +++ b/test/service/resolve/source_qualified_parent_test.dart @@ -0,0 +1,135 @@ +import 'dart:io'; + +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/service/cache.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_metadata_store.dart'; +import 'package:crypto/crypto.dart'; +import 'package:gitrinth/src/service/resolve/lock_graph.dart'; +import 'package:gitrinth/src/service/resolve/resolve_and_sync.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; +import 'package:test/test.dart'; + +import '../../helpers/capture.dart'; + +void main() { + test( + 'CF-only parent uses source evidence in offline get and dependency reports', + () async { + final root = Directory.systemTemp.createTempSync('source_parent_'); + addTearDown(() => root.deleteSync(recursive: true)); + final cache = GitrinthCache(root: '${root.path}/cache'); + LockedEntry cf(String slug, int id, {bool child = false}) => LockedEntry( + slug: slug, + kind: LockedEntryKind.resolved, + version: '1.0.0', + dependency: child + ? LockedDependencyKind.transitive + : LockedDependencyKind.direct, + requiredBy: child ? ['root'] : [], + curseforge: LockedCurseforgeSource( + projectId: id, + fileId: id, + file: LockedFile(name: '$id.jar', sha1: sha1.convert([1]).toString()), + gameVersions: ['1.21.1'], + ), + ); + final before = ModsLock( + gitrinthVersion: '0.1.0', + loader: const LoaderConfig( + mods: ModLoader.fabric, + modLoaderVersion: '0.16.0', + ), + mcVersion: '1.21.1', + mods: { + 'root': const LockedEntry( + slug: 'root', + kind: LockedEntryKind.resolved, + modrinth: LockedModrinthSource( + projectId: 'mr-only', + versionId: 'v1', + file: LockedFile(name: 'mr.jar'), + ), + ), + 'library': cf('library', 3, child: true), + }, + resourcePacks: {'root': cf('root', 2)}, + ); + for (final id in [2, 3]) { + final file = File( + cache.curseforgePath(projectId: id, fileId: id, filename: '$id.jar'), + ); + file.parent.createSync(recursive: true); + file.writeAsBytesSync([1]); + } + for (final entry in synthesizeCfResolutionFromLock( + before, + ).resolved.values) { + CurseForgeMetadataStore(cache).write(entry.file); + } + final graph = LockedDependencyGraph( + before, + cache, + defaultHost: 'https://default.example/v2', + ); + expect(graph.diagnostics, isEmpty); + expect( + graph.graph.parents(const LocalEntryKey(Section.mods, 'library')), + {const LocalEntryKey(Section.resourcePacks, 'root')}, + ); + File('${root.path}/mods.lock').writeAsStringSync(emitModsLock(before)); + File('${root.path}/mods.yaml').writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: {mods: 'fabric:0.16.0'} +gitrinth: {modrinth: disabled, curseforge: enabled} +resource_packs: + root: +'''); + final result = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': cache.root, + 'GITRINTH_CONFIG': '${root.path}/config.yaml', + }, + ); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final after = parseModsLock( + File('${root.path}/mods.lock').readAsStringSync(), + filePath: 'mods.lock', + ); + expect(after.mods.keys, ['library']); + expect(after.mods['library']!.requiredBy, ['root']); + expect(after.resourcePacks['root']!.curseforge!.projectId, 2); + final mixed = before.copyWith( + mods: { + ...before.mods, + 'library': before.mods['library']!.copyWith( + modrinth: const LockedModrinthSource( + projectId: 'library-mr', + versionId: 'v1', + host: 'https://other.example/v2', + file: LockedFile(name: 'library.jar'), + ), + ), + }, + ); + final retained = synthesizeCfResolutionFromLock( + mixed, + manifest: parseModsYaml( + File('${root.path}/mods.yaml').readAsStringSync(), + filePath: 'mods.yaml', + ), + ); + expect( + retained.resolved.keys, + contains(const LocalEntryKey(Section.mods, 'library')), + ); + }, + ); +} diff --git a/test/service/resolve/transitive_bridge_test.dart b/test/service/resolve/transitive_bridge_test.dart new file mode 100644 index 0000000..ef46030 --- /dev/null +++ b/test/service/resolve/transitive_bridge_test.dart @@ -0,0 +1,252 @@ +import 'package:gitrinth/src/model/curseforge/cf_mod.dart'; +import 'package:gitrinth/src/model/curseforge/cf_mod_file.dart'; +import 'package:gitrinth/src/model/curseforge/cf_file_relation.dart'; +import 'package:gitrinth/src/model/curseforge/cf_search_response.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:gitrinth/src/model/manifest/mods_lock.dart'; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:gitrinth/src/model/manifest/emitter.dart'; +import 'package:gitrinth/src/service/console.dart'; +import 'package:gitrinth/src/service/curseforge/curseforge_api.dart'; +import 'package:gitrinth/src/service/resolve/curseforge_resolver.dart'; +import 'package:gitrinth/src/service/resolve/resolve_and_sync.dart'; +import 'package:test/test.dart'; +import 'package:gitrinth/src/service/source_identity.dart'; + +class GraphApi implements CurseForgeApi { + final Map projects; + final Map> edges; + GraphApi(this.projects, this.edges); + @override + Future getMod(int id) async => ModEnvelope(data: projects[id]!); + @override + Future searchMods({ + required int gameId, + int? classId, + String? slug, + String? searchFilter, + int? index, + int? pageSize, + }) async { + final data = projects.values + .where((p) => p.slug == slug && p.classId == classId) + .toList(); + return ModSearchResponse( + data: data, + pagination: Pagination( + index: 0, + pageSize: 50, + resultCount: data.length, + totalCount: data.length, + ), + ); + } + + @override + Future listFiles( + int modId, { + String? gameVersion, + int? modLoaderType, + int? index, + int? pageSize, + }) async => ModFileSearchResponse( + data: [ + ModFile( + id: modId * 10, + modId: modId, + displayName: '1.0.0', + fileName: '$modId.jar', + releaseType: 1, + fileDate: '2026-01-01', + gameVersions: ['1.21.1'], + hashes: [], + dependencies: [ + for (final child in edges[modId] ?? []) + FileRelation(modId: child, relationTypeCode: 3), + ], + ), + ], + pagination: Pagination( + index: 0, + pageSize: 50, + resultCount: 1, + totalCount: 1, + ), + ); + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +Mod project(int id, String slug, [int classId = 6]) => + Mod(id: id, gameId: 432, name: slug, slug: slug, classId: classId); +ModsYaml pack(Map mods) => ModsYaml( + slug: 'p', + name: 'p', + version: '1', + description: '', + loader: LoaderConfig(mods: ModLoader.fabric), + mcVersion: '1.21.1', + mods: mods, + gitrinth: GitrinthConfig(curseforge: GitrinthPlatformState.enabled), +); +void main() { + for (final constraint in ['^2.0.0', '2.0.0', 'different-label']) { + test( + 'CF-only version constraint $constraint excludes unmatched files', + () async { + final result = await resolveCurseforgeEntries( + manifest: pack({ + 'root': ModEntry( + slug: 'root', + sources: {SourceKind.curseforge}, + constraintRaw: constraint, + ), + }), + api: GraphApi({1: project(1, 'root')}, {}), + console: Console(level: LogLevel.error), + ); + expect(result.resolved, isEmpty); + expect(result.notFound.single.reason, 'no-compatible-files'); + }, + ); + } + test( + 'CF cycles terminate and reuse aliases while retaining all parents', + () async { + final result = await resolveCurseforgeEntries( + manifest: pack({ + 'root': ModEntry(slug: 'root'), + 'my-library': ModEntry(slug: 'my-library', curseforgeSlug: 'library'), + }), + api: GraphApi( + { + 1: project(1, 'root'), + 2: project(2, 'library'), + 3: project(3, 'other'), + }, + { + 1: [2, 3], + 2: [1], + 3: [2], + }, + ), + console: Console(level: LogLevel.error), + ); + expect( + result.resolved.keys.map((node) => node.localKey), + unorderedEquals(['root', 'my-library', 'other']), + ); + expect( + result + .resolved[const LocalEntryKey(Section.mods, 'my-library')]! + .requiredBy, + ['other', 'root'], + ); + expect( + result + .resolved[const LocalEntryKey(Section.mods, 'my-library')]! + .transitive, + isFalse, + ); + expect( + result.resolved[const LocalEntryKey(Section.mods, 'other')]!.transitive, + isTrue, + ); + expect( + result.resolved[const LocalEntryKey(Section.mods, 'root')]!.requiredBy, + ['my-library'], + ); + }, + ); + test('aliases keep every selected parent edge', () async { + final result = await resolveCurseforgeEntries( + manifest: pack({ + 'first': ModEntry(slug: 'first', curseforgeSlug: '1'), + 'second': ModEntry(slug: 'second', curseforgeSlug: '1'), + 'child-a': ModEntry(slug: 'child-a', curseforgeSlug: '2'), + 'child-b': ModEntry(slug: 'child-b', curseforgeSlug: '2'), + }), + api: GraphApi( + {1: project(1, 'root'), 2: project(2, 'library')}, + { + 1: [2], + }, + ), + console: Console(level: LogLevel.error), + ); + expect(result.resolved, hasLength(4)); + for (final name in ['child-a', 'child-b']) { + expect(result.resolved[LocalEntryKey(Section.mods, name)]!.requiredBy, [ + 'first', + 'second', + ]); + } + }); + test('CF dependencies infer their own section', () async { + final result = await resolveCurseforgeEntries( + manifest: pack({'root': ModEntry(slug: 'root')}), + api: GraphApi( + {1: project(1, 'root'), 2: project(2, 'textures', 12)}, + { + 1: [2], + }, + ), + console: Console(level: LogLevel.error), + ); + expect( + result + .resolved[const LocalEntryKey(Section.resourcePacks, 'textures')]! + .section, + Section.resourcePacks, + ); + }); + test('required_by round trips and prunes offline orphan cycles', () { + final lock = ModsLock( + gitrinthVersion: '1', + loader: LoaderConfig(mods: ModLoader.fabric), + mcVersion: '1.21.1', + mods: { + for (final name in ['root', 'child', 'orphan-a', 'orphan-b']) + name: LockedEntry( + slug: name, + kind: LockedEntryKind.resolved, + dependency: name == 'root' + ? LockedDependencyKind.direct + : LockedDependencyKind.transitive, + requiredBy: switch (name) { + 'child' => ['root'], + 'orphan-a' => ['orphan-b'], + 'orphan-b' => ['orphan-a'], + _ => [], + }, + curseforge: LockedCurseforgeSource( + projectId: name.length, + fileId: 1, + file: LockedFile(name: '$name.jar'), + ), + ), + }, + ); + final roundTrip = parseModsLock(emitModsLock(lock), filePath: 'mods.lock'); + expect(roundTrip.mods['child']!.requiredBy, ['root']); + final retained = synthesizeCfResolutionFromLock( + roundTrip, + manifest: pack({'root': ModEntry(slug: 'root')}), + ); + expect( + retained.resolved.keys.map((node) => node.localKey), + unorderedEquals(['root', 'child']), + ); + expect( + retained.resolved[const LocalEntryKey(Section.mods, 'child')]!.requiredBy, + ['root'], + ); + final narrowed = synthesizeCfResolutionFromLock( + roundTrip, + manifest: pack({ + 'root': ModEntry(slug: 'root', sources: {SourceKind.modrinth}), + }), + ); + expect(narrowed.resolved, isEmpty); + }); +} diff --git a/test/service/resolve/transitive_integration_test.dart b/test/service/resolve/transitive_integration_test.dart new file mode 100644 index 0000000..c3ba043 --- /dev/null +++ b/test/service/resolve/transitive_integration_test.dart @@ -0,0 +1,448 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:crypto/crypto.dart'; +import 'package:path/path.dart' as p; +import 'package:gitrinth/src/model/manifest/parser.dart'; +import 'package:test/test.dart'; +import '../../helpers/capture.dart'; +import '../../helpers/fake_modrinth.dart'; + +void main() { + test( + 'identical project IDs on two hosts retain separate dependency identities offline', + () async { + final root = Directory.systemTemp.createTempSync('two_host_graph_'); + final defaultApi = FakeModrinth(); + final hostedApi = FakeModrinth(); + await defaultApi.start(); + await hostedApi.start(); + addTearDown(() async { + await defaultApi.stop(); + await hostedApi.stop(); + root.deleteSync(recursive: true); + }); + for (final api in [defaultApi, hostedApi]) { + api.registerVersion( + slug: 'anchor', + versionNumber: '1.0.0', + requiredDeps: ['library'], + ); + api.registerVersion(slug: 'library', versionNumber: '2.0.0'); + } + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +modrinth_host: ${hostedApi.baseUrl} +loader: + mods: "neoforge:21.1.50" +mods: + default-anchor: + modrinth: anchor + modrinth_host: ${defaultApi.baseUrl} + hosted-anchor: + modrinth: anchor +'''); + final environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': defaultApi.baseUrl, + 'GITRINTH_NEOFORGE_VERSIONS_URL': defaultApi.neoforgeVersionsUrl, + }; + final online = await runCli([ + '-C', + root.path, + 'get', + ], environment: environment); + expect(online.exitCode, 0, reason: '${online.stdout}\n${online.stderr}'); + final lockFile = File(p.join(root.path, 'mods.lock')); + final before = lockFile.readAsStringSync(); + final lock = parseModsLock(before, filePath: 'mods.lock'); + final children = lock.mods.values + .where((e) => e.modrinth?.projectId == 'library_ID') + .toList(); + expect(children, hasLength(2)); + expect( + children.map((e) => e.modrinth!.host), + unorderedEquals([null, hostedApi.baseUrl]), + ); + final offline = await runCli([ + '-C', + root.path, + 'get', + '--offline', + ], environment: environment); + expect(offline.exitCode, 0, reason: offline.stderr); + expect(lockFile.readAsStringSync(), before); + }, + ); + test( + 'same local key in different sections keeps independent source hosts online and offline', + () async { + final root = Directory.systemTemp.createTempSync('two_host_graph_'); + final defaultApi = FakeModrinth(); + final hostedApi = FakeModrinth(); + await defaultApi.start(); + await hostedApi.start(); + addTearDown(() async { + await defaultApi.stop(); + await hostedApi.stop(); + root.deleteSync(recursive: true); + }); + for (final api in [defaultApi, hostedApi]) { + api.registerVersion( + slug: 'anchor', + versionNumber: '1.0.0', + requiredDeps: ['library'], + ); + api.registerVersion(slug: 'library', versionNumber: '2.0.0'); + } + for (final versions in hostedApi.versions.values) { + versions.single['loaders'] = ['minecraft', 'neoforge']; + } + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +modrinth_host: ${hostedApi.baseUrl} +loader: + mods: "neoforge:21.1.50" +mods: + anchor: + modrinth: anchor + modrinth_host: ${defaultApi.baseUrl} +resource_packs: + anchor: + modrinth: anchor +'''); + final environment = { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': defaultApi.baseUrl, + 'GITRINTH_NEOFORGE_VERSIONS_URL': defaultApi.neoforgeVersionsUrl, + }; + final online = await runCli([ + '-C', + root.path, + 'get', + ], environment: environment); + expect(online.exitCode, 0, reason: '${online.stdout}\n${online.stderr}'); + final lockFile = File(p.join(root.path, 'mods.lock')); + final before = lockFile.readAsStringSync(); + final lock = parseModsLock(before, filePath: 'mods.lock'); + expect(lock.mods['anchor']!.modrinth!.host, isNull); + expect(lock.resourcePacks['anchor']!.modrinth!.host, hostedApi.baseUrl); + final children = [ + ...lock.mods.values, + ...lock.resourcePacks.values, + ].where((e) => e.modrinth?.projectId == 'library_ID').toList(); + expect(children, hasLength(2)); + expect( + children.map((e) => e.modrinth!.host), + unorderedEquals([null, hostedApi.baseUrl]), + ); + final offline = await runCli([ + '-C', + root.path, + 'get', + '--offline', + ], environment: environment); + expect(offline.exitCode, 0, reason: offline.stderr); + expect(lockFile.readAsStringSync(), before); + }, + ); + test( + 'declared plugin dependency alias takes precedence over sparse project metadata', + () async { + final root = Directory.systemTemp.createTempSync('declared_plugin_'); + final mr = FakeModrinth(); + await mr.start(); + addTearDown(() async { + await mr.stop(); + root.deleteSync(recursive: true); + }); + mr.registerVersion( + slug: 'anchor', + versionNumber: '1.0.0', + requiredDeps: ['library'], + ); + mr.registerVersion(slug: 'library', versionNumber: '2.0.0'); + for (final versions in mr.versions.values) { + versions.single['loaders'] = ['paper']; + } + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: + plugins: "paper:100" +plugins: + anchor: null + my-library: + modrinth: library + version: ^2.0.0 +'''); + final result = await runCli( + ['-C', root.path, 'get'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_PAPER_API_URL': mr.paperBuildsUrl, + }, + ); + expect(result.exitCode, 0, reason: '${result.stdout}\n${result.stderr}'); + final lock = parseModsLock( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + filePath: 'mods.lock', + ); + expect(lock.plugins.keys, unorderedEquals(['anchor', 'my-library'])); + expect(lock.plugins['my-library']!.requiredBy, ['anchor']); + expect(lock.mods, isEmpty); + }, + ); + for (final (olderSelection, mrOnlyRoot, cfOnlyRoot, searchPeers) in [ + (false, false, false, false), + (true, false, false, false), + (false, true, false, false), + (false, false, true, false), + (false, true, false, true), + (false, false, true, true), + ]) { + test( + searchPeers + ? 'transitive ${mrOnlyRoot ? "CurseForge" : "Modrinth"} peers use hash-verified search' + : cfOnlyRoot + ? 'offline preserves synthetic peers under a CurseForge-only root' + : mrOnlyRoot + ? 'offline preserves synthetic peers under a Modrinth-only root' + : olderSelection + ? 'hash-selected CF graph removes stale children and probes new MR peers' + : 'divergent synthetic slugs merge by hash with unioned parent edges', + () async { + final root = Directory.systemTemp.createTempSync('transitive_bridge_'); + final mr = FakeModrinth(); + await mr.start(); + final cf = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() async { + await cf.close(force: true); + await mr.stop(); + root.deleteSync(recursive: true); + }); + mr.registerVersion( + slug: 'anchor', + versionNumber: '1.0.0', + requiredDeps: olderSelection ? [] : ['library'], + ); + mr.registerVersion( + slug: 'library', + versionNumber: '2.0.0', + requiredDeps: ['leaf'], + ); + mr.registerVersion(slug: 'leaf', versionNumber: '3.0.0'); + mr.searchHits = [ + { + 'project_id': 'library_ID', + 'slug': 'library', + 'title': 'Library', + 'author': 'Author', + 'project_type': 'mod', + 'categories': ['neoforge'], + }, + ]; + for (final versions in mr.versions.values) { + for (final version in versions) { + final file = (version['files'] as List).single as Map; + final url = Uri.parse(file['url'] as String); + final key = url.pathSegments + .skip(url.pathSegments.length - 2) + .join('/'); + (file['hashes'] as Map)['sha1'] = sha1 + .convert(mr.artifacts[key]!) + .toString(); + } + } + final anchorFile = + (mr.versions['anchor']!.single['files'] as List).single as Map; + final libraryFile = + (mr.versions['library']!.single['files'] as List).single as Map; + final leafFile = + (mr.versions['leaf']!.single['files'] as List).single as Map; + final projects = { + 4: { + 'id': 4, + 'gameId': 432, + 'classId': 6, + 'slug': 'leaf', + 'name': 'Leaf', + }, + 1: { + 'id': 1, + 'gameId': 432, + 'classId': 6, + 'slug': 'anchor', + 'name': 'Anchor', + }, + 2: { + 'id': 2, + 'gameId': 432, + 'classId': 6, + 'slug': !searchPeers && (olderSelection || mrOnlyRoot || cfOnlyRoot) + ? 'library' + : 'library-forge', + 'name': 'Library', + }, + 3: { + 'id': 3, + 'gameId': 432, + 'classId': 6, + 'slug': 'stale-library', + 'name': 'Stale', + }, + }; + Map file( + int project, + int id, + Map reference, + List children, + String date, + ) => { + 'id': id, + 'modId': project, + 'displayName': '1.0.0', + 'fileName': '$id.jar', + 'releaseType': 1, + 'fileDate': date, + 'gameVersions': ['1.21.1'], + 'hashes': [ + {'algo': 1, 'value': (reference['hashes'] as Map)['sha1']}, + ], + 'dependencies': [ + for (final child in children) {'modId': child, 'relationType': 3}, + ], + 'downloadUrl': reference['url'], + }; + final files = { + 1: [ + if (olderSelection) file(1, 12, libraryFile, [3], '2026-02-01'), + file(1, 11, anchorFile, [2], '2026-01-01'), + ], + 2: [ + file(2, 21, libraryFile, [4], '2026-01-01'), + ], + 4: [file(4, 41, leafFile, [], '2026-01-01')], + 3: [file(3, 31, libraryFile, [], '2026-01-01')], + }; + final requestCounts = {}; + cf.listen((req) async { + requestCounts.update( + req.uri.toString(), + (count) => count + 1, + ifAbsent: () => 1, + ); + dynamic data; + if (req.uri.path.endsWith('/search')) { + data = projects.values + .where( + (project) => + project['slug'] == req.uri.queryParameters['slug'] || + (searchPeers && + req.uri.queryParameters['searchFilter'] == + 'library' && + project['id'] == 2), + ) + .toList(); + } else { + final parts = req.uri.path.split('/'); + final id = int.tryParse(parts[3]); + data = parts.length == 4 ? projects[id] : files[id] ?? []; + } + req.response.headers.contentType = ContentType.json; + req.response.write( + jsonEncode({ + 'data': data, + if (data is List) + 'pagination': { + 'index': 0, + 'pageSize': 50, + 'resultCount': data.length, + 'totalCount': data.length, + }, + }), + ); + await req.response.close(); + }); + File(p.join(root.path, 'mods.yaml')).writeAsStringSync(''' +slug: pack +name: Pack +version: 1.0.0 +description: test +mc_version: 1.21.1 +loader: + mods: "neoforge:21.1.50" +gitrinth: + curseforge: enabled +mods: + anchor: ${mrOnlyRoot + ? '{sources: modrinth}' + : cfOnlyRoot + ? '{sources: curseforge}' + : 'null'} +'''); + final result = await runCli( + ['-C', root.path, 'get', if (!searchPeers) '--no-search'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_NEOFORGE_VERSIONS_URL': mr.neoforgeVersionsUrl, + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_CURSEFORGE_URL': 'http://127.0.0.1:${cf.port}', + 'GITRINTH_CURSEFORGE_TOKEN': 'test-key', + }, + ); + expect( + result.exitCode, + 0, + reason: '${result.stdout}\n${result.stderr}', + ); + final lock = parseModsLock( + File(p.join(root.path, 'mods.lock')).readAsStringSync(), + filePath: 'mods.lock', + ); + expect(lock.mods.keys, unorderedEquals(['anchor', 'library', 'leaf'])); + expect(lock.mods['leaf']!.modrinth, isNotNull); + expect(lock.mods['leaf']!.curseforge, isNotNull); + expect(lock.mods['leaf']!.requiredBy, ['library']); + expect(lock.mods['library']!.modrinth, isNotNull); + expect(lock.mods['library']!.curseforge, isNotNull); + expect(lock.mods['library']!.requiredBy, ['anchor']); + expect( + requestCounts.values.every((count) => count == 1), + isTrue, + reason: '$requestCounts', + ); + if (olderSelection) { + expect(lock.mods['anchor']!.curseforge!.fileId, 11); + } else if (!mrOnlyRoot && !cfOnlyRoot) { + expect(lock.mods['library']!.curseforge!.slug, 'library-forge'); + } + { + final lockFile = File(p.join(root.path, 'mods.lock')); + final before = lockFile.readAsStringSync(); + final offline = await runCli( + ['-C', root.path, 'get', '--offline'], + environment: { + 'GITRINTH_CACHE': p.join(root.path, 'cache'), + 'GITRINTH_MODRINTH_URL': mr.baseUrl, + 'GITRINTH_CURSEFORGE_URL': 'http://127.0.0.1:${cf.port}', + }, + ); + expect(offline.exitCode, 0, reason: offline.stderr); + expect(lockFile.readAsStringSync(), before); + } + }, + ); + } +} diff --git a/test/service/section_inference_test.dart b/test/service/section_inference_test.dart index 6502b98..59c1bdd 100644 --- a/test/service/section_inference_test.dart +++ b/test/service/section_inference_test.dart @@ -2,7 +2,7 @@ import 'package:test/test.dart'; import 'package:gitrinth/src/cli/exceptions.dart'; import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; -import 'package:gitrinth/src/service/section_inference.dart'; +import 'package:gitrinth/src/service/resolve/section_inference.dart'; void main() { group('inferSectionFromProject', () { diff --git a/test/service/solve_report_test.dart b/test/service/solve_report_test.dart index 8f60bf5..da5cdc5 100644 --- a/test/service/solve_report_test.dart +++ b/test/service/solve_report_test.dart @@ -1,8 +1,9 @@ +import 'package:gitrinth/src/service/source_identity.dart'; import 'package:gitrinth/src/model/manifest/mods_lock.dart'; import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; import 'package:gitrinth/src/model/modrinth/version.dart' as modrinth; import 'package:gitrinth/src/service/console.dart'; -import 'package:gitrinth/src/service/solve_report.dart'; +import 'package:gitrinth/src/service/resolve/solve_report.dart'; import 'package:test/test.dart'; class _CaptureConsole extends Console { @@ -25,27 +26,31 @@ LockedEntry _modrinth( }) { return LockedEntry( slug: slug, - sourceKind: LockedSourceKind.modrinth, + kind: LockedEntryKind.resolved, version: version, - projectId: projectId, - versionId: versionId, - file: LockedFile( - name: '$slug-$version.jar', - url: 'https://cdn/$slug-$version.jar', - sha512: sha512, - size: 1024, + modrinth: LockedModrinthSource( + projectId: projectId, + versionId: versionId, + versionNumber: version, + file: LockedFile( + name: '$slug-$version.jar', + url: 'https://cdn/$slug-$version.jar', + sha512: sha512, + size: 1024, + ), ), ); } LockedEntry _url(String slug, String url) => LockedEntry( slug: slug, - sourceKind: LockedSourceKind.url, - file: LockedFile(name: 'x.jar', url: url), + kind: LockedEntryKind.url, + url: url, + urlFile: LockedFile(name: 'x.jar', url: url), ); LockedEntry _path(String slug, String path) => - LockedEntry(slug: slug, sourceKind: LockedSourceKind.path, path: path); + LockedEntry(slug: slug, kind: LockedEntryKind.path, path: path); ModsLock _lock({ Map mods = const {}, @@ -433,14 +438,34 @@ void main() { }); group('countOutdated', () { + test('same local name in two sections has independent candidates', () { + final lock = _lock( + mods: {'same': _modrinth('same', '1.0.0')}, + shaders: {'same': _modrinth('same', '1.0.0')}, + ); + expect( + countOutdated(lock, { + const LocalEntryKey(Section.mods, 'same'): [_mrVersion('2.0.0')], + const LocalEntryKey(Section.shaders, 'same'): [_mrVersion('1.0.0')], + }), + 1, + ); + }); + test('sums across sections', () { final lock = _lock( mods: {'a': _modrinth('a', '1.0.0')}, shaders: {'b': _modrinth('b', '1.0.0')}, ); final versions = { - 'a': [_mrVersion('1.0.0'), _mrVersion('2.0.0')], - 'b': [_mrVersion('1.0.0'), _mrVersion('1.0.1')], + const LocalEntryKey(Section.mods, 'a'): [ + _mrVersion('1.0.0'), + _mrVersion('2.0.0'), + ], + const LocalEntryKey(Section.shaders, 'b'): [ + _mrVersion('1.0.0'), + _mrVersion('1.0.1'), + ], }; expect(countOutdated(lock, versions), 2); }); @@ -454,9 +479,18 @@ void main() { }, ); final versions = { - 'a': [_mrVersion('1.0.0'), _mrVersion('2.0.0')], - 'u': [_mrVersion('1.0.0'), _mrVersion('9.9.9')], - 'p': [_mrVersion('1.0.0'), _mrVersion('9.9.9')], + const LocalEntryKey(Section.mods, 'a'): [ + _mrVersion('1.0.0'), + _mrVersion('2.0.0'), + ], + const LocalEntryKey(Section.mods, 'u'): [ + _mrVersion('1.0.0'), + _mrVersion('9.9.9'), + ], + const LocalEntryKey(Section.mods, 'p'): [ + _mrVersion('1.0.0'), + _mrVersion('9.9.9'), + ], }; expect(countOutdated(lock, versions), 1); }); @@ -464,7 +498,7 @@ void main() { test('returns 0 when everything is current', () { final lock = _lock(mods: {'a': _modrinth('a', '1.0.0')}); final versions = { - 'a': [_mrVersion('1.0.0')], + const LocalEntryKey(Section.mods, 'a'): [_mrVersion('1.0.0')], }; expect(countOutdated(lock, versions), 0); }); @@ -514,7 +548,7 @@ void main() { newLock: lock, diff: const [], versionsPerSlug: { - 'a': [_mrVersion('1.0.0')], + const LocalEntryKey(Section.mods, 'a'): [_mrVersion('1.0.0')], }, overriddenSlugs: const {}, ); diff --git a/test/service/source_identity_test.dart b/test/service/source_identity_test.dart new file mode 100644 index 0000000..bd8cc78 --- /dev/null +++ b/test/service/source_identity_test.dart @@ -0,0 +1,125 @@ +import 'package:gitrinth/src/service/source_identity.dart'; +import 'package:gitrinth/src/model/manifest/mods_yaml.dart'; +import 'package:test/test.dart'; +import 'package:gitrinth/src/model/resolver/resolver.dart'; +import 'package:gitrinth/src/model/resolver/pubgrub.dart'; +import 'package:gitrinth/src/model/resolver/modrinth_solver_adapter.dart'; +import 'package:gitrinth/src/model/modrinth/version.dart' as mr; +import 'package:gitrinth/src/model/modrinth/dependency.dart'; + +void main() { + test( + 'transitive opaque failures keep local labels and structured conflicts', + () async { + final identities = SourceIdentity(); + const root = LocalEntryKey(Section.mods, 'anchor'); + const existing = LocalEntryKey(Section.mods, 'library'); + const transitive = LocalEntryKey(Section.resourcePacks, 'library'); + for (final node in [root, existing]) { + identities.register(node, host: 'https://example.com/v2'); + } + final resolver = Resolver( + nodeKeyFor: (section, slug) => + identities.solverKey(LocalEntryKey(section, slug)), + localNameFor: (key) => identities.nodeForSolverKey(key).localKey, + formatError: identities.displayMessage, + sectionForSlug: (key) => identities.nodeForSolverKey(key).section, + resolveSlugForProjectId: (_) async => null, + resolveDependencySlug: (_, _) async { + identities.register(transitive, host: 'https://example.com/v2'); + return identities.solverKey(transitive); + }, + listVersions: (key) async => key == 'anchor' + ? [ + ModrinthSolverVersion( + const mr.Version( + id: 'V', + projectId: 'anchor', + versionNumber: '1.0.0', + files: [], + dependencies: [ + Dependency( + projectId: 'LIB', + dependencyType: DependencyType.required, + ), + ], + loaders: [], + gameVersions: [], + ), + ), + ] + : [], + ); + final manifest = ModsYaml( + slug: 'p', + name: 'p', + version: '1', + description: '', + mcVersion: '1.21.1', + loader: const LoaderConfig(mods: ModLoader.fabric), + mods: {'anchor': const ModEntry(slug: 'anchor')}, + ); + await expectLater( + resolver.resolve(manifest), + throwsA( + isA() + .having( + (error) => error.message, + 'message', + allOf( + contains('library'), + isNot(contains('\u0000')), + isNot(contains('resourcePacks/')), + ), + ) + .having( + (error) => error.conflictingUserSlugs, + 'conflicting names', + {'anchor'}, + ), + ), + ); + }, + ); + test( + 'remote aliases preserve local keys and full normalized host identity', + () { + final identities = SourceIdentity(); + const mod = LocalEntryKey(Section.mods, 'same'); + const resource = LocalEntryKey(Section.resourcePacks, 'same'); + const alias = LocalEntryKey(Section.mods, 'alias'); + for (final node in [mod, resource, alias]) { + identities.register(node, host: 'HTTPS://Example.COM:8443/api/v2/'); + identities.bind( + node, + ModrinthProjectKey('https://example.com:8443/api/v2', 'PID'), + ); + } + expect( + identities.aliases( + ModrinthProjectKey('https://EXAMPLE.com:8443/api/v2/', 'PID'), + ), + {mod, resource, alias}, + ); + expect( + identities.aliases( + ModrinthProjectKey('https://example.com:8443/other/v2', 'PID'), + ), + isEmpty, + ); + expect( + identities.aliases( + ModrinthProjectKey('https://example.com:8444/api/v2', 'PID'), + ), + isEmpty, + ); + expect(identities.solverKey(mod), isNot(identities.solverKey(resource))); + expect( + identities.nodeForSolverKey(identities.solverKey(resource)), + resource, + ); + expect(identities.find('same'), isNull); + expect(identities.find('same', Section.mods), mod); + }, + ); +}