Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmake/packaging/sunshine.iss.in
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ Source: "{#MySourceDir}\tools\mat-capture\LICENSE.MinHook.txt"; DestDir: "{app}\
#endif
Source: "{#MySourceDir}\tools\nefconw.exe"; DestDir: "{app}\tools"; Flags: ignoreversion; Components: application
; The bundled WebP decoder's license and patent grant.
Source: "{#MySourceDir}\licenses\libwebp\LICENSE.txt"; DestDir: "{app}\licenses\libwebp"; Flags: ignoreversion; Components: application
Source: "{#MySourceDir}\licenses\libwebp\PATENTS.txt"; DestDir: "{app}\licenses\libwebp"; Flags: ignoreversion; Components: application
Source: "{#MySourceDir}\tools\library\libwebp\LICENSE.txt"; DestDir: "{app}\tools\library\libwebp"; Flags: ignoreversion; Components: application
Source: "{#MySourceDir}\tools\library\libwebp\PATENTS.txt"; DestDir: "{app}\tools\library\libwebp"; Flags: ignoreversion; Components: application
; the protocol launcher of the Sparxie touch applications (published from tools/sparxie-launch when the build has dotnet)
Source: "{#MySourceDir}\tools\sparxie-launch.exe"; DestDir: "{app}\tools"; Flags: ignoreversion skipifsourcedoesntexist; Components: application

Expand Down
4 changes: 2 additions & 2 deletions cmake/packaging/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ install(FILES "${CMAKE_SOURCE_DIR}/NOTICE"

install(TARGETS sunshine RUNTIME DESTINATION "." COMPONENT application)
install(FILES "${sunshine_webp_SOURCE_DIR}/COPYING"
DESTINATION "licenses/libwebp" RENAME "LICENSE.txt" COMPONENT application)
DESTINATION "tools/library/libwebp" RENAME "LICENSE.txt" COMPONENT application)
install(FILES "${sunshine_webp_SOURCE_DIR}/PATENTS"
DESTINATION "licenses/libwebp" RENAME "PATENTS.txt" COMPONENT application)
DESTINATION "tools/library/libwebp" RENAME "PATENTS.txt" COMPONENT application)
set(SUNSHINE_MAT_CAPTURE_AVAILABLE 0)
if (TARGET sunshine_mat_capture)
set(SUNSHINE_MAT_CAPTURE_AVAILABLE 1)
Expand Down
3 changes: 3 additions & 0 deletions cmake/sources/host.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ set(SUNSHINE_HOST_SOURCES
src/sparxie/svg_path.cpp src/sparxie/svg_path.h
src/sparxie/host.cpp src/sparxie/host.h
src/sparxie/host_refresh.cpp
src/sparxie/art_refresh_job.h
src/sparxie/art_path.h
src/sparxie/apps_file_lock.h
src/sparxie/platform.cpp src/sparxie/platform.h
src/sparxie/http_body_reader.h
src/sparxie/webp_decode_policy.h
Expand Down
43 changes: 35 additions & 8 deletions docs/sparxie-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,14 +884,41 @@ for `icon`). `v` is the version token so a new poster is a new URL.
updates that application's `image-path` to the new composed file in the
host covers directory, which is what a Moonlight client reads.

Refresh runs at host start, again after six hours plus up to 30 minutes of
jitter, and when the application list is opened if the last check was more
than 15 minutes ago. The list request is not delayed for the fetch. Offline,
or a failed fetch, serves the last cache. `art_unavailable` is only the case
where there has never been a cache.

`POST /api/sparxie/art/refresh` with `{ "install": "genshin-cn" }` forces
one check. An empty body checks every known install.
Host start and application-list visits request an asynchronous refresh; there
is no periodic timer. Existing artwork stays available while the worker checks
for updates. Each game/region records its last successful check in
`art-metadata/<install>.json`; successful automatic network checks are at least
24 hours apart, including after a host restart. A failed or interrupted check
can be retried on the next application-list visit. Manual checks bypass the
interval. Missing local outputs can still be rebuilt from cached
sources during the interval. Newly retained source images have SHA-256 and byte
size recorded in that metadata; existing poster indexes remain in place.

`POST /api/sparxie/art/refresh` uses the required `X-Sparxie-Art-Action` header:

- `check`: start an automatic check, respecting each install's interval.
- `force`: check immediately, bypassing the interval. If an automatic check is
already running, coalesce force requests into one forced pass after it; the
same job remains in `checking` until that pass finishes.
- `status`: send `{ "jobId": 1 }` to read that job without starting work or contacting sources.

