callback.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. #include <callback/callback.h>
  2. #include "engine/engine.h"
  3. #include "engine/game_engine.h"
  4. #include "engine/game_manager_i.h"
  5. #include "engine/level_i.h"
  6. #include "engine/entity_i.h"
  7. #include "game/storage.h"
  8. // Below added by Derek Jamison
  9. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  10. #ifdef DEVELOPMENT
  11. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  12. #define DEV_CRASH() furi_crash()
  13. #else
  14. #define FURI_LOG_DEV(tag, format, ...)
  15. #define DEV_CRASH()
  16. #endif
  17. static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  18. {
  19. UNUSED(engine);
  20. GameManager *game_manager = context;
  21. game_manager_input_set(game_manager, input);
  22. game_manager_update(game_manager);
  23. game_manager_render(game_manager, canvas);
  24. }
  25. static int32_t game_app(void *p)
  26. {
  27. UNUSED(p);
  28. GameManager *game_manager = game_manager_alloc();
  29. if (!game_manager)
  30. {
  31. FURI_LOG_E("Game", "Failed to allocate game manager");
  32. return -1;
  33. }
  34. // Setup game engine settings...
  35. GameEngineSettings settings = game_engine_settings_init();
  36. settings.target_fps = atof_(fps_choices_str[fps_index]);
  37. settings.show_fps = game.show_fps;
  38. settings.always_backlight = strstr(yes_or_no_choices[screen_always_on_index], "Yes") != NULL;
  39. settings.frame_callback = frame_cb;
  40. settings.context = game_manager;
  41. GameEngine *engine = game_engine_alloc(settings);
  42. if (!engine)
  43. {
  44. FURI_LOG_E("Game", "Failed to allocate game engine");
  45. game_manager_free(game_manager);
  46. return -1;
  47. }
  48. game_manager_engine_set(game_manager, engine);
  49. // Allocate custom game context if needed
  50. void *game_context = NULL;
  51. if (game.context_size > 0)
  52. {
  53. game_context = malloc(game.context_size);
  54. game_manager_game_context_set(game_manager, game_context);
  55. }
  56. // Start the game
  57. game.start(game_manager, game_context);
  58. // 1) Run the engine
  59. game_engine_run(engine);
  60. // 2) Stop the game FIRST, so it can do any internal cleanup
  61. game.stop(game_context);
  62. // 3) Now free the engine
  63. game_engine_free(engine);
  64. // 4) Now free the manager
  65. game_manager_free(game_manager);
  66. // 5) Finally, free your custom context if it was allocated
  67. if (game_context)
  68. {
  69. free(game_context);
  70. }
  71. // 6) Check for leftover entities
  72. int32_t entities = entities_get_count();
  73. if (entities != 0)
  74. {
  75. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. static void error_draw(Canvas *canvas, DataLoaderModel *model)
  81. {
  82. if (canvas == NULL)
  83. {
  84. FURI_LOG_E(TAG, "error_draw - canvas is NULL");
  85. DEV_CRASH();
  86. return;
  87. }
  88. if (model->fhttp->last_response != NULL)
  89. {
  90. if (strstr(model->fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  91. {
  92. canvas_clear(canvas);
  93. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  94. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  95. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  96. }
  97. else if (strstr(model->fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  98. {
  99. canvas_clear(canvas);
  100. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  101. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  102. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  103. }
  104. else if (strstr(model->fhttp->last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  105. {
  106. canvas_clear(canvas);
  107. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  108. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  109. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  110. }
  111. else if (strstr(model->fhttp->last_response, "[PONG]") != NULL)
  112. {
  113. canvas_clear(canvas);
  114. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  115. }
  116. else
  117. {
  118. canvas_clear(canvas);
  119. FURI_LOG_E(TAG, "Received an error: %s", model->fhttp->last_response);
  120. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  121. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  122. }
  123. }
  124. else
  125. {
  126. canvas_clear(canvas);
  127. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  128. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  129. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  130. }
  131. }
  132. static bool alloc_about_view(void *context);
  133. static bool alloc_text_input_view(void *context, char *title);
  134. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  135. //
  136. static void wifi_settings_select(void *context, uint32_t index);
  137. static void updated_wifi_ssid(void *context);
  138. static void updated_wifi_pass(void *context);
  139. static void updated_username(void *context);
  140. static void updated_password(void *context);
  141. //
  142. static void fps_change(VariableItem *item);
  143. static void game_settings_select(void *context, uint32_t index);
  144. static void user_settings_select(void *context, uint32_t index);
  145. static void screen_on_change(VariableItem *item);
  146. static void sound_on_change(VariableItem *item);
  147. static void vibration_on_change(VariableItem *item);
  148. static void player_on_change(VariableItem *item);
  149. static void vgm_x_change(VariableItem *item);
  150. static void vgm_y_change(VariableItem *item);
  151. uint32_t callback_to_submenu(void *context)
  152. {
  153. UNUSED(context);
  154. return FlipWorldViewSubmenu;
  155. }
  156. static uint32_t callback_to_wifi_settings(void *context)
  157. {
  158. UNUSED(context);
  159. return FlipWorldViewVariableItemList;
  160. }
  161. static uint32_t callback_to_settings(void *context)
  162. {
  163. UNUSED(context);
  164. return FlipWorldViewSettings;
  165. }
  166. static void about_draw_callback(Canvas *canvas, void *model)
  167. {
  168. UNUSED(model);
  169. canvas_clear(canvas);
  170. // canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  171. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  172. // canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  173. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  174. canvas_draw_str(canvas, 0, 20, "Dev: JBlanked, codeallnight");
  175. canvas_draw_str(canvas, 0, 30, "GFX: the1anonlypr3");
  176. canvas_draw_str(canvas, 0, 40, "github.com/jblanked/FlipWorld");
  177. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  178. }
  179. // alloc
  180. static bool alloc_about_view(void *context)
  181. {
  182. FlipWorldApp *app = (FlipWorldApp *)context;
  183. if (!app)
  184. {
  185. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  186. return false;
  187. }
  188. if (!app->view_about)
  189. {
  190. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  191. {
  192. return false;
  193. }
  194. if (!app->view_about)
  195. {
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. static bool alloc_text_input_view(void *context, char *title)
  202. {
  203. FlipWorldApp *app = (FlipWorldApp *)context;
  204. if (!app)
  205. {
  206. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  207. return false;
  208. }
  209. if (!title)
  210. {
  211. FURI_LOG_E(TAG, "Title is NULL");
  212. return false;
  213. }
  214. app->text_input_buffer_size = 64;
  215. if (!app->text_input_buffer)
  216. {
  217. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  218. {
  219. return false;
  220. }
  221. }
  222. if (!app->text_input_temp_buffer)
  223. {
  224. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  225. {
  226. return false;
  227. }
  228. }
  229. if (!app->text_input)
  230. {
  231. if (!easy_flipper_set_uart_text_input(
  232. &app->text_input,
  233. FlipWorldViewTextInput,
  234. title,
  235. app->text_input_temp_buffer,
  236. app->text_input_buffer_size,
  237. is_str(title, "SSID") ? updated_wifi_ssid : is_str(title, "Password") ? updated_wifi_pass
  238. : is_str(title, "Username-Login") ? updated_username
  239. : updated_password,
  240. callback_to_wifi_settings,
  241. &app->view_dispatcher,
  242. app))
  243. {
  244. return false;
  245. }
  246. if (!app->text_input)
  247. {
  248. return false;
  249. }
  250. char ssid[64];
  251. char pass[64];
  252. char username[64];
  253. char password[64];
  254. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  255. {
  256. if (is_str(title, "SSID"))
  257. {
  258. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  259. }
  260. else if (is_str(title, "Password"))
  261. {
  262. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  263. }
  264. else if (is_str(title, "Username-Login"))
  265. {
  266. strncpy(app->text_input_temp_buffer, username, app->text_input_buffer_size);
  267. }
  268. else if (is_str(title, "Password-Login"))
  269. {
  270. strncpy(app->text_input_temp_buffer, password, app->text_input_buffer_size);
  271. }
  272. }
  273. }
  274. return true;
  275. }
  276. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  277. {
  278. FlipWorldApp *app = (FlipWorldApp *)context;
  279. if (!app)
  280. {
  281. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  282. return false;
  283. }
  284. char ssid[64];
  285. char pass[64];
  286. char username[64];
  287. char password[64];
  288. if (!app->variable_item_list)
  289. {
  290. switch (view_id)
  291. {
  292. case FlipWorldSubmenuIndexWiFiSettings:
  293. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
  294. {
  295. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  296. return false;
  297. }
  298. if (!app->variable_item_list)
  299. {
  300. FURI_LOG_E(TAG, "Variable item list is NULL");
  301. return false;
  302. }
  303. if (!app->variable_item_wifi_ssid)
  304. {
  305. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  306. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  307. }
  308. if (!app->variable_item_wifi_pass)
  309. {
  310. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  311. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  312. }
  313. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  314. {
  315. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  316. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  317. save_char("WiFi-SSID", ssid);
  318. save_char("WiFi-Password", pass);
  319. save_char("Flip-Social-Username", username);
  320. save_char("Flip-Social-Password", password);
  321. }
  322. break;
  323. case FlipWorldSubmenuIndexGameSettings:
  324. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_select, callback_to_settings, &app->view_dispatcher, app))
  325. {
  326. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  327. return false;
  328. }
  329. if (!app->variable_item_list)
  330. {
  331. FURI_LOG_E(TAG, "Variable item list is NULL");
  332. return false;
  333. }
  334. if (!app->variable_item_game_download_world)
  335. {
  336. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  337. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  338. }
  339. if (!app->variable_item_game_player_sprite)
  340. {
  341. app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, player_on_change, NULL);
  342. variable_item_set_current_value_index(app->variable_item_game_player_sprite, 1);
  343. variable_item_set_current_value_text(app->variable_item_game_player_sprite, player_sprite_choices[1]);
  344. }
  345. if (!app->variable_item_game_fps)
  346. {
  347. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, fps_change, NULL);
  348. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  349. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[0]);
  350. }
  351. if (!app->variable_item_game_vgm_x)
  352. {
  353. app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, vgm_x_change, NULL);
  354. variable_item_set_current_value_index(app->variable_item_game_vgm_x, 2);
  355. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[2]);
  356. }
  357. if (!app->variable_item_game_vgm_y)
  358. {
  359. app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, vgm_y_change, NULL);
  360. variable_item_set_current_value_index(app->variable_item_game_vgm_y, 2);
  361. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[2]);
  362. }
  363. if (!app->variable_item_game_screen_always_on)
  364. {
  365. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, screen_on_change, NULL);
  366. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  367. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[1]);
  368. }
  369. if (!app->variable_item_game_sound_on)
  370. {
  371. app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, sound_on_change, NULL);
  372. variable_item_set_current_value_index(app->variable_item_game_sound_on, 0);
  373. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[0]);
  374. }
  375. if (!app->variable_item_game_vibration_on)
  376. {
  377. app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, vibration_on_change, NULL);
  378. variable_item_set_current_value_index(app->variable_item_game_vibration_on, 0);
  379. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[0]);
  380. }
  381. char _game_player_sprite[8];
  382. if (load_char("Game-Player-Sprite", _game_player_sprite, sizeof(_game_player_sprite)))
  383. {
  384. int index = is_str(_game_player_sprite, "naked") ? 0 : is_str(_game_player_sprite, "sword") ? 1
  385. : is_str(_game_player_sprite, "axe") ? 2
  386. : is_str(_game_player_sprite, "bow") ? 3
  387. : 0;
  388. variable_item_set_current_value_index(app->variable_item_game_player_sprite, index);
  389. variable_item_set_current_value_text(
  390. app->variable_item_game_player_sprite,
  391. is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  392. }
  393. char _game_fps[8];
  394. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  395. {
  396. int index = is_str(_game_fps, "30") ? 0 : is_str(_game_fps, "60") ? 1
  397. : is_str(_game_fps, "120") ? 2
  398. : is_str(_game_fps, "240") ? 3
  399. : 0;
  400. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[index]);
  401. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  402. }
  403. char _game_vgm_x[8];
  404. if (load_char("Game-VGM-X", _game_vgm_x, sizeof(_game_vgm_x)))
  405. {
  406. int vgm_x = atoi(_game_vgm_x);
  407. int index = vgm_x == -2 ? 0 : vgm_x == -1 ? 1
  408. : vgm_x == 0 ? 2
  409. : vgm_x == 1 ? 3
  410. : vgm_x == 2 ? 4
  411. : vgm_x == 3 ? 5
  412. : vgm_x == 4 ? 6
  413. : vgm_x == 5 ? 7
  414. : vgm_x == 6 ? 8
  415. : vgm_x == 7 ? 9
  416. : vgm_x == 8 ? 10
  417. : vgm_x == 9 ? 11
  418. : vgm_x == 10 ? 12
  419. : 2;
  420. variable_item_set_current_value_index(app->variable_item_game_vgm_x, index);
  421. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[index]);
  422. }
  423. char _game_vgm_y[8];
  424. if (load_char("Game-VGM-Y", _game_vgm_y, sizeof(_game_vgm_y)))
  425. {
  426. int vgm_y = atoi(_game_vgm_y);
  427. int index = vgm_y == -2 ? 0 : vgm_y == -1 ? 1
  428. : vgm_y == 0 ? 2
  429. : vgm_y == 1 ? 3
  430. : vgm_y == 2 ? 4
  431. : vgm_y == 3 ? 5
  432. : vgm_y == 4 ? 6
  433. : vgm_y == 5 ? 7
  434. : vgm_y == 6 ? 8
  435. : vgm_y == 7 ? 9
  436. : vgm_y == 8 ? 10
  437. : vgm_y == 9 ? 11
  438. : vgm_y == 10 ? 12
  439. : 2;
  440. variable_item_set_current_value_index(app->variable_item_game_vgm_y, index);
  441. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[index]);
  442. }
  443. char _game_screen_always_on[8];
  444. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  445. {
  446. int index = is_str(_game_screen_always_on, "No") ? 0 : is_str(_game_screen_always_on, "Yes") ? 1
  447. : 0;
  448. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[index]);
  449. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  450. }
  451. char _game_sound_on[8];
  452. if (load_char("Game-Sound-On", _game_sound_on, sizeof(_game_sound_on)))
  453. {
  454. int index = is_str(_game_sound_on, "No") ? 0 : is_str(_game_sound_on, "Yes") ? 1
  455. : 0;
  456. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[index]);
  457. variable_item_set_current_value_index(app->variable_item_game_sound_on, index);
  458. }
  459. char _game_vibration_on[8];
  460. if (load_char("Game-Vibration-On", _game_vibration_on, sizeof(_game_vibration_on)))
  461. {
  462. int index = is_str(_game_vibration_on, "No") ? 0 : is_str(_game_vibration_on, "Yes") ? 1
  463. : 0;
  464. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[index]);
  465. variable_item_set_current_value_index(app->variable_item_game_vibration_on, index);
  466. }
  467. break;
  468. case FlipWorldSubmenuIndexUserSettings:
  469. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_select, callback_to_settings, &app->view_dispatcher, app))
  470. {
  471. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  472. return false;
  473. }
  474. if (!app->variable_item_list)
  475. {
  476. FURI_LOG_E(TAG, "Variable item list is NULL");
  477. return false;
  478. }
  479. // if logged in, show profile info, otherwise show login/register
  480. if (is_logged_in() || is_logged_in_to_flip_social())
  481. {
  482. if (!app->variable_item_user_username)
  483. {
  484. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  485. variable_item_set_current_value_text(app->variable_item_user_username, "");
  486. }
  487. if (!app->variable_item_user_password)
  488. {
  489. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  490. variable_item_set_current_value_text(app->variable_item_user_password, "");
  491. }
  492. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  493. {
  494. variable_item_set_current_value_text(app->variable_item_user_username, username);
  495. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  496. }
  497. }
  498. else
  499. {
  500. if (!app->variable_item_user_username)
  501. {
  502. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  503. variable_item_set_current_value_text(app->variable_item_user_username, "");
  504. }
  505. if (!app->variable_item_user_password)
  506. {
  507. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  508. variable_item_set_current_value_text(app->variable_item_user_password, "");
  509. }
  510. }
  511. break;
  512. }
  513. }
  514. return true;
  515. }
  516. static bool alloc_submenu_settings(void *context)
  517. {
  518. FlipWorldApp *app = (FlipWorldApp *)context;
  519. if (!app)
  520. {
  521. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  522. return false;
  523. }
  524. if (!app->submenu_settings)
  525. {
  526. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  527. {
  528. return NULL;
  529. }
  530. if (!app->submenu_settings)
  531. {
  532. return false;
  533. }
  534. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  535. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  536. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  537. }
  538. return true;
  539. }
  540. // free
  541. static void free_about_view(void *context)
  542. {
  543. FlipWorldApp *app = (FlipWorldApp *)context;
  544. if (!app)
  545. {
  546. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  547. return;
  548. }
  549. if (app->view_about)
  550. {
  551. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  552. view_free(app->view_about);
  553. app->view_about = NULL;
  554. }
  555. }
  556. static void free_text_input_view(void *context)
  557. {
  558. FlipWorldApp *app = (FlipWorldApp *)context;
  559. if (!app)
  560. {
  561. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  562. return;
  563. }
  564. if (app->text_input)
  565. {
  566. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  567. uart_text_input_free(app->text_input);
  568. app->text_input = NULL;
  569. }
  570. if (app->text_input_buffer)
  571. {
  572. free(app->text_input_buffer);
  573. app->text_input_buffer = NULL;
  574. }
  575. if (app->text_input_temp_buffer)
  576. {
  577. free(app->text_input_temp_buffer);
  578. app->text_input_temp_buffer = NULL;
  579. }
  580. }
  581. static void free_variable_item_list(void *context)
  582. {
  583. FlipWorldApp *app = (FlipWorldApp *)context;
  584. if (!app)
  585. {
  586. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  587. return;
  588. }
  589. if (app->variable_item_list)
  590. {
  591. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  592. variable_item_list_free(app->variable_item_list);
  593. app->variable_item_list = NULL;
  594. }
  595. if (app->variable_item_wifi_ssid)
  596. {
  597. free(app->variable_item_wifi_ssid);
  598. app->variable_item_wifi_ssid = NULL;
  599. }
  600. if (app->variable_item_wifi_pass)
  601. {
  602. free(app->variable_item_wifi_pass);
  603. app->variable_item_wifi_pass = NULL;
  604. }
  605. if (app->variable_item_game_fps)
  606. {
  607. free(app->variable_item_game_fps);
  608. app->variable_item_game_fps = NULL;
  609. }
  610. if (app->variable_item_game_screen_always_on)
  611. {
  612. free(app->variable_item_game_screen_always_on);
  613. app->variable_item_game_screen_always_on = NULL;
  614. }
  615. if (app->variable_item_game_download_world)
  616. {
  617. free(app->variable_item_game_download_world);
  618. app->variable_item_game_download_world = NULL;
  619. }
  620. if (app->variable_item_game_sound_on)
  621. {
  622. free(app->variable_item_game_sound_on);
  623. app->variable_item_game_sound_on = NULL;
  624. }
  625. if (app->variable_item_game_vibration_on)
  626. {
  627. free(app->variable_item_game_vibration_on);
  628. app->variable_item_game_vibration_on = NULL;
  629. }
  630. if (app->variable_item_game_player_sprite)
  631. {
  632. free(app->variable_item_game_player_sprite);
  633. app->variable_item_game_player_sprite = NULL;
  634. }
  635. if (app->variable_item_game_vgm_x)
  636. {
  637. free(app->variable_item_game_vgm_x);
  638. app->variable_item_game_vgm_x = NULL;
  639. }
  640. if (app->variable_item_game_vgm_y)
  641. {
  642. free(app->variable_item_game_vgm_y);
  643. app->variable_item_game_vgm_y = NULL;
  644. }
  645. if (app->variable_item_user_username)
  646. {
  647. free(app->variable_item_user_username);
  648. app->variable_item_user_username = NULL;
  649. }
  650. if (app->variable_item_user_password)
  651. {
  652. free(app->variable_item_user_password);
  653. app->variable_item_user_password = NULL;
  654. }
  655. }
  656. static void free_submenu_settings(void *context)
  657. {
  658. FlipWorldApp *app = (FlipWorldApp *)context;
  659. if (!app)
  660. {
  661. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  662. return;
  663. }
  664. if (app->submenu_settings)
  665. {
  666. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  667. submenu_free(app->submenu_settings);
  668. app->submenu_settings = NULL;
  669. }
  670. }
  671. static FuriThreadId thread_id;
  672. static bool game_thread_running = false;
  673. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  674. {
  675. FlipWorldApp *app = (FlipWorldApp *)context;
  676. if (!app)
  677. {
  678. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  679. return;
  680. }
  681. if (should_free_variable_item_list)
  682. {
  683. free_variable_item_list(app);
  684. }
  685. free_about_view(app);
  686. free_text_input_view(app);
  687. // free game thread
  688. if (game_thread_running)
  689. {
  690. game_thread_running = false;
  691. furi_thread_flags_set(thread_id, WorkerEvtStop);
  692. furi_thread_free(thread_id);
  693. }
  694. if (should_free_submenu_settings)
  695. free_submenu_settings(app);
  696. }
  697. static bool fetch_world_list(FlipperHTTP *fhttp)
  698. {
  699. if (!fhttp)
  700. {
  701. FURI_LOG_E(TAG, "fhttp is NULL");
  702. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  703. return false;
  704. }
  705. // Create the directory for saving worlds
  706. char directory_path[128];
  707. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  708. // Create the directory
  709. Storage *storage = furi_record_open(RECORD_STORAGE);
  710. storage_common_mkdir(storage, directory_path);
  711. // free storage
  712. furi_record_close(RECORD_STORAGE);
  713. snprintf(
  714. fhttp->file_path,
  715. sizeof(fhttp->file_path),
  716. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  717. fhttp->save_received_data = true;
  718. return flipper_http_get_request_with_headers(fhttp, "https://www.flipsocial.net/api/world/v3/list/10/", "{\"Content-Type\":\"application/json\"}");
  719. }
  720. // we will load the palyer stats from the API and save them
  721. // in player_spawn game method, it will load the player stats that we saved
  722. static bool fetch_player_stats(FlipperHTTP *fhttp)
  723. {
  724. if (!fhttp)
  725. {
  726. FURI_LOG_E(TAG, "fhttp is NULL");
  727. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  728. return false;
  729. }
  730. char username[64];
  731. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  732. {
  733. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  734. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  735. return false;
  736. }
  737. char url[128];
  738. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/game-stats/%s/", username);
  739. snprintf(
  740. fhttp->file_path,
  741. sizeof(fhttp->file_path),
  742. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  743. fhttp->save_received_data = true;
  744. return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  745. }
  746. static bool start_game_thread(void *context)
  747. {
  748. FlipWorldApp *app = (FlipWorldApp *)context;
  749. if (!app)
  750. {
  751. FURI_LOG_E(TAG, "app is NULL");
  752. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  753. return false;
  754. }
  755. // free game thread
  756. if (game_thread_running)
  757. {
  758. game_thread_running = false;
  759. furi_thread_flags_set(thread_id, WorkerEvtStop);
  760. furi_thread_free(thread_id);
  761. }
  762. // start game thread
  763. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  764. if (!thread)
  765. {
  766. FURI_LOG_E(TAG, "Failed to allocate game thread");
  767. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  768. return false;
  769. }
  770. furi_thread_start(thread);
  771. thread_id = furi_thread_get_id(thread);
  772. game_thread_running = true;
  773. return true;
  774. }
  775. // combine register, login, and world list fetch into one function to switch to the loader view
  776. static bool _fetch_game(DataLoaderModel *model)
  777. {
  778. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  779. if (!app)
  780. {
  781. FURI_LOG_E(TAG, "app is NULL");
  782. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  783. return false;
  784. }
  785. if (model->request_index == 0)
  786. {
  787. // login
  788. char username[64];
  789. char password[64];
  790. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  791. {
  792. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  793. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  794. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  795. return false;
  796. }
  797. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  798. {
  799. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  800. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  801. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  802. return false;
  803. }
  804. char payload[256];
  805. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  806. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  807. }
  808. else if (model->request_index == 1)
  809. {
  810. // check if login was successful
  811. char is_logged_in[8];
  812. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  813. {
  814. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  815. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  816. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  817. return false;
  818. }
  819. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  820. {
  821. // register
  822. char username[64];
  823. char password[64];
  824. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  825. {
  826. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  827. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  828. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  829. return false;
  830. }
  831. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  832. {
  833. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  834. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  835. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  836. return false;
  837. }
  838. char payload[172];
  839. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  840. model->title = "Registering...";
  841. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  842. }
  843. else
  844. {
  845. model->title = "Fetching World List..";
  846. return fetch_world_list(model->fhttp);
  847. }
  848. }
  849. else if (model->request_index == 2)
  850. {
  851. model->title = "Fetching World List..";
  852. return fetch_world_list(model->fhttp);
  853. }
  854. else if (model->request_index == 3)
  855. {
  856. snprintf(
  857. model->fhttp->file_path,
  858. sizeof(model->fhttp->file_path),
  859. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  860. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  861. if (!world_list)
  862. {
  863. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  864. FURI_LOG_E(TAG, "Failed to load world list");
  865. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  866. return false;
  867. }
  868. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  869. if (!first_world)
  870. {
  871. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  872. FURI_LOG_E(TAG, "Failed to get first world");
  873. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  874. furi_string_free(world_list);
  875. return false;
  876. }
  877. if (world_exists(furi_string_get_cstr(first_world)))
  878. {
  879. furi_string_free(world_list);
  880. furi_string_free(first_world);
  881. if (!start_game_thread(app))
  882. {
  883. FURI_LOG_E(TAG, "Failed to start game thread");
  884. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  885. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  886. return "Failed to start game thread";
  887. }
  888. return true;
  889. }
  890. snprintf(
  891. model->fhttp->file_path,
  892. sizeof(model->fhttp->file_path),
  893. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  894. model->fhttp->save_received_data = true;
  895. char url[128];
  896. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v3/get/world/%s/", furi_string_get_cstr(first_world));
  897. furi_string_free(world_list);
  898. furi_string_free(first_world);
  899. return flipper_http_get_request_with_headers(model->fhttp, url, "{\"Content-Type\":\"application/json\"}");
  900. }
  901. FURI_LOG_E(TAG, "Unknown request index");
  902. return false;
  903. }
  904. static char *_parse_game(DataLoaderModel *model)
  905. {
  906. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  907. if (model->request_index == 0)
  908. {
  909. if (!model->fhttp->last_response)
  910. {
  911. save_char("is_logged_in", "false");
  912. // Go back to the main menu
  913. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  914. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  915. return "Response is empty...";
  916. }
  917. // Check for successful conditions
  918. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  919. {
  920. save_char("is_logged_in", "true");
  921. model->title = "Login successful!";
  922. model->title = "Fetching World List..";
  923. return "Login successful!";
  924. }
  925. // Check if user not found
  926. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  927. {
  928. save_char("is_logged_in", "false");
  929. model->title = "Registering...";
  930. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  931. }
  932. // If not success, not found, check length conditions
  933. size_t resp_len = strlen(model->fhttp->last_response);
  934. if (resp_len == 0 || resp_len > 127)
  935. {
  936. // Empty or too long means failed login
  937. save_char("is_logged_in", "false");
  938. // Go back to the main menu
  939. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  940. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  941. return "Failed to login...";
  942. }
  943. // Handle any other unknown response as a failure
  944. save_char("is_logged_in", "false");
  945. // Go back to the main menu
  946. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  947. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  948. return "Failed to login...";
  949. }
  950. else if (model->request_index == 1)
  951. {
  952. if (is_str(model->title, "Registering..."))
  953. {
  954. // check registration response
  955. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  956. {
  957. save_char("is_logged_in", "true");
  958. char username[64];
  959. char password[64];
  960. // load the username and password, then save them
  961. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  962. {
  963. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  964. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  965. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  966. return "Failed to load Flip-Social-Username";
  967. }
  968. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  969. {
  970. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  971. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  972. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  973. return "Failed to load Flip-Social-Password";
  974. }
  975. // load wifi ssid,pass then save
  976. char ssid[64];
  977. char pass[64];
  978. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  979. {
  980. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  981. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  982. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  983. return "Failed to load WiFi-SSID";
  984. }
  985. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  986. {
  987. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  988. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  989. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  990. return "Failed to load WiFi-Password";
  991. }
  992. save_settings(ssid, pass, username, password);
  993. model->title = "Fetching World List..";
  994. return "Account created!";
  995. }
  996. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  997. {
  998. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  999. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1000. return "Please enter your credentials.";
  1001. }
  1002. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  1003. {
  1004. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  1005. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1006. return "Username already exists.";
  1007. }
  1008. else
  1009. {
  1010. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  1011. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1012. return "Registration failed...";
  1013. }
  1014. }
  1015. else
  1016. {
  1017. if (!start_game_thread(app))
  1018. {
  1019. FURI_LOG_E(TAG, "Failed to start game thread");
  1020. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1021. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1022. return "Failed to start game thread";
  1023. }
  1024. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1025. }
  1026. }
  1027. else if (model->request_index == 2)
  1028. {
  1029. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1030. }
  1031. else if (model->request_index == 3)
  1032. {
  1033. if (!start_game_thread(app))
  1034. {
  1035. FURI_LOG_E(TAG, "Failed to start game thread");
  1036. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1037. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1038. return "Failed to start game thread";
  1039. }
  1040. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1041. }
  1042. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1043. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1044. return "Unknown error";
  1045. }
  1046. static void switch_to_view_get_game(FlipWorldApp *app)
  1047. {
  1048. generic_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1049. }
  1050. void callback_submenu_choices(void *context, uint32_t index)
  1051. {
  1052. FlipWorldApp *app = (FlipWorldApp *)context;
  1053. if (!app)
  1054. {
  1055. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1056. return;
  1057. }
  1058. switch (index)
  1059. {
  1060. case FlipWorldSubmenuIndexRun:
  1061. free_all_views(app, true, true);
  1062. if (!is_enough_heap(45000)) // lowered from 60k to 45k since we saved 15k bytes
  1063. {
  1064. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1065. return;
  1066. }
  1067. // check if logged in
  1068. if (is_logged_in() || is_logged_in_to_flip_social())
  1069. {
  1070. FlipperHTTP *fhttp = flipper_http_alloc();
  1071. if (!fhttp)
  1072. {
  1073. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1074. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1075. return;
  1076. }
  1077. bool fetch_world_list_i()
  1078. {
  1079. return fetch_world_list(fhttp);
  1080. }
  1081. bool parse_world_list_i()
  1082. {
  1083. return fhttp->state != ISSUE;
  1084. }
  1085. bool fetch_player_stats_i()
  1086. {
  1087. return fetch_player_stats(fhttp);
  1088. }
  1089. Loading *loading;
  1090. int32_t loading_view_id = 987654321; // Random ID
  1091. loading = loading_alloc();
  1092. if (!loading)
  1093. {
  1094. FURI_LOG_E(HTTP_TAG, "Failed to allocate loading");
  1095. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1096. flipper_http_free(fhttp);
  1097. return;
  1098. }
  1099. view_dispatcher_add_view(app->view_dispatcher, loading_view_id, loading_get_view(loading));
  1100. // Switch to the loading view
  1101. view_dispatcher_switch_to_view(app->view_dispatcher, loading_view_id);
  1102. // Make the request
  1103. if (!flipper_http_process_response_async(fhttp, fetch_world_list_i, parse_world_list_i) ||
  1104. !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
  1105. {
  1106. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1107. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1108. view_dispatcher_remove_view(app->view_dispatcher, loading_view_id);
  1109. loading_free(loading);
  1110. flipper_http_free(fhttp);
  1111. }
  1112. else
  1113. {
  1114. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1115. view_dispatcher_remove_view(app->view_dispatcher, loading_view_id);
  1116. loading_free(loading);
  1117. flipper_http_free(fhttp);
  1118. }
  1119. if (!start_game_thread(app))
  1120. {
  1121. FURI_LOG_E(TAG, "Failed to start game thread");
  1122. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1123. return;
  1124. }
  1125. easy_flipper_dialog("Starting Game", "Please wait...");
  1126. }
  1127. else
  1128. {
  1129. switch_to_view_get_game(app);
  1130. }
  1131. break;
  1132. case FlipWorldSubmenuIndexAbout:
  1133. free_all_views(app, true, true);
  1134. if (!alloc_about_view(app))
  1135. {
  1136. FURI_LOG_E(TAG, "Failed to allocate about view");
  1137. return;
  1138. }
  1139. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  1140. break;
  1141. case FlipWorldSubmenuIndexSettings:
  1142. free_all_views(app, true, true);
  1143. if (!alloc_submenu_settings(app))
  1144. {
  1145. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1146. return;
  1147. }
  1148. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1149. break;
  1150. case FlipWorldSubmenuIndexWiFiSettings:
  1151. free_all_views(app, true, false);
  1152. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1153. {
  1154. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1155. return;
  1156. }
  1157. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1158. break;
  1159. case FlipWorldSubmenuIndexGameSettings:
  1160. free_all_views(app, true, false);
  1161. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1162. {
  1163. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1164. return;
  1165. }
  1166. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1167. break;
  1168. case FlipWorldSubmenuIndexUserSettings:
  1169. free_all_views(app, true, false);
  1170. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1171. {
  1172. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1173. return;
  1174. }
  1175. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. }
  1181. static void updated_wifi_ssid(void *context)
  1182. {
  1183. FlipWorldApp *app = (FlipWorldApp *)context;
  1184. if (!app)
  1185. {
  1186. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1187. return;
  1188. }
  1189. // store the entered text
  1190. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1191. // Ensure null-termination
  1192. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1193. // save the setting
  1194. save_char("WiFi-SSID", app->text_input_buffer);
  1195. // update the variable item text
  1196. if (app->variable_item_wifi_ssid)
  1197. {
  1198. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1199. // get value of password
  1200. char pass[64];
  1201. char username[64];
  1202. char password[64];
  1203. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1204. {
  1205. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1206. {
  1207. // save the settings
  1208. load_char("Flip-Social-Username", username, sizeof(username));
  1209. load_char("Flip-Social-Password", password, sizeof(password));
  1210. save_settings(app->text_input_buffer, pass, username, password);
  1211. // initialize the http
  1212. FlipperHTTP *fhttp = flipper_http_alloc();
  1213. if (fhttp)
  1214. {
  1215. // save the wifi if the device is connected
  1216. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1217. {
  1218. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1219. }
  1220. // free the resources
  1221. flipper_http_free(fhttp);
  1222. }
  1223. else
  1224. {
  1225. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1226. }
  1227. }
  1228. }
  1229. }
  1230. // switch to the settings view
  1231. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1232. }
  1233. static void updated_wifi_pass(void *context)
  1234. {
  1235. FlipWorldApp *app = (FlipWorldApp *)context;
  1236. if (!app)
  1237. {
  1238. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1239. return;
  1240. }
  1241. // store the entered text
  1242. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1243. // Ensure null-termination
  1244. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1245. // save the setting
  1246. save_char("WiFi-Password", app->text_input_buffer);
  1247. // update the variable item text
  1248. if (app->variable_item_wifi_pass)
  1249. {
  1250. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1251. }
  1252. // get value of ssid
  1253. char ssid[64];
  1254. char username[64];
  1255. char password[64];
  1256. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1257. {
  1258. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1259. {
  1260. // save the settings
  1261. load_char("Flip-Social-Username", username, sizeof(username));
  1262. load_char("Flip-Social-Password", password, sizeof(password));
  1263. save_settings(ssid, app->text_input_buffer, username, password);
  1264. // initialize the http
  1265. FlipperHTTP *fhttp = flipper_http_alloc();
  1266. if (fhttp)
  1267. {
  1268. // save the wifi if the device is connected
  1269. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1270. {
  1271. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1272. }
  1273. // free the resources
  1274. flipper_http_free(fhttp);
  1275. }
  1276. else
  1277. {
  1278. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1279. }
  1280. }
  1281. }
  1282. // switch to the settings view
  1283. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1284. }
  1285. static void updated_username(void *context)
  1286. {
  1287. FlipWorldApp *app = (FlipWorldApp *)context;
  1288. if (!app)
  1289. {
  1290. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1291. return;
  1292. }
  1293. // store the entered text
  1294. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1295. // Ensure null-termination
  1296. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1297. // save the setting
  1298. save_char("Flip-Social-Username", app->text_input_buffer);
  1299. // update the variable item text
  1300. if (app->variable_item_user_username)
  1301. {
  1302. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1303. }
  1304. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1305. }
  1306. static void updated_password(void *context)
  1307. {
  1308. FlipWorldApp *app = (FlipWorldApp *)context;
  1309. if (!app)
  1310. {
  1311. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1312. return;
  1313. }
  1314. // store the entered text
  1315. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1316. // Ensure null-termination
  1317. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1318. // save the setting
  1319. save_char("Flip-Social-Password", app->text_input_buffer);
  1320. // update the variable item text
  1321. if (app->variable_item_user_password)
  1322. {
  1323. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1324. }
  1325. // get value of username
  1326. char username[64];
  1327. char ssid[64];
  1328. char pass[64];
  1329. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1330. {
  1331. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1332. {
  1333. // save the settings
  1334. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1335. load_char("WiFi-Password", pass, sizeof(pass));
  1336. save_settings(ssid, pass, username, app->text_input_buffer);
  1337. }
  1338. }
  1339. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1340. }
  1341. static void wifi_settings_select(void *context, uint32_t index)
  1342. {
  1343. FlipWorldApp *app = (FlipWorldApp *)context;
  1344. if (!app)
  1345. {
  1346. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1347. return;
  1348. }
  1349. char ssid[64];
  1350. char pass[64];
  1351. char username[64];
  1352. char password[64];
  1353. switch (index)
  1354. {
  1355. case 0: // Input SSID
  1356. free_all_views(app, false, false);
  1357. if (!alloc_text_input_view(app, "SSID"))
  1358. {
  1359. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1360. return;
  1361. }
  1362. // load SSID
  1363. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1364. {
  1365. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1366. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1367. }
  1368. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1369. break;
  1370. case 1: // Input Password
  1371. free_all_views(app, false, false);
  1372. if (!alloc_text_input_view(app, "Password"))
  1373. {
  1374. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1375. return;
  1376. }
  1377. // load password
  1378. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1379. {
  1380. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1381. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1382. }
  1383. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1384. break;
  1385. default:
  1386. FURI_LOG_E(TAG, "Unknown configuration item index");
  1387. break;
  1388. }
  1389. }
  1390. static void fps_change(VariableItem *item)
  1391. {
  1392. uint8_t index = variable_item_get_current_value_index(item);
  1393. fps_index = index;
  1394. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1395. variable_item_set_current_value_index(item, index);
  1396. save_char("Game-FPS", fps_choices_str[index]);
  1397. }
  1398. static void screen_on_change(VariableItem *item)
  1399. {
  1400. uint8_t index = variable_item_get_current_value_index(item);
  1401. screen_always_on_index = index;
  1402. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1403. variable_item_set_current_value_index(item, index);
  1404. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1405. }
  1406. static void sound_on_change(VariableItem *item)
  1407. {
  1408. uint8_t index = variable_item_get_current_value_index(item);
  1409. sound_on_index = index;
  1410. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1411. variable_item_set_current_value_index(item, index);
  1412. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1413. }
  1414. static void vibration_on_change(VariableItem *item)
  1415. {
  1416. uint8_t index = variable_item_get_current_value_index(item);
  1417. vibration_on_index = index;
  1418. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1419. variable_item_set_current_value_index(item, index);
  1420. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1421. }
  1422. static void player_on_change(VariableItem *item)
  1423. {
  1424. uint8_t index = variable_item_get_current_value_index(item);
  1425. player_sprite_index = index;
  1426. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1427. variable_item_set_current_value_index(item, index);
  1428. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1429. }
  1430. static void vgm_x_change(VariableItem *item)
  1431. {
  1432. uint8_t index = variable_item_get_current_value_index(item);
  1433. vgm_x_index = index;
  1434. variable_item_set_current_value_text(item, vgm_levels[index]);
  1435. variable_item_set_current_value_index(item, index);
  1436. save_char("Game-VGM-X", vgm_levels[index]);
  1437. }
  1438. static void vgm_y_change(VariableItem *item)
  1439. {
  1440. uint8_t index = variable_item_get_current_value_index(item);
  1441. vgm_y_index = index;
  1442. variable_item_set_current_value_text(item, vgm_levels[index]);
  1443. variable_item_set_current_value_index(item, index);
  1444. save_char("Game-VGM-Y", vgm_levels[index]);
  1445. }
  1446. static bool _fetch_worlds(DataLoaderModel *model)
  1447. {
  1448. if (!model || !model->fhttp)
  1449. {
  1450. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1451. return false;
  1452. }
  1453. // Create the directory for saving settings
  1454. char directory_path[256];
  1455. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1456. // Create the directory
  1457. Storage *storage = furi_record_open(RECORD_STORAGE);
  1458. storage_common_mkdir(storage, directory_path);
  1459. furi_record_close(RECORD_STORAGE);
  1460. snprintf(
  1461. model->fhttp->file_path,
  1462. sizeof(model->fhttp->file_path),
  1463. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1464. model->fhttp->save_received_data = true;
  1465. return flipper_http_get_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/world/v3/get/10/", "{\"Content-Type\":\"application/json\"}");
  1466. }
  1467. static char *_parse_worlds(DataLoaderModel *model)
  1468. {
  1469. UNUSED(model);
  1470. return "World Pack Installed";
  1471. }
  1472. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1473. {
  1474. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1475. }
  1476. static void game_settings_select(void *context, uint32_t index)
  1477. {
  1478. FlipWorldApp *app = (FlipWorldApp *)context;
  1479. if (!app)
  1480. {
  1481. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1482. return;
  1483. }
  1484. switch (index)
  1485. {
  1486. case 0: // Download all world data as one huge json
  1487. switch_to_view_get_worlds(app);
  1488. case 1: // Player Sprite
  1489. break;
  1490. case 2: // Change FPS
  1491. break;
  1492. case 3: // VGM X
  1493. break;
  1494. case 4: // VGM Y
  1495. break;
  1496. case 5: // Screen Always On
  1497. break;
  1498. case 6: // Sound On
  1499. break;
  1500. case 7: // Vibration On
  1501. break;
  1502. }
  1503. }
  1504. static void user_settings_select(void *context, uint32_t index)
  1505. {
  1506. FlipWorldApp *app = (FlipWorldApp *)context;
  1507. if (!app)
  1508. {
  1509. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1510. return;
  1511. }
  1512. switch (index)
  1513. {
  1514. case 0: // Username
  1515. free_all_views(app, false, false);
  1516. if (!alloc_text_input_view(app, "Username-Login"))
  1517. {
  1518. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1519. return;
  1520. }
  1521. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1522. break;
  1523. case 1: // Password
  1524. free_all_views(app, false, false);
  1525. if (!alloc_text_input_view(app, "Password-Login"))
  1526. {
  1527. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1528. return;
  1529. }
  1530. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1531. break;
  1532. }
  1533. }
  1534. static void widget_set_text(char *message, Widget **widget)
  1535. {
  1536. if (widget == NULL)
  1537. {
  1538. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1539. DEV_CRASH();
  1540. return;
  1541. }
  1542. if (message == NULL)
  1543. {
  1544. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1545. DEV_CRASH();
  1546. return;
  1547. }
  1548. widget_reset(*widget);
  1549. uint32_t message_length = strlen(message); // Length of the message
  1550. uint32_t i = 0; // Index tracker
  1551. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1552. char *formatted_message; // Buffer to hold the final formatted message
  1553. // Allocate buffer with double the message length plus one for safety
  1554. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1555. {
  1556. return;
  1557. }
  1558. while (i < message_length)
  1559. {
  1560. uint32_t max_line_length = 31; // Maximum characters per line
  1561. uint32_t remaining_length = message_length - i; // Remaining characters
  1562. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1563. // Check for newline character within the current segment
  1564. uint32_t newline_pos = i;
  1565. bool found_newline = false;
  1566. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1567. {
  1568. if (message[newline_pos] == '\n')
  1569. {
  1570. found_newline = true;
  1571. break;
  1572. }
  1573. }
  1574. if (found_newline)
  1575. {
  1576. // If newline found, set line_length up to the newline
  1577. line_length = newline_pos - i;
  1578. }
  1579. // Temporary buffer to hold the current line
  1580. char line[32];
  1581. strncpy(line, message + i, line_length);
  1582. line[line_length] = '\0';
  1583. // If newline was found, skip it for the next iteration
  1584. if (found_newline)
  1585. {
  1586. i += line_length + 1; // +1 to skip the '\n' character
  1587. }
  1588. else
  1589. {
  1590. // Check if the line ends in the middle of a word and adjust accordingly
  1591. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1592. {
  1593. // Find the last space within the current line to avoid breaking a word
  1594. char *last_space = strrchr(line, ' ');
  1595. if (last_space != NULL)
  1596. {
  1597. // Adjust the line_length to avoid cutting the word
  1598. line_length = last_space - line;
  1599. line[line_length] = '\0'; // Null-terminate at the space
  1600. }
  1601. }
  1602. // Move the index forward by the determined line_length
  1603. i += line_length;
  1604. // Skip any spaces at the beginning of the next line
  1605. while (i < message_length && message[i] == ' ')
  1606. {
  1607. i++;
  1608. }
  1609. }
  1610. // Manually copy the fixed line into the formatted_message buffer
  1611. for (uint32_t j = 0; j < line_length; j++)
  1612. {
  1613. formatted_message[formatted_index++] = line[j];
  1614. }
  1615. // Add a newline character for line spacing
  1616. formatted_message[formatted_index++] = '\n';
  1617. }
  1618. // Null-terminate the formatted_message
  1619. formatted_message[formatted_index] = '\0';
  1620. // Add the formatted message to the widget
  1621. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1622. }
  1623. void loader_draw_callback(Canvas *canvas, void *model)
  1624. {
  1625. if (!canvas || !model)
  1626. {
  1627. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  1628. return;
  1629. }
  1630. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1631. SerialState http_state = data_loader_model->fhttp->state;
  1632. DataState data_state = data_loader_model->data_state;
  1633. char *title = data_loader_model->title;
  1634. canvas_set_font(canvas, FontSecondary);
  1635. if (http_state == INACTIVE)
  1636. {
  1637. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1638. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1639. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1640. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1641. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1642. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1643. return;
  1644. }
  1645. if (data_state == DataStateError || data_state == DataStateParseError)
  1646. {
  1647. error_draw(canvas, data_loader_model);
  1648. return;
  1649. }
  1650. canvas_draw_str(canvas, 0, 7, title);
  1651. canvas_draw_str(canvas, 0, 17, "Loading...");
  1652. if (data_state == DataStateInitial)
  1653. {
  1654. return;
  1655. }
  1656. if (http_state == SENDING)
  1657. {
  1658. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1659. return;
  1660. }
  1661. if (http_state == RECEIVING || data_state == DataStateRequested)
  1662. {
  1663. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1664. return;
  1665. }
  1666. if (http_state == IDLE && data_state == DataStateReceived)
  1667. {
  1668. canvas_draw_str(canvas, 0, 27, "Processing...");
  1669. return;
  1670. }
  1671. if (http_state == IDLE && data_state == DataStateParsed)
  1672. {
  1673. canvas_draw_str(canvas, 0, 27, "Processed...");
  1674. return;
  1675. }
  1676. }
  1677. static void loader_process_callback(void *context)
  1678. {
  1679. if (context == NULL)
  1680. {
  1681. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  1682. DEV_CRASH();
  1683. return;
  1684. }
  1685. FlipWorldApp *app = (FlipWorldApp *)context;
  1686. View *view = app->view_loader;
  1687. DataState current_data_state;
  1688. DataLoaderModel *loader_model = NULL;
  1689. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1690. if (!loader_model || !loader_model->fhttp)
  1691. {
  1692. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1693. DEV_CRASH();
  1694. return;
  1695. }
  1696. if (current_data_state == DataStateInitial)
  1697. {
  1698. with_view_model(
  1699. view,
  1700. DataLoaderModel * model,
  1701. {
  1702. model->data_state = DataStateRequested;
  1703. DataLoaderFetch fetch = model->fetcher;
  1704. if (fetch == NULL)
  1705. {
  1706. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1707. model->data_state = DataStateError;
  1708. return;
  1709. }
  1710. // Clear any previous responses
  1711. strncpy(model->fhttp->last_response, "", 1);
  1712. bool request_status = fetch(model);
  1713. if (!request_status)
  1714. {
  1715. model->data_state = DataStateError;
  1716. }
  1717. },
  1718. true);
  1719. }
  1720. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1721. {
  1722. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1723. {
  1724. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1725. {
  1726. FURI_LOG_DEV(TAG, "PONG received.");
  1727. }
  1728. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  1729. {
  1730. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1731. }
  1732. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  1733. {
  1734. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1735. }
  1736. else if (strlen(loader_model->fhttp->last_response))
  1737. {
  1738. // Still waiting on response
  1739. }
  1740. else
  1741. {
  1742. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1743. }
  1744. }
  1745. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1746. {
  1747. // continue waiting
  1748. }
  1749. else if (loader_model->fhttp->state == INACTIVE)
  1750. {
  1751. // inactive. try again
  1752. }
  1753. else if (loader_model->fhttp->state == ISSUE)
  1754. {
  1755. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1756. }
  1757. else
  1758. {
  1759. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1760. DEV_CRASH();
  1761. }
  1762. }
  1763. else if (current_data_state == DataStateReceived)
  1764. {
  1765. with_view_model(
  1766. view,
  1767. DataLoaderModel * model,
  1768. {
  1769. char *data_text;
  1770. if (model->parser == NULL)
  1771. {
  1772. data_text = NULL;
  1773. FURI_LOG_DEV(TAG, "Parser is NULL");
  1774. DEV_CRASH();
  1775. }
  1776. else
  1777. {
  1778. data_text = model->parser(model);
  1779. }
  1780. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1781. model->data_text = data_text;
  1782. if (data_text == NULL)
  1783. {
  1784. model->data_state = DataStateParseError;
  1785. }
  1786. else
  1787. {
  1788. model->data_state = DataStateParsed;
  1789. }
  1790. },
  1791. true);
  1792. }
  1793. else if (current_data_state == DataStateParsed)
  1794. {
  1795. with_view_model(
  1796. view,
  1797. DataLoaderModel * model,
  1798. {
  1799. if (++model->request_index < model->request_count)
  1800. {
  1801. model->data_state = DataStateInitial;
  1802. }
  1803. else
  1804. {
  1805. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1806. if (model->data_text != NULL)
  1807. {
  1808. free(model->data_text);
  1809. model->data_text = NULL;
  1810. }
  1811. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1812. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1813. }
  1814. },
  1815. true);
  1816. }
  1817. }
  1818. static void loader_timer_callback(void *context)
  1819. {
  1820. if (context == NULL)
  1821. {
  1822. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  1823. DEV_CRASH();
  1824. return;
  1825. }
  1826. FlipWorldApp *app = (FlipWorldApp *)context;
  1827. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1828. }
  1829. static void loader_on_enter(void *context)
  1830. {
  1831. if (context == NULL)
  1832. {
  1833. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  1834. DEV_CRASH();
  1835. return;
  1836. }
  1837. FlipWorldApp *app = (FlipWorldApp *)context;
  1838. View *view = app->view_loader;
  1839. with_view_model(
  1840. view,
  1841. DataLoaderModel * model,
  1842. {
  1843. view_set_previous_callback(view, model->back_callback);
  1844. if (model->timer == NULL)
  1845. {
  1846. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  1847. }
  1848. furi_timer_start(model->timer, 250);
  1849. },
  1850. true);
  1851. }
  1852. static void loader_on_exit(void *context)
  1853. {
  1854. if (context == NULL)
  1855. {
  1856. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  1857. DEV_CRASH();
  1858. return;
  1859. }
  1860. FlipWorldApp *app = (FlipWorldApp *)context;
  1861. View *view = app->view_loader;
  1862. with_view_model(
  1863. view,
  1864. DataLoaderModel * model,
  1865. {
  1866. if (model->timer)
  1867. {
  1868. furi_timer_stop(model->timer);
  1869. }
  1870. },
  1871. false);
  1872. }
  1873. void loader_init(View *view)
  1874. {
  1875. if (view == NULL)
  1876. {
  1877. FURI_LOG_E(TAG, "loader_init - view is NULL");
  1878. DEV_CRASH();
  1879. return;
  1880. }
  1881. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1882. view_set_enter_callback(view, loader_on_enter);
  1883. view_set_exit_callback(view, loader_on_exit);
  1884. }
  1885. void loader_free_model(View *view)
  1886. {
  1887. if (view == NULL)
  1888. {
  1889. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  1890. DEV_CRASH();
  1891. return;
  1892. }
  1893. with_view_model(
  1894. view,
  1895. DataLoaderModel * model,
  1896. {
  1897. if (model->timer)
  1898. {
  1899. furi_timer_free(model->timer);
  1900. model->timer = NULL;
  1901. }
  1902. if (model->parser_context)
  1903. {
  1904. // do not free the context here, it is the app context
  1905. // free(model->parser_context);
  1906. // model->parser_context = NULL;
  1907. }
  1908. if (model->fhttp)
  1909. {
  1910. flipper_http_free(model->fhttp);
  1911. model->fhttp = NULL;
  1912. }
  1913. },
  1914. false);
  1915. }
  1916. bool custom_event_callback(void *context, uint32_t index)
  1917. {
  1918. if (context == NULL)
  1919. {
  1920. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  1921. DEV_CRASH();
  1922. return false;
  1923. }
  1924. switch (index)
  1925. {
  1926. case FlipWorldCustomEventProcess:
  1927. loader_process_callback(context);
  1928. return true;
  1929. default:
  1930. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  1931. return false;
  1932. }
  1933. }
  1934. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1935. {
  1936. if (app == NULL)
  1937. {
  1938. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  1939. DEV_CRASH();
  1940. return;
  1941. }
  1942. View *view = app->view_loader;
  1943. if (view == NULL)
  1944. {
  1945. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  1946. DEV_CRASH();
  1947. return;
  1948. }
  1949. with_view_model(
  1950. view,
  1951. DataLoaderModel * model,
  1952. {
  1953. model->title = title;
  1954. model->fetcher = fetcher;
  1955. model->parser = parser;
  1956. model->request_index = 0;
  1957. model->request_count = request_count;
  1958. model->back_callback = back;
  1959. model->data_state = DataStateInitial;
  1960. model->data_text = NULL;
  1961. //
  1962. model->parser_context = app;
  1963. if (!model->fhttp)
  1964. {
  1965. model->fhttp = flipper_http_alloc();
  1966. }
  1967. },
  1968. true);
  1969. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1970. }