reshax.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <fstream>
  3. #include <map>
  4. #include <sstream>
  5. #include <sys/stat.h>
  6. std::ofstream resources_cpp, resources_h;
  7. std::stringstream resources_array;
  8. void load(std::string file);
  9. void write(std::string file, int size, char *data);
  10. const char * love_header = ""
  11. "/**\n"
  12. "* Copyright (c) 2006-2010 LOVE Development Team\n"
  13. "* \n"
  14. "* This software is provided 'as-is', without any express or implied\n"
  15. "* warranty. In no event will the authors be held liable for any damages\n"
  16. "* arising from the use of this software.\n"
  17. "* \n"
  18. "* Permission is granted to anyone to use this software for any purpose,\n"
  19. "* including commercial applications, and to alter it and redistribute it\n"
  20. "* freely, subject to the following restrictions:\n"
  21. "* \n"
  22. "* 1. The origin of this software must not be misrepresented; you must not\n"
  23. "* claim that you wrote the original software. If you use this software\n"
  24. "* in a product, an acknowledgment in the product documentation would be\n"
  25. "* appreciated but is not required.\n"
  26. "* 2. Altered source versions must be plainly marked as such, and must not be\n"
  27. "* misrepresented as being the original software.\n"
  28. "* 3. This notice may not be removed or altered from any source distribution.\n"
  29. "**/\n\n";
  30. using namespace std;
  31. int main(int argc, char *argv[])
  32. {
  33. if (argc > 1)
  34. {
  35. resources_h.open("resources.h");
  36. resources_h
  37. << love_header
  38. << "#ifndef LOVE_RESOURCES_H\n"
  39. << "#define LOVE_RESOURCES_H\n\n"
  40. << "namespace love\n"
  41. << "{\n"
  42. << "\tstruct Resource\n"
  43. << "\t{\n"
  44. << "\t\tconst char * name;\n"
  45. << "\t\tvoid * data;\n"
  46. << "\t\tunsigned int size;\n"
  47. << "\t};\n"
  48. << "\n"
  49. << "\textern const Resource resources[];\n"
  50. << "\n";
  51. resources_cpp.open("resources.cpp");
  52. resources_cpp
  53. << love_header
  54. << "#include \"resources.h\"\n\n"
  55. << "namespace love\n"
  56. << "{\n";
  57. resources_array << "\n\tconst Resource resources[] = {\n";
  58. for (int i = 1; i < argc; i++)
  59. {
  60. // use boost::filesystem to list all the files (in v2 maybe)
  61. load(argv[i]);
  62. }
  63. resources_array << "\t\t{0,0,0}\n\t};\n\n";
  64. resources_cpp << resources_array.str();
  65. resources_cpp << "} // love\n";
  66. resources_cpp.close();
  67. resources_h
  68. << "}\n\n"
  69. << "#endif // LOVE_RESOURCES_H \n";
  70. resources_h.close();
  71. }
  72. else
  73. printf("ResHax-5Million v1.0a\n- now empowered by rubber piggies\n\nUsage: reshax-5million [file1] [file2] [and so on...]\n");
  74. }
  75. void load(string file)
  76. {
  77. ifstream fs (file.c_str(), fstream::in | fstream::binary | fstream::ate);
  78. if (fs.is_open() && fs.good())
  79. {
  80. printf("Haxing %s", file.c_str());
  81. int size = (int)fs.tellg();
  82. char * buff = new char[size];
  83. fs.seekg(0, ios::beg);
  84. fs.read(buff, size);
  85. #ifndef WIN32
  86. struct stat fstat;
  87. stat(file.c_str(), &fstat);
  88. if (!S_ISDIR(fstat.st_mode))
  89. write(file, size, buff);
  90. else
  91. printf(" FAIL cuz DIR\n");
  92. #else
  93. write(file, size, buff);
  94. #endif
  95. fs.close();
  96. delete [] buff;
  97. }
  98. else
  99. printf("Hax does not liek '%s'\n", file.c_str());
  100. }
  101. void write(string file, int size, char *data)
  102. {
  103. char buffer[8];
  104. //size--;
  105. // removes directory from file path
  106. size_t found;
  107. found = file.find_last_of('/');
  108. if (found != string::npos)
  109. file = file.substr(found + 1);
  110. string var (file);
  111. // replaces dashes and dots (in UNIX only)
  112. //#ifndef WIN32
  113. found = var.find_first_of(" !\"#$%&'()*+,-.@[]", 0);
  114. while (found != string::npos)
  115. {
  116. var[found] = '_';
  117. found = var.find_first_of(" !\"#$%&'()*+,-.@[]", found);
  118. }
  119. //#endif
  120. // Lowercase plz.
  121. for (unsigned int i=0;i<strlen(var.c_str());i++)
  122. if (var[i] >= 0x41 && var[i] <= 0x5A)
  123. var[i] = var[i] + 0x20;
  124. resources_cpp << "\tconst char " << var << "_data[] = {";
  125. for (int i = 0; i < size - 1; i++)
  126. {
  127. sprintf(buffer, "%d,", data[i]);
  128. resources_cpp << buffer;
  129. if (i != 0 && i % 30 == 0)
  130. resources_cpp << "\n\t";
  131. }
  132. sprintf(buffer, "%d};\n\n", data[size]);
  133. resources_cpp << buffer;
  134. resources_array << "\t\t{\"_" << var << "\", (void*)" << var << "_data, " << size << "},\n";
  135. printf(" is haxed\n");
  136. }