فهرست منبع

check heap before game/switching levels

jblanked 1 سال پیش
والد
کامیت
750ce70c4f
7فایلهای تغییر یافته به همراه39 افزوده شده و 14 حذف شده
  1. 5 5
      callback/callback.c
  2. 11 1
      flip_world.c
  3. 3 1
      flip_world.h
  4. 2 2
      flipper_http/flipper_http.h
  5. 9 1
      game/game.c
  6. 8 4
      game/level.c
  7. 1 0
      game/player.h

+ 5 - 5
callback/callback.c

@@ -1053,6 +1053,11 @@ void callback_submenu_choices(void *context, uint32_t index)
     {
     case FlipWorldSubmenuIndexRun:
         free_all_views(app, true, true);
+        if (!is_enough_heap(60000))
+        {
+            easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
+            return;
+        }
         if (!flipper_http_init(flipper_http_rx_callback, app))
         {
             FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
@@ -1062,11 +1067,6 @@ void callback_submenu_choices(void *context, uint32_t index)
         // check if logged in
         if (is_logged_in() || is_logged_in_to_flip_social())
         {
-            size_t heap_size = memmgr_get_free_heap();
-            size_t total_heap_size = memmgr_get_total_heap();
-
-            FURI_LOG_I(TAG, "Heap size: %d", heap_size);
-            FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
 
             flip_world_world_list_switch_to_view(app_instance);
         }

+ 11 - 1
flip_world.c

@@ -4,4 +4,14 @@ const float game_fps_choices_2[] = {30.0, 60.0, 120.0, 240.0};
 int game_fps_index = 0;
 char *game_screen_always_on_choices[] = {"No", "Yes"};
 int game_screen_always_on_index = 1;
-FlipWorldApp *app_instance = NULL;
+FlipWorldApp *app_instance = NULL;
+
+bool is_enough_heap(size_t heap_size)
+{
+    size_t free_heap = memmgr_get_free_heap();
+
+    FURI_LOG_I(TAG, "Free heap: %d", free_heap);
+    FURI_LOG_I(TAG, "Total heap: %d", memmgr_get_total_heap());
+
+    return free_heap > (heap_size + 1024); // 1KB buffer
+}

+ 3 - 1
flip_world.h

@@ -79,4 +79,6 @@ extern const float game_fps_choices_2[];
 extern int game_fps_index;
 extern char *game_screen_always_on_choices[];
 extern int game_screen_always_on_index;
-extern FlipWorldApp *app_instance;
+extern FlipWorldApp *app_instance;
+
+bool is_enough_heap(size_t heap_size);

+ 2 - 2
flipper_http/flipper_http.h

@@ -23,8 +23,8 @@
 #define TIMEOUT_DURATION_TICKS (5 * 1000) // 5 seconds
 #define BAUDRATE (115200)                 // UART baudrate
 #define RX_BUF_SIZE 2048                  // UART RX buffer size
-#define RX_LINE_BUFFER_SIZE 4096          // UART RX line buffer size (increase for large responses)
-#define MAX_FILE_SHOW 4096                // Maximum data from file to show
+#define RX_LINE_BUFFER_SIZE 3000          // UART RX line buffer size (increase for large responses)
+#define MAX_FILE_SHOW 3000                // Maximum data from file to show
 #define FILE_BUFFER_SIZE 512              // File buffer size
 
 // Forward declaration for callback

+ 9 - 1
game/game.c

@@ -63,7 +63,15 @@ static void game_stop(void *ctx)
     }
     if (game_context->player_context)
     {
-        easy_flipper_dialog("Game Over", "Thanks for playing Flip World!\nHit BACK then wait for\nthe game to save.");
+        FURI_LOG_I("Game", "Game ending");
+        if (!game_context->ended_early)
+        {
+            easy_flipper_dialog("Game Over", "Thanks for playing Flip World!\nHit BACK then wait for\nthe game to save.");
+        }
+        else
+        {
+            easy_flipper_dialog("Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
+        }
         FURI_LOG_I("Game", "Saving player context");
         save_player_context(game_context->player_context);
         FURI_LOG_I("Game", "Player context saved");

+ 8 - 4
game/level.c

@@ -16,11 +16,15 @@ void set_world(Level *level, GameManager *manager, char *id)
         draw_town_world(level);
         return;
     }
-    size_t heap_size = memmgr_get_free_heap();
-    size_t total_heap_size = memmgr_get_total_heap();
 
-    FURI_LOG_I(TAG, "Heap size: %d", heap_size);
-    FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
+    if (!is_enough_heap(28400))
+    {
+        FURI_LOG_E("Game", "Not enough heap memory.. ending game early.");
+        GameContext *game_context = game_manager_game_context_get(manager);
+        game_context->ended_early = true;
+        game_manager_game_stop(manager); // end game early
+        return;
+    }
 
     FURI_LOG_I("Game", "Drawing world");
     if (!draw_json_world_furi(level, json_data_str))

+ 1 - 0
game/player.h

@@ -55,6 +55,7 @@ typedef struct
     int level_count;
     int enemy_count;
     int current_level;
+    bool ended_early;
 } GameContext;
 
 typedef struct