table_parser.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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* score = nx_json_get(json, "score");
  200. if(score) {
  201. table_file_parse_bool(score, "display", table->score.display);
  202. table_file_parse_vec2(score, "position", table->score.p);
  203. }
  204. const nx_json* balls = nx_json_get(json, "balls");
  205. if(balls) {
  206. for(int i = 0; i < balls->children.length; i++) {
  207. const nx_json* ball = nx_json_item(balls, i);
  208. if(!ball) continue;
  209. Vec2 p;
  210. if(!table_file_parse_vec2(ball, "position", p)) {
  211. FURI_LOG_E(TAG, "Ball missing \"position\", skipping");
  212. continue;
  213. }
  214. if(!ON_TABLE(p)) {
  215. FURI_LOG_W(
  216. TAG,
  217. "Ball with position %.1f,%.1f is not on table!",
  218. (double)p.x,
  219. (double)p.y);
  220. }
  221. Ball new_ball(p);
  222. table_file_parse_float(ball, "radius", new_ball.r);
  223. Vec2 v = (Vec2){0, 0};
  224. table_file_parse_vec2(ball, "velocity", v);
  225. new_ball.accelerate(v);
  226. table->balls_initial.push_back(new_ball);
  227. table->balls.push_back(new_ball);
  228. }
  229. }
  230. if(table->balls.size() == 0) {
  231. FURI_LOG_E(TAG, "Table has NO BALLS");
  232. snprintf(pb->text, 256, "No balls\nfound in\ntable file!");
  233. delete table;
  234. table = NULL;
  235. break;
  236. }
  237. // TODO: plungers need work
  238. const nx_json* plunger = nx_json_get(json, "plunger");
  239. if(plunger) {
  240. Vec2 p;
  241. table_file_parse_vec2(plunger, "position", p);
  242. int s = 100;
  243. table_file_parse_int(plunger, "size", s);
  244. table->plunger = new Plunger(p);
  245. } else {
  246. FURI_LOG_W(
  247. TAG, "Table has NO PLUNGER - s'ok, we don't really support one anyway (yet)");
  248. }
  249. const nx_json* flippers = nx_json_get(json, "flippers");
  250. if(flippers) {
  251. for(int i = 0; i < flippers->children.length; i++) {
  252. const nx_json* flipper = nx_json_item(flippers, i);
  253. Vec2 p;
  254. if(!table_file_parse_vec2(flipper, "position", p)) {
  255. FURI_LOG_E(TAG, "Flipper missing \"position\", skipping");
  256. continue;
  257. }
  258. if(!ON_TABLE(p)) {
  259. FURI_LOG_W(
  260. TAG,
  261. "Flipper with position %.1f,%.1f is not on table!",
  262. (double)p.x,
  263. (double)p.y);
  264. }
  265. const nx_json* side = nx_json_get(flipper, "side");
  266. Flipper::Side sd = Flipper::LEFT;
  267. if(side && !strcmp(side->text_value, "RIGHT")) {
  268. sd = Flipper::RIGHT;
  269. }
  270. int sz = DEF_FLIPPER_SIZE;
  271. table_file_parse_int(flipper, "size", sz);
  272. Flipper flip(p, sd, sz);
  273. // flip.notification = &notify_flipper;
  274. table->flippers.push_back(flip);
  275. }
  276. }
  277. const nx_json* bumpers = nx_json_get(json, "bumpers");
  278. if(bumpers) {
  279. for(int i = 0; i < bumpers->children.length; i++) {
  280. const nx_json* bumper = nx_json_item(bumpers, i);
  281. Vec2 p;
  282. if(!table_file_parse_vec2(bumper, "position", p)) {
  283. FURI_LOG_E(TAG, "Bumper missing \"position\", skipping");
  284. continue;
  285. }
  286. if(!ON_TABLE(p)) {
  287. FURI_LOG_W(
  288. TAG,
  289. "Bumper with position %.1f,%.1f is not on table!",
  290. (double)p.x,
  291. (double)p.y);
  292. }
  293. int r = DEF_BUMPER_RADIUS;
  294. table_file_parse_int(bumper, "radius", r);
  295. float bnc = DEF_BUMPER_BOUNCE;
  296. table_file_parse_float(bumper, "bounce", bnc);
  297. Bumper* new_bumper = new Bumper(p, r);
  298. new_bumper->bounce = bnc;
  299. new_bumper->notification = notify_bumper_hit;
  300. table->objects.push_back(new_bumper);
  301. }
  302. }
  303. constexpr float pi_180 = M_PI / 180;
  304. const nx_json* arcs = nx_json_get(json, "arcs");
  305. if(arcs) {
  306. for(int i = 0; i < arcs->children.length; i++) {
  307. const nx_json* arc = nx_json_item(arcs, i);
  308. Vec2 p;
  309. if(!table_file_parse_vec2(arc, "position", p)) {
  310. FURI_LOG_E(TAG, "Arc missing \"position\"");
  311. continue;
  312. }
  313. if(!ON_TABLE(p)) {
  314. FURI_LOG_W(
  315. TAG,
  316. "Arc with position %.1f,%.1f is not on table!",
  317. (double)p.x,
  318. (double)p.y);
  319. }
  320. int r = DEF_BUMPER_RADIUS;
  321. table_file_parse_int(arc, "radius", r);
  322. float bnc = 0.95f; // DEF_BUMPER_BOUNCE?
  323. table_file_parse_float(arc, "bounce", bnc);
  324. float start_angle = 0.0;
  325. table_file_parse_float(arc, "start_angle", start_angle);
  326. start_angle *= pi_180;
  327. float end_angle = 0.0;
  328. table_file_parse_float(arc, "end_angle", end_angle);
  329. end_angle *= pi_180;
  330. Arc::Surface surface = Arc::OUTSIDE;
  331. const nx_json* stype = nx_json_get(arc, "surface");
  332. if(stype && !strcmp(stype->text_value, "INSIDE")) {
  333. surface = Arc::INSIDE;
  334. }
  335. Arc* new_bumper = new Arc(p, r, start_angle, end_angle, surface);
  336. new_bumper->bounce = bnc;
  337. table->objects.push_back(new_bumper);
  338. }
  339. }
  340. const nx_json* rails = nx_json_get(json, "rails");
  341. if(rails) {
  342. for(int i = 0; i < rails->children.length; i++) {
  343. const nx_json* rail = nx_json_item(rails, i);
  344. Vec2 s;
  345. if(!table_file_parse_vec2(rail, "start", s)) {
  346. FURI_LOG_E(TAG, "Rail missing \"start\", skipping");
  347. continue;
  348. }
  349. if(!ON_TABLE(s)) {
  350. FURI_LOG_W(
  351. TAG,
  352. "Rail with starting position %.1f,%.1f is not on table!",
  353. (double)s.x,
  354. (double)s.y);
  355. }
  356. Vec2 e;
  357. if(!table_file_parse_vec2(rail, "end", e)) {
  358. FURI_LOG_E(TAG, "Rail missing \"end\", skipping");
  359. continue;
  360. }
  361. if(!ON_TABLE(e)) {
  362. FURI_LOG_W(
  363. TAG,
  364. "Rail with ending position %.1f,%.1f is not on table!",
  365. (double)e.x,
  366. (double)e.y);
  367. }
  368. Polygon* new_rail = new Polygon();
  369. new_rail->add_point(s);
  370. new_rail->add_point(e);
  371. float bnc = DEF_RAIL_BOUNCE;
  372. table_file_parse_float(rail, "bounce", bnc);
  373. new_rail->bounce = bnc;
  374. int double_sided = 0;
  375. table_file_parse_int(rail, "double_sided", double_sided);
  376. new_rail->finalize();
  377. new_rail->notification = &notify_rail_hit;
  378. table->objects.push_back(new_rail);
  379. if(double_sided) {
  380. new_rail = new Polygon();
  381. new_rail->add_point(e);
  382. new_rail->add_point(s);
  383. new_rail->bounce = bnc;
  384. new_rail->finalize();
  385. new_rail->notification = &notify_rail_hit;
  386. table->objects.push_back(new_rail);
  387. }
  388. }
  389. }
  390. const nx_json* portals = nx_json_get(json, "portals");
  391. if(portals) {
  392. for(int i = 0; i < portals->children.length; i++) {
  393. const nx_json* portal = nx_json_item(portals, i);
  394. Vec2 a1;
  395. if(!table_file_parse_vec2(portal, "a_start", a1)) {
  396. FURI_LOG_E(TAG, "Portal missing \"a_start\", skipping");
  397. continue;
  398. }
  399. if(!ON_TABLE(a1)) {
  400. FURI_LOG_W(
  401. TAG,
  402. "Portal A with starting position %.1f,%.1f is not on table!",
  403. (double)a1.x,
  404. (double)a1.y);
  405. }
  406. Vec2 a2;
  407. if(!table_file_parse_vec2(portal, "a_end", a2)) {
  408. FURI_LOG_E(TAG, "Portal missing \"a_end\", skipping");
  409. continue;
  410. }
  411. if(!ON_TABLE(a2)) {
  412. FURI_LOG_W(
  413. TAG,
  414. "Portal A with ending position %.1f,%.1f is not on table!",
  415. (double)a2.x,
  416. (double)a2.y);
  417. }
  418. Vec2 b1;
  419. if(!table_file_parse_vec2(portal, "b_start", b1)) {
  420. FURI_LOG_E(TAG, "Portal missing \"b_start\", skipping");
  421. continue;
  422. }
  423. if(!ON_TABLE(b1)) {
  424. FURI_LOG_W(
  425. TAG,
  426. "Portal B with starting position %.1f,%.1f is not on table!",
  427. (double)b1.x,
  428. (double)b1.y);
  429. }
  430. Vec2 b2;
  431. if(!table_file_parse_vec2(portal, "b_end", b2)) {
  432. FURI_LOG_E(TAG, "Portal missing \"b_end\", skipping");
  433. continue;
  434. }
  435. if(!ON_TABLE(b2)) {
  436. FURI_LOG_W(
  437. TAG,
  438. "Portal B with ending position %.1f,%.1f is not on table!",
  439. (double)b2.x,
  440. (double)b2.y);
  441. }
  442. Portal* new_portal = new Portal(a1, a2, b1, b2);
  443. new_portal->finalize();
  444. new_portal->notification = &notify_portal;
  445. table->objects.push_back(new_portal);
  446. }
  447. }
  448. const nx_json* rollovers = nx_json_get(json, "rollovers");
  449. if(rollovers) {
  450. for(int i = 0; i < rollovers->children.length; i++) {
  451. const nx_json* rollover = nx_json_item(rollovers, i);
  452. Vec2 p;
  453. if(!table_file_parse_vec2(rollover, "position", p)) {
  454. FURI_LOG_E(TAG, "Rollover missing \"position\", skipping");
  455. continue;
  456. }
  457. if(!ON_TABLE(p)) {
  458. FURI_LOG_W(
  459. TAG,
  460. "Rollover with position %.1f,%.1f is not on table!",
  461. (double)p.x,
  462. (double)p.y);
  463. }
  464. char sym = '*';
  465. const nx_json* symbol = nx_json_get(rollover, "symbol");
  466. if(symbol) {
  467. sym = symbol->text_value[0];
  468. }
  469. Rollover* new_rollover = new Rollover(p, sym);
  470. table->objects.push_back(new_rollover);
  471. }
  472. }
  473. const nx_json* turbos = nx_json_get(json, "turbos");
  474. if(turbos) {
  475. for(int i = 0; i < turbos->children.length; i++) {
  476. const nx_json* turbo = nx_json_item(turbos, i);
  477. Vec2 p;
  478. if(!table_file_parse_vec2(turbo, "position", p)) {
  479. FURI_LOG_E(TAG, "Turbo missing \"position\"");
  480. continue;
  481. }
  482. if(!ON_TABLE(p)) {
  483. FURI_LOG_W(
  484. TAG,
  485. "Turbo with position %.1f,%.1f is not on table!",
  486. (double)p.x,
  487. (double)p.y);
  488. }
  489. float angle = 0;
  490. table_file_parse_float(turbo, "angle", angle);
  491. angle *= pi_180;
  492. float boost = 10;
  493. table_file_parse_float(turbo, "boost", boost);
  494. Turbo* new_turbo = new Turbo(p, angle, boost);
  495. table->objects.push_back(new_turbo);
  496. }
  497. }
  498. break;
  499. } while(false);
  500. nx_json_free(json);
  501. free(json_buffer);
  502. return table;
  503. }