diff options
| author | skal <pascal.massimino@gmail.com> | 2026-01-28 09:15:56 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-01-28 09:15:56 +0100 |
| commit | f804dcb9740540b3735628ebf8c006235cc56fca (patch) | |
| tree | 69b4f87a47760ccc1eecd43d6c62bf9a611bfca1 /tools/asset_packer.cc | |
| parent | 3e9e69a6cd9212b5cfd956c0a36b9dc07ab88cf7 (diff) | |
test(assets): Add functional tests for asset management system
This commit makes the asset packer fully functional and adds an end-to-end test suite.
- Updated asset_packer.cc to read file contents and embed them as hex arrays.
- Added actual asset files (null.bin, test_asset.txt) for testing.
- Implemented src/tests/test_assets.cc to verify data integrity at runtime.
- Refactored CMakeLists.txt to handle generated file dependencies correctly.
Diffstat (limited to 'tools/asset_packer.cc')
| -rw-r--r-- | tools/asset_packer.cc | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc index 8b06829..a67ddcc 100644 --- a/tools/asset_packer.cc +++ b/tools/asset_packer.cc @@ -39,7 +39,8 @@ int main(int argc, char *argv[]) { // Generate assets.h assets_h_file << "#pragma once\n"; - assets_h_file << "#include <cstdint>\n\n"; + assets_h_file << "#include <cstdint>\n"; + assets_h_file << "#include <cstddef>\n\n"; assets_h_file << "enum class AssetId : uint16_t {\n"; // Generate assets_data.cc header @@ -65,23 +66,51 @@ int main(int argc, char *argv[]) { asset_name.erase(0, asset_name.find_first_not_of(" \t\r\n")); asset_name.erase(asset_name.find_last_not_of(" \t\r\n") + 1); - if (asset_name.empty()) { - std::cerr << "Warning: Skipping line with empty asset name: " << line - << "\n"; + size_t second_comma = line.find(',', first_comma + 1); + if (second_comma == std::string::npos) { + std::cerr << "Warning: Skipping malformed line in assets.txt (missing " + "filename): " + << line << "\n"; continue; } + std::string filename = + line.substr(first_comma + 1, second_comma - first_comma - 1); + filename.erase(0, filename.find_first_not_of(" \t\r\n")); + filename.erase(filename.find_last_not_of(" \t\r\n") + 1); + + // Read the actual file + std::string base_dir = + assets_txt_path.substr(0, assets_txt_path.find_last_of("/\\") + 1); + std::ifstream asset_file(base_dir + filename, std::ios::binary); + if (!asset_file.is_open()) { + std::cerr << "Error: Could not open asset file: " << base_dir + filename + << "\n"; + return 1; + } + + std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(asset_file)), + std::istreambuf_iterator<char>()); + asset_file.close(); + asset_names.push_back(asset_name); // Add to assets.h enum assets_h_file << " ASSET_" << asset_name << " = " << asset_id_counter << ",\n"; - // Placeholder for assets_data.cc (empty data for now) + // Write data to assets_data.cc assets_data_cc_file << "const uint8_t ASSET_DATA_" << asset_name - << "[] = {};\n"; - assets_data_cc_file << "const size_t ASSET_SIZE_" << asset_name - << " = 0;\n\n"; + << "[] = {"; + for (size_t i = 0; i < buffer.size(); ++i) { + if (i % 12 == 0) + assets_data_cc_file << "\n "; + assets_data_cc_file << "0x" << std::hex << (int)buffer[i] << std::dec + << (i == buffer.size() - 1 ? "" : ", "); + } + assets_data_cc_file << "\n};\n"; + assets_data_cc_file << "const size_t ASSET_SIZE_" << asset_name << " = " + << buffer.size() << ";\n\n"; asset_id_counter++; } @@ -89,19 +118,22 @@ int main(int argc, char *argv[]) { assets_h_file << "};\n\n"; // Generate GetAsset function declaration in assets.h + assets_h_file << "const uint8_t *GetAsset(AssetId asset_id, size_t *out_size " + "= nullptr);\n"; assets_h_file - << "const uint8_t *GetAsset(AssetId asset_id, size_t *out_size = nullptr);\n"; - assets_h_file << "void DropAsset(AssetId asset_id, const uint8_t *asset); // For lazy " - "decompression scaffolding\n"; + << "void DropAsset(AssetId asset_id, const uint8_t *asset); // For lazy " + "decompression scaffolding\n"; assets_h_file.close(); // Generate GetAsset function implementation in assets_data.cc - assets_data_cc_file << "const uint8_t *GetAsset(AssetId asset_id, size_t *out_size) {\n"; + assets_data_cc_file + << "const uint8_t *GetAsset(AssetId asset_id, size_t *out_size) {\n"; assets_data_cc_file << " if (out_size) *out_size = 0;\n"; // Default for now assets_data_cc_file << " switch (asset_id) {\n"; for (const std::string &name : asset_names) { assets_data_cc_file << " case AssetId::ASSET_" << name << ":\n"; - assets_data_cc_file << " if (out_size) *out_size = ASSET_SIZE_" << name << ";\n"; + assets_data_cc_file << " if (out_size) *out_size = ASSET_SIZE_" << name + << ";\n"; assets_data_cc_file << " return ASSET_DATA_" << name << ";\n"; } assets_data_cc_file << " default:\n"; @@ -110,10 +142,12 @@ int main(int argc, char *argv[]) { assets_data_cc_file << "}\n\n"; // Dummy DropAsset implementation - assets_data_cc_file << "void DropAsset(AssetId asset_id, const uint8_t *asset) {\n"; + assets_data_cc_file + << "void DropAsset(AssetId asset_id, const uint8_t *asset) {\n"; assets_data_cc_file << " (void)asset_id;\n"; assets_data_cc_file << " (void)asset;\n"; - assets_data_cc_file << " // No-op for now, actual implementation for lazy decompression goes here\n"; + assets_data_cc_file << " // No-op for now, actual implementation for lazy " + "decompression goes here\n"; assets_data_cc_file << "}\n"; assets_data_cc_file.close(); |
