diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cc | 24 | ||||
| -rw-r--r-- | src/tests/test_file_watcher.cc | 63 | ||||
| -rw-r--r-- | src/util/asset_manager.cc | 20 | ||||
| -rw-r--r-- | src/util/asset_manager.h | 5 | ||||
| -rw-r--r-- | src/util/file_watcher.cc | 44 | ||||
| -rw-r--r-- | src/util/file_watcher.h | 33 |
6 files changed, 189 insertions, 0 deletions
diff --git a/src/main.cc b/src/main.cc index 59001fb..b4091e7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,6 +11,7 @@ #include "audio/tracker.h" #if !defined(STRIP_ALL) #include "audio/backend/wav_dump_backend.h" +#include "util/file_watcher.h" #include <vector> #endif #include "generated/assets.h" // Include generated asset header @@ -32,6 +33,7 @@ int main(int argc, char** argv) { bool dump_wav = false; bool tempo_test_enabled = false; const char* wav_output_file = "audio_dump.wav"; + bool hot_reload_enabled = false; #if !defined(STRIP_ALL) for (int i = 1; i < argc; ++i) { @@ -57,6 +59,9 @@ int main(int argc, char** argv) { } } else if (strcmp(argv[i], "--tempo") == 0) { tempo_test_enabled = true; + } else if (strcmp(argv[i], "--hot-reload") == 0) { + hot_reload_enabled = true; + printf("Hot-reload enabled (watching config files)\n"); } } #else @@ -167,6 +172,16 @@ int main(int argc, char** argv) { g_last_audio_time = audio_get_playback_time(); // Initialize after start #if !defined(STRIP_ALL) + // Hot-reload setup + FileWatcher file_watcher; + if (hot_reload_enabled) { + file_watcher.add_file("assets/final/demo_assets.txt"); + file_watcher.add_file("assets/demo.seq"); + file_watcher.add_file("assets/music.track"); + } +#endif + +#if !defined(STRIP_ALL) // In WAV dump mode, run headless simulation and write audio to file if (dump_wav) { printf("Running WAV dump simulation...\n"); @@ -255,6 +270,15 @@ int main(int argc, char** argv) { // context fill_audio_buffer(audio_dt, current_physical_time); +#if !defined(STRIP_ALL) + // Hot-reload: Check for file changes + if (hot_reload_enabled && file_watcher.check_changes()) { + printf("\n[Hot-Reload] Config files changed - rebuild required\n"); + printf("[Hot-Reload] Run: cmake --build build -j4 && ./build/demo64k\n"); + file_watcher.reset(); + } +#endif + // --- Graphics Update --- const float aspect_ratio = platform_state.aspect_ratio; diff --git a/src/tests/test_file_watcher.cc b/src/tests/test_file_watcher.cc new file mode 100644 index 0000000..ac13afd --- /dev/null +++ b/src/tests/test_file_watcher.cc @@ -0,0 +1,63 @@ +// test_file_watcher.cc - Unit tests for file change detection + +#include "util/file_watcher.h" +#include <cstdio> +#include <fstream> +#include <unistd.h> + +#if !defined(STRIP_ALL) + +int main() { + // Create a temporary test file + const char* test_file = "/tmp/test_watcher_file.txt"; + { + std::ofstream f(test_file); + f << "initial content\n"; + } + + FileWatcher watcher; + watcher.add_file(test_file); + + // Initial check - no changes yet + bool changed = watcher.check_changes(); + if (changed) { + fprintf(stderr, "FAIL: Expected no changes on first check\n"); + return 1; + } + + // Sleep to ensure mtime changes (some filesystems have 1s granularity) + sleep(1); + + // Modify the file + { + std::ofstream f(test_file, std::ios::app); + f << "modified\n"; + } + + // Check for changes + changed = watcher.check_changes(); + if (!changed) { + fprintf(stderr, "FAIL: Expected changes after file modification\n"); + return 1; + } + + // Reset and check again - should be no changes + watcher.reset(); + changed = watcher.check_changes(); + if (changed) { + fprintf(stderr, "FAIL: Expected no changes after reset\n"); + return 1; + } + + printf("PASS: FileWatcher tests\n"); + return 0; +} + +#else + +int main() { + printf("SKIP: FileWatcher tests (STRIP_ALL build)\n"); + return 0; +} + +#endif 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 <sys/stat.h> + +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 <string> +#include <vector> +#include <ctime> + +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<WatchEntry> files_; + time_t get_mtime(const char* path); +}; + +#endif // !defined(STRIP_ALL) + +#endif // FILE_WATCHER_H_ |
