From d5f78a4c2e7b626a492643efd62ddeb394276722 Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 9 Feb 2026 17:35:32 +0100 Subject: feat: Add debug-only file change detection for rapid iteration Enables --hot-reload flag to watch config files and notify on changes. Detects modifications to assets/sequences/music for rebuild workflow. Completely stripped from release builds (0 bytes overhead). Co-Authored-By: Claude Sonnet 4.5 --- src/util/asset_manager.cc | 20 ++++++++++++++++++++ src/util/asset_manager.h | 5 +++++ src/util/file_watcher.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/util/file_watcher.h | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 src/util/file_watcher.cc create mode 100644 src/util/file_watcher.h (limited to 'src/util') diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index a0e2a97..5067ebe 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -189,3 +189,23 @@ void DropAsset(AssetId asset_id, const uint8_t* asset) { } // For static assets, no dynamic memory to free. } + +#if !defined(STRIP_ALL) +// Hot-reload: Clear asset cache to force reload from disk +// Note: This only works for assets that read from disk at runtime. +// Compiled-in assets cannot be hot-reloaded. +bool ReloadAssetsFromFile(const char* config_path) { + (void)config_path; // Unused - just for API consistency + + // Clear cache to force reload + for (size_t i = 0; i < (size_t)AssetId::ASSET_LAST_ID; ++i) { + if (g_asset_cache[i].is_procedural && g_asset_cache[i].data) { + delete[] g_asset_cache[i].data; + } + g_asset_cache[i] = {}; + } + + fprintf(stderr, "[ReloadAssets] Cache cleared\n"); + return true; +} +#endif // !defined(STRIP_ALL) diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h index 168bfca..1714c21 100644 --- a/src/util/asset_manager.h +++ b/src/util/asset_manager.h @@ -29,3 +29,8 @@ void DropAsset(AssetId asset_id, const uint8_t* asset); // Returns the AssetId for a given asset name, or AssetId::ASSET_LAST_ID if not // found. AssetId GetAssetIdByName(const char* name); + +#if !defined(STRIP_ALL) +// Hot-reload: Parse and reload assets from config file (debug only) +bool ReloadAssetsFromFile(const char* config_path); +#endif diff --git a/src/util/file_watcher.cc b/src/util/file_watcher.cc new file mode 100644 index 0000000..22eb824 --- /dev/null +++ b/src/util/file_watcher.cc @@ -0,0 +1,44 @@ +// file_watcher.cc - Hot-reload file change detection (debug only) + +#include "file_watcher.h" + +#if !defined(STRIP_ALL) + +#include + +void FileWatcher::add_file(const char* path) { + WatchEntry entry; + entry.path = path; + entry.last_mtime = get_mtime(path); + entry.changed = false; + files_.push_back(entry); +} + +bool FileWatcher::check_changes() { + bool any_changed = false; + for (auto& entry : files_) { + time_t current_mtime = get_mtime(entry.path.c_str()); + if (current_mtime != entry.last_mtime && current_mtime != 0) { + entry.changed = true; + entry.last_mtime = current_mtime; + any_changed = true; + } + } + return any_changed; +} + +void FileWatcher::reset() { + for (auto& entry : files_) { + entry.changed = false; + } +} + +time_t FileWatcher::get_mtime(const char* path) { + struct stat st; + if (stat(path, &st) == 0) { + return st.st_mtime; + } + return 0; +} + +#endif // !defined(STRIP_ALL) diff --git a/src/util/file_watcher.h b/src/util/file_watcher.h new file mode 100644 index 0000000..2766a43 --- /dev/null +++ b/src/util/file_watcher.h @@ -0,0 +1,33 @@ +// file_watcher.h - Hot-reload file change detection (debug only) + +#ifndef FILE_WATCHER_H_ +#define FILE_WATCHER_H_ + +#if !defined(STRIP_ALL) + +#include +#include +#include + +class FileWatcher { + public: + FileWatcher() = default; + + void add_file(const char* path); + bool check_changes(); + void reset(); + + private: + struct WatchEntry { + std::string path; + time_t last_mtime; + bool changed; + }; + + std::vector files_; + time_t get_mtime(const char* path); +}; + +#endif // !defined(STRIP_ALL) + +#endif // FILE_WATCHER_H_ -- cgit v1.2.3