Compare commits

..
Author SHA1 Message Date
Дмитрий 775aa58181 Merge remote-tracking branch 'origin/integrate/release-20261125' into preview/release-0.9.18 2026-11-25 14:38:41 +05:00
ДмитрийandClaude Sonnet 5.5 d4c4b472c9 Manager 0.9.18: translate the reviewed notes of the next server release
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 14:38:37 +05:00
Дмитрий b905625b4b Merge remote-tracking branch 'origin/integrate/release-20261125' into preview/release-0.9.18 2026-11-25 13:17:20 +05:00
ДмитрийandClaude Sonnet 5.5 728292f53c Test: make the model-list mock server reliable on Windows
The accepted socket inherits the listener's non-blocking mode, so the first read could fail and
reset the client. Send the response in one write and let the client close first.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 13:17:15 +05:00
Дмитрий 833094bc9f Merge remote-tracking branch 'origin/integrate/release-20261125' into preview/release-0.9.18 2026-11-25 12:24:37 +05:00
ДмитрийandClaude Sonnet 5.5 2d7a392df9 Test: compare the Playerbot Characters mount with the canonical server folder
The Windows runner's temporary folder is a short path, so the test failed there.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 12:24:33 +05:00
ДмитрийandClaude Sonnet 5.5 8476f4261d Merge Manager 0.9.18 into the Modules Preview line
Preview 0.9.18: Custom Races client 2.0.3, Playerbot Characters settings, companion reuse,
the Settings server card, and the two preview notes with their translations.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 12:07:21 +05:00
ДмитрийandClaude Sonnet 5.5 88ccce8280 Manager 0.9.18: version, release notes and their translations
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 08:00:35 +05:00
ДмитрийandClaude Sonnet 5.5 17eba1c30e Custom Races client 2.0.3
Installs the 2.0.3 set: new retail archives (clients 12 to 16), client 18 with the Snagtooth race and
the current CoACustomRace.exe. Clients 1 to 11 and 17 are unchanged and keep their cached names. The
2.0.2 inventory is kept for installs that still carry that manifest. Sizes and hashes of the changed
archives were verified against the release checksums.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 08:00:35 +05:00
ДмитрийandClaude Sonnet 5.5 dbf494c2c2 fix(companions): reuse offline bot pool before spawning and preserve AutoLogin.MaxCount
Ported from 7447347 onto the multi-realm companion flow. The server's queued count is used, an older
bots module without a count falls back to creating the bots, and requests for a chosen level or class
are always created.

(cherry picked from commit 7447347)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 07:52:58 +05:00
Дмитрий 086534f849 Merge PR #238: show the server version and update button in Settings 2026-11-25 07:52:07 +05:00
Дмитрий 1b0adbf94c Merge PR #234: Playerbot Characters model setup 2026-11-25 07:52:06 +05:00
ДмитрийandClaude Sonnet 5.5 64bfe33778 A failed module build is an error, never an old package installed in its place
install_module_sources ignored BuildResult.success, so a failing build (for example a
CMake error) installed the package an earlier build had left and reported success.
Found while checking the Update button. The dev mock also shows an available update.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 14:27:03 +05:00
ДмитрийandClaude Sonnet 5.5 e8bf35f95d Bot deploy shows the module's refusal; modules update only when the owner asks
- Deploy: a refusal text of the bot module (switched off, rejected, 0 created) is an
  error, not a started job; the outdated-module message points to the Modules page.
- Modules: a module whose build differs from the commit the core recommends shows
  an Update button. Nothing updates by itself, not even on a core change. Needs the
  world stopped, merges the module's settings.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 13:54:48 +05:00
ДмитрийandClaude Sonnet 5.5 5db6691b8b Merge master into the Modules Preview line
Keeps the preview identity (name, version, identifier) and its restructured Modules
page; takes master's translated client patch card and the release notes; the built-in
client download stays removed as on master.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 13:52:05 +05:00
ДмитрийandClaude Sonnet 5.5 a8afe88c83 Show the server version and update button in Settings after an update check
The Server card in Settings > Updates copied the shared update offer into local state once and
an effect wiped that copy whenever a finished update attempt or disk-state refresh had bumped
the shared revision, so after a check the card kept showing only "..." with no update button
while the main screen (which reads the shared state) still offered the update.

The card now derives the offer from the shared state; a manual check on the card is kept only
until the next finished attempt. Conflict choices default to "keep" without a copy as well.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 13:07:57 +05:00
github-actions[bot] 657229aafc Update published Manager changelog 2026-11-23 17:20:00 +00:00
Corfirean b5abc6fc4f Merge pull request #237 from Corfirean/release/manager-0.9.17
Manager 0.9.17: client link points to conquestofazerothcore.to
2026-11-23 21:52:24 +05:00
ДмитрийandClaude Sonnet 5.5 81c5556ffe Point the client setup link to conquestofazerothcore.to and prepare Manager 0.9.17
The old community site (coa-development.org) no longer works. The client setup dialog, the open-link
allowlist and the six translations now use https://conquestofazerothcore.to/, which serves the launcher
download and account sign-up. Version 0.9.17 (Manager only), reviewed note and its translations added.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 21:51:53 +05:00
Nightwalker743 4b51301230 Keep Playerbot Characters settings isolated across realms 2026-11-20 18:19:46 -06:00
Nightwalker743 6f1eaac719 Discover Playerbot Characters models from the provider 2026-11-20 16:41:03 -06:00
Nightwalker743 206d4844c9 Add Playerbot Characters model setup 2026-11-20 16:14:27 -06:00
github-actions[bot] 10cfdcf5a2 Update published Manager changelog 2026-11-20 17:37:52 +00:00
Corfirean 316ef078e8 Merge pull request #233 from Corfirean/release/manager-0.9.16
Manager 0.9.16
2026-11-20 22:08:03 +05:00
ДмитрийandClaude Sonnet 5.5 1974ff54e3 Prepare Manager 0.9.16 with reviewed notes in six languages
Version 0.9.16 (Manager only; no server release). Reviewed notes: the client download now comes from
the community launcher, HD graphics hint and translated Custom Races patch card, companions startup
limit creates the missing ones, weekly world restart, server log auto-cleanup. Translations in ru,
de, fr, es and zh for these five notes and for the 0.9.15 note, which the history test requires.
Bundled server history stays at 0.261115.110; notes of later server releases show in English until
a Manager refreshes and translates them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:57:49 +05:00
Corfirean de3af82e5d Merge pull request #231 from Corfirean/feat/weekly-restart-log-cleanup-on-master
Settings: weekly world restart and server log auto-cleanup
2026-11-20 21:43:38 +05:00
Corfirean 802bde220c Merge pull request #230 from Corfirean/feat/startup-fleet-top-up
Companions: the startup limit creates the missing ones
2026-11-20 21:43:30 +05:00
Corfirean dfe7ac656b Merge pull request #229 from Corfirean/fix/client-download-launcher-on-master
Remove the dead built-in client download; point players to the launcher
2026-11-20 21:43:21 +05:00
Corfirean cee5139879 Merge pull request #232 from Corfirean/fix/changelog-fragment-length
Changelog: shorten the Custom Races 2.0.2 fragment to the 300-character limit
2026-11-20 21:43:12 +05:00
ДмитрийandClaude Sonnet 5.5 1ea2c8318b docs(changelog): shorten the Custom Races 2.0.2 fragment to the 300-character limit
Its body was 375 characters, so `changelog.py check` over the whole directory failed. The title is
unchanged, so the published 0.9.15 note is the same.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:43:02 +05:00
ДмитрийandClaude Sonnet 5.5 2b7dbf8e1e feat(settings): translations and changelog for weekly restart and log auto-cleanup
The weekly restart schedule and the server log auto-cleanup (previous commit) had strings only in
en and ru and no changelog fragments. Adds de/es/fr/zh for the weekday names, the weekly toggle
and the log settings, and one fragment per change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:37:01 +05:00
Дмитрий 4ff190f6d1 feat(settings): add weekly restart schedule and server log auto-cleanup 2026-11-20 21:34:23 +05:00
ДмитрийandClaude Sonnet 5.5 bff5248591 feat(companions): the startup limit is a fleet size, so missing companions are created
CoaBots.AutoLogin.MaxCount only makes the server log in companions that already exist: with 50
companions and a limit of 300, 50 came online. The Manager now creates the difference.

- coa-core: fleet_shortfall / queue_fleet_top_up queue one durable pending job per realm for
  limit - existing (at most 2000 per job, the most one server command takes). Nothing is queued
  when the fleet is complete or an unfinished job exists for the realm.
- start_server: after the world is up, each realm whose auto-login is on is topped up before its
  pending job runs (shared reconcile_realm_companions helper). If the existing count cannot be
  read, nothing is created.
- Saving the startup settings of a running server tops up right away (new top_up_startup_fleet
  command); a shortfall above 200 asks for confirmation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:31:32 +05:00
ДмитрийandClaude Sonnet 5.5 f5205662a9 feat(client): say where HD graphics is switched, translate the Custom Races patch card
The community launcher owns the Standard/HD choice (Settings -> "HD graphics"; its setup
also offers it), so the Manager cannot toggle it. The client setup dialog now points players
there. The Custom Races client patch status badges, hints and Install/Repair/Uninstall
buttons were English/Russian ternaries; they are now i18n keys in all six locales.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:08:55 +05:00
ДмитрийandClaude Sonnet 5.5 169d8b8977 feat(client): drop built-in client download, point players to the community launcher
The community moved the game client behind its official launcher (account
sign-in, signed manifest, its own CDN), so the public manifest now answers 404
and the Manager must not pose as the launcher. Remove the client download and
update path (clientdl, plan/sync/download/cancel commands, update job and
progress UI, version tracking) and reduce "Set up the game client" to: open
coa-development.org for the launcher, or pick a folder that already has the
game. Linking a folder, realmlist switch, companion addon and the custom-race
patch are unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 21:08:50 +05:00
ДмитрийandClaude Sonnet 5.5 954088192e Modules Preview: translate the 0.9.15 Manager note in five languages
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 01:43:01 +05:00
ДмитрийandClaude Sonnet 5.5 2f04207d50 Modules Preview: install the exact compiler next to older Build Tools
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-20 01:31:07 +05:00
ДмитрийandClaude Sonnet 5.5 9f6d35fb9f Merge master (Manager 0.9.15, Custom Races 2.0.2 client) into the Modules Preview
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-19 13:45:55 +05:00
ДмитрийandClaude Sonnet 5.5 41977442e9 Merge master into integrate/manager-0.9.15
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-19 13:12:54 +05:00
github-actions[bot] c75785d13e Update published Manager changelog 2026-11-19 07:59:40 +00:00
ДмитрийandClaude Sonnet 5.5 2b9021ce07 Manager 0.9.15: Custom Races client 2.0.2
Installs the 2.0.2 client set (retail body cache, subclass and HD patches, current
CoACustomRace.exe, newer subclass addon without the Skinshifter collection),
with the 2.0 inventory kept for installs that still carry that manifest.
Archive sizes and hashes verified against the real release archives.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 21:38:11 +05:00
ДмитрийandClaude Opus 5.5 d812b7868f Preview 0.9.14: release note for the fixed new-server install
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 07:43:00 +05:00
Дмитрий 8cce092d16 Merge preview 0.9.12: one copy of the bot strength settings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	package-lock.json
#	package.json
#	src-tauri/tauri.conf.json
2026-11-17 07:42:50 +05:00
ДмитрийandClaude Opus 5.5 d087d65444 Merge Manager 0.9.14 (master) into the Modules Preview
Brings the rest of Manager 0.9.14: multi-realm playerbots, Companions on
Wildcard/Reborn, and the reviewed 0.9.14 notes. Preview 0.9.14 keeps its own
name, identifier and updater; installing the current stable server needs
Manager 0.9.13 or newer, which 0.9.11/0.9.12 could not meet.

modules.rs: Companions follow master's per-build Wildcard/Reborn rule; the
version shown still comes from the plugin registry first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 07:42:32 +05:00
Corfirean b9e663079e Merge integrate/manager-0.9.14: Manager 0.9.14 (Rust launcher, fast races switch, ports, realm names, addons, companions on Wildcard/Reborn) 2026-11-16 18:14:10 +05:00
github-actions[bot] d7693b0878 Update published Manager changelog 2026-11-16 10:21:01 +00:00
ДмитрийandClaude Opus 5.5 1f64da3ae0 Prepare Manager 0.9.14 with reviewed notes in six languages
Version 0.9.14; reviewed Manager (12) and server (6) records with translations in all
five languages; server components from the lock of build run 43758471036; bundled server
history through 0.261115.110 from build main 45c013be9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:00:33 +05:00
ДмитрийandClaude Opus 5.5 1aec8e5394 Release note title the collector keeps (no technical word)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 02:18:18 +05:00
Дмитрий 326a4fe3d3 Merge feat/companions-realms: companion bots on Wildcard and Warcraft Reborn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	crates/coa-core/src/ra.rs
2026-11-16 00:52:15 +05:00
Дмитрий 59132216f7 Merge integrate/manager-0.9.14 into preview: characters kept out while custom races are off
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	src/screens/ModulesPage.tsx
2026-11-16 00:37:21 +05:00
ДмитрийandClaude Opus 5.5 f9839ba369 Custom races off: count the characters kept out, delete their companion bots
A client without the races patch crashes on a unit of race 12+, so the server now keeps
those characters out while custom races are off. The Custom Races card shows how many
player characters and bots that are (active realm, read only while the database runs) and
offers botcmd purgecustomraces for the companion bots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:36:18 +05:00
ДмитрийandClaude Opus 5.5 e951eb134b Companions open on Wildcard and Reborn when the server build plays them
The module list blocked Companions on every realm but Conquest of Azeroth, so the
multi-realm bot work could not be switched on. It is allowed (as experimental) when the
world server or its bots plugin carries the standard-class bot profiles of the multi-realm
bots build; SQUID and Dungeon Clear stay CoA-only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:27:54 +05:00
ДмитрийandClaude Opus 5.5 6cbf007d72 Companions on Wildcard/Reborn: pending bot jobs survive crashes, validated spawn requests
The uncommitted RC hardening of the multirealm Manager branch: a per-realm pending job
state machine reconciled against the database (no duplicate bots after a crash or an RA
timeout), cancelled by Stop spawning / Delete all bots, legacy file migrated; spawn
requests and build ids validated; tests JOB-01..JOB-14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:20:50 +05:00
Дмитрий 07e9bc497d feat(manager): dynamic wildcard archetypes, realm-isolated pending jobs, and hardened bot deployment 2026-11-16 00:17:08 +05:00
Дмитрий e4edb7cbdd feat(manager): multi-realm character database routing, RA dispatch & extended bot manager UI 2026-11-16 00:16:51 +05:00
Дмитрий ca2514c790 feat(manager): expand multi-realm bot fleet controls, DB routing, and compatibility guards 2026-11-16 00:16:15 +05:00
Дмитрий 69c9938aba feat(manager): enable multi-realm playerbots support across CoA, Wildcard, and Reborn 2026-11-16 00:16:15 +05:00
Дмитрий 06b606c100 Merge integrate/manager-0.9.14 into preview: V2 parts inside Custom Races, every addon in the list
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	crates/coa-core/src/modules.rs
#	src/lib/api.ts
#	src/screens/ModulesPage.tsx
2026-11-16 00:09:32 +05:00
ДмитрийandClaude Opus 5.5 2fd42f2acd Custom races hold their V2 parts; the addon list shows every addon
- Subclasses/Hero Races and the /dev menu are parts of Custom Races (catalog part_of,
  hidden from the grid): their switch and settings sit inside the Custom Races settings.
- The companion bots addon (CoABotUI) moves from the client facts into the addon list,
  with install/update from the server package; the /dev menu addon of the V2 patch is
  listed as installed by the patch, without its own buttons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:07:54 +05:00
Дмитрий 34f6fa1cd1 Merge integrate/manager-0.9.14 into preview: game cache, friend's login, addon catalog
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	crates/coa-core/src/lib.rs
2026-11-15 21:08:21 +05:00
ДмитрийandClaude Opus 5.5 fc8b18ef97 Merge integrate/manager-0.9.14 into preview: Custom Races V2, fast races switch, ports, realm names, V2 switches, MySQL options, scheduled restart
Versions stay the preview's. schemas/bots.json keeps the preview's own bot scaling
settings (master brought the released ones a second time, as duplicate keys).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:08:14 +05:00
ДмитрийandClaude Opus 5.5 05630ec9eb Addon catalog in the game client card
addons/catalog.json pins SquidBots Lite, DragonUI (+Options), IncahootsCoA and CoACombat
to a commit and lists every file with its git blob SHA-1 and size. Install downloads
GitHub's archive of that commit, verifies each file (archives are not byte-stable, their
contents are), stages next to AddOns and moves the folders in; a foreign folder of the
same name goes to AddOns/.coa-addons-backup. .coa-addons.json records what the Manager
installed; update and remove touch only that. CoA_DevMenu ships with Custom Races V2, so
the separate dev menu addon is not in the catalog. Checked against the real GitHub
archives of the three small addons (ignored test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:58:29 +05:00
ДмитрийandClaude Opus 5.5 d64fdb51be Client: clear the game cache on Play, remember the friend's server login
- clear_game_cache removes Cache/WDB/<locale>/*.wdb before every Play (own server and
  friend's), on by default, switch in the client card; nothing else in Cache or Data.
- The connection profile keeps the listed realm it was joined through; Play fills that
  realm's saved account into GlueConfig.json every time. A host typed by hand drops it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:51:40 +05:00
ДмитрийandClaude Opus 5.5 260d10b729 Scheduled daily restart of the world (#202)
Settings/restart-schedule.json (Settings, Startup) holds a local time and a countdown.
The Rust world supervisor asks the world for 'server restart <seconds>' once that time has
passed (by at most 15 minutes, for a world that ran before it), and starts the world
again when it exits with RESTART_EXIT_CODE; the database and auth keep running. While it
is under way (.state/scheduled-restart) the status says so and the Manager does not take
the down world for a crash. Extra worlds follow their server's schedule. Repacks with the
Python supervisor do not run the schedule; the card says the server needs an update.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:26:14 +05:00
ДмитрийandClaude Opus 5.5 20657df203 MySQL options of the owner kept across starts (#103)
Settings, Service edits Settings/mysql-custom.ini. Every start adds its name = value lines
(outside a section or in [mysqld]) to the rendered my.ini, after the defaults so they
win; the port, addresses, data and log paths, sockets and !include stay managed. Nothing
changes for a server without the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:21:24 +05:00
ДмитрийandClaude Opus 5.5 f08ae1c76b Modules: switches for the Custom Races V2 subclasses and the /dev menu
mod-subclass (Subclass.Enable, the Thin Twins and Two-Headed Ogre races, two players per
character, levels) and mod-ilusixn-devmenu (DevMenu.Enable, who may use it) were shipped
with V2 but had no place in the Manager. They are catalog modules now, with translated
controls for their main settings; the rest stays under the advanced settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:17:04 +05:00
ДмитрийandClaude Opus 5.5 4fc133c35e Race restriction survives a catalog that only adds races
The worldserver ignores DisabledRaceIds saved against another ChrRaces catalog and with
it CoACustomRaces.Enable, so every server that had a restriction from 1.9.2 lost custom
races after the V2 update. Before a start the Manager binds the restriction to the new
catalog when every disabled race keeps its RaceID and name, judged by the server's own
snapshot (written by every save) or the table of every catalog a build has shipped
(race_catalogs.json, generated from the core history). Renamed races (1.9.1 reused 32/33)
and unknown catalogs still wait for the owner's review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:14:59 +05:00
ДмитрийandClaude Opus 5.5 de38e60444 Merge fix/races-fast-toggle: custom races switch in seconds
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:10:50 +05:00
ДмитрийandClaude Opus 5.5 3c26004e04 Custom races: switch in seconds (append-only journal, entry points only, hash cache)
V2 has 7,220 files. Switching rewrote the whole transaction journal with fsync after
every file (quadratic: minutes on fast disks, far longer with antivirus) and renamed
every file. Now:
- the journal is append-only JSON lines; recovery replays it, ignores a torn last
  line and still reads the old journal.json of 0.9.13;
- only what the stock client loads is hidden (Data/*.MPQ, root dll/exe/bat, addons,
  any file that replaced the player's own); Data/Retail, VulkanShaders and the
  Esteria data stay in place, and a client disabled by 0.9.13 is recognised;
- payload hashes are remembered for the run, keyed by name, size and times.
Real V2 archives, release build: disable 1.9 s, enable 2.2 s, 224 of 7,220 files moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:10:43 +05:00
ДмитрийandClaude Opus 5.5 d1534ebc92 Realm names chosen in the Manager (#61, #209)
Settings, Startup lists every realm of the server with its name. The choice is kept in
Settings/realm-names.json of the main folder; the launcher, the realm-list setup and the
client's Config.wtf use it, and a running database gets it at once. Names are 1-32
characters without quotation marks or control characters, written as escaped SQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:08:48 +05:00
ДмитрийandClaude Opus 5.5 20d4c3351e Ports: name Windows reservations at once, move the database to a free port
A reserved or taken database port no longer fails the start after 30 seconds: the
launcher recognises Windows excluded ranges (netsh), moves MySQL to a free port and
records it in Settings/repack.json. Only the server's own services use that port.
The database connection of with_database is rebuilt after the start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:59:20 +05:00
ДмитрийandClaude Opus 5.5 8a313b83b8 Cargo.lock: coa-launcher follows the workspace version
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:55:40 +05:00
ДмитрийandClaude Opus 5.5 7eea72c415 Merge feat/rust-launcher: the Manager starts and stops servers through the Rust launcher
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:54:21 +05:00
Corfirean e8ffa6efe3 Merge feat/custom-races-v2: Manager 0.9.13 with Custom Races V2 2026-11-15 08:14:40 +05:00
github-actions[bot] baeecd2ddf Update published Manager changelog 2026-11-15 02:08:39 +00:00
ДмитрийandClaude Opus 5.5 d352731280 Prepare Manager 0.9.13 with reviewed notes in six languages
Manager 0.9.13 carries the Custom Races V2 client. Its reviewed note and the 213 notes of the upcoming
server record (Custom Races V2: subclasses, Two-Players, the new races; collected with the pipeline's own
rules from the frozen sources of candidate run 43641984449) have Russian, German, French, Spanish and
Simplified Chinese translations. Bundled server history refreshed through 0.261114.106 from
coa-server-build 3308e3a.

Checks: release-history tests 5/5, check-i18n, npm run build, cargo check --locked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 02:27:37 +05:00
ДмитрийandClaude Opus 5.5 a0545286ee Install the Custom Races V2 client
The client update now takes ilusixn/CustomRacesCoa coa-custom-2.0: nine client archives plus the server
archive's client addons (subclass interface, dev menu, teleports, spell activation overlay), 76 race
archives and 7220 verified files in all, among them CoACustomRace.exe and its Vulkan renderer.

- The game starts CoACustomRace.exe when it is there; Ascension.exe is left alone, and a disabled V2
  falls back to it.
- Updating from 1.9.2 retires its patched Ascension.exe and puts the player's own back (1.9.2 replaced
  it; V2 does not ship one). Earlier installs keep their own hash lists, 1.9.2's included.
- Four V2 archives are byte-identical to 1.9.2's: a verified 1.9.2 copy in the cache or in Downloads
  is used instead of downloading it again.

Verified: unit tests (800 passed); against the real archives, every bundled hash, a full install,
disable, enable and uninstall in a disposable client, and an update of a client that Manager 0.9.12
had put 1.9.2 on (its own Ascension.exe comes back, and again after uninstalling).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:59:43 +05:00
ДмитрийandClaude Opus 5.5 4acc07d6fd Keep a single copy of the bot strength settings in the Modules Preview (schema and translations)
The previous commit only removed the duplicate release note; this one removes the
Bots > Combat copy of the ten CoaBots.Scaling.* settings and their translations,
leaving the Preview's Bot Strength section exactly as in preview 0.9.11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 12:07:25 +05:00
ДмитрийandClaude Opus 5.5 993a7b8e63 Keep a single copy of the bot strength settings in the Modules Preview
Merging Manager 0.9.12 added its Bots > Combat copy of the ten CoaBots.Scaling.*
settings next to the Preview's own Bot Strength section, so the schema failed to
load (duplicate key). The Preview keeps its section, translations and release note,
as shipped since preview 0.9.10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:53:35 +05:00
ДмитрийandClaude Opus 5.5 6f8ed793a7 Merge integrate/release-0.9.12 (release 2026-11-14)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 09:56:41 +05:00
github-actions[bot] b3c88ce2a6 Update published Manager changelog 2026-11-14 04:41:00 +00:00
Corfirean 1d9817bd7a Merge remote-tracking branch 'origin/integrate/release-0.9.12' into preview/release-0.9.12 2026-11-14 00:15:31 +05:00
CorfireanandClaude Opus 5.5 d71c5635e3 Translate the auction house bot note of the upcoming server release
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 00:15:27 +05:00
CorfireanandClaude Opus 5.5 cbaca4f917 Merge Manager 0.9.12 into the Modules Preview (preview 0.9.12)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 00:09:36 +05:00
CorfireanandClaude Opus 5.5 755761d0de Prepare Manager 0.9.12 with reviewed notes in six languages
Bundled server history refreshed through 0.261108.95 from coa-server-build ca04f96.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 00:05:14 +05:00
Corfirean 550e475962 Merge remote-tracking branch 'origin/feat/bot-scaling-settings' into integrate/release-0.9.12 2026-11-13 23:54:41 +05:00
ДмитрийandClaude Opus 5.5 f126cb4ed0 preview 0.9.11: real Server Migration page, world settings carried by the modular copy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 09:28:00 +05:00
ДмитрийandClaude Opus 5.5 fdbf38ee4e Server Migration: real analysis and a real modular copy
The page was a mock: it never read the database (always 0 accounts),
showed a hard-coded schema and core version, looked for server files in
the wrong folder, and its "transfer" reported accounts, characters and
tables as moved without moving anything. Its locale was fixed to "ru".

- remove server_migration.rs and its three commands
- modular_copy::preview: classic/modular core, version, database state,
  counts and schemas from a running database (read only), database size,
  settings files, extra worlds; nothing is started or changed
- the page shows that and hands over to the same modular-copy install
  screen as "Manage another server"; texts in en and ru via i18n
- the modular copy now carries world settings changed in Server
  Configuration (Settings/worldserver.conf.template was not copied)
- E2E preview_bare_and_copy_e2e checks the preview and the carried
  setting; passes against preview core 0.261108.9501

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 09:22:22 +05:00
ДмитрийandClaude Opus 5.5 6091e545f8 Merge feat/rust-launcher: drop the removed bridge's CI test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:48:27 +05:00
ДмитрийandClaude Opus 5.5 2e6fcceb05 CI: drop the launcher bridge test (the bridge was removed)
tools/test_launcher_bridge.py tested crates/coa-core/src/launcher_bridge.py,
which the Rust launcher replaced; check.yml and release.yml still ran it, so
the release build failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:48:17 +05:00
ДмитрийandClaude Opus 5.5 5ccd8871d1 Merge feat/rust-launcher: resend a lost shutdown request
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:08:05 +05:00
ДмитрийandClaude Opus 5.5 abcc816581 Stop: ask the world again when a shutdown request is lost
A console shutdown request is sometimes lost, and the world then keeps
running until the stop times out. While the world still runs and its
console still answers after 15 s, the launcher asks again (twice at most);
asking twice is harmless. Verified on a disposable repack with the first
World::ShutdownServ call suppressed in a debugger: the second request
stopped the world.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:07:54 +05:00
ДмитрийandClaude Opus 5.5 1ee17f8415 changelog: fragments within the shared limits; native launcher entry
Five preview fragments exceeded the body (300) or title (90) limits, so the
changelog-fragments check failed on every push to this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:36:22 +05:00
ДмитрийandClaude Opus 5.5 71ffd568bb Merge feat/rust-launcher into preview/modules-v2
The Manager starts servers with coa-core::launcher (no Python bridge),
the bug-report relay runs as coa-launcher relay-worker, and manage.py is
no longer edited. Preview identity in tauri.conf.json is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:29:27 +05:00
ДмитрийandClaude Opus 5.5 8353b791fb The Manager no longer edits Scripts/manage.py
Repacks ship no .bat files, and the Manager starts servers with
coa-core::launcher, so the realm-profile patch it wrote into manage.py
served no one. It is no longer applied (realm creation, realm switching and
start).

Recognising the edits older Managers made (realm patch, bridge import fix)
moves into legacy_launcher, so updates and file checks still treat such a
file as unmodified. Nothing there writes a file.

manage.py itself stays complete in the repack: Managers up to 0.9.10 run it
through their bridge and patch its text, so it cannot become a thin wrapper
until those Managers are no longer supported.

Verified: coa-core unit tests; examples/manager_launcher_e2e on a disposable
repack (manage.py unchanged after a start with a realm profile).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:07:41 +05:00
ДмитрийandClaude Opus 5.5 b81227b0ea Bug-report relay in Rust (coa-launcher relay-worker)
launcher::relay does what BugReport/relay.py and manage.py relay-worker do,
with the same spool: the <key>.status files the world reads, the journal
relay.sqlite3 (deliveries, settings), relay.lock (byte 0) and the
relay-ready record. Service answers map to the same states and retry times;
a post interrupted mid-flight stays uncertain and is never resent.

When coa-launcher.exe supervises a world it runs the relay itself; the
Python supervisor still runs relay.py.

Verified:
- examples/relay_equivalence: the same reports and scripted service answers
  through relay.py and the Rust relay give identical statuses, journal rows
  (including retry times) and posts, and each continues the other's journal.
- examples/manager_launcher_e2e on a disposable repack: supervisor and relay
  are coa-launcher.exe, start/stop interoperates with manage.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 12:00:40 +05:00
ДмитрийandClaude Opus 5.5 a9fb982ab0 Console shutdown request reads the reply before closing
send_without_answer closed the socket right after writing "server shutdown".
With the reply still unread, Windows resets the connection, and the world
can drop the command: a world supervised by coa-launcher.exe ignored the
shutdown and the stop timed out after 90 seconds. The reply is now read up
to the next prompt, the end of the connection or 10 seconds.

The E2E also checks which supervisor runs the world and that manage.py
stops a world the Manager started.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:38:59 +05:00
ДмитрийandClaude Opus 5.5 a822bfc99b Manager starts and stops servers through the Rust launcher
driver::run now calls coa-core::launcher::ops::run in-process instead of
running Scripts/manage.py through launcher_bridge.py. Failures carry a typed
code (Error::Service) instead of being guessed from stderr text.

Moved into the launcher: realm profiles (realm id, name and databases from
Settings/realm-profile.json), extra worlds sharing the main database,
the shared world startup deadline and console readiness check, the owned-world
check on shutdown, force-stop-all, and an environment block without
AC_REALM_SERVER_PORT for update validation.

The world supervisor is Scripts/coa-launcher.exe when the repack ships it,
otherwise manage.py watch-world. Extra worlds get an unmodified manage.py.
The realm patch of manage.py stays for the repack's own .bat files.

Verified with examples/manager_launcher_e2e on a disposable repack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:45:08 +05:00
ДмитрийandClaude Opus 5.5 1211de5a77 launcher (Rust): services inherit only their log and NUL; MySQL errors carry the server's text
std::process::Command lets a Windows child inherit every inheritable handle of the parent: a
MySQL started by the launcher kept the caller's files and pipes open (found by the SQUID test,
whose log stayed locked). Services now start through CreateProcessW with
PROC_THREAD_ATTRIBUTE_HANDLE_LIST = [log, NUL], as Python's subprocess does. The MySQL client's
outputs are read while it runs (a full pipe stopped it) and its last error line is reported.

E2E on a disposable repack: launcher_e2e PASS again; squid_equivalence PASS: Rust and
squid_playerbots.py leave identical journals, acore_playerbots tables (row counts) and
playerbots.conf (4106 lines), and a second run of either changes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 07:48:04 +05:00
ДмитрийandClaude Opus 5.5 ee36692db7 launcher (Rust): services, world supervisor, SQUID provisioning, coa-launcher.exe
- services: exclusive-bind port check, readiness by listener row or the server side of a
  loopback connection, MySQL client, start MySQL/auth, stop-all (world through its console,
  database last), status.
- world: start-world through a supervisor process (coa-launcher watch-world) that keeps the
  bug-report relay (still BugReport/relay.py via the repack's Python) alive; detailed logging.
- squid: squid_playerbots.py in Rust with the same journals (coa_squid_migrations,
  coa_squid_pending, coa_bots_installed).
- coa-launcher.exe: the manage.py commands and error texts (the Manager reads them).
- Services no longer inherit the launcher's stdin/stdout/stderr: a server holding the
  output pipe kept whoever read the launcher's output waiting.

E2E (examples/launcher_e2e, disposable stable repack on free ports): coa-launcher start-all ->
manage.py sees and stops it; manage.py start-all -> coa-launcher sees and stops it; a second
Rust-only cycle leaves nothing running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 07:29:55 +05:00
ДмитрийandClaude Opus 5.5 ee43522019 launcher (Rust): settings, state records, control lock, prepare
First part of moving Scripts/manage.py into coa-core (plan §17, PY-1). Same formats on disk as
manage.py so both can run on one server: .state records, the byte-0 control lock msvcrt.locking
takes, configuration.json. prepare renders byte-for-byte what manage.py renders (checked against
the real script, COA_MANAGE_PY). State files are flushed to disk before they replace the old ones,
and a damaged record counts as no process instead of failing every later start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 06:58:16 +05:00
ДмитрийandClaude Opus 5.5 a0d3cc08ce examples: catalogue build sweep (build every catalogue module against a realm's SDK, record evidence, aggregate missing exports)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 06:43:37 +05:00
ДмитрийandClaude Opus 5.5 89e2abb642 preview: Bot Strength settings for CoA Companions (CoaBots.Scaling.*)
Ten settings in a new bot settings section: the switch, where it applies (dungeons, raids,
battlegrounds/arenas, open world), the damage, healing, damage-taken and tank-threat
multipliers (1 = unchanged, the module's defaults) and the per-boss tuning (advanced).
Translations in the five locales.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 06:43:25 +05:00
github-actions[bot] b4dc30b84a Update published Manager changelog 2026-11-08 10:21:07 +00:00
Дмитрий 73a6cca753 Integrate the qualified November release sources [skip ci] 2026-11-08 15:09:20 +05:00
Corfirean 705bee7d56 Merge verified Custom Races download recovery [skip ci]
Recover interrupted Custom Races patch downloads
2026-11-08 01:44:09 +05:00
Corfirean 5aa9b32283 Merge release cold-cache preparation fix [skip ci]
release: build the frontend before the tests
2026-11-08 01:44:05 +05:00
Дмитрий 711f378ae8 Bind translated release notes to the exact candidate sources [skip ci] 2026-11-08 01:23:56 +05:00
Corfirean c29d56e2b3 Register local installer preparation workflow [skip ci] 2026-11-08 01:21:16 +05:00
Дмитрий d63e85bb18 Prepare Manager 0.9.10 with reviewed notes and signing of local installers [skip ci] 2026-11-08 01:20:32 +05:00
Дмитрий c513649beb Merge remote-tracking branch 'origin/fix/recover-client-patch-downloads' into release/20261108-manager 2026-11-08 01:09:43 +05:00
ДмитрийandClaude Sonnet 5.5 d2e6fef188 feat(bots): settings for bot strength scaling in dungeons and raids
Adds the CoaBots.Scaling.* keys (Enable, Dungeons, Raids, Battlegrounds,
World, DamageDone, HealingDone, DamageTaken, TankThreat, BossTuning) to the
bots schema under Combat, with defaults that change nothing (all multipliers 1,
boss tuning off), translations in five languages and a changelog fragment.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 16:22:47 +05:00
ДмитрийandClaude Opus 5.5 db23c9b098 preview catalogue: fixed column widths; last update of CoA's own modules
Rows were separate grids with an auto-width action column, so the columns moved from row
to row; every column has a fixed width now. CoA's modules are not in the public catalogue:
their last push comes from the GitHub API (cached 12 h).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 15:18:57 +05:00
ДмитрийandClaude Opus 5.5 f62729d281 preview catalogue: last update of each module (last commit, dimmed after two years)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 14:44:41 +05:00
ДмитрийandClaude Opus 5.5 e7744dfc7c preview modules: SQL file list, install with SQL waits for confirmation, link panel closes after install
- The SQL review listed '( statements)': the page read file_path and statement_count, the
  backend sends file, target_schema, line_count and what each file does.
- After a build from a link, a module with SQL files was installed without them (its NPCs
  missing) and the panel kept offering a second install; now the SQL confirmation opens
  instead, and the link panel closes once the module is installed.
- The catalogue says when a filter hides modules, with Show all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 13:23:13 +05:00
ДмитрийandClaude Opus 5.5 899b7c6617 preview: compressed module build cache with a clear dialog; module sources without client files
- Build cache (build_cache.rs): the build folder of a module is unpacked for a
  build and packed into <module>.tar.zst afterwards (object files ~4-6x
  smaller), modification times kept so Ninja recompiles only what changed.
  A cache made for another core is dropped instead of unpacked; .ilk and .map
  files are not kept; total limit 2 GB, least recently built first. The build
  log moves next to the archive (build-cache/<module>.log).
- "Build cache" in the Modules menu: size per module (packed, built when, for
  this core or not), remove one, remove all, remove downloaded sources.
  Builds now hold the server's busy guard, so the cache is never cleared
  under a running build.
- Module sources: "Add from a link" downloads GitHub's archive of the commit
  instead of a full git clone, and game-client files (.mpq, .dbc, maps,
  textures, archives) are left out and listed in .coa_skipped_assets.
  mod-bg-battle-for-gilneas: 365 MB repository, 3.1 MB kept (was refused at
  the 300 MB limit).
- Overlay: Helias's battleground modules (Gilneas, Twin Peaks, Slavery Valley)
  are Incompatible: they need the server's DBC files replaced (CoA ships its
  own) and a client patch.

Checked: build_cache unit tests; examples/build_cache_ninja_check with real
MSVC + CMake + Ninja: after pack/unpack nothing is recompiled, after one edit
one file is; coa-core lib tests 865 passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:29:26 +05:00
ДмитрийandClaude Opus 5.5 3987f1b2ff preview: simpler Modules screen, catalogue as a compact list with categories
- Header is just "Modules" with "Add from a link" (the GitHub URL bar is folded
  away until asked for) and a menu for builds/logs, build tools and the
  diagnostics export. The build-tools bar shows only when something is missing.
- Two tabs (Installed, Catalog) styled like the settings page.
- Catalogue: one list of rows (name, owner, category, stars, one action); CoA's
  own modules first. Compatibility and sort are drop-downs; 20 functional
  categories as chips with counts. "Checked by CoA" is the curated list only.
- Categories: owner's reviewed list (181 modules, high/medium confidence) plus
  keyword rules for every other module (coa-module-categories.json).
- Catalogue statuses come from the current overlay on every read (a cached copy
  kept the old Verified marks), and no more made-up entries for overlay rules
  the public catalogue does not list (fake AGPL-3.0, 0 stars, duplicate mod-cfbg).
- Window size, position and maximized state persist (tauri-plugin-window-state,
  visibility left to the app).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 10:05:58 +05:00
ДмитрийandClaude Opus 5.5 b647e62ca6 preview catalogue: notes are warnings only on incompatible modules; drop unproven Verified marks
The overlay reason was drawn as a red blocked notice on every card, so modules
marked Verified looked incompatible. mod-cfbg is built into the CoA core and is
now Incompatible; the Verified marks on duel-reset, pvp-titles, starter-guild and
skip-dk-starting-area had no build or test behind them and are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:01:18 +05:00
ДмитрийandClaude Opus 5.5 d990efcfad preview: one segmented catalogue filter with live counts
Six overlapping, differently coloured chips become four groups that do not
overlap (all, checked by CoA, community, incompatible), each with a count that
follows the search; the sort list gets a label and no emoji.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 08:36:29 +05:00
ДмитрийandClaude Opus 5.5 9f48cfc8d5 preview 0.9.10: bare modular core for new servers, modular copy of classic servers
- A new server goes on to the preview core without compiling any module
  (recommended modules are declined once; modules come from the Modules page).
- Choosing a server with the classic core makes a modular copy next to it:
  every acore_* database (read from the running database, or from a private
  copy of the stopped data files opened by the new server's own mysqld),
  module configs and realm profiles, ports and console login. The old server
  is only read. The copy then takes the preview core with the modules the old
  server had switched on.
- Installs and the preview update run on unused ports, so a server running on
  the shipped ports (the old one, or any other) no longer stops them; the
  schema check after "Preparing database" used the shipped port before.
- Dungeon Clear is part of SQUID's Playerbots: no card of its own (it said
  "Always on" while SQUID was off), switched with SQUID, settings on the SQUID card.

E2E (examples/preview_bare_and_copy_e2e, disposable servers, another server
running on the shipped ports): fresh install committed with no plugins and a
clean boot report; copy of a stopped and of a running classic server: data
identical, old server's files byte-for-byte unchanged, Content Scaling and
Companions built on the copy, preview core committed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 23:49:03 +05:00
ДмитрийandClaude Opus 5.5 3866e219ce preview: a new server goes straight on to the modular core
The install used the stable image and left the preview core for a later
update, so a fresh Modules Preview server showed every module as built in
and no Plugin SDK. install_new now applies the preview update right after
the base install (same signed update and module build every server takes);
a failure leaves the classic core and the done screen says so, and modules
that did not build are listed. A hand-picked package and Docker keep the
old behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 22:10:03 +05:00
ДмитрийandClaude Opus 5.5 2224afd2ea release: build the frontend before the tests
tauri::generate_context! needs ../dist. The tests run before tauri-action builds the frontend, so a
run without a warm target cache cannot compile the app (seen on preview/modules-v2, run 42224568581).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:47:11 +05:00
ДмитрийandClaude Opus 5.5 6723a7eda1 preview: remove developer test-realm shortcuts before the first public preview
The Modules page had a 'Register test-realm-v03' button and the app registered a hardcoded local
folder into an empty server list at startup. Both pointed at a developer's machine. The startup
registration now happens only when COA_TEST_REALM_DIR is set. test_detect_toolchain skips when the
local spike realm is absent (CI runners).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:48:00 +05:00
ДмитрийandClaude Opus 5.5 b65b632c63 release: build the frontend before the tests (cold cache cannot compile generate_context!)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:36:11 +05:00
Дмитрий 1a1976d9ad Recover interrupted Custom Races patch downloads 2026-10-31 07:55:15 +05:00
170 changed files with 47976 additions and 6830 deletions

No files matched your search

-1
View File
@@ -26,5 +26,4 @@ jobs:
- run: node tools/check-i18n.mjs - run: node tools/check-i18n.mjs
- run: npm run build - run: npm run build
- run: node --experimental-strip-types --test tools/*.test.mjs - run: node --experimental-strip-types --test tools/*.test.mjs
- run: python -B -m unittest discover -s tools -p test_launcher_bridge.py
- run: cargo test --workspace --locked - run: cargo test --workspace --locked
+2 -1
View File
@@ -69,10 +69,11 @@ jobs:
with: with:
node-version: 22 node-version: 22
- run: npm ci - run: npm ci
# tauri::generate_context! needs ../dist; without a warm target cache the tests do not compile without it.
- run: npm run build
- name: Tests - name: Tests
run: cargo test --workspace --locked -- --test-threads=1 run: cargo test --workspace --locked -- --test-threads=1
- run: node --experimental-strip-types --test tools/*.test.mjs - run: node --experimental-strip-types --test tools/*.test.mjs
- run: python -B -m unittest discover -s tools -p test_launcher_bridge.py
- name: Build and sign (publish only when requested) - name: Build and sign (publish only when requested)
id: tauri id: tauri
uses: tauri-apps/tauri-action@v0 uses: tauri-apps/tauri-action@v0
@@ -0,0 +1,72 @@
name: sign-local-installer
on:
push:
branches: [release/20261108-manager]
workflow_dispatch:
inputs:
build_run_id:
description: Successful manager-local run in coa-server-build
required: true
permissions:
contents: read
actions: read
jobs:
sign:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v4
with:
node-version: 22
- name: Verify local build provenance
env:
GH_TOKEN: ${{ github.token }}
BUILD_RUN: ${{ inputs.build_run_id }}
run: |
python3 - <<'PY'
import json, os, subprocess
run = os.environ['BUILD_RUN']
if not run.isdigit():
raise RuntimeError('Invalid build run')
data = json.loads(subprocess.check_output(['gh', 'api', f'repos/Corfirean/coa-server-build/actions/runs/{run}']))
if data['conclusion'] != 'success' or data['path'] != '.github/workflows/manager-local.yml':
raise RuntimeError('Not a successful local Manager build')
subprocess.run(['gh', 'run', 'download', run, '-R', 'Corfirean/coa-server-build', '-n', 'unsigned-manager-windows', '-D', '_prepared-manager'], check=True)
from pathlib import Path
import hashlib
root = Path('_prepared-manager')
meta = json.loads((root / 'prepared-source.json').read_text())
version = json.loads(Path('package.json').read_text())['version']
name = meta['installer']
if Path(name).name != name or meta['source'] != os.environ['GITHUB_SHA'] or meta['version'] != version or str(meta['run']) != run:
raise RuntimeError('Installer identity differs')
if hashlib.sha256((root / name).read_bytes()).hexdigest() != meta['sha256']:
raise RuntimeError('Installer hash differs')
PY
- run: npm ci
- name: Sign exact locally compiled bytes
env:
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
run: |
node --input-type=module <<'JS'
import { readFileSync, writeFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
const root = '_prepared-manager/';
const meta = JSON.parse(readFileSync(root + 'prepared-source.json'));
execFileSync('node_modules/.bin/tauri', ['signer', 'sign', '--app-version', meta.version, root + meta.installer], {stdio: 'inherit'});
if (createHash('sha256').update(readFileSync(root + meta.installer)).digest('hex') !== meta.sha256) throw new Error('Signed bytes changed');
const signature = readFileSync(root + meta.installer + '.sig', 'utf8').trim();
const url = `https://github.com/Corfirean/coa-server-manager/releases/download/v${meta.version}/${encodeURIComponent(meta.installer)}`;
writeFileSync(root + 'latest.json', JSON.stringify({version: meta.version, notes: 'See the release notes.', pub_date: new Date().toISOString(), platforms: {'windows-x86_64': {signature, url}}}, null, 2) + '\n');
meta.signingRun = process.env.GITHUB_RUN_ID;
writeFileSync(root + 'prepared-source.json', JSON.stringify(meta, null, 2) + '\n');
JS
- uses: actions/upload-artifact@v4
with:
name: prepared-manager-windows
path: _prepared-manager/*
retention-days: 14
if-no-files-found: error
+57
View File
@@ -8,6 +8,63 @@ Published releases and previews. Pre-release entries do not change the stable up
Dates follow GitHub publication (UTC); legacy records retain their archived dates. Newest releases first. Dates follow GitHub publication (UTC); legacy records retain their archived dates. Newest releases first.
## 0.9.17 — 2026-11-23
- The game client link points to the new community site, conquestofazerothcore.to.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.17) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.16...v0.9.17)
## 0.9.16 — 2026-11-20
- The game client now comes from the community launcher, not from the Manager.
- Client setup says where to switch HD graphics; Custom Races patch card is translated.
- Clean server logs by hand or automatically when they grow too big.
- The startup limit for companions now creates the ones that are missing.
- Restart the world every week on a chosen day and time.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.16) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.15...v0.9.16)
## 0.9.15 — 2026-11-19
- The Custom Races client update brings version 2.0.2.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.15) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.14...v0.9.15)
## 0.9.14 — 2026-11-16
- Install community addons from the Manager.
- Old item and creature data is cleared before every Play.
- Turning the custom races client patch on or off takes seconds again.
- The database moves to a free port when Windows reserves or another program takes 3307.
- Your own MySQL settings survive restarts and updates.
- Disabled custom races stay disabled after a server update that adds races.
- Rename your realms in the Manager.
- The account for a friend's server is in the login box at every Play.
- Restart the world every day at a chosen time.
- Switches for the Custom Races V2 subclasses, Hero Races and the /dev menu.
- Companion bots on Wildcard and Warcraft Reborn.
- The Custom Races card tells which characters stay out while races are off.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.14) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.13...v0.9.14)
## 0.9.13 — 2026-11-15
- The custom races client is updated to Custom Races V2.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.13) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.12...v0.9.13)
## 0.9.12 — 2026-11-14
- Settings for how much help bots get in dungeons and raids.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.12) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.10...v0.9.12)
## 0.9.10 — 2026-11-08
- Custom Races patch installation recovers from interrupted downloads.
[Release](https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.10) · [Source changes](https://github.com/Corfirean/coa-server-manager/compare/v0.9.9...v0.9.10)
## 0.9.9 — 2026-11-04 ## 0.9.9 — 2026-11-04
- Update and recovery checks use the temporary login port even with a Windows override. - Update and recovery checks use the temporary login port even with a Windows override.
Generated
+29 -3
View File
@@ -586,7 +586,7 @@ dependencies = [
[[package]] [[package]]
name = "coa-core" name = "coa-core"
version = "0.9.9" version = "0.9.18"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"chrono", "chrono",
@@ -606,6 +606,7 @@ dependencies = [
"serde_json", "serde_json",
"sha1", "sha1",
"sha2", "sha2",
"socket2",
"tar", "tar",
"tempfile", "tempfile",
"thiserror 2.0.21", "thiserror 2.0.21",
@@ -618,6 +619,15 @@ dependencies = [
"zstd", "zstd",
] ]
[[package]]
name = "coa-launcher"
version = "0.9.18"
dependencies = [
"coa-core",
"serde_json",
"windows-sys 0.60.2",
]
[[package]] [[package]]
name = "coa-registry-proto" name = "coa-registry-proto"
version = "0.1.0" version = "0.1.0"
@@ -634,7 +644,7 @@ dependencies = [
[[package]] [[package]]
name = "coa-release" name = "coa-release"
version = "0.9.9" version = "0.9.18"
dependencies = [ dependencies = [
"chrono", "chrono",
"coa-core", "coa-core",
@@ -772,7 +782,7 @@ dependencies = [
[[package]] [[package]]
name = "csm-modules-preview" name = "csm-modules-preview"
version = "0.9.9" version = "0.9.18"
dependencies = [ dependencies = [
"coa-control-proto", "coa-control-proto",
"coa-core", "coa-core",
@@ -786,6 +796,7 @@ dependencies = [
"tauri-plugin-process", "tauri-plugin-process",
"tauri-plugin-single-instance", "tauri-plugin-single-instance",
"tauri-plugin-updater", "tauri-plugin-updater",
"tauri-plugin-window-state",
"tracing", "tracing",
"uuid", "uuid",
] ]
@@ -4657,6 +4668,21 @@ dependencies = [
"zip 4.6.1", "zip 4.6.1",
] ]
[[package]]
name = "tauri-plugin-window-state"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d6815cab7b212f9cfe16a9d79d8d22f9e14159e4a9460f7dec1467167a008e3"
dependencies = [
"bitflags 2.13.2",
"log",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"thiserror 2.0.21",
]
[[package]] [[package]]
name = "tauri-runtime" name = "tauri-runtime"
version = "2.12.0" version = "2.12.0"
+2 -2
View File
@@ -1,10 +1,10 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["crates/coa-core", "crates/coa-release", "crates/coa-registry-proto", "crates/coa-control-proto", "src-tauri"] members = ["crates/coa-core", "crates/coa-launcher", "crates/coa-release", "crates/coa-registry-proto", "crates/coa-control-proto", "src-tauri"]
exclude = ["services/coa-registry", "services/coa-registry-e2e", "services/coa-coordinator", "services/coa-relay"] exclude = ["services/coa-registry", "services/coa-registry-e2e", "services/coa-coordinator", "services/coa-relay"]
[workspace.package] [workspace.package]
version = "0.9.9" version = "0.9.18"
edition = "2021" edition = "2021"
license = "AGPL-3.0-only" license = "AGPL-3.0-only"
+7247
View File
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Custom Races patch installation recovers from interrupted downloads
---
Unfinished downloads resume automatically. Invalid archives in Downloads are skipped without changing them, and damaged cached archives are downloaded again.
@@ -4,4 +4,4 @@ type: added
audience: admins audience: admins
title: Install the compiler for modules from the Manager title: Install the compiler for modules from the Manager
--- ---
When the C++ compiler that modules need is missing or differs from the one the core was built with, the Toolchain tab offers to install Visual Studio Build Tools with MSVC 14.44. The Manager downloads Microsoft's installer, checks that Microsoft signed it and opens it with the needed components selected; you accept Microsoft's terms there. When the compiler modules need is missing or does not match the core, the Toolchain tab offers to install Visual Studio Build Tools with MSVC 14.44. The Manager checks Microsoft's signature on the installer and opens it with the needed parts selected.
@@ -4,4 +4,4 @@ type: added
audience: admins audience: admins
title: Modules are rebuilt automatically after a core update title: Modules are rebuilt automatically after a core update
--- ---
After the core is updated, the Manager rebuilds every enabled module for the new core before the first start, or reuses a build it already has. A module that cannot be rebuilt is switched off with the reason shown, and the core update still goes ahead. Rolling back the core puts the earlier module builds back. After a core update, every enabled module is rebuilt for the new core before the first start, or an existing build is reused. A module that cannot be rebuilt is switched off with the reason shown. Rolling back the core restores the earlier builds.
@@ -4,4 +4,4 @@ type: added
audience: admins audience: admins
title: Moving to a core with modules installs the modules you used title: Moving to a core with modules installs the modules you used
--- ---
When a server moves to a core that loads Content Scaling, Companions or SQUID's Playerbots as modules, the Manager downloads them at the versions the core names, builds them and switches them on before the first start. Bots follow what the server used before: SQUID's Playerbots if it was switched on, otherwise Companions. A module you remove later is not installed again. On a core that loads Content Scaling, Companions or SQUID's Playerbots as modules, the Manager installs them at the versions the core names before the first start. Bots follow what the server used: SQUID if it was on, else Companions. Removed modules stay removed.
@@ -0,0 +1,8 @@
---
area: manager
type: changed
audience: admins
title: Dungeon Clear is shown and switched as part of SQUID's Playerbots
---
Dungeon Clear no longer has a card of its own, which said "Always on" while SQUID was off. It goes on
and off together with SQUID's Playerbots, and its settings open from the SQUID card.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: admins
title: Modules Preview installs a bare modular core and copies classic servers
---
A new server gets the modular core and compiles no module; add modules later on the Modules page. An existing classic server is not converted: a modular server is installed next to it with its accounts, characters, world, bots and module settings copied. The old server keeps working.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Bot strength settings for CoA Companions
---
A Bot Strength section in the bot settings: turn bot help on or off, choose where it applies (dungeons, raids, PvP, open world) and set bot damage, healing, damage taken and tank threat. All multipliers start at 1, so nothing changes until you raise them.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: admins
title: The Manager starts and stops servers without Python
---
Start, stop and forced stop run inside the Manager, and errors name their cause (a busy port, missing files, a startup failure). Servers that include Scripts/coa-launcher.exe also run the world supervisor and bug-report relay without Python.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Server Migration shows the real server and makes a real modular copy
---
The page read nothing and reported a transfer that did not happen. It now shows the server's version, database state and counts, then makes the same modular copy as "Manage another server". World settings from Server Configuration now go along with the copy.
+7
View File
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: players
title: Install community addons from the Manager
---
The game client card lists SquidBots Lite, DragonUI, IncahootsCoA and CoACombat. Each comes from its author's GitHub at a pinned version, every file is checked before it goes into Interface/AddOns, and the Manager updates or removes only what it installed.
+7
View File
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: players
title: Old item and creature data is cleared before every Play
---
After a server update the game showed old names and stats from its own cache (Cache/WDB). Play now clears that cache first; it fills again within seconds. A switch next to the client graphics settings turns it off.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: players
title: Turning the custom races client patch on or off takes seconds again
---
V2 has over 7,000 files and switching it could run for many minutes. Now only the files the game itself loads are hidden, and the rest stays in place. A switch that was cut short is undone safely.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: players
title: The custom races client is updated to Custom Races V2
---
The client update installs V2 and starts its own CoACustomRace.exe; Ascension.exe is left as it was. Four archives are the same as in 1.9.2, so a game folder that already had 1.9.2 downloads only the rest.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: The database moves to a free port when Windows reserves or another program takes 3307
---
Hyper-V, WSL and Docker can reserve a range that includes the database port; the start then waited 30 seconds and failed. The database now picks a free port and records it, and a reserved game port is named as such at once.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Your own MySQL settings survive restarts and updates
---
Settings, Service has a MySQL settings box. Its options are added to my.ini at every start of the database; before, my.ini was rewritten and hand edits were lost. Port, addresses and data paths stay managed.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Disabled custom races stay disabled after a server update that adds races
---
A race list saved before Custom Races V2 no longer matched V2's race catalog, and the server then ignored it and turned custom races off. Before a start the Manager now keeps the list when every disabled race kept its ID and name.
+7
View File
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Rename your realms in the Manager
---
Settings, Startup has a name for every realm. A running server shows it at once, the game client picks it on the next Play, and it stays after restarts and updates. Before, the launcher wrote the default name back at every start.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: players
title: The account for a friend's server is in the login box at every Play
---
The account the Manager made on a listed server was filled in only when joining. Play now fills it in every time, also after another server's account was used.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Restart the world every day at a chosen time
---
Settings, Startup, Scheduled restart: at that local time the world restarts with an in-game countdown and comes back by itself; the database keeps running. It works without the Manager open and needs a server with the new launcher.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Switches for the Custom Races V2 subclasses, Hero Races and the /dev menu
---
The Custom Races settings now hold Subclasses and Hero Races (subclasses, the Thin Twins, the Two-Headed Ogre, two players per character) and the GM /dev menu, each with its own switch.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Companion bots on Wildcard and Warcraft Reborn
---
With a server build that supports it, Companions can be switched on for Wildcard and Warcraft Reborn: bots get whole Wildcard archetypes or Reborn abilities by level, and bot creation survives a crash without duplicates.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: The Custom Races card tells which characters stay out while races are off
---
With custom races off, the card shows how many player characters and bots of custom races cannot enter the world, and offers to delete the companion bots of those races.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: players
title: The game client now comes from the community launcher, not from the Manager
---
The client download moved behind the community launcher's sign-in, so the Manager's built-in download and update are removed. Get the launcher at coa-development.org, then choose your game folder in the Manager.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Installing a new server works again in the Modules Preview
---
The newest server release needs a newer Manager than the previous preview, so a fresh install stopped with "This package needs a newer version". This preview meets that requirement.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: players
title: The Custom Races client update brings version 2.0.2
---
Updating the Custom Races client installs 2.0.2, including the retail body models, subclass and HD patches and the Two-Players bar. About 4.4 GB of new files are fetched. Update the Manager before the server.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: players
title: Client setup says where to switch HD graphics; Custom Races patch card is translated
---
HD or standard graphics is a launcher setting; the client setup dialog now says so. The Custom Races client patch status and buttons are now translated into every language the Manager offers.
@@ -0,0 +1,7 @@
---
area: modules
type: added
audience: admins
title: Configure Playerbot Characters from the Modules page
---
Set up an OpenAI, Ollama, Anthropic or OpenAI-compatible connection from Modules, including its API key. Playerbot Characters gives CoA Companions or SQUID Playerbots dialogue and memories.
@@ -0,0 +1,7 @@
---
area: modules
type: added
audience: admins
title: Choose Playerbot Characters models from a live provider list
---
Search the models available from your configured provider and refresh the list when models change. Custom endpoints can still use a model ID entered manually.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Installing the C++ Build Tools now gets the exact compiler the core needs
---
Plugins must be built by the same compiler build as the core. On a PC whose Visual Studio Build Tools were installed earlier, the installer kept the older compiler and the modules stayed switched off. When the existing Build Tools cannot supply the compiler this core names, the Manager now installs Microsoft's current Build Tools next to them and leaves the existing installation untouched.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Clean server logs by hand or automatically when they grow too big
---
Server logging settings show how much the logs take and clean them with one button. Optionally the Manager cleans them above a size you choose; logs the running server uses are kept.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: players
title: The startup limit for companions now creates the ones that are missing
---
With 50 companions and a startup limit of 300, the Manager now creates the missing 250 instead of logging in only 50. Companions that exist are never created again. Up to 2000 are created at a time.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Restart the world every week on a chosen day and time
---
Next to the daily restart, pick a weekday, a time and the in-game countdown in the scheduled restart settings. Existing daily schedules keep working.
+7
View File
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: players
title: The game client link points to the new community site, conquestofazerothcore.to
---
The old community website no longer works, so the client setup dialog now opens conquestofazerothcore.to, where you download the launcher and sign in.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Deploying companions shows why the server refused, instead of a bar stuck at 0
---
When the bot module refused a Deploy request, the page kept showing "Creating bots 0 / N" forever. It now shows the module's own reply, and the message for an outdated bot module points to the Modules page.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: admins
title: Update an installed module to the build your server recommends, when you choose
---
The Modules page now says when your server recommends a newer build of an installed module and offers an Update button. Nothing updates on its own, not even with a new server core; the server must be stopped, and the module's settings are kept.
@@ -0,0 +1,7 @@
---
area: manager
type: fixed
audience: admins
title: Settings now shows the server version and update button after you have checked for updates
---
The Server card in Settings > Updates could stay empty, with only "…" and no update button, while the main screen already offered an update. It now shows the same version and update as the main screen.
@@ -0,0 +1,7 @@
---
area: manager
type: changed
audience: admins
title: Adding companions reuses offline bots and keeps your startup limit
---
Bots that already exist and are offline are logged in first and only the missing ones are created. Adding companions no longer raises the number of bots that log in when the server starts.
@@ -0,0 +1,7 @@
---
area: manager
type: added
audience: players
title: Custom Races installs the 2.0.3 client with the Snagtooth race
---
Updating Custom Races adds the new Alliance race and its fixes. Only the changed archives are downloaded again.
+115 -1
View File
@@ -172,10 +172,32 @@
"Corfirean/coa-server-manager:20261030-window-lifecycle.md", "Corfirean/coa-server-manager:20261030-window-lifecycle.md",
"Corfirean/coa-server-manager:20261031-dungeon-clear-dependency.md", "Corfirean/coa-server-manager:20261031-dungeon-clear-dependency.md",
"Corfirean/coa-server-manager:20261031-launcher-diagnostics-export.md", "Corfirean/coa-server-manager:20261031-launcher-diagnostics-export.md",
"Corfirean/coa-server-manager:20261031-recover-client-patch-downloads.md",
"Corfirean/coa-server-manager:20261031-server-logging-controls.md", "Corfirean/coa-server-manager:20261031-server-logging-controls.md",
"Corfirean/coa-server-manager:20261031-validation-bot-population.md", "Corfirean/coa-server-manager:20261031-validation-bot-population.md",
"Corfirean/coa-server-manager:20261031-validation-mysql-reuse.md", "Corfirean/coa-server-manager:20261031-validation-mysql-reuse.md",
"Corfirean/coa-server-manager:20261103-isolated-login-port.md" "Corfirean/coa-server-manager:20261103-isolated-login-port.md",
"Corfirean/coa-server-manager:20261107-bot-strength-scaling-settings.md",
"Corfirean/coa-server-manager:20261115-addon-catalog.md",
"Corfirean/coa-server-manager:20261115-clear-game-cache.md",
"Corfirean/coa-server-manager:20261115-custom-races-fast-toggle.md",
"Corfirean/coa-server-manager:20261115-custom-races-v2-client.md",
"Corfirean/coa-server-manager:20261115-database-port-relocation.md",
"Corfirean/coa-server-manager:20261115-mysql-custom-options.md",
"Corfirean/coa-server-manager:20261115-race-restriction-kept.md",
"Corfirean/coa-server-manager:20261115-realm-names.md",
"Corfirean/coa-server-manager:20261115-remembered-friend-login.md",
"Corfirean/coa-server-manager:20261115-scheduled-restart.md",
"Corfirean/coa-server-manager:20261115-v2-feature-switches.md",
"Corfirean/coa-server-manager:20261116-companions-wildcard-reborn.md",
"Corfirean/coa-server-manager:20261116-custom-race-characters-off.md",
"Corfirean/coa-server-manager:20261117-client-download-needs-launcher.md",
"Corfirean/coa-server-manager:20261118-custom-races-2-0-2-client.md",
"Corfirean/coa-server-manager:20261120-hd-graphics-hint-and-patch-labels.md",
"Corfirean/coa-server-manager:20261120-server-log-auto-cleanup.md",
"Corfirean/coa-server-manager:20261120-startup-fleet-creates-missing-companions.md",
"Corfirean/coa-server-manager:20261120-weekly-world-restart.md",
"Corfirean/coa-server-manager:20261123-client-site-link.md"
], ],
"releases": [ "releases": [
{ {
@@ -900,6 +922,98 @@
], ],
"commit": "173248efbe24da9b4c1b5ca12fa7580fcd1e8ea7", "commit": "173248efbe24da9b4c1b5ca12fa7580fcd1e8ea7",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.8...v0.9.9" "compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.8...v0.9.9"
},
{
"tag": "v0.9.10",
"version": "0.9.10",
"date": "2026-11-08",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.10",
"notes": [
"Custom Races patch installation recovers from interrupted downloads"
],
"commit": "711f378ae82f75d3fc64368c042889c773315c54",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.9...v0.9.10"
},
{
"tag": "v0.9.12",
"version": "0.9.12",
"date": "2026-11-14",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.12",
"notes": [
"Settings for how much help bots get in dungeons and raids"
],
"commit": "d71c5635e3262a775b400a1c90ac81d7bddcbeaf",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.10...v0.9.12"
},
{
"tag": "v0.9.13",
"version": "0.9.13",
"date": "2026-11-15",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.13",
"notes": [
"The custom races client is updated to Custom Races V2"
],
"commit": "d3527312803fb5a3e1cd48e01496645a8bbc9dcf",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.12...v0.9.13"
},
{
"tag": "v0.9.14",
"version": "0.9.14",
"date": "2026-11-16",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.14",
"notes": [
"Install community addons from the Manager",
"Old item and creature data is cleared before every Play",
"Turning the custom races client patch on or off takes seconds again",
"The database moves to a free port when Windows reserves or another program takes 3307",
"Your own MySQL settings survive restarts and updates",
"Disabled custom races stay disabled after a server update that adds races",
"Rename your realms in the Manager",
"The account for a friend's server is in the login box at every Play",
"Restart the world every day at a chosen time",
"Switches for the Custom Races V2 subclasses, Hero Races and the /dev menu",
"Companion bots on Wildcard and Warcraft Reborn",
"The Custom Races card tells which characters stay out while races are off"
],
"commit": "1f64da3ae064b3f02d2bb11d818c44d26a557045",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.13...v0.9.14"
},
{
"tag": "v0.9.15",
"version": "0.9.15",
"date": "2026-11-19",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.15",
"notes": [
"The Custom Races client update brings version 2.0.2"
],
"commit": "2b9021ce072e3b6c7e9c8106f8a54f59f1b37424",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.14...v0.9.15"
},
{
"tag": "v0.9.16",
"version": "0.9.16",
"date": "2026-11-20",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.16",
"notes": [
"The game client now comes from the community launcher, not from the Manager",
"Client setup says where to switch HD graphics; Custom Races patch card is translated",
"Clean server logs by hand or automatically when they grow too big",
"The startup limit for companions now creates the ones that are missing",
"Restart the world every week on a chosen day and time"
],
"commit": "316ef078e8047c790cb081f0991d9e0d618fcd2d",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.15...v0.9.16"
},
{
"tag": "v0.9.17",
"version": "0.9.17",
"date": "2026-11-23",
"url": "https://github.com/Corfirean/coa-server-manager/releases/tag/v0.9.17",
"notes": [
"The game client link points to the new community site, conquestofazerothcore.to"
],
"commit": "b5abc6fc4f6ab348efd57521136a2153ec948ca0",
"compare": "https://github.com/Corfirean/coa-server-manager/compare/v0.9.16...v0.9.17"
} }
] ]
} }
+2 -1
View File
@@ -5,6 +5,7 @@ edition.workspace = true
license.workspace = true license.workspace = true
[dependencies] [dependencies]
socket2 = "0.6"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
sha2 = "0.10" sha2 = "0.10"
@@ -37,7 +38,7 @@ rand_core = { version = "0.6", features = ["getrandom"] }
ed25519-dalek = { version = "2", features = ["rand_core"] } ed25519-dalek = { version = "2", features = ["rand_core"] }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.60", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_Threading", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock", "Win32_Security_Cryptography", "Win32_UI_Shell", "Win32_System_Registry", "Win32_UI_WindowsAndMessaging"] } windows-sys = { version = "0.60", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_Threading", "Win32_System_Console", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_NetworkManagement_IpHelper", "Win32_Networking_WinSock", "Win32_Security_Cryptography", "Win32_UI_Shell", "Win32_System_Registry", "Win32_UI_WindowsAndMessaging"] }
[features] [features]
testkit = [] testkit = []
+7
View File
@@ -0,0 +1,7 @@
//! Analyze a module by its GitHub link, as "Add from a link" does. usage: analyze_url <url> <cache dir>
fn main() {
let a: Vec<String> = std::env::args().skip(1).collect();
let r = coa_core::analyzer::analyze_github_repository(&a[0], Some(std::path::Path::new(&a[1]))).expect("analysis");
println!("{} @ {} cache_hit={:?}", r.repository_name, r.commit, r.cache_hit);
println!("classification {:?}; hooks {}; sql {}; cmake {}", r.classification, r.hooks.len(), r.sql_files.len(), r.has_cmakelists);
}
@@ -0,0 +1,58 @@
//! Real MSVC + CMake + Ninja: after the build cache is packed and unpacked, a rebuild with nothing changed
//! compiles nothing, and a rebuild after one source changed compiles only that source.
//! usage: build_cache_ninja_check <empty folder>
use std::path::Path;
use std::process::Command;
fn build(root: &Path, dir: &Path, tc: &coa_core::build_engine::ToolchainStatus) -> String {
let bat = root.join("run.bat");
let ninja = Path::new(tc.ninja_path.as_ref().unwrap()).parent().unwrap().display().to_string();
std::fs::write(&bat, format!(
"@echo off\r\ncall \"{}\" > nul 2>&1\r\nset \"PATH={};%PATH%\"\r\n\"{}\" -G Ninja -S \"{}\" -B \"{}\\build\" -DCMAKE_BUILD_TYPE=RelWithDebInfo > nul\r\n\"{}\" --build \"{}\\build\" -- -d explain\r\n",
tc.vcvars64_path.as_ref().unwrap(), ninja, tc.cmake_path.as_ref().unwrap(), dir.display(), dir.display(), tc.cmake_path.as_ref().unwrap(), dir.display()
)).unwrap();
let out = Command::new("cmd.exe").args(["/c", &bat.to_string_lossy()]).output().unwrap();
assert!(out.status.success(), "{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).into_owned() + &String::from_utf8_lossy(&out.stderr)
}
fn compiled(log: &str) -> usize {
log.lines().filter(|l| l.contains("Building CXX object")).count()
}
fn main() {
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("usage: build_cache_ninja_check <empty folder>"));
assert!(!root.exists() || std::fs::read_dir(&root).unwrap().next().is_none(), "folder must be new or empty");
std::fs::create_dir_all(root.join(".manager")).unwrap();
let tc = coa_core::build_engine::detect_toolchain();
assert!(tc.msvc_available && tc.cmake_available && tc.ninja_available, "toolchain: {tc:?}");
let dir = coa_core::build_cache::prepare(&root, "probe", "fp").unwrap();
std::fs::write(dir.join("CMakeLists.txt"), "cmake_minimum_required(VERSION 3.22)\nproject(p CXX)\nadd_library(p SHARED a.cpp b.cpp c.cpp)\n").unwrap();
for f in ["a", "b", "c"] {
std::fs::write(dir.join(format!("{f}.cpp")), format!("#include <vector>\n#include <string>\n__declspec(dllexport) int {f}() {{ return (int)std::vector<std::string>(3).size(); }}\n")).unwrap();
}
let first = build(&root, &dir, &tc);
println!("first build compiled {}", compiled(&first));
assert_eq!(compiled(&first), 3);
coa_core::build_cache::finish(&root, "probe");
let s = coa_core::build_cache::summary(&root, None);
println!("packed: {} bytes from {} unpacked", s.total_bytes, s.modules[0].unpacked_bytes);
assert!(!dir.exists());
let dir = coa_core::build_cache::prepare(&root, "probe", "fp").unwrap();
let again = build(&root, &dir, &tc);
println!("after unpack, nothing changed: compiled {}\n{}", compiled(&again), again.lines().filter(|l| l.contains("explain")).take(8).collect::<Vec<_>>().join("\n"));
assert_eq!(compiled(&again), 0, "nothing should be recompiled");
coa_core::build_cache::finish(&root, "probe");
let dir = coa_core::build_cache::prepare(&root, "probe", "fp").unwrap();
std::thread::sleep(std::time::Duration::from_secs(2));
std::fs::write(dir.join("b.cpp"), "__declspec(dllexport) int b() { return 2; }\n").unwrap();
let one = build(&root, &dir, &tc);
println!("after one change: compiled {}", compiled(&one));
assert_eq!(compiled(&one), 1);
coa_core::build_cache::finish(&root, "probe");
println!("PASS");
}
@@ -0,0 +1,194 @@
//! Build the AzerothCore catalogue's modules against a plugin-host realm's SDK and record, per module, the
//! evidence: commit, analysis, build result, compiler errors and the core functions the host does not export.
//! A rerun skips modules that already have a result; `summary` aggregates the results.
//!
//! usage: catalog_build_sweep run <realm> <work dir> <how many, by stars>
//! catalog_build_sweep summary <work dir>
//! The realm needs only Core/worldserver.exe (a plugin host) and its .manager/sdk; use a disposable copy.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Default)]
struct Outcome {
slug: String,
stars: u32,
category: String,
pushed_at: Option<String>,
commit: Option<String>,
/// "built" | "link-missing-exports" | "compile-error" | "not-a-plugin" | "analysis-failed" | "build-error"
result: String,
detail: Option<String>,
classification: Option<String>,
seconds: f64,
/// Core functions the plugin needs that worldserver.exe does not export (MSVC decorated names).
missing_exports: Vec<String>,
/// Demangled form of each missing export, for reading.
missing_readable: Vec<String>,
compile_errors: Vec<String>,
}
fn file_name(slug: &str) -> String {
slug.replace('/', "__")
}
/// The decorated name and its readable form out of an LNK2019/LNK2001 message.
fn missing_symbol(message: &str) -> Option<(String, String)> {
let readable = message.split('"').nth(1).unwrap_or("").to_string();
let start = message.find("(?")?;
let end = message[start..].find(')')? + start;
Some((message[start + 1..end].to_string(), readable))
}
fn run(realm: &Path, work: &Path, count: usize) {
let results = work.join("results");
std::fs::create_dir_all(&results).unwrap();
let sources = work.join("sources");
let catalogue = coa_core::catalogue::fetch_or_load_catalogue(&work.join("catalogue"), false).expect("catalogue");
let mut items: Vec<_> = catalogue
.items
.into_iter()
.filter(|i| !matches!(i.coa_status, coa_core::catalogue::CoaCompatibilityStatus::Incompatible | coa_core::catalogue::CoaCompatibilityStatus::Blocked))
.collect();
items.sort_by(|a, b| b.stars.cmp(&a.stars));
let tc = coa_core::build_engine::detect_toolchain_for(Some(realm));
assert!(tc.msvc_available && tc.cmake_available && tc.ninja_available && tc.sdk_dir.is_some(), "toolchain/SDK for the realm: {tc:?}");
for (n, item) in items.iter().take(count).enumerate() {
let out = results.join(format!("{}.json", file_name(&item.full_name)));
if out.is_file() {
continue;
}
let started = std::time::Instant::now();
let mut o = Outcome {
slug: item.full_name.clone(),
stars: item.stars,
category: item.category.clone(),
pushed_at: item.pushed_at.clone(),
..Default::default()
};
println!("[{}/{}] {} ★{}", n + 1, count.min(items.len()), item.full_name, item.stars);
match coa_core::analyzer::analyze_github_repository(&item.html_url, Some(&sources)) {
Err(e) => {
o.result = "analysis-failed".into();
o.detail = Some(e.to_string());
}
Ok(report) => {
o.commit = Some(report.commit.clone());
o.classification = Some(format!("{:?}", report.classification));
let path = PathBuf::from(&report.repository_path);
let path = if path.is_dir() { path } else {
sources.join("github").join(&item.owner).join(&item.name).join(&report.commit)
};
match coa_core::build_engine::build_module_with_bundle(&path, &[], realm, None, None) {
Err(e) => {
let text = e.to_string();
o.result = if text.contains("NO_LOADER_FOUND") || text.contains("No C++") || text.contains("MANUAL_ADAPTER") {
"not-a-plugin".into()
} else {
"build-error".into()
};
o.detail = Some(text);
}
Ok(b) if b.success => o.result = "built".into(),
Ok(b) => {
for e in &b.errors {
if e.error_code == "LNK2019" || e.error_code == "LNK2001" {
if let Some((mangled, readable)) = missing_symbol(&e.message) {
if !o.missing_exports.contains(&mangled) {
o.missing_exports.push(mangled);
o.missing_readable.push(readable);
}
}
} else if !e.error_code.starts_with("LNK") {
o.compile_errors.push(format!("{} {}:{} {}", e.error_code, e.file.rsplit(['/', '\\']).next().unwrap_or(""), e.line, e.message.lines().next().unwrap_or("")));
}
}
o.result = if o.compile_errors.is_empty() && !o.missing_exports.is_empty() {
"link-missing-exports".into()
} else {
"compile-error".into()
};
o.compile_errors.truncate(10);
}
}
}
}
o.seconds = started.elapsed().as_secs_f64();
println!(" -> {} ({:.0}s){}", o.result, o.seconds, if o.missing_exports.is_empty() { String::new() } else { format!(", {} missing exports", o.missing_exports.len()) });
std::fs::write(&out, serde_json::to_vec_pretty(&o).unwrap()).unwrap();
// source downloads of finished modules are not needed again
let _ = std::fs::remove_dir_all(sources.join("github").join(&item.owner).join(&item.name));
}
}
fn summary(work: &Path) {
let mut all: Vec<Outcome> = std::fs::read_dir(work.join("results")).unwrap()
.flatten()
.filter_map(|e| serde_json::from_slice(&std::fs::read(e.path()).ok()?).ok())
.collect();
all.sort_by(|a, b| b.stars.cmp(&a.stars));
let mut by_result: BTreeMap<&str, usize> = BTreeMap::new();
for o in &all {
*by_result.entry(o.result.as_str()).or_default() += 1;
}
let mut symbols: BTreeMap<String, (String, Vec<String>)> = BTreeMap::new();
for o in all.iter().filter(|o| o.result == "link-missing-exports") {
for (m, r) in o.missing_exports.iter().zip(&o.missing_readable) {
symbols.entry(m.clone()).or_insert_with(|| (r.clone(), Vec::new())).1.push(o.slug.clone());
}
}
let mut lines = vec![format!("modules: {}", all.len())];
for (r, n) in &by_result {
lines.push(format!(" {r}: {n}"));
}
lines.push(format!("distinct missing exports (modules that only lack exports): {}", symbols.len()));
let mut sym: Vec<_> = symbols.iter().collect();
sym.sort_by(|a, b| b.1 .1.len().cmp(&a.1 .1.len()));
for (_, (readable, mods)) in sym.iter().take(40) {
lines.push(format!(" {:>3} {}", mods.len(), readable));
}
// why modules do not compile: a header they need is not there (a dependency: playerbots, a library), or
// the core's API differs from the one they were written for
let mut kinds: BTreeMap<String, usize> = BTreeMap::new();
for o in all.iter().filter(|o| o.result == "compile-error") {
let header = o.compile_errors.iter().find(|e| e.starts_with("C1083")).and_then(|e| {
let start = e.find(": ")? + 2;
Some(e[start..].split(':').next()?.trim().to_string())
});
let kind = match header {
Some(h) => format!("missing header {h}"),
None => format!("core API differs ({})", o.compile_errors.first().map(|e| e.split(' ').next().unwrap_or("")).unwrap_or("")),
};
*kinds.entry(kind).or_default() += 1;
}
lines.push(String::new());
lines.push("compile errors by cause:".into());
let mut k: Vec<_> = kinds.into_iter().collect();
k.sort_by(|a, b| b.1.cmp(&a.1));
for (kind, n) in k.iter().take(25) {
lines.push(format!(" {n:>3} {kind}"));
}
lines.push(String::new());
for o in &all {
lines.push(format!("{:<22} ★{:<5} {:<48} {}", o.result, o.stars, o.slug,
if o.result == "link-missing-exports" { format!("{} exports", o.missing_exports.len()) }
else { o.detail.clone().or_else(|| o.compile_errors.first().cloned()).unwrap_or_default().chars().take(110).collect() }));
}
let text = lines.join("\n");
println!("{text}");
std::fs::write(work.join("summary.txt"), &text).unwrap();
// the exports a core build would add, one decorated name per line
let def: Vec<&String> = symbols.keys().collect();
std::fs::write(work.join("missing-exports.txt"), def.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("\n")).unwrap();
}
fn main() {
let a: Vec<String> = std::env::args().skip(1).collect();
match a.first().map(String::as_str) {
Some("run") => run(Path::new(&a[1]), Path::new(&a[2]), a[3].parse().unwrap()),
Some("summary") => summary(Path::new(&a[1])),
_ => eprintln!("usage: catalog_build_sweep run <realm> <work> <count> | summary <work>"),
}
}
@@ -0,0 +1,23 @@
//! How the module categories fall on a real catalogue: counts per category and a few modules of each.
//! usage: catalogue_categories <catalogue.json downloaded from azerothcore.org>
fn main() {
let path = std::env::args().nth(1).expect("usage: catalogue_categories <catalogue.json>");
let raw: coa_core::catalogue::OfficialCatalogueRaw =
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
let overlay = coa_core::catalogue::load_coa_overlay(None);
let items = coa_core::catalogue::normalize_catalogue(&raw, &overlay);
let mut by: std::collections::BTreeMap<String, Vec<(u32, String, String)>> = Default::default();
for i in &items {
let c = coa_core::catalogue::categorize(&i.full_name, &i.name, &i.description, &i.topics);
by.entry(c).or_default().push((i.stars, i.full_name.clone(), i.description.chars().take(70).collect()));
}
println!("modules: {}", items.len());
for c in coa_core::catalogue::categories() {
let mut list = by.remove(&c.code).unwrap_or_default();
list.sort_by(|a, b| b.0.cmp(&a.0));
println!("\n== {} ({}) {}", c.code, c.ru, list.len());
for (s, n, d) in list.iter().take(6) {
println!(" {s:>5} {n:<45} {d}");
}
}
}
-75
View File
@@ -1,75 +0,0 @@
//! Live check of the client download path against the community launcher's public server:
//! reads the manifest and downloads the few smallest files into a temp folder (never a real client).
//! Usage: cargo run -p coa-core --example client_probe (PROBE_SKIP=n skips the n smallest files)
use coa_core::clientdl::{self, Apply, Manifest};
use coa_core::download::{Cancel, HttpTransport};
fn main() {
let full = clientdl::fetch_latest().expect("manifest");
println!(
"version {} · {} files · {:.1} GB",
full.version,
full.files.len(),
full.total_bytes() as f64 / 1e9
);
let mut small = full.files.clone();
small.sort_by_key(|f| f.size);
let skip: usize = std::env::var("PROBE_SKIP")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(0);
let small: Vec<_> = small.into_iter().skip(skip).take(6).collect();
let manifest = Manifest {
version: full.version.clone(),
published_at: None,
files: small,
};
let dir = std::env::temp_dir().join(format!("coa-client-probe-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let cancel = Cancel::default();
let plan = clientdl::plan(&dir, &manifest, &Default::default(), &cancel, &|_| {}).unwrap();
println!(
"plan: {} files, {} bytes",
plan.items.len(),
plan.download_bytes
);
let transport = HttpTransport::new().unwrap();
clientdl::apply(
&Apply {
client: &dir,
manifest: &manifest,
plan: &plan,
keep_modified: false,
transport: &transport,
objects_url: clientdl::OBJECTS_URL,
cancel: &cancel,
},
&|s| {
println!(
"{} {}/{} {}",
s.phase,
s.done,
s.total,
s.file.unwrap_or_default()
)
},
)
.expect("apply");
let again = clientdl::plan(
&dir,
&manifest,
&clientdl::load_state(&dir).unwrap(),
&cancel,
&|_| {},
)
.unwrap();
println!(
"second plan: {} to do, {} up to date, version {:?}",
again.items.len(),
again.up_to_date_files,
clientdl::local(&dir).version
);
std::fs::remove_dir_all(&dir).unwrap();
}
+114
View File
@@ -0,0 +1,114 @@
//! The Rust launcher on a DISPOSABLE repack, side by side with Scripts/manage.py:
//!
//! 1. install the stable server into a new folder on unused ports;
//! 2. coa-launcher start-all, then manage.py status-json must see every service;
//! 3. manage.py stop-all stops what coa-launcher started;
//! 4. manage.py start-all, coa-launcher status-json sees it, coa-launcher stop-all stops it;
//! 5. both stop-alls leave nothing of the repack running.
//!
//! usage: launcher_e2e <new folder ending in -fixture> <coa-launcher.exe>
use std::path::Path;
use std::process::Command;
use coa_core::install::{install_base, Params};
use coa_core::registry::Registry;
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
fn run(exe: &Path, args: &[&str]) -> (bool, String) {
let out = Command::new(exe).args(args).output().unwrap();
let text = String::from_utf8_lossy(&out.stdout).into_owned() + &String::from_utf8_lossy(&out.stderr);
(out.status.success(), text)
}
fn launcher(root: &Path, exe: &Path, action: &str) -> (bool, String) {
let r = root.to_string_lossy().into_owned();
let t = std::time::Instant::now();
let out = run(exe, &["--root", &r, action]);
println!("[coa-launcher {action}] ok={} in {:?}\n{}", out.0, t.elapsed(), indent(&out.1));
out
}
fn manage(root: &Path, action: &str) -> (bool, String) {
let python = root.join("Runtime/python/python.exe");
let script = root.join("Scripts/manage.py").to_string_lossy().into_owned();
let t = std::time::Instant::now();
let out = run(&python, &["-B", &script, action]);
println!("[manage.py {action}] ok={} in {:?}\n{}", out.0, t.elapsed(), indent(&out.1));
out
}
fn indent(s: &str) -> String {
s.lines().map(|l| format!(" {l}")).collect::<Vec<_>>().join("\n")
}
fn status(out: &str) -> serde_json::Value {
let line = out.lines().rev().find(|l| l.trim_start().starts_with('{')).expect("a JSON status line");
serde_json::from_str(line).unwrap()
}
fn all(v: &serde_json::Value, running: bool) -> bool {
["mysql", "auth", "world", "relay"].iter().all(|k| v[*k] == running)
}
fn main() {
let a: Vec<String> = std::env::args().skip(1).collect();
let root = Path::new(&a[0]);
let exe = Path::new(&a[1]);
assert!(a[0].ends_with("-fixture"), "a folder ending in -fixture is required");
if root.join("Core/worldserver.exe").is_file() {
println!("reusing the installed fixture");
} else {
install(root);
}
test(root, exe);
}
fn install(root: &Path) {
assert!(!root.exists(), "a new folder is required");
let reg_dir = std::env::temp_dir().join("coa-launcher-e2e-registry");
let _ = std::fs::remove_dir_all(&reg_dir);
let reg = Registry::at(reg_dir.join("installs.json"));
// what the Manager installs: the stable release's installation image
let image = coa_core::install::Source::Installation(Box::new(coa_core::channels::resolve("stable").expect("stable channel")));
let installed = install_base(
&Params { source: image, dest: root.into(), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, registry: &reg, cancel: Default::default() },
&|s| eprintln!(" {:>3}% {}", s.percent, s.step),
)
.expect("install");
println!("installed {}", installed.version);
let repack = root.join("Settings/repack.json");
let mut cfg: serde_json::Value = serde_json::from_slice(&std::fs::read(&repack).unwrap()).unwrap();
for k in ["mysqlPort", "authPort", "worldPort", "raPort"] {
cfg[k] = free_port().into();
}
std::fs::write(&repack, serde_json::to_vec_pretty(&cfg).unwrap()).unwrap();
}
fn test(root: &Path, exe: &Path) {
// 2. Rust starts, Python sees it
assert!(launcher(root, exe, "start-all").0, "coa-launcher start-all");
let seen = status(&manage(root, "status-json").1);
assert!(all(&seen, true), "manage.py must see the services coa-launcher started: {seen}");
// 3. Python stops what Rust started
assert!(manage(root, "stop-all").0, "manage.py stop-all");
assert!(all(&status(&launcher(root, exe, "status-json").1), false));
// 4. Python starts, Rust sees and stops it
assert!(manage(root, "start-all").0, "manage.py start-all");
let seen = status(&launcher(root, exe, "status-json").1);
assert!(all(&seen, true), "coa-launcher must see the services manage.py started: {seen}");
assert!(launcher(root, exe, "stop-all").0, "coa-launcher stop-all");
assert!(all(&status(&manage(root, "status-json").1), false));
// 5. Rust alone, a second time (start on a server it stopped)
assert!(launcher(root, exe, "start-all").0);
assert!(launcher(root, exe, "stop-all").0);
let left = coa_core::process::find_by_exe(&root.join("mysql/bin/mysqld.exe")).len()
+ coa_core::process::find_by_exe(&root.join("Core/worldserver.exe")).len()
+ coa_core::process::find_by_exe(&root.join("Core/authserver.exe")).len();
assert_eq!(left, 0, "nothing of the repack may be left running");
println!("LAUNCHER E2E PASS");
}
@@ -0,0 +1,149 @@
//! The Manager's start/stop (`driver`) on the Rust launcher, on a DISPOSABLE repack:
//!
//! 1. driver start-all: every service runs, the console answers, manage.py is not touched and sees the services;
//! 2. driver stop-all stops everything;
//! 3. manage.py start-all, then driver stop-all stops what Python started;
//! 4. a taken world port fails with the typed "port in use" code (no stderr parsing), then stop-all;
//! 5. driver start-all, then the forced stop;
//! 6. with a realm profile: the realm row gets the profile's name, manage.py is still not edited;
//! 7. nothing of the repack is left running.
//!
//! usage: manager_launcher_e2e <existing folder ending in -fixture>
use std::path::Path;
use std::process::Command;
use std::time::Instant;
use coa_core::driver::{self, Verb};
use coa_core::error::ErrorCode;
use coa_core::launcher::{services, settings::Settings, state};
fn indent(s: &str) -> String {
s.lines().map(|l| format!(" {l}")).collect::<Vec<_>>().join("\n")
}
fn drive(root: &Path, verb: Verb) -> driver::DriverOutcome {
let t = Instant::now();
let out = driver::run(root, verb).expect("driver");
println!("[driver {verb:?}] ok={} code={:?} in {:?}\n{}", out.ok, out.code, t.elapsed(), indent(&out.output));
out
}
fn manage(root: &Path, action: &str) -> (bool, String) {
let python = root.join("Runtime/python/python.exe");
let script = root.join("Scripts/manage.py").to_string_lossy().into_owned();
let t = Instant::now();
let out = Command::new(python).args(["-B", &script, action]).output().unwrap();
let text = String::from_utf8_lossy(&out.stdout).into_owned() + &String::from_utf8_lossy(&out.stderr);
println!("[manage.py {action}] ok={} in {:?}\n{}", out.status.success(), t.elapsed(), indent(&text));
(out.status.success(), text)
}
fn python_status(root: &Path) -> serde_json::Value {
let (ok, out) = manage(root, "status-json");
assert!(ok);
let line = out.lines().rev().find(|l| l.trim_start().starts_with('{')).expect("a JSON status line");
serde_json::from_str(line).unwrap()
}
fn all(v: &serde_json::Value, running: bool) -> bool {
["mysql", "auth", "world", "relay"].iter().all(|k| v[*k] == running)
}
fn nothing_running(root: &Path) -> bool {
["mysql", "auth", "world", "supervisor", "relay"].iter().all(|n| state::running(root, n).is_none())
&& ["mysql/bin/mysqld.exe", "Core/authserver.exe", "Core/worldserver.exe"]
.iter()
.all(|exe| coa_core::process::find_by_exe(&root.join(exe)).is_empty())
}
fn console(root: &Path) -> String {
let cfg = Settings::load(root, 0).unwrap();
let mut ra = coa_core::ra::Ra::connect_to(cfg.ra_port, &cfg.ra_username, &cfg.ra_password).unwrap();
ra.run("server info").unwrap()
}
fn realm_name(root: &Path) -> String {
services::mysql(root, Some("SELECT name FROM acore_auth.realmlist WHERE id=1;"), None).unwrap()
}
fn main() {
let a: Vec<String> = std::env::args().skip(1).collect();
let root = Path::new(&a[0]);
assert!(a[0].ends_with("-fixture"), "a folder ending in -fixture is required");
assert!(nothing_running(root), "the fixture must be stopped before the test");
let manage_py = root.join("Scripts/manage.py");
let original = std::fs::read(&manage_py).unwrap();
let profile = root.join("Settings/realm-profile.json");
assert!(!profile.exists(), "the fixture starts without a realm profile");
// 1
let started = drive(root, Verb::StartAll);
assert!(started.ok, "start-all");
assert!(all(&python_status(root), true), "manage.py sees what the Manager started");
let info = console(root);
println!("[console] {}", info.lines().next().unwrap_or(""));
assert_eq!(std::fs::read(&manage_py).unwrap(), original, "manage.py is not rewritten");
assert_eq!(realm_name(root), "AzerothCore", "no profile: the launcher's own realm name");
let shipped = root.join("Scripts/coa-launcher.exe");
let supervisor = state::running(root, "supervisor").expect("a supervisor");
println!("[supervisor] {}", supervisor.exe);
let expected = if shipped.is_file() { "coa-launcher.exe" } else { "python.exe" };
assert!(supervisor.exe.to_lowercase().ends_with(expected), "the world is supervised by {expected}");
let relay = state::running(root, "relay").expect("a bug-report relay");
println!("[relay] {}", relay.exe);
assert!(relay.exe.to_lowercase().ends_with(expected), "the relay is run by {expected}");
assert!(state::running(root, "relay-ready").is_some_and(|r| r.pid == relay.pid), "the relay reported ready");
// 2
assert!(drive(root, Verb::StopAll).ok, "stop-all");
assert!(nothing_running(root), "1-2 left something running");
// 2b: the repack's Python launcher stops a world the Manager started
assert!(drive(root, Verb::StartAll).ok);
assert!(manage(root, "stop-all").0, "manage.py stops what the Manager started");
assert!(nothing_running(root), "2b left something running");
// 3
assert!(manage(root, "start-all").0);
assert!(drive(root, Verb::StopAll).ok, "driver stops what manage.py started");
assert!(nothing_running(root), "3 left something running");
// 4
let cfg = Settings::load(root, 0).unwrap();
let blocker = std::net::TcpListener::bind(("127.0.0.1", cfg.world_port)).unwrap();
let refused = drive(root, Verb::StartAll);
assert!(!refused.ok);
assert_eq!(refused.code, Some(ErrorCode::PortInUse), "typed port error");
assert!(refused.output.contains(&format!("Port {}", cfg.world_port)));
drop(blocker);
assert!(drive(root, Verb::StopAll).ok);
assert!(nothing_running(root), "4 left something running");
// 5
assert!(drive(root, Verb::StartAll).ok);
let t = Instant::now();
let forced = driver::force_stop(root).expect("force stop");
println!("[driver force_stop] ok={} in {:?}\n{}", forced.ok, t.elapsed(), indent(&forced.output));
assert!(forced.ok && forced.output.contains("Force-stopped"));
assert!(nothing_running(root), "5 left something running");
// 6
std::fs::write(&profile, br#"{"active":"coa","wildcard_created":false}"#).unwrap();
let result = std::panic::catch_unwind(|| {
assert!(drive(root, Verb::StartAll).ok, "start-all with a realm profile");
assert_eq!(realm_name(root), "Conquest of Azeroth");
assert_eq!(std::fs::read(&manage_py).unwrap(), original, "manage.py is not edited for a realm profile either");
assert!(drive(root, Verb::StopAll).ok);
});
std::fs::remove_file(&profile).unwrap();
std::fs::write(&manage_py, &original).unwrap();
if result.is_err() {
let _ = driver::run(root, Verb::StopAll);
panic!("step 6 failed");
}
// 7
assert!(nothing_running(root), "something of the repack is still running");
println!("PASS: the Manager starts and stops through the Rust launcher, interoperates with manage.py, typed errors");
}
@@ -0,0 +1,197 @@
//! 0.9.10 install paths of the Modules Preview, end to end on DISPOSABLE servers under <work>:
//!
//! 1. fresh: a new server goes on to the preview core with the bare core (no module is compiled).
//! 2. copy: a classic server (stopped) gets a modular copy next to it: accounts, characters, the world and
//! module settings arrive, the old server's files are byte-for-byte unchanged, the copy takes the
//! preview core with the modules the old server had switched on.
//! 3. copy-running: the same copy step from a classic server whose database is running.
//!
//! usage: preview_bare_and_copy_e2e <empty work folder> [fresh|copy|all]
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use coa_core::install::{install_base, Params, Source};
use coa_core::registry::Registry;
use coa_core::update::{self, RepackEnv};
const KEY: &str = coa_core::signing::EMBEDDED_PUBLIC_KEY;
type R<T> = Result<T, Box<dyn std::error::Error>>;
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
fn sql(root: &Path, q: &str) -> String {
coa_core::backup::with_database(root, |db| db.query(q)).unwrap_or_else(|e| panic!("{q}: {e}"))
}
fn install(root: &Path, reg: &Registry) -> R<()> {
let image = coa_core::channels::stable_source()?;
let t = std::time::Instant::now();
let done = install_base(
&Params { source: Source::Installation(Box::new(image)), dest: root.into(), trusted_key: KEY, registry: reg, cancel: Default::default() },
&|s| eprintln!(" {:>3}% {}", s.percent, s.step),
)?;
println!("[{}] installed {} in {:?}", root.display(), done.version, t.elapsed());
Ok(())
}
fn set_free_ports(root: &Path) -> R<()> {
let repack = root.join("Settings/repack.json");
let mut cfg: serde_json::Value = serde_json::from_slice(&std::fs::read(&repack)?)?;
for k in ["mysqlPort", "authPort", "worldPort", "raPort"] {
cfg[k] = free_port().into();
}
std::fs::write(&repack, serde_json::to_vec_pretty(&cfg)?)?;
Ok(())
}
/// The same steps as the Manager's install_preview_core.
fn preview_core(root: &Path) -> R<update::Outcome> {
let meta_dir = coa_core::registry::metadata_dir_for(root)?;
let (_, meta) = coa_core::registry::MetaDir::open(&meta_dir)?;
let source = coa_core::update_route::select(&meta, coa_core::channels::preview_update_source()?, KEY)?.source;
let env = RepackEnv { root, meta_dir: &meta_dir };
let t = std::time::Instant::now();
let out = coa_core::install::with_scratch_ports(root, || update::apply(
&update::Params { root, meta_dir: &meta_dir, source, trusted_key: KEY, cancel: Default::default(), resolutions: Default::default(), env: &env, fail_after_ops: None },
&|phase, percent| println!(" {percent}% {phase}"),
));
let _ = coa_core::driver::run(root, coa_core::driver::Verb::StopAll);
let out = out?;
println!("[{}] preview core: {:?} in {:?}; modules {:?}", root.display(), out.txn.state, t.elapsed(), out.modules);
assert_eq!(out.txn.state, update::State::Committed);
assert!(coa_core::provenance::realm_host_fingerprint(root).is_some(), "the plugin host is installed");
Ok(out)
}
/// sha256 of every file under the old server, except the launcher's own run state.
fn tree(root: &Path) -> BTreeMap<String, String> {
fn walk(base: &Path, dir: &Path, out: &mut BTreeMap<String, String>) {
for e in std::fs::read_dir(dir).into_iter().flatten().flatten() {
let p = e.path();
if p.is_dir() {
walk(base, &p, out);
} else {
let rel = p.strip_prefix(base).unwrap().to_string_lossy().replace('\\', "/");
out.insert(rel, coa_core::fsx::sha256_file(&p).unwrap_or_else(|e| format!("unreadable: {e}")));
}
}
}
let mut out = BTreeMap::new();
walk(root, root, &mut out);
out
}
fn fresh(work: &Path, reg: &Registry) -> R<()> {
let root = work.join("fresh-fixture");
install(&root, reg)?; // shipped ports, as in the Manager: another server may be running on them
coa_core::rebuild::decline_recommended(&root)?;
let out = preview_core(&root)?;
assert!(out.modules.is_empty(), "a new server compiles nothing: {:?}", out.modules);
let registry = coa_core::plugins::load_registry(&root)?;
assert!(registry.plugins.values().all(|e| !e.installed), "no plugin installed: {:?}", registry.plugins.keys());
let boot: serde_json::Value = serde_json::from_slice(&std::fs::read(root.join("Core/plugins/plugin-boot-report.json"))?)?;
println!("boot report: {boot}");
assert_eq!(boot["anyFaulted"], serde_json::json!(false));
println!("FRESH PASS");
Ok(())
}
fn copy(work: &Path, reg: &Registry) -> R<()> {
// A classic server with data of its own.
let old = work.join("classic-fixture");
install(&old, reg)?;
set_free_ports(&old)?; // the test runs this "old" server; it must not meet a real one on the shipped ports
sql(&old, "INSERT INTO acore_auth.account (username, salt, verifier, email, reg_mail) \
VALUES ('E2ECOPY', UNHEX(REPEAT('11',32)), UNHEX(REPEAT('22',32)), '', '')");
let account = sql(&old, "SELECT id FROM acore_auth.account WHERE username='E2ECOPY'").trim().to_string();
sql(&old, &format!("SET SESSION sql_mode=''; INSERT INTO acore_characters.characters (guid, account, name, race, class, gender, level, money) \
VALUES (900001, {account}, 'Copytest', 1, 1, 0, 42, 123456)"));
let conf = old.join("Core/configs/modules/mod-coa-content-scaling.conf");
let conf_text = std::fs::read_to_string(&conf).unwrap_or_default() + "\n# e2e owner edit\n";
std::fs::write(&conf, &conf_text)?;
let fingerprint = |root: &Path| sql(root, "SELECT CONCAT((SELECT COUNT(*) FROM acore_auth.account),'|',(SELECT COUNT(*) FROM acore_characters.characters),'|',\
(SELECT CONCAT(name,':',level,':',money) FROM acore_characters.characters WHERE guid=900001))").trim().to_string();
let expected = fingerprint(&old);
let counts = sql(&old, "SELECT CONCAT((SELECT COUNT(*) FROM acore_auth.account),'|',(SELECT COUNT(*) FROM acore_characters.characters))").trim().to_string();
// A world setting changed on the Server Configuration page lives in the old server's template.
let old_meta = coa_core::registry::metadata_dir_for(&old)?;
let rate = BTreeMap::from([("Rate.XP.Kill".to_string(), "3".to_string())]);
assert_eq!(coa_core::allsettings::save(&old, &old_meta, &rate)?, ["Rate.XP.Kill"]);
let _ = coa_core::driver::run(&old, coa_core::driver::Verb::StopAll);
println!("old server data: {expected}");
assert!(coa_core::modular_copy::is_classic(&old));
let before_preview = tree(&old);
let p = coa_core::modular_copy::preview(&old)?;
println!("preview (stopped): {p:?}");
assert!(p.classic && !p.modular && p.database == "stopped" && p.accounts.is_none());
assert!(p.database_bytes > 0 && p.config_files.iter().any(|f| f.ends_with("mod-coa-content-scaling.conf")));
assert_eq!(tree(&old), before_preview, "the preview changed the old server's files");
// 2. copy of the stopped server, then the preview core on the copy.
let before = tree(&old);
let new = work.join("copy-fixture");
install(&new, reg)?;
let report = coa_core::modular_copy::copy_data(&old, &new, &|w| println!(" copy: {w}"))?;
println!("copy report: {report:?}");
assert_eq!(report.read_from, "copy of the stopped database");
assert_eq!(tree(&old), before, "the old server's files changed");
let observed = coa_core::process::observe(&new, &coa_core::layout::read_ports(&new));
assert_eq!(observed.mysql.state, coa_core::process::ServiceState::Stopped, "a database of the copy is still running: {:?}", observed.mysql);
assert!(!coa_core::registry::metadata_dir_for(&new)?.join("import").exists(), "the private copy of the old data is removed");
assert_eq!(std::fs::read_to_string(new.join("Core/configs/modules/mod-coa-content-scaling.conf"))?, conf_text);
assert_eq!(report.world_settings, ["Rate.XP.Kill"], "skipped: {:?}", report.world_settings_skipped);
let world_value = |root: &Path| coa_core::allsettings::list(root).unwrap().into_iter().find(|i| i.key == "Rate.XP.Kill").map(|i| i.value);
assert_eq!(world_value(&new).as_deref(), Some("3"));
let out = preview_core(&new)?;
println!("modules on the copy: {:?}", out.modules);
let got = fingerprint(&new);
let _ = coa_core::driver::run(&new, coa_core::driver::Verb::StopAll);
assert_eq!(got, expected, "data of the copy after the preview core");
assert_eq!(tree(&old), before, "the old server's files changed");
println!("COPY (stopped) PASS");
// 3. copy while the old database runs: read from it, change nothing.
let started = coa_core::driver::run(&old, coa_core::driver::Verb::StartMysql)?;
assert!(started.ok, "{}", started.output);
let data_before: BTreeMap<String, String> = tree(&old).into_iter().filter(|(k, _)| !k.starts_with("mysql/") && !k.starts_with(".state/") && !k.starts_with("Core/configs/")).collect();
let rows_before = fingerprint(&old);
let p = coa_core::modular_copy::preview(&old)?;
println!("preview (running): accounts {:?} characters {:?} schemas {:?}", p.accounts, p.characters, p.schemas);
assert_eq!(p.database, "running");
assert_eq!(format!("{}|{}", p.accounts.unwrap(), p.characters.unwrap()), counts);
assert!(p.schemas.iter().any(|s| s == "acore_characters"));
let new2 = work.join("copy-running-fixture");
install(&new2, reg)?;
let report = coa_core::modular_copy::copy_data(&old, &new2, &|w| println!(" copy: {w}"));
let rows_after = fingerprint(&old);
let _ = coa_core::driver::run(&old, coa_core::driver::Verb::StopAll);
let report = report?;
assert_eq!(report.read_from, "running database");
assert_eq!(rows_after, rows_before);
let data_after: BTreeMap<String, String> = tree(&old).into_iter().filter(|(k, _)| !k.starts_with("mysql/") && !k.starts_with(".state/") && !k.starts_with("Core/configs/")).collect();
assert_eq!(data_after, data_before, "files outside the database changed");
assert_eq!(fingerprint(&new2), expected);
assert_eq!(world_value(&new2).as_deref(), Some("3"));
let _ = coa_core::driver::run(&new2, coa_core::driver::Verb::StopAll);
println!("COPY (running) PASS");
Ok(())
}
fn main() -> R<()> {
let a: Vec<String> = std::env::args().skip(1).collect();
let work = PathBuf::from(a.first().expect("usage: preview_bare_and_copy_e2e <empty work folder> [fresh|copy|all]"));
let mode = a.get(1).map(String::as_str).unwrap_or("all");
assert!(!work.exists() || std::fs::read_dir(&work)?.next().is_none(), "work folder must be new or empty");
std::fs::create_dir_all(&work)?;
let reg = Registry::at(work.join("installs.json"));
if mode == "fresh" || mode == "all" {
fresh(&work, &reg)?;
}
if mode == "copy" || mode == "all" {
copy(&work, &reg)?;
}
println!("ALL PASS ({mode})");
Ok(())
}
@@ -0,0 +1,170 @@
//! The Rust bug-report relay against the repack's `BugReport/relay.py`, offline (a scripted service, no network):
//!
//! 1. the same reports and the same service answers give the same `.status` files and journal rows;
//! 2. each relay continues the other's journal: what one delivered is never sent again by the other, and a post the
//! other left in progress is uncertain.
//!
//! usage: relay_equivalence <repack folder with Runtime/python and BugReport/relay.py>
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Arc, Mutex};
use coa_core::launcher::relay::{Failure, Relay, Service};
const REPORTS: [(&str, &str); 5] = [
("1-00000000000000a1", "COABUG1\r\nQuest broken\r\nThe NPC is gone.\r\n"),
("9-00000000000000b2", "COABUG1\nRate limited\nBody text\n"),
("3-00000000000000c3", "COABUG1\nRejected\nBody\n"),
("4-00000000000000d4", "COABUG1\nxy\nTitle too short\n"),
("5-00000000000000e5", "COABUG1\nUnknown outcome\nBody\n"),
];
/// The answers per title, in the vocabulary of both relays.
fn answer(title: &str) -> std::result::Result<u64, Failure> {
match title {
"Quest broken" => Ok(101),
"Rate limited" => Err(Failure::Limited(60)),
"Rejected" => Err(Failure::Invalid),
_ => Err(Failure::Uncertain),
}
}
const PYTHON: &str = r#"
import importlib.util, sys, json
from pathlib import Path
spec = importlib.util.spec_from_file_location("relay", sys.argv[1]); relay = importlib.util.module_from_spec(spec); spec.loader.exec_module(relay)
spool = Path(sys.argv[2]); calls = []
class Fake:
def create(self, title, body):
calls.append(title)
if title == "Quest broken": return 101
if title == "Rate limited": raise relay.DeliveryError("limited", 60)
if title == "Rejected": raise relay.DeliveryError("invalid")
raise relay.DeliveryError("uncertain")
t = [float(sys.argv[3])]
def clock():
t[0] += 3
return t[0]
r = relay.Relay(spool, Fake(), clock=clock)
for path in sorted(spool.glob("*.report")):
r.process(path)
r.close()
print(json.dumps(calls))
"#;
struct Scripted(Arc<Mutex<Vec<String>>>);
impl Service for Scripted {
fn create(&self, title: &str, _: &str) -> std::result::Result<u64, Failure> {
self.0.lock().unwrap().push(title.into());
answer(title)
}
}
fn spool(dir: &Path) -> PathBuf {
std::fs::create_dir_all(dir).unwrap();
for (key, text) in REPORTS {
std::fs::write(dir.join(format!("{key}.report")), text).unwrap();
}
dir.into()
}
fn python(repack: &Path, spool: &Path, start: f64) -> Vec<String> {
let out = Command::new(repack.join("Runtime/python/python.exe"))
.args(["-I", "-B", "-c", PYTHON])
.arg(repack.join("BugReport/relay.py"))
.arg(spool)
.arg(start.to_string())
.output()
.unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
serde_json::from_slice(&out.stdout).unwrap()
}
/// The clock of both relays: `start`, then 3 seconds later at every reading.
fn rust(spool: &Path, start: f64) -> Vec<String> {
let calls = Arc::new(Mutex::new(Vec::new()));
let t = Mutex::new(start);
let clock = move || {
let mut v = t.lock().unwrap();
*v += 3.0;
*v
};
let relay = Relay::with_clock(spool, Scripted(calls.clone()), Box::new(clock)).unwrap();
relay.pass().unwrap();
drop(relay);
let c = calls.lock().unwrap().clone();
c
}
/// `.status` files and journal rows (without the post time).
fn snapshot(spool: &Path) -> (Vec<(String, String)>, Vec<String>) {
let mut statuses: Vec<(String, String)> = std::fs::read_dir(spool)
.unwrap()
.flatten()
.filter(|e| e.path().extension().is_some_and(|x| x == "status"))
.map(|e| (e.file_name().to_string_lossy().into_owned(), std::fs::read_to_string(e.path()).unwrap()))
.collect();
statuses.sort();
let db = rusqlite::Connection::open(spool.join("relay.sqlite3")).unwrap();
let mut q = db.prepare("SELECT key, digest, marker, state, number, next_try FROM deliveries ORDER BY key").unwrap();
let rows = q
.query_map([], |r| {
Ok(format!(
"{}|{}|{}|{}|{:?}|{}",
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, String>(2)?,
r.get::<_, String>(3)?,
r.get::<_, Option<i64>>(4)?,
r.get::<_, f64>(5)?
))
})
.unwrap()
.map(Result::unwrap)
.collect();
(statuses, rows)
}
fn main() {
let repack = PathBuf::from(std::env::args().nth(1).expect("repack folder"));
let work = tempfile::tempdir().unwrap();
// 1
let (a, b) = (spool(&work.path().join("python")), spool(&work.path().join("rust")));
let py_calls = python(&repack, &a, 1000.0);
let rs_calls = rust(&b, 1000.0);
println!("python posted {py_calls:?}\nrust posted {rs_calls:?}");
assert_eq!(py_calls, rs_calls, "the same reports are posted");
let (pa, pb) = (snapshot(&a), snapshot(&b));
for s in &pa.0 {
println!(" {} = {}", s.0, s.1.trim());
}
assert_eq!(pa, pb, "the same statuses and journal");
// 2: each continues the other's journal. Before the retry times nothing is sent again; after them only the
// rate-limited report is (created, rejected and uncertain ones never are).
assert!(python(&repack, &b, 1000.0).is_empty(), "Python does not resend what Rust handled");
assert!(rust(&a, 1000.0).is_empty(), "Rust does not resend what Python handled");
// Later passes: the queued reports go out, a refused or uncertain one never again.
for start in [5000.0, 6000.0, 7000.0] {
let (p, r) = (python(&repack, &b, start), rust(&a, start));
println!("at {start}: python on rust's journal posted {p:?}, rust on python's posted {r:?}");
assert_eq!(p, r, "the same retries");
assert!(!p.iter().any(|t| t == "Quest broken" || t == "Unknown outcome"), "created and uncertain are final");
}
assert_eq!(snapshot(&a), snapshot(&b), "the same after a retry by the other relay");
for dir in [&a, &b] {
let db = rusqlite::Connection::open(dir.join("relay.sqlite3")).unwrap();
db.execute("UPDATE deliveries SET state='posting' WHERE key='9-00000000000000b2'", []).unwrap();
}
python(&repack, &b, 9000.0);
rust(&a, 9000.0);
for dir in [&a, &b] {
let db = rusqlite::Connection::open(dir.join("relay.sqlite3")).unwrap();
let state: String = db.query_row("SELECT state FROM deliveries WHERE key='9-00000000000000b2'", [], |r| r.get(0)).unwrap();
assert_eq!(state, "uncertain", "an interrupted post is not retried ({})", dir.display());
}
assert_eq!(snapshot(&a), snapshot(&b), "still the same after the crossover");
println!("PASS: relay journals, statuses and posts are the same in Python and Rust");
}
@@ -0,0 +1,103 @@
//! SQUID database provisioning in Rust gives exactly what Scripts/squid_playerbots.py gives, on a DISPOSABLE repack:
//! the same journals (coa_squid_migrations, coa_squid_pending) in every schema, the same acore_playerbots tables
//! with the same row counts, the same playerbots.conf. Each side starts from the same state (SQUID never
//! provisioned), and a second run of each must change nothing.
//!
//! usage: squid_equivalence <fixture ending in -fixture> <coa-launcher.exe>
use std::path::Path;
use std::process::Command;
use coa_core::launcher::{services, squid};
fn sql(root: &Path, q: &str) -> String {
services::mysql(root, Some(q), None).unwrap_or_else(|e| panic!("{q}: {e}"))
}
/// What provisioning leaves behind, comparable between runs.
fn snapshot(root: &Path) -> String {
let mut out = Vec::new();
for schema in ["acore_characters", "acore_world", "acore_playerbots"] {
let tables = sql(root, &format!("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='{schema}' ORDER BY TABLE_NAME;"));
for t in tables.lines() {
if schema != "acore_playerbots" && !t.starts_with("coa_squid") {
continue;
}
let n = sql(root, &format!("SELECT COUNT(*) FROM `{schema}`.`{t}`;"));
out.push(format!("{schema}.{t} rows={n}"));
if t == "coa_squid_migrations" || t == "coa_squid_pending" {
for row in sql(root, &format!("SELECT path, sha256 FROM `{schema}`.`{t}` ORDER BY path;")).lines() {
out.push(format!(" {row}"));
}
}
}
}
let conf = std::fs::read_to_string(root.join("Core/configs/modules/playerbots.conf")).unwrap();
out.push(format!("playerbots.conf:\n{}", conf.replace("\r\n", "\n")));
out.join("\n")
}
fn reset(root: &Path, conf: &[u8]) {
sql(root, "DROP DATABASE IF EXISTS acore_playerbots;");
for schema in ["acore_characters", "acore_world"] {
sql(root, &format!("DROP TABLE IF EXISTS `{schema}`.coa_squid_migrations, `{schema}`.coa_squid_pending;"));
}
std::fs::write(root.join("Core/configs/modules/playerbots.conf"), conf).unwrap();
}
fn python(root: &Path) {
let harness = root.join(".state/squid_harness.py");
std::fs::write(
&harness,
"import sys, pathlib\nroot = pathlib.Path(sys.argv[1])\nsys.path.insert(0, str(root / 'Scripts'))\nsys.argv = ['manage.py']\nimport manage\nfrom squid_playerbots import prepare_playerbots\nprepare_playerbots(manage.ROOT, manage.settings(), manage.mysql)\n",
)
.unwrap();
let out = Command::new(root.join("Runtime/python/python.exe")).arg("-B").arg(&harness).arg(root).output().unwrap();
assert!(out.status.success(), "python: {}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
print!("{}", String::from_utf8_lossy(&out.stdout));
}
fn rust(root: &Path) {
let cfg = coa_core::launcher::Settings::load(root, 0).unwrap();
squid::prepare_playerbots(root, cfg.mysql_port, &|q| services::mysql(root, Some(q), None), &|m| println!("{m}")).unwrap();
}
fn main() {
let a: Vec<String> = std::env::args().skip(1).collect();
let root = Path::new(&a[0]);
let exe = Path::new(&a[1]);
assert!(a[0].ends_with("-fixture"));
let modules = root.join("Core/configs/modules");
// SQUID on, Companions off
let companions = std::fs::read_to_string(modules.join("mod_coa_playerbots.conf")).unwrap();
std::fs::write(modules.join("mod_coa_playerbots.conf"), companions.replace("CoaBots.Enable = 1", "CoaBots.Enable = 0")).unwrap();
let squid_conf = std::fs::read_to_string(modules.join("playerbots.conf")).unwrap().replace("AiPlayerbot.Enabled = 0", "AiPlayerbot.Enabled = 1");
let r = root.to_string_lossy().into_owned();
let started = Command::new(exe).args(["--root", &r, "start-mysql"]).output().unwrap();
assert!(started.status.success(), "{}", String::from_utf8_lossy(&started.stderr));
reset(root, squid_conf.as_bytes());
let t = std::time::Instant::now();
python(root);
println!("python provisioned in {:?}", t.elapsed());
let by_python = snapshot(root);
python(root);
assert_eq!(snapshot(root), by_python, "a second Python run changed something");
reset(root, squid_conf.as_bytes());
let t = std::time::Instant::now();
rust(root);
println!("rust provisioned in {:?}", t.elapsed());
let by_rust = snapshot(root);
rust(root);
assert_eq!(snapshot(root), by_rust, "a second Rust run changed something");
if by_python != by_rust {
std::fs::write(root.join(".state/squid-python.txt"), &by_python).unwrap();
std::fs::write(root.join(".state/squid-rust.txt"), &by_rust).unwrap();
panic!("Rust and Python differ: see .state/squid-python.txt and squid-rust.txt");
}
println!("{} lines of journal/table/config state are identical", by_rust.lines().count());
let stopped = Command::new(exe).args(["--root", &r, "stop-all"]).output().unwrap();
assert!(stopped.status.success());
println!("SQUID EQUIVALENCE PASS");
}
+494
View File
@@ -0,0 +1,494 @@
//! A catalog of game addons the Manager can install into a client's `Interface/AddOns`.
//!
//! Every addon is pinned to a commit of its GitHub repository; the catalog lists each file it installs with its
//! git blob SHA-1 and size, which are checked after download (GitHub's archives are not byte-stable, their
//! contents are). The Manager records what it installed in `Interface/AddOns/.coa-addons.json` and only ever
//! removes or replaces those folders; a folder of the same name that someone else put there is moved to
//! `Interface/AddOns/.coa-addons-backup` first.
use std::collections::BTreeMap;
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use crate::download::{Cancel, Transport};
use crate::error::{Error, Result};
use crate::fsx;
static CATALOG: &str = include_str!("../../../addons/catalog.json");
const STATE: &str = ".coa-addons.json";
const BACKUP: &str = ".coa-addons-backup";
/// No addon archive is anywhere near this; a larger answer is not what the catalog describes.
const MAX_ARCHIVE: u64 = 1 << 30;
#[derive(Debug, Clone, Deserialize)]
struct AddonFile {
path: String,
from: String,
sha1: String,
size: u64,
}
#[derive(Debug, Clone, Deserialize)]
struct Entry {
id: String,
name: String,
description: BTreeMap<String, String>,
author: String,
license: String,
#[serde(default)]
needs: Option<String>,
repo: String,
commit: String,
archive: String,
folders: Vec<String>,
files: Vec<AddonFile>,
}
#[derive(Deserialize)]
struct Catalog {
addons: Vec<Entry>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct Installed {
commit: String,
folders: Vec<String>,
}
/// One addon of the catalog as the Manager shows it for a client.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AddonView {
pub id: String,
pub name: String,
pub description: BTreeMap<String, String>,
pub author: String,
pub license: String,
/// A server module the addon is made for (`playerbots`), shown as a hint.
pub needs: Option<String>,
pub repo: String,
pub size: u64,
pub installed: bool,
pub update_available: bool,
/// Folders of this addon that are in the client but were not installed by the Manager.
pub foreign_folders: Vec<String>,
/// Installed by something else, shown so nobody looks for it twice: `custom-races` (the client patch).
pub bundled: Option<String>,
}
fn catalog() -> Result<Vec<Entry>> {
let catalog: Catalog = serde_json::from_str(CATALOG)?;
for entry in &catalog.addons {
for folder in &entry.folders {
if folder.is_empty() || folder.contains(['/', '\\']) || folder.starts_with('.') {
return Err(Error::Invalid(format!("Invalid addon folder in the catalog: {folder}")));
}
}
}
Ok(catalog.addons)
}
fn addons_dir(client: &Path) -> PathBuf {
client.join("Interface").join("AddOns")
}
fn read_state(client: &Path) -> BTreeMap<String, Installed> {
fsx::read_json(&addons_dir(client).join(STATE)).unwrap_or_default()
}
fn write_state(client: &Path, state: &BTreeMap<String, Installed>) -> Result<()> {
fsx::atomic_write_json(&addons_dir(client).join(STATE), state)
}
/// The catalog with what is installed in `client`.
pub fn list(client: &Path) -> Result<Vec<AddonView>> {
let state = read_state(client);
let dir = addons_dir(client);
let owned: Vec<&String> = state.values().flat_map(|i| i.folders.iter()).collect();
Ok(catalog()?
.into_iter()
.map(|entry| {
let installed = state.get(&entry.id);
AddonView {
foreign_folders: entry
.folders
.iter()
.filter(|f| dir.join(f).exists() && !owned.contains(f))
.cloned()
.collect(),
size: entry.files.iter().map(|f| f.size).sum(),
installed: installed.is_some(),
update_available: installed.is_some_and(|i| i.commit != entry.commit),
id: entry.id,
name: entry.name,
description: entry.description,
author: entry.author,
license: entry.license,
needs: entry.needs,
repo: entry.repo,
bundled: None,
}
})
.chain(std::iter::once(dev_menu(&dir)))
.collect())
}
/// The /dev menu addon of Custom Races V2: the client patch installs it and switches it with the patch.
fn dev_menu(dir: &Path) -> AddonView {
let mut description = BTreeMap::new();
for (lang, text) in [
("en", "The in-game /dev editor for game masters. It comes with the Custom Races V2 client patch and is switched with it."),
("ru", "Внутриигровой редактор /dev для гейм-мастеров. Ставится вместе с клиентским патчем Custom Races V2 и включается вместе с ним."),
("de", "Der /dev-Editor im Spiel für Spielleiter. Er kommt mit dem Client-Patch Custom Races V2 und wird mit ihm geschaltet."),
("fr", "L'éditeur /dev en jeu pour les maîtres du jeu. Il vient avec le patch client Custom Races V2 et s'active avec lui."),
("es", "El editor /dev del juego para los máster. Viene con el parche de cliente Custom Races V2 y se activa con él."),
("zh", "供游戏管理员使用的游戏内 /dev 编辑器。随 Custom Races V2 客户端补丁安装,并随补丁一起开关。"),
] {
description.insert(lang.to_string(), text.to_string());
}
AddonView {
id: "coa-dev-menu".into(),
name: "CoA Dev Menu".into(),
description,
author: "ilusixn".into(),
license: "see repository".into(),
needs: None,
repo: "https://github.com/ilusixn/ilusixn-dev-menu".into(),
size: 0,
installed: dir.join("CoA_DevMenu/CoA_DevMenu.toc").is_file(),
update_available: false,
foreign_folders: Vec::new(),
bundled: Some("custom-races".into()),
}
}
/// The git blob id of `bytes` (what the catalog records for each file).
fn git_blob_sha1(bytes: &[u8]) -> String {
let mut hasher = Sha1::new();
hasher.update(format!("blob {}\0", bytes.len()).as_bytes());
hasher.update(bytes);
hex::encode(hasher.finalize())
}
fn download(transport: &dyn Transport, url: &str, dest: &Path, cancel: &Cancel) -> Result<()> {
crate::download::check_url(url)?;
let reply = transport.get(url, 0).map_err(Error::Invalid)?;
if reply.status != 200 {
return Err(Error::Invalid(format!("The addon download answered HTTP {}.", reply.status)));
}
let mut body = reply.body.take(MAX_ARCHIVE + 1);
let mut file = fs::File::create(dest)?;
let mut buf = vec![0u8; 1 << 20];
let mut total = 0u64;
loop {
if cancel.is_set() {
return Err(Error::Invalid("Operation cancelled.".into()));
}
let n = body.read(&mut buf)?;
if n == 0 {
break;
}
total += n as u64;
if total > MAX_ARCHIVE {
return Err(Error::Invalid("The addon download is larger than any catalog addon.".into()));
}
file.write_all(&buf[..n])?;
}
file.sync_all()?;
Ok(())
}
/// Unpack the catalog's files of `entry` from `archive` into `staging`, checking every one.
fn unpack(entry: &Entry, archive: &Path, staging: &Path) -> Result<()> {
let mut zip = zip::ZipArchive::new(fs::File::open(archive)?)
.map_err(|e| Error::Invalid(format!("The addon download is not a zip archive: {e}")))?;
// GitHub archives hold everything under one top folder (`<repo>-<commit>/`).
let mut by_path = BTreeMap::new();
for index in 0..zip.len() {
let name = zip
.by_index(index)
.map_err(|e| Error::Invalid(e.to_string()))?
.name()
.replace('\\', "/");
if let Some((_, inner)) = name.split_once('/') {
by_path.insert(inner.to_string(), index);
}
}
for file in &entry.files {
let index = *by_path
.get(&file.from)
.ok_or_else(|| Error::Invalid(format!("The addon download lacks {}.", file.from)))?;
let mut zipped = zip.by_index(index).map_err(|e| Error::Invalid(e.to_string()))?;
let mut bytes = Vec::with_capacity(file.size as usize);
zipped.by_ref().take(file.size + 1).read_to_end(&mut bytes)?;
if bytes.len() as u64 != file.size || git_blob_sha1(&bytes) != file.sha1 {
return Err(Error::Invalid(format!("{} failed verification.", file.from)));
}
let target = fsx::safe_join(staging, &file.path)?;
if let Some(parent) = target.parent() {
fs::create_dir_all(parent)?;
}
fs::write(target, bytes)?;
}
Ok(())
}
fn stamp() -> String {
chrono::Local::now().format("%Y%m%d-%H%M%S").to_string()
}
/// Install or update `id` in `client` from `transport`.
pub fn install_with(transport: &dyn Transport, client: &Path, id: &str, cancel: &Cancel) -> Result<Vec<AddonView>> {
if crate::client::is_running(client) {
return Err(Error::Invalid("Close the game before installing addons.".into()));
}
let entry = catalog()?
.into_iter()
.find(|e| e.id == id)
.ok_or_else(|| Error::Invalid(format!("Unknown addon {id}.")))?;
install_entry(transport, client, &entry, cancel)?;
list(client)
}
fn install_entry(transport: &dyn Transport, client: &Path, entry: &Entry, cancel: &Cancel) -> Result<()> {
let dir = addons_dir(client);
fs::create_dir_all(&dir)?;
// Staged next to AddOns' own folders, so the final moves stay on one volume even for a linked AddOns.
let staging = tempfile::Builder::new().prefix(".coa-addon-").tempdir_in(&dir)?;
let archive = staging.path().join("archive.zip");
download(transport, &entry.archive, &archive, cancel)?;
let unpacked = staging.path().join("files");
unpack(entry, &archive, &unpacked)?;
if cancel.is_set() {
return Err(Error::Invalid("Operation cancelled.".into()));
}
let mut state = read_state(client);
let ours: Vec<String> = state.values().flat_map(|i| i.folders.clone()).collect();
// Move what is in the way out first, so a failed move leaves the client as it was.
let mut moved: Vec<(PathBuf, PathBuf)> = Vec::new();
let undo = |moved: &[(PathBuf, PathBuf)]| {
for (from, to) in moved.iter().rev() {
let _ = fs::rename(to, from);
}
};
for folder in &entry.folders {
let target = dir.join(folder);
if !target.exists() {
continue;
}
let aside = if ours.contains(folder) {
staging.path().join("old").join(folder)
} else {
dir.join(BACKUP).join(format!("{folder}-{}", stamp()))
};
if let Some(parent) = aside.parent() {
fs::create_dir_all(parent)?;
}
if let Err(e) = fs::rename(&target, &aside) {
undo(&moved);
return Err(Error::Invalid(format!("{folder} is in use and could not be replaced: {e}")));
}
moved.push((target, aside));
}
let mut placed: Vec<PathBuf> = Vec::new();
for folder in &entry.folders {
let target = dir.join(folder);
if let Err(e) = fs::rename(unpacked.join(folder), &target) {
for p in &placed {
let _ = fs::remove_dir_all(p);
}
undo(&moved);
return Err(Error::Invalid(format!("{folder} could not be installed: {e}")));
}
placed.push(target);
}
state.insert(
entry.id.clone(),
Installed {
commit: entry.commit.clone(),
folders: entry.folders.clone(),
},
);
write_state(client, &state)?;
tracing::info!(addon = %entry.id, commit = %entry.commit, "addon installed");
Ok(())
}
pub fn install(client: &Path, id: &str, cancel: &Cancel) -> Result<Vec<AddonView>> {
install_with(&crate::download::HttpTransport::with_total_timeout(1800)?, client, id, cancel)
}
/// Remove an addon the Manager installed (only its recorded folders).
pub fn remove(client: &Path, id: &str) -> Result<Vec<AddonView>> {
if crate::client::is_running(client) {
return Err(Error::Invalid("Close the game before removing addons.".into()));
}
let mut state = read_state(client);
let Some(installed) = state.remove(id) else {
return list(client);
};
let dir = addons_dir(client);
for folder in &installed.folders {
if folder.is_empty() || folder.contains(['/', '\\']) || folder.starts_with('.') {
continue;
}
let target = dir.join(folder);
if target.exists() {
fs::remove_dir_all(&target)?;
}
}
write_state(client, &state)?;
list(client)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::download::Reply;
struct Serve(Vec<u8>);
impl Transport for Serve {
fn get(&self, _: &str, _: u64) -> std::result::Result<Reply, String> {
Ok(Reply { status: 200, content_range: None, body: Box::new(std::io::Cursor::new(self.0.clone())) })
}
}
/// A GitHub-like archive of the catalog entry `id` with the given file bodies (path in repo -> bytes).
fn archive(entry: &Entry, body: &dyn Fn(&AddonFile) -> Vec<u8>) -> Vec<u8> {
let mut out = std::io::Cursor::new(Vec::new());
let mut zip = zip::ZipWriter::new(&mut out);
for file in &entry.files {
zip.start_file(format!("repo-{}/{}", entry.commit, file.from), zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(&body(file)).unwrap();
}
zip.finish().unwrap();
out.into_inner()
}
/// A small catalog entry whose files hash to what the archive will hold.
fn small_entry() -> Entry {
let bytes = b"## Interface: 30300\n".to_vec();
Entry {
id: "t".into(),
name: "T".into(),
description: BTreeMap::new(),
author: "a".into(),
license: "x".into(),
needs: None,
repo: "r".into(),
commit: "c1".into(),
archive: "https://example.invalid/a.zip".into(),
folders: vec!["TestAddon".into()],
files: vec![AddonFile {
path: "TestAddon/TestAddon.toc".into(),
from: "TestAddon/TestAddon.toc".into(),
sha1: git_blob_sha1(&bytes),
size: bytes.len() as u64,
}],
}
}
#[test]
fn the_catalog_is_valid_and_pins_every_addon() {
let entries = catalog().unwrap();
assert!(entries.len() >= 4);
let views = list(Path::new("C:/no-such-client")).unwrap();
assert_eq!(views.iter().filter(|v| v.bundled.is_some()).count(), 1, "the dev menu is shown, not installable");
for e in &entries {
assert_eq!(e.commit.len(), 40, "{}", e.id);
assert!(e.archive.starts_with("https://codeload.github.com/") && e.archive.ends_with(&e.commit));
assert!(e.description.contains_key("en") && e.description.contains_key("ru"));
for folder in &e.folders {
assert!(e.files.iter().any(|f| f.path.eq_ignore_ascii_case(&format!("{folder}/{folder}.toc"))
|| f.path.starts_with(&format!("{folder}/")) && f.path.ends_with(".toc")), "{folder} has no toc");
}
for f in &e.files {
fsx::safe_join(Path::new("C:/x"), &f.path).unwrap();
assert!(e.folders.iter().any(|folder| f.path.starts_with(&format!("{folder}/"))));
}
}
}
#[test]
#[ignore = "Downloads the catalog's small addons from GitHub"]
fn the_real_catalog_installs_from_github() {
let client = tempfile::tempdir().unwrap();
for id in ["incahoots", "coacombat", "squidbots-lite"] {
let views = install(client.path(), id, &Cancel::default()).unwrap();
let view = views.iter().find(|v| v.id == id).unwrap();
assert!(view.installed && !view.update_available, "{id}");
}
assert!(addons_dir(client.path()).join("IncahootsCoA/IncahootsCoA.toc").is_file());
assert!(addons_dir(client.path()).join("SquidBotsLite/SquidBotsLite.toc").is_file());
}
#[test]
fn git_blob_ids_match_git() {
// `printf 'hello\n' | git hash-object --stdin`
assert_eq!(git_blob_sha1(b"hello\n"), "ce013625030ba8dba906f756967f9e9ca394464a");
}
fn unpack_and_place(entry: &Entry, client: &Path, bytes: Vec<u8>) -> Result<()> {
let dir = addons_dir(client);
fs::create_dir_all(&dir)?;
let staging = tempfile::tempdir_in(&dir)?;
let archive = staging.path().join("a.zip");
download(&Serve(bytes), &entry.archive, &archive, &Cancel::default())?;
unpack(entry, &archive, &staging.path().join("files"))
}
#[test]
fn a_tampered_or_incomplete_download_is_refused() {
let client = tempfile::tempdir().unwrap();
let entry = small_entry();
let good = archive(&entry, &|_| b"## Interface: 30300\n".to_vec());
unpack_and_place(&entry, client.path(), good).unwrap();
let bad = archive(&entry, &|_| b"## Interface: 30300 evil\n".to_vec());
assert!(unpack_and_place(&entry, client.path(), bad).unwrap_err().to_string().contains("verification"));
let empty = {
let mut out = std::io::Cursor::new(Vec::new());
zip::ZipWriter::new(&mut out).finish().unwrap();
out.into_inner()
};
assert!(unpack_and_place(&entry, client.path(), empty).unwrap_err().to_string().contains("lacks"));
}
#[test]
fn install_update_and_remove_keep_other_folders() {
let client = tempfile::tempdir().unwrap();
let mut entry = small_entry();
let dir = addons_dir(client.path());
fs::create_dir_all(dir.join("TestAddon")).unwrap();
fs::write(dir.join("TestAddon/mine.lua"), b"player's own").unwrap();
fs::create_dir_all(dir.join("Other")).unwrap();
let serve = Serve(archive(&entry, &|_| b"## Interface: 30300
".to_vec()));
install_entry(&serve, client.path(), &entry, &Cancel::default()).unwrap();
assert!(dir.join("TestAddon/TestAddon.toc").is_file());
assert!(!dir.join("TestAddon/mine.lua").exists(), "the player's folder was set aside");
let backups: Vec<_> = fs::read_dir(dir.join(BACKUP)).unwrap().flatten().collect();
assert_eq!(backups.len(), 1);
assert!(backups[0].path().join("mine.lua").is_file());
assert_eq!(read_state(client.path())["t"].commit, "c1");
// An update replaces the Manager's own folder without another backup.
entry.commit = "c2".into();
install_entry(&serve, client.path(), &entry, &Cancel::default()).unwrap();
assert_eq!(read_state(client.path())["t"].commit, "c2");
assert_eq!(fs::read_dir(dir.join(BACKUP)).unwrap().count(), 1);
// A failed download changes nothing.
let broken = Serve(archive(&entry, &|_| b"tampered".to_vec()));
assert!(install_entry(&broken, client.path(), &entry, &Cancel::default()).is_err());
assert!(dir.join("TestAddon/TestAddon.toc").is_file());
remove(client.path(), "t").unwrap();
assert!(!dir.join("TestAddon").exists());
assert!(dir.join("Other").is_dir());
assert!(!read_state(client.path()).contains_key("t"));
// No leftover staging folders.
assert!(fs::read_dir(&dir).unwrap().flatten().all(|e| !e.file_name().to_string_lossy().starts_with(".coa-addon-")));
}
}
+5
View File
@@ -1032,8 +1032,13 @@ pub fn analyze_github_repository(url_str: &str, cache_base_dir: Option<&Path>) -
let marker_file = commit_cache_dir.join(".coa_tree_digest"); let marker_file = commit_cache_dir.join(".coa_tree_digest");
let cache_hit = commit_cache_dir.exists() && marker_file.exists(); let cache_hit = commit_cache_dir.exists() && marker_file.exists();
let pinned = resolved_commit.len() == 40 && resolved_commit.bytes().all(|b| b.is_ascii_hexdigit());
let target_dir = if cache_hit { let target_dir = if cache_hit {
commit_cache_dir commit_cache_dir
} else if pinned {
// GitHub's archive of the exact commit, without the game-client files some repositories carry (they
// are not needed to build a server module and used to push such repositories over the size limit).
crate::module_sources::fetch_pinned(&owner, &repo, &resolved_commit, &base_cache)?
} else { } else {
// Clone into temporary staging directory // Clone into temporary staging directory
let staging_id = format!("{}_{}_{}", owner, repo, std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis()); let staging_id = format!("{}_{}_{}", owner, repo, std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis());
+6 -2
View File
@@ -308,7 +308,9 @@ pub fn with_database<T>(root: &Path, f: impl FnOnce(&Db) -> Result<T>) -> Result
let db = Db::from_repack(root, Account::Admin)?; let db = Db::from_repack(root, Account::Admin)?;
let observed = process::observe(root, &read_ports(root)); let observed = process::observe(root, &read_ports(root));
let mysql_was_up = observed.mysql.state == ServiceState::Running && db.ping(); let mysql_was_up = observed.mysql.state == ServiceState::Running && db.ping();
if !mysql_was_up { let db = if mysql_was_up {
db
} else {
let out = driver::run(root, Verb::StartMysql)?; let out = driver::run(root, Verb::StartMysql)?;
if !out.ok { if !out.ok {
// Keep the launcher's own last lines: the title alone ("Something went wrong") hides the cause. // Keep the launcher's own last lines: the title alone ("Something went wrong") hides the cause.
@@ -317,7 +319,9 @@ pub fn with_database<T>(root: &Path, f: impl FnOnce(&Db) -> Result<T>) -> Result
driver::startup_failure(root, &out) driver::startup_failure(root, &out)
))); )));
} }
} // The start may have moved the database to another port (a reserved or taken one).
Db::from_repack(root, Account::Admin)?
};
let result = f(&db); let result = f(&db);
if !mysql_was_up { if !mysql_was_up {
let now = process::observe(root, &read_ports(root)); let now = process::observe(root, &read_ports(root));
+355
View File
@@ -0,0 +1,355 @@
//! The compiler's work kept between builds of a module, small on disk.
//!
//! A build works in `<meta>/build-cache/<module>/` (sources, CMake/Ninja state, object files). Between builds
//! that folder is packed into `<module>.tar.zst` (object files compress about 4x) with a small record of the
//! core it was made for. Before the next build it is unpacked again, so Ninja recompiles only what changed.
//!
//! Object files made for another core are useless (every one depends on the core's headers), so a cache for
//! another core is dropped instead of unpacked. The whole cache is kept under a size limit: the modules built
//! longest ago go first. Incremental-link files (`.ilk`) and linker maps are not kept.
use std::fs;
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
/// Packed caches above this total are removed, the least recently built first.
pub const LIMIT_BYTES: u64 = 2 * 1024 * 1024 * 1024;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Record {
/// ABI fingerprint of the core the objects were compiled for.
fingerprint: String,
/// Unix seconds of the last build.
last_used: i64,
unpacked_bytes: u64,
}
#[derive(Debug, Clone, Serialize)]
pub struct CacheEntry {
pub module: String,
/// Bytes on disk now (packed, or unpacked while a build runs).
pub bytes: u64,
pub unpacked_bytes: u64,
pub last_used: i64,
/// Made for the core this server runs now (otherwise it is dropped at the next build).
pub current_core: bool,
pub packed: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct CacheSummary {
pub modules: Vec<CacheEntry>,
pub total_bytes: u64,
/// Downloaded module sources (shared by all servers of this Manager).
pub sources_bytes: u64,
pub limit_bytes: u64,
}
pub fn root(realm_root: &Path) -> PathBuf {
crate::plugins::meta_dir(realm_root).join("build-cache")
}
fn valid_id(id: &str) -> bool {
!id.is_empty() && id.len() <= 100 && id.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')) && id != "." && id != ".."
}
fn paths(realm_root: &Path, id: &str) -> Result<(PathBuf, PathBuf, PathBuf)> {
if !valid_id(id) {
return Err(Error::Invalid(format!("Invalid module id for the build cache: {id}")));
}
let r = root(realm_root);
Ok((r.join(id), r.join(format!("{id}.tar.zst")), r.join(format!("{id}.json"))))
}
fn now() -> i64 {
chrono::Utc::now().timestamp()
}
pub(crate) fn dir_bytes(dir: &Path) -> u64 {
fs::read_dir(dir)
.map(|rd| {
rd.flatten()
.map(|e| match e.metadata() {
Ok(m) if m.is_dir() => dir_bytes(&e.path()),
Ok(m) => m.len(),
Err(_) => 0,
})
.sum()
})
.unwrap_or(0)
}
fn read_record(path: &Path) -> Option<Record> {
serde_json::from_slice(&fs::read(path).ok()?).ok()
}
/// The working folder for a build of `id` for the core `fingerprint`, with the earlier work of a build for the
/// same core unpacked into it. A cache for another core is dropped.
pub fn prepare(realm_root: &Path, id: &str, fingerprint: &str) -> Result<PathBuf> {
let (dir, archive, record) = paths(realm_root, id)?;
let rec = read_record(&record);
let same_core = rec.as_ref().is_some_and(|r| r.fingerprint == fingerprint);
if !same_core {
// made for another core (or unknown): its objects would all be rebuilt anyway
if dir.exists() {
fs::remove_dir_all(&dir)?;
}
let _ = fs::remove_file(&archive);
let _ = fs::remove_file(&record);
} else if archive.is_file() {
if dir.exists() {
fs::remove_dir_all(&dir)?; // an older, interrupted working folder: the archive is the newer state
}
unpack(&archive, &dir).map_err(|e| {
let _ = fs::remove_dir_all(&dir);
Error::Invalid(format!("The build cache of {id} could not be unpacked: {e}"))
})?;
fs::remove_file(&archive)?;
}
fs::create_dir_all(&dir)?;
let unpacked_bytes = dir_bytes(&dir);
crate::fsx::atomic_write_json(&record, &Record { fingerprint: fingerprint.into(), last_used: now(), unpacked_bytes })?;
Ok(dir)
}
/// After a build (successful or not): drop what does not speed up the next one, pack the rest, keep the total
/// under the limit. Never fails the build: problems only cost the cache.
pub fn finish(realm_root: &Path, id: &str) {
let Ok((dir, archive, record)) = paths(realm_root, id) else { return };
if !dir.is_dir() {
return;
}
remove_by_ext(&dir, &["ilk", "map"]);
let unpacked_bytes = dir_bytes(&dir);
let tmp = archive.with_extension("zst.partial");
match pack(&dir, &tmp).and_then(|_| fs::rename(&tmp, &archive).map_err(Into::into)) {
Ok(()) => {
let _ = fs::remove_dir_all(&dir);
if let Some(mut r) = read_record(&record) {
r.last_used = now();
r.unpacked_bytes = unpacked_bytes;
let _ = crate::fsx::atomic_write_json(&record, &r);
}
}
Err(e) => {
tracing::warn!(module = id, error = %e, "build cache not packed; it is kept unpacked");
let _ = fs::remove_file(&tmp);
}
}
enforce_limit(realm_root, LIMIT_BYTES, Some(id));
}
fn remove_by_ext(dir: &Path, exts: &[&str]) {
for e in fs::read_dir(dir).into_iter().flatten().flatten() {
let p = e.path();
if p.is_dir() {
remove_by_ext(&p, exts);
} else if p.extension().and_then(|x| x.to_str()).is_some_and(|x| exts.iter().any(|w| w.eq_ignore_ascii_case(x))) {
let _ = fs::remove_file(p);
}
}
}
fn pack(dir: &Path, out: &Path) -> Result<()> {
let file = BufWriter::new(fs::File::create(out)?);
let mut enc = zstd::Encoder::new(file, 3)?;
let _ = enc.multithread(std::thread::available_parallelism().map(|n| n.get() as u32).unwrap_or(1).min(8));
{
let mut tar = tar::Builder::new(&mut enc);
tar.follow_symlinks(false);
// modification times are kept: Ninja decides from them what is up to date
tar.mode(tar::HeaderMode::Complete);
tar.append_dir_all(".", dir)?;
tar.finish()?;
}
enc.finish()?;
Ok(())
}
fn unpack(archive: &Path, dir: &Path) -> Result<()> {
fs::create_dir_all(dir)?;
let dec = zstd::Decoder::new(BufReader::new(fs::File::open(archive)?))?;
let mut tar = tar::Archive::new(dec);
tar.set_preserve_mtime(true);
for entry in tar.entries()? {
let mut entry = entry?;
// unpack_in refuses paths that would leave `dir`
if !entry.unpack_in(dir)? {
return Err(Error::PathRejected(format!("build cache entry {}", entry.path()?.display())));
}
}
Ok(())
}
/// Everything in the build cache of this server, largest first.
pub fn summary(realm_root: &Path, sources_dir: Option<&Path>) -> CacheSummary {
let current = crate::provenance::realm_host_fingerprint(realm_root);
let r = root(realm_root);
let mut modules = Vec::new();
for e in fs::read_dir(&r).into_iter().flatten().flatten() {
let name = e.file_name().to_string_lossy().into_owned();
let Some(id) = name.strip_suffix(".json") else { continue };
let Ok((dir, archive, record)) = paths(realm_root, id) else { continue };
let Some(rec) = read_record(&record) else { continue };
let packed = archive.is_file();
let bytes = if packed { fs::metadata(&archive).map(|m| m.len()).unwrap_or(0) } else { dir_bytes(&dir) };
modules.push(CacheEntry {
module: id.to_string(),
bytes,
unpacked_bytes: rec.unpacked_bytes,
last_used: rec.last_used,
current_core: current.as_deref() == Some(rec.fingerprint.as_str()),
packed,
});
}
// working folders of an older Manager, without a record
for e in fs::read_dir(&r).into_iter().flatten().flatten() {
let name = e.file_name().to_string_lossy().into_owned();
if e.path().is_dir() && valid_id(&name) && !modules.iter().any(|m| m.module == name) {
let bytes = dir_bytes(&e.path());
modules.push(CacheEntry { module: name, bytes, unpacked_bytes: bytes, last_used: 0, current_core: false, packed: false });
}
}
modules.sort_by(|a, b| b.bytes.cmp(&a.bytes));
CacheSummary {
total_bytes: modules.iter().map(|m| m.bytes).sum(),
sources_bytes: sources_dir.map(dir_bytes).unwrap_or(0),
limit_bytes: LIMIT_BYTES,
modules,
}
}
/// Remove the cache of one module, or of all. The next build of a module compiles it in full.
pub fn clear(realm_root: &Path, module: Option<&str>) -> Result<u64> {
let before = summary(realm_root, None).total_bytes;
match module {
Some(id) => {
let (dir, archive, record) = paths(realm_root, id)?;
if dir.exists() {
fs::remove_dir_all(dir)?;
}
let _ = fs::remove_file(archive);
let _ = fs::remove_file(record);
}
None => {
let r = root(realm_root);
if r.exists() {
fs::remove_dir_all(&r)?;
}
}
}
Ok(before.saturating_sub(summary(realm_root, None).total_bytes))
}
/// Keep the packed caches under `limit`, dropping the least recently built (never `keep`).
fn enforce_limit(realm_root: &Path, limit: u64, keep: Option<&str>) {
let mut s = summary(realm_root, None);
s.modules.sort_by_key(|m| m.last_used);
let mut total = s.total_bytes;
for m in &s.modules {
if total <= limit {
break;
}
if Some(m.module.as_str()) == keep {
continue;
}
if clear(realm_root, Some(&m.module)).is_ok() {
tracing::info!(module = %m.module, bytes = m.bytes, "build cache over its limit: oldest removed");
total = total.saturating_sub(m.bytes);
}
}
}
/// Downloaded module sources the Manager keeps (`<cache>/github`, `<cache>/staging`).
pub fn clear_sources(sources_dir: &Path) -> Result<u64> {
let before = dir_bytes(sources_dir);
for sub in ["github", "staging"] {
let p = sources_dir.join(sub);
if p.exists() {
fs::remove_dir_all(p)?;
}
}
Ok(before.saturating_sub(dir_bytes(sources_dir)))
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{Duration, SystemTime};
fn server() -> tempfile::TempDir {
let d = tempfile::tempdir().unwrap();
fs::create_dir_all(d.path().join(".manager")).unwrap();
d
}
#[test]
fn a_cache_is_packed_between_builds_and_comes_back_with_its_times() {
let s = server();
let dir = prepare(s.path(), "companions", "fp1").unwrap();
fs::create_dir_all(dir.join("build/obj")).unwrap();
fs::write(dir.join("build/obj/a.obj"), vec![7u8; 100_000]).unwrap();
fs::write(dir.join("build/x.ilk"), b"incremental link").unwrap();
let old = SystemTime::now() - Duration::from_secs(3600);
fs::File::options().write(true).open(dir.join("build/obj/a.obj")).unwrap().set_modified(old).unwrap();
finish(s.path(), "companions");
assert!(!dir.exists(), "packed between builds");
let sum = summary(s.path(), None);
assert_eq!(sum.modules.len(), 1);
assert!(sum.modules[0].packed && sum.modules[0].bytes < 100_000 / 4, "compressed: {}", sum.modules[0].bytes);
let dir = prepare(s.path(), "companions", "fp1").unwrap();
assert_eq!(fs::read(dir.join("build/obj/a.obj")).unwrap().len(), 100_000);
assert!(!dir.join("build/x.ilk").exists(), "incremental-link files are not kept");
let mtime = fs::metadata(dir.join("build/obj/a.obj")).unwrap().modified().unwrap();
let drift = mtime.duration_since(old).unwrap_or_else(|e| e.duration());
assert!(drift < Duration::from_secs(2), "modification time kept (drift {drift:?})");
}
#[test]
fn a_cache_for_another_core_is_dropped() {
let s = server();
let dir = prepare(s.path(), "scaling", "old-core").unwrap();
fs::write(dir.join("a.obj"), b"old").unwrap();
finish(s.path(), "scaling");
let dir = prepare(s.path(), "scaling", "new-core").unwrap();
assert!(!dir.join("a.obj").exists());
}
#[test]
fn clear_removes_one_module_or_all_and_bad_ids_are_refused() {
let s = server();
for id in ["a", "b"] {
let d = prepare(s.path(), id, "fp").unwrap();
fs::write(d.join("x.obj"), b"x").unwrap();
finish(s.path(), id);
}
clear(s.path(), Some("a")).unwrap();
assert_eq!(summary(s.path(), None).modules.iter().map(|m| m.module.as_str()).collect::<Vec<_>>(), ["b"]);
clear(s.path(), None).unwrap();
assert!(summary(s.path(), None).modules.is_empty());
assert!(clear(s.path(), Some("../x")).is_err());
}
#[test]
fn the_least_recently_built_go_first_over_the_limit() {
let s = server();
for (n, id) in ["old", "new"].iter().enumerate() {
let d = prepare(s.path(), id, "fp").unwrap();
fs::write(d.join("x.obj"), vec![n as u8; 50_000]).unwrap();
finish(s.path(), id);
let rec = root(s.path()).join(format!("{id}.json"));
let mut r = read_record(&rec).unwrap();
r.last_used = 1000 + n as i64;
crate::fsx::atomic_write_json(&rec, &r).unwrap();
}
let total = summary(s.path(), None).total_bytes;
enforce_limit(s.path(), total - 1, None);
let left: Vec<String> = summary(s.path(), None).modules.into_iter().map(|m| m.module).collect();
assert_eq!(left, ["new"]);
}
}
+26 -5
View File
@@ -977,8 +977,25 @@ pub fn build_module_with_bundle(
.unwrap_or_else(|| with_plugin_id(&adapter_source, &plugin_id)); .unwrap_or_else(|| with_plugin_id(&adapter_source, &plugin_id));
// 2. Prepare the persistent staging directory // 2. Prepare the persistent staging directory
let default_staging = realm_root.join(".manager").join("build-cache").join(&module_id); // The Manager's own builds use the module's build cache (unpacked for the build, packed again afterwards);
let staging_dir = custom_staging_dir.map(PathBuf::from).unwrap_or(default_staging); // a caller-chosen folder is used as it is.
let staging_dir = match custom_staging_dir {
Some(p) => PathBuf::from(p),
None => crate::build_cache::prepare(realm_root, &module_id, &sdk_abi_fingerprint(Path::new(&sdk_dir))?)?,
};
struct PackAfter<'a>(Option<(&'a Path, String)>);
impl Drop for PackAfter<'_> {
fn drop(&mut self) {
if let Some((root, id)) = self.0.take() {
crate::build_cache::finish(root, &id);
}
}
}
let _pack_after = PackAfter(custom_staging_dir.is_none().then(|| (realm_root, module_id.clone())));
let log_path = match custom_staging_dir {
Some(_) => staging_dir.join("build.log"),
None => crate::build_cache::root(realm_root).join(format!("{module_id}.log")),
};
fs::create_dir_all(&staging_dir)?; fs::create_dir_all(&staging_dir)?;
let staging_src = staging_dir.join("src"); let staging_src = staging_dir.join("src");
fs::create_dir_all(&staging_src)?; fs::create_dir_all(&staging_src)?;
@@ -1142,11 +1159,11 @@ if errorlevel 1 exit /b 103
} }
// The full log stays next to the build, so a failure can always be read afterwards. // The full log stays next to the build, so a failure can always be read afterwards.
let _ = fs::write(staging_dir.join("build.log"), all_logs.join(" let _ = fs::write(&log_path, all_logs.join("
")); "));
let mut errors = parse_msvc_errors(&all_logs); let mut errors = parse_msvc_errors(&all_logs);
if exit_code != 0 && errors.is_empty() { if exit_code != 0 && errors.is_empty() {
errors.push(unparsed_failure(exit_code, &all_logs, &staging_dir.join("build.log"))); errors.push(unparsed_failure(exit_code, &all_logs, &log_path));
} }
let expected_dll_path = build_output_dir.join(format!("{}.dll", safe_target_name)); let expected_dll_path = build_output_dir.join(format!("{}.dll", safe_target_name));
@@ -1332,7 +1349,11 @@ mod tests {
#[test] #[test]
fn test_detect_toolchain() { fn test_detect_toolchain() {
let tc = detect_toolchain_for(Some(Path::new("C:/games/coa-work/plugin-spike/server-v03"))); let realm = Path::new("C:/games/coa-work/plugin-spike/server-v03");
if !realm.join("worldserver.exe").is_file() {
return; // needs the local spike realm and its SDK (not on CI runners)
}
let tc = detect_toolchain_for(Some(realm));
assert!(tc.msvc_available, "MSVC vcvars64.bat should be detected"); assert!(tc.msvc_available, "MSVC vcvars64.bat should be detected");
assert!(tc.cmake_available, "CMake should be detected"); assert!(tc.cmake_available, "CMake should be detected");
assert!(tc.ninja_available, "Ninja should be detected"); assert!(tc.ninja_available, "Ninja should be detected");
+235 -81
View File
@@ -170,6 +170,178 @@ pub struct PublicCatalogueItem {
pub coa_status_source: String, // "Overlay", "VerifiedCatalog", "AutoPublic" pub coa_status_source: String, // "Overlay", "VerifiedCatalog", "AutoPublic"
pub conflict_reason: Option<String>, pub conflict_reason: Option<String>,
pub is_coa_recommended: bool, pub is_coa_recommended: bool,
/// Functional category code (see `coa-module-categories.json`); "other" when nothing fits.
#[serde(default)]
pub category: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CategoryInfo {
pub code: String,
pub ru: String,
pub en: String,
#[serde(default)]
pub keywords: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
struct CategoryDocument {
categories: Vec<CategoryInfo>,
/// Owner-reviewed assignments (lowercase `owner/repo` -> category code).
repos: BTreeMap<String, String>,
}
static EMBEDDED_CATEGORIES_JSON: &str = include_str!("coa-module-categories.json");
fn category_document() -> &'static CategoryDocument {
static DOC: std::sync::OnceLock<CategoryDocument> = std::sync::OnceLock::new();
DOC.get_or_init(|| {
serde_json::from_str(EMBEDDED_CATEGORIES_JSON).unwrap_or(CategoryDocument { categories: Vec::new(), repos: BTreeMap::new() })
})
}
pub fn categories() -> Vec<CategoryInfo> {
category_document().categories.clone()
}
/// Lowercase words of a text: letters and digits, so `mod-pvp-titles` gives `mod`, `pvp`, `titles`.
fn words(text: &str) -> Vec<String> {
text.to_lowercase()
.split(|c: char| !c.is_alphanumeric() && c != '+')
.filter(|w| !w.is_empty())
.map(str::to_string)
.collect()
}
fn keyword_hits(keyword: &str, text_words: &[String]) -> usize {
let kw = words(keyword);
match kw.as_slice() {
[] => 0,
// one word: whole-word match; words of 4+ letters also match their plural or longer forms
[k] => text_words.iter().filter(|w| *w == k || (k.chars().count() >= 4 && w.starts_with(k.as_str()))).count(),
// a phrase: its words in a row
many => text_words.windows(many.len()).filter(|win| win.iter().zip(many).all(|(a, b)| a == b)).count(),
}
}
/// The category of a module: the owner-reviewed list first, then the keywords of each category against the
/// repository's name (counted twice), description and topics; "other" when no keyword matches.
pub fn categorize(slug: &str, name: &str, description: &str, topics: &[String]) -> String {
let doc = category_document();
if let Some(code) = doc.repos.get(&normalize_repo_slug(slug)) {
return code.clone();
}
let name_words = words(name);
let mut body = words(description);
for t in topics.iter().filter(|t| !t.starts_with("azerothcore") && !matches!(t.as_str(), "wow" | "wotlk" | "world-of-warcraft")) {
body.extend(words(t));
}
let mut best: Option<(&str, usize)> = None;
for c in &doc.categories {
let score: usize = c.keywords.iter().map(|k| 2 * keyword_hits(k, &name_words) + keyword_hits(k, &body)).sum();
if score > 0 && best.is_none_or(|(_, s)| score > s) {
best = Some((&c.code, score));
}
}
best.map(|(c, _)| c.to_string()).unwrap_or_else(|| "other".into())
}
/// Statuses and categories come from the current overlay and category list on every read, never from what a
/// cache stored: a corrected overlay must show at once. Entries an earlier version made up for overlay rules the
/// public catalogue does not contain (marked with the `coa-verified` topic) are dropped.
fn finalize(items: Vec<PublicCatalogueItem>, overlay: &CoaOverlayDocument) -> Vec<PublicCatalogueItem> {
let rules: BTreeMap<String, &CoaOverlayRule> =
overlay.rules.iter().map(|r| (normalize_repo_slug(&r.repo_slug), r)).collect();
items
.into_iter()
.filter(|i| !i.topics.iter().any(|t| t == "coa-verified"))
.map(|mut i| {
let slug = normalize_repo_slug(&i.full_name);
match rules.get(&slug) {
Some(rule) => {
i.coa_status = rule.status;
i.coa_status_source = "Overlay".into();
i.conflict_reason = rule.reason.clone();
i.is_coa_recommended = rule.is_recommended || rule.status == CoaCompatibilityStatus::Recommended;
}
None => {
i.coa_status = CoaCompatibilityStatus::Unverified;
i.coa_status_source = "AutoPublic".into();
i.conflict_reason = None;
i.is_coa_recommended = false;
}
}
i.category = categorize(&slug, &i.name, &i.description, &i.topics);
i
})
.collect()
}
fn respond(items: Vec<PublicCatalogueItem>, overlay: &CoaOverlayDocument, cached_at: String, is_offline: bool, etag: Option<String>) -> PublicCatalogueResponse {
let items = finalize(items, overlay);
PublicCatalogueResponse {
total_count: items.len(),
recommended_count: items.iter().filter(|i| i.is_coa_recommended).count(),
cached_at,
is_offline,
etag,
items,
categories: categories().into_iter().map(|c| CategoryInfo { keywords: Vec::new(), ..c }).collect(),
}
}
/// Last push (`pushed_at`) of GitHub repositories given as `owner/name`, for modules the public catalogue does
/// not list. Kept in `cache_file` for 12 hours; a repository GitHub does not answer for is `None` and asked
/// again next time.
pub fn repo_last_push(cache_file: &Path, repos: &[String]) -> Result<BTreeMap<String, Option<String>>> {
#[derive(Serialize, Deserialize, Default)]
struct Cache {
at: i64,
pushed: BTreeMap<String, String>,
}
let now = Utc::now().timestamp();
let mut cache: Cache = fs::read(cache_file).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default();
if now - cache.at > 12 * 3600 {
cache = Cache { at: now, pushed: BTreeMap::new() };
}
let name_ok = |s: Option<&str>| {
s.is_some_and(|s| !s.is_empty() && s.len() <= 100 && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')))
};
let valid = |r: &str| {
let mut parts = r.split('/');
name_ok(parts.next()) && name_ok(parts.next()) && parts.next().is_none()
};
let client = reqwest::blocking::Client::builder()
.connect_timeout(Duration::from_secs(CONNECT_TIMEOUT_SECS))
.timeout(Duration::from_secs(10))
.build()
.map_err(|e| Error::Invalid(e.to_string()))?;
let mut out = BTreeMap::new();
for repo in repos.iter().map(|r| r.trim().to_ascii_lowercase()).filter(|r| valid(r)) {
if let Some(v) = cache.pushed.get(&repo) {
out.insert(repo, Some(v.clone()));
continue;
}
let pushed = client
.get(format!("https://api.github.com/repos/{repo}"))
.header(reqwest::header::USER_AGENT, "CSM-Modules-Preview")
.header(reqwest::header::ACCEPT, "application/vnd.github+json")
.send()
.ok()
.filter(|r| r.status().is_success())
.and_then(|r| r.text().ok())
.and_then(|t| serde_json::from_str::<serde_json::Value>(&t).ok())
.and_then(|v| v.get("pushed_at")?.as_str().map(str::to_string));
if let Some(p) = &pushed {
cache.pushed.insert(repo.clone(), p.clone());
}
out.insert(repo, pushed);
}
if let Some(dir) = cache_file.parent() {
let _ = fs::create_dir_all(dir);
}
let _ = fsx::atomic_write_json(cache_file, &cache);
Ok(out)
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -191,6 +363,8 @@ pub struct PublicCatalogueResponse {
pub etag: Option<String>, pub etag: Option<String>,
pub total_count: usize, pub total_count: usize,
pub recommended_count: usize, pub recommended_count: usize,
#[serde(default)]
pub categories: Vec<CategoryInfo>,
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -368,44 +542,14 @@ pub fn normalize_catalogue(
coa_status_source: coa_source, coa_status_source: coa_source,
conflict_reason: reason, conflict_reason: reason,
is_coa_recommended: is_rec, is_coa_recommended: is_rec,
category: String::new(),
}); });
} }
// 4. Also ensure any Recommended overlay rules that were not present in the public raw catalog // Overlay rules for repositories the public catalogue does not list are not turned into entries: the
// are included so they are never lost // catalogue shows only what azerothcore.org lists (CoA's own modules are in the Manager's curated list).
for rule in &overlay.rules {
let slug = normalize_repo_slug(&rule.repo_slug);
if seen_slugs.insert(slug.clone()) {
let parts: Vec<&str> = slug.split('/').collect();
let (owner, name) = if parts.len() >= 2 {
(parts[0].to_string(), parts[1].to_string())
} else {
("corfirean".to_string(), slug.clone())
};
normalized.push(PublicCatalogueItem { // 4. Sort:
id: rule.repo_id.unwrap_or(0),
name: name.clone(),
full_name: slug.clone(),
owner,
html_url: format!("https://github.com/{}", slug),
description: rule.reason.clone().unwrap_or_default(),
stars: 0,
forks: 0,
default_branch: "master".to_string(),
pushed_at: None,
updated_at: None,
license: Some("AGPL-3.0".to_string()),
topics: vec!["azerothcore-module".to_string(), "coa-verified".to_string()],
coa_status: rule.status,
coa_status_source: "Overlay".to_string(),
conflict_reason: rule.reason.clone(),
is_coa_recommended: rule.is_recommended || rule.status == CoaCompatibilityStatus::Recommended,
});
}
}
// 5. Sort:
// Group A (Recommended/Verified) first, then by Stars desc, then by pushed_at desc // Group A (Recommended/Verified) first, then by Stars desc, then by pushed_at desc
normalized.sort_by(|a, b| { normalized.sort_by(|a, b| {
// Priority 1: Recommended modules first // Priority 1: Recommended modules first
@@ -461,15 +605,7 @@ pub fn fetch_or_load_catalogue(
if let Ok(cached) = serde_json::from_str::<CachedDocument>(&content) { if let Ok(cached) = serde_json::from_str::<CachedDocument>(&content) {
let age = now_ts - cached.timestamp; let age = now_ts - cached.timestamp;
if age < DEFAULT_CACHE_TTL_SECS && !cached.items.is_empty() { if age < DEFAULT_CACHE_TTL_SECS && !cached.items.is_empty() {
let rec_count = cached.items.iter().filter(|i| i.is_coa_recommended).count(); return Ok(respond(cached.items, &overlay, cached.cached_at, false, cached.etag));
return Ok(PublicCatalogueResponse {
total_count: cached.items.len(),
recommended_count: rec_count,
cached_at: cached.cached_at,
is_offline: false,
etag: cached.etag,
items: cached.items,
});
} }
} }
} }
@@ -507,15 +643,7 @@ pub fn fetch_or_load_catalogue(
cached.timestamp = now_ts; cached.timestamp = now_ts;
cached.cached_at = Utc::now().to_rfc3339(); cached.cached_at = Utc::now().to_rfc3339();
let _ = fsx::atomic_write_json(&cache_file, &cached); let _ = fsx::atomic_write_json(&cache_file, &cached);
let rec_count = cached.items.iter().filter(|i| i.is_coa_recommended).count(); return Ok(respond(cached.items, &overlay, cached.cached_at, false, cached.etag));
return Ok(PublicCatalogueResponse {
total_count: cached.items.len(),
recommended_count: rec_count,
cached_at: cached.cached_at,
is_offline: false,
etag: cached.etag,
items: cached.items,
});
} }
} }
} }
@@ -540,7 +668,6 @@ pub fn fetch_or_load_catalogue(
})?; })?;
let items = normalize_catalogue(&raw, &overlay); let items = normalize_catalogue(&raw, &overlay);
let rec_count = items.iter().filter(|i| i.is_coa_recommended).count();
let now_str = Utc::now().to_rfc3339(); let now_str = Utc::now().to_rfc3339();
let cached_doc = CachedDocument { let cached_doc = CachedDocument {
@@ -557,14 +684,7 @@ pub fn fetch_or_load_catalogue(
let _ = fs::write(&etag_file, tag); let _ = fs::write(&etag_file, tag);
} }
return Ok(PublicCatalogueResponse { return Ok(respond(items, &overlay, now_str, false, new_etag));
total_count: items.len(),
recommended_count: rec_count,
cached_at: now_str,
is_offline: false,
etag: new_etag,
items,
});
} }
// If non-200 and non-304 HTTP status, fallback to existing cache if available // If non-200 and non-304 HTTP status, fallback to existing cache if available
@@ -585,15 +705,7 @@ fn try_offline_fallback(
if cache_file.is_file() { if cache_file.is_file() {
if let Ok(content) = fs::read_to_string(cache_file) { if let Ok(content) = fs::read_to_string(cache_file) {
if let Ok(cached) = serde_json::from_str::<CachedDocument>(&content) { if let Ok(cached) = serde_json::from_str::<CachedDocument>(&content) {
let rec_count = cached.items.iter().filter(|i| i.is_coa_recommended).count(); return Ok(respond(cached.items, overlay, cached.cached_at, true, cached.etag.or(stored_etag)));
return Ok(PublicCatalogueResponse {
total_count: cached.items.len(),
recommended_count: rec_count,
cached_at: cached.cached_at,
is_offline: true,
etag: cached.etag.or(stored_etag),
items: cached.items,
});
} }
} }
} }
@@ -604,16 +716,7 @@ fn try_offline_fallback(
organizations: BTreeMap::new(), organizations: BTreeMap::new(),
}; };
let items = normalize_catalogue(&empty_raw, overlay); let items = normalize_catalogue(&empty_raw, overlay);
let rec_count = items.iter().filter(|i| i.is_coa_recommended).count(); Ok(respond(items, overlay, Utc::now().to_rfc3339(), true, None))
Ok(PublicCatalogueResponse {
total_count: items.len(),
recommended_count: rec_count,
cached_at: Utc::now().to_rfc3339(),
is_offline: true,
etag: None,
items,
})
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -808,6 +911,7 @@ mod tests {
coa_status_source: "CacheTest".into(), coa_status_source: "CacheTest".into(),
conflict_reason: None, conflict_reason: None,
is_coa_recommended: false, is_coa_recommended: false,
category: String::new(),
} }
]; ];
@@ -833,6 +937,56 @@ mod tests {
let overlay = load_coa_overlay(None); let overlay = load_coa_overlay(None);
let fallback_resp = try_offline_fallback(&cache_file, None, &overlay).expect("fallback must succeed"); let fallback_resp = try_offline_fallback(&cache_file, None, &overlay).expect("fallback must succeed");
assert!(fallback_resp.is_offline); assert!(fallback_resp.is_offline);
assert!(!fallback_resp.items.is_empty(), "Fallback must supply overlay items even on corrupt cache"); // nothing is made up from overlay rules when there is no catalogue to show
assert!(fallback_resp.items.is_empty());
}
fn item(full_name: &str, name: &str, description: &str, topics: &[&str]) -> PublicCatalogueItem {
PublicCatalogueItem {
id: 1,
name: name.into(),
full_name: full_name.into(),
owner: full_name.split('/').next().unwrap().into(),
html_url: format!("https://github.com/{full_name}"),
description: description.into(),
stars: 0,
forks: 0,
default_branch: "master".into(),
pushed_at: None,
updated_at: None,
license: None,
topics: topics.iter().map(|t| t.to_string()).collect(),
coa_status: CoaCompatibilityStatus::Verified,
coa_status_source: "Overlay".into(),
conflict_reason: Some("stale".into()),
is_coa_recommended: true,
category: String::new(),
}
}
#[test]
fn statuses_come_from_the_current_overlay_not_the_cache_and_made_up_entries_are_dropped() {
let overlay = load_coa_overlay(None);
let cached = vec![
item("azerothcore/mod-duel-reset", "mod-duel-reset", "Duel reset module", &["azerothcore-module"]),
item("azerothcore/mod-cfbg", "mod-cfbg", "Cross-faction battlegrounds", &["azerothcore-module", "coa-verified"]),
];
let out = finalize(cached, &overlay);
assert_eq!(out.len(), 1, "the made-up mod-cfbg entry is dropped");
assert_eq!(out[0].coa_status, CoaCompatibilityStatus::Unverified);
assert!(out[0].conflict_reason.is_none() && !out[0].is_coa_recommended);
assert_eq!(out[0].category, "pvp");
}
#[test]
fn modules_get_a_category_from_the_reviewed_list_or_keywords() {
// reviewed list (the owner's file)
assert_eq!(categorize("DustinHendrickson/mod-ollama-chat", "mod-ollama-chat", "", &[]), "bots");
// keywords: whole words, not parts of other words
assert_eq!(categorize("x/mod-arena-points", "mod-arena-points", "Gives arena points", &[]), "pvp");
assert_eq!(categorize("x/mod-guild-chat", "mod-guild-chat", "", &[]), "social");
assert_eq!(categorize("x/mod-maintain", "mod-maintain", "Maintains things", &[]), "other");
assert_eq!(categorize("x/mod-nothing", "mod-nothing", "", &["azerothcore-module".to_string(), "wotlk".to_string()]), "other");
assert!(categories().iter().any(|c| c.code == "other"));
} }
} }
+65 -10
View File
@@ -15,7 +15,8 @@ use crate::fsx;
pub use crate::glue_config; pub use crate::glue_config;
pub const ADDON_NAME: &str = "CoABotUI"; pub const ADDON_NAME: &str = "CoABotUI";
const EXECUTABLES: [&str; 3] = ["Ascension.exe", "Wow.exe", "WoW.exe"]; // CoA Custom Races V2 ships its own client (CoACustomRace.exe) next to Ascension.exe and must be started instead.
const EXECUTABLES: [&str; 4] = ["CoACustomRace.exe", "Ascension.exe", "Wow.exe", "WoW.exe"];
pub fn is_running(client: &Path) -> bool { pub fn is_running(client: &Path) -> bool {
if crate::platform::flavor() == crate::platform::Flavor::Docker { if crate::platform::flavor() == crate::platform::Flavor::Docker {
@@ -71,13 +72,23 @@ fn realm_config(old: &[u8], realm: &str) -> Vec<u8> {
} }
pub fn sync_realm(client: &Path, meta: &Path, mode: crate::realms::Mode) -> Result<bool> { pub fn sync_realm(client: &Path, meta: &Path, mode: crate::realms::Mode) -> Result<bool> {
sync_realm_when_closed(client, meta, mode, is_running(client)) let realm = match mode {
crate::realms::Mode::Coa => "Conquest of Azeroth",
crate::realms::Mode::Wildcard => "Wildcard",
crate::realms::Mode::Reborn => "Warcraft Reborn",
};
sync_realm_when_closed(client, meta, realm, is_running(client))
}
/// [`sync_realm`] with the realm's name as the server shows it (the owner may have renamed it).
pub fn sync_realm_named(client: &Path, meta: &Path, realm: &str) -> Result<bool> {
sync_realm_when_closed(client, meta, realm, is_running(client))
} }
fn sync_realm_when_closed( fn sync_realm_when_closed(
client: &Path, client: &Path,
meta: &Path, meta: &Path,
mode: crate::realms::Mode, realm: &str,
running: bool, running: bool,
) -> Result<bool> { ) -> Result<bool> {
if running { if running {
@@ -89,11 +100,6 @@ fn sync_realm_when_closed(
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Vec::new(), Err(e) if e.kind() == std::io::ErrorKind::NotFound => Vec::new(),
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
}; };
let realm = match mode {
crate::realms::Mode::Coa => "Conquest of Azeroth",
crate::realms::Mode::Wildcard => "Wildcard",
crate::realms::Mode::Reborn => "Warcraft Reborn",
};
let new = realm_config(&old, realm); let new = realm_config(&old, realm);
if new == old { if new == old {
return Ok(true); return Ok(true);
@@ -409,6 +415,38 @@ pub fn install_addon(client: &Path, meta: &Path, source: &Path) -> Result<()> {
Ok(()) Ok(())
} }
/// Remove the game's cache of server data (`Cache/WDB/<locale>/*.wdb`: creatures, items, quests, pages). The client
/// keeps answers it got from a server; after the server's data changes they show old names and stats until the
/// cache is gone. Only `.wdb` files under `Cache/WDB` are removed. Returns how many.
pub fn clear_game_cache(client: &Path) -> Result<usize> {
if is_running(client) {
return Ok(0);
}
let root = client.join("Cache").join("WDB");
let mut removed = 0;
let Ok(locales) = fs::read_dir(&root) else {
return Ok(0);
};
for locale in locales.flatten() {
if !locale.file_type().is_ok_and(|t| t.is_dir()) {
continue;
}
let Ok(files) = fs::read_dir(locale.path()) else {
continue;
};
for file in files.flatten() {
let path = file.path();
let is_wdb = path
.extension()
.is_some_and(|e| e.eq_ignore_ascii_case("wdb"));
if is_wdb && file.file_type().is_ok_and(|t| t.is_file()) && fs::remove_file(&path).is_ok() {
removed += 1;
}
}
}
Ok(removed)
}
/// Start the game client. The caller is responsible for making sure the server is ready first. /// Start the game client. The caller is responsible for making sure the server is ready first.
pub fn launch(client: &Path) -> Result<u32> { pub fn launch(client: &Path) -> Result<u32> {
let exe = let exe =
@@ -607,6 +645,23 @@ pub fn recover_all_realmlist_overrides(meta_roots: &[PathBuf]) -> usize {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn only_the_server_data_cache_is_cleared() {
let d = tempfile::tempdir().unwrap();
let wdb = d.path().join("Cache/WDB/enUS");
fs::create_dir_all(&wdb).unwrap();
fs::write(wdb.join("creaturecache.wdb"), b"x").unwrap();
fs::write(wdb.join("itemcache.WDB"), b"x").unwrap();
fs::write(wdb.join("notes.txt"), b"keep").unwrap();
fs::create_dir_all(d.path().join("Data/Retail/Cache")).unwrap();
fs::write(d.path().join("Data/Retail/Cache/id-1.bin"), b"keep").unwrap();
assert_eq!(clear_game_cache(d.path()).unwrap(), 2);
assert!(wdb.join("notes.txt").is_file());
assert!(d.path().join("Data/Retail/Cache/id-1.bin").is_file());
assert_eq!(clear_game_cache(d.path()).unwrap(), 0);
assert_eq!(clear_game_cache(&d.path().join("missing")).unwrap(), 0);
}
#[cfg(windows)] #[cfg(windows)]
#[test] #[test]
@@ -677,10 +732,10 @@ mod tests {
let meta = d.path().join("meta"); let meta = d.path().join("meta");
let original = b"SET realmName \"Wildcard\"\n"; let original = b"SET realmName \"Wildcard\"\n";
fs::write(c.join("WTF/Config.wtf"), original).unwrap(); fs::write(c.join("WTF/Config.wtf"), original).unwrap();
assert!(!sync_realm_when_closed(&c, &meta, crate::realms::Mode::Coa, true).unwrap()); assert!(!sync_realm_when_closed(&c, &meta, "Conquest of Azeroth", true).unwrap());
assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), original); assert_eq!(fs::read(c.join("WTF/Config.wtf")).unwrap(), original);
assert!(!backup_dir(&meta).exists()); assert!(!backup_dir(&meta).exists());
assert!(sync_realm_when_closed(&c, &meta, crate::realms::Mode::Coa, false).unwrap()); assert!(sync_realm_when_closed(&c, &meta, "Conquest of Azeroth", false).unwrap());
assert_eq!( assert_eq!(
fs::read(c.join("WTF/Config.wtf")).unwrap(), fs::read(c.join("WTF/Config.wtf")).unwrap(),
b"SET realmName \"Conquest of Azeroth\"\n" b"SET realmName \"Conquest of Azeroth\"\n"
-975
View File
@@ -1,975 +0,0 @@
//! Download and update of the game client from the community launcher's public manifest.
//!
//! The manifest lists every file with its size and SHA-256; each file is fetched by hash and verified before it is
//! moved into place (`download::fetch_with`, resumable). The Manager keeps a small state file inside the client
//! folder (`.coa-manager/client-state.json`) so that later checks are cheap and so that files the player changed
//! are recognised: they are never overwritten without being asked, and a replaced file is moved aside, not deleted.
//! WTF, Cache, other addons, logs and `realmlist.wtf` are not part of what the Manager synchronises.
use std::collections::{BTreeMap, HashMap};
use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::{Instant, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::download::{self, Cancel, HttpTransport, Job, Transport};
use crate::error::{Error, Result};
use crate::fsx;
pub const MANIFEST_URL: &str =
"https://launcher-api.coa-development.org/downloads/client/latest.json";
pub const OBJECTS_URL: &str = "https://launcher-api.coa-development.org/downloads/client/objects";
const STATE_DIR: &str = ".coa-manager";
const MAX_MANIFEST_BYTES: u64 = 16 * 1024 * 1024;
const MAX_FILE_BYTES: u64 = 16 * 1024 * 1024 * 1024;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManifestFile {
pub path: String,
pub size: u64,
pub sha256: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct Manifest {
pub version: String,
pub published_at: Option<String>,
pub files: Vec<ManifestFile>,
}
impl Manifest {
pub fn total_bytes(&self) -> u64 {
self.files.iter().map(|f| f.size).sum()
}
}
#[derive(Deserialize)]
struct RawManifest {
schema: u32,
version: String,
#[serde(rename = "publishedAt")]
published_at: Option<String>,
files: Vec<ManifestFile>,
}
/// Things the Manager never writes, even if a manifest lists them: logs, the realmlist, the player's own settings,
/// caches and the companion addon (those have their own safe paths).
fn is_excluded(path: &str) -> bool {
let p = path.replace('\\', "/").to_ascii_lowercase();
p.ends_with(".log")
|| p.rsplit('/').next() == Some("realmlist.wtf")
|| p.starts_with("wtf/")
|| p.starts_with("cache/")
|| p.starts_with("errors/")
|| p.starts_with("logs/")
|| p.starts_with(".coa-manager/")
|| p.starts_with("interface/addons/coabotui/")
}
/// Files players customise: graphics wrappers and their configuration. If one exists and differs from the published
/// file it is left exactly as it is, whatever the "keep my files" choice; it is only created when missing.
fn is_player_owned(path: &str) -> bool {
let name = path.rsplit('/').next().unwrap_or(path).to_ascii_lowercase();
matches!(
name.as_str(),
"d3d8.dll" | "d3d9.dll" | "d3d10core.dll" | "d3d11.dll" | "dxgi.dll" | "dxvk.conf"
) || matches!(
name.rsplit('.').next(),
Some("ini" | "conf" | "cfg" | "wtf")
)
}
pub fn parse_manifest(text: &str) -> Result<Manifest> {
let raw: RawManifest = serde_json::from_str(text)
.map_err(|e| Error::Invalid(format!("The client list could not be read: {e}")))?;
if raw.schema != 1 {
return Err(Error::Invalid(format!(
"The client list uses a newer format ({}); update the Manager.",
raw.schema
)));
}
if raw.version.trim().is_empty() || raw.version.len() > 64 {
return Err(Error::Invalid("The client list has no version.".into()));
}
let root = Path::new("client-root");
let mut files = Vec::with_capacity(raw.files.len());
for f in raw.files {
fsx::safe_join(root, &f.path)?;
let hash_ok = f.sha256.len() == 64 && f.sha256.bytes().all(|b| b.is_ascii_hexdigit());
if !hash_ok || f.size > MAX_FILE_BYTES {
return Err(Error::Invalid(format!(
"The client list has a bad entry for {}.",
f.path
)));
}
if is_excluded(&f.path) {
continue;
}
files.push(ManifestFile {
path: f.path.replace('\\', "/"),
size: f.size,
sha256: f.sha256.to_ascii_lowercase(),
});
}
if files.is_empty() {
return Err(Error::Invalid("The client list is empty.".into()));
}
Ok(Manifest {
version: raw.version,
published_at: raw.published_at,
files,
})
}
pub fn fetch_manifest(transport: &dyn Transport, url: &str) -> Result<Manifest> {
let mut reply = transport
.get(url, 0)
.map_err(|e| Error::Invalid(format!("Could not reach the client download server: {e}")))?;
if reply.status != 200 {
return Err(Error::Invalid(format!(
"The client download server answered {}.",
reply.status
)));
}
let mut text = String::new();
reply
.body
.by_ref()
.take(MAX_MANIFEST_BYTES)
.read_to_string(&mut text)
.map_err(|e| Error::Invalid(format!("Could not read the client list: {e}")))?;
parse_manifest(&text)
}
pub fn fetch_latest() -> Result<Manifest> {
download::check_url(MANIFEST_URL)?;
fetch_manifest(&HttpTransport::with_total_timeout(30)?, MANIFEST_URL)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entry {
pub size: u64,
pub mtime: u64,
pub sha256: String,
/// The player chose to keep this file although the manifest has a different one (this is the manifest hash
/// they were asked about; a newer manifest asks again).
#[serde(default)]
pub kept_against: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct State {
/// The manifest version this folder was brought to; None while a download is unfinished.
pub version: Option<String>,
pub files: BTreeMap<String, Entry>,
}
fn state_path(client: &Path) -> PathBuf {
client.join(STATE_DIR).join("client-state.json")
}
pub fn load_state(client: &Path) -> Option<State> {
fsx::read_json(&state_path(client)).ok()
}
fn save_state(client: &Path, state: &State) -> Result<()> {
fsx::atomic_write_json(&state_path(client), state)
}
#[derive(Debug, Clone, Serialize)]
pub struct Local {
/// The Manager downloaded or adopted this folder (a state file exists).
pub managed: bool,
pub version: Option<String>,
}
pub fn local(client: &Path) -> Local {
let s = load_state(client);
Local {
managed: s.is_some(),
version: s.and_then(|s| s.version),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
/// Not in the folder.
Missing,
/// Still as the Manager left it; the manifest has a newer file.
Changed,
/// Different from the manifest and not as the Manager left it (changed by the player or by something else).
Modified,
}
#[derive(Debug, Clone, Serialize)]
pub struct Item {
pub path: String,
pub size: u64,
pub kind: Kind,
#[serde(skip)]
sha256: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct Plan {
pub version: String,
pub items: Vec<Item>,
pub download_bytes: u64,
pub total_files: usize,
pub up_to_date_files: usize,
pub kept_files: usize,
#[serde(skip)]
confirmed: BTreeMap<String, Entry>,
}
impl Plan {
pub fn modified(&self) -> Vec<&Item> {
self.items
.iter()
.filter(|i| i.kind == Kind::Modified)
.collect()
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Step {
/// "scan" while comparing files, "download" while fetching, "finish" at the end.
pub phase: &'static str,
pub done: u64,
pub total: u64,
pub bytes_per_sec: u64,
pub file: Option<String>,
}
fn cancelled() -> Error {
Error::Invalid("Download cancelled.".into())
}
fn mtime_secs(m: &fs::Metadata) -> u64 {
m.modified()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0)
}
fn hash_file(path: &Path, cancel: &Cancel, on_bytes: &mut dyn FnMut(u64)) -> Result<String> {
let mut f = File::open(path)?;
let mut hasher = Sha256::new();
let mut buf = vec![0u8; 1 << 20];
loop {
if cancel.is_set() {
return Err(cancelled());
}
let n = f.read(&mut buf)?;
if n == 0 {
return Ok(hex::encode(hasher.finalize()));
}
hasher.update(&buf[..n]);
on_bytes(n as u64);
}
}
/// Compare the folder with the manifest. Files are hashed only when their size or modification time no longer
/// matches what the state file recorded, so a routine check of a managed folder is quick.
pub fn plan(
client: &Path,
m: &Manifest,
state: &State,
cancel: &Cancel,
on_step: &dyn Fn(Step),
) -> Result<Plan> {
let total = m.total_bytes();
let mut done = 0u64;
let mut items = Vec::new();
let mut confirmed = BTreeMap::new();
let (mut up_to_date, mut kept) = (0usize, 0usize);
for f in &m.files {
if cancel.is_set() {
return Err(cancelled());
}
on_step(Step {
phase: "scan",
done,
total,
bytes_per_sec: 0,
file: Some(f.path.clone()),
});
if crate::client_graphics::disabled(client, &f.path)? {
kept += 1;
done += f.size;
continue;
}
let path = fsx::safe_join(client, &f.path)?;
let meta = match fs::metadata(&path) {
Ok(meta) if meta.is_file() => meta,
_ => {
items.push(Item {
path: f.path.clone(),
size: f.size,
kind: Kind::Missing,
sha256: f.sha256.clone(),
});
done += f.size;
continue;
}
};
let (size, mtime) = (meta.len(), mtime_secs(&meta));
let entry = state.files.get(&f.path);
let trusted = entry.filter(|e| e.size == size && e.mtime == mtime);
let local: Option<String> = if let Some(e) = trusted {
Some(e.sha256.clone())
} else if size == f.size || entry.map(|e| e.size == size).unwrap_or(false) {
let base = done;
let mut seen = 0u64;
let h = hash_file(&path, cancel, &mut |n| {
seen += n;
on_step(Step {
phase: "scan",
done: base + seen.min(f.size),
total,
bytes_per_sec: 0,
file: Some(f.path.clone()),
});
})?;
Some(h)
} else {
None
};
done += f.size;
if is_player_owned(&f.path) && local.as_deref() != Some(f.sha256.as_str()) {
let sha = match local {
Some(h) => h,
None => hash_file(&path, cancel, &mut |_| {})?,
};
kept += 1;
confirmed.insert(
f.path.clone(),
Entry {
size,
mtime,
sha256: sha,
kept_against: Some(f.sha256.clone()),
},
);
continue;
}
if local.as_deref() == Some(f.sha256.as_str()) {
up_to_date += 1;
confirmed.insert(
f.path.clone(),
Entry {
size,
mtime,
sha256: f.sha256.clone(),
kept_against: None,
},
);
continue;
}
let as_left = entry.filter(|e| local.as_deref() == Some(e.sha256.as_str()));
match as_left {
Some(e) if e.kept_against.as_deref() == Some(f.sha256.as_str()) => {
kept += 1;
confirmed.insert(
f.path.clone(),
Entry {
size,
mtime,
sha256: e.sha256.clone(),
kept_against: e.kept_against.clone(),
},
);
}
Some(e) if e.kept_against.is_none() => {
items.push(Item {
path: f.path.clone(),
size: f.size,
kind: Kind::Changed,
sha256: f.sha256.clone(),
});
}
_ => items.push(Item {
path: f.path.clone(),
size: f.size,
kind: Kind::Modified,
sha256: f.sha256.clone(),
}),
}
}
on_step(Step {
phase: "scan",
done: total,
total,
bytes_per_sec: 0,
file: None,
});
let download_bytes = items.iter().map(|i| i.size).sum();
Ok(Plan {
version: m.version.clone(),
items,
download_bytes,
total_files: m.files.len(),
up_to_date_files: up_to_date,
kept_files: kept,
confirmed,
})
}
pub struct Apply<'a> {
pub client: &'a Path,
pub manifest: &'a Manifest,
pub plan: &'a Plan,
/// Leave files the player changed alone (they are remembered, and asked about again only when the manifest changes).
pub keep_modified: bool,
pub transport: &'a dyn Transport,
pub objects_url: &'a str,
pub cancel: &'a Cancel,
}
fn blocked(path: &str, e: std::io::Error) -> Error {
Error::Invalid(format!("Could not replace {path}: {e}. Close the game and anything else using the client, then try again."))
}
/// Download what the plan lists. Progress is saved after every file, so an interrupted run resumes where it stopped.
pub fn apply(a: &Apply, on_step: &dyn Fn(Step)) -> Result<()> {
let mut state = State {
version: None,
files: a.plan.confirmed.clone(),
};
save_state(a.client, &state)?;
let mut todo = Vec::new();
for item in &a.plan.items {
if crate::client_graphics::disabled(a.client, &item.path)? {
continue;
}
if item.kind == Kind::Modified && a.keep_modified {
let path = fsx::safe_join(a.client, &item.path)?;
let meta = fs::metadata(&path)?;
let sha = hash_file(&path, a.cancel, &mut |_| {})?;
state.files.insert(
item.path.clone(),
Entry {
size: meta.len(),
mtime: mtime_secs(&meta),
sha256: sha,
kept_against: Some(item.sha256.clone()),
},
);
} else {
todo.push(item);
}
}
let total: u64 = todo.iter().map(|i| i.size).sum();
fsx::require_space(a.client, total)?;
let staging = a.client.join(STATE_DIR).join("staging");
let replaced = a
.client
.join(STATE_DIR)
.join("replaced")
.join(chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string());
let mut done = 0u64;
let mut placed: HashMap<&str, PathBuf> = HashMap::new();
for item in todo {
if a.cancel.is_set() {
save_state(a.client, &state)?;
return Err(cancelled());
}
let target = fsx::safe_join(a.client, &item.path)?;
let started = Instant::now();
let staged = staging.join(&item.sha256);
let copy_from = placed.get(item.sha256.as_str()).cloned();
if copy_from.is_none() {
let job = Job {
url: format!("{}/{}", a.objects_url, item.sha256),
dest: staged.clone(),
sha256: item.sha256.clone(),
size: item.size,
};
let base = done;
let result = download::fetch_with(a.transport, &job, a.cancel, &|p| {
on_step(Step {
phase: "download",
done: base + p.downloaded,
total,
bytes_per_sec: p.bytes_per_sec,
file: Some(item.path.clone()),
});
});
if let Err(e) = result {
let _ = save_state(a.client, &state);
return Err(e);
}
}
if let Some(parent) = target.parent() {
fs::create_dir_all(parent)?;
}
if target.exists() {
if item.kind == Kind::Modified {
let keep = fsx::safe_join(&replaced, &item.path)?;
if let Some(parent) = keep.parent() {
fs::create_dir_all(parent)?;
}
fs::rename(&target, &keep).map_err(|e| blocked(&item.path, e))?;
} else {
fs::remove_file(&target).map_err(|e| blocked(&item.path, e))?;
}
}
match copy_from {
Some(src) => {
fs::copy(&src, &target).map_err(|e| blocked(&item.path, e))?;
}
None => fs::rename(&staged, &target).map_err(|e| blocked(&item.path, e))?,
}
placed.insert(&item.sha256, target.clone());
let meta = fs::metadata(&target)?;
state.files.insert(
item.path.clone(),
Entry {
size: meta.len(),
mtime: mtime_secs(&meta),
sha256: item.sha256.clone(),
kept_against: None,
},
);
save_state(a.client, &state)?;
done += item.size;
let secs = started.elapsed().as_secs_f64().max(0.001);
on_step(Step {
phase: "download",
done,
total,
bytes_per_sec: (item.size as f64 / secs) as u64,
file: Some(item.path.clone()),
});
}
state.version = Some(a.manifest.version.clone());
save_state(a.client, &state)?;
let _ = fs::remove_dir_all(&staging);
on_step(Step {
phase: "finish",
done: total,
total,
bytes_per_sec: 0,
file: None,
});
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::download::Reply;
use std::io::Cursor;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
/// Serves objects by hash; remembers what was requested and can fail after N objects.
struct Server {
objects: Mutex<HashMap<String, Vec<u8>>>,
requested: Mutex<Vec<String>>,
cancel_after: Option<(AtomicUsize, Cancel)>,
}
impl Server {
fn new(files: &[(&str, &[u8])]) -> Self {
let s = Server {
objects: Default::default(),
requested: Default::default(),
cancel_after: None,
};
for (_, body) in files {
s.objects
.lock()
.unwrap()
.insert(fsx::sha256_bytes(body), body.to_vec());
}
s
}
}
impl Transport for Server {
fn get(&self, url: &str, from: u64) -> std::result::Result<Reply, String> {
let sha = url.rsplit('/').next().unwrap().to_string();
self.requested.lock().unwrap().push(sha.clone());
if let Some((left, cancel)) = &self.cancel_after {
if left.fetch_sub(1, Ordering::SeqCst) == 1 {
cancel.cancel();
}
}
let body = self
.objects
.lock()
.unwrap()
.get(&sha)
.cloned()
.ok_or("404")?;
let (status, start) = if from > 0 {
(206, from as usize)
} else {
(200, 0)
};
let cr = (from > 0).then(|| format!("bytes {from}-{}/{}", body.len() - 1, body.len()));
Ok(Reply {
status,
content_range: cr,
body: Box::new(Cursor::new(body[start..].to_vec())),
})
}
}
fn manifest(version: &str, files: &[(&str, &[u8])]) -> Manifest {
Manifest {
version: version.into(),
published_at: None,
files: files
.iter()
.map(|(p, b)| ManifestFile {
path: p.to_string(),
size: b.len() as u64,
sha256: fsx::sha256_bytes(b),
})
.collect(),
}
}
const V1: [(&str, &[u8]); 4] = [
("Wow.exe", b"exe-1"),
("Data/common.MPQ", b"common-mpq-data"),
("Data/patch-A.MPQ", b"patch-a-1"),
("Data/enUS/locale.MPQ", b"locale"),
];
fn sync(dir: &Path, m: &Manifest, server: &Server, keep: bool) -> Result<Plan> {
let state = load_state(dir).unwrap_or_default();
let cancel = Cancel::default();
let p = plan(dir, m, &state, &cancel, &|_| {})?;
apply(
&Apply {
client: dir,
manifest: m,
plan: &p,
keep_modified: keep,
transport: server,
objects_url: "https://x.invalid/objects",
cancel: &cancel,
},
&|_| {},
)?;
Ok(p)
}
#[test]
fn manifest_rejects_traversal_and_bad_entries_and_skips_personal_files() {
let ok = r#"{"schema":1,"version":"v1","files":[
{"path":"Wow.exe","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"path":"Data/enUS/realmlist.wtf","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"path":"WTF/Config.wtf","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"path":"Interface/AddOns/CoABotUI/x.lua","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{"path":"MemoryBridge.log","size":3,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#;
let m = parse_manifest(ok).unwrap();
assert_eq!(
m.files.iter().map(|f| f.path.as_str()).collect::<Vec<_>>(),
["Wow.exe"]
);
for bad in [
r#"{"schema":1,"version":"v","files":[{"path":"../evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#,
r#"{"schema":1,"version":"v","files":[{"path":"C:/evil.dll","size":1,"sha256":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}"#,
r#"{"schema":1,"version":"v","files":[{"path":"a.dll","size":1,"sha256":"short"}]}"#,
r#"{"schema":2,"version":"v","files":[]}"#,
r#"{"schema":1,"version":"v","files":[]}"#,
] {
assert!(parse_manifest(bad).is_err(), "{bad}");
}
}
#[test]
fn fresh_download_installs_every_file_verified_and_leaves_nothing_behind() {
let d = tempfile::tempdir().unwrap();
let (m, s) = (manifest("v1", &V1), Server::new(&V1));
let p = sync(d.path(), &m, &s, false).unwrap();
assert_eq!(p.items.len(), 4);
for (path, body) in V1 {
assert_eq!(fs::read(d.path().join(path)).unwrap(), body);
}
let st = load_state(d.path()).unwrap();
assert_eq!(st.version.as_deref(), Some("v1"));
assert_eq!(st.files.len(), 4);
assert!(!d.path().join(".coa-manager/staging").exists());
assert!(local(d.path()).managed);
}
#[test]
fn an_update_downloads_only_what_changed_and_a_second_check_is_empty() {
let d = tempfile::tempdir().unwrap();
sync(d.path(), &manifest("v1", &V1), &Server::new(&V1), false).unwrap();
let mut v2 = V1.to_vec();
v2[2] = ("Data/patch-A.MPQ", b"patch-a-2-longer");
v2.push(("Data/new.MPQ", b"brand new"));
let (m2, s2) = (manifest("v2", &v2), Server::new(&v2));
let p = sync(d.path(), &m2, &s2, false).unwrap();
let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect();
assert_eq!(
kinds,
[
("Data/patch-A.MPQ", Kind::Changed),
("Data/new.MPQ", Kind::Missing)
]
);
assert_eq!(
s2.requested.lock().unwrap().len(),
2,
"unchanged files were not downloaded again"
);
assert_eq!(
fs::read(d.path().join("Data/patch-A.MPQ")).unwrap(),
b"patch-a-2-longer"
);
assert!(
!d.path().join(".coa-manager/replaced").exists(),
"a file the Manager itself wrote needs no backup"
);
let again = sync(d.path(), &m2, &s2, false).unwrap();
assert!(again.items.is_empty() && again.up_to_date_files == 5);
assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v2"));
}
#[test]
fn a_file_the_player_changed_is_asked_about_kept_when_told_to_and_saved_when_replaced() {
let d = tempfile::tempdir().unwrap();
sync(d.path(), &manifest("v1", &V1), &Server::new(&V1), false).unwrap();
fs::write(d.path().join("Wow.exe"), b"my patched exe").unwrap();
let mut v2 = V1.to_vec();
v2[0] = ("Wow.exe", b"exe-2");
let (m2, s2) = (manifest("v2", &v2), Server::new(&v2));
let p = sync(d.path(), &m2, &s2, true).unwrap();
assert_eq!(p.modified().len(), 1);
assert_eq!(
fs::read(d.path().join("Wow.exe")).unwrap(),
b"my patched exe",
"kept"
);
assert!(
s2.requested.lock().unwrap().is_empty(),
"nothing needed downloading"
);
let quiet = sync(d.path(), &m2, &s2, true).unwrap();
assert!(
quiet.items.is_empty() && quiet.kept_files == 1,
"the decision is remembered"
);
let mut v3 = V1.to_vec();
v3[0] = ("Wow.exe", b"exe-3");
let (m3, s3) = (manifest("v3", &v3), Server::new(&v3));
let again = sync(d.path(), &m3, &s3, false).unwrap();
assert_eq!(again.modified().len(), 1, "a newer client asks again");
assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"exe-3");
let saved = fs::read_dir(d.path().join(".coa-manager/replaced"))
.unwrap()
.next()
.unwrap()
.unwrap()
.path();
assert_eq!(
fs::read(saved.join("Wow.exe")).unwrap(),
b"my patched exe",
"the player's version was moved aside, not lost"
);
}
#[test]
fn disabled_launcher_renderers_are_never_restored_even_with_a_stale_plan() {
let dir = tempfile::tempdir().unwrap();
let files: Vec<(&str, &[u8])> = vec![("d3d9.dll", b"dxvk"), ("dxgi.dll", b"enhancements")];
let manifest = manifest("v1", &files);
let cancel = Cancel::default();
let old_plan = plan(dir.path(), &manifest, &State::default(), &cancel, &|_| {}).unwrap();
fs::write(dir.path().join("d3d9.dll.DISABLED"), b"custom disabled").unwrap();
crate::client_graphics::set(dir.path(), "dxgi.dll", true).unwrap();
let server = Server::new(&files);
apply(
&Apply {
client: dir.path(),
manifest: &manifest,
plan: &old_plan,
keep_modified: false,
transport: &server,
objects_url: "https://x.invalid/objects",
cancel: &cancel,
},
&|_| {},
)
.unwrap();
assert!(server.requested.lock().unwrap().is_empty());
assert!(!dir.path().join("d3d9.dll").exists());
assert!(!dir.path().join("dxgi.dll").exists());
assert_eq!(
fs::read(dir.path().join("d3d9.dll.DISABLED")).unwrap(),
b"custom disabled"
);
let after = plan(
dir.path(),
&manifest,
&load_state(dir.path()).unwrap(),
&cancel,
&|_| {},
)
.unwrap();
assert!(after.items.is_empty());
crate::client_graphics::set(dir.path(), "d3d9.dll", false).unwrap();
assert_eq!(
fs::read(dir.path().join("d3d9.dll")).unwrap(),
b"custom disabled"
);
}
#[test]
fn renderer_files_configs_and_everything_not_in_the_manifest_survive_an_update_untouched() {
let d = tempfile::tempdir().unwrap();
let v1: Vec<(&str, &[u8])> = vec![
("Wow.exe", b"exe-1"),
("d3d9.dll", b"official d3d9"),
("dxvk.conf", b"official conf"),
("Data/common.MPQ", b"common-1"),
];
sync(d.path(), &manifest("v1", &v1), &Server::new(&v1), false).unwrap();
// the player's own setup: custom renderer + configs, addons, WTF, realmlist, extra archives, backups
let mine: Vec<(&str, &[u8])> = vec![
("d3d9.dll", b"my renderer"),
("dxvk.conf", b"my conf"),
("ModernWoWRenderer.ini", b"cfg"),
("GraphicsEffects.ini.bak", b"bak"),
(
"Data/enUS/realmlist.wtf",
b"set realmlist 127.0.0.1
",
),
("Data/patch-5.MPQ", b"extra archive"),
("Interface/AddOns/ElvUI/ElvUI.toc", b"## Title: Elv"),
("WTF/Account/x/SavedVariables.lua", b"vars"),
("Cache/x.wdb", b"cache"),
];
for (p, b) in &mine {
let path = d.path().join(p);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, b).unwrap();
}
let before: BTreeMap<String, String> = mine
.iter()
.map(|(p, b)| (p.to_string(), fsx::sha256_bytes(b)))
.collect();
let v2: Vec<(&str, &[u8])> = vec![
("Wow.exe", b"exe-2"),
("d3d9.dll", b"official d3d9 v2"),
("dxvk.conf", b"official conf v2"),
("Data/common.MPQ", b"common-2"),
];
let (m2, s2) = (manifest("v2", &v2), Server::new(&v2));
for keep in [false, true] {
sync(d.path(), &m2, &s2, keep).unwrap();
for (p, h) in &before {
assert_eq!(
&fsx::sha256_file(&d.path().join(p)).unwrap(),
h,
"{p} must not change (keep_modified={keep})"
);
}
assert_eq!(fs::read(d.path().join("Wow.exe")).unwrap(), b"exe-2");
assert_eq!(
fs::read(d.path().join("Data/common.MPQ")).unwrap(),
b"common-2"
);
}
assert!(
!d.path().join(".coa-manager/replaced").exists(),
"nothing of the player's was displaced"
);
}
#[test]
fn an_existing_client_without_state_is_compared_by_content() {
let d = tempfile::tempdir().unwrap();
for (p, b) in V1 {
let path = d.path().join(p);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, b).unwrap();
}
fs::write(d.path().join("Data/patch-A.MPQ"), b"patch-a-X").unwrap(); // same size, different content
fs::remove_file(d.path().join("Data/enUS/locale.MPQ")).unwrap();
fs::create_dir_all(d.path().join("WTF")).unwrap();
fs::write(d.path().join("WTF/Config.wtf"), b"mine").unwrap();
let m = manifest("v1", &V1);
let p = plan(d.path(), &m, &State::default(), &Cancel::default(), &|_| {}).unwrap();
let kinds: Vec<_> = p.items.iter().map(|i| (i.path.as_str(), i.kind)).collect();
assert_eq!(
kinds,
[
("Data/patch-A.MPQ", Kind::Modified),
("Data/enUS/locale.MPQ", Kind::Missing)
]
);
assert_eq!(p.up_to_date_files, 2);
assert_eq!(fs::read(d.path().join("WTF/Config.wtf")).unwrap(), b"mine");
}
#[test]
fn cancel_keeps_finished_files_and_the_next_run_only_fetches_the_rest() {
let d = tempfile::tempdir().unwrap();
let m = manifest("v1", &V1);
let mut s = Server::new(&V1);
let cancel = Cancel::default();
s.cancel_after = Some((AtomicUsize::new(3), cancel.clone()));
let state = State::default();
let p = plan(d.path(), &m, &state, &cancel, &|_| {}).unwrap();
let r = apply(
&Apply {
client: d.path(),
manifest: &m,
plan: &p,
keep_modified: false,
transport: &s,
objects_url: "https://x.invalid/o",
cancel: &cancel,
},
&|_| {},
);
assert!(r.is_err());
let st = load_state(d.path()).unwrap();
assert!(
st.version.is_none() && !st.files.is_empty() && st.files.len() < 4,
"unfinished but remembered: {}",
st.files.len()
);
assert!(local(d.path()).managed && local(d.path()).version.is_none());
let s2 = Server::new(&V1);
sync(d.path(), &m, &s2, false).unwrap();
assert_eq!(
s2.requested.lock().unwrap().len(),
4 - st.files.len(),
"only the missing files were requested"
);
assert_eq!(load_state(d.path()).unwrap().version.as_deref(), Some("v1"));
}
#[test]
fn a_wrong_object_never_reaches_the_client_folder() {
let d = tempfile::tempdir().unwrap();
let m = manifest("v1", &V1);
let s = Server::new(&V1);
// the server returns different bytes under the right hash
let sha = fsx::sha256_bytes(b"exe-1");
s.objects.lock().unwrap().insert(sha, b"EVIL!".to_vec());
assert!(sync(d.path(), &m, &s, false).is_err());
assert!(!d.path().join("Wow.exe").exists());
assert!(load_state(d.path()).unwrap().version.is_none());
}
}
@@ -0,0 +1,473 @@
{
"version": 1,
"source": "azerothcore_catalogue_categorized_2026-11-07.xlsx (owner), modules with high or medium confidence",
"categories": [
{
"code": "bots",
"ru": "Боты и искусственный интеллект",
"en": "Bots and AI",
"keywords": [
"playerbots",
"bots",
"npcbot",
"llm",
"ollama",
"ai",
"автономные спутники"
]
},
{
"code": "pve",
"ru": "PvE, подземелья и рейды",
"en": "PvE, dungeons and raids",
"keywords": [
"pve",
"dungeon",
"raid",
"boss",
"mythic+",
"scaling",
"rdf",
"инстансы"
]
},
{
"code": "pvp",
"ru": "PvP, арены и поля боя",
"en": "PvP, arenas and battlegrounds",
"keywords": [
"pvp",
"arena",
"battleground",
"bg",
"duel",
"ffa",
"фракции"
]
},
{
"code": "hardcore",
"ru": "Hardcore и режимы сложности",
"en": "Hardcore and difficulty",
"keywords": [
"hardcore",
"ironman",
"permadeath",
"high risk",
"difficulty"
]
},
{
"code": "progression",
"ru": "Прокачка и прогрессия",
"en": "Leveling and progression",
"keywords": [
"xp",
"level",
"rates",
"leveling",
"rewards",
"prestige",
"progression",
"experience",
"reputation",
"rested"
]
},
{
"code": "classes",
"ru": "Классы, расы и способности",
"en": "Classes, races and abilities",
"keywords": [
"race",
"class",
"talent",
"spells",
"specialization",
"pets"
]
},
{
"code": "cosmetics",
"ru": "Косметика и трансмог",
"en": "Cosmetics and transmog",
"keywords": [
"transmog",
"appearance",
"morph",
"visual",
"vanity",
"cosmetics"
]
},
{
"code": "items",
"ru": "Лут, предметы и экипировка",
"en": "Loot, items and gear",
"keywords": [
"loot",
"item",
"gear",
"enchant",
"reforging",
"bank",
"inventory",
"items",
"reagent"
]
},
{
"code": "economy",
"ru": "Торговля, экономика и профессии",
"en": "Trade, economy and professions",
"keywords": [
"auction",
"market",
"vendor",
"trade",
"gold",
"shop",
"profession",
"ahbot",
"auction house",
"gather",
"herbs",
"mining",
"fishing",
"crafting"
]
},
{
"code": "account",
"ru": "Аккаунты и персонажи",
"en": "Accounts and characters",
"keywords": [
"account",
"character",
"mounts",
"collection",
"migration",
"recruit",
"login",
"alts"
]
},
{
"code": "social",
"ru": "Гильдии, чат и социальные",
"en": "Guilds, chat and social",
"keywords": [
"guild",
"chat",
"messages",
"announcements",
"friends",
"mail"
]
},
{
"code": "npc",
"ru": "NPC и игровые сервисы",
"en": "NPCs and services",
"keywords": [
"npc",
"buffer",
"trainer",
"enchanter",
"services",
"assistant",
"vendor npc",
"gossip"
]
},
{
"code": "world",
"ru": "Мир, зоны и перемещение",
"en": "World, zones and travel",
"keywords": [
"world",
"zone",
"spawn",
"teleport",
"portal",
"travel",
"respawn"
]
},
{
"code": "events",
"ru": "События и мини-игры",
"en": "Events and mini-games",
"keywords": [
"events",
"holidays",
"quests",
"trivia",
"minigames",
"treasure"
]
},
{
"code": "server",
"ru": "Сервер, хостинг и автоматизация",
"en": "Server and automation",
"keywords": [
"server",
"deployment",
"backup",
"cache",
"relay",
"restart",
"monitoring"
]
},
{
"code": "admin",
"ru": "Администрирование и безопасность",
"en": "Administration and security",
"keywords": [
"gm",
"admin",
"moderation",
"logs",
"security",
"bans",
"abuse"
]
},
{
"code": "web",
"ru": "Сайты, API и интеграции",
"en": "Websites, API and integrations",
"keywords": [
"api",
"cms",
"website",
"discord",
"telegram",
"web",
"metrics"
]
},
{
"code": "dev",
"ru": "Разработка, SQL и инструменты",
"en": "Development, SQL and tools",
"keywords": [
"editor",
"db",
"sql",
"lua",
"eluna",
"ide",
"tooling",
"sdk"
]
},
{
"code": "client",
"ru": "Клиент, патчи и интерфейс",
"en": "Client, patches and UI",
"keywords": [
"client",
"patch",
"addon",
"ui",
"launcher",
"controller",
"mpq"
]
},
{
"code": "other",
"ru": "Прочее",
"en": "Other",
"keywords": [
]
}
],
"repos": {
"liyunfan1223/mod-playerbots": "bots",
"dustinhendrickson/mod-ollama-chat": "bots",
"dustinhendrickson/mod-player-bot-level-brackets": "bots",
"dustinhendrickson/mod-ollama-bot-buddy": "bots",
"azerothcore/mod-autobalance": "pve",
"zhengpeiru21/mod-challenge-modes": "pve",
"anchydev/dungeonrespawn": "pve",
"azerothcore/mod-rdf-expansion": "pve",
"silviu20092/mod-mythic-plus": "pve",
"avarishd/mod-brawlers-guild": "pve",
"sogladev/mod-vanilla-naxxramas": "pve",
"pangolp/mod-cfpve": "pve",
"bewarellamas/mod-dungeonmaster": "pve",
"araxiaonline/mod-mythic-plus": "pve",
"sogladev/mod-reset-raid-cooldowns": "pve",
"hallgaeuer/mod-quickbalance": "pve",
"valsan-azerty-boi/mod-ony-naxx-logout-teleport": "pve",
"stealthyape/raidteleporter": "pve",
"zesman21/mod-nordf": "pve",
"helias/mod-bg-slaveryvalley": "pvp",
"azerothcore/mod-duel-reset": "pvp",
"gitdalisar/mod-faction-free": "pvp",
"azerothcore/mod-pvp-titles": "pvp",
"helias/mod-bg-twinpeaks": "pvp",
"helias/mod-bg-battle-for-gilneas": "pvp",
"talamortis/mod-pvpscript": "pvp",
"gozzim/mod-npc-spectator": "pvp",
"azerothcore/mod-bg-reward": "pvp",
"helias/mod-arena-tigerspeak": "pvp",
"helias/mod-arena-tolviron": "pvp",
"tbcstar/mod-high-risk-system": "pvp",
"azerothcore/mod-pvp-zones": "pvp",
"azerothcore/mod-bg-item-reward": "pvp",
"azerothcore/mod-desertion-warnings": "pvp",
"azerothcore/mod-low-level-rbg": "pvp",
"youpeoples/ultimate-full-loot-pvp": "pvp",
"azerothcore/mod-pvpstats-announcer": "pvp",
"anchydev/ffafix": "pvp",
"azerothcore/mod-low-level-arena": "pvp",
"trembon/mod-peacekeeper": "pvp",
"anchydev/bgqueuechecker": "pvp",
"anchydev/hardmode": "hardcore",
"kjack9/mod-dead-means-dead": "hardcore",
"conan1996-2/mod-hard-modes": "hardcore",
"jorge990125/mod-hardcore-makgora": "hardcore",
"stuntmonkey4u/mod-trial-of-finality": "hardcore",
"zindokar/mod-hardcore": "hardcore",
"zhengpeiru21/mod-individual-progression": "progression",
"azerothcore/mod-congrats-on-level": "progression",
"noisiver/mod-weekendbonus": "progression",
"youpeoples/prestige-and-draft-mode": "progression",
"anchydev/attriboost": "progression",
"azerothcore/mod-fireworks-on-level": "progression",
"anchydev/extendedxp": "progression",
"anchydev/prestigious": "progression",
"noisiver/mod-appreciation": "progression",
"betayi/mod_spec_reward": "progression",
"brian8544/setxpbar": "progression",
"nhobodyimportant/prestige": "progression",
"azerothcore/mod-alpha-rewards": "progression",
"michaeldelago/mod-quest-count-level": "progression",
"regrad/update-mob-level-to-player-and-random-item-stats": "progression",
"azerothcore/mod-chromie-xp": "progression",
"kadan-gx/mod-login-rewards": "progression",
"talamortis/mod-individual-xp": "progression",
"talamortis/reward-system": "progression",
"brian8544/mod-setxpbar": "progression",
"fischerlol/mod-level-15-boost": "progression",
"notepadguyofficial/mod-level-rewards": "progression",
"thearktect/mod-weekend-xp": "progression",
"heyitsbench/mod-arac": "classes",
"heyitsbench/mod-worgoblin": "classes",
"azerothcore/mod-npc-beastmaster": "classes",
"zhengpeiru21/mod-leech": "classes",
"azerothcore/mod-racial-trait-swap": "classes",
"noisiver/mod-learnspells": "classes",
"silviu20092/mod-warlock-pet-rename": "classes",
"vhiperdev/mod-spellregulator": "classes",
"xiii-hearts/mod-npc-subclass": "classes",
"gozzim/mod-starting-pet": "classes",
"bewarellamas/mod-profspecs": "classes",
"sogladev/mod-demonic-pact-classic": "classes",
"brian8544/mod-talentbutton": "classes",
"lenzork/mod-petequip": "classes",
"azerothcore/mod-transmog": "cosmetics",
"azerothcore/mod-weapon-visual": "cosmetics",
"azerothcore/mod-morphsummon": "cosmetics",
"azerothcore/mod-costumes": "cosmetics",
"azerothcore/mod-npc-morph": "cosmetics",
"azerothcore/mod-aoe-loot": "items",
"azerothcore/mod-npc-enchanter": "items",
"anchydev/statbooster": "items",
"zhengpeiru21/mod-reagent-bank": "items",
"silviu20092/mod-item-upgrade": "items",
"silviu20092/mod-reforging": "items",
"terrabyte-tbwps/mod-aoe-loot": "items",
"silviu20092/mod-improved-bank": "items",
"pangolp/mod-quest-loot-party": "items",
"anchydev/recycleditems": "items",
"anchydev/statboosterreroller": "items",
"hallgaeuer/mod-dynamic-loot-rates": "items",
"seaferer/aoe-loot---merge": "items",
"protonull/mod-starter-wands": "items",
"talamortis/mod-itemlevel": "items",
"noisiver/mod-junk-to-gold": "economy",
"youpeoples/black-market-auction-house": "economy",
"wowhellgarve-holydeew/mod-multi-vendor": "economy",
"azerothcore/mod-money-for-kills": "economy",
"azerothcore/mod-npc-gambler": "economy",
"elmsroth/mod-trade-items-filter": "economy",
"talamortis/mod_reward_shop": "economy",
"silviu20092/mod-sell-items": "economy",
"tereneckla/mod-profession-experience": "economy",
"jorge990125/mod-black-market": "economy",
"heyitsbench/mod-ptr-template": "account",
"pangolp/mod-recruit-friend": "account",
"azerothcore/mod-npc-all-mounts": "account",
"gozzim/mod-firstlogin-aio": "account",
"talamortis/mod-premium": "account",
"zerkenn/mod-character-services": "account",
"lucafolloni/mod-account-vanity-pets": "account",
"noisiver/mod-accountbound": "account",
"pangolp/mod-mounts-on-account": "account",
"microbica/mod-share-mounts": "account",
"gozzim/mod-globalchat": "social",
"azerothcore/mod-guild-zone-system": "social",
"azerothcore/mod-starter-guild": "social",
"anchydev/itembroadcastguildchat": "social",
"azerothcore/mod-chat-transmitter": "social",
"azerothcore/mod-global-chat": "social",
"noisiver/mod-guildfunds": "social",
"jorge990125/mod-streams": "social",
"noisiver/mod-assistant": "npc",
"azerothcore/mod-npc-services": "npc",
"azerothcore/mod-npc-buffer": "npc",
"pangolp/mod-npc-promotion": "npc",
"azerothcore/mod-npc-codebox": "npc",
"azerothcore/mod-buff-command": "npc",
"jorge990125/mod-bienvenida": "npc",
"dunjeon/mod-timeistime": "world",
"bytesgalore/mod-no-hearthstone-cooldown": "world",
"justin-kaufmann/mod-changeablespawnrates": "world",
"hallgaeuer/mod-quick-respawn": "world",
"justin-kaufmann/mod-noclip": "world",
"eldoba/mod-dynamic-resurrections-": "world",
"elmegaard/mod-auto-resurrect": "world",
"noisiver/mod-spawnpoints": "world",
"tralenor/mod-objscale": "world",
"silviu20092/mod-flightmaster-whistle": "world",
"sogladev/mod-ghost-speed": "world",
"talamortis/playerteleport": "world",
"vhiperdev/mod-no-farming": "world",
"noisiver/mod-activatezones": "world",
"zyggy123/treasure-chest-system": "events",
"azerothcore/mod-tic-tac-toe": "events",
"pangolp/mod-quest-status": "events",
"azerothcore/mod-war-effort": "events",
"youpeoples/quizmaster": "events",
"azerothcore/mod-cta-switch": "events",
"azerothcore/mod-dmf-switch": "events",
"azerothcore/mod-server-auto-shutdown": "server",
"anchydev/recache": "server",
"azerothcore/mod-queue-list-cache": "server",
"maluramichael/mod-influxdb": "server",
"sogladev/mod-increment-cache-version": "server",
"anchydev/aci": "server",
"azerothcore/mod-ip-tracker": "admin",
"tralenor/mod-gomove": "admin",
"talamortis/mod-whologged": "admin",
"talamortis/mod_character_tools": "admin",
"azerothcore/mod-notify-muted": "admin",
"mellianstudios/mod-encounter-logs": "admin",
"miithrandiir/mod_sponsorship": "web",
"exitare/mod-discord-webhook": "web",
"abutbul/mod-game-state-api": "web",
"thearktect/mod-discord-announce": "web",
"azerothcore/skeleton-module": "dev",
"azerothcore/eluna-ts": "dev",
"azerothcore/mod-premium-lib": "dev",
"anchydev/breakingnewsoverride": "client",
"gitdalisar/addon-faction-free-unit-popup": "client"
}
}
+11 -17
View File
@@ -58,33 +58,27 @@
}, },
{ {
"repo_slug": "azerothcore/mod-cfbg", "repo_slug": "azerothcore/mod-cfbg",
"status": "Verified", "status": "Incompatible",
"is_recommended": false, "is_recommended": false,
"reason": "Cross-faction battlegrounds system. Built into CoA core fork." "reason": "Already built into the CoA core (cross-faction battlegrounds); a separate DLL would load it a second time."
}, },
{ {
"repo_slug": "azerothcore/mod-duel-reset", "repo_slug": "helias/mod-bg-battle-for-gilneas",
"status": "Verified", "status": "Incompatible",
"is_recommended": false, "is_recommended": false,
"reason": "Resets health, mana, cooldowns and auras after duels. Verified dynamic DLL." "reason": "Needs the server's DBC files replaced (including Spell.dbc, which CoA ships its own of), a client patch and a modified wow.exe; written for AzerothCore 2019."
}, },
{ {
"repo_slug": "azerothcore/mod-pvp-titles", "repo_slug": "helias/mod-bg-twinpeaks",
"status": "Verified", "status": "Incompatible",
"is_recommended": false, "is_recommended": false,
"reason": "Classic PvP rank titles awarded based on honorable kills." "reason": "Needs the server's DBC files replaced (CoA ships its own), a client patch and a modified wow.exe."
}, },
{ {
"repo_slug": "azerothcore/mod-starter-guild", "repo_slug": "helias/mod-bg-slaveryvalley",
"status": "Verified", "status": "Incompatible",
"is_recommended": false, "is_recommended": false,
"reason": "Automatically invites newly created characters into a designated starter guild." "reason": "Needs the server's DBC files replaced (CoA ships its own) and a client patch."
},
{
"repo_slug": "azerothcore/mod-skip-dk-starting-area",
"status": "Verified",
"is_recommended": false,
"reason": "Allows Death Knights to skip the Scarlet Enclave starter zone."
}, },
{ {
"repo_slug": "azerothcore/mod-learn-spells", "repo_slug": "azerothcore/mod-learn-spells",
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,116 @@
{
"Ascension.exe": "e2c347019d554a59639e5978fd68e50e906973ab313c6573cc56ca82e8e3c547",
"Data/Retail/Assets/EquipmentFitting/1068-1.bin": "332d2c38b037a118f262bbb3096a01ebfb33c5347e96bbfb64157437b300281b",
"Data/Retail/Assets/EquipmentFitting/60-0.bin": "ffbf6efc9ea09b0c442f048c5bc7b4c5975258045cfddedd9a77a93363312b5e",
"Data/Retail/Assets/EquipmentFitting/60-1.bin": "36ceed2a55756f3322e018938809acea5124351161405058ef27d100173778da",
"Data/Retail/Assets/EquipmentFitting/61-0.bin": "3b302a4055d1e795c451b02e38dfe8007527ee1bc5f926ebcd6ef17f433cd232",
"Data/Retail/Assets/EquipmentFitting/61-1.bin": "c42df399ed99be3c7ce1b5fdb95a873711011713a4cd867bc226a14b1ce0e8ca",
"Data/Retail/Assets/EquipmentFitting/65-0.bin": "8ce3f884ae197d14dff0f955c2cf05abb716742c8aff79f7af1cec5cf7225f04",
"Data/Retail/Assets/EquipmentFitting/68-0.bin": "f1d878f0f943cbdb8025af8daac4a38c1d1e159bd961d9d76f225931c11648ac",
"Data/Retail/Assets/EquipmentFitting/68-1.bin": "2ccbae634384200340c0cc3dca36a7a014a8ddad110598e4470ff1b266576465",
"Data/Retail/Assets/EquipmentFitting/73-0.bin": "5530882678f677ad47987ce906e851338a35f7d9737e3f9d829938027b8af67f",
"Data/Retail/Assets/EquipmentFitting/74-0.bin": "53a7be86b3bfdd6995406c706df3ca091484206d8141cebb39113c714500384a",
"Data/Retail/Assets/EquipmentFitting/74-1.bin": "e688c55a355231978d42480a003c26d379460f5931c2469c119ecb073ea153e1",
"Data/Retail/Assets/EquipmentFitting/75-0.bin": "289abc71c6df0e031a64d62a40e658eaefd37e45373a062ca52f08fbe803500c",
"Data/Retail/Assets/EquipmentFitting/76-0.bin": "63a493e70b91f59ff49f1942d959df986062bd267fe2ba589f344963df771021",
"Data/Retail/Assets/EquipmentFitting/77-0.bin": "b1366ce0dc4f31cb8c8c9705be2da565df4b905224af04b6b88f8c8e9c87e3c9",
"Data/Retail/Assets/EquipmentFitting/78-0.bin": "7b0031fb12ab19020005bdccacca2dd3991349c36949a58b035992585b4134db",
"Data/patch-T.MPQ": "fe597138ab7f56881b5f4f27d6dc9d81bb278b483ca0d91b7207ffd4a5ae7bfd",
"Data/patch-ZE1.MPQ": "69d79dbbea54ec0312c37770debc79c7bc9ce2e21eedef99c03cd8d911955283",
"Data/patch-ZE2.MPQ": "1a41cae77ad730e9cf69e8d63fd12ae3a579418bb550d351fff469573a97ac3c",
"Data/patch-ZE3.MPQ": "b587202c21b6f580682b601b7c2e905fbfa766276e74733db55c1bddbffb7f87",
"Data/patch-ZE5.MPQ": "476b5a1fcf69f64fdb281e3b445362b936d016066ccd12f897acfcf200d6c0cf",
"Data/patch-ZE6.MPQ": "762592c9ed950b4a7b6c7d85d45050b30fa3f35d53e348ee1e1516fdbb7e5484",
"Data/patch-ZE7.MPQ": "de54b0189d242763d2872223419a259b6b8a4b380c24b50a2ef9c6fa44466248",
"Data/patch-ZE8.MPQ": "1bc5691fb84a84f3e7a0a70baaa9192a97b4ec5906ee37c85838ffe08adef934",
"Data/patch-ZE9.MPQ": "d49c94463e9548e76378ce2c0454d8f204f0745c8c0de32b6dce7f0b0fd67c4a",
"Data/patch-ZEA.MPQ": "78fd10ec5262ae2417eba9700d2172553bbc1e5cfb0b597aada8f1753ca46d9d",
"Data/patch-ZES.MPQ": "a2d9348f917f745c7aa25b2eed96efa312f7b2d2f62cb86a4f2eaabc2dfa895f",
"Data/patch-ZG.MPQ": "48979e1fda775f8deb47bffa48e0a468595d4c71286fee518f51dc5801c5ca39",
"Data/patch-ZH.MPQ": "5e1d2fe287053abba703d7af381e10096c6525ba477ffed3971c2fc4f93e919c",
"Data/patch-ZHA.MPQ": "0fc6ebeafe1d14192dc64bafc90bde3c49230df1297f98bf65900a3bcf953617",
"Data/patch-ZHM.MPQ": "317d3ae00f0bd674b358af07544976acb7804aa6d8b19d1788ad96030e8c9bf0",
"Data/patch-ZJ.MPQ": "f821e99add86ef68d108c5ec466328c97677d3ed52f7944e200f3fe38c13b4de",
"Data/patch-ZK.MPQ": "444d0ecb72c1e990509d53e3cfa5b7634c816733533e8a97e5d7f5653ee32cce",
"Data/patch-ZM.MPQ": "424ab1978986e854551633983a94bd15beba6328aabea8c02f98eaa32f6f0b61",
"Data/patch-ZVU.MPQ": "c679b85a2e97344395756b54d2cf6abefe4e80e74944cb55b6e573ad17b7d020",
"Data/patch-ZZE.MPQ": "4b1e289e12c82644cb78f50170e6b5fd53918eb2de834f9e0cebddd81873a8bb",
"Data/patch-ZZR.MPQ": "5a7575e592dc0c8a6913e0d946bbe06fa0ca2e4d817ef1f8548d3d13142bcbd3",
"Data/patch-ZZS.MPQ": "533fd69d5784d83e872a5a28d27301dc9ab5d0cb925f8b2116926854e4a0a5b8",
"Data/patch-ZZT.MPQ": "e60dab63fe5650fdf5f0846ea6d4136219293bcfdad46d9b796697faeff9ab4c",
"Data/patch-ZZU.MPQ": "0311de8f034eae7dec1bd2c92b1fe6c741cbaf53da62c7609cd12b6c37908e47",
"Data/patch-ZZV.MPQ": "3b04008905e7185d165827d68f7ca228dda8272816364f7d71874ecc83b64f0e",
"Data/patch-ZZW.MPQ": "3bbbc72901fd2d662113036485c380701e693f5a39ba9479af306bf2fa2030bd",
"Data/patch-ZZX.MPQ": "4c12608089cfe356f7c8882d66db90239ed3ec8ddd980d91d7a3a488680f3c88",
"Data/patch-ZZZ.MPQ": "566aaadf501af42e29a828b1bc2edbd36d436288fb309fe423bd74b9619bf593",
"Data/patch-ZZZA.MPQ": "57ccbdc0a66000767554ffd194088df6d602ddd8341a141bca8201e7d5435889",
"Data/patch-ZZZC.MPQ": "691a53f6325c2615a64ced0802f18bc4d96e99c62aaa606fc930f9406ee74cdf",
"Data/patch-ZZZD.MPQ": "ca893cf8129f6fd442a63f0697a8ddea88546e3ce71bde713be2cc832b892bc9",
"Data/patch-ZZZE.MPQ": "2e245ee0fe3b913087eed399062fda5843d98b04afac38024f7c64ac3bf43e18",
"Data/patch-ZZZF.MPQ": "40eac695cfcafb598b8376f3b9a61ecc037e4d445b87279cfe387a91edad32c3",
"Data/patch-ZZZZ.MPQ": "860c990db3b4185fdc36e1a001d3ca3507a68253a4506793a115f86eb0892a15",
"EsteriaAppearance.bin": "90d9cfaeaf6de10f014f1d1610ea10453ec9b240255759aea99513cac488a3ec",
"EsteriaAppearance.dll": "88e268d3be9eca3b0644f543d823f4bdcc04a9700a6e8ed73fd2a1260ad191af",
"EsteriaAppearanceGeometry.bin": "19de5b4b4f79778ea5316659efc2f66c6edb6b5149c46efa47c46bfa4ec6904c",
"EsteriaAppearanceMaterials.bin": "f72f027f54a55c05aa0f76b457b97b484fe0e1aedc0e068cc0e0ca53e1878cba",
"EsteriaBanshee.bin": "d3fe66caa6a0ecba96bddb07ac1cbdb915694e87fc1608da0fdabecdf9ffea4d",
"EsteriaBansheeTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaCentaur.bin": "d63f0b7d21cee67d234d134e832fe6d55c398f2b63f9f6c191dcfa9671637185",
"EsteriaCentaurTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaCosmeticWings.bin": "57352c28459d073a29b3014d33b70f3b3756afa37fa9583ef6b30d503422ac3f",
"EsteriaDrakonid.bin": "95440ee9c065ef20bb7fed6158d20623d68745291cedc2726fc746ff54601f95",
"EsteriaDrakonidCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
"EsteriaDrakonidTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
"EsteriaEarthen.bin": "8a0db93ba2d5c756b44ed84f2f7bc7c15eef13be6a7d87baf1383c6c0c624e02",
"EsteriaEthereal.bin": "ada3b7588511a847dd5154d91e3041e9f7f193998b06bfede5e0afc32570b48c",
"EsteriaEtherealTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaFaceless.bin": "1c5a1d7a6b1db6984a8e41bc4804e9f7c50d65dcdbb8c0a9d1a9e8af8c91bd8f",
"EsteriaFacelessTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaGnoll.bin": "cfacc8e681bb8e54a4f01ee9c12a91bf0bd23a23527bb548d4dc21ec1b33eea2",
"EsteriaGnollTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
"EsteriaHaranir.bin": "59f31d00e372fd2646e1cf0d9d3d5d671b31edd60ff7b862f8d1ce7e53ba1f2f",
"EsteriaHaranirTextures.bin": "99948ca570be2d376f0c67e3b8088739b6fa9f9d2c73985e19f68df77689a229",
"EsteriaHighmountain.bin": "e83349252ad2687ac23fcfc5623db7401cf32e5faf47a5a7be20c2f94b014938",
"EsteriaHozen.bin": "739bc3775388976f5c09bcbf3a98b62d804dff78d545e25a41d07dd99b5fddc5",
"EsteriaHozenTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaKvItems.bin": "fc5f9d86114234b38dbb0e14a6a5ec16249eaa92a73411a7fc371a950549dcce",
"EsteriaKvSkins.bin": "60b2b899f5f83137368514928f92f3b04c0fae058f534c487ddbe913ee4bec18",
"EsteriaKyrian.bin": "9329147f08805ff4003f9e062fea7c6ea8fdd8c4d6a40de63a6d228ebe1b7c9a",
"EsteriaKyrianTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaMantid.bin": "71536dee6c2abd0b0e13f33bc73cad7e6fb1b0fc1f6912a95c55d57bc7ee1787",
"EsteriaMantidTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaNaga.bin": "e87547cff5d09500c294f8b06935d134cdd0bbb962e294a72de09404505bd8b3",
"EsteriaNagaTextures.bin": "57ec560f99c1d48ac1e8f6a797c156513b99e7d349e3071afe405f812d159ae3",
"EsteriaSaberon.bin": "7735c9325c807c5125760d87f323d6b7d4162fb542c1d8b5f435ea2d6b9a4daa",
"EsteriaSaberonCoverage.bin": "9cdc227f08329e1245d2b864a996e7a772c96ecdbe5d81925f4d60cdbe4fa11e",
"EsteriaSaberonTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
"EsteriaSethrak.bin": "bd14857a8c11fdf8027128763293701586b2102f9a6539d33a727014789fd606",
"EsteriaSethrakTextures.bin": "d2b2cf58087a391f00198008fe871edd400ddf28d54a3a05674df74c5198528a",
"EsteriaSlime.bin": "491e942ceb6abda83c0249afebae7c9c43af3897b35e45fa33d6f9177ecbc177",
"EsteriaSlimeTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaTarasek.bin": "f718444d5ec36f7aff018883486ae1981151fe6e01f5b7781d68d74e9fadfa47",
"EsteriaTarasekTextures.bin": "1ba9133a75dad291d98a068b2dcde01f31a897a1764cd7f3e2c316c288a8f605",
"EsteriaThinHuman.bin": "bc12e5ca2c087ca0a8642ff6cd8e78fe2e1f85928518b12c19b4245a8320585d",
"EsteriaThinHumanTextures.bin": "27488a7494fa9733b53118d1d0f4acef50a9e19004f81f9c8a614b10f4c3fd5e",
"EsteriaTuskarr.bin": "ecbe7ef490ba67eca029a57639192db61238522618491549766fe38529914387",
"EsteriaTuskarrTextures.bin": "e2116c2c83adea079126fd5da765d20da24f044d4f161c4124e4368cb3d6bc71",
"EsteriaVenthyr.bin": "5bcf7804363e59b3c922830d06de17bbd348b9967307f39b7d72bc09f58814eb",
"EsteriaVenthyrTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaVorkai.bin": "d6d2088c7e867b5db437e9afc2d323c22d830c6f84a3598415eedcaa27e2461f",
"EsteriaVorkaiTextures.bin": "837774377ca055e6a5c79c1a4cfd8151e7c1f7ce2c16990b00fd445281137433",
"EsteriaVrykul.bin": "39e2b7b4c67997dbbd34bae840bd606a59290f7f2fc82565a0ba00090335415c",
"EsteriaVrykulTextures.bin": "46098750a0e235724c6e9cc8d9d4e26550aa47bef95fd5f0f82efe5e945bf296",
"EsteriaVulpera.bin": "c5434993b9af4a17d881965e4735cd2ac379e39c7142aac9844adff9528562d4",
"EsteriaVulperaTextures.bin": "1ab844e74684aced2b069f1b91cd563aade45e05bd5e23f866a6c0c301cb098e",
"Interface/AddOns/CoAHideArmor/CoAHideArmor.lua": "8c1c7403524fc9bd56c0809b4101b141da700ba163701209bf1420f2ef106aa7",
"Interface/AddOns/CoAHideArmor/CoAHideArmor.toc": "123580d92fd5ceee258e96296f42c359b7e83cdff151af49c50c6c4e2059677c",
"Interface/AddOns/CoAQuestSafe/CoAQuestSafe.lua": "062de55c91228971553f482fd2c8753f4752993b4e8e49d4ded04fa18b98bde7",
"Interface/AddOns/CoAQuestSafe/CoAQuestSafe.toc": "890bb9d1087e236f4823603f5df3ab0e316dbb65c28feebbda08fb0ab53a408f",
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.lua": "1cf9537ff5f10b77250fea4407aea1008da690afe102ed6ed2dac21f79638cfa",
"Interface/AddOns/CoAWC2Cursor/CoAWC2Cursor.toc": "48ce695dce712b3643ebe13a77e318d623c964acc69479b8fc8cc675099801bf",
"Interface/AddOns/CoA_TalentReset/CoA_TalentReset.lua": "827d756d5948ef918fafaf8b869290b3a1faa4e4cc21b19b512a00d4509f3d41",
"Interface/AddOns/CoA_TalentReset/CoA_TalentReset.toc": "16298c04af68e68d6445e7a11f42aee525dd4145f85d2771ff434fb7c6e95a85",
"dinput8.dll": "f8de78606c241fb3911dce22faf68f5b9f9885b142b183c1e2c09ddc8f7e380c",
"wc2_human.cur": "f609736e29596bb386ef1597b157d211e01a86d32c2a929e256c581c37d71791",
"wc2_orc.cur": "baa512c9b9045762ba35b6e0d35b8f79c54b7649f35cc9c0e0cc8c168a1e5a97"
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+11
View File
@@ -337,6 +337,17 @@ impl Db {
.map(|o| o.status.success()).unwrap_or(false) .map(|o| o.status.success()).unwrap_or(false)
} }
/// Ask the server to shut down cleanly (`mysqladmin shutdown`).
pub fn shutdown_server(&self) -> Result<()> {
let mut c = self.command("mysqladmin.exe");
c.arg("shutdown").arg("--connect-timeout=5");
let out = bounded_client_output(&mut c, b"", Instant::now() + Duration::from_secs(60))?;
if !out.status.success() {
return Err(self.fail(&out.stderr));
}
Ok(())
}
pub(crate) fn with_read_deadline(mut self, deadline: Instant) -> Self { pub(crate) fn with_read_deadline(mut self, deadline: Instant) -> Self {
self.read_deadline = Some(deadline); self.read_deadline = Some(deadline);
self self
+17
View File
@@ -665,6 +665,11 @@ fn game_args(
a.extend(env("AC_UPDATES_ENABLE_DATABASES", "0")); a.extend(env("AC_UPDATES_ENABLE_DATABASES", "0"));
if kind == GameKind::World { if kind == GameKind::World {
a.extend(["--volume".into(), format!("{}:{DATA}:ro", data.display())]); a.extend(["--volume".into(), format!("{}:{DATA}:ro", data.display())]);
let pbc = crate::modules::pbc::ASSET_DIR;
let assets = host.join(pbc);
if assets.is_dir() {
a.extend(["--volume".into(), format!("{}:/srv/{pbc}:ro", assets.display())]);
}
let races = host.join("Data/dbc_races"); let races = host.join("Data/dbc_races");
if races.is_dir() && races != data.join("dbc_races") { if races.is_dir() && races != data.join("dbc_races") {
a.extend([ a.extend([
@@ -1066,6 +1071,8 @@ mod tests {
#[test] #[test]
fn a_first_start_creates_the_network_the_image_and_the_containers_in_order() { fn a_first_start_creates_the_network_the_image_and_the_containers_in_order() {
let (_d, root) = server("t1"); let (_d, root) = server("t1");
let assets = root.join(crate::modules::pbc::ASSET_DIR);
fs::create_dir_all(&assets).unwrap();
let sim = Sim::new(); let sim = Sim::new();
let o = start_all(&sim, &root); let o = start_all(&sim, &root);
assert!(o.ok, "{}", o.output); assert!(o.ok, "{}", o.output);
@@ -1091,6 +1098,16 @@ mod tests {
["coa-t1-db", "coa-t1-world", "coa-t1-auth"], ["coa-t1-db", "coa-t1-world", "coa-t1-auth"],
"database, then world, then auth" "database, then world, then auth"
); );
let calls = sim.calls_of("run");
// The containers get the canonical server folder; a temporary folder may be a short or linked path.
let host = std::fs::canonicalize(&root).unwrap();
let host = crate::fsx::canonicalize_lenient(&host).unwrap_or(host);
let mount = format!(
"{}:/srv/Extras/PlayerbotCharacters:ro",
host.join(crate::modules::pbc::ASSET_DIR).display()
);
assert!(calls[1].args.windows(2).any(|w| w == ["--volume", mount.as_str()]));
assert!(!calls[0].args.contains(&mount) && !calls[2].args.contains(&mount));
let exec = sim.calls_of("exec"); let exec = sim.calls_of("exec");
assert_eq!( assert_eq!(
exec.len(), exec.len(),
+58 -308
View File
@@ -1,11 +1,10 @@
//! Start/stop of a repack-shaped installation by delegating to the repack's own supervisor //! Start/stop of a repack-shaped installation through the repack launcher in this crate
//! (`Runtime\python\python.exe Scripts\manage.py <verb>`), which already implements config rendering, the //! ([`crate::launcher`]): config rendering, the services, the world supervisor, graceful shutdown and readiness.
//! bug-report relay, RA graceful shutdown and readiness. Observation stays native (see `process`). //! It writes the same `.state` records as the repack's `Scripts/manage.py`, so the repack's own launcher and
//! this Manager can be used on the same folder. Observation stays native (see `process`).
use std::io::Read; use std::path::Path;
use std::path::{Path, PathBuf}; use std::sync::Mutex;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use serde::Serialize; use serde::Serialize;
@@ -22,22 +21,13 @@ pub enum Verb {
} }
impl Verb { impl Verb {
fn arg(self) -> &'static str { fn action(self) -> crate::launcher::ops::Action {
use crate::launcher::ops::Action;
match self { match self {
Verb::StartAll => "start-all", Verb::StartAll => Action::StartAll,
Verb::StartMysql => "start-mysql", Verb::StartMysql => Action::StartMysql,
Verb::StartWorld => "start-world", Verb::StartWorld => Action::StartWorld,
Verb::StopAll => "stop-all", Verb::StopAll => Action::StopAll,
}
}
fn timeout(self, _root: &Path) -> Duration {
match self {
// Includes database startup, optional bot provisioning and the shared 25-minute
// world/console wait in launcher_bridge.py, even for older installed scripts.
Verb::StartAll | Verb::StartWorld => Duration::from_secs(3600),
Verb::StartMysql => Duration::from_secs(360),
Verb::StopAll => Duration::from_secs(240),
} }
} }
} }
@@ -52,41 +42,14 @@ pub struct DriverOutcome {
pub output: String, pub output: String,
} }
/// Map the launcher's own messages onto stable error codes. Unknown text stays `Unknown` (details remain visible). fn has_launcher_settings(root: &Path) -> Result<()> {
pub fn translate(output: &str) -> ErrorCode { if !root.join("Settings/repack.json").is_file() {
let o = output.to_lowercase();
if o.contains("is already used") {
ErrorCode::PortInUse
} else if o.contains("another start/stop action is in progress")
|| o.contains("shutdown is still finishing")
{
ErrorCode::OperationInProgress
} else if o.contains("packaged database is missing")
|| o.contains("extract the complete repack")
{
ErrorCode::ServerFilesIncomplete
} else if o.contains("stopped during startup")
|| o.contains("is still starting")
|| o.contains("world startup failed")
{
ErrorCode::StartupFailed
} else if o.contains("invalid port") || o.contains("unresolved placeholder") {
ErrorCode::InvalidConfigValue
} else {
ErrorCode::Unknown
}
}
fn launcher(root: &Path) -> Result<(PathBuf, PathBuf)> {
let python = root.join("Runtime/python/python.exe");
let script = root.join("Scripts/manage.py");
if !python.is_file() || !script.is_file() {
return Err(Error::Invalid( return Err(Error::Invalid(
"This server has no CoA Repack launcher; start/stop is not available for it yet." "This server has no CoA Repack launcher; start/stop is not available for it yet."
.into(), .into(),
)); ));
} }
Ok((python, script)) Ok(())
} }
/// Run a launcher verb to completion (blocking; call from a worker thread). /// Run a launcher verb to completion (blocking; call from a worker thread).
@@ -181,6 +144,10 @@ fn run_inner_mode(
} }
if matches!(verb, Verb::StartAll | Verb::StartWorld) { if matches!(verb, Verb::StartAll | Verb::StartWorld) {
crate::modules::ensure_bot_exclusivity(&root)?; crate::modules::ensure_bot_exclusivity(&root)?;
// A server update that only added races keeps the owner's race restriction (and custom races on).
if let Err(error) = crate::race_availability::rebind_unchanged(&root) {
tracing::warn!(%error, "the race restriction could not be checked against the race catalog");
}
} }
if crate::docker::is_docker(&root) { if crate::docker::is_docker(&root) {
prepare_headless_world(&root, verb)?; prepare_headless_world(&root, verb)?;
@@ -205,108 +172,51 @@ fn run_inner_mode(
if matches!(verb, Verb::StartAll | Verb::StartWorld) { if matches!(verb, Verb::StartAll | Verb::StartWorld) {
crate::modules::ensure_bot_exclusivity(&root)?; crate::modules::ensure_bot_exclusivity(&root)?;
} }
let (python, script) = launcher(&root)?; has_launcher_settings(&root)?;
prepare_headless_world(&root, verb)?; prepare_headless_world(&root, verb)?;
let original = std::fs::read_to_string(&script)?;
let patched = patch_launcher_imports(&original)?;
if original != patched {
fsx::atomic_write(&script, patched.as_bytes())?;
if let Ok(dir) = crate::registry::metadata_dir_for(&root) {
if let Ok((_, mut meta)) = crate::registry::MetaDir::open(&dir) {
if meta.original_hashes.get("Scripts/manage.py")
== Some(&fsx::sha256_bytes(original.as_bytes()))
{
meta.original_hashes.insert(
"Scripts/manage.py".into(),
fsx::sha256_bytes(patched.as_bytes()),
);
fsx::atomic_write_json(&dir.join("install.json"), &meta)?;
}
}
}
}
discard_damaged_launcher_state(&root);
if matches!(verb, Verb::StopAll | Verb::StartMysql | Verb::StartAll) { if matches!(verb, Verb::StopAll | Verb::StartMysql | Verb::StartAll) {
recover_mysql_launcher_state(&root, verb != Verb::StopAll)?; recover_mysql_launcher_state(&root, verb != Verb::StopAll)?;
} }
if force { if force {
recover_world_for_forced_stop(&root)?; recover_world_for_forced_stop(&root)?;
} }
let mut cmd = Command::new(&python); let action = if force {
if validating_update { crate::launcher::ops::Action::ForceStopAll
// Isolated startup must use the port from its temporary auth config.
cmd.env_remove("AC_REALM_SERVER_PORT");
}
cmd.arg("-B")
.arg("-c")
.arg(LAUNCH_SCRIPT)
.arg(&script)
.arg(if force { "force-stop-all" } else { verb.arg() })
.current_dir(&root)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
}
tracing::info!(?verb, root = %root.display(), "driver: starting launcher verb");
let mut child = cmd.spawn()?;
let mut stdout = child.stdout.take().expect("piped");
let mut stderr = child.stderr.take().expect("piped");
let out_t = std::thread::spawn(move || {
let mut s = String::new();
let _ = stdout.read_to_string(&mut s);
s
});
let err_t = std::thread::spawn(move || {
let mut s = String::new();
let _ = stderr.read_to_string(&mut s);
s
});
let deadline = Instant::now() + verb.timeout(&root);
let status = loop {
if let Some(st) = child.try_wait()? {
break Some(st);
}
if Instant::now() > deadline {
// Never kill the servers themselves: only abandon the launcher process we spawned.
let _ = child.kill();
break None;
}
std::thread::sleep(Duration::from_millis(200));
};
let mut output = out_t.join().unwrap_or_default();
output.push_str(&err_t.join().unwrap_or_default());
let mut output = output.trim().to_string();
if status.is_none() {
output.push_str(&format!(
"\nLauncher {} exceeded its {} second deadline; check the server logs.",
verb.arg(),
verb.timeout(&root).as_secs()
));
}
let (ok, exit_code) = match status {
Some(st) => (st.success(), st.code()),
None => (false, None),
};
let code = if ok {
None
} else { } else {
// Log evidence beats the launcher's generic "stopped during startup" message. verb.action()
let base = if status.is_none() { };
ErrorCode::StartupFailed tracing::info!(?verb, root = %root.display(), "driver: starting launcher action");
} else { let lines = Mutex::new(Vec::<String>::new());
translate(&output) let progress = |line: &str| {
}; tracing::info!(?verb, "launcher: {line}");
match base { lines.lock().unwrap_or_else(|e| e.into_inner()).push(line.to_string());
ErrorCode::StartupFailed | ErrorCode::Unknown => { };
Some(crate::health::diagnose_installation(&root).unwrap_or(base)) let result = crate::launcher::Settings::load(&root, 0).and_then(|mut cfg| {
if validating_update {
// Isolated startup must use the port from its temporary auth config.
cfg.env_remove.push("AC_REALM_SERVER_PORT".into());
}
crate::launcher::ops::run(&root, &cfg, 0, action, None, &progress)
});
let mut output = lines.into_inner().unwrap_or_else(|e| e.into_inner()).join("\n");
let ok = result.is_ok();
let code = match &result {
Ok(()) => None,
Err(error) => {
if !output.is_empty() {
output.push('\n');
}
output.push_str(&format!("ERROR: {error}"));
// Log evidence beats a generic "stopped during startup".
match error.code() {
ErrorCode::StartupFailed | ErrorCode::Unknown => Some(
crate::health::diagnose_installation(&root).unwrap_or(error.code()),
),
other => Some(other),
} }
other => Some(other),
} }
}; };
let exit_code = Some(if ok { 0 } else { 1 });
if code == Some(ErrorCode::PortInUse) { if code == Some(ErrorCode::PortInUse) {
let observed = crate::process::observe(&root, &crate::layout::read_ports(&root)); let observed = crate::process::observe(&root, &crate::layout::read_ports(&root));
for service in [&observed.mysql, &observed.auth, &observed.world] for service in [&observed.mysql, &observed.auth, &observed.world]
@@ -374,35 +284,6 @@ fn prepare_headless_world(root: &Path, verb: Verb) -> Result<()> {
Ok(()) Ok(())
} }
/// The launcher keeps one JSON record per service in `.state`. A crash or power loss while it writes one can leave
/// a file of the right length that holds only zero bytes (NTFS keeps the length but not the data), and the launcher
/// then dies on every start with an unreadable record. Such a file carries no information, so remove it: the
/// launcher treats a missing record as "not running". Only empty or all-zero `*.json` files are touched.
pub(crate) fn discard_damaged_launcher_state(root: &Path) -> Vec<PathBuf> {
let mut removed = Vec::new();
let Ok(entries) = std::fs::read_dir(root.join(".state")) else {
return removed;
};
for entry in entries.flatten() {
let path = entry.path();
if path
.extension()
.is_none_or(|e| !e.eq_ignore_ascii_case("json"))
|| !path.is_file()
{
continue;
}
let Ok(bytes) = std::fs::read(&path) else {
continue;
};
if bytes.iter().all(|b| *b == 0) && std::fs::remove_file(&path).is_ok() {
tracing::warn!(file = %path.display(), bytes = bytes.len(), "driver: removed a launcher state file damaged by an interrupted write");
removed.push(path);
}
}
removed
}
/// A lost launcher record must not hide this installation's running MySQL from /// A lost launcher record must not hide this installation's running MySQL from
/// graceful shutdown. Adopt only the exact executable owning its configured port. /// graceful shutdown. Adopt only the exact executable owning its configured port.
fn recover_mysql_launcher_state(root: &Path, prefer_listener: bool) -> Result<()> { fn recover_mysql_launcher_state(root: &Path, prefer_listener: bool) -> Result<()> {
@@ -453,60 +334,6 @@ fn recover_mysql_launcher_state(root: &Path, prefer_listener: bool) -> Result<()
Ok(()) Ok(())
} }
const LAUNCH_SCRIPT: &str = include_str!("launcher_bridge.py");
pub(crate) fn patch_launcher_imports(text: &str) -> Result<String> {
let line = "sys.path.insert(0, str(Path(__file__).resolve().parent))";
if !text.contains("from squid_playerbots import") || text.contains(line) {
return Ok(text.into());
}
let root = "ROOT = Path(__file__).resolve().parents[1]";
if !text.contains(root) || !text.lines().any(|line| line.trim() == "import sys") {
return Err(Error::Invalid(
"The server launcher cannot load its integration scripts. Repair its program files."
.into(),
));
}
Ok(text.replacen(root, &format!("{root}\n{line}"), 1))
}
pub(crate) fn launcher_matches(signed: &str, actual: &[u8]) -> bool {
if signed.as_bytes() == actual {
return true;
}
if patch_launcher_imports(signed).is_ok_and(|s| s.as_bytes() == actual) {
return true;
}
crate::realms::patch_launcher(signed).is_ok_and(|s| {
s.as_bytes() == actual || patch_launcher_imports(&s).is_ok_and(|p| p.as_bytes() == actual)
})
}
/// Recover legacy metadata only when undoing supported Manager edits reproduces the recorded hash.
pub(crate) fn launcher_matches_recorded(recorded: &str, actual: &[u8]) -> bool {
let Ok(text) = std::str::from_utf8(actual) else {
return false;
};
let without_import = text.replacen(
"ROOT = Path(__file__).resolve().parents[1]\nsys.path.insert(0, str(Path(__file__).resolve().parent))",
"ROOT = Path(__file__).resolve().parents[1]", 1,
);
for candidate in [
Some(without_import.clone()),
crate::realms::unpatch_launcher(&without_import),
]
.into_iter()
.flatten()
{
if crate::fsx::sha256_bytes(candidate.as_bytes()).eq_ignore_ascii_case(recorded)
&& launcher_matches(&candidate, actual)
{
return true;
}
}
false
}
pub(crate) fn startup_failure(root: &Path, started: &DriverOutcome) -> String { pub(crate) fn startup_failure(root: &Path, started: &DriverOutcome) -> String {
let code = started let code = started
.code .code
@@ -608,7 +435,7 @@ mod tests {
text.contains("launcher request failed before completion") text.contains("launcher request failed before completion")
&& text.contains("in progress") && text.contains("in progress")
); );
assert!(!text.contains("starting launcher verb")); assert!(!text.contains("starting launcher action"));
} }
#[test] #[test]
@@ -665,78 +492,10 @@ mod tests {
} }
#[test] #[test]
fn slow_world_startup_has_time_with_either_bot_system_without_extending_shutdown() { fn launcher_failures_carry_their_error_code() {
let folder = tempfile::tempdir().unwrap(); let e = Error::Service(ErrorCode::PortInUse, "Port 8085 is already used (PID 4).".into());
let modules = folder.path().join("Core/configs/modules"); assert_eq!(e.code(), ErrorCode::PortInUse);
std::fs::create_dir_all(&modules).unwrap(); assert_eq!(e.to_string(), "Port 8085 is already used (PID 4).");
std::fs::write(
modules.join("playerbots.conf.dist"),
"AiPlayerbot.Enabled = 1\n",
)
.unwrap();
assert_eq!(
Verb::StartAll.timeout(folder.path()),
Duration::from_secs(3600)
);
assert_eq!(
Verb::StopAll.timeout(folder.path()),
Duration::from_secs(240)
);
std::fs::write(modules.join("playerbots.conf"), "AiPlayerbot.Enabled = 0\n").unwrap();
assert_eq!(
Verb::StartWorld.timeout(folder.path()),
Duration::from_secs(3600)
);
assert_eq!(
Verb::StartMysql.timeout(folder.path()),
Duration::from_secs(360)
);
}
#[test]
fn zero_filled_launcher_state_is_discarded_and_valid_state_is_kept() {
let folder = tempfile::tempdir().unwrap();
let state = folder.path().join(".state");
std::fs::create_dir_all(&state).unwrap();
std::fs::write(state.join("mysql.json"), vec![0u8; 116]).unwrap();
std::fs::write(state.join("configuration.json"), Vec::<u8>::new()).unwrap();
std::fs::write(state.join("world.json"), "{\"pid\": 4}\n").unwrap();
std::fs::write(state.join("stop-relay"), Vec::<u8>::new()).unwrap();
std::fs::write(state.join("auth.json"), "{\"pid\": 0}").unwrap();
let mut removed: Vec<_> = discard_damaged_launcher_state(folder.path())
.iter()
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
.collect();
removed.sort();
assert_eq!(removed, ["configuration.json", "mysql.json"]);
assert!(
state.join("world.json").exists()
&& state.join("auth.json").exists()
&& state.join("stop-relay").exists()
);
assert!(discard_damaged_launcher_state(folder.path()).is_empty());
assert!(discard_damaged_launcher_state(&folder.path().join("missing")).is_empty());
}
#[test]
fn translates_known_launcher_messages() {
assert_eq!(
translate("RuntimeError: Port 8085 is already used. Stop the other server"),
ErrorCode::PortInUse
);
assert_eq!(
translate("Another start/stop action is in progress."),
ErrorCode::OperationInProgress
);
assert_eq!(
translate("The packaged database is missing. Extract the complete repack."),
ErrorCode::ServerFilesIncomplete
);
assert_eq!(
translate("world stopped during startup. Check its log."),
ErrorCode::StartupFailed
);
assert_eq!(translate("Exit code -1073741819"), ErrorCode::Unknown);
} }
#[test] #[test]
@@ -779,13 +538,4 @@ mod tests {
assert!(!reason.contains("private")); assert!(!reason.contains("private"));
assert!(!reason.contains("Something went wrong")); assert!(!reason.contains("Something went wrong"));
} }
#[test]
fn launcher_import_fix_is_idempotent_and_retains_other_code() {
let text = "import sys\nROOT = Path(__file__).resolve().parents[1]\nfrom squid_playerbots import validate_bots\ncustom = 42\n";
let fixed = patch_launcher_imports(text).unwrap();
assert!(fixed.contains("sys.path.insert(0, str(Path(__file__).resolve().parent))"));
assert!(fixed.ends_with("custom = 42\n"));
assert_eq!(patch_launcher_imports(&fixed).unwrap(), fixed);
}
} }
+5 -1
View File
@@ -24,6 +24,9 @@ pub enum Error {
UnknownInstallation(String), UnknownInstallation(String),
#[error("{0}")] #[error("{0}")]
Invalid(String), Invalid(String),
/// A server start or stop failed for a reason the UI can explain (the repack launcher's own errors).
#[error("{1}")]
Service(ErrorCode, String),
#[error("package not published: {0}")] #[error("package not published: {0}")]
PackageNotPublished(String), PackageNotPublished(String),
#[error("download server unreachable: {0}")] #[error("download server unreachable: {0}")]
@@ -175,7 +178,7 @@ impl ErrorCode {
), ),
ErrorCode::CompanionCommandUnsupported => ( ErrorCode::CompanionCommandUnsupported => (
"This server version can not do that yet", "This server version can not do that yet",
"The bot module of this server build has no such command. Update the server, or restart it to stop what is running.", "The bot module on this server is older than this Manager and does not know this command. Update the bot module on the Modules page (or update the server), then restart the world.",
&[ShowDetails], &[ShowDetails],
), ),
ErrorCode::Unknown => ("Something went wrong", "See the technical details.", &[ShowDetails]), ErrorCode::Unknown => ("Something went wrong", "See the technical details.", &[ShowDetails]),
@@ -199,6 +202,7 @@ impl Error {
Error::PackageNotPublished(_) => ErrorCode::PackageNotPublished, Error::PackageNotPublished(_) => ErrorCode::PackageNotPublished,
Error::NetworkUnreachable(_) => ErrorCode::NetworkUnreachable, Error::NetworkUnreachable(_) => ErrorCode::NetworkUnreachable,
Error::BotModulesConflict => ErrorCode::BotModulesConflict, Error::BotModulesConflict => ErrorCode::BotModulesConflict,
Error::Service(code, _) => *code,
Error::CompanionTemplatesMissing => ErrorCode::CompanionTemplatesMissing, Error::CompanionTemplatesMissing => ErrorCode::CompanionTemplatesMissing,
Error::CompanionCommandUnsupported => ErrorCode::CompanionCommandUnsupported, Error::CompanionCommandUnsupported => ErrorCode::CompanionCommandUnsupported,
_ => ErrorCode::Unknown, _ => ErrorCode::Unknown,
+16 -1
View File
@@ -154,6 +154,14 @@ pub fn safe_join(root: &Path, rel: &str) -> Result<PathBuf> {
/// Write `bytes` to `path` so readers see either the old or the new content, never a partial file. /// Write `bytes` to `path` so readers see either the old or the new content, never a partial file.
pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> { pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
atomic_write_inner(path, bytes, false)
}
pub(crate) fn atomic_write_private(path: &Path, bytes: &[u8]) -> Result<()> {
atomic_write_inner(path, bytes, true)
}
fn atomic_write_inner(path: &Path, bytes: &[u8], _private: bool) -> Result<()> {
let parent = path let parent = path
.parent() .parent()
.ok_or_else(|| Error::PathRejected("no parent directory".into()))?; .ok_or_else(|| Error::PathRejected("no parent directory".into()))?;
@@ -166,7 +174,14 @@ pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
uuid::Uuid::new_v4().simple() uuid::Uuid::new_v4().simple()
)); ));
let write = || -> std::io::Result<()> { let write = || -> std::io::Result<()> {
let mut f = OpenOptions::new().write(true).create_new(true).open(&tmp)?; let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
if _private {
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut f = options.open(&tmp)?;
f.write_all(bytes)?; f.write_all(bytes)?;
f.sync_all()?; f.sync_all()?;
Ok(()) Ok(())
+13 -10
View File
@@ -210,7 +210,7 @@ pub(crate) fn free_port() -> Result<u16> {
/// Run `f` with the staging copy listening on unused ports, so an installation never collides with a server (or any /// Run `f` with the staging copy listening on unused ports, so an installation never collides with a server (or any
/// other program) already using the shipped ports. The shipped ports are put back afterwards. /// other program) already using the shipped ports. The shipped ports are put back afterwards.
pub(crate) fn with_scratch_ports<T>(root: &Path, f: impl FnOnce() -> Result<T>) -> Result<T> { pub fn with_scratch_ports<T>(root: &Path, f: impl FnOnce() -> Result<T>) -> Result<T> {
const KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"]; const KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"];
let path = root.join("Settings/repack.json"); let path = root.join("Settings/repack.json");
let original: serde_json::Value = fsx::read_json(&path)?; let original: serde_json::Value = fsx::read_json(&path)?;
@@ -378,15 +378,18 @@ pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
})?; })?;
say("Preparing database", 82, None); say("Preparing database", 82, None);
bootstrap_database(&staging_root)?; bootstrap_database(&staging_root)?;
crate::backup::with_database(&staging_root, |db| { // On unused ports too: another server running on the shipped ports must not stop the installation.
let problems = crate::schema_check::check(db, &staging_root)?; with_scratch_ports(&staging_root, || {
if let Some(p) = problems.first() { crate::backup::with_database(&staging_root, |db| {
return Err(Error::Invalid(format!( let problems = crate::schema_check::check(db, &staging_root)?;
"The packaged database is incomplete: {}.{}.{}: {}.", if let Some(p) = problems.first() {
p.database, p.table, p.column, p.detail return Err(Error::Invalid(format!(
))); "The packaged database is incomplete: {}.{}.{}: {}.",
} p.database, p.table, p.column, p.detail
Ok(()) )));
}
Ok(())
})
})?; })?;
// 4. Commit: the only moment the destination changes. The marker stays until everything is done, so an // 4. Commit: the only moment the destination changes. The marker stays until everything is done, so an
// unfinished install can always be recognised as ours (and cleaned up), wherever it stopped. // unfinished install can always be recognised as ours (and cleaned up), wherever it stopped.
+25
View File
@@ -0,0 +1,25 @@
//! The repack launcher in Rust: what `Scripts/manage.py` does, without Python.
//!
//! Formats on disk stay exactly those of `manage.py`, so the two can be used on the same server during the move
//! (an old `.bat` and a new Manager, or the other way round):
//!
//! * `.state/<service>.json` process records `{pid, exe, created}` (see [`crate::process::ProcessIdentity`]);
//! * `.state/control.lock` one start/stop action at a time: byte 0 locked, as `msvcrt.locking` does;
//! * `.state/control-owner.json`, `.state/configuration.json`, `.state/debug-logs.json`;
//! * the rendered configs (`Core/configs/*.conf`, `mysql/my.ini`, `mysql/admin-client.ini`) byte for byte.
//!
//! State files are written with a flush to disk before they replace the old ones (`manage.py` did not, and a
//! power cut could leave a zero-filled record that stopped every later start).
pub mod ops;
pub mod relay;
pub mod render;
pub mod schedule;
pub mod services;
pub mod settings;
pub mod spawn;
pub mod squid;
pub mod state;
pub mod world;
pub use settings::Settings;
+103
View File
@@ -0,0 +1,103 @@
//! One start/stop action of a repack, with the control lock held (`manage.py main()`): the entry point of both
//! `coa-launcher.exe` and the Manager.
use std::path::Path;
use std::time::Duration;
use crate::error::Result;
use crate::launcher::services::{self, Progress};
use crate::launcher::world::{self, Supervisor};
use crate::launcher::{render, settings::Settings, squid, state};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
StartAll,
StartMysql,
StartAuth,
StartWorld,
StopAll,
/// Stop the world without its console first (unsaved progress is lost), then everything else.
ForceStopAll,
Prepare,
}
impl Action {
/// The command name, as the `.bat` files and `manage.py` call it.
pub fn name(self) -> &'static str {
match self {
Action::StartAll => "start-all",
Action::StartMysql => "start-mysql",
Action::StartAuth => "start-auth",
Action::StartWorld => "start-world",
Action::StopAll => "stop-all",
Action::ForceStopAll => "force-stop-all",
Action::Prepare => "prepare",
}
}
pub fn parse(name: &str) -> Option<Action> {
[
Action::StartAll,
Action::StartMysql,
Action::StartAuth,
Action::StartWorld,
Action::StopAll,
Action::ForceStopAll,
Action::Prepare,
]
.into_iter()
.find(|a| a.name() == name)
}
}
/// Run `action`. A started world is supervised by `supervisor`, or by what the repack ships when it is `None`.
pub fn run(root: &Path, cfg: &Settings, offset: i32, action: Action, supervisor: Option<&Supervisor>, progress: Progress) -> Result<()> {
let _lock = state::ControlLock::acquire(root, action.name(), Duration::from_secs(30), progress)?;
match action {
// Shutdown uses the existing client and console settings: the templates may be exactly what a recovery
// needs to replace, so nothing is rendered.
Action::StopAll => return services::stop_all(root, cfg, progress),
Action::ForceStopAll => return services::force_stop_all(root, cfg, progress),
_ => {}
}
if matches!(action, Action::StartAll | Action::StartWorld) {
squid::validate_bots(root)?;
}
let relocated;
let cfg = match action {
Action::StartAll | Action::StartMysql | Action::StartAuth | Action::StartWorld => {
match services::relocate_database_port(root, cfg, offset, progress)? {
Some(_) => {
let mut moved = Settings::load(root, offset)?;
moved.env_remove = cfg.env_remove.clone();
relocated = moved;
&relocated
}
None => cfg,
}
}
_ => cfg,
};
render::prepare(root, cfg)?;
if action == Action::Prepare {
return Ok(());
}
if matches!(action, Action::StartAll | Action::StartMysql) {
services::start_mysql(root, cfg, progress)?;
}
if matches!(action, Action::StartAll | Action::StartAuth) {
services::start_auth(root, cfg, progress)?;
}
if matches!(action, Action::StartAll | Action::StartWorld) {
let shipped;
let supervisor = match supervisor {
Some(s) => s,
None => {
shipped = Supervisor::of(root)?;
&shipped
}
};
world::start_world(root, cfg, offset, supervisor, progress)?;
}
Ok(())
}
+493
View File
@@ -0,0 +1,493 @@
//! The bug-report relay (`BugReport/relay.py` and `manage.py relay-worker`): delivers the reports the world
//! writes to `BugReport/reports/*.report` through the CoA report service.
//!
//! On disk everything stays as the Python relay keeps it, so either can take over a spool: the `<key>.status`
//! files the world reads back, the delivery journal `relay.sqlite3` (tables `deliveries` and `settings`), and
//! `relay.lock` (byte 0 locked while a worker runs). A delivery interrupted while it was posting becomes
//! `uncertain` and is never sent again (the service's duplicate filter may have been reset in the meantime).
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use rusqlite::{params, Connection, OptionalExtension};
use sha2::{Digest, Sha256};
use crate::error::{Error, Result};
use crate::launcher::state;
const REPOSITORY: &str = "jealous-sound/azerothcore-wotlk-coa";
pub const SERVICE_URL: &str = "https://coa-bug-report.up.railway.app";
/// Distribution credential for this narrow intake API (the same as relay.py's); not a GitHub token.
pub const DEFAULT_API_KEY: &str = "coa_zRatxUHShOn-HEkoreKGME6sd3DdMG7eKTTKlj2JCK4";
const MAX_FILE: u64 = 16000;
const MAX_RESPONSE: usize = 64 * 1024;
/// `[1-9][0-9]{0,9}-[0-9a-f]{16,48}`: the name of a report file (without `.report`).
pub fn valid_key(key: &str) -> bool {
let Some((id, hex)) = key.split_once('-') else { return false };
(1..=10).contains(&id.len())
&& !id.starts_with('0')
&& id.bytes().all(|b| b.is_ascii_digit())
&& (16..=48).contains(&hex.len())
&& hex.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
/// Why a delivery did not create an issue. Only fixed local codes: never service bodies, credentials or report
/// content.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Failure {
/// 202 pending: the service is still creating it.
Pending(u64),
/// 429: try again later.
Limited(u64),
/// The report itself was refused.
Invalid,
/// The service refused this relay (redirect, auth, missing endpoint, GitHub access).
Blocked(u64),
/// The outcome is unknown (network, timeout, unexpected answer).
Uncertain,
}
/// Where reports go.
pub trait Service {
fn create(&self, title: &str, body: &str) -> std::result::Result<u64, Failure>;
}
/// The CoA report service over HTTPS.
pub struct ReportService {
api: String,
key: String,
client: reqwest::blocking::Client,
}
impl ReportService {
pub fn new(base_url: &str, api_key: &str) -> Result<ReportService> {
if api_key.is_empty() || !api_key.is_ascii() || api_key.chars().any(char::is_whitespace) {
return Err(Error::Invalid("Configure a valid report-service intake key.".into()));
}
let client = reqwest::blocking::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(Duration::from_secs(30))
// One report every few seconds at most: a fresh connection each time, never a stale pooled one.
.pool_max_idle_per_host(0)
.user_agent("CoA-InGame-BugReport/1")
.build()
.map_err(|e| Error::Invalid(format!("The report client could not start: {e}")))?;
Ok(ReportService { api: format!("{base_url}/v1/reports"), key: api_key.into(), client })
}
}
/// `Retry-After` in seconds, kept within 1 s .. 1 day; `default` when absent or not a number.
fn retry_delay(retry_after: Option<&str>, default: u64) -> u64 {
retry_after.and_then(|v| v.trim().parse::<i64>().ok()).map_or(default, |v| v.clamp(1, 86400) as u64)
}
/// A JSON object of at most 64 KiB, or nothing.
fn json_object(raw: Option<&[u8]>) -> Option<serde_json::Map<String, serde_json::Value>> {
let raw = raw?;
if raw.len() > MAX_RESPONSE {
return None;
}
match serde_json::from_slice(raw).ok()? {
serde_json::Value::Object(map) => Some(map),
_ => None,
}
}
fn issue_number(data: &serde_json::Map<String, serde_json::Value>) -> std::result::Result<u64, Failure> {
let number = data.get("issue_number").and_then(|n| if n.is_u64() { n.as_u64() } else { None });
match number {
Some(n)
if data.get("status").and_then(|s| s.as_str()) == Some("created")
&& (1..=4294967295).contains(&n)
&& data.get("issue_url").and_then(|u| u.as_str())
== Some(format!("https://github.com/{REPOSITORY}/issues/{n}").as_str()) =>
{
Ok(n)
}
_ => Err(Failure::Uncertain),
}
}
/// What a service answer means (relay.py's `create`): the status, its `Retry-After` and its body (`None` when it
/// could not be read or was longer than 64 KiB).
pub fn classify(status: u16, retry_after: Option<&str>, body: Option<&[u8]>) -> std::result::Result<u64, Failure> {
let field = |data: &serde_json::Map<String, serde_json::Value>, k: &str| data.get(k).and_then(|v| v.as_str()).map(str::to_owned);
match status {
200..=202 => {
let data = json_object(body).ok_or(Failure::Uncertain)?;
match status {
202 if field(&data, "status").as_deref() == Some("pending") => Err(Failure::Pending(retry_delay(retry_after, 2))),
202 => Err(Failure::Uncertain),
_ => issue_number(&data),
}
}
429 => Err(Failure::Limited(retry_delay(retry_after, 300))),
400 | 413 | 415 | 422 => Err(Failure::Invalid),
301 | 302 | 303 | 307 | 308 | 401 | 403 | 404 | 405 => Err(Failure::Blocked(300)),
503 => {
let data = json_object(body).unwrap_or_default();
if field(&data, "status").as_deref() == Some("failed") && field(&data, "error").as_deref() == Some("github_access_denied") {
Err(Failure::Blocked(retry_delay(retry_after, 300)))
} else {
Err(Failure::Uncertain)
}
}
_ => Err(Failure::Uncertain),
}
}
impl Service for ReportService {
fn create(&self, title: &str, body: &str) -> std::result::Result<u64, Failure> {
use std::io::Read;
let sent = self
.client
.post(&self.api)
.header("Authorization", format!("Bearer {}", self.key))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(serde_json::json!({ "title": title, "body": body }).to_string())
.send();
let response = sent.map_err(|_| Failure::Uncertain)?;
let status = response.status().as_u16();
let retry_after = response.headers().get("Retry-After").and_then(|v| v.to_str().ok()).map(str::to_owned);
let mut raw = Vec::new();
let read = response.take(MAX_RESPONSE as u64 + 1).read_to_end(&mut raw).is_ok() && raw.len() <= MAX_RESPONSE;
classify(status, retry_after.as_deref(), read.then_some(raw.as_slice()))
}
}
/// A report: `COABUG1\n<title>\n<body>`, checked as the world writes it.
pub struct Report {
pub digest: String,
pub marker: String,
pub title: String,
pub body: String,
}
pub fn read_report(path: &Path) -> Result<Report> {
let bad = || Error::Invalid("Invalid report file".into());
let key = path.file_stem().and_then(|s| s.to_str()).ok_or_else(bad)?;
let meta = std::fs::symlink_metadata(path)?;
if !valid_key(key) || meta.file_type().is_symlink() || meta.len() > MAX_FILE {
return Err(bad());
}
let raw = std::fs::read(path)?;
let text = String::from_utf8(raw.clone()).map_err(|_| bad())?.replace("\r\n", "\n");
let rest = text.strip_prefix("COABUG1\n").ok_or_else(|| Error::Invalid("Unsupported report format".into()))?;
let invalid = || Error::Invalid("Invalid report content".into());
let (title, body) = rest.split_once('\n').ok_or_else(invalid)?;
if !(3..=200).contains(&title.len())
|| body.trim().is_empty()
|| title.chars().any(|c| (c as u32) < 32)
|| body.chars().any(|c| (c as u32) < 32 && !matches!(c, '\r' | '\n' | '\t'))
{
return Err(invalid());
}
let digest = hex(&Sha256::digest(&raw));
let id = hex(&Sha256::digest(format!("{key}:{digest}").as_bytes()));
Ok(Report { digest, marker: format!("<!-- coa-report:{id} -->"), title: title.into(), body: body.into() })
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
/// `<key>.status`: what the world shows the player (`queued`, `created|<issue>`, `failed`, ...).
pub fn write_status(spool: &Path, key: &str, state: &str, number: Option<u64>) -> Result<()> {
let value = match (state, number) {
("created", Some(n)) => format!("created|{n}"),
("created", None) => "created|None".into(),
_ => state.to_string(),
};
let data = format!("{value}\n");
let destination = spool.join(format!("{key}.status"));
if std::fs::read(&destination).is_ok_and(|d| d == data.as_bytes()) {
return Ok(());
}
crate::fsx::atomic_write(&destination, data.as_bytes())
}
fn now() -> f64 {
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs_f64()).unwrap_or(0.0)
}
/// The delivery journal of one spool.
pub struct Relay<S: Service> {
spool: PathBuf,
service: S,
db: Connection,
clock: Box<dyn Fn() -> f64 + Send>,
}
impl<S: Service> Relay<S> {
pub fn open(spool: &Path, service: S) -> Result<Relay<S>> {
Self::with_clock(spool, service, Box::new(now))
}
pub fn with_clock(spool: &Path, service: S, clock: Box<dyn Fn() -> f64 + Send>) -> Result<Relay<S>> {
let db = Connection::open(spool.join("relay.sqlite3")).map_err(sql)?;
db.execute_batch(
"PRAGMA synchronous=FULL;
CREATE TABLE IF NOT EXISTS deliveries (
key TEXT PRIMARY KEY, digest TEXT NOT NULL, marker TEXT NOT NULL, state TEXT NOT NULL,
number INTEGER, attempted REAL NOT NULL DEFAULT 0, next_try REAL NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value REAL NOT NULL);
UPDATE deliveries SET state='uncertain' WHERE state='posting';",
)
.map_err(sql)?;
Ok(Relay { spool: spool.into(), service, db, clock })
}
fn set_state(&self, key: &str, state: &str, number: Option<u64>, delay: u64) -> Result<()> {
self.db
.execute(
"UPDATE deliveries SET state=?1, number=?2, next_try=?3 WHERE key=?4",
params![state, number.map(|n| n as i64), (self.clock)() + delay as f64, key],
)
.map_err(sql)?;
write_status(&self.spool, key, state, number)
}
fn set_network_gate(&self, delay: u64) -> Result<()> {
self.db
.execute("INSERT OR REPLACE INTO settings VALUES('next_network',?1)", params![(self.clock)() + delay as f64])
.map_err(sql)?;
Ok(())
}
/// Handle one report file; returns its delivery state.
pub fn process(&self, path: &Path) -> Result<String> {
let Some(key) = path.file_stem().and_then(|s| s.to_str()).filter(|k| valid_key(k)) else {
return Ok("ignored".into());
};
let report = match read_report(path) {
Ok(r) => r,
Err(_) => {
write_status(&self.spool, key, "failed", None)?;
return Ok("failed".into());
}
};
self.db
.execute(
"INSERT OR IGNORE INTO deliveries(key,digest,marker,state) VALUES(?1,?2,?3,'queued')",
params![key, report.digest, report.marker],
)
.map_err(sql)?;
let (digest, row_state, number, next_try): (String, String, Option<i64>, f64) = self
.db
.query_row("SELECT digest, state, number, next_try FROM deliveries WHERE key=?1", params![key], |r| {
Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?))
})
.map_err(sql)?;
let number = number.map(|n| n as u64);
if digest != report.digest {
// Never replace an existing delivery or publish edited content under the same ID.
write_status(&self.spool, key, "failed", None)?;
return Ok("changed".into());
}
if matches!(row_state.as_str(), "created" | "failed" | "uncertain") {
write_status(&self.spool, key, &row_state, number)?;
return Ok(row_state);
}
if next_try > (self.clock)() {
write_status(&self.spool, key, &row_state, None)?;
return Ok(row_state);
}
let gate: Option<f64> = self
.db
.query_row("SELECT value FROM settings WHERE key='next_network'", [], |r| r.get(0))
.optional()
.map_err(sql)?;
if gate.is_some_and(|g| g > (self.clock)()) {
write_status(&self.spool, key, &row_state, None)?;
return Ok(row_state);
}
self.set_network_gate(5)?;
// A crash after this point needs review: the service's duplicate cache can reset on deploy.
self.db
.execute("UPDATE deliveries SET state='posting', attempted=?1 WHERE key=?2", params![(self.clock)(), key])
.map_err(sql)?;
match self.service.create(&report.title, &report.body) {
Ok(n) => {
self.set_state(key, "created", Some(n), 0)?;
Ok("created".into())
}
Err(failure) => {
let (state, delay) = match failure {
Failure::Limited(d) | Failure::Pending(d) => ("queued", d),
Failure::Blocked(d) => ("blocked", d),
Failure::Invalid => ("failed", 300),
Failure::Uncertain => ("uncertain", 300),
};
self.set_state(key, state, None, delay)?;
if let Failure::Limited(d) | Failure::Blocked(d) = failure {
self.set_network_gate(d)?;
}
Ok(state.into())
}
}
}
/// One pass over the spool, in name order.
pub fn pass(&self) -> Result<()> {
let mut reports: Vec<PathBuf> = std::fs::read_dir(&self.spool)?
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|x| x == "report"))
.collect();
reports.sort();
for path in reports {
self.process(&path)?;
}
Ok(())
}
}
fn sql(e: rusqlite::Error) -> Error {
Error::Invalid(format!("The bug-report journal failed ({e})."))
}
/// `relay-worker`: deliver reports until the supervisor asks the relay to stop (`.state/stop-relay`).
pub fn worker(root: &Path) -> Result<()> {
let spool = root.join("BugReport/reports");
std::fs::create_dir_all(&spool)?;
let _lock = state::FileLock::try_acquire(&spool.join("relay.lock"))?
.ok_or_else(|| Error::Invalid("Another bug-report relay is already using this spool.".into()))?;
let key = std::env::var("COA_BUGREPORT_API_KEY").unwrap_or_else(|_| DEFAULT_API_KEY.into());
let relay = Relay::open(&spool, ReportService::new(SERVICE_URL, &key)?)?;
state::record(root, "relay-ready", std::process::id())?;
println!("Bug-report relay ready. Waiting for in-game reports.");
let stop = state::dir(root).join("stop-relay");
while !stop.exists() {
relay.pass()?;
for _ in 0..10 {
if stop.exists() {
return Ok(());
}
std::thread::sleep(Duration::from_millis(500));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Mutex};
type Answers = Arc<Mutex<Vec<std::result::Result<u64, Failure>>>>;
struct Fake(Answers, Arc<Mutex<u32>>);
impl Service for Fake {
fn create(&self, _: &str, _: &str) -> std::result::Result<u64, Failure> {
*self.1.lock().unwrap() += 1;
self.0.lock().unwrap().remove(0)
}
}
fn report(spool: &Path, key: &str, text: &str) -> PathBuf {
let p = spool.join(format!("{key}.report"));
std::fs::write(&p, text).unwrap();
p
}
#[test]
fn keys_follow_the_world_format() {
assert!(valid_key("1-0123456789abcdef"));
assert!(valid_key("4294967295-0123456789abcdef0123456789abcdef0123456789abcdef"));
for bad in ["0-0123456789abcdef", "1-0123456789abcde", "1-0123456789ABCDEF", "12345678901-0123456789abcdef", "x"] {
assert!(!valid_key(bad), "{bad}");
}
}
#[test]
fn service_answers_map_like_relay_py() {
let ok = format!(r#"{{"status":"created","issue_number":12,"issue_url":"https://github.com/{REPOSITORY}/issues/12"}}"#);
let cases: Vec<(u16, Option<&str>, Option<&[u8]>, std::result::Result<u64, Failure>)> = vec![
(201, None, Some(ok.as_bytes()), Ok(12)),
(200, None, Some(ok.as_bytes()), Ok(12)),
(201, None, Some(br#"{"status":"created","issue_number":12,"issue_url":"https://evil/12"}"#), Err(Failure::Uncertain)),
(201, None, Some(br#"{"status":"created","issue_number":0,"issue_url":"x"}"#), Err(Failure::Uncertain)),
(201, None, Some(b"[1]"), Err(Failure::Uncertain)),
(201, None, None, Err(Failure::Uncertain)),
(202, Some("7"), Some(br#"{"status":"pending"}"#), Err(Failure::Pending(7))),
(202, None, Some(br#"{"status":"pending"}"#), Err(Failure::Pending(2))),
(202, None, Some(br#"{"status":"other"}"#), Err(Failure::Uncertain)),
(429, Some("999999"), None, Err(Failure::Limited(86400))),
(429, Some("0"), None, Err(Failure::Limited(1))),
(429, Some("soon"), None, Err(Failure::Limited(300))),
(422, None, None, Err(Failure::Invalid)),
(302, None, None, Err(Failure::Blocked(300))),
(401, None, None, Err(Failure::Blocked(300))),
(503, Some("30"), Some(br#"{"status":"failed","error":"github_access_denied"}"#), Err(Failure::Blocked(30))),
(503, None, Some(b"{}"), Err(Failure::Uncertain)),
(500, None, None, Err(Failure::Uncertain)),
];
for (i, (status, retry, body, expected)) in cases.into_iter().enumerate() {
assert_eq!(classify(status, retry, body), expected, "case {i}");
}
assert!(json_object(Some(&vec![b' '; MAX_RESPONSE + 1])).is_none(), "oversized answers are refused");
assert!(ReportService::new(SERVICE_URL, "has space").is_err());
assert!(ReportService::new(SERVICE_URL, "coa_key").is_ok());
}
#[test]
fn a_report_is_checked_and_identified_like_relay_py() {
let d = tempfile::tempdir().unwrap();
let p = report(d.path(), "7-0123456789abcdef", "COABUG1\r\nBroken quest\r\nThe NPC is missing.\r\n");
let r = read_report(&p).unwrap();
assert_eq!(r.title, "Broken quest");
assert_eq!(r.body, "The NPC is missing.\n");
assert_eq!(r.digest, hex(&Sha256::digest(std::fs::read(&p).unwrap())));
assert!(r.marker.starts_with("<!-- coa-report:") && r.marker.len() == 16 + 64 + 4);
for bad in ["COABUG2\nTitle\nBody", "COABUG1\nab\nBody", "COABUG1\nTitle\n \n", "COABUG1\nTi\u{7}tle\nBody", "COABUG1\nTitle"] {
std::fs::write(&p, bad).unwrap();
assert!(read_report(&p).is_err(), "{bad:?}");
}
}
#[test]
fn deliveries_follow_the_journal() {
let d = tempfile::tempdir().unwrap();
let spool = d.path();
let t = Arc::new(Mutex::new(1000.0f64));
let calls = Arc::new(Mutex::new(0));
let answers: Answers = Arc::new(Mutex::new(vec![Err(Failure::Limited(60)), Ok(42)]));
let clock = t.clone();
let relay = Relay::with_clock(spool, Fake(answers, calls.clone()), Box::new(move || *clock.lock().unwrap())).unwrap();
let p = report(spool, "7-0123456789abcdef", "COABUG1\nBroken quest\nThe NPC is missing.\n");
let status = || std::fs::read_to_string(spool.join("7-0123456789abcdef.status")).unwrap();
assert_eq!(relay.process(&p).unwrap(), "queued", "429 keeps it queued");
assert_eq!(status(), "queued\n");
assert_eq!(relay.process(&p).unwrap(), "queued", "not before its retry time");
assert_eq!(*calls.lock().unwrap(), 1);
*t.lock().unwrap() += 61.0;
assert_eq!(relay.process(&p).unwrap(), "created");
assert_eq!(status(), "created|42\n");
assert_eq!(relay.process(&p).unwrap(), "created", "never sent twice");
assert_eq!(*calls.lock().unwrap(), 2);
std::fs::write(&p, "COABUG1\nBroken quest\nEdited.\n").unwrap();
assert_eq!(relay.process(&p).unwrap(), "changed");
relay
.db
.execute("INSERT INTO deliveries(key,digest,marker,state) VALUES('8-0123456789abcdef','x','m','posting')", [])
.unwrap();
drop(relay);
let reopened = Relay::open(spool, Fake(Arc::new(Mutex::new(vec![])), calls.clone())).unwrap();
let state: String =
reopened.db.query_row("SELECT state FROM deliveries WHERE key='8-0123456789abcdef'", [], |r| r.get(0)).unwrap();
assert_eq!(state, "uncertain", "an interrupted post is never retried");
}
#[test]
fn a_second_worker_cannot_take_the_spool() {
let d = tempfile::tempdir().unwrap();
let first = state::FileLock::try_acquire(&d.path().join("relay.lock")).unwrap();
assert!(first.is_some());
assert!(state::FileLock::try_acquire(&d.path().join("relay.lock")).unwrap().is_none());
drop(first);
assert!(state::FileLock::try_acquire(&d.path().join("relay.lock")).unwrap().is_some());
}
}
+367
View File
@@ -0,0 +1,367 @@
//! `prepare`: the configuration files a repack runs with, rendered from `Settings/*.template` and
//! `Settings/repack.json` (`manage.py prepare()`), byte for byte the same.
use std::path::{Path, PathBuf};
use crate::error::{Error, Result};
use crate::launcher::{settings::Settings, state};
/// Services whose running process blocks moving the folder or changing ports.
const SERVICES: [&str; 5] = ["mysql", "auth", "world", "supervisor", "relay"];
/// The root as the servers see it: forward slashes, and the short (8.3) form when the real one has non-Latin
/// letters, which MySQL and the servers do not read reliably from their config files.
pub fn native_root(root: &Path) -> Result<String> {
let mut value = root.to_string_lossy().into_owned();
if !value.is_ascii() {
if let Some(short) = sys::short_path(root) {
value = short;
}
}
if !value.is_ascii() {
return Err(Error::Invalid(
"Extract the repack to a path with Latin letters, such as C:\\Games\\CoA-Repack.".into(),
));
}
Ok(value.replace('\\', "/"))
}
/// Text as Python's text mode reads it: strict UTF-8, Windows and old Mac line ends turned into "\n".
fn read_text(path: &Path) -> Result<String> {
let bytes = std::fs::read(path)?;
let text = String::from_utf8(bytes).map_err(|_| Error::Invalid(format!("{} is not UTF-8 text.", path.display())))?;
Ok(text.replace("\r\n", "\n").replace('\r', "\n"))
}
/// Text written as Python's text mode writes it on Windows: "\n" becomes "\r\n".
fn windows_text(text: &str) -> Vec<u8> {
if cfg!(windows) {
text.replace('\n', "\r\n").into_bytes()
} else {
text.as_bytes().to_vec()
}
}
fn placeholders(line: &str) -> Vec<String> {
let mut out = Vec::new();
let bytes = line.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'@' {
let mut j = i + 1;
while j < bytes.len() && (bytes[j].is_ascii_alphabetic() || bytes[j] == b'_') {
j += 1;
}
if j < bytes.len() && j > i + 1 && bytes[j] == b'@' {
out.push(line[i..=j].to_string());
i = j + 1;
continue;
}
}
i += 1;
}
out
}
fn credentials(root: &Path) -> Result<(String, String)> {
let v: serde_json::Value = crate::fsx::read_json(&root.join("Settings/database.json"))?;
let good = |k: &str| {
v.get(k)
.and_then(|x| x.as_str())
.filter(|s| s.len() == 48 && s.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)))
.map(str::to_string)
};
match (good("rootPassword"), good("appPassword")) {
(Some(r), Some(a)) => Ok((r, a)),
_ => Err(Error::Invalid("Invalid packaged database configuration.".into())),
}
}
/// Render every file the repack's services read. Refuses while a service runs and the folder or the ports
/// changed since the last render.
pub fn prepare(root: &Path, config: &Settings) -> Result<Vec<PathBuf>> {
let base = native_root(root)?;
let signature = serde_json::json!({
"root": root.to_string_lossy(),
"ports": serde_json::Value::Object(config.port_map()),
});
let previous = state::dir(root).join("configuration.json");
if previous.exists() {
let recorded: Option<serde_json::Value> = crate::fsx::read_json(&previous).ok();
if recorded.as_ref() != Some(&signature) && SERVICES.iter().any(|s| state::running(root, s).is_some()) {
return Err(Error::Invalid("Stop this repack before moving its folder or changing its ports.".into()));
}
}
let (root_password, app_password) = credentials(root)?;
for folder in ["Core/configs/modules", "Core/Logs", "BugReport/reports", "BugReport/Logs", "mysql/logs", ".state"] {
std::fs::create_dir_all(root.join(folder))?;
}
let mysql_port = config.mysql_port;
let mut values: Vec<(&str, String)> = vec![
("DATA", format!("{base}/Data")),
("LOGS", format!("{base}/Core/Logs")),
("ASCENSION_DBC", format!("{base}/Data/dbc/Ascension")),
("MYSQL_EXE", format!("{base}/mysql/bin/mysql.exe")),
("AUTH_PORT", config.auth_port.to_string()),
("WORLD_PORT", config.world_port.to_string()),
("RA_PORT", config.ra_port.to_string()),
];
for (key, db) in [
("LoginDatabaseInfo", "acore_auth"),
("WorldDatabaseInfo", config.realm.world_db),
("CharacterDatabaseInfo", config.realm.characters_db),
] {
values.push((key, format!("127.0.0.1;{mysql_port};acore;{app_password};{db}")));
}
let mut written = Vec::new();
for (name, target) in [
("authserver.conf", "Core/configs/authserver.conf"),
("worldserver.conf", "Core/configs/worldserver.conf"),
("mod_ascension_compat.conf", "Core/configs/modules/mod_ascension_compat.conf"),
] {
let mut text = read_text(&root.join("Settings").join(format!("{name}.template")))?;
for (key, value) in &values {
text = text.replace(&format!("@{key}@"), value);
}
let mut unresolved: Vec<String> = text
.split('\n')
.filter(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#'))
.flat_map(placeholders)
.collect();
unresolved.sort();
unresolved.dedup();
if !unresolved.is_empty() {
return Err(Error::Service(
crate::error::ErrorCode::InvalidConfigValue,
format!("An unresolved placeholder remains in Settings/{name}.template: {}", unresolved.join(", ")),
));
}
// these three keep "\n" (Python wrote them with newline="\n")
std::fs::write(root.join(target), text.as_bytes())?;
written.push(root.join(target));
}
let custom = custom_mysql_options(root);
let files: [(&str, String); 3] = [
(
"Core/configs/modules/coa_bugreport.conf",
format!(
"# Managed by the repack launcher. Enabled whenever worldserver starts.\nCoABugReport.Enable = 1\nCoABugReport.SpoolDirectory = \"{base}/BugReport/reports\"\nCoABugReport.CooldownSeconds = 120\n"
),
),
(
"mysql/my.ini",
format!(
"[mysqld]\nbasedir=\"{base}/mysql\"\ndatadir=\"{base}/mysql/data\"\nport={mysql_port}\nbind-address=127.0.0.1\nmysqlx=0\ncharacter-set-server=utf8mb4\ncollation-server=utf8mb4_unicode_ci\nmax_allowed_packet=128M\ninnodb_buffer_pool_size=256M\nskip-log-bin\nlog-error=\"{base}/mysql/logs/mysql-error.log\"\npid-file=\"{base}/mysql/mysql.pid\"\n{custom}"
),
),
(
"mysql/admin-client.ini",
format!(
"[client]\nuser=root\npassword={root_password}\nhost=127.0.0.1\nport={mysql_port}\nprotocol=tcp\ndefault-character-set=utf8mb4\n"
),
),
];
for (rel, text) in files {
std::fs::write(root.join(rel), windows_text(&text))?;
written.push(root.join(rel));
}
state::write_json(&previous, &signature)?;
Ok(written)
}
/// The owner's file of extra MySQL options; the launcher rewrites `mysql/my.ini` at every start.
pub const CUSTOM_MYSQL: &str = "Settings/mysql-custom.ini";
/// The owner's MySQL options as written, and the lines a start will add to `[mysqld]`.
#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomMysql {
pub text: String,
pub applied: String,
}
pub fn read_custom_mysql(root: &Path) -> CustomMysql {
let text = std::fs::read(root.join(CUSTOM_MYSQL))
.map(|b| String::from_utf8_lossy(&b).into_owned())
.unwrap_or_default();
CustomMysql { text, applied: custom_mysql_options(root) }
}
/// Save the owner's MySQL options; they apply at the next start of the database.
pub fn write_custom_mysql(root: &Path, text: &str) -> Result<CustomMysql> {
if text.len() > 16 * 1024 || text.contains(char::from(0)) {
return Err(Error::Invalid("The MySQL options must be plain text of at most 16 KB.".into()));
}
crate::fsx::atomic_write(&root.join(CUSTOM_MYSQL), text.replace("\r\n", "\n").as_bytes())?;
Ok(read_custom_mysql(root))
}
/// Options the server cannot run without, which only the launcher sets.
const MANAGED_MYSQL_OPTIONS: [&str; 10] = [
"basedir",
"datadir",
"port",
"bind-address",
"mysqlx",
"log-error",
"pid-file",
"socket",
"skip-networking",
"defaults-file",
];
/// `name = value` lines of the owner's file (outside any section or in `[mysqld]`), as lines for `[mysqld]`.
/// Comments, other sections, `!include` and the options the launcher manages are left out.
pub fn custom_mysql_options(root: &Path) -> String {
let Ok(bytes) = std::fs::read(root.join(CUSTOM_MYSQL)) else {
return String::new();
};
let text = String::from_utf8_lossy(&bytes);
let mut in_mysqld = true;
let mut out = String::new();
for line in text.trim_start_matches('\u{feff}').lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
continue;
}
if line.starts_with('[') {
in_mysqld = line.eq_ignore_ascii_case("[mysqld]");
continue;
}
if !in_mysqld || line.starts_with('!') {
continue;
}
let name = line
.split('=')
.next()
.unwrap_or("")
.trim()
.to_ascii_lowercase()
.replace('_', "-");
let name = name.strip_prefix("loose-").unwrap_or(&name);
if name.is_empty() || MANAGED_MYSQL_OPTIONS.contains(&name) {
continue;
}
out.push_str(line);
out.push('\n');
}
out
}
#[cfg(windows)]
mod sys {
use std::os::windows::ffi::OsStrExt;
use std::path::Path;
use windows_sys::Win32::Storage::FileSystem::GetShortPathNameW;
pub fn short_path(path: &Path) -> Option<String> {
let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
let mut buf = vec![0u16; 32768];
let n = unsafe { GetShortPathNameW(wide.as_ptr(), buf.as_mut_ptr(), buf.len() as u32) };
(n > 0 && (n as usize) < buf.len()).then(|| String::from_utf16_lossy(&buf[..n as usize]))
}
}
#[cfg(not(windows))]
mod sys {
pub fn short_path(_: &std::path::Path) -> Option<String> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) fn fixture(root: &Path) {
let w = |rel: &str, text: &[u8]| {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
};
w("Settings/repack.json", br#"{"authPort": 3724, "mysqlPort": 3307, "raPassword": "e0c39beaa5c14128", "raPort": 3443, "raUsername": "COAMANAGER", "worldPort": 8085}"#);
w("Settings/database.json", format!(r#"{{"rootPassword": "{}", "appPassword": "{}"}}"#, "a".repeat(48), "0123456789abcdef".repeat(3)).as_bytes());
w("Settings/authserver.conf.template", b"[authserver]\r\nLoginDatabaseInfo = \"@LoginDatabaseInfo@\"\r\nRealmServerPort = @AUTH_PORT@\r\nLogsDir = \"@LOGS@\"\r\n# @NOT_A_VALUE@ in a comment is fine\r\n");
w("Settings/worldserver.conf.template", "[worldserver]\nDataDir = \"@DATA@\"\nWorldServerPort = @WORLD_PORT@\nRa.Port = @RA_PORT@\nWorldDatabaseInfo = \"@WorldDatabaseInfo@\"\nCharacterDatabaseInfo = \"@CharacterDatabaseInfo@\"\nMySQLExecutable = \"@MYSQL_EXE@\"\nMotd = \"Ünïcode stays\"\n".as_bytes());
w("Settings/mod_ascension_compat.conf.template", b"Ascension.DbcDir = \"@ASCENSION_DBC@\"\rOld.Mac = 1\r");
}
#[test]
fn the_owners_mysql_options_are_kept_except_the_managed_ones() {
let d = tempfile::tempdir().unwrap();
std::fs::create_dir_all(d.path().join("Settings")).unwrap();
std::fs::write(
d.path().join(CUSTOM_MYSQL),
"\u{feff}# tuning\ninnodb_buffer_pool_size = 2G\nport=3999\nloose_bind_address=0.0.0.0\n!include C:/x.ini\n[client]\nuser=evil\n[mysqld]\nmax_connections=500\nDATADIR=D:/elsewhere\n",
)
.unwrap();
assert_eq!(
custom_mysql_options(d.path()),
"innodb_buffer_pool_size = 2G\nmax_connections=500\n"
);
assert_eq!(custom_mysql_options(&d.path().join("missing")), "");
}
#[test]
fn placeholders_left_outside_comments_are_refused() {
let d = tempfile::tempdir().unwrap();
fixture(d.path());
std::fs::write(d.path().join("Settings/worldserver.conf.template"), "A = @UNKNOWN@\n").unwrap();
let cfg = Settings::load(d.path(), 0).unwrap();
let err = prepare(d.path(), &cfg).unwrap_err().to_string();
assert!(err.contains("worldserver.conf.template: @UNKNOWN@"), "{err}");
}
#[test]
fn a_bad_password_file_is_refused() {
let d = tempfile::tempdir().unwrap();
fixture(d.path());
std::fs::write(d.path().join("Settings/database.json"), r#"{"rootPassword":"short","appPassword":"x"}"#).unwrap();
let cfg = Settings::load(d.path(), 0).unwrap();
assert!(prepare(d.path(), &cfg).unwrap_err().to_string().contains("Invalid packaged database configuration"));
}
/// Byte-for-byte the same files as the Python launcher. COA_MANAGE_PY = a manage.py to compare with (taken from
/// coa-server-build); skipped without it.
#[test]
fn renders_exactly_what_manage_py_renders() {
let Ok(manage) = std::env::var("COA_MANAGE_PY") else { return };
let python = std::env::var("COA_PYTHON").unwrap_or_else(|_| "python".into());
let rust = tempfile::tempdir().unwrap();
let py = tempfile::tempdir().unwrap();
for d in [rust.path(), py.path()] {
fixture(d);
}
std::fs::create_dir_all(py.path().join("Scripts")).unwrap();
std::fs::copy(&manage, py.path().join("Scripts/manage.py")).unwrap();
// manage.py imports squid_playerbots lazily; prepare does not need it.
let out = std::process::Command::new(&python)
.args(["-I", "-B"])
.arg(py.path().join("Scripts/manage.py"))
.arg("prepare")
.output()
.unwrap();
assert!(out.status.success(), "{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
let cfg = Settings::load(rust.path(), 0).unwrap();
let written = prepare(rust.path(), &cfg).unwrap();
let root_text = |p: &Path, base: &Path| {
let n = native_root(base).unwrap();
String::from_utf8(std::fs::read(p).unwrap()).unwrap().replace(&n, "<ROOT>")
};
for file in &written {
let rel = file.strip_prefix(rust.path()).unwrap();
assert_eq!(
root_text(file, rust.path()),
root_text(&py.path().join(rel), py.path()),
"{} differs",
rel.display()
);
}
let sig = |base: &Path| -> serde_json::Value {
let mut v: serde_json::Value = crate::fsx::read_json(&base.join(".state/configuration.json")).unwrap();
v["root"] = serde_json::Value::Null;
v
};
assert_eq!(sig(rust.path()), sig(py.path()));
}
}
+310
View File
@@ -0,0 +1,310 @@
//! Restarts on a schedule (#202): once a day at the owner's local time, the world supervisor asks the world to
//! restart with an in-game countdown, waits for it to exit with AzerothCore's restart code and starts it again.
//! The database and the auth server keep running.
use std::path::{Path, PathBuf};
use std::time::Duration;
use chrono::{Datelike, Local, NaiveDateTime, NaiveTime, Timelike};
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
const FILE: &str = "Settings/restart-schedule.json";
/// Present while a scheduled restart is under way, so a world that is briefly down is not taken for a crash.
const UNDER_WAY: &str = ".state/scheduled-restart";
/// The exit code of `server restart` (AzerothCore's `RESTART_EXIT_CODE`).
pub const RESTART_EXIT_CODE: i32 = 2;
/// A restart missed by more than this (the computer slept, the world started late) waits for the next day.
const LATE: Duration = Duration::from_secs(15 * 60);
/// A world that has not come back within this time no longer counts as restarting.
const UNDER_WAY_LIMIT: Duration = Duration::from_secs(45 * 60);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
#[default]
Sunday,
}
impl Weekday {
fn to_chrono(self) -> chrono::Weekday {
match self {
Self::Monday => chrono::Weekday::Mon,
Self::Tuesday => chrono::Weekday::Tue,
Self::Wednesday => chrono::Weekday::Wed,
Self::Thursday => chrono::Weekday::Thu,
Self::Friday => chrono::Weekday::Fri,
Self::Saturday => chrono::Weekday::Sat,
Self::Sunday => chrono::Weekday::Sun,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DueRestart {
pub moment: NaiveDateTime,
pub warning_minutes: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct RestartSchedule {
pub enabled: bool,
/// Local time, `HH:MM`.
pub time: String,
/// The in-game countdown before the restart, in minutes (1-60).
pub warning_minutes: u32,
pub weekly_enabled: bool,
pub weekly_day: Weekday,
/// Local time for weekly restart, `HH:MM`.
pub weekly_time: String,
/// The in-game countdown before the weekly restart, in minutes (1-60).
pub weekly_warning_minutes: u32,
}
impl Default for RestartSchedule {
fn default() -> Self {
Self {
enabled: false,
time: "05:00".into(),
warning_minutes: 5,
weekly_enabled: false,
weekly_day: Weekday::Sunday,
weekly_time: "05:00".into(),
weekly_warning_minutes: 5,
}
}
}
impl RestartSchedule {
fn parse_time(text: &str) -> Option<NaiveTime> {
NaiveTime::parse_from_str(text.trim(), "%H:%M").ok()
}
fn validate_entry(time: &str, warning_minutes: u32) -> Result<()> {
if Self::parse_time(time).is_none() {
return Err(Error::Invalid("The restart time must be HH:MM, for example 05:00.".into()));
}
if !(1..=60).contains(&warning_minutes) {
return Err(Error::Invalid("The restart warning must be 1 to 60 minutes.".into()));
}
Ok(())
}
fn validate(&self) -> Result<()> {
if self.enabled {
Self::validate_entry(&self.time, self.warning_minutes)?;
}
if self.weekly_enabled {
Self::validate_entry(&self.weekly_time, self.weekly_warning_minutes)?;
}
Ok(())
}
fn due_at(time_str: &str, now: NaiveDateTime, started: NaiveDateTime) -> Option<NaiveDateTime> {
let at = Self::parse_time(time_str)?;
let moment = now.date().and_time(at);
let late = now.signed_duration_since(moment).to_std().ok()?;
(late <= LATE && started < moment).then_some(moment)
}
/// The scheduled moment of `now`'s day, when a daily or weekly restart is due at `now` for a world started at `started`:
/// the moment has passed by less than [`LATE`] and the world was already running before it.
pub fn due_restart(&self, now: NaiveDateTime, started: NaiveDateTime) -> Option<DueRestart> {
if self.weekly_enabled && now.weekday() == self.weekly_day.to_chrono() {
if let Some(moment) = Self::due_at(&self.weekly_time, now, started) {
return Some(DueRestart {
moment,
warning_minutes: self.weekly_warning_minutes,
});
}
}
if self.enabled {
if let Some(moment) = Self::due_at(&self.time, now, started) {
return Some(DueRestart {
moment,
warning_minutes: self.warning_minutes,
});
}
}
None
}
/// The scheduled moment of `now`'s day, when a restart is due at `now` for a world started at `started`.
pub fn due(&self, now: NaiveDateTime, started: NaiveDateTime) -> Option<NaiveDateTime> {
self.due_restart(now, started).map(|d| d.moment)
}
}
/// The folder whose schedule a world follows: an extra world (`<server>/.realms/<name>`) follows its server's.
fn owner(root: &Path) -> PathBuf {
if crate::launcher::settings::is_extra_world(root) {
if let Some(main) = root.parent().and_then(Path::parent) {
return main.to_path_buf();
}
}
root.to_path_buf()
}
pub fn load(root: &Path) -> RestartSchedule {
crate::fsx::read_json(&owner(root).join(FILE)).unwrap_or_default()
}
pub fn save(root: &Path, schedule: &RestartSchedule) -> Result<RestartSchedule> {
let mut schedule = schedule.clone();
schedule.time = schedule.time.trim().to_string();
schedule.weekly_time = schedule.weekly_time.trim().to_string();
schedule.validate()?;
if !schedule.enabled && RestartSchedule::parse_time(&schedule.time).is_none() {
schedule.time = RestartSchedule::default().time;
}
if !schedule.weekly_enabled && RestartSchedule::parse_time(&schedule.weekly_time).is_none() {
schedule.weekly_time = RestartSchedule::default().weekly_time;
}
crate::fsx::atomic_write_json(&owner(root).join(FILE), &schedule)?;
Ok(schedule)
}
pub fn now() -> NaiveDateTime {
Local::now().naive_local().with_nanosecond(0).unwrap_or_else(|| Local::now().naive_local())
}
pub fn mark_under_way(root: &Path) -> Result<()> {
let path = root.join(UNDER_WAY);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(path, b"")?;
Ok(())
}
pub fn clear_under_way(root: &Path) {
let _ = std::fs::remove_file(root.join(UNDER_WAY));
}
/// A scheduled restart of this world is under way (its world may be down for a few minutes).
pub fn under_way(root: &Path) -> bool {
std::fs::metadata(root.join(UNDER_WAY))
.and_then(|m| m.modified())
.is_ok_and(|t| t.elapsed().is_ok_and(|age| age < UNDER_WAY_LIMIT))
}
#[cfg(test)]
mod tests {
use super::*;
fn at(text: &str) -> NaiveDateTime {
NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M").unwrap()
}
#[test]
fn a_restart_is_due_once_after_its_time_for_a_world_that_ran_before_it() {
let s = RestartSchedule { enabled: true, time: "05:00".into(), warning_minutes: 5, ..RestartSchedule::default() };
let started = at("2026-11-14 20:00");
assert_eq!(s.due(at("2026-11-15 04:59"), started), None);
assert_eq!(s.due(at("2026-11-15 05:00"), started), Some(at("2026-11-15 05:00")));
assert_eq!(s.due(at("2026-11-15 05:14"), started), Some(at("2026-11-15 05:00")));
assert_eq!(s.due(at("2026-11-15 05:16"), started), None, "missed by too much");
// The world that came back at 05:06 is not restarted again that day.
assert_eq!(s.due(at("2026-11-15 05:10"), at("2026-11-15 05:06")), None);
assert_eq!(s.due(at("2026-11-16 05:01"), at("2026-11-15 05:06")), Some(at("2026-11-16 05:00")));
let off = RestartSchedule { enabled: false, ..s };
assert_eq!(off.due(at("2026-11-15 05:00"), started), None);
}
#[test]
fn weekly_restart_triggers_only_on_configured_weekday_and_independently_of_daily() {
// 2026-11-15 is Sunday; 2026-11-16 is Monday.
let s = RestartSchedule {
enabled: false,
weekly_enabled: true,
weekly_day: Weekday::Sunday,
weekly_time: "04:00".into(),
weekly_warning_minutes: 15,
..RestartSchedule::default()
};
let started = at("2026-11-14 20:00");
assert_eq!(s.due_restart(at("2026-11-15 03:59"), started), None);
assert_eq!(
s.due_restart(at("2026-11-15 04:05"), started),
Some(DueRestart { moment: at("2026-11-15 04:00"), warning_minutes: 15 })
);
// Monday does not trigger Sunday's weekly restart.
assert_eq!(s.due_restart(at("2026-11-16 04:05"), started), None);
// Both daily and weekly enabled: each triggers on its own schedule.
let both = RestartSchedule { enabled: true, time: "05:00".into(), warning_minutes: 5, ..s };
assert_eq!(
both.due_restart(at("2026-11-15 04:05"), started),
Some(DueRestart { moment: at("2026-11-15 04:00"), warning_minutes: 15 })
);
assert_eq!(
both.due_restart(at("2026-11-15 05:02"), at("2026-11-15 04:16")),
Some(DueRestart { moment: at("2026-11-15 05:00"), warning_minutes: 5 })
);
}
#[test]
fn schedules_are_validated_and_extra_worlds_follow_their_server() {
let d = tempfile::tempdir().unwrap();
assert_eq!(load(d.path()), RestartSchedule::default());
let bad = RestartSchedule { enabled: true, time: "25:00".into(), warning_minutes: 5, ..RestartSchedule::default() };
assert!(save(d.path(), &bad).is_err());
let quiet = RestartSchedule { enabled: true, time: "05:00".into(), warning_minutes: 0, ..RestartSchedule::default() };
assert!(save(d.path(), &quiet).is_err());
let bad_weekly = RestartSchedule {
weekly_enabled: true,
weekly_time: "99:00".into(),
..RestartSchedule::default()
};
assert!(save(d.path(), &bad_weekly).is_err());
let quiet_weekly = RestartSchedule {
weekly_enabled: true,
weekly_warning_minutes: 61,
..RestartSchedule::default()
};
assert!(save(d.path(), &quiet_weekly).is_err());
let good = RestartSchedule {
enabled: true,
time: " 04:30 ".into(),
warning_minutes: 10,
weekly_enabled: true,
weekly_day: Weekday::Wednesday,
weekly_time: " 03:15 ".into(),
weekly_warning_minutes: 20,
};
let saved = save(d.path(), &good).unwrap();
assert_eq!(saved.time, "04:30");
assert_eq!(saved.weekly_time, "03:15");
assert_eq!(saved.weekly_day, Weekday::Wednesday);
let extra = d.path().join(".realms/secondary");
std::fs::create_dir_all(&extra).unwrap();
assert!(load(&extra).enabled);
assert!(load(&extra).weekly_enabled);
// Legacy schedule JSON without weekly fields loads cleanly with defaults.
std::fs::write(
d.path().join(FILE),
r#"{"enabled":true,"time":"06:00","warningMinutes":10}"#,
)
.unwrap();
let legacy = load(d.path());
assert!(legacy.enabled);
assert_eq!(legacy.time, "06:00");
assert!(!legacy.weekly_enabled);
assert_eq!(legacy.weekly_day, Weekday::Sunday);
assert!(!under_way(d.path()));
mark_under_way(d.path()).unwrap();
assert!(under_way(d.path()));
clear_under_way(d.path());
assert!(!under_way(d.path()));
}
}
+768
View File
@@ -0,0 +1,768 @@
//! Starting, checking and stopping the services of one repack: MySQL, authserver, worldserver
//! (`manage.py start_mysql`, `start_auth`, `stop_all` and their helpers), with the same rules.
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use crate::error::{Error, ErrorCode, Result};
use crate::launcher::{settings::Settings, state};
use crate::process::ProcessIdentity;
pub type Progress<'a> = &'a dyn Fn(&str);
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
fn hidden(cmd: &mut Command) -> &mut Command {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP);
}
cmd
}
/// Start `exe` with its output appended to `log`, and record it as `name`. `env_remove` are variables of this
/// process the service must not see.
pub fn spawn(root: &Path, name: &str, exe: &Path, args: &[&str], cwd: &Path, log: &Path, env_remove: &[String]) -> Result<ProcessIdentity> {
let child = crate::launcher::spawn::start(exe, args, cwd, log, env_remove)?;
state::record(root, name, child.pid)
}
// ---------------------------------------------------------------------------------------------------------------
// Ports
// ---------------------------------------------------------------------------------------------------------------
/// What keeps a new server off a port.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PortBlock {
/// Processes listen on it.
Owned(Vec<u32>),
/// Windows keeps it in an excluded range (Hyper-V, WSL and Docker reserve such ranges); waiting never helps.
Reserved(u16, u16),
/// Windows still refuses it after the wait (a stopped process' socket, or an owner the table does not show).
Unavailable(i32),
}
/// Wait (up to `wait`) until `port` can be bound by a new server. A port with a listening owner or inside a
/// Windows excluded range is reported at once; a port Windows still holds for a stopped process (a PID-zero row)
/// is waited for. The test bind is exclusive (`SO_EXCLUSIVEADDRUSE`), never a shared one.
pub fn port_block(port: u16, wait: Duration, progress: Progress) -> std::result::Result<(), PortBlock> {
let deadline = Instant::now() + wait;
let mut announced = false;
let mut reserved: Option<Vec<(u16, u16)>> = None;
loop {
let mut owners: Vec<u32> = sys::tcp_rows_v4()
.into_iter()
.filter(|r| r.state == sys::LISTEN && r.local_port == port)
.map(|r| r.pid)
.collect();
owners.sort_unstable();
owners.dedup();
if owners.iter().any(|p| *p != 0) {
return Err(PortBlock::Owned(owners));
}
match sys::exclusive_bind(port) {
Ok(()) => return Ok(()),
Err(code) if code == 10013 || code == 10048 => {
let ranges = reserved.get_or_insert_with(sys::excluded_ranges);
if let Some((start, end)) = ranges.iter().copied().find(|(s, e)| (*s..=*e).contains(&port)) {
return Err(PortBlock::Reserved(start, end));
}
if Instant::now() >= deadline {
return Err(PortBlock::Unavailable(code));
}
if !announced {
progress(&format!("Waiting for Windows to release port {port}..."));
announced = true;
}
std::thread::sleep(Duration::from_millis(250));
}
Err(code) => return Err(PortBlock::Unavailable(code)),
}
}
}
fn port_error(port: u16, block: PortBlock) -> Error {
match block {
PortBlock::Owned(owners) => {
let list: Vec<String> = owners.iter().map(u32::to_string).collect();
Error::Service(
ErrorCode::PortInUse,
format!("Port {port} is already used (PID {}). Stop the other server or change Settings/repack.json.", list.join(", ")),
)
}
PortBlock::Reserved(start, end) => Error::Service(
ErrorCode::PortInUse,
format!(
"Port {port} is reserved by Windows (excluded range {start}-{end}; Hyper-V, WSL and Docker reserve such ranges). Choose another port in Settings/repack.json, or restart the Windows NAT service as administrator (net stop winnat, then net start winnat)."
),
),
PortBlock::Unavailable(code) if code == 10013 || code == 10048 => Error::Invalid(format!(
"Port {port} remains unavailable after 30 seconds (Windows bind error {code}). No process was stopped. Retry after the previous database shutdown has finished, or check the Windows port reservation."
)),
PortBlock::Unavailable(code) => Error::Invalid(format!("Port {port} cannot be checked (Windows error {code}).")),
}
}
/// [`port_block`] with up to 30 s of waiting, as an error a person can act on.
pub fn ensure_free(port: u16, progress: Progress) -> Result<()> {
port_block(port, Duration::from_secs(30), progress).map_err(|block| port_error(port, block))
}
/// The ports of this server's other services and extra worlds, which the database must not take.
fn ports_in_use_by_server(root: &Path, cfg: &Settings) -> Vec<u16> {
let mut ports = vec![cfg.auth_port, cfg.world_port, cfg.ra_port];
for tree in crate::multiworld::Tree::ALL {
let other = crate::multiworld::tree_root(root, tree);
if let Ok(other) = Settings::load(&other, 0) {
ports.extend([other.world_port, other.ra_port, other.auth_port]);
}
}
ports
}
/// Before the database starts: when its port is reserved by Windows or held by a program that is not this
/// server's MySQL, move the database to a free port and record it in `Settings/repack.json`. Only this server's
/// own services connect to MySQL (players never do), and every start renders their configs from that file, so
/// the move needs nothing else. Returns the new port, or `None` when the configured one is usable.
pub fn relocate_database_port(root: &Path, cfg: &Settings, offset: i32, progress: Progress) -> Result<Option<u16>> {
if cfg.shared_database || offset != 0 || state::running(root, "mysql").is_some() {
return Ok(None);
}
let block = match port_block(cfg.mysql_port, Duration::from_secs(30), progress) {
Ok(()) => return Ok(None),
Err(block) => block,
};
let ours = root.join("mysql/bin/mysqld.exe");
if let PortBlock::Owned(owners) = &block {
// This server's own database without a record: never start a second one on the same data.
let same = |pid: &u32| {
crate::process::identity(*pid).is_some_and(|p| same_file(Path::new(&p.exe), &ours))
};
if owners.iter().any(same) {
return Err(port_error(cfg.mysql_port, block));
}
}
let taken = ports_in_use_by_server(root, cfg);
let reserved = sys::excluded_ranges();
let candidate = (cfg.mysql_port.saturating_add(1)..=cfg.mysql_port.saturating_add(400))
.chain(33070..=33470)
.filter(|p| *p > 1024)
.find(|p| {
!taken.contains(p)
&& !reserved.iter().any(|(s, e)| (*s..=*e).contains(p))
&& port_block(*p, Duration::ZERO, &|_| {}).is_ok()
});
let Some(port) = candidate else {
return Err(port_error(cfg.mysql_port, block));
};
let reason = match &block {
PortBlock::Owned(_) => "used by another program",
PortBlock::Reserved(..) => "reserved by Windows",
PortBlock::Unavailable(_) => "unavailable",
};
let path = root.join("Settings/repack.json");
let mut raw = cfg.raw.clone();
raw.insert("mysqlPort".into(), port.into());
crate::fsx::atomic_write_json(&path, &serde_json::Value::Object(raw))?;
tracing::warn!(old = cfg.mysql_port, new = port, reason, "database moved to a free port");
progress(&format!("Port {} is {reason}; the database now uses port {port}.", cfg.mysql_port));
Ok(Some(port))
}
fn same_file(a: &Path, b: &Path) -> bool {
match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
(Ok(a), Ok(b)) => a.to_string_lossy().eq_ignore_ascii_case(&b.to_string_lossy()),
_ => false,
}
}
/// The ranges of `netsh interface ipv4 show excludedportrange protocol=tcp`: every line that starts with two
/// port numbers (the headers are translated, the numbers are not).
pub(crate) fn parse_excluded_ranges(text: &str) -> Vec<(u16, u16)> {
text.lines()
.filter_map(|line| {
let mut parts = line.split_whitespace();
let start = parts.next()?.parse::<u16>().ok()?;
let end = parts.next()?.parse::<u16>().ok()?;
(start <= end).then_some((start, end))
})
.collect()
}
/// Is the recorded process of `name` serving `port`? Its listener row is the evidence; when a system does not
/// show the owner of a listener, the server side of a fresh loopback connection is (never for MySQL, whose
/// listener has always been visible and which should not get stray connections).
pub fn ready(root: &Path, name: &str, port: u16) -> bool {
let Some(record) = state::running(root, name) else { return false };
let listening = sys::tcp_rows_v4().iter().any(|r| r.state == sys::LISTEN && r.local_port == port && r.pid == record.pid);
if listening {
return crate::process::is_alive(&record);
}
if name == "mysql" {
return false;
}
accepted_by(port, record.pid) && crate::process::is_alive(&record)
}
/// Connect to 127.0.0.1:`port` and check that exactly one established server-side row, owned by `pid`, answers
/// that connection.
fn accepted_by(port: u16, pid: u32) -> bool {
let addr: SocketAddr = ([127, 0, 0, 1], port).into();
let Ok(client) = TcpStream::connect_timeout(&addr, Duration::from_millis(500)) else { return false };
let Ok(mine) = client.local_addr().map(|a| a.port()) else { return false };
let me = std::process::id();
let rows: Vec<sys::TcpRow> = sys::tcp_rows_v4()
.into_iter()
.chain(sys::tcp_rows_v6())
.filter(|r| r.remote_port == mine && r.remote_loopback && !(r.local_port == mine && r.pid == me))
.collect();
rows.len() == 1 && rows[0].state == sys::ESTABLISHED && rows[0].pid == pid
}
pub fn wait_ready(root: &Path, name: &str, port: u16, seconds: u64) -> Result<()> {
let until = Instant::now() + Duration::from_secs(seconds);
while Instant::now() < until {
if ready(root, name, port) {
return Ok(());
}
if state::running(root, name).is_none() {
return Err(Error::Service(ErrorCode::StartupFailed, format!("{name} stopped during startup. Check its log.")));
}
std::thread::sleep(Duration::from_millis(500));
}
Err(Error::Service(
ErrorCode::StartupFailed,
format!("{name} is still starting: its expected TCP listener on port {port} was not found; check its log."),
))
}
// ---------------------------------------------------------------------------------------------------------------
// MySQL client
// ---------------------------------------------------------------------------------------------------------------
/// Run SQL (or a `mysqladmin` command) with the repack's own client and administrator login.
pub fn mysql(root: &Path, sql: Option<&str>, admin: Option<&str>) -> Result<String> {
let exe = root.join(if admin.is_some() { "mysql/bin/mysqladmin.exe" } else { "mysql/bin/mysql.exe" });
let defaults = format!("--defaults-file={}", root.join("mysql/admin-client.ini").display());
let mut cmd = Command::new(exe);
hidden(&mut cmd).arg(defaults);
match admin {
Some(a) => cmd.arg(a),
None => cmd.args(["--batch", "--skip-column-names"]),
};
let payload = sql.map(|s| s.as_bytes().to_vec());
let timeout = if payload.as_ref().is_some_and(|p| p.len() > 1024 * 1024) { 900 } else { 90 };
cmd.stdin(if payload.is_some() { Stdio::piped() } else { Stdio::null() }).stdout(Stdio::piped()).stderr(Stdio::piped());
let mut child = cmd.spawn()?;
if let (Some(data), Some(mut stdin)) = (payload, child.stdin.take()) {
std::thread::spawn(move || {
use std::io::Write;
let _ = stdin.write_all(&data);
});
}
// Both outputs are read while the client runs: a full pipe would stop it (and look like a timeout).
let read = |pipe: Option<Box<dyn std::io::Read + Send>>| {
std::thread::spawn(move || {
let mut buf = Vec::new();
if let Some(mut p) = pipe {
let _ = p.read_to_end(&mut buf);
}
buf
})
};
let out_reader = read(child.stdout.take().map(|p| Box::new(p) as Box<dyn std::io::Read + Send>));
let err_reader = read(child.stderr.take().map(|p| Box::new(p) as Box<dyn std::io::Read + Send>));
let started = Instant::now();
let failed = |stderr: &[u8]| {
let text = String::from_utf8_lossy(stderr);
let detail = text.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or("").trim().to_string();
let mut message = "The repack database command failed; check mysql/logs/mysql-error.log.".to_string();
if !detail.is_empty() {
message.push_str(&format!(" ({detail})"));
}
Error::Invalid(message)
};
let status = loop {
if let Some(status) = child.try_wait()? {
break status;
}
if started.elapsed() > Duration::from_secs(timeout) {
let _ = child.kill();
let _ = child.wait();
let _ = out_reader.join();
return Err(failed(b"timed out"));
}
std::thread::sleep(Duration::from_millis(50));
};
let stdout = out_reader.join().unwrap_or_default();
let stderr = err_reader.join().unwrap_or_default();
if !status.success() {
return Err(failed(&stderr));
}
Ok(String::from_utf8_lossy(&stdout).trim().to_string())
}
// ---------------------------------------------------------------------------------------------------------------
// Start and stop
// ---------------------------------------------------------------------------------------------------------------
pub fn start_mysql(root: &Path, cfg: &Settings, progress: Progress) -> Result<()> {
if cfg.shared_database {
// An extra world uses the main folder's database; it must already run.
return mysql(root, None, Some("ping")).map(|_| ()).map_err(|e| {
Error::Service(ErrorCode::DatabaseNotRunning, format!("The main server's database is not running. {e}"))
});
}
if state::running(root, "mysql").is_some() {
wait_ready(root, "mysql", cfg.mysql_port, 300)?;
mysql(root, None, Some("ping"))?;
progress("MySQL is already running.");
return Ok(());
}
ensure_free(cfg.mysql_port, progress)?;
if !root.join("mysql/data/mysql").is_dir() {
return Err(Error::Service(ErrorCode::ServerFilesIncomplete, "The packaged database is missing. Extract the complete repack.".into()));
}
progress("Starting MySQL...");
let ini = format!("--defaults-file={}", root.join("mysql/my.ini").display());
spawn(root, "mysql", &root.join("mysql/bin/mysqld.exe"), &[&ini, "--no-monitor"], &root.join("mysql"), &root.join("mysql/logs/startup.log"), &cfg.env_remove)?;
wait_ready(root, "mysql", cfg.mysql_port, 300)?;
mysql(root, None, Some("ping"))?;
mysql(
root,
Some(&format!(
"UPDATE acore_auth.realmlist SET name={},address='127.0.0.1',localAddress='127.0.0.1',port={} WHERE id={};",
crate::realm_names::sql_literal(&cfg.realm.name), cfg.world_port, cfg.realm.id
)),
None,
)?;
Ok(())
}
pub fn start_auth(root: &Path, cfg: &Settings, progress: Progress) -> Result<()> {
start_mysql(root, cfg, progress)?;
if state::running(root, "auth").is_some() {
wait_ready(root, "auth", cfg.auth_port, 60)?;
progress("Authserver is already running.");
return Ok(());
}
ensure_free(cfg.auth_port, progress)?;
// An interrupted world startup can leave flag 3, which authserver excludes.
mysql(root, Some(&format!("UPDATE acore_auth.realmlist SET flag=0 WHERE id={};", cfg.realm.id)), None)?;
progress("Starting authserver...");
spawn(root, "auth", &root.join("Core/authserver.exe"), &["-c", "configs/authserver.conf"], &root.join("Core"), &root.join("Core/Logs/auth-console.log"), &cfg.env_remove)?;
wait_ready(root, "auth", cfg.auth_port, 60)
}
/// Stop the recorded process, only if it is still that process.
pub fn terminate(record: &ProcessIdentity) -> Result<()> {
if !crate::process::is_alive(record) {
return Ok(());
}
sys::terminate(record)
}
pub fn wait_stopped(record: &ProcessIdentity, seconds: u64) -> bool {
let until = Instant::now() + Duration::from_secs(seconds);
while crate::process::is_alive(record) && Instant::now() < until {
std::thread::sleep(Duration::from_millis(250));
}
!crate::process::is_alive(record)
}
/// The recorded world, when it is this folder's own `Core/worldserver.exe` (never stop a process only because a
/// record names it).
fn owned_world(root: &Path) -> Result<Option<ProcessIdentity>> {
let Some(world) = state::running(root, "world") else { return Ok(None) };
let expected = root.join("Core/worldserver.exe");
let same = std::fs::canonicalize(&world.exe).ok().zip(std::fs::canonicalize(&expected).ok()).is_some_and(|(a, b)| a == b);
if !same {
return Err(Error::Invalid("World process ownership could not be verified; the database was left running.".into()));
}
Ok(Some(world))
}
/// Ask the world's console for a graceful shutdown. False when the console is not this world's or does not answer.
fn ask_shutdown(root: &Path, cfg: &Settings) -> bool {
ready(root, "world", cfg.ra_port)
&& crate::ra::Ra::connect_to(cfg.ra_port, &cfg.ra_username, &cfg.ra_password)
.and_then(|ra| ra.send_without_answer("server shutdown 1s 0"))
.is_ok()
}
/// Stop this folder's world without its console (unsaved progress since the last save is lost).
fn stop_world_process(world: &ProcessIdentity) -> Result<()> {
terminate(world).map_err(|_| Error::Invalid("Worldserver could not be stopped safely; its database was left running.".into()))?;
if !wait_stopped(world, 90) {
return Err(Error::Invalid("Worldserver is still stopping; its database was left running.".into()));
}
Ok(())
}
/// Stop the world at once, then everything else as [`stop_all`] does. Only when the user asked for it.
pub fn force_stop_all(root: &Path, cfg: &Settings, progress: Progress) -> Result<()> {
if let Some(world) = owned_world(root)? {
stop_world_process(&world)?;
progress("Force-stopped this server's verified world process; unsaved progress may be lost.");
}
stop_all(root, cfg, progress)
}
/// Stop everything of this repack, world first (gracefully through its console), its database last. An extra
/// world leaves the shared database alone.
pub fn stop_all(root: &Path, cfg: &Settings, progress: Progress) -> Result<()> {
if let Some(world) = owned_world(root)? {
if ask_shutdown(root, cfg) {
progress("Graceful world shutdown requested.");
} else {
progress("World console unavailable; stopping this repack's recorded world process.");
stop_world_process(&world)?;
}
// A console request can be lost (seen when a client disconnected at once). While the world still runs
// and its console still answers, it has not begun to stop: ask again. Asking twice is harmless.
let mut stopped = wait_stopped(&world, 15);
for _ in 0..2 {
if stopped {
break;
}
if ask_shutdown(root, cfg) {
progress("The world has not begun to stop; shutdown requested again.");
}
stopped = wait_stopped(&world, 15);
}
if !stopped && !wait_stopped(&world, 45) {
return Err(Error::Invalid("Worldserver is still stopping; its database has been left running.".into()));
}
}
if let Some(supervisor) = state::running(root, "supervisor") {
if !wait_stopped(&supervisor, 45) {
return Err(Error::Invalid("The report relay is still finishing a request. Try Stop All again shortly.".into()));
}
}
if let Some(relay) = state::running(root, "relay") {
std::fs::write(state::dir(root).join("stop-relay"), b"")?;
if !wait_stopped(&relay, 40) {
return Err(Error::Invalid("The report relay did not finish; it was left running for recovery.".into()));
}
}
if let Some(auth) = state::running(root, "auth") {
terminate(&auth)?;
if !wait_stopped(&auth, 10) {
return Err(Error::Invalid("Authserver did not stop.".into()));
}
}
if let Some(database) = state::running(root, "mysql").filter(|_| !cfg.shared_database) {
let shutdown = mysql(root, None, Some("shutdown"));
if !wait_stopped(&database, 60) {
shutdown?;
return Err(Error::Invalid("MySQL is still stopping; wait before copying the repack.".into()));
}
}
progress("All services from this repack are stopped. It is safe to move or copy this folder.");
Ok(())
}
/// Which services run (`manage.py status-json`).
pub fn status(root: &Path) -> serde_json::Value {
let mut out = serde_json::Map::new();
for name in ["mysql", "auth", "world", "relay"] {
out.insert(name.into(), state::running(root, name).is_some().into());
}
let debug: bool = crate::fsx::read_json::<serde_json::Value>(&state::dir(root).join("debug-logs.json"))
.ok()
.and_then(|v| v.get("enabled").and_then(|e| e.as_bool()))
.unwrap_or(false);
out.insert("debugLogs".into(), debug.into());
serde_json::Value::Object(out)
}
pub fn log_path(root: &Path, name: &str) -> PathBuf {
match name {
"mysql" => root.join("mysql/logs/startup.log"),
"auth" => root.join("Core/Logs/auth-console.log"),
"world" => root.join("Core/Logs/world-console.log"),
"supervisor" => root.join("Core/Logs/supervisor.log"),
_ => root.join("BugReport/Logs/relay.log"),
}
}
// ---------------------------------------------------------------------------------------------------------------
// Windows
// ---------------------------------------------------------------------------------------------------------------
#[cfg(windows)]
mod sys {
use crate::error::{Error, Result};
use crate::process::ProcessIdentity;
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::NetworkManagement::IpHelper::GetExtendedTcpTable;
use windows_sys::Win32::Networking::WinSock::{setsockopt, SOL_SOCKET, SO_EXCLUSIVEADDRUSE};
use windows_sys::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_TERMINATE};
pub const LISTEN: u32 = 2;
pub const ESTABLISHED: u32 = 5;
pub struct TcpRow {
pub state: u32,
pub local_port: u16,
pub remote_port: u16,
pub remote_loopback: bool,
pub pid: u32,
}
fn table(af: u32) -> Vec<u8> {
const TCP_TABLE_OWNER_PID_ALL: i32 = 5;
let mut size = 0u32;
let mut buf: Vec<u8> = Vec::new();
for _ in 0..4 {
let code = unsafe {
GetExtendedTcpTable(
if buf.is_empty() { std::ptr::null_mut() } else { buf.as_mut_ptr().cast() },
&mut size,
0,
af,
TCP_TABLE_OWNER_PID_ALL,
0,
)
};
match code {
0 if !buf.is_empty() => return buf,
0 | 122 => buf = vec![0u8; size as usize + 4096],
_ => return Vec::new(),
}
}
Vec::new()
}
fn u32_at(b: &[u8], o: usize) -> u32 {
u32::from_le_bytes(b[o..o + 4].try_into().unwrap())
}
pub fn tcp_rows_v4() -> Vec<TcpRow> {
let b = table(2);
let mut out = Vec::new();
if b.len() < 4 {
return out;
}
for i in 0..u32_at(&b, 0) as usize {
let o = 4 + 24 * i;
if o + 24 > b.len() {
break;
}
let remote = u32_at(&b, o + 12).to_le_bytes();
out.push(TcpRow {
state: u32_at(&b, o),
local_port: u16::from_be((u32_at(&b, o + 8) & 0xFFFF) as u16),
remote_port: u16::from_be((u32_at(&b, o + 16) & 0xFFFF) as u16),
remote_loopback: remote == [127, 0, 0, 1],
pid: u32_at(&b, o + 20),
});
}
out
}
pub fn tcp_rows_v6() -> Vec<TcpRow> {
let b = table(23);
let mut out = Vec::new();
if b.len() < 4 {
return out;
}
// MIB_TCP6ROW_OWNER_PID: local addr 16, scope 4, local port 4, remote addr 16, scope 4, remote port 4, state 4, pid 4
for i in 0..u32_at(&b, 0) as usize {
let o = 4 + 56 * i;
if o + 56 > b.len() {
break;
}
let remote = &b[o + 24..o + 40];
let mapped_loopback = remote[..10].iter().all(|x| *x == 0) && remote[10] == 0xff && remote[11] == 0xff && remote[12..] == [127, 0, 0, 1];
out.push(TcpRow {
state: u32_at(&b, o + 48),
local_port: u16::from_be((u32_at(&b, o + 20) & 0xFFFF) as u16),
remote_port: u16::from_be((u32_at(&b, o + 44) & 0xFFFF) as u16),
remote_loopback: mapped_loopback,
pid: u32_at(&b, o + 52),
});
}
out
}
/// Bind 0.0.0.0:port exclusively and release it. Err carries the Windows socket error.
/// Windows' excluded TCP port ranges, read with the system's own netsh (never one from PATH or the server).
pub fn excluded_ranges() -> Vec<(u16, u16)> {
use std::os::windows::process::CommandExt;
let mut directory = vec![0u16; 32768];
let length = unsafe {
windows_sys::Win32::System::SystemInformation::GetSystemDirectoryW(directory.as_mut_ptr(), directory.len() as u32)
} as usize;
if length == 0 || length >= directory.len() {
return Vec::new();
}
let netsh = std::path::PathBuf::from(String::from_utf16_lossy(&directory[..length])).join("netsh.exe");
let output = std::process::Command::new(netsh)
.args(["interface", "ipv4", "show", "excludedportrange", "protocol=tcp"])
.stdin(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.creation_flags(super::CREATE_NO_WINDOW)
.output();
match output {
Ok(out) if out.status.success() => super::parse_excluded_ranges(&String::from_utf8_lossy(&out.stdout)),
_ => Vec::new(),
}
}
pub fn exclusive_bind(port: u16) -> std::result::Result<(), i32> {
use socket2::{Domain, Socket, Type};
use std::os::windows::io::AsRawSocket;
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).map_err(|e| e.raw_os_error().unwrap_or(-1))?;
let one: i32 = 1;
let set = unsafe {
setsockopt(socket.as_raw_socket() as _, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (&one as *const i32).cast(), 4)
};
if set != 0 {
return Err(std::io::Error::last_os_error().raw_os_error().unwrap_or(-1));
}
let addr: std::net::SocketAddr = ([0, 0, 0, 0], port).into();
socket.bind(&addr.into()).map_err(|e| e.raw_os_error().unwrap_or(-1))
}
pub fn terminate(record: &ProcessIdentity) -> Result<()> {
unsafe {
let h = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION, 0, record.pid);
if h.is_null() {
return Err(Error::Invalid("Unable to stop the repack process.".into()));
}
let same = crate::process::is_alive(record);
let ok = same && TerminateProcess(h, 0) != 0;
CloseHandle(h);
if !ok {
return Err(Error::Invalid("The process changed while stopping it.".into()));
}
}
Ok(())
}
}
#[cfg(not(windows))]
mod sys {
use crate::error::{Error, Result};
use crate::process::ProcessIdentity;
pub const LISTEN: u32 = 2;
pub const ESTABLISHED: u32 = 5;
pub struct TcpRow {
pub state: u32,
pub local_port: u16,
pub remote_port: u16,
pub remote_loopback: bool,
pub pid: u32,
}
pub fn tcp_rows_v4() -> Vec<TcpRow> {
Vec::new()
}
pub fn tcp_rows_v6() -> Vec<TcpRow> {
Vec::new()
}
pub fn exclusive_bind(_: u16) -> std::result::Result<(), i32> {
Ok(())
}
pub fn excluded_ranges() -> Vec<(u16, u16)> {
Vec::new()
}
pub fn terminate(_: &ProcessIdentity) -> Result<()> {
Err(Error::Invalid("not supported".into()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn excluded_ranges_are_read_from_translated_netsh_output() {
let english = "\r\nProtocol tcp Port Exclusion Ranges\r\n\r\nStart Port End Port\r\n---------- --------\r\n 3250 3349\r\n 50000 50059 *\r\n\r\n* - Administered port exclusions.\r\n";
assert_eq!(parse_excluded_ranges(english), vec![(3250, 3349), (50000, 50059)]);
let russian = "\r\nПротокол tcp Диапазоны исключения портов\r\n\r\nНачальный порт Конечный порт\r\n---------- --------\r\n 50000 50059 *\r\n\r\n* — управляемые исключения портов.\r\n";
assert_eq!(parse_excluded_ranges(russian), vec![(50000, 50059)]);
assert!(parse_excluded_ranges("").is_empty());
}
fn repack(dir: &Path, mysql: u16) {
std::fs::create_dir_all(dir.join("Settings")).unwrap();
std::fs::write(
dir.join("Settings/repack.json"),
format!(r#"{{"mysqlPort":{mysql},"authPort":3724,"worldPort":8085,"raPort":3443,"raUsername":"U","raPassword":"P"}}"#),
)
.unwrap();
}
#[test]
fn a_taken_database_port_moves_to_a_free_one_and_is_recorded() {
let d = tempfile::tempdir().unwrap();
let held = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let port = held.local_addr().unwrap().port();
repack(d.path(), port);
let cfg = Settings::load(d.path(), 0).unwrap();
let said = std::cell::RefCell::new(Vec::new());
let moved = relocate_database_port(d.path(), &cfg, 0, &|line| said.borrow_mut().push(line.to_string()))
.unwrap()
.expect("moved");
assert_ne!(moved, port);
let again = Settings::load(d.path(), 0).unwrap();
assert_eq!(again.mysql_port, moved);
assert_eq!((again.auth_port, again.ra_username.as_str()), (3724, "U"), "other keys are kept");
assert!(said.borrow().iter().any(|l| l.contains("used by another program")), "{said:?}");
// The new port is usable: nothing moves on the next start.
assert_eq!(relocate_database_port(d.path(), &again, 0, &|_| {}).unwrap(), None);
}
#[test]
fn extra_worlds_and_shifted_test_ports_never_move_the_database() {
let d = tempfile::tempdir().unwrap();
let held = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let port = held.local_addr().unwrap().port();
repack(d.path(), port);
let cfg = Settings::load(d.path(), 0).unwrap();
let shared = Settings { shared_database: true, ..cfg.clone() };
assert_eq!(relocate_database_port(d.path(), &shared, 0, &|_| {}).unwrap(), None);
assert_eq!(relocate_database_port(d.path(), &cfg, 5, &|_| {}).unwrap(), None);
assert_eq!(Settings::load(d.path(), 0).unwrap().mysql_port, port);
}
#[test]
fn a_port_with_a_listener_is_refused_and_a_free_one_passes() {
let held = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let port = held.local_addr().unwrap().port();
let err = ensure_free(port, &|_| {}).unwrap_err().to_string();
assert!(err.contains(&format!("Port {port} is already used (PID {}", std::process::id())), "{err}");
drop(held);
ensure_free(port, &|_| {}).unwrap();
}
#[test]
fn a_service_is_ready_only_on_its_own_port() {
let d = tempfile::tempdir().unwrap();
let held = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = held.local_addr().unwrap().port();
assert!(!ready(d.path(), "auth", port), "no record yet");
state::record(d.path(), "auth", std::process::id()).unwrap();
assert!(ready(d.path(), "auth", port));
// A port nobody listens on now (port + 1 may belong to a test running in parallel in this process).
let other = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let free = other.local_addr().unwrap().port();
drop(other);
assert!(!ready(d.path(), "auth", free));
}
#[test]
fn status_reads_the_records() {
let d = tempfile::tempdir().unwrap();
state::record(d.path(), "mysql", std::process::id()).unwrap();
let s = status(d.path());
assert_eq!(s["mysql"], true);
assert_eq!(s["world"], false);
assert_eq!(s["debugLogs"], false);
}
}
+172
View File
@@ -0,0 +1,172 @@
//! `Settings/repack.json`: ports and the console login of one repack (`manage.py settings()`).
use std::path::Path;
use crate::error::{Error, Result};
pub const PORT_KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"];
/// The realm a folder runs: its row in `acore_auth.realmlist` and its two game databases.
///
/// Without `Settings/realm-profile.json` this is what the repack launcher always did (realm 1 "AzerothCore" on
/// `acore_world`/`acore_characters`). With it, the active profile decides, as the Manager's realm patch of
/// `manage.py` did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Realm {
pub id: u32,
pub name: String,
pub world_db: &'static str,
pub characters_db: &'static str,
}
impl Realm {
pub fn load(root: &Path) -> Result<Realm> {
let path = root.join("Settings/realm-profile.json");
let (id, name, world_db, characters_db) = if !path.exists() {
(1, "AzerothCore", "acore_world", "acore_characters")
} else {
let v: serde_json::Value = crate::fsx::read_json(&path)?;
match v.get("active").and_then(|a| a.as_str()).unwrap_or("coa") {
"wildcard" => (2, "Wildcard", "acore_world_wildcard", "acore_characters_wildcard"),
"reborn" => (3, "Warcraft Reborn", "acore_world_reborn", "acore_characters_reborn"),
_ => (1, "Conquest of Azeroth", "acore_world", "acore_characters"),
}
};
// The owner's own name for this realm, when one was chosen in the Manager.
let name = crate::realm_names::name_or(root, id, name);
Ok(Realm { id, name, world_db, characters_db })
}
}
/// An extra world of a server (`<server>/.realms/<name>`): it shares the main folder's database and never
/// starts or stops it.
pub fn is_extra_world(root: &Path) -> bool {
root.parent().and_then(|p| p.file_name()).is_some_and(|n| n == ".realms")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Settings {
pub mysql_port: u16,
pub auth_port: u16,
pub world_port: u16,
pub ra_port: u16,
pub ra_username: String,
pub ra_password: String,
pub realm: Realm,
/// The database belongs to the main folder of this server (an extra world): it is only checked, never
/// started.
pub shared_database: bool,
/// Environment variables the started services must not see (for example a port override of the caller).
pub env_remove: Vec<String>,
/// The whole document, for keys this code does not know (kept when it is written back).
pub raw: serde_json::Map<String, serde_json::Value>,
}
impl Settings {
/// Read `Settings/repack.json`, with every port moved by `offset` (a second world of the same server runs on
/// shifted ports). The same checks as `manage.py`: integer ports in 1025..65535 after the offset, all
/// different.
pub fn load(root: &Path, offset: i32) -> Result<Settings> {
let bytes = std::fs::read(root.join("Settings/repack.json"))?;
let text = String::from_utf8_lossy(&bytes);
let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
let raw: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(text).map_err(|e| Error::Invalid(format!("Settings/repack.json: {e}")))?;
let mut ports = [0u16; 4];
for (i, key) in PORT_KEYS.iter().enumerate() {
// Python's `type(value) is not int` refuses booleans and floats alike.
let value = raw.get(*key).and_then(|v| v.as_i64()).filter(|_| raw[*key].is_i64() || raw[*key].is_u64());
let shifted = value.map(|v| v + i64::from(offset));
match shifted {
Some(p) if p > 1024 && p < 65536 => ports[i] = p as u16,
_ => return Err(Error::Service(crate::error::ErrorCode::InvalidConfigValue, "Invalid port in Settings/repack.json".into())),
}
}
let mut unique = ports.to_vec();
unique.sort_unstable();
unique.dedup();
if unique.len() != 4 {
return Err(Error::Invalid("Each service needs a different port.".into()));
}
let text_of = |k: &str| raw.get(k).and_then(|v| v.as_str()).unwrap_or_default().to_string();
Ok(Settings {
mysql_port: ports[0],
auth_port: ports[1],
world_port: ports[2],
ra_port: ports[3],
ra_username: text_of("raUsername"),
ra_password: text_of("raPassword"),
realm: Realm::load(root)?,
shared_database: is_extra_world(root),
env_remove: Vec::new(),
raw,
})
}
/// `{"mysqlPort": .., "authPort": .., "worldPort": .., "raPort": ..}` as `manage.py` records it in
/// `.state/configuration.json` (the keys of repack.json ending in "Port", in the file's order).
pub fn port_map(&self) -> serde_json::Map<String, serde_json::Value> {
let shifted = [self.mysql_port, self.auth_port, self.world_port, self.ra_port];
let mut out = serde_json::Map::new();
for key in self.raw.keys().filter(|k| k.ends_with("Port")) {
let value = PORT_KEYS
.iter()
.position(|p| p == key)
.map(|i| serde_json::Value::from(shifted[i]))
.unwrap_or_else(|| self.raw[key].clone());
out.insert(key.clone(), value);
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write(dir: &Path, text: &str) {
std::fs::create_dir_all(dir.join("Settings")).unwrap();
std::fs::write(dir.join("Settings/repack.json"), text).unwrap();
}
#[test]
fn ports_are_checked_like_the_python_launcher() {
let d = tempfile::tempdir().unwrap();
write(d.path(), r#"{"mysqlPort":3307,"authPort":3724,"worldPort":8085,"raPort":3443,"raUsername":"U","raPassword":"P"}"#);
let s = Settings::load(d.path(), 0).unwrap();
assert_eq!((s.mysql_port, s.auth_port, s.world_port, s.ra_port), (3307, 3724, 8085, 3443));
assert_eq!(s.ra_username, "U");
let shifted = Settings::load(d.path(), 10).unwrap();
assert_eq!(shifted.world_port, 8095);
assert_eq!(shifted.port_map()["worldPort"], serde_json::json!(8095));
for bad in [
r#"{"mysqlPort":1024,"authPort":3724,"worldPort":8085,"raPort":3443}"#,
r#"{"mysqlPort":"3307","authPort":3724,"worldPort":8085,"raPort":3443}"#,
r#"{"mysqlPort":3307.0,"authPort":3724,"worldPort":8085,"raPort":3443}"#,
r#"{"mysqlPort":true,"authPort":3724,"worldPort":8085,"raPort":3443}"#,
r#"{"mysqlPort":3307,"authPort":3307,"worldPort":8085,"raPort":3443}"#,
r#"{"authPort":3724,"worldPort":8085,"raPort":3443}"#,
] {
write(d.path(), bad);
assert!(Settings::load(d.path(), 0).is_err(), "{bad}");
}
write(d.path(), "\u{feff}{\"mysqlPort\":3307,\"authPort\":3724,\"worldPort\":8085,\"raPort\":65535}");
assert!(Settings::load(d.path(), 1).is_err(), "65536 after the offset");
assert!(Settings::load(d.path(), 0).is_ok(), "a byte order mark is accepted");
}
#[test]
fn the_realm_follows_the_active_profile() {
let d = tempfile::tempdir().unwrap();
assert_eq!(Realm::load(d.path()).unwrap().name, "AzerothCore");
std::fs::create_dir_all(d.path().join("Settings")).unwrap();
for (active, id, db) in [("coa", 1, "acore_world"), ("wildcard", 2, "acore_world_wildcard"), ("reborn", 3, "acore_world_reborn")] {
std::fs::write(d.path().join("Settings/realm-profile.json"), format!("{{\"active\":\"{active}\"}}")).unwrap();
let r = Realm::load(d.path()).unwrap();
assert_eq!((r.id, r.world_db), (id, db));
}
assert!(is_extra_world(&d.path().join(".realms/secondary")));
assert!(!is_extra_world(d.path()));
}
}
Binary file not shown.
+398
View File
@@ -0,0 +1,398 @@
//! SQUID Playerbots on a repack (`Scripts/squid_playerbots.py`): the bot modules must not both run, how long a
//! SQUID world may take to start, and its database, provisioned before the world starts.
//!
//! The SQL journals are the ones the Python script keeps, unchanged: `coa_squid_migrations(path, sha256)` and
//! `coa_squid_pending(path, sha256)` in every target schema, and the SQUID installer's `coa_bots_installed`.
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use sha1::Digest as _;
use crate::error::{Error, Result};
/// `key = value` lines of a module config (comments and quotes dropped), as `options()` reads them.
pub fn options(path: &Path) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let Ok(bytes) = std::fs::read(path) else { return out };
let text = String::from_utf8_lossy(&bytes);
let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
for line in text.lines() {
let line = line.trim_start();
let Some((key, rest)) = line.split_once('=') else { continue };
let key = key.trim_end();
if key.is_empty() || key.starts_with('#') || key.contains(char::is_whitespace) {
continue;
}
let value = rest.split('#').next().unwrap_or("").trim();
out.insert(key.to_string(), value.trim_matches('"').to_string());
}
out
}
/// A secondary world shares the primary's package files.
pub fn package_root(root: &Path) -> PathBuf {
let secondary = root.file_name().is_some_and(|n| n == "secondary")
&& root.parent().and_then(|p| p.file_name()).is_some_and(|n| n == ".realms");
if secondary {
root.parent().and_then(Path::parent).map(Path::to_path_buf).unwrap_or_else(|| root.to_path_buf())
} else {
root.to_path_buf()
}
}
pub fn module_options(root: &Path, name: &str) -> (PathBuf, BTreeMap<String, String>) {
let folder = root.join("Core/configs/modules");
let active = folder.join(name);
let mut path = if active.exists() { active } else { folder.join(format!("{name}.dist")) };
if !path.exists() && name == "playerbots.conf" {
path = package_root(root).join("Core/configs/modules/playerbots.conf.dist");
}
let values = options(&path);
(path, values)
}
/// Invalid booleans count as the modules' compiled default: enabled.
pub fn enabled(values: &BTreeMap<String, String>, key: &str) -> bool {
!matches!(values.get(key).map(|v| v.to_ascii_lowercase()).as_deref(), Some("0" | "false" | "no" | "off"))
}
pub fn validate_bots(root: &Path) -> Result<()> {
let (a, av) = module_options(root, "mod_coa_playerbots.conf");
let (b, bv) = module_options(root, "playerbots.conf");
if a.exists() && b.exists() && enabled(&av, "CoaBots.Enable") && enabled(&bv, "AiPlayerbot.Enabled") {
return Err(Error::Invalid(
"CoA Companions and SQUID Playerbots cannot both be enabled. Disable one bot module in Modules or its config.".into(),
));
}
Ok(())
}
/// Seconds a world may take to open its port: SQUID loads its bots first.
pub fn startup_timeout(root: &Path) -> u64 {
let (path, values) = module_options(root, "playerbots.conf");
if path.exists() && enabled(&values, "AiPlayerbot.Enabled") {
1500
} else {
180
}
}
/// Replace (or append) `key = value` lines; the file is written with "\n" line ends.
pub fn set_options(path: &Path, values: &[(&str, String)]) -> Result<()> {
let bytes = std::fs::read(path)?;
let text = String::from_utf8_lossy(&bytes);
let mut text = text.strip_prefix('\u{feff}').unwrap_or(&text).replace("\r\n", "\n");
for (key, value) in values {
let line = format!("{key} = {value}");
let mut found = false;
let lines: Vec<String> = text
.split('\n')
.map(|l| {
let t = l.trim_start();
let matches = t.strip_prefix(key).is_some_and(|rest| rest.trim_start().starts_with('='));
if matches {
found = true;
line.clone()
} else {
l.to_string()
}
})
.collect();
text = lines.join("\n");
if !found {
text.push('\n');
text.push_str(&line);
text.push('\n');
}
}
crate::fsx::atomic_write(path, text.as_bytes())
}
fn valid_relative(rel: &str) -> bool {
!rel.is_empty() && rel.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b'/' | b'-'))
}
/// Table names a base file creates (`CREATE TABLE [IF NOT EXISTS] name`).
fn created_tables(sql: &str) -> BTreeSet<String> {
let mut out = BTreeSet::new();
let upper = sql.to_ascii_uppercase();
let mut from = 0;
while let Some(i) = upper[from..].find("CREATE") {
let start = from + i + "CREATE".len();
from = start;
let rest = &sql[start..];
let mut words = rest.split_whitespace();
if !words.next().is_some_and(|w| w.eq_ignore_ascii_case("TABLE")) {
continue;
}
let mut name = words.next().unwrap_or("");
if name.eq_ignore_ascii_case("IF") {
let _ = words.next(); // NOT
let _ = words.next(); // EXISTS
name = words.next().unwrap_or("");
}
let name: String = name.trim_start_matches('`').chars().take_while(|c| c.is_ascii_alphanumeric() || *c == '_').collect();
if !name.is_empty() {
out.insert(name);
}
}
out
}
fn sha256_hex(bytes: &[u8]) -> String {
crate::fsx::sha256_bytes(bytes)
}
fn sha1_hex(bytes: &[u8]) -> String {
let mut h = sha1::Sha1::new();
h.update(bytes);
h.finalize().iter().map(|b| format!("{b:02x}")).collect()
}
fn rows(text: &str) -> Vec<(String, String)> {
text.lines().filter_map(|r| r.split_once('\t')).map(|(a, b)| (a.to_string(), b.to_string())).collect()
}
/// Provision an enabled SQUID without replaying imported base tables (`prepare_playerbots`). `mysql(sql)` runs
/// SQL with the repack's administrator login and returns its output.
pub fn prepare_playerbots(root: &Path, mysql_port: u16, mysql: &dyn Fn(&str) -> Result<String>, progress: &dyn Fn(&str)) -> Result<()> {
let (mut path, values) = module_options(root, "playerbots.conf");
if !path.exists() || !enabled(&values, "AiPlayerbot.Enabled") {
return Ok(());
}
validate_bots(root)?;
let sql_root = package_root(root).join("Extras/SquidPlayerbots/sql");
if !sql_root.join("playerbots/base").is_dir() {
return Err(Error::Invalid("SQUID Playerbots database files are missing. Repair the server installation.".into()));
}
let world = options(&root.join("Core/configs/worldserver.conf"));
let mut schemas: Vec<(&str, String)> = Vec::new();
for (kind, key) in [("characters", "CharacterDatabaseInfo"), ("world", "WorldDatabaseInfo")] {
let schema = world.get(key).and_then(|v| v.rsplit(';').next()).unwrap_or("").to_string();
let ok = matches!(schema.as_str(), "acore_world" | "acore_characters" | "acore_world_wildcard" | "acore_characters_wildcard");
if !ok {
return Err(Error::Invalid("Unsupported world/characters database for SQUID Playerbots.".into()));
}
schemas.push((kind, schema));
}
schemas.push(("playerbots", "acore_playerbots".into()));
let list = |schema: &str| -> Result<BTreeSet<String>> {
Ok(mysql(&format!("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='{schema}';"))?
.lines()
.map(str::to_string)
.collect())
};
let existing_playerbots = list("acore_playerbots")?;
let bookkeeping = ["updates", "updates_include", "version_db_playerbots", "coa_squid_migrations", "coa_bots_installed", "coa_squid_pending"];
let data_tables: Vec<&String> = existing_playerbots.iter().filter(|t| !bookkeeping.contains(&t.as_str())).collect();
let mut installer_history = BTreeSet::new();
if existing_playerbots.contains("coa_bots_installed") {
for name in mysql("SELECT file FROM `acore_playerbots`.coa_bots_installed;")?.lines() {
let mut n = name.replace('\\', "/");
while n.contains("//") {
n = n.replace("//", "/");
}
let n = n.trim_matches('/').to_string();
if n.to_ascii_lowercase().ends_with(".sql") {
installer_history.insert(n);
}
}
}
let mut tables: BTreeMap<&str, BTreeSet<String>> = BTreeMap::new();
let mut histories: BTreeMap<&str, BTreeMap<String, String>> = BTreeMap::new();
let mut ledgers: BTreeMap<&str, BTreeMap<String, String>> = BTreeMap::new();
for (kind, schema) in &schemas {
let t = if *kind == "playerbots" { existing_playerbots.clone() } else { list(schema)? };
if t.contains("coa_squid_pending") {
let pending = mysql(&format!("SELECT path FROM `{schema}`.coa_squid_pending;"))?;
let pending = pending.trim();
if !pending.is_empty() {
return Err(Error::Invalid(format!(
"A SQUID base import was interrupted. Restore the database backup or repair the incomplete import before continuing; no SQL was applied: {pending}"
)));
}
}
let mut history = BTreeMap::new();
if t.contains("updates") {
for (name, digest) in rows(&mysql(&format!("SELECT name,hash FROM `{schema}`.updates;"))?) {
history.insert(name, digest.to_ascii_lowercase());
}
}
let mut ledger = BTreeMap::new();
if t.contains("coa_squid_migrations") {
for (name, digest) in rows(&mysql(&format!("SELECT path,sha256 FROM `{schema}`.coa_squid_migrations;"))?) {
ledger.insert(name, digest);
}
}
tables.insert(kind, t);
histories.insert(kind, history);
ledgers.insert(kind, ledger);
}
if !data_tables.is_empty() && installer_history.is_empty() && histories["playerbots"].is_empty() && ledgers["playerbots"].is_empty() {
for table in data_tables {
let quoted = table.replace('`', "``");
if mysql(&format!("SELECT EXISTS(SELECT 1 FROM `acore_playerbots`.`{quoted}` LIMIT 1);"))?.trim() == "1" {
return Err(Error::Invalid(
"Existing SQUID data has no recorded installation history. Repair the imported database history before continuing; no SQL was applied.".into(),
));
}
}
}
mysql("CREATE DATABASE IF NOT EXISTS acore_playerbots CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")?;
for host in ["localhost", "127.0.0.1"] {
mysql(&format!("GRANT ALL PRIVILEGES ON acore_playerbots.* TO 'acore'@'{host}';"))?;
}
let (_, password) = crate::db::credentials(root)?;
if password.len() != 48 || !password.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) {
return Err(Error::Invalid("Invalid packaged database configuration.".into()));
}
if path.file_name().is_some_and(|n| n.to_string_lossy().ends_with(".dist")) {
let active = root.join("Core/configs/modules/playerbots.conf");
std::fs::create_dir_all(active.parent().unwrap())?;
std::fs::write(&active, std::fs::read(&path)?)?;
path = active;
}
set_options(
&path,
&[
("PlayerbotsDatabaseInfo", format!("\"127.0.0.1;{mysql_port};acore;{password};acore_playerbots\"")),
("Playerbots.Updates.EnableDatabases", "0".into()),
],
)?;
for (kind, schema) in &schemas {
mysql(&format!("CREATE TABLE IF NOT EXISTS `{schema}`.coa_squid_migrations (path VARCHAR(240) PRIMARY KEY, sha256 CHAR(64) NOT NULL);"))?;
mysql(&format!("CREATE TABLE IF NOT EXISTS `{schema}`.coa_squid_pending (path VARCHAR(240) PRIMARY KEY, sha256 CHAR(64) NOT NULL);"))?;
let history = &histories[kind];
let recorded = ledgers[kind].clone();
for stage in ["base", "updates", "custom"] {
let mut sources: Vec<PathBuf> = std::fs::read_dir(sql_root.join(kind).join(stage))
.map(|rd| rd.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|e| e == "sql")).collect())
.unwrap_or_default();
sources.sort();
for source in sources {
let relative = source.strip_prefix(&sql_root).unwrap_or(&source).to_string_lossy().replace('\\', "/");
if !valid_relative(&relative) {
return Err(Error::Invalid("Invalid SQUID SQL filename.".into()));
}
let bytes = std::fs::read(&source)?;
let digest = sha256_hex(&bytes);
if let Some(previous) = recorded.get(&relative) {
if *previous != digest {
return Err(Error::Invalid(format!("A previously applied SQUID database migration changed: {relative}")));
}
if stage != "base" {
continue;
}
}
let ledger_sql = if recorded.contains_key(&relative) {
String::new()
} else {
format!("INSERT INTO `{schema}`.coa_squid_migrations VALUES ('{relative}','{digest}');")
};
let text = String::from_utf8_lossy(&bytes);
let text = text.strip_prefix('\u{feff}').unwrap_or(&text).to_string();
if stage == "base" {
let definitions = created_tables(&text);
let present: BTreeSet<String> = definitions.intersection(&tables[kind]).cloned().collect();
if !present.is_empty() && present != definitions {
return Err(Error::Invalid(format!("SQUID base file has only some of its tables. Repair the incomplete database before continuing: {relative}")));
}
if definitions.is_empty() {
return Err(Error::Invalid(format!("SQUID base file has no recognizable table definitions; repair is required: {relative}")));
}
if present == definitions {
if !ledger_sql.is_empty() {
mysql(&ledger_sql)?;
}
continue;
}
mysql(&format!("INSERT INTO `{schema}`.coa_squid_pending VALUES ('{relative}','{digest}'); COMMIT;"))?;
let complete = format!("DELETE FROM `{schema}`.coa_squid_pending WHERE path='{relative}' AND sha256='{digest}'; COMMIT;");
progress(&format!("Importing SQUID {relative} into {schema} ({} bytes); large imports may take several minutes...", bytes.len()));
mysql(&format!("USE `{schema}`;\n{text}\n{ledger_sql}\n{complete}")).map_err(|_| {
Error::Invalid(format!(
"SQUID import failed: {relative} in {schema}. The world server was not started. The incomplete import is recorded; restore a database backup or repair that import before retrying."
))
})?;
progress(&format!("Imported SQUID {relative}."));
if let Some(t) = tables.get_mut(kind) {
t.extend(definitions);
}
continue;
}
if installer_history.iter().any(|n| *n == relative || n.ends_with(&format!("/{relative}"))) {
mysql(&ledger_sql)?;
continue;
}
let file_name = source.file_name().unwrap_or_default().to_string_lossy().into_owned();
if let Some(upstream) = history.get(&file_name) {
let lf = String::from_utf8_lossy(&bytes).replace("\r\n", "\n");
let accepted = [sha1_hex(&bytes), sha1_hex(lf.as_bytes())];
if !upstream.is_empty() && !accepted.contains(upstream) {
return Err(Error::Invalid(format!("SQUID update history does not match the shipped SQL: {relative}")));
}
mysql(&ledger_sql)?;
continue;
}
mysql(&format!("USE `{schema}`;\n{text}\n{ledger_sql}"))?;
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn config_lines_are_read_like_the_python_script() {
let d = tempfile::tempdir().unwrap();
let p = d.path().join("playerbots.conf");
std::fs::write(&p, "\u{feff}# comment = 1\nAiPlayerbot.Enabled = 0 # off\n Key.Two=\"quoted\"\nnot a line\nEmpty =\n").unwrap();
let o = options(&p);
assert_eq!(o.get("AiPlayerbot.Enabled").map(String::as_str), Some("0"));
assert_eq!(o.get("Key.Two").map(String::as_str), Some("quoted"));
assert_eq!(o.get("Empty").map(String::as_str), Some(""));
assert!(!o.contains_key("#"));
assert!(!enabled(&o, "AiPlayerbot.Enabled"));
assert!(enabled(&o, "Missing"));
}
#[test]
fn both_bot_modules_on_is_refused_and_squid_gets_a_long_startup() {
let d = tempfile::tempdir().unwrap();
let m = d.path().join("Core/configs/modules");
std::fs::create_dir_all(&m).unwrap();
std::fs::write(m.join("mod_coa_playerbots.conf"), "CoaBots.Enable = 1\n").unwrap();
std::fs::write(m.join("playerbots.conf.dist"), "AiPlayerbot.Enabled = 1\n").unwrap();
assert!(validate_bots(d.path()).is_err());
assert_eq!(startup_timeout(d.path()), 1500);
std::fs::write(m.join("playerbots.conf"), "AiPlayerbot.Enabled = 0\n").unwrap();
validate_bots(d.path()).unwrap();
assert_eq!(startup_timeout(d.path()), 180);
}
#[test]
fn options_are_replaced_or_appended() {
let d = tempfile::tempdir().unwrap();
let p = d.path().join("playerbots.conf");
std::fs::write(&p, "A = 1\r\n Playerbots.Updates.EnableDatabases = 1\r\n").unwrap();
set_options(&p, &[("Playerbots.Updates.EnableDatabases", "0".into()), ("PlayerbotsDatabaseInfo", "\"x\"".into())]).unwrap();
assert_eq!(std::fs::read_to_string(&p).unwrap(), "A = 1\nPlayerbots.Updates.EnableDatabases = 0\n\nPlayerbotsDatabaseInfo = \"x\"\n");
}
#[test]
fn base_tables_are_found() {
let t = created_tables("CREATE TABLE IF NOT EXISTS `ai_playerbot_names` (\n id int);\ncreate table playerbots_random_bots(x int);\n-- CREATE INDEX i ON t(x);");
assert_eq!(t, ["ai_playerbot_names".to_string(), "playerbots_random_bots".to_string()].into_iter().collect());
}
#[test]
fn a_secondary_world_uses_the_primary_package() {
assert_eq!(package_root(Path::new("C:/S/.realms/secondary")), Path::new("C:/S"));
assert_eq!(package_root(Path::new("C:/S")), Path::new("C:/S"));
}
}
+198
View File
@@ -0,0 +1,198 @@
//! `.state/`: which processes belong to this repack, and the one-action-at-a-time lock.
use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use crate::error::{Error, Result};
use crate::fsx;
use crate::process::{self, ProcessIdentity};
pub fn dir(root: &Path) -> PathBuf {
root.join(".state")
}
/// Written like `manage.py atomic_json`: two-space JSON and a newline, now flushed to disk before it replaces
/// the old file.
pub fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<()> {
let mut text = serde_json::to_string_pretty(value)?;
text.push('\n');
fsx::atomic_write(path, text.as_bytes())
}
/// The recorded process of `name` when it is still that process (same executable and start time). A missing,
/// empty or damaged record is no process: `manage.py` failed on those, and every later start with it.
pub fn running(root: &Path, name: &str) -> Option<ProcessIdentity> {
let record = process::read_state_record(root, name)?;
process::is_alive(&record).then_some(record)
}
/// Record the process `pid` as `name`. Fails when it has already exited.
pub fn record(root: &Path, name: &str, pid: u32) -> Result<ProcessIdentity> {
let identity = process::identity(pid)
.ok_or_else(|| Error::Invalid(format!("{name} exited during startup; check its log.")))?;
write_json(&dir(root).join(format!("{name}.json")), &identity)?;
Ok(identity)
}
/// Held while one start/stop action runs. Byte 0 of `.state/control.lock` is locked, exactly the region
/// `msvcrt.locking(fd, LK_NBLCK, 1)` locks in `manage.py`, so a Python launcher and this one exclude each other.
pub struct ControlLock {
file: File,
owner: PathBuf,
}
impl ControlLock {
/// Wait up to `wait` for the lock. `action` is recorded for whoever waits next ("start-all", ...).
pub fn acquire(root: &Path, action: &str, wait: Duration, progress: &dyn Fn(&str)) -> Result<ControlLock> {
let state = dir(root);
std::fs::create_dir_all(&state)?;
let mut file = OpenOptions::new().read(true).append(true).create(true).open(state.join("control.lock"))?;
if file.seek(SeekFrom::End(0))? == 0 {
file.write_all(b"0")?;
file.flush()?;
}
let owner = state.join("control-owner.json");
let deadline = Instant::now() + wait;
let mut announced = false;
loop {
if sys::try_lock(&file)? {
break;
}
let detail = match fsx::read_json::<serde_json::Value>(&owner) {
Ok(v) if v.get("pid").is_some() && v.get("action").is_some() => {
format!(" PID {}, action {}.", v["pid"], v["action"].as_str().unwrap_or(""))
}
_ => " Owner information is unavailable.".into(),
};
if Instant::now() >= deadline {
return Err(Error::Service(crate::error::ErrorCode::OperationInProgress, format!("Another start/stop action is in progress.{detail}")));
}
if !announced {
progress(&format!("Waiting for the current server action.{detail}"));
announced = true;
}
std::thread::sleep(Duration::from_millis(250));
}
let started = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
let lock = ControlLock { file, owner };
write_json(
&lock.owner,
&serde_json::json!({ "pid": std::process::id(), "action": action, "started": started }),
)?;
Ok(lock)
}
}
/// Byte 0 of a file locked while held (`msvcrt.locking`), for example the bug-report relay's `relay.lock`.
pub struct FileLock {
file: File,
}
impl FileLock {
/// Take the lock, or `None` when another process (or handle) holds it.
pub fn try_acquire(path: &Path) -> Result<Option<FileLock>> {
let mut file = OpenOptions::new().read(true).append(true).create(true).open(path)?;
if file.seek(SeekFrom::End(0))? == 0 {
file.write_all(b"0")?;
file.flush()?;
}
Ok(sys::try_lock(&file)?.then_some(FileLock { file }))
}
}
impl Drop for FileLock {
fn drop(&mut self) {
sys::unlock(&self.file);
}
}
impl Drop for ControlLock {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.owner);
sys::unlock(&self.file);
}
}
#[cfg(windows)]
mod sys {
use std::fs::File;
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Storage::FileSystem::{LockFile, UnlockFile};
/// `LockFile` on byte 0, length 1: the call `msvcrt.locking` makes. Non-blocking.
pub fn try_lock(file: &File) -> std::io::Result<bool> {
let ok = unsafe { LockFile(file.as_raw_handle() as _, 0, 0, 1, 0) };
if ok != 0 {
return Ok(true);
}
let err = std::io::Error::last_os_error();
// ERROR_LOCK_VIOLATION (33): someone else holds it.
if err.raw_os_error() == Some(33) {
Ok(false)
} else {
Err(err)
}
}
pub fn unlock(file: &File) {
unsafe {
UnlockFile(file.as_raw_handle() as _, 0, 0, 1, 0);
}
}
}
#[cfg(not(windows))]
mod sys {
use std::fs::File;
pub fn try_lock(_: &File) -> std::io::Result<bool> {
Ok(true)
}
pub fn unlock(_: &File) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_damaged_record_is_no_process() {
let d = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir(d.path())).unwrap();
std::fs::write(dir(d.path()).join("mysql.json"), [0u8; 116]).unwrap();
assert!(running(d.path(), "mysql").is_none());
std::fs::write(dir(d.path()).join("auth.json"), b"").unwrap();
assert!(running(d.path(), "auth").is_none());
}
#[test]
fn this_process_is_recorded_and_found_running() {
let d = tempfile::tempdir().unwrap();
let me = record(d.path(), "probe", std::process::id()).unwrap();
assert_eq!(running(d.path(), "probe"), Some(me));
let text = std::fs::read_to_string(dir(d.path()).join("probe.json")).unwrap();
assert!(text.starts_with("{\n \"pid\": ") && text.ends_with("}\n"), "{text}");
}
#[test]
fn one_action_at_a_time() {
let d = tempfile::tempdir().unwrap();
let first = ControlLock::acquire(d.path(), "start-all", Duration::from_secs(1), &|_| {}).unwrap();
let owner: serde_json::Value = fsx::read_json(&dir(d.path()).join("control-owner.json")).unwrap();
assert_eq!(owner["action"], "start-all");
let said = std::sync::Mutex::new(Vec::new());
let second = ControlLock::acquire(d.path(), "stop-all", Duration::from_millis(600), &|m| said.lock().unwrap().push(m.to_string()));
if cfg!(windows) {
let err = second.err().expect("the second action waits and gives up").to_string();
assert!(err.contains("Another start/stop action is in progress") && err.contains("start-all"), "{err}");
assert_eq!(said.lock().unwrap().len(), 1);
}
drop(first);
assert!(!dir(d.path()).join("control-owner.json").exists());
ControlLock::acquire(d.path(), "stop-all", Duration::from_secs(1), &|_| {}).unwrap();
}
}
+360
View File
@@ -0,0 +1,360 @@
//! The world server and the bug-report relay beside it (`manage.py start_world`, `watch_world`, `wait_relay`),
//! and the detailed-logging switch (`debug_logs`).
//!
//! A world is started through a supervisor process that outlives the caller: it starts `worldserver.exe`, keeps
//! the relay running while the world runs, and stops the relay when the world ends. The supervisor is
//! `coa-launcher.exe watch-world`, or `manage.py watch-world` on a repack that does not ship it yet (both write
//! the same records). The relay itself is still the Python `BugReport/relay.py`, run through the repack's
//! Python, until it is ported too.
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use crate::error::{Error, ErrorCode, Result};
use crate::launcher::services::{self, ensure_free, mysql, spawn, start_mysql, wait_ready, Progress};
use crate::launcher::spawn::Proc;
use crate::launcher::{schedule, settings::Settings, squid, state};
fn stop_relay_flag(root: &Path) -> std::path::PathBuf {
state::dir(root).join("stop-relay")
}
/// The relay is up and has finished starting (it records itself as `relay-ready` once it serves).
pub fn wait_relay(root: &Path) -> Result<()> {
let until = Instant::now() + Duration::from_secs(20);
while Instant::now() < until {
if let (Some(relay), Some(ready)) = (state::running(root, "relay"), state::running(root, "relay-ready")) {
if relay == ready {
return Ok(());
}
}
std::thread::sleep(Duration::from_millis(250));
}
Err(Error::Invalid("Bug-report relay is not ready; check BugReport/Logs/relay.log.".into()))
}
/// The process that runs `watch-world` for a started world.
#[derive(Debug, Clone)]
pub enum Supervisor {
/// `coa-launcher.exe` (this crate's launcher).
Launcher(PathBuf),
/// The repack's Python with `Scripts/manage.py`.
Python { python: PathBuf, script: PathBuf },
}
impl Supervisor {
/// The supervisor a repack ships: `Scripts/coa-launcher.exe` when present, otherwise its Python launcher.
pub fn of(root: &Path) -> Result<Supervisor> {
let exe = root.join("Scripts/coa-launcher.exe");
if exe.is_file() {
return Ok(Supervisor::Launcher(exe));
}
let python = squid::package_root(root).join("Runtime/python/python.exe");
let script = root.join("Scripts/manage.py");
if python.is_file() && script.is_file() {
return Ok(Supervisor::Python { python, script });
}
Err(Error::Service(
ErrorCode::ServerFilesIncomplete,
"This server has no world supervisor (Scripts/coa-launcher.exe or Scripts/manage.py). Repair its program files.".into(),
))
}
}
/// How long a world may take to open both of its listeners, together: slow worlds (many bots, a cold disk) are
/// common, and the limit must not depend on the bot module.
const WORLD_STARTUP_SECONDS: u64 = 1500;
/// Wait until the world's console answers a command (it can listen before it accepts logins).
fn wait_console(root: &Path, cfg: &Settings, until: Instant) -> Result<()> {
let mut last = String::new();
while Instant::now() < until {
if state::running(root, "world").is_none() {
return Err(Error::Service(
ErrorCode::StartupFailed,
"World startup failed: worldserver stopped before its console accepted commands.".into(),
));
}
match crate::ra::Ra::connect_to(cfg.ra_port, &cfg.ra_username, &cfg.ra_password).and_then(|mut ra| ra.run("server info")) {
Ok(_) => return Ok(()),
Err(e) => last = e.to_string(),
}
std::thread::sleep(Duration::from_millis(500));
}
Err(Error::Service(ErrorCode::StartupFailed, format!("World startup failed: its console is still starting. {last}")))
}
fn seconds_until(until: Instant) -> u64 {
until.saturating_duration_since(Instant::now()).as_secs()
}
/// Start the world (and its database, unless it is shared), and wait until its console accepts commands.
pub fn start_world(root: &Path, cfg: &Settings, offset: i32, supervisor: &Supervisor, progress: Progress) -> Result<()> {
start_mysql(root, cfg, progress)?;
squid::validate_bots(root)?;
let until = Instant::now() + Duration::from_secs(squid::startup_timeout(root).max(WORLD_STARTUP_SECONDS));
if state::running(root, "world").is_some() {
if state::running(root, "supervisor").is_none() {
return Err(Error::Service(
ErrorCode::WorldserverAlreadyRunning,
"Worldserver has no repack supervisor. Use Stop_All_Server.bat before restarting it.".into(),
));
}
wait_ready(root, "world", cfg.world_port, seconds_until(until))?;
wait_relay(root)?;
wait_console(root, cfg, until)?;
progress("Worldserver and bug-report relay are already running.");
return Ok(());
}
ensure_free(cfg.world_port, progress)?;
ensure_free(cfg.ra_port, progress)?;
if state::running(root, "supervisor").is_some() || state::running(root, "relay").is_some() {
return Err(Error::Service(
ErrorCode::OperationInProgress,
"The previous world/relay shutdown is still finishing. Try again shortly.".into(),
));
}
squid::prepare_playerbots(root, cfg.mysql_port, &|sql| mysql(root, Some(sql), None), progress)?;
progress("Starting worldserver and its bug-report relay...");
let _ = std::fs::remove_file(stop_relay_flag(root));
let offset_text = offset.to_string();
let root_text = root.to_string_lossy().into_owned();
let log = root.join("Core/Logs/supervisor.log");
match supervisor {
Supervisor::Launcher(exe) => {
spawn(root, "supervisor", exe, &["--root", &root_text, "--port-offset", &offset_text, "watch-world"], root, &log, &cfg.env_remove)?
}
Supervisor::Python { python, script } => {
let script = script.to_string_lossy().into_owned();
spawn(root, "supervisor", python, &["-B", &script, "--port-offset", &offset_text, "watch-world"], root, &log, &cfg.env_remove)?
}
};
let first = Instant::now() + Duration::from_secs(20);
while state::running(root, "world").is_none() && Instant::now() < first {
if state::running(root, "supervisor").is_none() {
return Err(Error::Service(ErrorCode::StartupFailed, "World startup failed; check Core/Logs/supervisor.log.".into()));
}
std::thread::sleep(Duration::from_millis(250));
}
wait_ready(root, "world", cfg.world_port, seconds_until(until))?;
wait_ready(root, "world", cfg.ra_port, seconds_until(until))?;
wait_relay(root)?;
wait_console(root, cfg, until)?;
progress("Worldserver and automatic bug reporting are ready.");
Ok(())
}
fn spawn_child(root: &Path, name: &str, exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<Proc> {
// The supervisor was started with the environment its services should have.
let child = crate::launcher::spawn::start(exe, args, cwd, log, &[])?;
state::record(root, name, child.pid)?;
Ok(child)
}
/// The relay worker: `coa-launcher relay-worker` when the supervisor is the launcher (`launcher`), otherwise the
/// repack's Python running `Scripts/manage.py relay-worker` (BugReport/relay.py).
fn spawn_relay(root: &Path, launcher: Option<&Path>) -> Result<Proc> {
let log = root.join("BugReport/Logs/relay.log");
if let Some(exe) = launcher {
let root_text = root.to_string_lossy().into_owned();
return spawn_child(root, "relay", exe, &["--root", &root_text, "relay-worker"], root, &log);
}
let python = squid::package_root(root).join("Runtime/python/python.exe");
let manage = root.join("Scripts/manage.py");
if !python.is_file() || !manage.is_file() {
return Err(Error::Invalid("The bug-report relay needs the repack's Python and Scripts/manage.py.".into()));
}
let manage = manage.to_string_lossy().into_owned();
spawn_child(root, "relay", &python, &["-B", &manage, "relay-worker"], root, &root.join("BugReport/Logs/relay.log"))
}
/// The supervisor (`watch-world`): run the world, keep the relay alive while it runs, stop the relay after it.
/// `launcher` is the launcher executable that runs the relay (`None`: the Python relay). At the owner's scheduled
/// time it asks the world to restart with an in-game countdown and starts it again when it exits for that.
pub fn watch_world(root: &Path, launcher: Option<&Path>, offset: i32, log: Progress) -> Result<()> {
squid::validate_bots(root)?;
let mut relay: Option<Proc> = None;
let mut last_log_clean = Instant::now();
loop {
if let Some(report) = crate::server_logging::auto_clean_if_needed(root) {
if report.freed_bytes > 0 {
log(&format!("Auto-cleaned server logs ({} MB freed).", report.freed_bytes / (1024 * 1024)));
}
}
let world = spawn_child(root, "world", &root.join("Core/worldserver.exe"), &["-c", "configs/worldserver.conf"], &root.join("Core"), &root.join("Core/Logs/world-console.log"))?;
let started = schedule::now();
let mut requested = false;
let mut last_check: Option<Instant> = None;
let code = loop {
if let Some(code) = world.try_wait() {
break code;
}
let dead = relay.as_ref().is_none_or(|r| r.try_wait().is_some());
if dead {
match spawn_relay(root, launcher) {
Ok(r) => relay = Some(r),
Err(e) => {
log(&format!("Report relay could not start ({e}); retrying."));
std::thread::sleep(Duration::from_secs(5));
continue;
}
}
}
if last_check.is_none_or(|t| t.elapsed() >= Duration::from_secs(30)) {
last_check = Some(Instant::now());
if schedule::under_way(root) && !requested {
// The world that a scheduled restart started again is back once it accepts connections.
if Settings::load(root, offset).is_ok_and(|cfg| services::ready(root, "world", cfg.world_port)) {
schedule::clear_under_way(root);
}
}
if !requested {
let plan = schedule::load(root);
if let Some(due) = plan.due_restart(schedule::now(), started) {
let seconds = due.warning_minutes * 60;
let asked = Settings::load(root, offset).and_then(|cfg| {
crate::ra::Ra::connect_to(cfg.ra_port, &cfg.ra_username, &cfg.ra_password)
.and_then(|mut ra| ra.run(&format!("server restart {seconds}")))
});
match asked {
Ok(_) => {
schedule::mark_under_way(root)?;
requested = true;
log(&format!("Scheduled restart: the world restarts in {} minutes.", due.warning_minutes));
}
Err(e) => log(&format!("Scheduled restart could not be requested ({e}); retrying.")),
}
}
}
if last_log_clean.elapsed() >= Duration::from_secs(300) {
last_log_clean = Instant::now();
if let Some(report) = crate::server_logging::auto_clean_if_needed(root) {
if report.freed_bytes > 0 {
log(&format!("Auto-cleaned server logs ({} MB freed).", report.freed_bytes / (1024 * 1024)));
}
}
}
}
std::thread::sleep(Duration::from_secs(2));
};
if requested && code == schedule::RESTART_EXIT_CODE as u32 {
log("Scheduled restart: starting the world again.");
if let Err(e) = squid::validate_bots(root) {
schedule::clear_under_way(root);
return Err(e);
}
continue;
}
schedule::clear_under_way(root);
break;
}
std::fs::write(stop_relay_flag(root), b"")?;
if let Some(r) = relay {
let until = Instant::now() + Duration::from_secs(40);
while r.try_wait().is_none() && Instant::now() < until {
std::thread::sleep(Duration::from_millis(250));
}
if r.try_wait().is_none() {
// An interrupted POST stays uncertain in the relay's journal; it is not resent.
if let Some(record) = state::running(root, "relay") {
services::terminate(&record)?;
}
}
}
Ok(())
}
// ---------------------------------------------------------------------------------------------------------------
// Detailed logging (Debug_Logs_On.bat / Debug_Logs_Off.bat)
// ---------------------------------------------------------------------------------------------------------------
const DEBUG_TEMPLATES: [&str; 2] = ["worldserver.conf.template", "authserver.conf.template"];
/// One template line with every logger forced to Trace and the console/server appenders to the most detail.
fn debug_line(line: &str) -> String {
let body = line.strip_prefix('#').unwrap_or(line).trim_start();
for appender in ["Appender.Console", "Appender.Server"] {
if let Some(rest) = body.strip_prefix(appender).and_then(|r| r.strip_prefix('=')) {
let parts: Vec<&str> = rest.splitn(4, ',').collect();
if parts.len() == 4 && parts[..3].iter().all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())) {
return format!("{appender}={},6,7,{}", parts[0], parts[3]);
}
}
}
if let Some(rest) = body.strip_prefix("Logger.") {
if let Some((name, value)) = rest.split_once('=') {
if !name.is_empty() && name.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'.') {
if let Some((level, tail)) = value.split_once(',') {
if !level.is_empty() && level.bytes().all(|b| b.is_ascii_digit()) {
return format!("Logger.{name}=6,{tail}");
}
}
}
}
}
let t = line.trim_end();
if let Some(v) = t.strip_prefix("Log.Async.Enable") {
let v = v.trim_start();
if let Some(n) = v.strip_prefix('=') {
let n = n.trim();
if !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()) {
return "Log.Async.Enable = 1".into();
}
}
}
line.to_string()
}
/// Turn detailed logging on or off (the templates; services apply it at their next start). Returns the changed
/// templates.
pub fn debug_logs(root: &Path, enable: bool) -> Result<Vec<String>> {
let mut changed = Vec::new();
for name in DEBUG_TEMPLATES {
let path = root.join("Settings").join(name);
let original = root.join("Settings").join(format!("{name}.original"));
if !original.exists() {
std::fs::write(&original, std::fs::read(&path)?)?;
}
let new_bytes = if enable {
let text = String::from_utf8(std::fs::read(&original)?).map_err(|_| Error::Invalid(format!("{name} is not UTF-8.")))?;
let newline = if text.contains("\r\n") { "\r\n" } else { "\n" };
let mut out = text.lines().map(debug_line).collect::<Vec<_>>().join(newline);
if text.ends_with('\n') {
out.push_str(newline);
}
out.into_bytes()
} else {
std::fs::read(&original)?
};
if std::fs::read(&path)? != new_bytes {
std::fs::write(&path, &new_bytes)?;
changed.push(name.to_string());
}
}
state::write_json(&state::dir(root).join("debug-logs.json"), &serde_json::json!({ "enabled": enable }))?;
Ok(changed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detailed_logging_rewrites_loggers_and_restores_the_original() {
let d = tempfile::tempdir().unwrap();
std::fs::create_dir_all(d.path().join("Settings")).unwrap();
let world = "Appender.Console=1,3,0\r\nAppender.Server=2,2,0,Server.log,w\r\n#Logger.module=4,Console Server\r\nLogger.root=5,Console Server\r\nLog.Async.Enable = 0\r\nOther = 1\r\n";
std::fs::write(d.path().join("Settings/worldserver.conf.template"), world).unwrap();
std::fs::write(d.path().join("Settings/authserver.conf.template"), "Logger.root=2,Console\n").unwrap();
let changed = debug_logs(d.path(), true).unwrap();
assert_eq!(changed.len(), 2);
assert_eq!(
std::fs::read_to_string(d.path().join("Settings/worldserver.conf.template")).unwrap(),
"Appender.Console=1,3,0\r\nAppender.Server=2,6,7,Server.log,w\r\nLogger.module=6,Console Server\r\nLogger.root=6,Console Server\r\nLog.Async.Enable = 1\r\nOther = 1\r\n"
);
assert_eq!(debug_logs(d.path(), true).unwrap().len(), 0, "already on");
debug_logs(d.path(), false).unwrap();
assert_eq!(std::fs::read_to_string(d.path().join("Settings/worldserver.conf.template")).unwrap(), world);
assert_eq!(services::status(d.path())["debugLogs"], false);
}
}
-85
View File
@@ -1,85 +0,0 @@
import runpy
import sys
import time
from pathlib import Path
script = sys.argv[1]
sys.path.insert(0, str(Path(script).resolve().parent))
sys.argv = sys.argv[1:]
launcher = runpy.run_path(script, run_name="coa_manager_launcher")
original_wait = launcher["wait_ready"]
world_deadline = None
def wait_ready(name, port, seconds=180):
global world_deadline
if name == "mysql":
seconds = max(seconds, 300)
elif name == "world":
# Both game and console listeners belong to one startup, including on old repacks.
# Ordinary CoA worlds can load slowly too; this must not depend on SQUID.
if world_deadline is None:
world_deadline = time.monotonic() + max(seconds, 1500)
seconds = max(0, world_deadline - time.monotonic())
return original_wait(name, port, seconds)
launcher["start_mysql"].__globals__["wait_ready"] = wait_ready
if sys.argv[1] in ("stop-all", "force-stop-all"):
shutdown = launcher["stop_all"].__globals__
class UnsafeShutdown(Exception):
pass
def stop_owned_world(world):
expected = Path(script).resolve().parents[1] / "Core" / "worldserver.exe"
if Path(world.get("exe", "")) != expected or not shutdown["same_process"](world):
raise UnsafeShutdown("World process ownership could not be verified; the database was left running.")
try:
shutdown["terminate"](world)
except (OSError, RuntimeError) as error:
raise UnsafeShutdown("Worldserver could not be stopped safely; its database was left running.") from error
if not shutdown["wait_stopped"](world, 90):
raise UnsafeShutdown("Worldserver is still stopping; its database was left running.")
if "ra" in shutdown:
original_ra = shutdown["ra"]
def shutdown_command(command, config):
try:
return original_ra(command, config)
except (OSError, RuntimeError):
if not command.startswith("server shutdown "):
raise
world = shutdown["process"]("world")
if not world:
return "Worldserver has already stopped."
stop_owned_world(world)
return "Stopped this server's recorded world process without its unavailable console."
shutdown["ra"] = shutdown_command
config = launcher["settings"]()
with launcher["control_lock"]():
if sys.argv[1] == "force-stop-all":
world = shutdown["process"]("world")
if world:
stop_owned_world(world)
print("Force-stopped this server's verified world process; unsaved progress may be lost.", flush=True)
launcher["stop_all"](config)
else:
launcher["main"]()
if sys.argv[1] in ("start-all", "start-world"):
config = launcher["settings"]()
deadline = world_deadline if world_deadline is not None else time.monotonic() + 1500
last_error = None
while time.monotonic() < deadline:
if not launcher["process"]("world"):
raise RuntimeError("World startup failed: worldserver stopped before its console accepted commands.")
try:
launcher["ra"]("server info", config)
print("World console is accepting commands.", flush=True)
break
except (OSError, RuntimeError) as error:
last_error = error
time.sleep(0.5)
else:
raise RuntimeError(f"World startup failed: its console is still starting. {last_error}")
+222
View File
@@ -0,0 +1,222 @@
//! `Scripts/manage.py` as Managers up to 0.9.10 edited it.
//!
//! Those Managers ran the repack's Python launcher themselves and changed its text: an import fix for their bridge
//! and a realm-profile patch (database and realm row of the active realm). This Manager starts servers with
//! [`crate::launcher`] and never edits the file; it only has to recognise those edits, so that an update or a file
//! check treats a file one of them changed as unmodified. Nothing here writes a file.
use crate::error::{Error, Result};
const LAUNCHER_REALM_REPLACEMENTS: &[(&str, &str)] = &[
("(\"WorldDatabaseInfo\", \"acore_world\")", "(\"WorldDatabaseInfo\", \"acore_world_wildcard\" if coa_realm() == 'wildcard' else \"acore_world\")"),
("(\"CharacterDatabaseInfo\", \"acore_characters\")", "(\"CharacterDatabaseInfo\", \"acore_characters_wildcard\" if coa_realm() == 'wildcard' else \"acore_characters\")"),
("SET name='AzerothCore',address=", "SET name='{coa_realm_name()}',address="),
("WHERE id=1;", "WHERE id={coa_realm_id()};"),
("mysql(\"UPDATE acore_auth.realmlist SET flag=0", "mysql(f\"UPDATE acore_auth.realmlist SET flag=0"),
];
const LAUNCHER_REALM_INSERT: &str = "# coa-manager-realm-profiles-v1\ndef coa_realm():\n path = ROOT / 'Settings/realm-profile.json'\n return json.loads(path.read_text(encoding='utf-8')).get('active', 'coa') if path.exists() else 'coa'\n\ndef coa_realm_id():\n return 2 if coa_realm() == 'wildcard' else 1\n\ndef coa_realm_name():\n return 'Wildcard' if coa_realm() == 'wildcard' else 'Conquest of Azeroth'\n\n";
fn launcher_insert() -> String {
LAUNCHER_REALM_INSERT
.replace("profiles-v1", "profiles-v2")
.replace(
"return 2 if",
"return 3 if coa_realm() == 'reborn' else 2 if",
)
.replace(
"return 'Wildcard' if",
"return 'Warcraft Reborn' if coa_realm() == 'reborn' else 'Wildcard' if",
)
}
fn launcher_replacements() -> Vec<(&'static str, String)> {
LAUNCHER_REALM_REPLACEMENTS.iter().map(|(from, to)| (*from, to
.replace("\"acore_world_wildcard\" if", "\"acore_world_reborn\" if coa_realm() == 'reborn' else \"acore_world_wildcard\" if")
.replace("\"acore_characters_wildcard\" if", "\"acore_characters_reborn\" if coa_realm() == 'reborn' else \"acore_characters_wildcard\" if"))).collect()
}
/// The realm-profile patch older Managers wrote.
pub(crate) fn patch_launcher(source: &str) -> Result<String> {
if source.contains("# coa-manager-realm-profiles-v2") {
return Ok(source.into());
}
if source.contains("# coa-manager-realm-profiles-v1") {
let original = unpatch_launcher(source)
.ok_or_else(|| Error::Invalid("Unrecognised realm launcher patch.".into()))?;
return patch_launcher(&original);
}
let mut out = source.to_string();
for (from, to) in launcher_replacements() {
if !out.contains(from) {
return Err(Error::Invalid(
"This launcher does not support realm profiles. Update the server package first."
.into(),
));
}
out = out.replace(from, &to);
}
// Insert before the entry point so ROOT, json and functions are all defined when called.
let marker = "if __name__ == \"__main__\":";
if !out.contains(marker) {
return Err(Error::Invalid(
"Unrecognised server launcher entry point.".into(),
));
}
Ok(out.replace(marker, &format!("{}{marker}", launcher_insert())))
}
/// Reverse only our exact realm transformation; callers must verify the recovered original hash.
pub(crate) fn unpatch_launcher(source: &str) -> Option<String> {
let (insert, replacements) = if source.contains(&launcher_insert()) {
(launcher_insert(), launcher_replacements())
} else if source.contains(LAUNCHER_REALM_INSERT) {
(
LAUNCHER_REALM_INSERT.into(),
LAUNCHER_REALM_REPLACEMENTS
.iter()
.map(|(a, b)| (*a, b.to_string()))
.collect(),
)
} else {
return None;
};
let mut original = source.replacen(&insert, "", 1);
for (from, to) in &replacements {
original = original.replace(to, from);
}
let mut check = original.clone();
for (from, to) in &replacements {
check = check.replace(from, to);
}
check = check.replace(
"if __name__ == \"__main__\":",
&format!("{insert}if __name__ == \"__main__\":"),
);
(check == source).then_some(original)
}
/// The import fix older Managers wrote (needed to run manage.py through their bridge).
pub(crate) fn patch_launcher_imports(text: &str) -> Result<String> {
let line = "sys.path.insert(0, str(Path(__file__).resolve().parent))";
if !text.contains("from squid_playerbots import") || text.contains(line) {
return Ok(text.into());
}
let root = "ROOT = Path(__file__).resolve().parents[1]";
if !text.contains(root) || !text.lines().any(|line| line.trim() == "import sys") {
return Err(Error::Invalid(
"The server launcher cannot load its integration scripts. Repair its program files."
.into(),
));
}
Ok(text.replacen(root, &format!("{root}\n{line}"), 1))
}
pub(crate) fn launcher_matches(signed: &str, actual: &[u8]) -> bool {
if signed.as_bytes() == actual {
return true;
}
if patch_launcher_imports(signed).is_ok_and(|s| s.as_bytes() == actual) {
return true;
}
patch_launcher(signed).is_ok_and(|s| {
s.as_bytes() == actual || patch_launcher_imports(&s).is_ok_and(|p| p.as_bytes() == actual)
})
}
/// Recover legacy metadata only when undoing supported Manager edits reproduces the recorded hash.
pub(crate) fn launcher_matches_recorded(recorded: &str, actual: &[u8]) -> bool {
let Ok(text) = std::str::from_utf8(actual) else {
return false;
};
let without_import = text.replacen(
"ROOT = Path(__file__).resolve().parents[1]\nsys.path.insert(0, str(Path(__file__).resolve().parent))",
"ROOT = Path(__file__).resolve().parents[1]", 1,
);
for candidate in [
Some(without_import.clone()),
unpatch_launcher(&without_import),
]
.into_iter()
.flatten()
{
if crate::fsx::sha256_bytes(candidate.as_bytes()).eq_ignore_ascii_case(recorded)
&& launcher_matches(&candidate, actual)
{
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fsx;
use std::collections::BTreeMap;
use std::fs;
#[test]
fn old_launcher_upgrades_and_both_patch_versions_are_reversible() {
let original = "(\"WorldDatabaseInfo\", \"acore_world\")\n(\"CharacterDatabaseInfo\", \"acore_characters\")\nSET name='AzerothCore',address=\nWHERE id=1;\nmysql(\"UPDATE acore_auth.realmlist SET flag=0\nif __name__ == \"__main__\":\n";
let mut old = original.to_string();
for (from, to) in LAUNCHER_REALM_REPLACEMENTS {
old = old.replace(from, to);
}
old = old.replace(
"if __name__ == \"__main__\":",
&format!("{LAUNCHER_REALM_INSERT}if __name__ == \"__main__\":"),
);
assert_eq!(unpatch_launcher(&old).as_deref(), Some(original));
let new = patch_launcher(&old).unwrap();
assert!(new.contains("acore_world_reborn"));
assert!(new.contains("return 3 if coa_realm() == 'reborn'"));
assert!(new.contains("return 'Warcraft Reborn' if"));
assert_eq!(patch_launcher(&new).unwrap(), new);
assert_eq!(unpatch_launcher(&new).as_deref(), Some(original));
}
#[test]
fn unknown_launcher_is_refused_without_partial_patching() {
assert!(patch_launcher("print('custom launcher')").is_err());
}
#[test]
fn launcher_import_fix_is_idempotent_and_retains_other_code() {
let text = "import sys\nROOT = Path(__file__).resolve().parents[1]\nfrom squid_playerbots import validate_bots\ncustom = 42\n";
let fixed = patch_launcher_imports(text).unwrap();
assert!(fixed.contains("sys.path.insert(0, str(Path(__file__).resolve().parent))"));
assert!(fixed.ends_with("custom = 42\n"));
assert_eq!(patch_launcher_imports(&fixed).unwrap(), fixed);
}
#[test]
fn a_launcher_patched_by_an_older_manager_stays_pristine_but_external_edits_conflict() {
let d = tempfile::tempdir().unwrap();
let root = d.path().join("server");
let source = "(\"WorldDatabaseInfo\", \"acore_world\")\n(\"CharacterDatabaseInfo\", \"acore_characters\")\nSET name='AzerothCore',address=\nWHERE id=1;\nmysql(\"UPDATE acore_auth.realmlist SET flag=0\nif __name__ == \"__main__\":\n";
fsx::atomic_write(&root.join("Scripts/manage.py"), source.as_bytes()).unwrap();
// What an older Manager left: the realm patch applied and its hash recorded as the pristine one.
let patched = patch_launcher(source).unwrap();
fsx::atomic_write(&root.join("Scripts/manage.py"), patched.as_bytes()).unwrap();
let mut meta = crate::registry::InstallMeta::new(crate::registry::InstallKind::New, &root);
meta.original_hashes.insert("Scripts/manage.py".into(), fsx::sha256_bytes(patched.as_bytes()));
crate::registry::MetaDir::create(&root, &meta).unwrap();
assert!(crate::diag::verify_managed(&root, &meta).is_empty());
let manifest: crate::manifest::Manifest = serde_json::from_value(serde_json::json!({
"schema": 1, "kind": "update", "version": "2.0.0", "core": {"commit": null}, "builtAt": "fixture", "minManagerVersion": "0.1.0",
"files": [{"path":"Scripts/manage.py","sha256":fsx::sha256_bytes(b"next release"),"size":12,"owner":"core","policy":"replace"}]
})).unwrap();
assert_eq!(
crate::update::plan(&root, &meta, &manifest, &BTreeMap::new(), None).unwrap()[0].action,
crate::update::Action::Replace
);
let mut modified = fs::read(root.join("Scripts/manage.py")).unwrap();
modified.extend_from_slice(b"# custom edit\n");
fsx::atomic_write(&root.join("Scripts/manage.py"), &modified).unwrap();
assert_eq!(
crate::update::plan(&root, &meta, &manifest, &BTreeMap::new(), None).unwrap()[0].action,
crate::update::Action::Conflict
);
}
}
+6 -2
View File
@@ -9,8 +9,10 @@
pub mod accounts; pub mod accounts;
pub mod allsettings; pub mod allsettings;
pub mod addons;
pub mod analyzer; pub mod analyzer;
pub mod backup; pub mod backup;
pub mod build_cache;
pub mod build_engine; pub mod build_engine;
pub mod catalogue; pub mod catalogue;
pub mod channels; pub mod channels;
@@ -18,7 +20,6 @@ pub mod character_start_check;
pub mod cleanbase; pub mod cleanbase;
pub mod client; pub mod client;
pub mod client_graphics; pub mod client_graphics;
pub mod clientdl;
pub mod companions; pub mod companions;
pub mod config; pub mod config;
pub mod console; pub mod console;
@@ -31,6 +32,7 @@ pub mod diag;
pub mod docker; pub mod docker;
pub mod download; pub mod download;
pub mod driver; pub mod driver;
pub mod legacy_launcher;
pub mod error; pub mod error;
pub mod firewall; pub mod firewall;
pub mod friends; pub mod friends;
@@ -39,10 +41,12 @@ pub mod glue_config;
pub mod health; pub mod health;
pub mod install; pub mod install;
pub mod layout; pub mod layout;
pub mod launcher;
pub mod logging; pub mod logging;
pub mod server_logging; pub mod server_logging;
pub mod manifest; pub mod manifest;
pub mod migrations; pub mod migrations;
pub mod modular_copy;
pub mod modules; pub mod modules;
pub mod module_sources; pub mod module_sources;
pub mod msvc_setup; pub mod msvc_setup;
@@ -62,6 +66,7 @@ pub mod ra;
pub mod race_availability; pub mod race_availability;
pub mod realm_registry; pub mod realm_registry;
pub mod realmlist; pub mod realmlist;
pub mod realm_names;
pub mod realms; pub mod realms;
pub mod rebuild; pub mod rebuild;
pub mod registry; pub mod registry;
@@ -72,7 +77,6 @@ pub mod remote_client;
pub mod repair; pub mod repair;
pub mod report; pub mod report;
pub mod schema_check; pub mod schema_check;
pub mod server_migration;
pub mod signing; pub mod signing;
pub mod solver; pub mod solver;
pub mod squid; pub mod squid;
+521
View File
@@ -0,0 +1,521 @@
//! A modular copy of a classic server, next to it.
//!
//! A server with the classic core (every module compiled into `worldserver.exe`) is never converted in place.
//! The Modules Preview installs a new server beside it and copies the old one's data into it: every game
//! database (accounts, characters, world, bots) and the module settings. The old server is only read:
//!
//! * if its database is running, the schemas are dumped from it (consistent snapshots, nothing is written);
//! * if it is stopped, its data files are copied to a private folder and read by a temporary database
//! process of the NEW server; nothing of the old server is started or executed.
//!
//! The new server then takes the preview core like any other server (the caller applies the update).
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
use serde::Serialize;
use crate::db::{Account, Db};
use crate::error::{Error, Result};
use crate::fsx;
use crate::process::ServiceState;
/// Whether `root` runs the classic core: a Windows repack whose `worldserver.exe` carries no plugin-host
/// fingerprint.
pub fn is_classic(root: &Path) -> bool {
!crate::docker::is_docker(root)
&& root.join("Core/worldserver.exe").is_file()
&& crate::provenance::realm_host_fingerprint(root).is_none()
}
#[derive(Debug, Clone, Serialize)]
pub struct CopyReport {
pub schemas: Vec<String>,
pub accounts: u64,
pub characters: u64,
pub config_files: Vec<String>,
/// How the old data was read: "running database" or "copy of the stopped database".
pub read_from: String,
/// World settings the owner changed (Server Configuration) that were set on the new server too.
pub world_settings: Vec<String>,
/// Changed world settings the new server's configuration does not take, with the reason.
pub world_settings_skipped: Vec<String>,
}
/// What a modular copy of `old` would take, read without changing anything: the old server is never started,
/// and its database is only queried when it is already running.
#[derive(Debug, Clone, Serialize)]
pub struct CopyPreview {
pub path: PathBuf,
/// The classic core (modules compiled in): the only kind that is copied.
pub classic: bool,
/// Already a modular core: it is added as it is, not copied.
pub modular: bool,
pub version: Option<String>,
/// "running", "stopped" or "missing" (no database folder).
pub database: String,
/// Read from the running database; `None` when it is stopped (they are counted while copying).
pub accounts: Option<u64>,
pub characters: Option<u64>,
pub schemas: Vec<String>,
/// Size of the old database files; a stopped database is copied once more while it is read.
pub database_bytes: u64,
/// Settings files that go along (module configs, realm profiles).
pub config_files: Vec<String>,
/// Extra worlds of the old server (`.realms/<name>`). Their databases are in the same MySQL and go along, and
/// the copied realm profile switches them on; the new server prepares their folders when it starts them.
pub extra_worlds: Vec<String>,
}
pub fn preview(old: &Path) -> Result<CopyPreview> {
if !old.is_dir() {
return Err(Error::Invalid(format!("{} is not a folder.", old.display())));
}
let classic = is_classic(old);
let modular = !crate::docker::is_docker(old)
&& old.join("Core/worldserver.exe").is_file()
&& crate::provenance::realm_host_fingerprint(old).is_some();
let version = crate::registry::metadata_dir_for(old)
.ok()
.and_then(|dir| crate::registry::MetaDir::open(&dir).ok())
.and_then(|(_, meta)| meta.core.version);
let data = old.join("mysql/data");
let has_data = data.join("mysql").is_dir();
let observed = crate::process::observe(old, &crate::layout::read_ports(old));
let running_db = (has_data && observed.mysql.state == ServiceState::Running)
.then(|| Db::from_repack(old, Account::Admin).ok())
.flatten()
.filter(|db| db.ping())
.map(|db| db.for_realm(crate::realms::Mode::Coa));
let (database, accounts, characters, schemas) = match &running_db {
Some(db) => (
"running",
Some(count(db, "SELECT COUNT(*) FROM acore_auth.account;")),
Some(count(db, "SELECT COUNT(*) FROM acore_characters.characters;")),
game_schemas(db).unwrap_or_default(),
),
None if has_data => ("stopped", None, None, Vec::new()),
None => ("missing", None, None, Vec::new()),
};
let mut extra_worlds: Vec<String> = fs::read_dir(old.join(".realms"))
.map(|rd| rd.flatten().filter(|e| e.path().is_dir()).map(|e| e.file_name().to_string_lossy().into_owned()).collect())
.unwrap_or_default();
extra_worlds.sort();
Ok(CopyPreview {
path: old.to_path_buf(),
classic,
modular,
version,
database: database.into(),
accounts,
characters,
schemas,
database_bytes: if has_data { dir_size(&data) } else { 0 },
config_files: settings_files(old),
extra_worlds,
})
}
/// Game schemas of a CoA server: `acore_*` (auth, characters, world, their Wildcard/Reborn variants, bots).
fn game_schemas(db: &Db) -> Result<Vec<String>> {
let mut out: Vec<String> = db
.query("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME LIKE 'acore\\_%' ORDER BY SCHEMA_NAME;")?
.lines()
.map(|l| l.trim().to_string())
.filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'))
// work schemas a restore leaves behind are not game data
.filter(|s| !s.contains("_restore_") && !s.contains("_before_restore_"))
.collect();
out.dedup();
if !out.iter().any(|s| s == "acore_auth") || !out.iter().any(|s| s == "acore_characters") {
return Err(Error::Invalid("The old server's database has no accounts or characters schema.".into()));
}
Ok(out)
}
fn count(db: &Db, sql: &str) -> u64 {
db.query(sql).ok().and_then(|s| s.trim().parse().ok()).unwrap_or(0)
}
/// A database process of the new server reading a private copy of the old server's stopped data files.
struct TemporaryDatabase {
child: Child,
dir: PathBuf,
db: Option<Db>,
stopped: bool,
}
impl TemporaryDatabase {
/// Shut it down cleanly, force it if it does not go, and remove the private copy. Fails if the process is
/// still there afterwards, so a leftover database never holds the new server's files.
fn stop(&mut self) -> Result<()> {
if self.stopped {
return Ok(());
}
if let Some(db) = &self.db {
let _ = db.shutdown_server();
}
let deadline = Instant::now() + Duration::from_secs(90);
loop {
match self.child.try_wait()? {
Some(_) => break,
None if Instant::now() > deadline => {
tracing::warn!(pid = self.child.id(), "temporary database did not shut down; terminating it");
self.child.kill()?;
self.child.wait()?;
break;
}
None => std::thread::sleep(Duration::from_millis(250)),
}
}
self.stopped = true;
fs::remove_dir_all(&self.dir)?;
Ok(())
}
}
impl Drop for TemporaryDatabase {
fn drop(&mut self) {
if !self.stopped {
if let Err(e) = self.stop() {
tracing::warn!(error = %e, pid = self.child.id(), "temporary database left behind");
}
}
}
}
fn copy_tree(from: &Path, to: &Path) -> Result<u64> {
let mut bytes = 0;
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let md = fs::symlink_metadata(entry.path())?;
if md.file_type().is_symlink() {
return Err(Error::Invalid(format!("{} is a link; the database folder is not copied.", entry.path().display())));
}
let target = to.join(entry.file_name());
if md.is_dir() {
bytes += copy_tree(&entry.path(), &target)?;
} else {
fs::copy(entry.path(), &target)?;
bytes += md.len();
}
}
Ok(bytes)
}
fn dir_size(dir: &Path) -> u64 {
fs::read_dir(dir).map(|rd| rd.flatten().map(|e| {
let p = e.path();
if p.is_dir() { dir_size(&p) } else { e.metadata().map(|m| m.len()).unwrap_or(0) }
}).sum()).unwrap_or(0)
}
fn start_temporary(old: &Path, new: &Path, work: &Path, progress: &dyn Fn(&str)) -> Result<(TemporaryDatabase, Db)> {
let source = old.join("mysql/data");
if !source.join("mysql").is_dir() || !source.join("ibdata1").is_file() {
return Err(Error::Invalid("The old server's database folder is incomplete.".into()));
}
let dir = work.join("old-database");
let _ = fs::remove_dir_all(&dir);
fsx::require_space(work, dir_size(&source))?;
progress("Copying the old server's database files");
let data = dir.join("data");
copy_tree(&source, &data)?;
let (root_password, _) = crate::db::credentials(old)?;
let bin = new.join("mysql/bin");
let port = crate::install::free_port()?;
let child = Command::new(bin.join("mysqld.exe"))
.arg("--no-defaults")
.arg(format!("--basedir={}", new.join("mysql").display()))
.arg(format!("--datadir={}", data.display()))
.arg(format!("--port={port}"))
.args(["--bind-address=127.0.0.1", "--mysqlx=0", "--skip-log-bin", "--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_ci", "--max_allowed_packet=128M"])
.arg(format!("--log-error={}", dir.join("mysql-error.log").display()))
.arg(format!("--pid-file={}", dir.join("mysql.pid").display()))
.stdout(Stdio::null())
.stderr(Stdio::null())
.creation_flags_hidden()
.spawn()?;
let db = Db::with_tools(bin, port, "root", &root_password, crate::realms::Mode::Coa);
let mut temp = TemporaryDatabase { child, dir, db: Some(db.clone()), stopped: false };
progress("Opening the copy of the old database");
let deadline = Instant::now() + Duration::from_secs(300);
while !db.ping() {
if let Ok(Some(status)) = temp.child.try_wait() {
let log = fs::read_to_string(temp.dir.join("mysql-error.log")).unwrap_or_default();
let tail: Vec<&str> = log.lines().rev().take(8).collect();
return Err(Error::Invalid(format!(
"The copy of the old database did not open ({status}). {}",
tail.into_iter().rev().collect::<Vec<_>>().join(" ")
)));
}
if Instant::now() > deadline {
return Err(Error::Invalid("The copy of the old database did not open within 5 minutes.".into()));
}
std::thread::sleep(Duration::from_millis(300));
}
Ok((temp, db))
}
trait HiddenWindow {
fn creation_flags_hidden(&mut self) -> &mut Self;
}
impl HiddenWindow for Command {
fn creation_flags_hidden(&mut self) -> &mut Self {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
self.creation_flags(0x0800_0000);
}
self
}
}
/// Settings the owner may have changed that the new core can take as they are: the module configs
/// (`Core/configs/modules/*.conf`; the shipped `.dist` files belong to the new core) and the realm profiles.
/// `worldserver.conf` and `authserver.conf` are rendered by the launcher from the new server's own
/// templates at every start, so they are not copied.
fn settings_files(old: &Path) -> Vec<String> {
crate::backup::config_files(old)
.into_iter()
.filter(|f| {
(f.starts_with("Core/configs/modules/") && f.ends_with(".conf") && !f.ends_with("/mod_ascension_compat.conf"))
|| f.starts_with("Settings/realm-profile")
})
.collect()
}
/// Copy the old server's data into `new`, a server just installed and not yet started by anyone.
pub fn copy_data(old: &Path, new: &Path, progress: &dyn Fn(&str)) -> Result<CopyReport> {
if !is_classic(old) {
return Err(Error::Invalid("Only a server with the classic core is copied.".into()));
}
if fsx::canonicalize_lenient(old)? == fsx::canonicalize_lenient(new)? {
return Err(Error::Invalid("The new server must be in another folder.".into()));
}
let work = crate::registry::metadata_dir_for(new)?.join("import");
let _ = fs::remove_dir_all(&work);
fs::create_dir_all(&work)?;
// 1. Read the old databases.
let observed = crate::process::observe(old, &crate::layout::read_ports(old));
let running = observed.mysql.state == ServiceState::Running
&& Db::from_repack(old, Account::Admin).map(|db| db.ping()).unwrap_or(false);
let mut temporary: Option<TemporaryDatabase> = None;
let (source, read_from) = if running {
(Db::from_repack(old, Account::Admin)?.for_realm(crate::realms::Mode::Coa), "running database")
} else {
let (temp, db) = start_temporary(old, new, &work, progress)?;
temporary = Some(temp);
(db, "copy of the stopped database")
};
let schemas = game_schemas(&source)?;
let accounts = count(&source, "SELECT COUNT(*) FROM acore_auth.account;");
let characters = count(&source, "SELECT COUNT(*) FROM acore_characters.characters;");
let mut dumps = Vec::new();
for schema in &schemas {
progress(&format!("Copying {schema}"));
let file = work.join(format!("{schema}.sql.zst"));
source.dump_to(schema, &file)?;
dumps.push((schema.clone(), file));
}
// The private copy of the old data files is not needed any more; its database must be gone before the new
// server's own database starts.
if let Some(mut temp) = temporary.take() {
progress("Closing the copy of the old database");
temp.stop()?;
}
// 2. Put them into the new server, on ports nobody uses, so a running old server is not disturbed.
crate::install::with_scratch_ports(new, || {
crate::backup::with_database(new, |db| {
let db = db.clone().for_realm(crate::realms::Mode::Coa);
for (schema, file) in &dumps {
progress(&format!("Importing {schema}"));
db.query(&format!(
"DROP DATABASE IF EXISTS `{schema}`; CREATE DATABASE `{schema}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
))?;
db.import_from(schema, file)?;
}
let (a, c) = (
count(&db, "SELECT COUNT(*) FROM acore_auth.account;"),
count(&db, "SELECT COUNT(*) FROM acore_characters.characters;"),
);
if (a, c) != (accounts, characters) {
return Err(Error::Invalid(format!(
"The copy is incomplete: {a} of {accounts} accounts and {c} of {characters} characters arrived."
)));
}
Ok(())
})
})?;
// 3. Settings: module configs and realm profiles; ports and console login as the old server had them
// (the console account lives in the copied accounts database).
progress("Copying settings");
let files = settings_files(old);
for rel in &files {
let from = fsx::ensure_within(old, &fsx::safe_join(old, rel)?)?;
let to = fsx::ensure_within(new, &fsx::safe_join(new, rel)?)?;
fsx::atomic_write(&to, &fs::read(from)?)?;
}
let old_repack: serde_json::Value = fsx::read_json(&old.join("Settings/repack.json"))?;
let mut repack: serde_json::Value = fsx::read_json(&new.join("Settings/repack.json"))?;
if let (Some(dst), Some(src)) = (repack.as_object_mut(), old_repack.as_object()) {
for (k, v) in src {
dst.insert(k.clone(), v.clone());
}
}
fsx::atomic_write_json(&new.join("Settings/repack.json"), &repack)?;
let _ = fs::remove_dir_all(&work);
progress("Copying world settings");
let (world_settings, world_settings_skipped) = carry_world_settings(old, new)?;
Ok(CopyReport {
schemas,
accounts,
characters,
config_files: files,
read_from: read_from.into(),
world_settings,
world_settings_skipped,
})
}
/// The world settings changed on the old server (they live in its `Settings/worldserver.conf.template`, which the new
/// server does not take: it keeps the template of its own version) are set on the new server through the same checks
/// the Server Configuration page uses. Settings the Manager owns (ports, folders, passwords) are not among them.
///
/// The shipped template already differs from `worldserver.conf.dist` in many places, so "changed" means "differs from
/// what the new server has now": for a copy onto the same version that is exactly what the owner changed. (A value the
/// owner never touched but a newer release ships differently cannot be told apart from an owner's change; it keeps the
/// old server's value, which is what that server ran with.)
fn carry_world_settings(old: &Path, new: &Path) -> Result<(Vec<String>, Vec<String>)> {
let current: std::collections::BTreeMap<String, String> =
crate::allsettings::list(new)?.into_iter().map(|i| (i.key, i.value)).collect();
let mut changes = std::collections::BTreeMap::new();
let mut skipped = Vec::new();
for item in crate::allsettings::list(old)? {
match current.get(&item.key) {
Some(now) if *now == item.value => {}
Some(_) => {
changes.insert(item.key, item.value);
}
None if item.changed => skipped.push(format!("{}: not a setting of the new version", item.key)),
None => {}
}
}
let meta = crate::registry::metadata_dir_for(new)?;
let mut carried = Vec::new();
match crate::allsettings::save(new, &meta, &changes) {
Ok(set) => carried.extend(set),
// One value the new version refuses must not lose the others: set them one at a time.
Err(_) => {
for (key, value) in changes {
match crate::allsettings::save(new, &meta, &std::collections::BTreeMap::from([(key.clone(), value)])) {
Ok(set) => carried.extend(set),
Err(e) => skipped.push(format!("{key}: {e}")),
}
}
}
}
Ok((carried, skipped))
}
#[cfg(test)]
mod tests {
use super::*;
const DIST: &str = "[worldserver]\r\n\r\n#\r\n# Rate.Drop.Item.Epic\r\n# Description: Epic drops.\r\n# Default: 1\r\n#\r\n\r\nRate.Drop.Item.Epic = 1\r\n\r\n#\r\n# LoginDatabaseInfo\r\n# Description: secret\r\n#\r\n\r\nLoginDatabaseInfo = \"127.0.0.1;3306;a;b;c\"\r\n\r\n#\r\n# Instance.ResetTimeHour\r\n# Description: Hour of the reset.\r\n# Default: 4\r\n#\r\n\r\nInstance.ResetTimeHour = 4\r\n";
fn world(root: &Path, dist: &str, conf: &str) {
fs::create_dir_all(root.join("Core/configs")).unwrap();
fs::create_dir_all(root.join("Settings")).unwrap();
fs::create_dir_all(crate::registry::metadata_dir_for(root).unwrap()).unwrap();
fs::write(root.join("Core/configs/worldserver.conf.dist"), dist).unwrap();
fs::write(root.join("Settings/worldserver.conf.template"), conf).unwrap();
fs::write(root.join("Core/configs/worldserver.conf"), conf).unwrap();
}
#[test]
fn changed_world_settings_go_to_the_new_server_and_the_rest_keep_its_defaults() {
let dir = tempfile::tempdir().unwrap();
let (old, new) = (dir.path().join("old"), dir.path().join("new"));
let old_dist = format!("{DIST}\r\n#\r\n# Old.Only\r\n# Description: gone in the new version.\r\n# Default: 1\r\n#\r\n\r\nOld.Only = 1\r\n");
world(&old, &old_dist, "[worldserver]\r\nRate.Drop.Item.Epic = 3\r\nOld.Only = 2\r\nLoginDatabaseInfo = \"old\"\r\n");
world(&new, DIST, "[worldserver]\r\nLoginDatabaseInfo = \"new\"\r\n");
let (carried, skipped) = carry_world_settings(&old, &new).unwrap();
assert_eq!(carried, ["Rate.Drop.Item.Epic"]);
assert_eq!(skipped.len(), 1, "{skipped:?}");
assert!(skipped[0].starts_with("Old.Only"));
for f in ["Settings/worldserver.conf.template", "Core/configs/worldserver.conf"] {
let text = fs::read_to_string(new.join(f)).unwrap();
assert!(text.contains("Rate.Drop.Item.Epic = 3"), "{f}: {text}");
assert!(text.contains("LoginDatabaseInfo = \"new\""), "the Manager's own settings stay: {text}");
assert!(!text.contains("Old.Only"), "{text}");
}
}
#[test]
fn the_preview_of_a_folder_reads_only_files() {
let dir = tempfile::tempdir().unwrap();
let classic = dir.path().join("classic");
fs::create_dir_all(classic.join("Core/configs/modules")).unwrap();
fs::create_dir_all(classic.join("mysql/data/mysql")).unwrap();
fs::create_dir_all(classic.join(".realms/secondary")).unwrap();
fs::write(classic.join("Core/worldserver.exe"), b"not a plugin host").unwrap();
fs::write(classic.join("Core/configs/modules/mod_x.conf"), b"x").unwrap();
fs::write(classic.join("mysql/data/ibdata1"), vec![0u8; 2048]).unwrap();
let p = preview(&classic).unwrap();
assert!(p.classic && !p.modular);
assert_eq!(p.database, "stopped");
assert_eq!((p.accounts, p.characters), (None, None), "a stopped database is not opened");
assert_eq!(p.database_bytes, 2048);
assert_eq!(p.config_files, ["Core/configs/modules/mod_x.conf"]);
assert_eq!(p.extra_worlds, ["secondary"]);
let empty = dir.path().join("empty");
fs::create_dir_all(&empty).unwrap();
let p = preview(&empty).unwrap();
assert!(!p.classic && !p.modular);
assert_eq!(p.database, "missing");
assert!(preview(&dir.path().join("nowhere")).is_err());
}
#[test]
fn only_owner_settings_are_copied() {
let dir = tempfile::tempdir().unwrap();
let old = dir.path();
for rel in [
"Core/configs/worldserver.conf",
"Core/configs/worldserver.conf.dist",
"Core/configs/modules/mod_x.conf",
"Core/configs/modules/mod_x.conf.dist",
"Core/configs/modules/mod_ascension_compat.conf",
"Settings/repack.json",
"Settings/worldserver.conf.template",
"Settings/realm-profile.json",
] {
let p = old.join(rel);
fs::create_dir_all(p.parent().unwrap()).unwrap();
fs::write(p, b"x").unwrap();
}
let mut got = settings_files(old);
got.sort();
assert_eq!(got, vec![
"Core/configs/modules/mod_x.conf".to_string(),
"Settings/realm-profile.json".to_string(),
]);
}
#[test]
fn a_folder_without_a_core_is_not_classic() {
let dir = tempfile::tempdir().unwrap();
assert!(!is_classic(dir.path()));
}
}
+38 -1
View File
@@ -61,10 +61,24 @@ pub fn fetch_pinned(owner: &str, repo: &str, sha: &str, cache_root: &Path) -> Re
Ok(dest) Ok(dest)
} }
/// Game-client and media files a server module's build never reads (client patches, DBC/map data, textures,
/// archives). Some repositories carry hundreds of MB of them next to a few KB of server code; they are left out
/// of the sources, and their names listed in `.coa_skipped_assets`.
fn is_client_asset(rel: &Path) -> bool {
const EXT: [&str; 22] = [
"mpq", "dbc", "db2", "adt", "wdt", "wdl", "wmo", "m2", "skin", "anim", "blp", "tga", "trs", "vmtile", "vmtree",
"mmap", "mmtile", "zip", "7z", "rar", "mp3", "ogg",
];
rel.extension()
.and_then(|e| e.to_str())
.is_some_and(|e| EXT.iter().any(|x| x.eq_ignore_ascii_case(e)))
}
/// Extracts a GitHub tar.gz (one top-level `<repo>-<sha>/` folder) into `dest`, files and folders only. /// Extracts a GitHub tar.gz (one top-level `<repo>-<sha>/` folder) into `dest`, files and folders only.
fn unpack(reader: impl Read, dest: &Path) -> Result<()> { pub(crate) fn unpack(reader: impl Read, dest: &Path) -> Result<()> {
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(reader)); let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(reader));
let mut total = 0u64; let mut total = 0u64;
let mut skipped = Vec::new();
for entry in archive.entries()? { for entry in archive.entries()? {
let mut entry = entry?; let mut entry = entry?;
let path = entry.path()?.into_owned(); let path = entry.path()?.into_owned();
@@ -79,6 +93,8 @@ fn unpack(reader: impl Read, dest: &Path) -> Result<()> {
let target = dest.join(&rel); let target = dest.join(&rel);
if kind.is_dir() { if kind.is_dir() {
fs::create_dir_all(&target)?; fs::create_dir_all(&target)?;
} else if kind.is_file() && is_client_asset(&rel) {
skipped.push(rel.to_string_lossy().replace('\\', "/"));
} else if kind.is_file() { } else if kind.is_file() {
total += entry.header().size()?; total += entry.header().size()?;
if total > MAX_ARCHIVE_BYTES { if total > MAX_ARCHIVE_BYTES {
@@ -90,6 +106,9 @@ fn unpack(reader: impl Read, dest: &Path) -> Result<()> {
} }
// Links and special entries are not part of a module's sources: skipped. // Links and special entries are not part of a module's sources: skipped.
} }
if !skipped.is_empty() {
fs::write(dest.join(".coa_skipped_assets"), skipped.join("\n"))?;
}
Ok(()) Ok(())
} }
@@ -118,6 +137,24 @@ mod tests {
assert!(dir.path().join("conf/x.conf.dist").is_file()); assert!(dir.path().join("conf/x.conf.dist").is_file());
} }
#[test]
fn client_assets_are_left_out_and_listed() {
let dir = tempfile::tempdir().unwrap();
let gz = tar_gz(&[
("mod-bg-abc/server-side/src/BG.cpp", b"int a;"),
("mod-bg-abc/client-side/DBFilesClient/Spell.dbc", b"dbc"),
("mod-bg-abc/client-data.zip", b"zip"),
("mod-bg-abc/data/sql/db-world/bg.sql", b"SELECT 1;"),
]);
unpack(&gz[..], dir.path()).unwrap();
assert!(dir.path().join("server-side/src/BG.cpp").is_file());
assert!(dir.path().join("data/sql/db-world/bg.sql").is_file());
assert!(!dir.path().join("client-side/DBFilesClient/Spell.dbc").exists());
assert!(!dir.path().join("client-data.zip").exists());
let listed = fs::read_to_string(dir.path().join(".coa_skipped_assets")).unwrap();
assert!(listed.contains("Spell.dbc") && listed.contains("client-data.zip"));
}
#[test] #[test]
fn bad_names_are_refused_before_any_download() { fn bad_names_are_refused_before_any_download() {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
+151 -4
View File
@@ -12,6 +12,8 @@ use crate::config::parser::ConfFile;
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::fsx; use crate::fsx;
pub mod pbc;
static CATALOG: &str = include_str!("../../../modules/catalog.json"); static CATALOG: &str = include_str!("../../../modules/catalog.json");
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@@ -48,6 +50,10 @@ pub struct Entry {
/// Not shown on the Modules page; its settings are offered elsewhere (a server setting card). /// Not shown on the Modules page; its settings are offered elsewhere (a server setting card).
#[serde(default)] #[serde(default)]
pub hidden: bool, pub hidden: bool,
/// A part of another module, switched and set up inside that module's settings (Custom Races V2 brings
/// subclasses and the /dev menu).
#[serde(default)]
pub part_of: Option<String>,
} }
fn yes() -> bool { fn yes() -> bool {
@@ -164,6 +170,10 @@ pub struct ModuleView {
pub component_type: ComponentType, pub component_type: ComponentType,
pub can_remove: bool, pub can_remove: bool,
pub source_commit: Option<String>, pub source_commit: Option<String>,
/// A newer build of this module is recommended by the server's core; updating is the owner's choice.
#[serde(default)]
pub available_commit: Option<String>,
pub part_of: Option<String>,
} }
pub fn list(root: &Path) -> Vec<ModuleView> { pub fn list(root: &Path) -> Vec<ModuleView> {
@@ -224,6 +234,32 @@ fn installed_module_commits(root: &Path) -> BTreeMap<String, String> {
Default::default() Default::default()
} }
/// The companion bots of this server build play Wildcard and Warcraft Reborn: their bot profiles for the standard
/// classes are in the world server or its bots plugin. Older builds only know Conquest of Azeroth characters.
pub fn companions_on_other_realms(root: &Path) -> bool {
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
static SEEN: OnceLock<Mutex<HashMap<(PathBuf, u64, Option<std::time::SystemTime>), bool>>> = OnceLock::new();
const MARKER: &[u8] = b"Druid_Guardian_Tank";
let mut files = vec![crate::layout::executables(root).0];
if let Ok(plugins) = fs::read_dir(root.join("Core/plugins")) {
files.extend(plugins.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x.eq_ignore_ascii_case("dll"))));
}
files.into_iter().any(|file| {
let Ok(meta) = fs::metadata(&file) else { return false };
let key = (file.clone(), meta.len(), meta.modified().ok());
let seen = SEEN.get_or_init(Mutex::default);
if let Some(found) = seen.lock().ok().and_then(|s| s.get(&key).copied()) {
return found;
}
let found = fs::read(&file).is_ok_and(|bytes| bytes.windows(MARKER.len()).any(|w| w == MARKER));
if let Ok(mut s) = seen.lock() {
s.insert(key, found);
}
found
})
}
fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> { fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
let installed_commits = installed_module_commits(root); let installed_commits = installed_module_commits(root);
let plugin_reg = crate::plugins::load_registry(root).ok(); let plugin_reg = crate::plugins::load_registry(root).ok();
@@ -400,7 +436,9 @@ fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
}; };
let dependency_missing = e.id == "dungeon-clear" && !is_squid_active; let dependency_missing = e.id == "dungeon-clear" && !is_squid_active;
let blocked = dependency_missing let blocked = dependency_missing
|| (wildcard && matches!(e.id.as_str(), "companions" | "playerbots" | "dungeon-clear")); || (wildcard
&& (matches!(e.id.as_str(), "playerbots" | "dungeon-clear")
|| (e.id == "companions" && !companions_on_other_realms(root))));
let version = if let Some(p) = plugin_entry { let version = if let Some(p) = plugin_entry {
Some(p.version.clone()) Some(p.version.clone())
} else if e.id == "playerbots" { } else if e.id == "playerbots" {
@@ -460,6 +498,8 @@ fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
component_type, component_type,
can_remove, can_remove,
source_commit, source_commit,
available_commit: None,
part_of: e.part_of,
} }
}) })
.collect(); .collect();
@@ -489,6 +529,10 @@ fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
views[idx].version = Some(plugin_entry.version.clone()); views[idx].version = Some(plugin_entry.version.clone());
views[idx].component_type = ComponentType::DynamicDll; views[idx].component_type = ComponentType::DynamicDll;
views[idx].can_remove = true; views[idx].can_remove = true;
if views[idx].source_commit.is_none() {
views[idx].source_commit = crate::build_engine::read_build_recipe(root, plugin_id).and_then(|r| r.source_commit);
}
views[idx].available_commit = crate::rebuild::available_update(root, plugin_id);
continue; continue;
} }
@@ -558,6 +602,8 @@ fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
component_type: ComponentType::DynamicDll, component_type: ComponentType::DynamicDll,
can_remove: true, can_remove: true,
source_commit, source_commit,
available_commit: None,
part_of: None,
}); });
} }
} }
@@ -711,6 +757,17 @@ pub fn ensure_bot_exclusivity(root: &Path) -> Result<()> {
Ok(()) Ok(())
} }
/// Dungeon Clear is part of SQUID's Playerbots (the plugin core builds it into the SQUID DLL): it goes on
/// with SQUID, and off with it, wherever its config exists.
fn switch_on_dungeon_clear_with_squid(cat: &[Entry], root: &Path, meta: &Path) {
if let Some(dc) = cat.iter().find(|entry| entry.id == "dungeon-clear") {
let name = conf_name(root, dc);
if dir(root).join(&name).exists() || dir(root).join(format!("{name}.dist")).exists() {
let _ = set_enabled_in(cat, root, meta, "dungeon-clear", true);
}
}
}
fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -> Result<()> { fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -> Result<()> {
match entry_in(cat, id) { match entry_in(cat, id) {
Ok(e) => { Ok(e) => {
@@ -770,6 +827,7 @@ fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -
} }
if id == "playerbots" && on { if id == "playerbots" && on {
let _ = crate::plugins::record_squid_migrations_applied(root); let _ = crate::plugins::record_squid_migrations_applied(root);
switch_on_dungeon_clear_with_squid(cat, root, meta);
} }
tracing::info!(module = id, on, "module switched"); tracing::info!(module = id, on, "module switched");
@@ -806,6 +864,9 @@ fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -
let _ = set_enabled_in(cat, root, meta, "dungeon-clear", false); let _ = set_enabled_in(cat, root, meta, "dungeon-clear", false);
} }
} }
if id == "playerbots" && on {
switch_on_dungeon_clear_with_squid(cat, root, meta);
}
return Ok(()); return Ok(());
} }
} }
@@ -869,6 +930,12 @@ fn settings_in(cat: &[Entry], root: &Path, id: &str) -> Result<Vec<Setting>> {
}; };
Ok(effective Ok(effective
.entries() .entries()
.filter(|(key, _)| {
id != "mod-pbc"
|| !["PBC.ApiKey", "PBC.AltModelApiKey"]
.iter()
.any(|secret| key.eq_ignore_ascii_case(secret))
})
.map(|(k, v)| { .map(|(k, v)| {
let mut val = fields let mut val = fields
.get(k) .get(k)
@@ -1082,6 +1149,63 @@ fn save_settings_in(
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn playerbot_characters_uses_the_packaged_default_and_keeps_host_settings() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("server");
let meta = temp.path().join("metadata");
let cat = catalog();
let id = "mod-pbc";
let card = || list_in(&cat, &root).into_iter().find(|m| m.id == id).unwrap();
fs::create_dir_all(dir(&root)).unwrap();
fs::create_dir_all(&meta).unwrap();
assert!(!card().installed);
let dist = dir(&root).join("playerbots_characters.conf.dist");
let defaults = "[worldserver]\nPBC.Enable = 0\nPBC.DefaultConnection = ../Extras/PlayerbotCharacters/my-connection.jsonc\n";
fs::write(&dist, defaults).unwrap();
let installed = card();
assert!(installed.installed && installed.switchable && installed.has_settings);
assert!(!installed.enabled);
let key = "PBC.DefaultConnection";
let path = "C:/Test Server/Model Config/custom.jsonc";
save_settings_in(&cat, &root, &meta, id, &BTreeMap::from([(key.into(), path.into())])).unwrap();
set_enabled_in(&cat, &root, &meta, id, true).unwrap();
assert!(card().enabled);
set_enabled_in(&cat, &root, &meta, id, false).unwrap();
assert!(!card().enabled);
let active = read(&dir(&root).join("playerbots_characters.conf")).unwrap();
assert_eq!(active.get(key), Some(path));
assert_eq!(fs::read_to_string(dist).unwrap(), defaults);
}
#[test]
fn playerbot_characters_preserves_legacy_connections_without_exposing_keys() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
fs::create_dir_all(dir(root)).unwrap();
let name = "playerbots_characters.conf";
fs::write(
dir(root).join(name),
"[worldserver]\nPBC.Enable = 1\nPBC.BaseUrl = https://example.test/v1\nPBC.Model = legacy\nPBC.ApiKey = fixture-key\nPBC.AltModelApiKey = fixture-alt-key\n",
).unwrap();
fs::write(
dir(root).join(format!("{name}.dist")),
"[worldserver]\nPBC.Enable = 0\nPBC.DefaultConnection = \"\"\n",
).unwrap();
crate::config::materialize_module_configs(root).unwrap();
let config = read(&dir(root).join(name)).unwrap();
assert_eq!(config.get("PBC.Enable"), Some("1"));
assert_eq!(config.get("PBC.DefaultConnection"), Some("\"\""));
assert_eq!(config.get("PBC.ApiKey"), Some("fixture-key"));
assert_eq!(config.get("PBC.AltModelApiKey"), Some("fixture-alt-key"));
let visible = serde_json::to_string(&settings(root, "mod-pbc").unwrap()).unwrap();
assert!(!visible.contains("fixture-key") && !visible.contains("fixture-alt-key"));
assert!(!pbc::load(root).unwrap().active);
}
#[test] #[test]
fn time_is_time_card_settings_and_backups_match_the_module() { fn time_is_time_card_settings_and_backups_match_the_module() {
let temp = tempfile::tempdir().unwrap(); let temp = tempfile::tempdir().unwrap();
@@ -1129,6 +1253,27 @@ mod tests {
assert!(meta.join("backups").exists()); assert!(meta.join("backups").exists());
} }
#[test]
fn companions_open_on_other_realms_only_with_a_build_that_plays_them() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("server");
fs::create_dir_all(dir(&root)).unwrap();
fs::create_dir_all(root.join("Settings")).unwrap();
fs::write(root.join("Settings/realm-profile.json"), r#"{"active":"wildcard","wildcard_created":true}"#).unwrap();
for e in catalog().iter().filter(|e| matches!(e.id.as_str(), "companions" | "playerbots")) {
fs::write(dir(&root).join(format!("{}.dist", e.conf)), format!("{} = 0\n", e.enable_key)).unwrap();
}
let exe = crate::layout::executables(&root).0;
fs::write(&exe, b"an old world server").unwrap();
let view = |id: &str| list(&root).into_iter().find(|m| m.id == id).unwrap();
assert_eq!(view("companions").compatibility, "unsupported");
fs::write(&exe, b"profiles ... Druid_Guardian_Tank ... a new world server").unwrap();
assert!(companions_on_other_realms(&root));
assert_eq!(view("companions").compatibility, "experimental");
assert!(view("companions").switchable);
assert_eq!(view("playerbots").compatibility, "unsupported", "SQUID stays CoA-only");
}
fn test_catalog() -> Vec<Entry> { fn test_catalog() -> Vec<Entry> {
let one = |id: &str, name: &str, conf: &str, key: &str| Entry { let one = |id: &str, name: &str, conf: &str, key: &str| Entry {
id: id.into(), id: id.into(),
@@ -1147,6 +1292,7 @@ mod tests {
icon: String::new(), icon: String::new(),
page: None, page: None,
hidden: false, hidden: false,
part_of: None,
}; };
vec![ vec![
one( one(
@@ -1255,7 +1401,7 @@ mod tests {
} }
#[test] #[test]
fn dungeon_clear_requires_squid_and_stays_off_after_squid_is_reenabled() { fn dungeon_clear_requires_squid_and_follows_it() {
let d = tempfile::tempdir().unwrap(); let d = tempfile::tempdir().unwrap();
let root = d.path(); let root = d.path();
fs::create_dir_all(dir(root)).unwrap(); fs::create_dir_all(dir(root)).unwrap();
@@ -1266,13 +1412,13 @@ mod tests {
let cat = catalog(); let cat = catalog();
assert!(set_enabled_in(&cat, root, root, "dungeon-clear", true).is_err()); assert!(set_enabled_in(&cat, root, root, "dungeon-clear", true).is_err());
assert!(!list_in(&cat, root).iter().find(|entry| entry.id == "dungeon-clear").unwrap().switchable); assert!(!list_in(&cat, root).iter().find(|entry| entry.id == "dungeon-clear").unwrap().switchable);
// part of SQUID: on with it, off with it, on again with it
set_enabled_in(&cat, root, root, "playerbots", true).unwrap(); set_enabled_in(&cat, root, root, "playerbots", true).unwrap();
set_enabled_in(&cat, root, root, "dungeon-clear", true).unwrap();
assert!(read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap()); assert!(read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
set_enabled_in(&cat, root, root, "playerbots", false).unwrap(); set_enabled_in(&cat, root, root, "playerbots", false).unwrap();
assert!(!read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap()); assert!(!read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
set_enabled_in(&cat, root, root, "playerbots", true).unwrap(); set_enabled_in(&cat, root, root, "playerbots", true).unwrap();
assert!(!read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap()); assert!(read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
} }
#[test] #[test]
@@ -1792,6 +1938,7 @@ mod tests {
icon: String::new(), icon: String::new(),
page: None, page: None,
hidden: false, hidden: false,
part_of: None,
}; };
let cat = vec![compat]; let cat = vec![compat];
let v = list_in(&cat, &root).remove(0); let v = list_in(&cat, &root).remove(0);
+292
View File
@@ -0,0 +1,292 @@
//! Owns the connection file created by the Manager. Handwritten connection files stay with their owners.
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use crate::config::parser::{ConfFile, unquote};
use crate::realms::{self, Mode};
use crate::{Error, Result, fsx};
pub mod models;
const MODULE: &str = "mod-pbc";
const PATH_SETTING: &str = "PBC.DefaultConnection";
pub(crate) const ASSET_DIR: &str = "Extras/PlayerbotCharacters";
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ApiType {
Openai,
Ollama,
Anthropic,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConnectionUpdate {
pub api_type: ApiType,
pub base_url: String,
pub model: String,
pub api_key: Option<String>,
pub remove_key: bool,
}
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct Connection {
api_type: ApiType,
base_url: String,
model: String,
api_key: String,
request_timeout_sec: u32,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConnectionView {
pub api_type: ApiType,
pub base_url: String,
pub model: String,
pub has_key: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Setup {
pub active: bool,
pub connection: Option<ConnectionView>,
}
fn managed_connection(root: &Path) -> Result<(PathBuf, String)> {
let file = match realms::state(root)?.active {
Mode::Coa => "manager-connection.jsonc",
Mode::Wildcard => "manager-connection-wildcard.jsonc",
Mode::Reborn => "manager-connection-reborn.jsonc",
};
let relative = format!("{ASSET_DIR}/{file}");
let path = fsx::ensure_within(root, &root.join(&relative))?;
Ok((path, format!("../{relative}")))
}
/// Secondary worlds use their own Core directory. Point their packaged paths back to the shared assets.
pub(crate) fn rebase_packaged_paths(config: &str, root: &Path) -> Result<String> {
let mut conf = ConfFile::parse_bytes(config.as_bytes())?;
let assets = root.join(ASSET_DIR);
for key in [
PATH_SETTING, "PBC.UtilityConnection", "PBC.CondensationConnection",
"PBC.RelationshipUpdateConnection", "PBC.PromptsPath", "PBC.CharacterCardsPath",
"PBC.HttpServerFrontendPath",
] {
let Some(relative) = conf.get(key).map(unquote)
.and_then(|path| path.strip_prefix("../Extras/PlayerbotCharacters/"))
else { continue };
let Ok(path) = fsx::safe_join(&assets, relative) else { continue };
let path = path.to_string_lossy().replace('\\', "/");
conf.set(key, &format!("\"{path}\""), &[]);
}
Ok(conf.to_text())
}
fn read_connection(path: &Path) -> Result<Option<Vec<u8>>> {
match fs::read(path) {
Ok(bytes) => Ok(Some(bytes)),
Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
Err(error) => Err(error.into()),
}
}
fn parse(bytes: &[u8]) -> Result<Connection> {
serde_json::from_slice(bytes).map_err(|_| Error::Invalid("The Manager model connection file is invalid.".into()))
}
fn view(connection: Connection) -> ConnectionView {
ConnectionView {
api_type: connection.api_type,
base_url: connection.base_url,
model: connection.model,
has_key: !connection.api_key.is_empty(),
}
}
fn module_setting(root: &Path) -> Result<String> {
super::settings(root, MODULE)?
.into_iter()
.find(|setting| setting.key == PATH_SETTING)
.map(|setting| setting.value)
.ok_or_else(|| Error::Invalid("This server package does not provide a model connection setting.".into()))
}
pub fn load(root: &Path) -> Result<Setup> {
let (path, setting) = managed_connection(root)?;
let active = unquote(&module_setting(root)?) == setting;
let connection = read_connection(&path)?
.as_deref()
.map(parse)
.transpose()?
.map(view);
Ok(Setup { active, connection })
}
fn connection_url(base_url: &str) -> Result<reqwest::Url> {
let url = reqwest::Url::parse(base_url.trim().trim_end_matches('/'))
.map_err(|_| Error::Invalid("Enter a valid model API base URL.".into()))?;
if !matches!(url.scheme(), "http" | "https") || url.host().is_none()
|| !url.username().is_empty() || url.password().is_some() || url.query().is_some() || url.fragment().is_some()
{
return Err(Error::Invalid("Use an HTTP or HTTPS base URL without credentials, a query, or a fragment.".into()));
}
Ok(url)
}
fn connection_key(
api_type: ApiType,
base_url: &str,
submitted_key: Option<&str>,
remove_key: bool,
previous: Option<Connection>,
) -> Result<String> {
let submitted_key = submitted_key.map(str::trim).filter(|value| !value.is_empty());
if submitted_key.is_some_and(|value| value.chars().any(char::is_control)) {
return Err(Error::Invalid("Enter the API key on a single line.".into()));
}
let key = if remove_key {
String::new()
} else if let Some(key) = submitted_key {
key.to_owned()
} else {
previous
.filter(|old| old.api_type == api_type && old.base_url == base_url)
.map(|old| old.api_key)
.unwrap_or_default()
};
Ok(key)
}
pub fn save(root: &Path, meta: &Path, update: ConnectionUpdate) -> Result<Setup> {
let (path, setting) = managed_connection(root)?;
let active = unquote(&module_setting(root)?) == setting;
let url = connection_url(&update.base_url)?;
let base_url = url.as_str().trim_end_matches('/');
let model = update.model.trim();
if model.is_empty() || model.chars().any(char::is_control) {
return Err(Error::Invalid("Enter a model name on a single line.".into()));
}
let previous = read_connection(&path)?;
let key = connection_key(
update.api_type, base_url, update.api_key.as_deref(), update.remove_key,
previous.as_deref().map(parse).transpose()?,
)?;
let connection = Connection {
api_type: update.api_type,
base_url: base_url.into(),
model: model.into(),
api_key: key,
request_timeout_sec: if matches!(update.api_type, ApiType::Ollama) { 60 } else { 30 },
};
fsx::atomic_write_private(&path, &serde_json::to_vec_pretty(&connection)?)?;
if !active {
let changes = BTreeMap::from([(PATH_SETTING.into(), setting)]);
if let Err(error) = super::save_settings(root, meta, MODULE, &changes) {
if let Some(bytes) = previous {
fsx::atomic_write_private(&path, &bytes)?;
} else {
fs::remove_file(&path)?;
}
return Err(error);
}
}
Ok(Setup { active: true, connection: Some(view(connection)) })
}
#[cfg(test)]
mod tests {
use super::*;
fn update(api_type: ApiType, base_url: &str, api_key: Option<&str>) -> ConnectionUpdate {
ConnectionUpdate { api_type, base_url: base_url.into(), model: "example-model".into(),
api_key: api_key.map(str::to_owned), remove_key: false }
}
#[test]
fn model_setup_preserves_manual_files_and_only_reuses_keys_for_the_same_endpoint() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
let meta = root.join("metadata");
let configs = root.join("Core/configs/modules");
fs::create_dir_all(&meta).unwrap();
fs::create_dir_all(&configs).unwrap();
fs::write(configs.join("playerbots_characters.conf.dist"),
"[worldserver]\nPBC.Enable = 0\nPBC.DefaultConnection = ../my-custom.jsonc\n").unwrap();
fs::write(root.join("my-custom.jsonc"), b"// manual connection").unwrap();
assert!(!load(root).unwrap().active);
let base_url = "https://api.example.com/v1";
let saved = save(root, &meta, update(ApiType::Openai, base_url, Some("fixture-key"))).unwrap();
assert!(saved.active && saved.connection.as_ref().unwrap().has_key);
assert!(!serde_json::to_string(&saved).unwrap().contains("fixture-key"));
assert!(save(root, &meta, update(ApiType::Openai, base_url, None)).unwrap().connection.unwrap().has_key);
let mut clear = update(ApiType::Openai, base_url, None);
clear.remove_key = true;
assert!(!save(root, &meta, clear).unwrap().connection.unwrap().has_key);
save(root, &meta, update(ApiType::Openai, base_url, Some("replacement-key"))).unwrap();
let changed = save(root, &meta, update(ApiType::Ollama, "http://127.0.0.1:11434", None)).unwrap();
assert!(!changed.connection.unwrap().has_key);
assert!(save(root, &meta, update(ApiType::Openai, "https://user:secret@example.com", None)).is_err());
assert_eq!(fs::read(root.join("my-custom.jsonc")).unwrap(), b"// manual connection");
let config = fs::read_to_string(configs.join("playerbots_characters.conf")).unwrap();
assert!(config.contains("PBC.Enable = 0"));
assert!(!config.contains("replacement-key"));
}
#[test]
fn secondary_world_uses_the_shared_pbc_assets_and_keeps_custom_paths() {
let temp = tempfile::tempdir().unwrap();
let config = "[worldserver]\nPBC.DefaultConnection = \"../Extras/PlayerbotCharacters/manager-connection.jsonc\"\nPBC.PromptsPath = \"../Extras/PlayerbotCharacters/prompts\"\nPBC.UtilityConnection = \"custom.jsonc\"\n";
let result = rebase_packaged_paths(config, temp.path()).unwrap();
let result = ConfFile::parse_bytes(result.as_bytes()).unwrap();
let expected = temp.path().join(ASSET_DIR).to_string_lossy().replace('\\', "/");
assert_eq!(result.get(PATH_SETTING).map(unquote), Some(format!("{expected}/manager-connection.jsonc").as_str()));
assert_eq!(result.get("PBC.PromptsPath").map(unquote), Some(format!("{expected}/prompts").as_str()));
assert_eq!(result.get("PBC.UtilityConnection").map(unquote), Some("custom.jsonc"));
}
#[test]
fn each_realm_saves_its_own_model_connection() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
let config = root.join("Core/configs/modules/playerbots_characters.conf");
let meta = root.join("metadata");
fsx::atomic_write(&config, b"[worldserver]\nPBC.Enable = 0\nPBC.DefaultConnection = \"\"\n").unwrap();
let (coa_path, _) = managed_connection(root).unwrap();
let endpoint = "https://example.test/v1";
save(root, &meta, update(ApiType::Openai, endpoint, Some("coa-key"))).unwrap();
let coa_config = fs::read(&config).unwrap();
let coa_connection = fs::read(&coa_path).unwrap();
let state = root.join("Settings/realm-profile.json");
let mut realm_state = realms::RealmState {
active: Mode::Wildcard,
wildcard_created: true,
..Default::default()
};
fsx::atomic_write_json(&state, &realm_state).unwrap();
let (wildcard_path, _) = managed_connection(root).unwrap();
assert_ne!(coa_path, wildcard_path);
assert!(load(root).unwrap().connection.is_none());
let mut wildcard = update(ApiType::Openai, endpoint, None);
wildcard.model = "wildcard-model".into();
let saved = save(root, &meta, wildcard).unwrap().connection.unwrap();
assert!(!saved.has_key);
assert_eq!(saved.model, "wildcard-model");
assert_eq!(fs::read(&coa_path).unwrap(), coa_connection);
realm_state.active = Mode::Coa;
fsx::atomic_write_json(&state, &realm_state).unwrap();
fsx::atomic_write(&config, &coa_config).unwrap();
let restored = load(root).unwrap();
assert!(restored.active && restored.connection.unwrap().has_key);
}
}
+320
View File
@@ -0,0 +1,320 @@
//! Read-only model discovery for the endpoint being edited in the Manager.
use reqwest::blocking::Client;
use reqwest::header::{self, HeaderMap, HeaderValue};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashSet};
use std::io::Read;
use std::path::Path;
use std::time::{Duration, Instant};
use crate::{Error, Result};
use super::{connection_key, connection_url, managed_connection, module_setting, parse, read_connection, ApiType};
const TIMEOUT: Duration = Duration::from_secs(10);
const MAX_PAGE_BYTES: u64 = 4 * 1024 * 1024;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
pub api_type: ApiType,
pub base_url: String,
pub api_key: Option<String>,
pub remove_key: bool,
}
#[derive(Serialize)]
pub struct Model {
pub id: String,
pub name: String,
}
#[derive(Deserialize)]
struct CatalogPage {
data: Vec<ApiModel>,
#[serde(default)]
has_more: bool,
last_id: Option<String>,
}
#[derive(Deserialize)]
struct ApiModel {
id: String,
display_name: Option<String>,
name: Option<String>,
lifecycle: Option<String>,
}
#[derive(Deserialize)]
struct OllamaList {
models: Vec<OllamaModel>,
}
#[derive(Deserialize)]
struct OllamaModel {
model: Option<String>,
name: Option<String>,
}
fn request_headers(api_type: ApiType, key: &str) -> Result<HeaderMap> {
let mut headers = HeaderMap::new();
if api_type == ApiType::Anthropic {
// Match the Claude API version and credential format used for dialogue in mod-pbc.
headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
}
if !key.is_empty() {
let (name, value) = if api_type == ApiType::Anthropic {
(header::HeaderName::from_static("x-api-key"), key.to_owned())
} else {
(header::AUTHORIZATION, format!("Bearer {key}"))
};
let mut value = HeaderValue::from_str(&value)
.map_err(|_| Error::Invalid("The API key contains unsupported characters.".into()))?;
value.set_sensitive(true);
headers.insert(name, value);
}
Ok(headers)
}
pub fn list(root: &Path, request: Request) -> Result<Vec<Model>> {
module_setting(root)?;
let mut url = connection_url(&request.base_url)?;
let (path, _) = managed_connection(root)?;
let previous = read_connection(&path)?;
let key = connection_key(
request.api_type, url.as_str().trim_end_matches('/'), request.api_key.as_deref(),
request.remove_key, previous.as_deref().map(parse).transpose()?,
)?;
let mut path = url.path_segments_mut()
.map_err(|_| Error::Invalid("Enter a valid model API base URL.".into()))?;
path.pop_if_empty();
if request.api_type == ApiType::Ollama {
path.extend(["api", "tags"]);
} else {
path.push("models");
}
drop(path);
let headers = request_headers(request.api_type, &key)?;
let client = Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.map_err(|_| Error::Invalid("Could not start the model list request.".into()))?;
let deadline = Instant::now() + TIMEOUT;
let mut models = BTreeMap::new();
let mut cursor = None::<String>;
let mut seen_cursors = HashSet::new();
loop {
let mut page_url = url.clone();
if let Some(cursor) = &cursor {
page_url.query_pairs_mut().append_pair("after_id", cursor);
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(Error::Invalid("The model list request timed out.".into()));
}
let response = client.get(page_url).headers(headers.clone()).timeout(remaining).send()
.map_err(|error| Error::Invalid(if error.is_timeout() {
"The model list request timed out."
} else {
"Could not reach the model endpoint."
}.into()))?;
let status = response.status();
if !status.is_success() {
return Err(Error::Invalid(format!("The provider returned HTTP {status} for its model list.")));
}
let mut body = Vec::new();
response.take(MAX_PAGE_BYTES + 1).read_to_end(&mut body)
.map_err(|_| Error::Invalid("Could not read the provider's model list.".into()))?;
if body.len() as u64 > MAX_PAGE_BYTES {
return Err(Error::Invalid("The provider returned an oversized model list.".into()));
}
let invalid_list = |_| Error::Invalid("The endpoint did not return a model list.".into());
if request.api_type == ApiType::Ollama {
let list: OllamaList = serde_json::from_slice(&body).map_err(invalid_list)?;
for model in list.models {
if let Some(id) = present(model.model).or_else(|| present(model.name)) {
models.insert(id.clone(), id);
}
}
break;
}
let page: CatalogPage = serde_json::from_slice(&body).map_err(invalid_list)?;
for model in page.data {
if model.lifecycle.as_deref() == Some("retired") || model.id.trim().is_empty() {
continue;
}
let name = present(model.display_name).or_else(|| present(model.name))
.unwrap_or_else(|| model.id.clone());
models.insert(model.id, name);
}
if !page.has_more {
break;
}
cursor = page.last_id.filter(|value| !value.is_empty() && seen_cursors.insert(value.clone()));
if cursor.is_none() {
return Err(Error::Invalid("The provider returned an invalid model list cursor.".into()));
}
}
let mut choices: Vec<_> = models.into_iter().map(|(id, name)| Model { id, name }).collect();
choices.sort_by_cached_key(|model| model.name.to_lowercase());
Ok(choices)
}
fn present(value: Option<String>) -> Option<String> {
value.filter(|text| !text.trim().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::modules::pbc::{self, ConnectionUpdate};
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;
use std::thread::{self, JoinHandle};
fn server(pages: Vec<(&'static str, &'static str, &'static str)>) -> (String, JoinHandle<Vec<String>>) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
listener.set_nonblocking(true).unwrap();
let worker = thread::spawn(move || {
let deadline = Instant::now() + Duration::from_secs(3);
pages.into_iter().map(|(status, headers, body)| {
let mut socket = loop {
match listener.accept() {
Ok((socket, _)) => break socket,
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock && Instant::now() < deadline => {
thread::sleep(Duration::from_millis(10));
}
Err(error) => panic!("Mock model endpoint did not receive its request: {error}"),
}
};
// Windows hands the listener's non-blocking mode to the accepted socket.
socket.set_nonblocking(false).unwrap();
socket.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
let mut request = String::new();
let mut reader = BufReader::new(&socket);
loop {
let mut line = String::new();
if reader.read_line(&mut line).unwrap() == 0 || line == "\r\n" {
break;
}
request.push_str(&line.to_ascii_lowercase());
}
let response = format!("HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n{headers}\r\n{body}", body.len());
socket.write_all(response.as_bytes()).unwrap();
// Closing too early resets the connection on Windows: end our side, then wait for the client.
socket.shutdown(std::net::Shutdown::Write).ok();
std::io::copy(&mut socket, &mut std::io::sink()).ok();
request
}).collect()
});
(format!("http://127.0.0.1:{port}"), worker)
}
fn install() -> tempfile::TempDir {
let root = tempfile::tempdir().unwrap();
let configs = root.path().join("Core/configs/modules");
fs::create_dir_all(&configs).unwrap();
fs::write(configs.join("playerbots_characters.conf.dist"),
"[worldserver]\nPBC.Enable = 0\nPBC.DefaultConnection = ../connection.jsonc\n").unwrap();
root
}
fn request(api_type: ApiType, base_url: &str, api_key: Option<&str>) -> Request {
Request { api_type, base_url: base_url.into(), api_key: api_key.map(str::to_owned), remove_key: false }
}
#[test]
fn openai_compatible_lists_current_models_and_reuses_the_matching_saved_key() {
let root = install();
let (endpoint, requests) = server(vec![
("200 OK", "", r#"{"data":[{"id":"model-a"}]}"#),
("200 OK", "", r#"{"data":[{"id":"model-b","name":"New Model"},{"id":"model-b","name":"New Model"}]}"#),
]);
let base = format!("{endpoint}/proxy/v1");
pbc::save(root.path(), &root.path().join("metadata"), ConnectionUpdate {
api_type: ApiType::Openai, base_url: base.clone(), model: "saved-model".into(),
api_key: Some("fixture-secret".into()), remove_key: false,
}).unwrap();
let (path, _) = managed_connection(root.path()).unwrap();
let saved = fs::read(&path).unwrap();
let models = list(root.path(), request(ApiType::Openai, &base, None)).unwrap();
assert_eq!(models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["model-a"]);
let refreshed = list(root.path(), request(ApiType::Openai, &base, Some("new-draft-key"))).unwrap();
assert_eq!(refreshed.len(), 1);
assert_eq!((&*refreshed[0].id, &*refreshed[0].name), ("model-b", "New Model"));
assert_eq!(fs::read(path).unwrap(), saved);
let requests = requests.join().unwrap();
assert!(requests.iter().all(|r| r.starts_with("get /proxy/v1/models ")));
assert!(requests[0].contains("authorization: bearer fixture-secret\r\n"));
assert!(requests[1].contains("authorization: bearer new-draft-key\r\n"));
}
#[test]
fn ollama_lists_installed_models_without_reusing_a_key_for_another_endpoint() {
let root = install();
pbc::save(root.path(), &root.path().join("metadata"), ConnectionUpdate {
api_type: ApiType::Ollama, base_url: "http://127.0.0.1:11434".into(), model: "saved-model".into(),
api_key: Some("fixture-secret".into()), remove_key: false,
}).unwrap();
let (endpoint, requests) = server(vec![
("200 OK", "", r#"{"models":[{"model":"qwen:7b","name":"alternate"},{"name":"gemma:4b"}]}"#),
]);
let models = list(root.path(), request(ApiType::Ollama, &endpoint, None)).unwrap();
assert_eq!(models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["gemma:4b", "qwen:7b"]);
let requests = requests.join().unwrap();
assert!(requests[0].starts_with("get /api/tags "));
assert!(!requests[0].contains("authorization:"));
}
#[test]
fn anthropic_follows_cursors_and_keeps_display_names_and_exact_ids() {
let root = install();
let (endpoint, requests) = server(vec![
("200 OK", "", r#"{"data":[{"id":"claude-sonnet-test","display_name":"Claude Sonnet Test"}],"has_more":true,"last_id":"opaque/page+1"}"#),
("200 OK", "", r#"{"data":[{"id":"claude-haiku-test","display_name":"Claude Haiku Test"},{"id":"retired-test","lifecycle":"retired"}],"has_more":false}"#),
]);
let models = list(root.path(), request(ApiType::Anthropic, &format!("{endpoint}/v1"), Some("fixture-key"))).unwrap();
assert_eq!(models.iter().map(|m| (&*m.id, &*m.name)).collect::<Vec<_>>(), [
("claude-haiku-test", "Claude Haiku Test"), ("claude-sonnet-test", "Claude Sonnet Test"),
]);
let requests = requests.join().unwrap();
assert!(requests[0].starts_with("get /v1/models "));
assert!(requests[1].starts_with("get /v1/models?after_id=opaque%2fpage%2b1 "));
for request in requests {
assert!(request.contains("anthropic-version: 2023-06-01\r\n"));
assert!(request.contains("x-api-key: fixture-key\r\n"));
}
}
#[test]
fn redirects_and_http_errors_keep_the_connection_file_and_do_not_expose_the_key() {
let root = install();
let (endpoint, requests) = server(vec![
("302 Found", "Location: http://127.0.0.1:1/models\r\n", ""),
("401 Unauthorized", "", "fixture-secret"),
]);
pbc::save(root.path(), &root.path().join("metadata"), ConnectionUpdate {
api_type: ApiType::Openai, base_url: endpoint.clone(), model: "saved-model".into(),
api_key: Some("fixture-secret".into()), remove_key: false,
}).unwrap();
let (path, _) = managed_connection(root.path()).unwrap();
let saved = fs::read(&path).unwrap();
let redirect = list(root.path(), request(ApiType::Openai, &endpoint, None)).err().unwrap();
assert!(redirect.to_string().contains("302"));
let mut no_auth = request(ApiType::Openai, &endpoint, None);
no_auth.remove_key = true;
let unauthorized = list(root.path(), no_auth).err().unwrap();
assert!(unauthorized.to_string().contains("401"));
assert!(!unauthorized.to_string().contains("fixture-secret"));
let requests = requests.join().unwrap();
assert!(requests[0].contains("authorization: bearer fixture-secret"));
assert!(!requests[1].contains("authorization:"));
assert_eq!(fs::read(path).unwrap(), saved);
}
}
+66 -18
View File
@@ -59,7 +59,7 @@ fn hide_window(cmd: &mut Command) {
let _ = cmd; let _ = cmd;
} }
const SCRIPT: &str = r#"param([string]$Dir, [string]$Url, [string]$InstallPath, [string]$Components) const SCRIPT: &str = r#"param([string]$Dir, [string]$Url, [string]$InstallPath, [string]$Components, [switch]$NewInstance)
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue' $ProgressPreference = 'SilentlyContinue'
New-Item -ItemType Directory -Force -Path $Dir | Out-Null New-Item -ItemType Directory -Force -Path $Dir | Out-Null
@@ -71,35 +71,67 @@ if ($sig.Status -ne 'Valid' -or $sig.SignerCertificate.Subject -notmatch 'O=Micr
"RESULT=bad-signature $($sig.Status)"; exit 0 "RESULT=bad-signature $($sig.Status)"; exit 0
} }
$a = @() $a = @()
if ($InstallPath) { $a += @('modify', '--installPath', ('"' + $InstallPath + '"')) } if ($InstallPath -and $NewInstance) { $a += @('--installPath', ('"' + $InstallPath + '"')) }
elseif ($InstallPath) { $a += @('modify', '--installPath', ('"' + $InstallPath + '"')) }
$a += @('--wait', '--norestart', '--nocache') $a += @('--wait', '--norestart', '--nocache')
foreach ($c in $Components.Split(',')) { $a += @('--add', $c) } foreach ($c in $Components.Split(',')) { $a += @('--add', $c) }
try { $p = Start-Process -FilePath $bt -ArgumentList $a -Wait -PassThru } catch { "RESULT=not-started $($_.Exception.Message)"; exit 0 } try { $p = Start-Process -FilePath $bt -ArgumentList $a -Wait -PassThru } catch { "RESULT=not-started $($_.Exception.Message)"; exit 0 }
"RESULT=exit $($p.ExitCode)" "RESULT=exit $($p.ExitCode)"
"#; "#;
/// Where the second, separate Build Tools instance goes when the existing one cannot supply the compiler.
fn separate_instance_dir() -> PathBuf {
let pf86 = std::env::var("ProgramFiles(x86)").unwrap_or_else(|_| "C:\\Program Files (x86)".into());
Path::new(&pf86).join("Microsoft Visual Studio").join("18").join("BuildTools-CoA")
}
/// Whether the installer finished but the compiler this core's SDK names is still missing. `modify --add` keeps
/// the toolset an existing instance already has, so an older instance stays on an older compiler build.
fn needs_separate_instance(modified_existing: bool, installed: bool, toolchain: &ToolchainStatus) -> bool {
modified_existing && installed && !toolchain.ready && toolchain.required_cl_version.is_some()
}
/// One run of Microsoft's installer: `(result line, stderr)`. With `new_instance` the instance is installed at
/// `install_path` as a separate one next to the existing Build Tools; otherwise `install_path` is modified.
fn run_installer(install_path: &str, new_instance: bool) -> Result<(String, String)> {
let dir = std::env::temp_dir().join("coa-msvc-setup");
std::fs::create_dir_all(&dir)?;
let script = dir.join("install-build-tools.ps1");
std::fs::write(&script, SCRIPT)?;
let mut cmd = Command::new("powershell.exe");
cmd.args(["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File"])
.arg(&script)
.args(["-Dir"]).arg(dir.join("bootstrapper"))
.args(["-Url", BOOTSTRAPPER_URL, "-InstallPath", install_path, "-Components", &COMPONENTS.join(",")]);
if new_instance {
cmd.arg("-NewInstance");
}
hide_window(&mut cmd);
let out = cmd.output()?;
let text = String::from_utf8_lossy(&out.stdout);
let result = text.lines().find_map(|l| l.trim().strip_prefix("RESULT=")).unwrap_or("").to_string();
Ok((result, String::from_utf8_lossy(&out.stderr).into_owned()))
}
/// Runs Microsoft's installer and reports whether plugins can now be built for `realm_root`'s core. /// Runs Microsoft's installer and reports whether plugins can now be built for `realm_root`'s core.
/// Blocks until the installer window is closed. /// Blocks until the installer window is closed.
pub fn install_build_tools(realm_root: Option<&Path>) -> Result<MsvcSetupOutcome> { pub fn install_build_tools(realm_root: Option<&Path>) -> Result<MsvcSetupOutcome> {
if !cfg!(windows) { if !cfg!(windows) {
return Err(Error::Invalid("The C++ Build Tools installer is only available on Windows.".into())); return Err(Error::Invalid("The C++ Build Tools installer is only available on Windows.".into()));
} }
let dir = std::env::temp_dir().join("coa-msvc-setup"); let existing = existing_build_tools().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
std::fs::create_dir_all(&dir)?; let (result, stderr) = run_installer(&existing, false)?;
let script = dir.join("install-build-tools.ps1"); let (mut installed, mut restart, mut message) = interpret(&result, &stderr);
std::fs::write(&script, SCRIPT)?; let mut toolchain = detect_toolchain_for(realm_root);
let install_path = existing_build_tools().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default(); if needs_separate_instance(!existing.is_empty(), installed, &toolchain) {
let mut cmd = Command::new("powershell.exe"); let target = separate_instance_dir().to_string_lossy().into_owned();
cmd.args(["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File"]) let (result, stderr) = run_installer(&target, true)?;
.arg(&script) let second = interpret(&result, &stderr);
.args(["-Dir"]).arg(dir.join("bootstrapper")) installed = second.0;
.args(["-Url", BOOTSTRAPPER_URL, "-InstallPath", &install_path, "-Components", &COMPONENTS.join(",")]); restart = restart || second.1;
hide_window(&mut cmd); message = second.2;
let out = cmd.output()?; toolchain = detect_toolchain_for(realm_root);
let text = String::from_utf8_lossy(&out.stdout); }
let result = text.lines().find_map(|l| l.trim().strip_prefix("RESULT=")).unwrap_or("").to_string();
let (installed, restart, message) = interpret(&result, &String::from_utf8_lossy(&out.stderr));
let toolchain = detect_toolchain_for(realm_root);
let ready = installed && toolchain.ready; let ready = installed && toolchain.ready;
let message = if installed && !toolchain.ready { let message = if installed && !toolchain.ready {
format!("The installer finished, but the compiler still does not match this core: {}", toolchain.diagnostic_message) format!("The installer finished, but the compiler still does not match this core: {}", toolchain.diagnostic_message)
@@ -138,6 +170,22 @@ mod tests {
assert!(interpret("", "boom").2.contains("boom")); assert!(interpret("", "boom").2.contains("boom"));
} }
fn status(ready: bool, required: Option<&str>) -> ToolchainStatus {
let mut t = detect_toolchain_for(None);
t.ready = ready;
t.required_cl_version = required.map(String::from);
t
}
#[test]
fn a_separate_instance_is_installed_only_when_the_existing_one_lacks_the_named_compiler() {
assert!(needs_separate_instance(true, true, &status(false, Some("19.44.35230"))));
assert!(!needs_separate_instance(true, true, &status(true, Some("19.44.35230"))));
assert!(!needs_separate_instance(false, true, &status(false, Some("19.44.35230"))));
assert!(!needs_separate_instance(true, false, &status(false, Some("19.44.35230"))));
assert!(!needs_separate_instance(true, true, &status(false, None)));
}
#[test] #[test]
fn the_components_name_the_pinned_toolset() { fn the_components_name_the_pinned_toolset() {
assert!(COMPONENTS.iter().any(|c| c.contains("VC.14.44"))); assert!(COMPONENTS.iter().any(|c| c.contains("VC.14.44")));
File diff suppressed because it is too large. Load diff
Loaded 100 of 170 files, more files were not shown because too many files have changed in this diff. Show more