10#include "absl/status/status.h"
17#include "nlohmann/json.hpp"
25using json = nlohmann::json;
38 std::string normalized = value;
40 normalized.begin(), normalized.end(), normalized.begin(),
41 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
42 return normalized ==
"1" || normalized ==
"true" || normalized ==
"yes" ||
49 res.set_header(
"Access-Control-Allow-Origin", kCorsAllowOrigin);
50 res.set_header(
"Access-Control-Allow-Headers", kCorsAllowHeaders);
51 res.set_header(
"Access-Control-Allow-Methods", kCorsAllowMethods);
52 res.set_header(
"Access-Control-Max-Age", kCorsMaxAge);
58 const char* window_state, httplib::Response& res) {
64 response[
"status"] =
"ok";
65 response[
"window"] = window_state;
67 res.set_content(response.dump(),
"application/json");
72 response[
"status"] =
"error";
73 response[
"message"] =
"window action failed";
74 res.set_content(response.dump(),
"application/json");
80 response[
"status"] =
"error";
81 response[
"message"] =
"window control unavailable";
82 res.set_content(response.dump(),
"application/json");
87void HandleHealth(
const httplib::Request& req, httplib::Response& res,
93 j[
"service"] =
"yaze-agent-api";
97 j[
"discovery"] =
"bonjour";
99 j[
"discovery"] =
"none";
103 res.set_content(j.dump(),
"application/json");
109 const bool force_refresh =
110 IsTruthyParam(req.get_param_value(
"refresh"), req.has_param(
"refresh"));
111 auto models_or = registry.ListAllModels(force_refresh);
115 if (!models_or.ok()) {
117 j[
"error"] = models_or.status().message();
119 res.set_content(j.dump(),
"application/json");
123 json j_models = json::array();
124 for (
const auto& info : *models_or) {
126 j_model[
"name"] = info.name;
127 j_model[
"display_name"] = info.display_name;
128 j_model[
"provider"] = info.provider;
129 j_model[
"description"] = info.description;
130 j_model[
"family"] = info.family;
131 j_model[
"parameter_size"] = info.parameter_size;
132 j_model[
"quantization"] = info.quantization;
133 j_model[
"size_bytes"] = info.size_bytes;
134 j_model[
"is_local"] = info.is_local;
135 j_models.push_back(j_model);
139 response[
"models"] = j_models;
140 response[
"count"] = j_models.size();
143 res.set_content(response.dump(),
"application/json");
150 std::string format_str = req.get_param_value(
"format");
151 if (format_str.empty())
152 format_str =
"mesen";
156 if (format_str ==
"mesen") {
158 }
else if (format_str ==
"asar") {
160 }
else if (format_str ==
"wla") {
162 }
else if (format_str ==
"bsnes") {
166 j[
"error"] =
"Unsupported symbol format";
167 j[
"format"] = format_str;
168 j[
"supported"] = {
"mesen",
"asar",
"wla",
"bsnes"};
170 res.set_content(j.dump(),
"application/json");
176 j[
"error"] =
"Symbol provider not available";
178 res.set_content(j.dump(),
"application/json");
183 if (export_or.ok()) {
184 const std::string accept = req.has_header(
"Accept")
185 ? req.get_header_value(
"Accept")
187 const bool wants_json =
188 accept.find(
"application/json") != std::string::npos;
191 j[
"symbols"] = *export_or;
192 j[
"format"] = format_str;
194 res.set_content(j.dump(),
"application/json");
197 res.set_content(*export_or,
"text/plain");
201 j[
"error"] = export_or.status().message();
203 res.set_content(j.dump(),
"application/json");
211 json body = json::parse(req.body);
212 uint32_t address = body.value(
"address", 0);
213 std::string source = body.value(
"source",
"unknown");
221 response[
"status"] =
"ok";
222 response[
"address"] = address;
223 response[
"message"] =
"Navigation request received";
225 res.set_content(response.dump(),
"application/json");
226 }
catch (
const std::exception& e) {
228 j[
"error"] = e.what();
230 res.set_content(j.dump(),
"application/json");
238 json body = json::parse(req.body);
239 uint32_t address = body.value(
"address", 0);
240 std::string source = body.value(
"source",
"unknown");
246 if (body.contains(
"cpu_state")) {
247 cpu_info = body[
"cpu_state"];
252 response[
"status"] =
"ok";
253 response[
"address"] = address;
255 res.set_content(response.dump(),
"application/json");
256 }
catch (
const std::exception& e) {
258 j[
"error"] = e.what();
260 res.set_content(j.dump(),
"application/json");
268 json body = json::parse(req.body);
275 response[
"status"] =
"ok";
277 res.set_content(response.dump(),
"application/json");
278 }
catch (
const std::exception& e) {
280 j[
"error"] = e.what();
282 res.set_content(j.dump(),
"application/json");
287 const std::function<
bool()>& action) {
289 HandleWindowAction(action,
"shown", res);
293 const std::function<
bool()>& action) {
295 HandleWindowAction(action,
"hidden", res);
314 if (!req.has_param(
"room")) {
316 j[
"error"] =
"Missing required parameter: room";
318 res.set_content(j.dump(),
"application/json");
321 const std::string s = req.get_param_value(
"room");
323 room_id = std::stoi(s,
nullptr, 0);
326 j[
"error"] =
"Invalid room parameter";
329 res.set_content(j.dump(),
"application/json");
341 if (!render_service) {
343 j[
"error"] =
"Render service not available";
345 res.set_content(j.dump(),
"application/json");
350 if (!ParseRoomParam(req, res, room_id))
356 if (req.has_param(
"overlays")) {
357 std::istringstream ss(req.get_param_value(
"overlays"));
359 while (std::getline(ss, tok,
',')) {
360 tok.erase(0, tok.find_first_not_of(
" \t"));
362 tok.erase(tok.find_last_not_of(
" \t") + 1);
363 if (tok ==
"collision")
365 else if (tok ==
"sprites")
367 else if (tok ==
"objects")
369 else if (tok ==
"track")
371 else if (tok ==
"camera")
373 else if (tok ==
"grid")
375 else if (tok ==
"tiletype")
377 else if (tok ==
"all")
384 if (req.has_param(
"scale")) {
385 const auto scale_or =
387 if (!scale_or.ok()) {
389 j[
"error"] = std::string(scale_or.status().message());
391 res.set_content(j.dump(),
"application/json");
400 render_req.
scale = scale;
403 if (!result_or.ok()) {
404 const auto& s = result_or.status();
406 j[
"error"] = std::string(s.message());
407 if (s.code() == absl::StatusCode::kInvalidArgument)
409 else if (s.code() == absl::StatusCode::kFailedPrecondition)
413 res.set_content(j.dump(),
"application/json");
417 const auto& result = *result_or;
419 res.set_content(
reinterpret_cast<const char*
>(result.png_data.data()),
420 result.png_data.size(),
"image/png");
421 res.set_header(
"X-Room-Id", std::to_string(room_id));
422 res.set_header(
"X-Room-Width", std::to_string(result.width));
423 res.set_header(
"X-Room-Height", std::to_string(result.height));
427 const httplib::Request& req, httplib::Response& res,
431 if (!render_service) {
433 j[
"error"] =
"Render service not available";
435 res.set_content(j.dump(),
"application/json");
440 if (!ParseRoomParam(req, res, room_id))
445 const auto& s = meta_or.status();
447 j[
"error"] = std::string(s.message());
448 res.status = (s.code() == absl::StatusCode::kInvalidArgument) ? 400 : 500;
449 res.set_content(j.dump(),
"application/json");
453 const auto& meta = *meta_or;
455 j[
"room_id"] = meta.room_id;
456 j[
"blockset"] = meta.blockset;
457 j[
"spriteset"] = meta.spriteset;
458 j[
"palette"] = meta.palette;
459 j[
"layout_id"] = meta.layout_id;
460 j[
"effect"] = meta.effect;
461 j[
"collision"] = meta.collision;
462 j[
"tag1"] = meta.tag1;
463 j[
"tag2"] = meta.tag2;
464 j[
"message_id"] = meta.message_id;
465 j[
"has_custom_collision"] = meta.has_custom_collision;
466 j[
"object_count"] = meta.object_count;
467 j[
"sprite_count"] = meta.sprite_count;
469 res.set_content(j.dump(),
"application/json");
481 json body = json::parse(req.body);
482 std::string command_name = body.value(
"command",
"");
483 if (command_name.empty()) {
485 j[
"error"] =
"Missing 'command' field";
487 res.set_content(j.dump(),
"application/json");
491 std::vector<std::string> args;
492 if (body.contains(
"args") && body[
"args"].is_array()) {
493 for (
const auto& arg : body[
"args"]) {
494 args.push_back(arg.get<std::string>());
499 if (!registry.HasCommand(command_name)) {
501 j[
"error"] =
"Unknown command: " + command_name;
503 res.set_content(j.dump(),
"application/json");
507 std::string captured_output;
508 auto status = registry.Execute(command_name, args, rom, &captured_output);
512 response[
"status"] =
"ok";
513 response[
"command"] = command_name;
516 response[
"result"] = json::parse(captured_output);
518 response[
"result"] = captured_output;
522 response[
"status"] =
"error";
523 response[
"command"] = command_name;
524 response[
"error"] = std::string(status.message());
525 if (!captured_output.empty()) {
526 response[
"output"] = captured_output;
530 res.set_content(response.dump(),
"application/json");
531 }
catch (
const std::exception& e) {
533 j[
"error"] = e.what();
535 res.set_content(j.dump(),
"application/json");
544 json j_commands = json::array();
546 for (
const auto& category : registry.GetCategories()) {
547 for (
const auto& cmd_name : registry.GetCommandsInCategory(category)) {
548 const auto* meta = registry.GetMetadata(cmd_name);
553 j_cmd[
"name"] = meta->name;
554 j_cmd[
"category"] = meta->category;
555 j_cmd[
"description"] = meta->description;
556 j_cmd[
"usage"] = meta->usage;
557 j_cmd[
"requires_rom"] = meta->requires_rom;
558 j_commands.push_back(j_cmd);
563 response[
"commands"] = j_commands;
564 response[
"count"] = j_commands.size();
566 res.set_content(response.dump(),
"application/json");
576 if (project_path.empty())
579 return project_path +
"/Docs/Dev/Planning/annotations.json";
584 return json::object();
585 std::ifstream file(path);
587 return json::object();
589 return json::parse(file);
591 return json::object();
599 const std::filesystem::path parent =
600 std::filesystem::path(path).parent_path();
601 if (!parent.empty()) {
602 std::error_code error;
603 std::filesystem::create_directories(parent, error);
609 std::ofstream file(path);
612 file << data.dump(2);
619 const std::string& project_path) {
622 std::string path = ResolveAnnotationsPath(project_path);
625 j[
"error"] =
"No project path configured";
627 res.set_content(j.dump(),
"application/json");
631 json file_data = LoadAnnotationsFile(path);
632 json annotations = file_data.value(
"annotations", json::array());
635 if (req.has_param(
"room")) {
638 room_id = std::stoi(req.get_param_value(
"room"),
nullptr, 0);
641 j[
"error"] =
"Invalid room parameter";
643 res.set_content(j.dump(),
"application/json");
647 json filtered = json::array();
648 for (
const auto& ann : annotations) {
649 if (ann.value(
"room_id", -1) == room_id) {
650 filtered.push_back(ann);
653 annotations = filtered;
657 response[
"annotations"] = annotations;
659 res.set_content(response.dump(),
"application/json");
663 const std::string& project_path) {
666 std::string path = ResolveAnnotationsPath(project_path);
669 j[
"error"] =
"No project path configured";
671 res.set_content(j.dump(),
"application/json");
676 json new_annotation = json::parse(req.body);
677 json file_data = LoadAnnotationsFile(path);
679 if (!file_data.contains(
"annotations")) {
680 file_data[
"annotations"] = json::array();
682 file_data[
"annotations"].push_back(new_annotation);
684 if (SaveAnnotationsFile(path, file_data)) {
686 response[
"status"] =
"ok";
687 response[
"id"] = new_annotation.value(
"id",
"");
689 res.set_content(response.dump(),
"application/json");
692 j[
"error"] =
"Failed to write annotations file";
694 res.set_content(j.dump(),
"application/json");
696 }
catch (
const std::exception& e) {
698 j[
"error"] = e.what();
700 res.set_content(j.dump(),
"application/json");
705 const std::string& project_path) {
708 std::string path = ResolveAnnotationsPath(project_path);
711 j[
"error"] =
"No project path configured";
713 res.set_content(j.dump(),
"application/json");
718 std::string annotation_id;
719 if (!req.matches.empty() && req.matches.size() > 1) {
720 annotation_id = req.matches[1].str();
722 if (annotation_id.empty()) {
724 j[
"error"] =
"Missing annotation ID in URL";
726 res.set_content(j.dump(),
"application/json");
731 json updated = json::parse(req.body);
732 json file_data = LoadAnnotationsFile(path);
733 json& annotations = file_data[
"annotations"];
736 for (
auto& ann : annotations) {
737 if (ann.value(
"id",
"") == annotation_id) {
739 for (
auto it = updated.begin(); it != updated.end(); ++it) {
740 ann[it.key()] = it.value();
749 j[
"error"] =
"Annotation not found: " + annotation_id;
751 res.set_content(j.dump(),
"application/json");
755 if (SaveAnnotationsFile(path, file_data)) {
757 response[
"status"] =
"ok";
758 response[
"id"] = annotation_id;
760 res.set_content(response.dump(),
"application/json");
763 j[
"error"] =
"Failed to write annotations file";
765 res.set_content(j.dump(),
"application/json");
767 }
catch (
const std::exception& e) {
769 j[
"error"] = e.what();
771 res.set_content(j.dump(),
"application/json");
776 const std::string& project_path) {
780 std::string path = ResolveAnnotationsPath(project_path);
783 j[
"error"] =
"No project path configured";
785 res.set_content(j.dump(),
"application/json");
789 std::string annotation_id;
790 if (!req.matches.empty() && req.matches.size() > 1) {
791 annotation_id = req.matches[1].str();
793 if (annotation_id.empty()) {
795 j[
"error"] =
"Missing annotation ID in URL";
797 res.set_content(j.dump(),
"application/json");
801 json file_data = LoadAnnotationsFile(path);
802 json& annotations = file_data[
"annotations"];
804 size_t original_size = annotations.size();
805 json filtered = json::array();
806 for (
const auto& ann : annotations) {
807 if (ann.value(
"id",
"") != annotation_id) {
808 filtered.push_back(ann);
812 if (filtered.size() == original_size) {
814 j[
"error"] =
"Annotation not found: " + annotation_id;
816 res.set_content(j.dump(),
"application/json");
820 file_data[
"annotations"] = filtered;
821 if (SaveAnnotationsFile(path, file_data)) {
823 response[
"status"] =
"ok";
824 response[
"id"] = annotation_id;
826 res.set_content(response.dump(),
"application/json");
829 j[
"error"] =
"Failed to write annotations file";
831 res.set_content(j.dump(),
"application/json");
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::StatusOr< RoomMetadata > GetDungeonRoomMetadata(int room_id)
absl::StatusOr< RenderResult > RenderDungeonRoom(const RenderRequest &req)
static CommandRegistry & Instance()
static ModelRegistry & GetInstance()
Provider for symbol (label) resolution in disassembly.
absl::StatusOr< std::string > ExportSymbols(SymbolFormat format) const
Export all symbols to a string in the specified format.
constexpr uint32_t kTileTypes
constexpr uint32_t kCollision
constexpr uint32_t kSprites
constexpr uint32_t kCameraQuads
constexpr uint32_t kObjects
constexpr uint32_t kTrack
absl::StatusOr< float > ParseRenderScale(absl::string_view value)
constexpr const char * kCorsMaxAge
constexpr const char * kCorsAllowOrigin
bool IsTruthyParam(const std::string &value, bool param_present)
bool ParseRoomParam(const httplib::Request &req, httplib::Response &res, int &room_id)
void HandleWindowAction(const std::function< bool()> &action, const char *window_state, httplib::Response &res)
bool SaveAnnotationsFile(const std::string &path, const json &data)
std::string ResolveAnnotationsPath(const std::string &project_path)
constexpr const char * kCorsAllowHeaders
constexpr const char * kCorsAllowMethods
json LoadAnnotationsFile(const std::string &path)
void HandleStateUpdate(const httplib::Request &req, httplib::Response &res)
void ApplyCorsHeaders(httplib::Response &res)
void HandleCorsPreflight(const httplib::Request &req, httplib::Response &res)
void HandleBreakpointHit(const httplib::Request &req, httplib::Response &res)
void HandleListModels(const httplib::Request &req, httplib::Response &res)
void HandleGetSymbols(const httplib::Request &req, httplib::Response &res, yaze::emu::debug::SymbolProvider *symbols)
void HandleNavigate(const httplib::Request &req, httplib::Response &res)
void HandleWindowShow(const httplib::Request &req, httplib::Response &res, const std::function< bool()> &action)
void HandleAnnotationUpdate(const httplib::Request &req, httplib::Response &res, const std::string &project_path)
void HandleRenderDungeonMetadata(const httplib::Request &req, httplib::Response &res, yaze::app::service::RenderService *render_service)
void HandleRenderDungeon(const httplib::Request &req, httplib::Response &res, yaze::app::service::RenderService *render_service)
void HandleCommandExecute(const httplib::Request &req, httplib::Response &res, yaze::Rom *rom)
void HandleAnnotationDelete(const httplib::Request &req, httplib::Response &res, const std::string &project_path)
void HandleAnnotationList(const httplib::Request &req, httplib::Response &res, const std::string &project_path)
void HandleAnnotationCreate(const httplib::Request &req, httplib::Response &res, const std::string &project_path)
void HandleCommandList(const httplib::Request &req, httplib::Response &res)
void HandleWindowHide(const httplib::Request &req, httplib::Response &res, const std::function< bool()> &action)
void HandleHealth(const httplib::Request &req, httplib::Response &res, const BonjourPublisher *bonjour)
SymbolFormat
Supported symbol file formats.