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 ==
"all")
382 if (req.has_param(
"scale")) {
383 const auto scale_or =
385 if (!scale_or.ok()) {
387 j[
"error"] = std::string(scale_or.status().message());
389 res.set_content(j.dump(),
"application/json");
398 render_req.
scale = scale;
401 if (!result_or.ok()) {
402 const auto& s = result_or.status();
404 j[
"error"] = std::string(s.message());
405 if (s.code() == absl::StatusCode::kInvalidArgument)
407 else if (s.code() == absl::StatusCode::kFailedPrecondition)
411 res.set_content(j.dump(),
"application/json");
415 const auto& result = *result_or;
417 res.set_content(
reinterpret_cast<const char*
>(result.png_data.data()),
418 result.png_data.size(),
"image/png");
419 res.set_header(
"X-Room-Id", std::to_string(room_id));
420 res.set_header(
"X-Room-Width", std::to_string(result.width));
421 res.set_header(
"X-Room-Height", std::to_string(result.height));
425 const httplib::Request& req, httplib::Response& res,
429 if (!render_service) {
431 j[
"error"] =
"Render service not available";
433 res.set_content(j.dump(),
"application/json");
438 if (!ParseRoomParam(req, res, room_id))
443 const auto& s = meta_or.status();
445 j[
"error"] = std::string(s.message());
446 res.status = (s.code() == absl::StatusCode::kInvalidArgument) ? 400 : 500;
447 res.set_content(j.dump(),
"application/json");
451 const auto& meta = *meta_or;
453 j[
"room_id"] = meta.room_id;
454 j[
"blockset"] = meta.blockset;
455 j[
"spriteset"] = meta.spriteset;
456 j[
"palette"] = meta.palette;
457 j[
"layout_id"] = meta.layout_id;
458 j[
"effect"] = meta.effect;
459 j[
"collision"] = meta.collision;
460 j[
"tag1"] = meta.tag1;
461 j[
"tag2"] = meta.tag2;
462 j[
"message_id"] = meta.message_id;
463 j[
"has_custom_collision"] = meta.has_custom_collision;
464 j[
"object_count"] = meta.object_count;
465 j[
"sprite_count"] = meta.sprite_count;
467 res.set_content(j.dump(),
"application/json");
479 json body = json::parse(req.body);
480 std::string command_name = body.value(
"command",
"");
481 if (command_name.empty()) {
483 j[
"error"] =
"Missing 'command' field";
485 res.set_content(j.dump(),
"application/json");
489 std::vector<std::string> args;
490 if (body.contains(
"args") && body[
"args"].is_array()) {
491 for (
const auto& arg : body[
"args"]) {
492 args.push_back(arg.get<std::string>());
497 if (!registry.HasCommand(command_name)) {
499 j[
"error"] =
"Unknown command: " + command_name;
501 res.set_content(j.dump(),
"application/json");
505 std::string captured_output;
506 auto status = registry.Execute(command_name, args, rom, &captured_output);
510 response[
"status"] =
"ok";
511 response[
"command"] = command_name;
514 response[
"result"] = json::parse(captured_output);
516 response[
"result"] = captured_output;
520 response[
"status"] =
"error";
521 response[
"command"] = command_name;
522 response[
"error"] = std::string(status.message());
523 if (!captured_output.empty()) {
524 response[
"output"] = captured_output;
528 res.set_content(response.dump(),
"application/json");
529 }
catch (
const std::exception& e) {
531 j[
"error"] = e.what();
533 res.set_content(j.dump(),
"application/json");
542 json j_commands = json::array();
544 for (
const auto& category : registry.GetCategories()) {
545 for (
const auto& cmd_name : registry.GetCommandsInCategory(category)) {
546 const auto* meta = registry.GetMetadata(cmd_name);
551 j_cmd[
"name"] = meta->name;
552 j_cmd[
"category"] = meta->category;
553 j_cmd[
"description"] = meta->description;
554 j_cmd[
"usage"] = meta->usage;
555 j_cmd[
"requires_rom"] = meta->requires_rom;
556 j_commands.push_back(j_cmd);
561 response[
"commands"] = j_commands;
562 response[
"count"] = j_commands.size();
564 res.set_content(response.dump(),
"application/json");
574 if (project_path.empty())
577 return project_path +
"/Docs/Dev/Planning/annotations.json";
582 return json::object();
583 std::ifstream file(path);
585 return json::object();
587 return json::parse(file);
589 return json::object();
597 const std::filesystem::path parent =
598 std::filesystem::path(path).parent_path();
599 if (!parent.empty()) {
600 std::error_code error;
601 std::filesystem::create_directories(parent, error);
607 std::ofstream file(path);
610 file << data.dump(2);
617 const std::string& project_path) {
620 std::string path = ResolveAnnotationsPath(project_path);
623 j[
"error"] =
"No project path configured";
625 res.set_content(j.dump(),
"application/json");
629 json file_data = LoadAnnotationsFile(path);
630 json annotations = file_data.value(
"annotations", json::array());
633 if (req.has_param(
"room")) {
636 room_id = std::stoi(req.get_param_value(
"room"),
nullptr, 0);
639 j[
"error"] =
"Invalid room parameter";
641 res.set_content(j.dump(),
"application/json");
645 json filtered = json::array();
646 for (
const auto& ann : annotations) {
647 if (ann.value(
"room_id", -1) == room_id) {
648 filtered.push_back(ann);
651 annotations = filtered;
655 response[
"annotations"] = annotations;
657 res.set_content(response.dump(),
"application/json");
661 const std::string& project_path) {
664 std::string path = ResolveAnnotationsPath(project_path);
667 j[
"error"] =
"No project path configured";
669 res.set_content(j.dump(),
"application/json");
674 json new_annotation = json::parse(req.body);
675 json file_data = LoadAnnotationsFile(path);
677 if (!file_data.contains(
"annotations")) {
678 file_data[
"annotations"] = json::array();
680 file_data[
"annotations"].push_back(new_annotation);
682 if (SaveAnnotationsFile(path, file_data)) {
684 response[
"status"] =
"ok";
685 response[
"id"] = new_annotation.value(
"id",
"");
687 res.set_content(response.dump(),
"application/json");
690 j[
"error"] =
"Failed to write annotations file";
692 res.set_content(j.dump(),
"application/json");
694 }
catch (
const std::exception& e) {
696 j[
"error"] = e.what();
698 res.set_content(j.dump(),
"application/json");
703 const std::string& project_path) {
706 std::string path = ResolveAnnotationsPath(project_path);
709 j[
"error"] =
"No project path configured";
711 res.set_content(j.dump(),
"application/json");
716 std::string annotation_id;
717 if (!req.matches.empty() && req.matches.size() > 1) {
718 annotation_id = req.matches[1].str();
720 if (annotation_id.empty()) {
722 j[
"error"] =
"Missing annotation ID in URL";
724 res.set_content(j.dump(),
"application/json");
729 json updated = json::parse(req.body);
730 json file_data = LoadAnnotationsFile(path);
731 json& annotations = file_data[
"annotations"];
734 for (
auto& ann : annotations) {
735 if (ann.value(
"id",
"") == annotation_id) {
737 for (
auto it = updated.begin(); it != updated.end(); ++it) {
738 ann[it.key()] = it.value();
747 j[
"error"] =
"Annotation not found: " + annotation_id;
749 res.set_content(j.dump(),
"application/json");
753 if (SaveAnnotationsFile(path, file_data)) {
755 response[
"status"] =
"ok";
756 response[
"id"] = annotation_id;
758 res.set_content(response.dump(),
"application/json");
761 j[
"error"] =
"Failed to write annotations file";
763 res.set_content(j.dump(),
"application/json");
765 }
catch (
const std::exception& e) {
767 j[
"error"] = e.what();
769 res.set_content(j.dump(),
"application/json");
774 const std::string& project_path) {
778 std::string path = ResolveAnnotationsPath(project_path);
781 j[
"error"] =
"No project path configured";
783 res.set_content(j.dump(),
"application/json");
787 std::string annotation_id;
788 if (!req.matches.empty() && req.matches.size() > 1) {
789 annotation_id = req.matches[1].str();
791 if (annotation_id.empty()) {
793 j[
"error"] =
"Missing annotation ID in URL";
795 res.set_content(j.dump(),
"application/json");
799 json file_data = LoadAnnotationsFile(path);
800 json& annotations = file_data[
"annotations"];
802 size_t original_size = annotations.size();
803 json filtered = json::array();
804 for (
const auto& ann : annotations) {
805 if (ann.value(
"id",
"") != annotation_id) {
806 filtered.push_back(ann);
810 if (filtered.size() == original_size) {
812 j[
"error"] =
"Annotation not found: " + annotation_id;
814 res.set_content(j.dump(),
"application/json");
818 file_data[
"annotations"] = filtered;
819 if (SaveAnnotationsFile(path, file_data)) {
821 response[
"status"] =
"ok";
822 response[
"id"] = annotation_id;
824 res.set_content(response.dump(),
"application/json");
827 j[
"error"] =
"Failed to write annotations file";
829 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 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.