reshax.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <fstream>
  2. #include <map>
  3. #include <sstream>
  4. #include <sys/stat.h>
  5. std::ofstream resources_cpp, resources_h;
  6. void load(std::string file);
  7. void write(std::string file, int size, char *data);
  8. using namespace std;
  9. int main(int argc, char *argv[])
  10. {
  11. if (argc > 1)
  12. {
  13. resources_h.open("resources.h");
  14. resources_h
  15. << "#ifndef LOVE_RESOURCES_H\n"
  16. << "#define LOVE_RESOURCES_H\n\n"
  17. << "#include <vector>\n"
  18. << "#include <love/MemoryFile.h>\n\n"
  19. << "namespace love\n"
  20. << "{\n";
  21. resources_cpp.open("resources.cpp");
  22. resources_cpp
  23. << "#include \"resources.h\"\n\n"
  24. << "namespace love\n"
  25. << "{\n";
  26. for (int i = 1; i < argc; i++)
  27. {
  28. // use boost::filesystem to list all the files (in v2 maybe)
  29. load(argv[i]);
  30. }
  31. resources_cpp << "}\n";
  32. resources_cpp.close();
  33. resources_h
  34. << "}\n\n"
  35. << "#endif\n";
  36. resources_h.close();
  37. }
  38. else
  39. printf("ResHax-5Million v1.0a\n- now empowered by rubber piggies\n\nUsage: reshax-5million [file1] [file2] [and so on...]\n");
  40. }
  41. void load(string file)
  42. {
  43. ifstream fs (file.c_str(), fstream::in | fstream::binary | fstream::ate);
  44. if (fs.is_open() && fs.good())
  45. {
  46. printf("Haxing %s", file.c_str());
  47. int size = (int)fs.tellg();
  48. char * buff = new char[size];
  49. fs.seekg(0, ios::beg);
  50. fs.read(buff, size);
  51. #ifndef WIN32
  52. struct stat fstat;
  53. stat(file.c_str(), &fstat);
  54. if (!S_ISDIR(fstat.st_mode))
  55. write(file, size, buff);
  56. else
  57. printf(" FAIL cuz DIR\n");
  58. #else
  59. write(file, size, buff);
  60. #endif
  61. fs.close();
  62. delete [] buff;
  63. }
  64. else
  65. printf("Hax does not liek '%s'\n", file.c_str());
  66. }
  67. void write(string file, int size, char *data)
  68. {
  69. char buffer[8];
  70. //size--;
  71. // removes directory from file path
  72. size_t found;
  73. found = file.find_last_of('/');
  74. if (found != string::npos)
  75. file = file.substr(found + 1);
  76. string var (file);
  77. // replaces dashes and dots (in UNIX only)
  78. //#ifndef WIN32
  79. found = var.find_first_of(" !\"#$%&'()*+,-.@[]", 0);
  80. while (found != string::npos)
  81. {
  82. var[found] = '_';
  83. found = var.find_first_of(" !\"#$%&'()*+,-.@[]", found);
  84. }
  85. //#endif
  86. resources_cpp << "\tstatic char " << var << "_data[" << size << "] = {";
  87. for (int i = 0; i < size - 1; i++)
  88. {
  89. sprintf(buffer, "%d,", data[i]);
  90. resources_cpp << buffer;
  91. if (i != 0 && i % 30 == 0)
  92. resources_cpp << "\n\t";
  93. }
  94. sprintf(buffer, "%d};\n", data[size]);
  95. resources_cpp << buffer << "\tpFile " << var.c_str() << "(new MemoryFile(" << var << "_data, " << size << ", \"" << file.c_str() << "\"));\n\n";
  96. resources_h << "\textern pFile " << var << ";\n";
  97. printf(" is haxed\n");
  98. }