table_parser.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #include <toolbox/dir_walk.h>
  2. #include <toolbox/path.h>
  3. #include <toolbox/stream/stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include <toolbox/args.h>
  6. #include "nxjson/nxjson.h"
  7. #include "pinball0.h"
  8. #include "table.h"
  9. #include "notifications.h"
  10. namespace {
  11. bool ON_TABLE(const Vec2& p) {
  12. return 0 <= p.x && p.x <= 630 && 0 <= p.y && p.y <= 1270;
  13. }
  14. };
  15. void table_table_list_init(void* ctx) {
  16. PinballApp* pb = (PinballApp*)ctx;
  17. // using the asset file path, read the table files, and for each one, extract their
  18. // display name (oof). let's just use their filenames for now (stripping any XX_ prefix)
  19. // sort tables by original filename
  20. const char* paths[] = {APP_ASSETS_PATH("tables"), APP_DATA_PATH("tables")};
  21. const size_t ext_len_max = 32;
  22. char ext[ext_len_max];
  23. for(size_t p = 0; p < 2; p++) {
  24. const char* path = paths[p];
  25. // const char* asset_path = APP_ASSETS_PATH("tables");
  26. FURI_LOG_I(TAG, "Loading table list from: %s", path);
  27. FuriString* table_path = furi_string_alloc();
  28. DirWalk* dir_walk = dir_walk_alloc(pb->storage);
  29. dir_walk_set_recursive(dir_walk, false);
  30. if(dir_walk_open(dir_walk, path)) {
  31. while(dir_walk_read(dir_walk, table_path, NULL) == DirWalkOK) {
  32. path_extract_extension(table_path, ext, ext_len_max);
  33. if(strcmp(ext, ".json") != 0) {
  34. FURI_LOG_W(
  35. TAG, "Skipping non-json file: %s", furi_string_get_cstr(table_path));
  36. continue;
  37. }
  38. const char* cpath = furi_string_get_cstr(table_path);
  39. FuriString* filename_no_ext = furi_string_alloc();
  40. path_extract_filename_no_ext(cpath, filename_no_ext);
  41. // If filename starts with XX_ (for custom sorting) strip the prefix
  42. char c = furi_string_get_char(filename_no_ext, 2);
  43. if(c == '_') {
  44. char a = furi_string_get_char(filename_no_ext, 0);
  45. char b = furi_string_get_char(filename_no_ext, 1);
  46. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  47. furi_string_right(filename_no_ext, 3);
  48. }
  49. }
  50. if(!pb->settings.debug_mode &&
  51. !strncmp("dbg", furi_string_get_cstr(filename_no_ext), 3)) {
  52. furi_string_free(filename_no_ext);
  53. continue;
  54. }
  55. FURI_LOG_I(
  56. TAG,
  57. "Found table: name=%s | path=%s",
  58. furi_string_get_cstr(filename_no_ext),
  59. furi_string_get_cstr(table_path));
  60. // set display 'name' and 'filename'
  61. TableList::TableMenuItem tmi;
  62. tmi.filename = furi_string_alloc_set_str(cpath);
  63. tmi.name = filename_no_ext;
  64. // Insert in sorted order
  65. size_t i = 0;
  66. auto it = pb->table_list.menu_items.begin();
  67. for(; it != pb->table_list.menu_items.end(); it++, i++) {
  68. if(strcmp(
  69. furi_string_get_cstr(tmi.filename),
  70. furi_string_get_cstr(it->filename)) > 0) {
  71. continue;
  72. }
  73. pb->table_list.menu_items.insert(it, tmi);
  74. break;
  75. }
  76. if(pb->table_list.menu_items.size() == i) {
  77. pb->table_list.menu_items.push_back(tmi);
  78. }
  79. }
  80. }
  81. furi_string_free(table_path);
  82. dir_walk_free(dir_walk);
  83. }
  84. // Add 'Settings' as last element
  85. TableList::TableMenuItem settings;
  86. settings.filename = furi_string_alloc_set_str("99_Settings");
  87. settings.name = furi_string_alloc_set_str("SETTINGS");
  88. pb->table_list.menu_items.push_back(settings);
  89. FURI_LOG_I(TAG, "Found %d tables", pb->table_list.menu_items.size());
  90. for(auto& tmi : pb->table_list.menu_items) {
  91. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tmi.name));
  92. }
  93. pb->table_list.display_size = 5; // how many tables to display at once
  94. pb->table_list.selected = 0;
  95. }
  96. // json parse helper function
  97. bool table_file_parse_vec2(const nx_json* json, const char* key, Vec2& v) {
  98. const nx_json* item = nx_json_get(json, key);
  99. if(!item || item->children.length != 2) {
  100. return false;
  101. }
  102. v.x = nx_json_item(item, 0)->num.dbl_value;
  103. v.y = nx_json_item(item, 1)->num.dbl_value;
  104. return true;
  105. }
  106. bool table_file_parse_int(const nx_json* json, const char* key, int& v) {
  107. const nx_json* item = nx_json_get(json, key);
  108. if(!item) return false;
  109. v = item->num.u_value;
  110. return true;
  111. }
  112. bool table_file_parse_bool(const nx_json* json, const char* key, bool& v) {
  113. int value = v == true ? 1 : 0; // set default value
  114. if(table_file_parse_int(json, key, value)) {
  115. v = value > 0 ? true : false;
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool table_file_parse_float(const nx_json* json, const char* key, float& v) {
  121. const nx_json* item = nx_json_get(json, key);
  122. if(!item) return false;
  123. v = item->num.dbl_value;
  124. return true;
  125. }
  126. Table* table_load_table_from_file(PinballApp* pb, size_t index) {
  127. auto& tmi = pb->table_list.menu_items[index];
  128. FURI_LOG_I(TAG, "Reading file: %s", furi_string_get_cstr(tmi.filename));
  129. File* file = storage_file_alloc(pb->storage);
  130. FileInfo fileinfo;
  131. FS_Error error =
  132. storage_common_stat(pb->storage, furi_string_get_cstr(tmi.filename), &fileinfo);
  133. if(error != FSE_OK) {
  134. FURI_LOG_E(TAG, "Could not find file");
  135. storage_file_free(file);
  136. return NULL;
  137. }
  138. // TODO: determine an appropriate max file size and make configurable
  139. FURI_LOG_I(TAG, "Found file ok!");
  140. if(fileinfo.size >= 8192) {
  141. FURI_LOG_E(TAG, "Table file size too big");
  142. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  143. storage_file_free(file);
  144. return NULL;
  145. }
  146. FURI_LOG_I(TAG, "File size is ok!");
  147. bool ok =
  148. storage_file_open(file, furi_string_get_cstr(tmi.filename), FSAM_READ, FSOM_OPEN_EXISTING);
  149. FURI_LOG_I(TAG, "File opened? %s", ok ? "YES" : "NO");
  150. // read the file as a string
  151. uint8_t* buffer;
  152. uint64_t file_size = storage_file_size(file);
  153. if(file_size > 8192) { // TODO - what's the right size?
  154. FURI_LOG_E(TAG, "Table file is too large! (> 8192 bytes)");
  155. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  156. storage_file_free(file);
  157. return NULL;
  158. }
  159. buffer = (uint8_t*)malloc(file_size);
  160. size_t read_count = storage_file_read(file, buffer, file_size);
  161. // if(storage_file_get_error(file) != FSE_OK) {
  162. // FURI_LOG_E(TAG, "Um, couldn't read file");
  163. // storage_file_free(file);
  164. // return NULL;
  165. // }
  166. storage_file_free(file);
  167. if(read_count != file_size) {
  168. FURI_LOG_E(TAG, "Error reading file. expected %lld, got %d", file_size, read_count);
  169. free(buffer);
  170. return NULL;
  171. }
  172. FURI_LOG_I(TAG, "Read file into buffer! %d bytes", read_count);
  173. // let's parse this shit
  174. char* json_buffer = (char*)malloc(read_count * sizeof(char) + 1);
  175. for(uint16_t i = 0; i < read_count; i++) {
  176. json_buffer[i] = buffer[i];
  177. }
  178. json_buffer[read_count] = 0;
  179. free(buffer);
  180. const nx_json* json = nx_json_parse(json_buffer, 0);
  181. if(!json) {
  182. FURI_LOG_E(TAG, "Failed to parse table json!");
  183. snprintf(pb->text, 256, "Failed to\nparse table\njson!!");
  184. free(json_buffer);
  185. return NULL;
  186. }
  187. Table* table = new Table();
  188. do {
  189. const nx_json* lives = nx_json_get(json, "lives");
  190. if(lives) {
  191. table_file_parse_int(lives, "value", table->lives.value);
  192. table_file_parse_bool(lives, "display", table->lives.display);
  193. table_file_parse_vec2(lives, "position", table->lives.p);
  194. const nx_json* align = nx_json_get(lives, "align");
  195. if(align && !strcmp(align->text_value, "VERTICAL")) {
  196. table->lives.alignment = Lives::Vertical;
  197. }
  198. }
  199. const nx_json* tilt = nx_json_get(json, "tilt_detect");
  200. if(tilt) {
  201. table->tilt_detect_enabled = tilt->num.u_value > 0 ? true : false;
  202. }
  203. const nx_json* score = nx_json_get(json, "score");
  204. if(score) {
  205. table_file_parse_bool(score, "display", table->score.display);
  206. table_file_parse_vec2(score, "position", table->score.p);
  207. }
  208. const nx_json* balls = nx_json_get(json, "balls");
  209. if(balls) {
  210. for(int i = 0; i < balls->children.length; i++) {
  211. const nx_json* ball = nx_json_item(balls, i);
  212. if(!ball) continue;
  213. Vec2 p;
  214. if(!table_file_parse_vec2(ball, "position", p)) {
  215. FURI_LOG_E(TAG, "Ball missing \"position\", skipping");
  216. continue;
  217. }
  218. if(!ON_TABLE(p)) {
  219. FURI_LOG_W(
  220. TAG,
  221. "Ball with position %.1f,%.1f is not on table!",
  222. (double)p.x,
  223. (double)p.y);
  224. }
  225. Ball new_ball(p);
  226. table_file_parse_float(ball, "radius", new_ball.r);
  227. Vec2 v = (Vec2){0, 0};
  228. table_file_parse_vec2(ball, "velocity", v);
  229. new_ball.accelerate(v);
  230. table->balls_initial.push_back(new_ball);
  231. table->balls.push_back(new_ball);
  232. }
  233. }
  234. if(table->balls.size() == 0) {
  235. FURI_LOG_E(TAG, "Table has NO BALLS");
  236. snprintf(pb->text, 256, "No balls\nfound in\ntable file!");
  237. delete table;
  238. table = NULL;
  239. break;
  240. }
  241. // TODO: plungers need work
  242. const nx_json* plunger = nx_json_get(json, "plunger");
  243. if(plunger) {
  244. Vec2 p;
  245. table_file_parse_vec2(plunger, "position", p);
  246. int s = 100;
  247. table_file_parse_int(plunger, "size", s);
  248. table->plunger = new Plunger(p);
  249. } else {
  250. FURI_LOG_W(
  251. TAG, "Table has NO PLUNGER - s'ok, we don't really support one anyway (yet)");
  252. }
  253. const nx_json* flippers = nx_json_get(json, "flippers");
  254. if(flippers) {
  255. for(int i = 0; i < flippers->children.length; i++) {
  256. const nx_json* flipper = nx_json_item(flippers, i);
  257. Vec2 p;
  258. if(!table_file_parse_vec2(flipper, "position", p)) {
  259. FURI_LOG_E(TAG, "Flipper missing \"position\", skipping");
  260. continue;
  261. }
  262. if(!ON_TABLE(p)) {
  263. FURI_LOG_W(
  264. TAG,
  265. "Flipper with position %.1f,%.1f is not on table!",
  266. (double)p.x,
  267. (double)p.y);
  268. }
  269. const nx_json* side = nx_json_get(flipper, "side");
  270. Flipper::Side sd = Flipper::LEFT;
  271. if(side && !strcmp(side->text_value, "RIGHT")) {
  272. sd = Flipper::RIGHT;
  273. }
  274. int sz = DEF_FLIPPER_SIZE;
  275. table_file_parse_int(flipper, "size", sz);
  276. Flipper flip(p, sd, sz);
  277. // flip.notification = &notify_flipper;
  278. table->flippers.push_back(flip);
  279. }
  280. }
  281. const nx_json* bumpers = nx_json_get(json, "bumpers");
  282. if(bumpers) {
  283. for(int i = 0; i < bumpers->children.length; i++) {
  284. const nx_json* bumper = nx_json_item(bumpers, i);
  285. Vec2 p;
  286. if(!table_file_parse_vec2(bumper, "position", p)) {
  287. FURI_LOG_E(TAG, "Bumper missing \"position\", skipping");
  288. continue;
  289. }
  290. if(!ON_TABLE(p)) {
  291. FURI_LOG_W(
  292. TAG,
  293. "Bumper with position %.1f,%.1f is not on table!",
  294. (double)p.x,
  295. (double)p.y);
  296. }
  297. int r = DEF_BUMPER_RADIUS;
  298. table_file_parse_int(bumper, "radius", r);
  299. float bnc = DEF_BUMPER_BOUNCE;
  300. table_file_parse_float(bumper, "bounce", bnc);
  301. Bumper* new_bumper = new Bumper(p, r);
  302. new_bumper->bounce = bnc;
  303. new_bumper->notification = notify_bumper_hit;
  304. table->objects.push_back(new_bumper);
  305. }
  306. }
  307. constexpr float pi_180 = M_PI / 180;
  308. const nx_json* arcs = nx_json_get(json, "arcs");
  309. if(arcs) {
  310. for(int i = 0; i < arcs->children.length; i++) {
  311. const nx_json* arc = nx_json_item(arcs, i);
  312. Vec2 p;
  313. if(!table_file_parse_vec2(arc, "position", p)) {
  314. FURI_LOG_E(TAG, "Arc missing \"position\"");
  315. continue;
  316. }
  317. if(!ON_TABLE(p)) {
  318. FURI_LOG_W(
  319. TAG,
  320. "Arc with position %.1f,%.1f is not on table!",
  321. (double)p.x,
  322. (double)p.y);
  323. }
  324. int r = DEF_BUMPER_RADIUS;
  325. table_file_parse_int(arc, "radius", r);
  326. float bnc = 0.95f; // DEF_BUMPER_BOUNCE?
  327. table_file_parse_float(arc, "bounce", bnc);
  328. float start_angle = 0.0;
  329. table_file_parse_float(arc, "start_angle", start_angle);
  330. start_angle *= pi_180;
  331. float end_angle = 0.0;
  332. table_file_parse_float(arc, "end_angle", end_angle);
  333. end_angle *= pi_180;
  334. Arc::Surface surface = Arc::OUTSIDE;
  335. const nx_json* stype = nx_json_get(arc, "surface");
  336. if(stype && !strcmp(stype->text_value, "INSIDE")) {
  337. surface = Arc::INSIDE;
  338. }
  339. Arc* new_bumper = new Arc(p, r, start_angle, end_angle, surface);
  340. new_bumper->bounce = bnc;
  341. table->objects.push_back(new_bumper);
  342. }
  343. }
  344. const nx_json* rails = nx_json_get(json, "rails");
  345. if(rails) {
  346. for(int i = 0; i < rails->children.length; i++) {
  347. const nx_json* rail = nx_json_item(rails, i);
  348. Vec2 s;
  349. if(!table_file_parse_vec2(rail, "start", s)) {
  350. FURI_LOG_E(TAG, "Rail missing \"start\", skipping");
  351. continue;
  352. }
  353. if(!ON_TABLE(s)) {
  354. FURI_LOG_W(
  355. TAG,
  356. "Rail with starting position %.1f,%.1f is not on table!",
  357. (double)s.x,
  358. (double)s.y);
  359. }
  360. Vec2 e;
  361. if(!table_file_parse_vec2(rail, "end", e)) {
  362. FURI_LOG_E(TAG, "Rail missing \"end\", skipping");
  363. continue;
  364. }
  365. if(!ON_TABLE(e)) {
  366. FURI_LOG_W(
  367. TAG,
  368. "Rail with ending position %.1f,%.1f is not on table!",
  369. (double)e.x,
  370. (double)e.y);
  371. }
  372. Polygon* new_rail = new Polygon();
  373. new_rail->add_point(s);
  374. new_rail->add_point(e);
  375. float bnc = DEF_RAIL_BOUNCE;
  376. table_file_parse_float(rail, "bounce", bnc);
  377. new_rail->bounce = bnc;
  378. int double_sided = 0;
  379. table_file_parse_int(rail, "double_sided", double_sided);
  380. new_rail->finalize();
  381. new_rail->notification = &notify_rail_hit;
  382. table->objects.push_back(new_rail);
  383. if(double_sided) {
  384. new_rail = new Polygon();
  385. new_rail->add_point(e);
  386. new_rail->add_point(s);
  387. new_rail->bounce = bnc;
  388. new_rail->finalize();
  389. new_rail->notification = &notify_rail_hit;
  390. table->objects.push_back(new_rail);
  391. }
  392. }
  393. }
  394. const nx_json* portals = nx_json_get(json, "portals");
  395. if(portals) {
  396. for(int i = 0; i < portals->children.length; i++) {
  397. const nx_json* portal = nx_json_item(portals, i);
  398. Vec2 a1;
  399. if(!table_file_parse_vec2(portal, "a_start", a1)) {
  400. FURI_LOG_E(TAG, "Portal missing \"a_start\", skipping");
  401. continue;
  402. }
  403. if(!ON_TABLE(a1)) {
  404. FURI_LOG_W(
  405. TAG,
  406. "Portal A with starting position %.1f,%.1f is not on table!",
  407. (double)a1.x,
  408. (double)a1.y);
  409. }
  410. Vec2 a2;
  411. if(!table_file_parse_vec2(portal, "a_end", a2)) {
  412. FURI_LOG_E(TAG, "Portal missing \"a_end\", skipping");
  413. continue;
  414. }
  415. if(!ON_TABLE(a2)) {
  416. FURI_LOG_W(
  417. TAG,
  418. "Portal A with ending position %.1f,%.1f is not on table!",
  419. (double)a2.x,
  420. (double)a2.y);
  421. }
  422. Vec2 b1;
  423. if(!table_file_parse_vec2(portal, "b_start", b1)) {
  424. FURI_LOG_E(TAG, "Portal missing \"b_start\", skipping");
  425. continue;
  426. }
  427. if(!ON_TABLE(b1)) {
  428. FURI_LOG_W(
  429. TAG,
  430. "Portal B with starting position %.1f,%.1f is not on table!",
  431. (double)b1.x,
  432. (double)b1.y);
  433. }
  434. Vec2 b2;
  435. if(!table_file_parse_vec2(portal, "b_end", b2)) {
  436. FURI_LOG_E(TAG, "Portal missing \"b_end\", skipping");
  437. continue;
  438. }
  439. if(!ON_TABLE(b2)) {
  440. FURI_LOG_W(
  441. TAG,
  442. "Portal B with ending position %.1f,%.1f is not on table!",
  443. (double)b2.x,
  444. (double)b2.y);
  445. }
  446. Portal* new_portal = new Portal(a1, a2, b1, b2);
  447. new_portal->finalize();
  448. new_portal->notification = &notify_portal;
  449. table->objects.push_back(new_portal);
  450. }
  451. }
  452. const nx_json* rollovers = nx_json_get(json, "rollovers");
  453. if(rollovers) {
  454. for(int i = 0; i < rollovers->children.length; i++) {
  455. const nx_json* rollover = nx_json_item(rollovers, i);
  456. Vec2 p;
  457. if(!table_file_parse_vec2(rollover, "position", p)) {
  458. FURI_LOG_E(TAG, "Rollover missing \"position\", skipping");
  459. continue;
  460. }
  461. if(!ON_TABLE(p)) {
  462. FURI_LOG_W(
  463. TAG,
  464. "Rollover with position %.1f,%.1f is not on table!",
  465. (double)p.x,
  466. (double)p.y);
  467. }
  468. char sym = '*';
  469. const nx_json* symbol = nx_json_get(rollover, "symbol");
  470. if(symbol) {
  471. sym = symbol->text_value[0];
  472. }
  473. Rollover* new_rollover = new Rollover(p, sym);
  474. table->objects.push_back(new_rollover);
  475. }
  476. }
  477. const nx_json* turbos = nx_json_get(json, "turbos");
  478. if(turbos) {
  479. for(int i = 0; i < turbos->children.length; i++) {
  480. const nx_json* turbo = nx_json_item(turbos, i);
  481. Vec2 p;
  482. if(!table_file_parse_vec2(turbo, "position", p)) {
  483. FURI_LOG_E(TAG, "Turbo missing \"position\"");
  484. continue;
  485. }
  486. if(!ON_TABLE(p)) {
  487. FURI_LOG_W(
  488. TAG,
  489. "Turbo with position %.1f,%.1f is not on table!",
  490. (double)p.x,
  491. (double)p.y);
  492. }
  493. float angle = 0;
  494. table_file_parse_float(turbo, "angle", angle);
  495. angle *= pi_180;
  496. float boost = 10;
  497. table_file_parse_float(turbo, "boost", boost);
  498. Turbo* new_turbo = new Turbo(p, angle, boost);
  499. table->objects.push_back(new_turbo);
  500. }
  501. }
  502. break;
  503. } while(false);
  504. nx_json_free(json);
  505. free(json_buffer);
  506. return table;
  507. }