This commit is contained in:
2025-11-03 09:58:19 +01:00
parent 3f1c737247
commit d4030ec1bc
4 changed files with 26 additions and 30 deletions

View File

@@ -10,8 +10,7 @@
#include "resource_loader.hpp"
namespace Jdd {
namespace ResourceHelper {
namespace Jdd::ResourceHelper {
static bool resource_system_initialized = false;
@@ -106,7 +105,7 @@ auto getPackPath(const std::string& asset_path) -> std::string {
std::string path = asset_path;
// Convert backslashes to forward slashes
std::replace(path.begin(), path.end(), '\\', '/');
std::ranges::replace(path, '\\', '/');
// Remove leading slashes
while (!path.empty() && path[0] == '/') {
@@ -114,25 +113,25 @@ auto getPackPath(const std::string& asset_path) -> std::string {
}
// Remove "./" prefix if present
if (path.find("./") == 0) {
if (path.starts_with("./")) {
path = path.substr(2);
}
// Remove "../" prefixes (for macOS bundle paths in development)
while (path.find("../") == 0) {
while (path.starts_with("../")) {
path = path.substr(3);
}
// Remove "Resources/" prefix if present (for macOS bundle)
const std::string resources_prefix = "Resources/";
if (path.find(resources_prefix) == 0) {
path = path.substr(resources_prefix.length());
const std::string RESOURCES_PREFIX = "Resources/";
if (path.starts_with(RESOURCES_PREFIX)) {
path = path.substr(RESOURCES_PREFIX.length());
}
// Remove "data/" prefix if present
const std::string data_prefix = "data/";
if (path.find(data_prefix) == 0) {
path = path.substr(data_prefix.length());
const std::string DATA_PREFIX = "data/";
if (path.starts_with(DATA_PREFIX)) {
path = path.substr(DATA_PREFIX.length());
}
return path;
@@ -141,7 +140,7 @@ auto getPackPath(const std::string& asset_path) -> std::string {
// Check if file should use resource pack
auto shouldUseResourcePack(const std::string& filepath) -> bool {
std::string path = filepath;
std::replace(path.begin(), path.end(), '\\', '/');
std::ranges::replace(path, '\\', '/');
// Don't use pack for most config files (except config/assets.txt which is loaded
// directly via ResourceLoader::loadAssetsConfig() in release builds)
@@ -174,5 +173,4 @@ auto isPackLoaded() -> bool {
return ResourceLoader::get().isPackLoaded();
}
} // namespace ResourceHelper
} // namespace Jdd
} // namespace Jdd::ResourceHelper

View File

@@ -8,8 +8,7 @@
#include <string>
#include <vector>
namespace Jdd {
namespace ResourceHelper {
namespace Jdd::ResourceHelper {
// Initialize the resource system
// pack_file: Path to resources.pack
@@ -37,7 +36,6 @@ auto shouldUseResourcePack(const std::string& filepath) -> bool;
// Check if pack is loaded
auto isPackLoaded() -> bool;
} // namespace ResourceHelper
} // namespace Jdd
} // namespace Jdd::ResourceHelper
#endif // RESOURCE_HELPER_HPP

View File

@@ -11,8 +11,8 @@ namespace Jdd {
// Get singleton instance
auto ResourceLoader::get() -> ResourceLoader& {
static ResourceLoader instance;
return instance;
static ResourceLoader instance_;
return instance_;
}
// Initialize with a pack file

View File

@@ -28,30 +28,30 @@ class ResourceLoader {
auto resourceExists(const std::string& filename) -> bool;
// Check if pack is loaded
auto isPackLoaded() const -> bool;
[[nodiscard]] auto isPackLoaded() const -> bool;
// Get pack statistics
auto getPackResourceCount() const -> size_t;
[[nodiscard]] auto getPackResourceCount() const -> size_t;
// Validate pack integrity (checksum)
auto validatePack() const -> bool;
[[nodiscard]] auto validatePack() const -> bool;
// Load assets.txt from pack (for release builds)
auto loadAssetsConfig() const -> std::string;
[[nodiscard]] auto loadAssetsConfig() const -> std::string;
// Cleanup
void shutdown();
// Disable copy/move
ResourceLoader(const ResourceLoader&) = delete;
auto operator=(const ResourceLoader&) -> ResourceLoader& = delete;
ResourceLoader(ResourceLoader&&) = delete;
auto operator=(ResourceLoader&&) -> ResourceLoader& = delete;
private:
ResourceLoader() = default;
~ResourceLoader() = default;
// Disable copy/move
ResourceLoader(const ResourceLoader&) = delete;
ResourceLoader& operator=(const ResourceLoader&) = delete;
ResourceLoader(ResourceLoader&&) = delete;
ResourceLoader& operator=(ResourceLoader&&) = delete;
// Load from filesystem
static auto loadFromFilesystem(const std::string& filepath) -> std::vector<uint8_t>;