Missing/unknown actions return 400 (`invalid_art_action`). Requests operate on
all known games/regions. While a job is running, further refresh requests return
its same `jobId`, `state: "checking"`, and `retryAfterMs: 2000`. On completion,
`state: "ready"` includes `updated`, `heroes`, and `skipped`. If any artwork
could not be refreshed, `state: "cached"` marks a completed job that kept the
available cache; `networkFailed` marks a failed network request, `skipped`
identifies games that could not be refreshed, and successful updates are still
reported. The client stops polling in either state. An unexpected job failure
returns `state: "failed"`, and shutdown returns `state: "stopped"`. The latest
16 completed results are retained so a new job does not hide an earlier result.
An unknown/evicted job returns `state: "expired"`. Status is process-local;
after a restart it starts as `idle`. All these status responses
use HTTP 200. Source requests have per-operation timeouts and a 60-second total
deadline checked between reads; cancellation is checked between operations, so
shutdown may still wait for a single in-progress WinHTTP operation to time out.
A page can poll `status` while checking and reread games/apps when
ready; polling must stop on page disposal and is bounded by the client.

`PUT /api/sparxie/apps/{index}/cover` with `{ "cover": "official" }` or
`"custom"` sets the flag. `custom` stops the sync from changing
Expand Down
3 changes: 3 additions & 0 deletions src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "image_enhancement/api.h"
#include "logging.h"
#include "network.h"
#include "sparxie/runtime.h"
#include <atomic>
#include <latch>
#include <optional>
Expand All @@ -36,6 +37,7 @@ namespace confighttp {

https_server_t server { config::nvhttp.cert, config::nvhttp.pkey };
register_routes(server);
::sparxie::note_host_started();
server.config.reuse_address = true;
server.config.address = net::get_bind_address(address_family);
server.config.port = port_https;
Expand Down Expand Up @@ -167,6 +169,7 @@ namespace confighttp {
local_auth_token::stop_monitor();
ai::http::shutdown_streams();
image_enhancement::api::shutdown();
::sparxie::stop_art_refresh();
server.stop();
if (loopback_server) {
loopback_server->stop();
Expand Down
12 changes: 10 additions & 2 deletions src/confighttp/apps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "src/platform/run_command.h"
#include "src/process.h"
#include "src/sparxie/runtime.h"
#include "src/sparxie/apps_file_lock.h"
#include "src/utility.h"
#include <atomic>
#include <chrono>
Expand Down Expand Up @@ -49,10 +50,13 @@ namespace confighttp::apps {
getApps(resp_https_t response, req_https_t request) {
if (!authenticate(response, request)) return;

sparxie::note_app_list_opened();
print_req(request);

std::string content = sparxie::typed_apps_text(file_handler::read_file(config::stream.file_apps.c_str()));
std::string content;
{
std::lock_guard apps_lock(::sparxie::apps_file_mutex);
content = sparxie::typed_apps_text(file_handler::read_file(config::stream.file_apps.c_str()));
}
web::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "application/json");
response->write(content, headers);
Expand All @@ -77,6 +81,7 @@ namespace confighttp::apps {
return;
}
auto writing_guard = util::fail_guard([]() { apps_writing = false; });
std::lock_guard apps_lock(::sparxie::apps_file_mutex);

std::stringstream ss;
ss << request->content.rdbuf();
Expand Down Expand Up @@ -202,6 +207,7 @@ namespace confighttp::apps {
return;
}
auto writing_guard = util::fail_guard([]() { apps_writing = false; });
std::lock_guard apps_lock(::sparxie::apps_file_mutex);

pt::ptree outputTree;
auto g = util::fail_guard([&]() {
Expand Down Expand Up @@ -292,6 +298,7 @@ namespace confighttp::apps {
return;
}
auto writing_guard = util::fail_guard([]() { apps_writing = false; });
std::lock_guard apps_lock(::sparxie::apps_file_mutex);

pt::ptree outputTree;
auto g = util::fail_guard([&]() {
Expand Down Expand Up @@ -823,6 +830,7 @@ namespace confighttp::apps {
return;
}
auto writing_guard = util::fail_guard([]() { apps_writing = false; });
std::lock_guard apps_lock(::sparxie::apps_file_mutex);
if (default_apps::retitle_file(config::stream.file_apps, locale)) {
proc::refresh(config::stream.file_apps);
}
Expand Down
56 changes: 49 additions & 7 deletions src/confighttp/sparxie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "src/logging.h"
#include "src/process.h"
#include "src/sparxie/install.h"
#include "src/sparxie/art_path.h"
#include "src/sparxie/apps_file_lock.h"
#include "src/sparxie/platform.h"
#include "src/sparxie/runtime.h"
#include <fstream>
Expand Down Expand Up @@ -39,7 +41,10 @@ namespace confighttp::sparxie {
// The host keeps the application list in memory: a request that rewrote apps.json makes it reload once, the way the plain application routes do, so clients see the change without a restart
void
reload_apps(const ::sparxie::HostEnv &env) {
if (env.apps_writes > 0) proc::refresh(config::stream.file_apps);
if (env.apps_writes > 0) {
std::lock_guard apps_lock(::sparxie::apps_file_mutex);
proc::refresh(config::stream.file_apps);
}
}
bool
ready(resp_https_t response, req_https_t request, bool with_body) {
Expand Down Expand Up @@ -223,9 +228,34 @@ namespace confighttp::sparxie {
void
post_refresh(resp_https_t response, req_https_t request) {
if (!ready(response, request, false)) return;
auto env = ::sparxie::make_host_env();
auto result = ::sparxie::refresh_official(env, ::sparxie::host_http_get, ::sparxie::decode_art_image, ::sparxie::host_http_post_json);
reload_apps(env);
std::string action;
const auto header = request->header.find("X-Sparxie-Art-Action");
if (header != request->header.end()) {
action = header->second;
}
::sparxie::ApiResult result;
if (action == "status") {
if (!check_content_type(response, request, "application/json")) {
return;
}
json body;
if (!read_json_body(response, request, body)) {
return;
}
const auto job = body.find("jobId");
if (job == body.end() || !job->is_number_integer() || *job <= 0) {
result.status = ::sparxie::Status::fail(400, "invalid_art_job", "A positive artwork jobId is required");
}
else {
result.body = ::sparxie::art_refresh_status(job->get<std::uint64_t>());
}
}
else if (action == "check" || action == "force") {
result.body = ::sparxie::request_art_refresh(action == "force");
}
else {
result.status = ::sparxie::Status::fail(400, "invalid_art_action", "Unknown artwork refresh action");
}
finish(response, std::move(result));
}
// The official art of an install. `badge` asks for the variant the host drew the Sparxie mark on (the poster a touch application hands to the clients): covers/sparxie-<install>-<version>-badge<rev>.png
Expand Down Expand Up @@ -273,10 +303,23 @@ namespace confighttp::sparxie {
const auto query = request->parse_query_string();
const auto version = query.find("v");
const auto badge = query.find("badge");
const std::string id = request->path_match[2];
const std::string art_version = version == query.end() ? std::string() : version->second;
if (!::sparxie::valid_art_install(id) || !::sparxie::valid_art_version(art_version)) {
write_json(response, web::StatusCode::not_found, json { { "ok", false }, { "error", "No cached image" }, { "error_code", "art_unavailable" } });
return;
}
auto env = ::sparxie::make_host_env();
const fs::path file = art_file(env, std::string(request->path_match[1]), std::string(request->path_match[2]), version == query.end() ? std::string() : version->second,
const std::string slot = request->path_match[1];
const fs::path file = art_file(env, slot, id, art_version,
badge != query.end() && badge->second == "1");
std::ifstream in(file, std::ios::binary);
const fs::path root = slot == "card" ? env.covers_dir : env.host_data / "posters";
fs::path resolved_file;
if (!::sparxie::art_file_within(root, file, &resolved_file)) {
write_json(response, web::StatusCode::not_found, json { { "ok", false }, { "error", "No cached image" }, { "error_code", "art_unavailable" } });
return;
}
std::ifstream in(resolved_file, std::ios::binary);
if (!in) {
json err;
err["ok"] = false;
Expand Down Expand Up @@ -395,6 +438,5 @@ namespace confighttp::sparxie {
// profiles that failed saves of an earlier build left in Sparxie's config go (its config is backed up first)
if (const int removed = ::sparxie::remove_orphan_profiles(env); removed > 0) BOOST_LOG(info) << "Sparxie: removed " << removed << " profile(s) that no application uses";
}
::sparxie::note_host_started();
}
} // namespace confighttp::sparxie
8 changes: 8 additions & 0 deletions src/sparxie/apps_file_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <mutex>

// Protect the complete read-modify-write cycle shared by Sparxie and the
// ordinary applications API. Recursive because route handlers call helpers.
namespace sparxie {
inline std::recursive_mutex apps_file_mutex;
}
24 changes: 5 additions & 19 deletions src/sparxie/art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,25 +886,11 @@ namespace sparxie {
return out;
}
bool
art_due(ArtSchedule &schedule, std::chrono::system_clock::time_point now, ArtReason reason) {
if (reason == ArtReason::manual) return true;
if (reason == ArtReason::host_start) {
if (schedule.host_started) return false;
schedule.host_started = true;
schedule.last_check = now;
schedule.last_fetch = now;
return true;
}
if (reason == ArtReason::app_list) {
if (schedule.last_check.time_since_epoch().count() != 0 && now - schedule.last_check < std::chrono::minutes(15)) return false;
schedule.last_check = now;
return true;
}
const auto gap = std::chrono::hours(6) + schedule.jitter;
if (schedule.last_fetch.time_since_epoch().count() != 0 && now - schedule.last_fetch < gap) return false;
schedule.last_fetch = now;
schedule.last_check = now;
return true;
art_check_due(std::chrono::system_clock::time_point last_attempt, std::chrono::system_clock::time_point now) {
constexpr auto check_interval = std::chrono::hours(24);
// A missing record or a clock rollback permits a new check. Otherwise,
// attempts (including failures) share the same per-install daily interval.
return last_attempt.time_since_epoch().count() == 0 || now < last_attempt || now - last_attempt >= check_interval;
}
ArtUrls
art_urls_from_payload(const json &games_payload, const json &basic_payload, const std::string &biz, bool prefer_carousel) {
Expand Down
12 changes: 1 addition & 11 deletions src/sparxie/art.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ namespace sparxie {
int h = 0;
std::vector<std::uint8_t> px;
};
struct ArtSchedule {
std::chrono::system_clock::time_point last_fetch {};
std::chrono::system_clock::time_point last_check {};
std::chrono::minutes jitter { 0 };
bool host_started = false;
};
enum class ArtReason { host_start,
interval,
app_list,
manual };
struct ArtUrls {
std::string icon;
std::string logo;
Expand Down Expand Up @@ -123,7 +113,7 @@ namespace sparxie {
Rgba
decode_image(const std::uint8_t *data, std::size_t size);
bool
art_due(ArtSchedule &schedule, std::chrono::system_clock::time_point now, ArtReason reason);
art_check_due(std::chrono::system_clock::time_point last_attempt, std::chrono::system_clock::time_point now);
ArtUrls
art_urls_from_payload(const json &games_payload, const json &basic_payload, const std::string &biz, bool prefer_carousel);
} // namespace sparxie
59 changes: 59 additions & 0 deletions src/sparxie/art_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include "src/sparxie/model.h"

namespace sparxie {
// Artwork belongs only to a known game and region, never to a filesystem path.
inline bool
valid_art_install(std::string_view id, std::string_view game, std::string_view region) {
bool known_game = false;
bool known_region = false;
for (const auto value : kGames) known_game |= value == game;
for (const auto value : kRegions) known_region |= value == region;
if (!known_game || !known_region) return false;
const std::string base = std::string(game) + "-" + std::string(region);
if (id == base) return true;
if (id.size() <= base.size() + 1 || id.size() > base.size() + 21 || id.substr(0, base.size()) != base || id[base.size()] != '-') return false;
for (const char ch : id.substr(base.size() + 1)) {
if (ch < '0' || ch > '9') return false;
}
return true;
}

inline bool
valid_art_install(std::string_view id) {
for (const auto game : kGames) {
for (const auto region : kRegions) {
if (valid_art_install(id, game, region)) return true;
}
}
return false;
}

inline bool
valid_art_version(std::string_view version) {
if (version == "." || version == "..") return false;
for (const unsigned char ch : version) {
if (ch < 32 || ch == 127 || ch == '/' || ch == '\\' || ch == ':' || ch == '*' || ch == '?' || ch == '"' || ch == '<' || ch == '>' || ch == '|') return false;
}
return true;
}

// Resolve symlinks/junctions as well as lexical traversal before serving a file.
inline bool
art_file_within(const fs::path &root, const fs::path &file, fs::path *resolved = nullptr) {
std::error_code ec;
const auto canonical_root = fs::canonical(root, ec);
if (ec) return false;
const auto canonical_file = fs::canonical(file, ec);
if (ec) return false;
auto parent = canonical_root.begin();
auto child = canonical_file.begin();
for (; parent != canonical_root.end(); ++parent, ++child) {
if (child == canonical_file.end() || *parent != *child) return false;
}
if (child == canonical_file.end()) return false;
if (resolved) *resolved = canonical_file;
return true;
}
} // namespace sparxie
Loading