feat(framework): convencion local_files/ — separacion distribuible vs estado
Toda app C++ basada en fn::run_app coloca sus archivos escribibles
bajo <exe_dir>/local_files/. Los distribuibles (.exe, dlls, ttfs,
enrichers/, runtime/) siguen junto al .exe. Esto deja la carpeta
distribuible limpia para zippear y separa con claridad lo que
viaja con la app de lo que el PC genera.
API publica en fn:: (cpp/framework/app_base.h):
- exe_dir() directorio del ejecutable
- local_dir() <exe_dir>/local_files/, creado on-demand
- local_path(name) <local_dir>/<name>
- migrate_to_local_files(...) mueve archivos viejos desde cwd/exe_dir
Cambios:
- run_app configura io.IniFilename = local_path("imgui.ini") y
llama migrate_to_local_files(["imgui.ini","app_settings.ini"])
antes de settings_load(). Migracion idempotente para PCs con
instalacion previa.
- app_settings.cpp usa local_path("app_settings.ini") en lugar de
hardcoded "app_settings.ini" relativo al cwd.
- cpp_apps.md §7 documenta la convencion como obligatoria. Las
apps deben usar fn::local_path() para cualquier archivo
escribible nuevo.
Beneficios:
- zip distribuible no se "ensucia" con .ini/.db generados al usar.
- reset trivial: borrar local_files/.
- backup/sync per-PC: solo local_files/ es propio del PC.
- elimina la mezcla de paths Linux/Windows que generaba bugs como
"projects\\default\\operations.db" en builds cross-platform.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,10 +11,25 @@
|
||||
#include "core/app_about.h"
|
||||
#include "core/app_menubar.h"
|
||||
#include "core/fps_overlay.h"
|
||||
#include "core/logger.h"
|
||||
#include "core/log_window.h"
|
||||
#include "gfx/gl_loader.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef TRACY_ENABLE
|
||||
#include "tracy/Tracy.hpp"
|
||||
@@ -26,10 +41,121 @@ static void glfw_error_callback(int error, const char* description) {
|
||||
|
||||
namespace fn {
|
||||
|
||||
// ============================================================================
|
||||
// Local files
|
||||
// ============================================================================
|
||||
namespace {
|
||||
|
||||
std::string compute_exe_dir() {
|
||||
#ifdef _WIN32
|
||||
wchar_t buf[MAX_PATH * 2];
|
||||
DWORD n = GetModuleFileNameW(nullptr, buf,
|
||||
(DWORD)(sizeof(buf) / sizeof(buf[0])));
|
||||
if (n == 0 || n >= sizeof(buf)/sizeof(buf[0])) return "";
|
||||
int u8n = WideCharToMultiByte(CP_UTF8, 0, buf, (int)n,
|
||||
nullptr, 0, nullptr, nullptr);
|
||||
std::string out(u8n, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, buf, (int)n, out.data(), u8n,
|
||||
nullptr, nullptr);
|
||||
size_t slash = out.find_last_of("/\\");
|
||||
return (slash == std::string::npos) ? "" : out.substr(0, slash);
|
||||
#else
|
||||
char buf[4096];
|
||||
ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
|
||||
if (n <= 0) return "";
|
||||
buf[n] = 0;
|
||||
std::string out(buf);
|
||||
size_t slash = out.find_last_of('/');
|
||||
return (slash == std::string::npos) ? "" : out.substr(0, slash);
|
||||
#endif
|
||||
}
|
||||
|
||||
const std::string& exe_dir_ref() {
|
||||
static std::string cached = compute_exe_dir();
|
||||
return cached;
|
||||
}
|
||||
|
||||
const std::string& local_dir_ref() {
|
||||
static std::string cached;
|
||||
static bool inited = false;
|
||||
if (inited) return cached;
|
||||
const std::string& edir = exe_dir_ref();
|
||||
if (edir.empty()) {
|
||||
cached = "local_files"; // fallback: relativo al cwd
|
||||
} else {
|
||||
cached = edir + "/local_files";
|
||||
}
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(cached, ec);
|
||||
inited = true;
|
||||
return cached;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const char* exe_dir() { return exe_dir_ref().c_str(); }
|
||||
const char* local_dir() { return local_dir_ref().c_str(); }
|
||||
|
||||
const char* local_path(const char* name) {
|
||||
static thread_local std::string buf;
|
||||
buf = local_dir_ref();
|
||||
if (name && *name) {
|
||||
if (!buf.empty() && buf.back() != '/' && buf.back() != '\\') buf += '/';
|
||||
buf += name;
|
||||
}
|
||||
return buf.c_str();
|
||||
}
|
||||
|
||||
void migrate_to_local_files(const char* const* names, std::size_t n) {
|
||||
if (!names || n == 0) return;
|
||||
const std::string& ldir = local_dir_ref();
|
||||
const std::string& edir = exe_dir_ref();
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
const char* name = names[i];
|
||||
if (!name || !*name) continue;
|
||||
std::string dst = ldir + "/" + name;
|
||||
struct stat st{};
|
||||
if (::stat(dst.c_str(), &st) == 0) continue; // ya existe en local
|
||||
|
||||
// Buscar en exe_dir y en cwd. Mover el primero que aparezca.
|
||||
std::string cands[] = {
|
||||
edir.empty() ? std::string() : (edir + "/" + name),
|
||||
std::string(name),
|
||||
};
|
||||
for (const auto& src : cands) {
|
||||
if (src.empty() || src == dst) continue;
|
||||
if (::stat(src.c_str(), &st) != 0) continue;
|
||||
std::error_code ec;
|
||||
std::filesystem::rename(src, dst, ec);
|
||||
if (ec) {
|
||||
// Cross-device o permisos — fallback a copy + remove.
|
||||
std::filesystem::copy(src, dst,
|
||||
std::filesystem::copy_options::recursive |
|
||||
std::filesystem::copy_options::overwrite_existing, ec);
|
||||
if (!ec) std::filesystem::remove_all(src, ec);
|
||||
}
|
||||
std::fprintf(stdout,
|
||||
"[local_files] migrado: %s -> %s\n",
|
||||
src.c_str(), dst.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
// Logger primero para capturar fallos del propio init (GLFW, ventana, GL).
|
||||
if (config.log.file_path != nullptr) {
|
||||
fn_log::logger_init(
|
||||
config.log.file_path,
|
||||
static_cast<fn_log::Level>(config.log.level));
|
||||
fn_log::log_info("app start: %s", config.title ? config.title : "(no title)");
|
||||
}
|
||||
|
||||
glfwSetErrorCallback(glfw_error_callback);
|
||||
if (!glfwInit()) {
|
||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||
fn_log::log_error("GLFW init failed");
|
||||
if (config.log.file_path != nullptr) fn_log::logger_close();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -44,6 +170,8 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
GLFWwindow* window = glfwCreateWindow(config.width, config.height, config.title, nullptr, nullptr);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Failed to create GLFW window\n");
|
||||
fn_log::log_error("GLFW createWindow failed (%dx%d)", config.width, config.height);
|
||||
if (config.log.file_path != nullptr) fn_log::logger_close();
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
@@ -56,6 +184,8 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
if (config.init_gl_loader) {
|
||||
if (!fn::gfx::gl_loader_init()) {
|
||||
fprintf(stderr, "Failed to initialize GL function loader\n");
|
||||
fn_log::log_error("gl_loader_init failed");
|
||||
if (config.log.file_path != nullptr) fn_log::logger_close();
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
@@ -72,6 +202,17 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||
|
||||
// Convencion local_files: imgui.ini y app_settings.ini viven en
|
||||
// <exe_dir>/local_files/. Migra automaticamente desde el cwd o
|
||||
// exe_dir si vienen de una version previa.
|
||||
{
|
||||
static const char* legacy_names[] = {"imgui.ini", "app_settings.ini"};
|
||||
migrate_to_local_files(legacy_names,
|
||||
sizeof(legacy_names) / sizeof(legacy_names[0]));
|
||||
}
|
||||
static std::string s_imgui_ini = local_path("imgui.ini");
|
||||
io.IniFilename = s_imgui_ini.c_str();
|
||||
|
||||
// Lee app_settings.ini (font_id, font_size_px, show_fps) antes de cargar
|
||||
// fuentes. Si no existe el .ini, los defaults se aplican.
|
||||
fn_ui::settings_load();
|
||||
@@ -160,6 +301,9 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
// Ventana de Settings (no-op si esta cerrada).
|
||||
fn_ui::settings_window_render();
|
||||
|
||||
// Ventana de Logs (no-op si esta cerrada).
|
||||
fn_ui::log_window_render();
|
||||
|
||||
// Ventana About (no-op si esta cerrada).
|
||||
fn_ui::about_window_render();
|
||||
|
||||
@@ -194,6 +338,12 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
|
||||
// Persiste settings al exit (idempotente con auto-saves del menu).
|
||||
fn_ui::settings_save();
|
||||
|
||||
// Cierra el archivo de log (si la app lo abrio).
|
||||
if (config.log.file_path != nullptr) {
|
||||
fn_log::log_info("app exit");
|
||||
fn_log::logger_close();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
|
||||
Reference in New Issue
Block a user