/* * spmcat.h — scorpiox-scorpioplayer-media-categorizer * * SCORPIO PLAYER 2027 media-index walker: consumes a #26 spffa backend vtable, * filters/keys each media file, matches filenames to IMDb IDs through the * mfunc /api/lookup matcher, decides ADDED/UPDATED/UNCHANGED per file and * writes index records through an injected IMediaKeyVault-shaped vault vtable * (the "metadata vault" — FinderIMediaKeyVault in the legacy C#). * * The walker only: the 12 legacy *FolderFileAccesssor backends belong to other * repos. This engine sees only the vtable (#26 spffa, vendored verbatim). * * Safety model: spmcat_scan / spmcat_set_correction / spmcat_notify_scan_complete * REFUSE with SPMCAT_ERR_NOT_ARMED until spmcat_arm_writes(1). --test never arms. * * Threading: single-threaded except spmcat_cancel() (any thread). * Strings: returned const char * are library-owned; valid until the next * spmcat_* call that mutates the same state. Copy immediately. * All JSON is vendored yyjson. No credentials are ever hardcoded. */ #ifndef SPMCAT_H #define SPMCAT_H #include #ifdef __cplusplus extern "C" { #endif #if defined(SPMCAT_STATIC) # define SPMCAT_API #elif defined(_WIN32) # if defined(SPMCAT_BUILD) # define SPMCAT_API __declspec(dllexport) # else # define SPMCAT_API __declspec(dllimport) # endif #else # define SPMCAT_API __attribute__((visibility("default"))) #endif #define SPMCAT_VERSION "1.0.0" /* ---- error taxonomy (SPEC §10) — values are frozen ---- * ⭐ ERR_MATCHER_DOWN (-6) and ERR_NO_MATCH (-5) are DIFFERENT outcomes and must * never collapse (SPEC D1): a dead matcher must never silently index a whole * library as "unmatched" the way the C# catch { return null; } did. */ #define SPMCAT_OK 0 #define SPMCAT_ERR_TRANSPORT -1 /* matcher/vault/queue transport failure (io backend) */ #define SPMCAT_ERR_HTTP -2 /* completed, unexpected non-2xx on a required call */ #define SPMCAT_ERR_MATCHER_DOWN -3 /* matcher answered non-2xx (e.g. 500/404-hard) — scan halted */ #define SPMCAT_ERR_NO_MATCH -4 /* matcher answered: this name has no IMDb match */ #define SPMCAT_ERR_VAULT -5 /* vault backend returned failure */ #define SPMCAT_ERR_DEPTH_EXCEEDED -6 /* folder walk exceeded spmcat_set_max_depth */ #define SPMCAT_ERR_CYCLE -7 /* visited-set detected a loop (repeated folder key) */ #define SPMCAT_ERR_CANCELLED -8 /* spmcat_cancel() observed between nodes */ #define SPMCAT_ERR_NOT_ARMED -9 /* a mutating export was called without arming */ #define SPMCAT_ERR_NO_IO -10 /* no IO backend installed for the matcher */ #define SPMCAT_ERR_NO_BACKEND -11 /* no #26 backend installed for the walk */ #define SPMCAT_ERR_NO_VAULT -12 /* no vault installed */ #define SPMCAT_ERR_INVALID_ARG -13 #define SPMCAT_ERR_OOM -14 /* ---- per-file outcomes (SPMCAT_OUTCOME_*, SPEC §8) ---- */ #define SPMCAT_OUTCOME_ADDED 1 #define SPMCAT_OUTCOME_UPDATED 2 #define SPMCAT_OUTCOME_UNCHANGED 3 #define SPMCAT_OUTCOME_UNMATCHED 4 #define SPMCAT_OUTCOME_ERROR 5 /* ---- stats block (spmcat_stats) ---- */ typedef struct spmcat_stats { unsigned struct_size; /* sizeof(spmcat_stats_t) as the caller compiled it */ long long walked; /* backend entries visited (files + folders) */ long long considered; /* files passing the media predicate */ long long matched; /* got an IMDb id */ long long unmatched; /* matcher said "no match" */ long long added, updated, unchanged; long long errors; /* per-file ERROR outcomes */ int depth_max; /* deepest folder level reached */ long long vault_writes; /* SetAsync-equivalent calls actually issued */ long long matcher_calls; } spmcat_stats_t; /* ================= wiring (all injected; nothing compiled in) ================ */ /* IO backend for the matcher / queue HTTP calls (scorpiox_io v1, copied verbatim * from the fleet; see include/scorpiox_io.h). */ struct scorpiox_io; SPMCAT_API void spmcat_set_io(const struct scorpiox_io *io); SPMCAT_API int spmcat_use_curl(void); /* OK, or ERR_NO_IO in a no-curl build */ SPMCAT_API int spmcat_use_fake(void); /* in-repo fake matcher/vault/queue, offline */ /* #26 storage backend (vendored spffa.h — SPFFA_BUILD/SPFFA_STATIC rules apply). * The engine registers the backend in the spffa registry itself * (caller calls spffa_register + spffa_fake_* first, or installs a real one), * then points the walker at it by backend id + config. */ SPMCAT_API int spmcat_set_backend(const char *backend_id, const char *config_json); SPMCAT_API int spmcat_set_walk_root(const char *root_path); /* default "/" */ /* metadata vault (the IMediaKeyVault twin): injected vtable, never the network */ typedef struct spmcat_vault { unsigned struct_size; void *ctx; /* get: 0 = found (*out_value malloc'd, caller frees), 1 = not found, -1 = error */ int (*get)(void *ctx, const char *key, char **out_value); /* set: 0 = written, -1 = error */ int (*set)(void *ctx, const char *key, const char *value); /* search_keys: keys that start with `prefix`; fills *out_keys (malloc'd array of * malloc'd strings), *out_n. 0 = ok, -1 = error. */ int (*search_keys)(void *ctx, const char *prefix, char ***out_keys, size_t *out_n); void (*free_fn)(void *ctx, void *ptr); } spmcat_vault; SPMCAT_API int spmcat_set_vault(const spmcat_vault *v); SPMCAT_API void spmcat_set_logger(void (*cb)(const char *msg, void *ud), void *ud); /* matcher host: https only, REQUIRED, no default literal (SPEC D6). * Example: "https://mfunc.scorpioplayer.com" */ SPMCAT_API int spmcat_set_matcher_host(const char *host_url); /* queue credential for the completion notification: runtime value only (SPEC D7) */ SPMCAT_API int spmcat_set_queue_credential(const char *secret); /* ================= policy configuration ================= */ SPMCAT_API int spmcat_set_max_depth(int d); /* default 32 */ SPMCAT_API int spmcat_set_include_paths(const char *const *paths, int n); /* path-SEGMENT match */ SPMCAT_API int spmcat_set_exclude_paths(const char *const *paths, int n); /* symmetric */ SPMCAT_API int spmcat_set_video_extensions(const char *const *exts, int n);/* default: the 17 legacy VideoTypes */ SPMCAT_API int spmcat_set_add_new_only(int on); /* legacy AddNew */ SPMCAT_API int spmcat_set_correct_mode(int on); /* legacy RunCorrect */ /* ================= identity (delegates the stable hash) ================= * The legacy key hash (Core/System.cs:620 GetStringStableHashCode, UTF-16 walk, * Math.Abs fold) is owned by scorpiox-scorpioplayer-media-hash; the algorithm is * vendored here byte-exact so the .so/.dll stay self-contained (SPEC D4). */ SPMCAT_API int spmcat_derive_key(const char *path, long long size, char *out, size_t n); SPMCAT_API int spmcat_vault_key(const char *item_key, char *out, size_t n); /* MatchedIMDbId_ */ SPMCAT_API int spmcat_correction_key(const char *item_key, char *out, size_t n); /* CorrectIMDbId_ */ /* ================= the walk ================= */ /* One full scan. user_id goes into the vault key prefix (legacy * SCORPIOX_IMediaKeyVault__); runtime_config_id is stamped on records. * ☢️ Writes (vault set + completion notification) refuse unless armed. */ SPMCAT_API int spmcat_scan(const char *user_id, const char *runtime_config_id); SPMCAT_API void spmcat_cancel(void); /* any thread */ /* ================= results (a REAL result set — the C# returned null) ======== */ SPMCAT_API int spmcat_result_count(void); SPMCAT_API const char *spmcat_result_key(int i); SPMCAT_API const char *spmcat_result_path(int i); SPMCAT_API const char *spmcat_result_imdb_id(int i); /* "" when unmatched */ SPMCAT_API int spmcat_result_outcome(int i); /* SPMCAT_OUTCOME_* */ SPMCAT_API int spmcat_result_season(int i); /* -1 absent */ SPMCAT_API int spmcat_result_episode(int i); /* -1 absent */ SPMCAT_API int spmcat_unmatched_count(void); SPMCAT_API const char *spmcat_unmatched_name(int i); SPMCAT_API int spmcat_stats(spmcat_stats_t *out); /* out->struct_size in */ SPMCAT_API const char *spmcat_result_json(void); /* vendored yyjson */ /* ================= ☢️ mutating exports (REFUSE unless armed) ================ */ SPMCAT_API int spmcat_arm_writes(int armed); /* default 0 */ SPMCAT_API int spmcat_set_correction(const char *item_key, const char *imdb_id); SPMCAT_API int spmcat_notify_scan_complete(const char *user_id, const char *cfg_id); /* ================= diagnostics ================= */ SPMCAT_API int spmcat_last_code(void); SPMCAT_API const char *spmcat_last_error(void); SPMCAT_API long spmcat_last_http_code(void); SPMCAT_API const char *spmcat_last_url(void); /* never contains credentials */ SPMCAT_API int spmcat_warning_count(void); SPMCAT_API const char *spmcat_warning(int i); /* ================= fake matcher/vault (tests + .console --test; offline) ==== */ SPMCAT_API void sp_fake_reset(void); SPMCAT_API int sp_fake_set_matcher(const char *name, const char *imdb_id, int http_code); SPMCAT_API int sp_fake_fail_matcher(const char *err); /* transport-level failure */ SPMCAT_API int sp_fake_vault_seed(const char *key, const char *value); SPMCAT_API int sp_fake_vault_calls(int kind); /* 0=get 1=set 2=search */ SPMCAT_API int sp_fake_matcher_calls(void); /* the walk also touches the gcp/spimdb endpoints; the fake answers them offline */ SPMCAT_API int sp_fake_set_ismovie(const char *imdb_id, int is_movie); /* default: tt… = true */ SPMCAT_API int sp_fake_set_tvshow(const char *name, const char *imdb_id); /* isTVShow:true route */ SPMCAT_API int sp_fake_set_corrections(const char *json); /* default "{}" */ SPMCAT_API int sp_fake_set_indexed(const char *json); /* default "[]" */ SPMCAT_API int sp_fake_queue_calls(void); /* SendMessageQueue count */ SPMCAT_API void spmcat_shutdown(void); #ifdef __cplusplus } #endif #endif /* SPMCAT_H */ /* load a fake storage tree into the in-repo #26 fake backend (JSON: * {"name":"","children":[...]} with optional "generate":[{path,count,size}]). * Call AFTER spmcat_use_fake(); part of the offline fake surface (SPEC §2 fake). */ SPMCAT_API int spmcat_fake_load_tree(const char *json);