Skip to content

Commit

Permalink
Fix pcksl target and wrong asset command.
Browse files Browse the repository at this point in the history
  • Loading branch information
crud89 committed Dec 13, 2024
1 parent 592069c commit 573890d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/cmake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ FUNCTION(TARGET_ADD_ASSET_DIRECTORY target_name)

ADD_CUSTOM_TARGET(${directory_target_name}
COMMENT "Copying assets to runtime directory '${OUTPUT_DIR}'..."
SOURCES ${ASSET_FILE}
)

ADD_CUSTOM_COMMAND(TARGET ${directory_target_name} POST_BUILD
Expand All @@ -58,7 +59,6 @@ FUNCTION(TARGET_ADD_ASSET_DIRECTORY target_name)
GET_FILENAME_COMPONENT(ASSET_NAME ${ASSET_FILE} NAME)
ADD_CUSTOM_COMMAND(TARGET ${directory_target_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${ASSET_FILE} ${OUTPUT_DIR}/${ASSET_NAME}
SOURCES ${ASSET_FILE}
)
ENDFOREACH(ASSET_FILE ${ASSET_DIRECTORY_ASSETS})

Expand Down
152 changes: 86 additions & 66 deletions src/cmake/Shaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -394,84 +394,104 @@ FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/Auxiliary/pcksl.cxx" CONTENT [==[
#include <vector>
#include <cstdint>

// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
// NOLINTBEGIN(performance-avoid-endl)

int main(int argc, char* argv[]) {
if (argc < 2)
return -1;

std::string command(argv[1]);

if (command == "init")
{
std::string sourceFile(argv[2]);
std::ofstream file(sourceFile);
file << "#pragma once" << std::endl <<
"#include <iostream>" << std::endl <<
"#include <array>" << std::endl <<
"#include <istream>" << std::endl <<
"#include <string>" << std::endl <<
"#include <streambuf>" << std::endl << std::endl;

file << "#ifndef _LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl;
file << "struct _pcksl_mem_buf : public std::streambuf {" << std::endl;
file << " _pcksl_mem_buf(const char* begin, size_t size) { char* b = const_cast<char*>(begin); this->setg(b, b, b + size); }" << std::endl;
file << "};" << std::endl << std::endl;
file << "struct _pcksl_mem_istream : private virtual _pcksl_mem_buf, public std::istream {" << std::endl;
file << " explicit _pcksl_mem_istream(const char* begin, size_t size) :" << std::endl;
file << " _pcksl_mem_buf(begin, size), std::istream(static_cast<std::streambuf*>(this)) { }" << std::endl;
file << "};" << std::endl;
file << "#define _LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl;
file << "#endif // !_LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl << std::endl;

file.close();
}
else if (command == "pack")
try
{
if (argc != 6)
std::string command(argv[1]);

if (command == "init")
{
std::string sourceFile(argv[2]);
std::ofstream file(sourceFile);
file << "#pragma once" << std::endl <<
"#include <iostream>" << std::endl <<
"#include <array>" << std::endl <<
"#include <istream>" << std::endl <<
"#include <string>" << std::endl <<
"#include <streambuf>" << std::endl << std::endl;

file << "// NOLINTBEGIN" << std::endl;
file << "#ifndef _LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl;
file << "struct _pcksl_mem_buf : public std::streambuf {" << std::endl;
file << " _pcksl_mem_buf(char* begin, size_t size) { this->setg(begin, begin, begin + size); }" << std::endl;
file << "};" << std::endl << std::endl;
file << "struct _pcksl_mem_istream : private virtual _pcksl_mem_buf, public std::istream {" << std::endl;
file << " explicit _pcksl_mem_istream(char* begin, size_t size) :" << std::endl;
file << " _pcksl_mem_buf(begin, size), std::istream(static_cast<std::streambuf*>(this)) { }" << std::endl;
file << "};" << std::endl;
file << "#define _LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl;
file << "#endif // !_LITEFX_PCKSL_MEMBUF_DEFINED" << std::endl << std::endl;
file << "// NOLINTEND" << std::endl;

file.close();
}
else if (command == "pack")
{
if (argc != 6)
return -1;

std::string sourceFile(argv[2]);
std::string resourceFile(argv[3]);
std::string ns(argv[4]);
std::string resourceName(argv[5]);
std::string name(argv[5]);
std::replace(resourceName.begin(), resourceName.end(), '.', '_');
std::replace(resourceName.begin(), resourceName.end(), ':', '_');

std::ifstream resource(resourceFile, std::ios::binary);
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(resource), { });

std::ofstream file(sourceFile, std::ios::app);
file << "namespace " << ns << " {" << std::endl;
file << " // Shader source: " << resourceFile << "." << std::endl;
file << " // NOLINTBEGIN" << std::endl;
file << " class " << resourceName << " {" << std::endl;
file << " public:" << std::endl;
file << " " << resourceName << "() = delete;" << std::endl;
file << " ~" << resourceName << "() = delete;" << std::endl;
file << "" << std::endl;
file << " static const std::string name() { return \"" << name << "\"; }" << std::endl << std::endl;
file << " static _pcksl_mem_istream open() {" << std::endl;
file << " static std::array<uint8_t, " << buffer.size() << "> _data = {" << std::endl;
file << " ";

for (const auto& v : buffer)
file << "0x" << std::setfill('0') << std::setw(sizeof(v) * 2) << std::hex << +v << ", ";

file << std::endl;
file << " };" << std::endl << std::endl;
file << " // NOTE: reinterpret_cast should be safe here: https://eel.is/c++draft/basic.lval#11.3." << std::endl;
file << " return ::_pcksl_mem_istream(reinterpret_cast<char*>(_data.data()), _data.size());" << std::endl; // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
file << " }" << std::endl;
file << " };" << std::endl;
file << " // NOLINTEND" << std::endl;
file << "}" << std::endl << std::endl;

file.close();
}
else
{
return -1;

std::string sourceFile(argv[2]);
std::string resourceFile(argv[3]);
std::string ns(argv[4]);
std::string resourceName(argv[5]);
std::string name(argv[5]);
std::replace(resourceName.begin(), resourceName.end(), '.', '_');
std::replace(resourceName.begin(), resourceName.end(), ':', '_');

std::ifstream resource(resourceFile, std::ios::binary);
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(resource), { });

std::ofstream file(sourceFile, std::ios::app);
file << "namespace " << ns << " {" << std::endl;
file << " // Shader source: " << resourceFile << "." << std::endl;
file << " class " << resourceName << " {" << std::endl;
file << " public:" << std::endl;
file << " " << resourceName << "() = delete;" << std::endl;
file << " ~" << resourceName << "() = delete;" << std::endl;
file << "" << std::endl;
file << " static const std::string name() { return \"" << name << "\"; }" << std::endl << std::endl;
file << " static _pcksl_mem_istream open() {" << std::endl;
file << " static std::array<uint8_t, " << buffer.size() << "> _data = {" << std::endl;
file << " ";

for (const auto& v : buffer)
file << "0x" << std::setfill('0') << std::setw(sizeof(v) * 2) << std::hex << +v << ", ";

file << std::endl;
file << " };" << std::endl << std::endl;
file << " return ::_pcksl_mem_istream(reinterpret_cast<const char*>(_data.data()), _data.size());" << std::endl;
file << " }" << std::endl;
file << " };" << std::endl;
file << "}" << std::endl << std::endl;

file.close();
}
}
else
catch(...)
{
return -1;
return -2;
}

return 0;
}

// NOLINTEND(performance-avoid-endl)
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
]==])

ADD_EXECUTABLE(pcksl "${CMAKE_BINARY_DIR}/Auxiliary/pcksl.cxx")
Expand Down

0 comments on commit 573890d

Please sign in to comment.