mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-09-18 07:24:55 +08:00
补全资源拉取、解析、翻译、重打包和本地化发布命令,支持单次、限定次数与周期调度。移除 TUI 计划并通过 schedule.* RPC 暴露给 bat-api dashboard。 Closes #43
361 lines
13 KiB
Go
361 lines
13 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"bat-api/internal/backendrpc"
|
|
)
|
|
|
|
// Backend is the subset of daemon RPC used by bat-api.
|
|
//
|
|
// Call order for discovery (per plan review):
|
|
// 1. daemon.status
|
|
// 2. daemon.doctor
|
|
// 3. catalog.status / resource.manifest (and resource.state as needed)
|
|
type Backend interface {
|
|
DaemonStatus(ctx context.Context) (*backendrpc.DaemonStatusReport, error)
|
|
DaemonDoctor(ctx context.Context) (*backendrpc.DoctorReport, error)
|
|
ResourceState(ctx context.Context) (*backendrpc.ResourceState, error)
|
|
CatalogStatus(ctx context.Context) (json.RawMessage, error)
|
|
ResourceManifest(ctx context.Context, offset int, limit int) (*backendrpc.ResourceManifestPage, error)
|
|
}
|
|
|
|
// ControlBackend is the explicitly allowlisted mutation subset exposed through
|
|
// the authenticated bat-api admin control surface.
|
|
//
|
|
// It intentionally does not include daemon.stop, cleanup, or generic RPC calls.
|
|
// Restart is forwarded only to Rust's lifecycle RPC; Go never execs bat itself.
|
|
type ControlBackend interface {
|
|
DaemonRestart(ctx context.Context) (*backendrpc.Ack, error)
|
|
DaemonReload(ctx context.Context) (*backendrpc.Ack, error)
|
|
DaemonRefresh(ctx context.Context, force bool) (*backendrpc.Ack, error)
|
|
ResourceSync(ctx context.Context, force bool) (*backendrpc.TaskAccepted, error)
|
|
ResourceVerify(ctx context.Context) (*backendrpc.TaskAccepted, error)
|
|
ResourceRepair(ctx context.Context) (*backendrpc.TaskAccepted, error)
|
|
CatalogRefresh(ctx context.Context, force bool) (*backendrpc.TaskAccepted, error)
|
|
}
|
|
|
|
// ScheduleBackend exposes the Rust-owned schedule store to an authenticated
|
|
// dashboard. The JSON result remains Rust's report shape so the API does not
|
|
// duplicate schedule state or invent a second schema.
|
|
type ScheduleBackend interface {
|
|
ScheduleList(ctx context.Context) (json.RawMessage, error)
|
|
ScheduleAdd(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error)
|
|
ScheduleUpdate(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error)
|
|
ScheduleRemove(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error)
|
|
ScheduleRun(ctx context.Context, params backendrpc.ScheduleRunParams) (json.RawMessage, error)
|
|
}
|
|
|
|
// RPCClient adapts *backendrpc.Client to Backend.
|
|
type RPCClient struct {
|
|
Client *backendrpc.Client
|
|
}
|
|
|
|
func (r RPCClient) DaemonStatus(ctx context.Context) (*backendrpc.DaemonStatusReport, error) {
|
|
return r.Client.DaemonStatus(ctx)
|
|
}
|
|
func (r RPCClient) DaemonDoctor(ctx context.Context) (*backendrpc.DoctorReport, error) {
|
|
return r.Client.DaemonDoctor(ctx)
|
|
}
|
|
func (r RPCClient) ResourceState(ctx context.Context) (*backendrpc.ResourceState, error) {
|
|
return r.Client.ResourceState(ctx)
|
|
}
|
|
func (r RPCClient) CatalogStatus(ctx context.Context) (json.RawMessage, error) {
|
|
return r.Client.CatalogStatus(ctx)
|
|
}
|
|
func (r RPCClient) ResourceManifest(ctx context.Context, offset int, limit int) (*backendrpc.ResourceManifestPage, error) {
|
|
return r.Client.ResourceManifest(ctx, offset, limit)
|
|
}
|
|
func (r RPCClient) DaemonRestart(ctx context.Context) (*backendrpc.Ack, error) {
|
|
return r.Client.DaemonRestart(ctx)
|
|
}
|
|
func (r RPCClient) DaemonReload(ctx context.Context) (*backendrpc.Ack, error) {
|
|
return r.Client.DaemonReload(ctx)
|
|
}
|
|
func (r RPCClient) DaemonRefresh(ctx context.Context, force bool) (*backendrpc.Ack, error) {
|
|
return r.Client.DaemonRefresh(ctx, force)
|
|
}
|
|
func (r RPCClient) ResourceSync(ctx context.Context, force bool) (*backendrpc.TaskAccepted, error) {
|
|
return r.Client.ResourceSync(ctx, force)
|
|
}
|
|
func (r RPCClient) ResourceVerify(ctx context.Context) (*backendrpc.TaskAccepted, error) {
|
|
return r.Client.ResourceVerify(ctx)
|
|
}
|
|
func (r RPCClient) ResourceRepair(ctx context.Context) (*backendrpc.TaskAccepted, error) {
|
|
return r.Client.ResourceRepair(ctx)
|
|
}
|
|
func (r RPCClient) CatalogRefresh(ctx context.Context, force bool) (*backendrpc.TaskAccepted, error) {
|
|
return r.Client.CatalogRefresh(ctx, force)
|
|
}
|
|
func (r RPCClient) ScheduleList(ctx context.Context) (json.RawMessage, error) {
|
|
return r.Client.ScheduleList(ctx)
|
|
}
|
|
func (r RPCClient) ScheduleAdd(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error) {
|
|
return r.Client.ScheduleAdd(ctx, params)
|
|
}
|
|
func (r RPCClient) ScheduleUpdate(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error) {
|
|
return r.Client.ScheduleUpdate(ctx, params)
|
|
}
|
|
func (r RPCClient) ScheduleRemove(ctx context.Context, params backendrpc.ScheduleMutationParams) (json.RawMessage, error) {
|
|
return r.Client.ScheduleRemove(ctx, params)
|
|
}
|
|
func (r RPCClient) ScheduleRun(ctx context.Context, params backendrpc.ScheduleRunParams) (json.RawMessage, error) {
|
|
return r.Client.ScheduleRun(ctx, params)
|
|
}
|
|
func (r RPCClient) ParseStatus(ctx context.Context) (json.RawMessage, error) {
|
|
return r.Client.ParseStatus(ctx)
|
|
}
|
|
func (r RPCClient) ParseTextUnits(ctx context.Context, query backendrpc.TextUnitQueryParams) (json.RawMessage, error) {
|
|
return r.Client.ParseTextUnits(ctx, query)
|
|
}
|
|
func (r RPCClient) ParseErrors(ctx context.Context, query backendrpc.TextUnitQueryParams) (json.RawMessage, error) {
|
|
return r.Client.ParseErrors(ctx, query)
|
|
}
|
|
func (r RPCClient) LocalizedStatus(ctx context.Context) (json.RawMessage, error) {
|
|
return r.Client.LocalizedStatus(ctx)
|
|
}
|
|
func (r RPCClient) UnityFSPatchTextAsset(ctx context.Context, params backendrpc.UnityFSTextAssetPatchParams) (json.RawMessage, error) {
|
|
return r.Client.UnityFSPatchTextAsset(ctx, params)
|
|
}
|
|
func (r RPCClient) UnityFSPatchStringField(ctx context.Context, params backendrpc.UnityFSStringFieldPatchParams) (json.RawMessage, error) {
|
|
return r.Client.UnityFSPatchStringField(ctx, params)
|
|
}
|
|
func (r RPCClient) UnityFSPatchField(ctx context.Context, params backendrpc.UnityFSFieldPatchParams) (json.RawMessage, error) {
|
|
return r.Client.UnityFSPatchField(ctx, params)
|
|
}
|
|
|
|
// DiscoverResult is the outcome of talking to the bat daemon.
|
|
type DiscoverResult struct {
|
|
RPCAvailable bool
|
|
DoctorHealthy *bool
|
|
Status *backendrpc.DaemonStatusReport
|
|
Doctor *backendrpc.DoctorReport
|
|
Snapshot *SnapshotSummary
|
|
ResourceRoot string
|
|
Index *ReleaseIndex
|
|
Warnings []string
|
|
}
|
|
|
|
// DiscoverAndIndex contacts the daemon (status first, then doctor) and builds
|
|
// a release index from paginated resource.manifest plus on-disk checks.
|
|
//
|
|
// If resourceRootOverride is non-empty, it wins over RPC-reported roots after
|
|
// RPC health probes (still preferred for production to call status/doctor).
|
|
func DiscoverAndIndex(ctx context.Context, backend Backend, resourceRootOverride string) (*DiscoverResult, error) {
|
|
out := &DiscoverResult{}
|
|
if backend == nil {
|
|
if resourceRootOverride == "" {
|
|
out.Index = &ReleaseIndex{Source: "empty", byRel: map[string]int{}}
|
|
return out, nil
|
|
}
|
|
idx, err := LoadIndexFromResourceRoot(resourceRootOverride)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out.ResourceRoot = idx.ResourceRoot
|
|
out.Index = idx
|
|
return out, nil
|
|
}
|
|
|
|
// 1) daemon.status first
|
|
status, err := backend.DaemonStatus(ctx)
|
|
if err != nil {
|
|
out.Warnings = append(out.Warnings, fmt.Sprintf("daemon.status: %v", err))
|
|
if resourceRootOverride != "" {
|
|
idx, loadErr := LoadIndexFromResourceRoot(resourceRootOverride)
|
|
if loadErr != nil {
|
|
return out, fmt.Errorf("daemon.status failed (%v) and resource-root load failed: %w", err, loadErr)
|
|
}
|
|
out.ResourceRoot = idx.ResourceRoot
|
|
out.Index = idx
|
|
return out, nil
|
|
}
|
|
out.Index = &ReleaseIndex{Source: "empty", byRel: map[string]int{}}
|
|
return out, nil
|
|
}
|
|
out.RPCAvailable = true
|
|
out.Status = status
|
|
|
|
// 2) daemon.doctor second
|
|
doctor, err := backend.DaemonDoctor(ctx)
|
|
if err != nil {
|
|
out.Warnings = append(out.Warnings, fmt.Sprintf("daemon.doctor: %v", err))
|
|
} else {
|
|
out.Doctor = doctor
|
|
h := doctor.Healthy
|
|
out.DoctorHealthy = &h
|
|
}
|
|
|
|
// Catalog / resource discovery
|
|
var snapshot *SnapshotSummary
|
|
var resourceRoot string
|
|
|
|
if raw, err := backend.CatalogStatus(ctx); err != nil {
|
|
out.Warnings = append(out.Warnings, fmt.Sprintf("catalog.status: %v", err))
|
|
} else {
|
|
snap, root, ok := parseCatalogStatus(raw)
|
|
if ok {
|
|
snapshot = snap
|
|
resourceRoot = root
|
|
}
|
|
}
|
|
|
|
if resourceRoot == "" {
|
|
if state, err := backend.ResourceState(ctx); err != nil {
|
|
out.Warnings = append(out.Warnings, fmt.Sprintf("resource.state: %v", err))
|
|
} else if state.ResourceOutputRoot != nil && *state.ResourceOutputRoot != "" {
|
|
// Prefer published current under output root when catalog root missing.
|
|
candidate := filepath.Join(*state.ResourceOutputRoot, "current")
|
|
resourceRoot = candidate
|
|
}
|
|
}
|
|
|
|
if resourceRootOverride != "" {
|
|
resourceRoot = resourceRootOverride
|
|
}
|
|
if resourceRoot == "" {
|
|
out.Snapshot = snapshot
|
|
out.Index = &ReleaseIndex{
|
|
Source: "rpc",
|
|
RPCAvailable: true,
|
|
DoctorHealthy: out.DoctorHealthy,
|
|
Snapshot: snapshot,
|
|
byRel: map[string]int{},
|
|
}
|
|
out.Warnings = append(out.Warnings, "no resource root from RPC; set --resource-root or publish a version")
|
|
return out, nil
|
|
}
|
|
|
|
entries, manifestVersion, rootFromManifest, err := fetchAllManifestEntries(ctx, backend)
|
|
if err != nil {
|
|
out.Warnings = append(out.Warnings, fmt.Sprintf("resource.manifest: %v", err))
|
|
// Fallback: load local manifest file under root.
|
|
idx, loadErr := LoadIndexFromResourceRoot(resourceRoot)
|
|
if loadErr != nil {
|
|
return out, fmt.Errorf("manifest RPC and local load failed: rpc=%v local=%w", err, loadErr)
|
|
}
|
|
idx.Source = "rpc+local_manifest"
|
|
idx.RPCAvailable = true
|
|
idx.DoctorHealthy = out.DoctorHealthy
|
|
if snapshot != nil {
|
|
idx.Snapshot = snapshot
|
|
}
|
|
out.ResourceRoot = idx.ResourceRoot
|
|
out.Snapshot = idx.Snapshot
|
|
out.Index = idx
|
|
return out, nil
|
|
}
|
|
if rootFromManifest != "" {
|
|
resourceRoot = rootFromManifest
|
|
}
|
|
if resourceRootOverride != "" {
|
|
resourceRoot = resourceRootOverride
|
|
}
|
|
|
|
idx, err := BuildIndexFromManifestEntries(
|
|
resourceRoot,
|
|
"rpc",
|
|
true,
|
|
out.DoctorHealthy,
|
|
snapshot,
|
|
manifestVersion,
|
|
entries,
|
|
)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.ResourceRoot = idx.ResourceRoot
|
|
out.Snapshot = snapshot
|
|
out.Index = idx
|
|
return out, nil
|
|
}
|
|
|
|
func parseCatalogStatus(raw json.RawMessage) (*SnapshotSummary, string, bool) {
|
|
if len(raw) == 0 || string(raw) == "null" {
|
|
return nil, "", false
|
|
}
|
|
var payload struct {
|
|
Available bool `json:"available"`
|
|
Status string `json:"status"`
|
|
StatusCode string `json:"status_code"`
|
|
DistributionStatus string `json:"distribution_status"`
|
|
DistributionStatusCode string `json:"distribution_status_code"`
|
|
AppVersion string `json:"app_version"`
|
|
BundleVersion string `json:"bundle_version"`
|
|
ConnectionGroupName string `json:"connection_group_name"`
|
|
AddressablesRoot string `json:"addressables_root"`
|
|
LauncherMetadata *LauncherMetadataSummary `json:"launcher_metadata"`
|
|
GameMainConfigBootstrap *GameMainConfigSummary `json:"game_main_config_bootstrap"`
|
|
Version *struct {
|
|
ID string `json:"id"`
|
|
CompletedUnixSeconds *uint64 `json:"completed_unix_seconds"`
|
|
ResourceRoot string `json:"resource_root"`
|
|
} `json:"version"`
|
|
}
|
|
if err := json.Unmarshal(raw, &payload); err != nil || !payload.Available {
|
|
return nil, "", false
|
|
}
|
|
snap := &SnapshotSummary{
|
|
Status: payload.Status,
|
|
StatusCode: payload.StatusCode,
|
|
DistributionStatus: payload.DistributionStatus,
|
|
DistributionStatusCode: payload.DistributionStatusCode,
|
|
AppVersion: payload.AppVersion,
|
|
BundleVersion: payload.BundleVersion,
|
|
ConnectionGroupName: payload.ConnectionGroupName,
|
|
AddressablesRoot: payload.AddressablesRoot,
|
|
LauncherMetadata: payload.LauncherMetadata,
|
|
GameMainConfig: payload.GameMainConfigBootstrap,
|
|
}
|
|
root := ""
|
|
if payload.Version != nil {
|
|
snap.VersionID = payload.Version.ID
|
|
snap.CompletedUnixSeconds = payload.Version.CompletedUnixSeconds
|
|
root = payload.Version.ResourceRoot
|
|
}
|
|
return snap, root, true
|
|
}
|
|
|
|
func fetchAllManifestEntries(ctx context.Context, backend Backend) ([]manifestEntry, int, string, error) {
|
|
const pageSize = 500
|
|
offset := 0
|
|
var all []manifestEntry
|
|
var version int
|
|
var root string
|
|
for {
|
|
page, err := backend.ResourceManifest(ctx, offset, pageSize)
|
|
if err != nil {
|
|
return nil, 0, "", err
|
|
}
|
|
if !page.Available {
|
|
return nil, 0, "", fmt.Errorf("resource.manifest available=false")
|
|
}
|
|
if root == "" {
|
|
root = page.ResourceRoot
|
|
}
|
|
if version == 0 {
|
|
version = page.ManifestVersion
|
|
}
|
|
for _, e := range page.Entries {
|
|
var bytes uint64
|
|
if e.Bytes != nil {
|
|
bytes = *e.Bytes
|
|
}
|
|
all = append(all, manifestEntry{
|
|
URL: e.URL,
|
|
Destination: e.Destination,
|
|
Bytes: bytes,
|
|
BLAKE3: e.BLAKE3,
|
|
})
|
|
}
|
|
offset += len(page.Entries)
|
|
if len(page.Entries) == 0 || offset >= page.TotalEntries {
|
|
break
|
|
}
|
|
}
|
|
return all, version, root, nil
|
|
}
|