# Switch 平台 Asset 类崩溃修复计划 ## 问题描述 调用 `Sprite::createFromFile("assets/player.png")` 时崩溃,甚至连 printf 都没执行。崩溃发生在 `Asset::readBinaryFile()` 内部的 `fs::exists()` 调用上。 ## 问题根因分析 Switch 平台上的 `std::filesystem` 实现可能不稳定或不完整,导致 `fs::exists()` 调用直接崩溃。 ## 修复方案 修改 `Asset::readBinaryFile()` 函数,**移除文件存在性检查**,直接尝试打开文件。这样可以避免调用有问题的 `fs::exists()`。 ### 修复: 修改 asset.cpp **文件**: `Frostbite2D/src/frostbite2D/utils/asset.cpp` **变更**: - 移除 `readBinaryFile()` 中的 `exists()` 检查 - 直接尝试打开文件,通过 `file.is_open()` 来判断是否成功 **代码修改**: ```cpp bool Asset::readBinaryFile(const std::string &path, std::vector &outData) { std::string fullPath = resolveFullPath(path); // 移除 fs::exists() 检查,直接尝试打开文件 std::ifstream file(toPath(fullPath), std::ios::in | std::ios::binary); if (!file.is_open()) { return false; } file.seekg(0, std::ios::end); auto size = file.tellg(); file.seekg(0, std::ios::beg); outData.resize(static_cast(size)); file.read(reinterpret_cast(outData.data()), size); file.close(); return true; } ``` ### 可选: 同时修复 readTextFile 为了保持一致性,也可以对 `readTextFile()` 做同样的修改: ```cpp bool Asset::readTextFile(const std::string &path, std::string &outContent) { std::string fullPath = resolveFullPath(path); // 移除 fs::exists() 检查,直接尝试打开文件 std::ifstream file(toPath(fullPath), std::ios::in | std::ios::binary); if (!file.is_open()) { return false; } std::ostringstream ss; ss << file.rdbuf(); outContent = ss.str(); file.close(); return true; } ``` ## 测试步骤 1. 应用修复 2. 重新构建 Switch 版本: `xmake build -p switch` 3. 在 Switch 上运行生成的 NRO 文件 4. 验证图片能正常加载 ## 预期结果 - 不再崩溃 - 文件能正常读取 - 图片能正常加载和显示