Skip to content

Commit

Permalink
sync libromfs to the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 28, 2024
1 parent 19f0410 commit fc6ec51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
23 changes: 15 additions & 8 deletions third_party/libromfs/generator/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ int main() {

auto resourceLocations = splitString(RESOURCE_LOCATION, ",");
for (const auto &resourceLocation : resourceLocations) {
std::printf("[libromfs] Resource Folder: %s\n", resourceLocation.c_str());
std::printf("[libromfs] Packing resource folder: %s\n", resourceLocation.c_str());

std::error_code errorCode;
if (!std::filesystem::exists(resourceLocation, errorCode)) {
if (errorCode) {
std::printf("[libromfs] Error: %s\n", errorCode.message().c_str());
}

continue;
}

outputFile << "\n/* Resource folder: " << resourceLocation << " */\n";
for (const auto &entry : fs::recursive_directory_iterator(resourceLocation)) {
Expand All @@ -81,13 +90,11 @@ int main() {
bytes.resize(std::fread(bytes.data(), 1, entry.file_size(), file));
std::fclose(file);

outputFile << std::hex << std::uppercase << std::setfill('0') << std::setw(2);
for (std::byte byte : bytes) {
outputFile << "0x" << static_cast<std::uint32_t>(byte) << ", ";
outputFile << static_cast<std::uint32_t>(byte) << ",";
}
outputFile << std::dec << std::nouppercase << std::setfill(' ') << std::setw(0);

outputFile << "\n 0x00 };\n\n";
outputFile << "0 };\n\n";

paths.push_back(relativePath);

Expand All @@ -99,7 +106,7 @@ int main() {

{
outputFile << "/* Resource map */\n";
outputFile << "std::span<romfs::impl::ResourceLocation> RomFs_" LIBROMFS_PROJECT_NAME "_get_resources() {\n";
outputFile << "ROMFS_VISIBILITY std::span<romfs::impl::ResourceLocation> RomFs_" LIBROMFS_PROJECT_NAME "_get_resources() {\n";
outputFile << " static std::array<romfs::impl::ResourceLocation, " << identifierCount << "> resources = {\n";

for (std::uint64_t i = 0; i < identifierCount; i++) {
Expand All @@ -117,7 +124,7 @@ int main() {

{
outputFile << "/* Resource paths */\n";
outputFile << "std::span<std::string_view> RomFs_" LIBROMFS_PROJECT_NAME "_get_paths() {\n";
outputFile << "ROMFS_VISIBILITY std::span<std::string_view> RomFs_" LIBROMFS_PROJECT_NAME "_get_paths() {\n";
outputFile << " static std::array<std::string_view, " << identifierCount << "> paths = {\n";

for (std::uint64_t i = 0; i < identifierCount; i++) {
Expand All @@ -133,7 +140,7 @@ int main() {

{
outputFile << "/* RomFS name */\n";
outputFile << "const char* RomFs_" LIBROMFS_PROJECT_NAME "_get_name() {\n";
outputFile << "ROMFS_VISIBILITY const char* RomFs_" LIBROMFS_PROJECT_NAME "_get_name() {\n";
outputFile << " return \"" LIBROMFS_PROJECT_NAME "\";\n";
outputFile << "}\n\n";
}
Expand Down
18 changes: 12 additions & 6 deletions third_party/libromfs/lib/include/romfs/romfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

#define ROMFS_NAME ROMFS_CONCAT(RomFs_, LIBROMFS_PROJECT_NAME)

#if !defined(WIN32)
#define ROMFS_VISIBILITY [[gnu::visibility("hidden")]]
#else
#define ROMFS_VISIBILITY
#endif

template<typename T>
concept ByteType = std::is_trivial_v<T> && sizeof(T) == sizeof(std::byte);

Expand Down Expand Up @@ -63,15 +69,15 @@ namespace romfs {
Resource resource;
};

[[nodiscard]] const Resource& ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path);
[[nodiscard]] std::vector<std::filesystem::path> ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path);
[[nodiscard]] std::string_view ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)();
[[nodiscard]] ROMFS_VISIBILITY const Resource& ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path);
[[nodiscard]] ROMFS_VISIBILITY std::vector<std::filesystem::path> ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path);
[[nodiscard]] ROMFS_VISIBILITY std::string_view ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)();

}

[[nodiscard]] inline const Resource& get(const std::filesystem::path &path) { return impl::ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(path); }
[[nodiscard]] inline std::vector<std::filesystem::path> list(const std::filesystem::path &path = {}) { return impl::ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(path); }
[[nodiscard]] inline std::string_view name() { return impl::ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)(); }
[[nodiscard]] ROMFS_VISIBILITY inline const Resource& get(const std::filesystem::path &path) { return impl::ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(path); }
[[nodiscard]] ROMFS_VISIBILITY inline std::vector<std::filesystem::path> list(const std::filesystem::path &path = {}) { return impl::ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(path); }
[[nodiscard]] ROMFS_VISIBILITY inline std::string_view name() { return impl::ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)(); }


}
6 changes: 3 additions & 3 deletions third_party/libromfs/lib/source/romfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const char* ROMFS_CONCAT(ROMFS_NAME, _get_name)();

namespace romfs {

const romfs::Resource &impl::ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path) {
ROMFS_VISIBILITY const romfs::Resource &impl::ROMFS_CONCAT(get_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &path) {
for (const auto &[resourcePath, resourceData] : ROMFS_CONCAT(ROMFS_NAME, _get_resources)()) {
if (path == resourcePath)
return resourceData;
Expand All @@ -15,7 +15,7 @@ namespace romfs {
throw std::invalid_argument(std::string("Invalid romfs resource path! File '") + std::string(romfs::name()) + "' : " + path.string());
}

std::vector<std::filesystem::path> impl::ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &parent) {
ROMFS_VISIBILITY std::vector<std::filesystem::path> impl::ROMFS_CONCAT(list_, LIBROMFS_PROJECT_NAME)(const std::filesystem::path &parent) {
std::vector<std::filesystem::path> result;
for (const auto &pathString : ROMFS_CONCAT(ROMFS_NAME, _get_paths)()) {
auto path = std::filesystem::path(pathString);
Expand All @@ -26,7 +26,7 @@ namespace romfs {
return result;
}

std::string_view impl::ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)() {
ROMFS_VISIBILITY std::string_view impl::ROMFS_CONCAT(name_, LIBROMFS_PROJECT_NAME)() {
return ROMFS_CONCAT(ROMFS_NAME, _get_name)();
}

Expand Down

0 comments on commit fc6ec51

Please sign in to comment.