mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-09-30 14:04:55 -07:00
dad88caf01
## Link to GitHub Issue or related Pull Request, if one exists N/A ## Description of change I am developing [spice.nimabe.net](https://spice.nimabe.net), a static frontend for SpiceAPI. It already supports generating and inserting new card IDs, but SpiceAPI does not provide a way to discover cards already configured on the game machine. This makes the frontend inconvenient for existing users who generated and saved their card IDs locally. They currently have to open each card file on the game machine and manually copy its ID into the frontend. This change adds a `card.get_cards` API function that enumerates the current cards for readers supported by the running game. Each entry contains: - reader index - canonical 16-character card ID - `source`, set to `file` or `override` - `file_name` for file-backed cards File-backed cards expose only the save-file basename. Active `-card0` and `-card1` values are returned with `source: "override"` and no `file_name`, instead of representing an override as a file. spicefe derives the useful default import names `card0` and `card1` from the reader index. Reader enumeration uses `eamuse_get_game_keypads()` so games with only one reader do not expose a second card. This allows spicefe and other API clients to offer one-click import for existing cards, without requiring users to manually find and copy their card IDs. Because card IDs are sensitive, `card.get_cards` can only be used when the operator has configured an API password. No full filesystem path is exposed. Missing, unreadable, and invalid cards are omitted. The existing `card.insert` function remains unchanged. To avoid requiring a password for the whole card module, this change adds support for password requirements on individual API functions while preserving existing module-wide password behavior. Access to configured card paths and runtime card overrides is synchronized because API requests may read them concurrently with configuration updates. The Python and Dart API wrappers and README documentation have been updated for the new function and source metadata. ## Testing - Ran the complete `src/spice2x/build_docker.sh` build successfully. - Built all required 32-bit and 64-bit targets. - Built the Windows XP-compatible 32-bit targets. - Passed static import checks. - Passed Windows 7 and Windows XP compatibility checks. - Completed release packaging successfully. - Verified both updated Dart wrappers are correctly formatted. - Ran all 106 spicefe tests successfully. - `git diff --check` passes. *This implementation was prepared with assistance from OpenAI Codex.*
118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
#include "card.h"
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include "external/rapidjson/document.h"
|
|
#include "util/logging.h"
|
|
#include "util/utils.h"
|
|
#include "misc/eamuse.h"
|
|
|
|
using namespace std::placeholders;
|
|
using namespace rapidjson;
|
|
|
|
|
|
namespace api::modules {
|
|
|
|
static bool normalize_card_id(const std::string &value, std::string &card_id) {
|
|
if (value.size() != 16) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t card_bin[8] {};
|
|
if (!hex2bin(value.c_str(), card_bin)) {
|
|
return false;
|
|
}
|
|
|
|
card_id = bin2hex(card_bin, std::size(card_bin));
|
|
return true;
|
|
}
|
|
|
|
static bool read_card_id(const std::filesystem::path &path, std::string &card_id) {
|
|
std::ifstream file(path);
|
|
char buffer[16] {};
|
|
if (!file.read(buffer, std::size(buffer))) {
|
|
return false;
|
|
}
|
|
|
|
return normalize_card_id(std::string(buffer, std::size(buffer)), card_id);
|
|
}
|
|
|
|
Card::Card() : Module("card") {
|
|
functions["get_cards"] = std::bind(&Card::get_cards, this, _1, _2);
|
|
functions["insert"] = std::bind(&Card::insert, this, _1, _2);
|
|
require_password("get_cards");
|
|
}
|
|
|
|
/**
|
|
* get_cards()
|
|
*/
|
|
void Card::get_cards(Request &req, Response &res) {
|
|
auto &alloc = res.doc()->GetAllocator();
|
|
|
|
for (int index = 0; index < eamuse_get_game_keypads(); index++) {
|
|
std::string card_id;
|
|
std::string filename;
|
|
const auto card_override = eamuse_get_card_override(index);
|
|
const bool has_override = !card_override.empty();
|
|
|
|
if (has_override) {
|
|
if (!normalize_card_id(card_override, card_id)) {
|
|
continue;
|
|
}
|
|
} else {
|
|
const auto path = eamuse_get_card_path(index);
|
|
if (!read_card_id(path, card_id)) {
|
|
continue;
|
|
}
|
|
|
|
const auto filename_u8 = path.filename().u8string();
|
|
filename.assign(filename_u8.begin(), filename_u8.end());
|
|
}
|
|
|
|
Value card(kObjectType);
|
|
card.AddMember("index", index, alloc);
|
|
card.AddMember("card_id", Value(card_id.c_str(), alloc), alloc);
|
|
card.AddMember("source", Value(has_override ? "override" : "file", alloc), alloc);
|
|
if (!has_override) {
|
|
card.AddMember("file_name", Value(filename.c_str(), alloc), alloc);
|
|
}
|
|
res.add_data(card);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* insert(index, card_id)
|
|
* index: uint in range [0, 1]
|
|
* card_id: hex string of length 16
|
|
*/
|
|
void Card::insert(Request &req, Response &res) {
|
|
|
|
// check params
|
|
if (req.params.Size() < 2)
|
|
return error_params_insufficient(res);
|
|
if (!req.params[0].IsUint())
|
|
return error_type(res, "index", "uint");
|
|
if (!req.params[1].IsString())
|
|
return error_type(res, "card_id", "hex string");
|
|
if (req.params[1].GetStringLength() != 16)
|
|
return error_size(res, "card_id", 16);
|
|
|
|
// get params
|
|
auto index = req.params[0].GetUint();
|
|
auto card_hex = req.params[1].GetString();
|
|
|
|
// convert to binary
|
|
uint8_t card_bin[8] {};
|
|
if (!hex2bin(card_hex, card_bin)) {
|
|
return error_type(res, "card_id", "hex string");
|
|
}
|
|
|
|
// log
|
|
if (LOGGING) {
|
|
log_info("api::card", "inserting card: {}", card_hex);
|
|
}
|
|
|
|
// insert card
|
|
eamuse_card_insert(index & 1, card_bin);
|
|
}
|
|
}
|