sourcery.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <nall/nall.hpp>
  2. using namespace nall;
  3. struct Sourcery {
  4. auto main(Arguments arguments) -> void;
  5. auto parse(Markup::Node&) -> void;
  6. private:
  7. string pathname;
  8. file_buffer source;
  9. file_buffer header;
  10. };
  11. auto Sourcery::main(Arguments arguments) -> void {
  12. if(arguments.size() != 3) return print("usage: sourcery resource.bml resource.cpp resource.hpp\n");
  13. string markupName = arguments.take();
  14. string sourceName = arguments.take();
  15. string headerName = arguments.take();
  16. if(!markupName.endsWith(".bml")) return print("error: arguments in incorrect order\n");
  17. if(!sourceName.endsWith(".cpp")) return print("error: arguments in incorrect order\n");
  18. if(!headerName.endsWith(".hpp")) return print("error: arguments in incorrect order\n");
  19. string markup = string::read(markupName);
  20. if(!markup) return print("error: unable to read resource manifest\n");
  21. pathname = Location::path(markupName);
  22. if(!source.open(sourceName, file::mode::write)) return print("error: unable to write source file\n");
  23. if(!header.open(headerName, file::mode::write)) return print("error: unable to write header file\n");
  24. source.print("#include \"", headerName, "\"\n");
  25. source.print("\n");
  26. auto document = BML::unserialize(markup);
  27. parse(document);
  28. }
  29. auto Sourcery::parse(Markup::Node& root) -> void {
  30. for(auto node : root) {
  31. if(node.name() == "namespace") {
  32. header.print("namespace ", node["name"].text(), " {\n");
  33. source.print("namespace ", node["name"].text(), " {\n");
  34. parse(node);
  35. header.print("}\n");
  36. source.print("}\n");
  37. } else if(node.name() == "binary") {
  38. string filename{pathname, node["file"].text()};
  39. if(!file::exists(filename)) {
  40. print("warning: binary file ", node["file"].text(), " not found\n");
  41. continue;
  42. }
  43. auto buffer = file::read(filename);
  44. header.print("extern const unsigned char ", node["name"].text(), "[", buffer.size(), "];\n");
  45. source.print("const unsigned char ", node["name"].text(), "[", buffer.size(), "] = {\n");
  46. buffer.foreach([&](uint offset, int data) {
  47. if((offset & 31) == 0) source.print(" ");
  48. source.print(data, ",");
  49. if((offset & 31) == 31) source.print("\n");
  50. });
  51. if(buffer.size() & 31) source.print("\n");
  52. source.print("};\n");
  53. }
  54. }
  55. }
  56. #include <nall/main.hpp>
  57. auto nall::main(Arguments arguments) -> void {
  58. Sourcery().main(arguments);
  59. }