polybuild.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "polybuild.h"
  2. #include "zip.h"
  3. vector<BuildArg> args;
  4. #define MAXFILENAME (256)
  5. String getArg(String argName) {
  6. for(int i=0; i < args.size(); i++) {
  7. if(args[i].name == argName) {
  8. return args[i].value;
  9. }
  10. }
  11. return "";
  12. }
  13. uLong filetime(
  14. const char *f,
  15. tm_zip *tmzip,
  16. uLong *dt)
  17. {
  18. int ret=0;
  19. struct stat s; /* results of stat() */
  20. struct tm* filedate;
  21. time_t tm_t=0;
  22. if (strcmp(f,"-")!=0)
  23. {
  24. char name[MAXFILENAME+1];
  25. int len = strlen(f);
  26. if (len > MAXFILENAME)
  27. len = MAXFILENAME;
  28. strncpy(name, f,MAXFILENAME-1);
  29. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  30. name[ MAXFILENAME ] = '\0';
  31. if (name[len - 1] == '/')
  32. name[len - 1] = '\0';
  33. /* not all systems allow stat'ing a file with / appended */
  34. if (stat(name,&s)==0)
  35. {
  36. tm_t = s.st_mtime;
  37. ret = 1;
  38. }
  39. }
  40. filedate = localtime(&tm_t);
  41. tmzip->tm_sec = filedate->tm_sec;
  42. tmzip->tm_min = filedate->tm_min;
  43. tmzip->tm_hour = filedate->tm_hour;
  44. tmzip->tm_mday = filedate->tm_mday;
  45. tmzip->tm_mon = filedate->tm_mon ;
  46. tmzip->tm_year = filedate->tm_year;
  47. return ret;
  48. }
  49. void addFileToZip(zipFile z, String filePath, String pathInZip, bool silent) {
  50. if(!silent)
  51. printf("Packaging %s as %s\n", filePath.c_str(), pathInZip.c_str());
  52. zip_fileinfo zi;
  53. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  54. zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  55. zi.dosDate = 0;
  56. zi.internal_fa = 0;
  57. zi.external_fa = 0;
  58. filetime(filePath.c_str(),&zi.tmz_date,&zi.dosDate);
  59. zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2);
  60. FILE *f = fopen(filePath.c_str(), "rb");
  61. fseek(f, 0, SEEK_END);
  62. long fileSize = ftell(f);
  63. fseek(f, 0, SEEK_SET);
  64. char *buf = (char*) malloc(fileSize);
  65. fread(buf, fileSize, 1, f);
  66. zipWriteInFileInZip(z, buf, fileSize);
  67. free(buf);
  68. fclose(f);
  69. zipCloseFileInZip(z);
  70. }
  71. void addFolderToZip(zipFile z, String folderPath, String parentFolder, bool silent) {
  72. std::vector<OSFileEntry> files = OSBasics::parseFolder(folderPath, false);
  73. for(int i=0; i < files.size(); i++) {
  74. if(files[i].type == OSFileEntry::TYPE_FILE) {
  75. String pathInZip;
  76. if(parentFolder == "") {
  77. pathInZip = files[i].name;
  78. } else {
  79. pathInZip = parentFolder + "/" + files[i].name;
  80. }
  81. addFileToZip(z, files[i].fullPath, pathInZip, silent);
  82. } else {
  83. addFolderToZip(z, files[i].fullPath.c_str(), files[i].name, silent);
  84. }
  85. }
  86. }
  87. int main(int argc, char **argv) {
  88. printf("Polycode build tool v0.1.1\n");
  89. for(int i=0; i < argc; i++) {
  90. String argString = String(argv[i]);
  91. vector<String> bits = argString.split("=");
  92. if(bits.size() == 2) {
  93. BuildArg arg;
  94. arg.name = bits[0];
  95. arg.value = bits[1];
  96. args.push_back(arg);
  97. }
  98. }
  99. if(getArg("--config") == "") {
  100. printf("\n\nInput config XML missing. Use --config=path to specify.\n\n");
  101. return 1;
  102. }
  103. if(getArg("--out") == "") {
  104. printf("\n\nOutput file not specified. Use --out=outfile.polyapp to specify.\n\n");
  105. return 1;
  106. }
  107. char dirPath[4098];
  108. getcwd(dirPath, sizeof(dirPath));
  109. String currentPath = String(dirPath);
  110. String configPath = getArg("--config");
  111. String finalPath = configPath;
  112. if(configPath[0] != '/') {
  113. finalPath = currentPath+"/"+configPath;
  114. }
  115. printf("Reading config file from %s\n", finalPath.c_str());
  116. Object configFile;
  117. if(!configFile.loadFromXML(finalPath)) {
  118. printf("Specified config file doesn't exist!\n");
  119. return 1;
  120. }
  121. // start required params
  122. String entryPoint;
  123. int defaultWidth;
  124. int defaultHeight;
  125. int frameRate = 60;
  126. int antiAliasingLevel = 0;
  127. bool fullScreen = false;
  128. float backgroundColorR = 0.2;
  129. float backgroundColorG = 0.2;
  130. float backgroundColorB = 0.2;
  131. if(configFile.root["entryPoint"]) {
  132. printf("Entry point: %s\n", configFile.root["entryPoint"]->stringVal.c_str());
  133. entryPoint = configFile.root["entryPoint"]->stringVal;
  134. } else {
  135. printf("Required parameter: \"entryPoint\" is missing from config file!\n");
  136. return 1;
  137. }
  138. if(configFile.root["defaultWidth"]) {
  139. printf("Width: %d\n", configFile.root["defaultWidth"]->intVal);
  140. defaultWidth = configFile.root["defaultWidth"]->intVal;
  141. } else {
  142. printf("Required parameter: \"defaultWidth\" is missing from config file!\n");
  143. return 1;
  144. }
  145. if(configFile.root["defaultHeight"]) {
  146. printf("Height: %d\n", configFile.root["defaultHeight"]->intVal);
  147. defaultHeight = configFile.root["defaultHeight"]->intVal;
  148. } else {
  149. printf("Required parameter: \"defaultHeight\" is missing from config file!\n");
  150. return 1;
  151. }
  152. // start optional params
  153. if(configFile.root["frameRate"]) {
  154. printf("Frame rate: %d\n", configFile.root["frameRate"]->intVal);
  155. frameRate = configFile.root["frameRate"]->intVal;
  156. }
  157. if(configFile.root["antiAliasingLevel"]) {
  158. printf("Anti-aliasing level: %d\n", configFile.root["antiAliasingLevel"]->intVal);
  159. antiAliasingLevel = configFile.root["antiAliasingLevel"]->intVal;
  160. }
  161. if(configFile.root["fullScreen"]) {
  162. fullScreen = configFile.root["fullScreen"]->boolVal;
  163. if(fullScreen) {
  164. printf("Full-screen: true\n");
  165. } else {
  166. printf("Full-screen: false\n");
  167. }
  168. }
  169. if(configFile.root["backgroundColor"]) {
  170. ObjectEntry *color = configFile.root["backgroundColor"];
  171. if((*color)["red"] && (*color)["green"] && (*color)["blue"]) {
  172. backgroundColorR = (*color)["red"]->NumberVal;
  173. backgroundColorG = (*color)["green"]->NumberVal;
  174. backgroundColorB = (*color)["blue"]->NumberVal;
  175. printf("Background color: %f %f %f\n", backgroundColorR, backgroundColorG, backgroundColorB);
  176. } else {
  177. printf("backgroundColor node specified, but missing all three color attributes (red,green,blue). Ignoring.\n");
  178. }
  179. }
  180. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  181. Object runInfo;
  182. runInfo.root.name = "PolycodeApp";
  183. runInfo.root.addChild("entryPoint", entryPoint);
  184. runInfo.root.addChild("defaultHeight", defaultHeight);
  185. runInfo.root.addChild("defaultWidth", defaultWidth);
  186. runInfo.root.addChild("frameRate", frameRate);
  187. runInfo.root.addChild("antiAliasingLevel", antiAliasingLevel);
  188. runInfo.root.addChild("fullScreen", fullScreen);
  189. ObjectEntry *color = runInfo.root.addChild("backgroundColor");
  190. color->addChild("red", backgroundColorR);
  191. color->addChild("green", backgroundColorG);
  192. color->addChild("blue", backgroundColorB);
  193. addFileToZip(z, entryPoint, entryPoint, false);
  194. if(configFile.root["packedItems"]) {
  195. ObjectEntry *packed = configFile.root["packedItems"];
  196. if(packed) {
  197. for(int i=0; i < packed->length; i++) {
  198. ObjectEntry *entryPath = (*(*packed)[i])["path"];
  199. ObjectEntry *entryType = (*(*packed)[i])["type"];
  200. if(entryPath && entryType) {
  201. if(entryType->stringVal == "folder") {
  202. addFolderToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  203. } else {
  204. addFileToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. runInfo.root.addChild(configFile.root["packedItems"]);
  211. runInfo.saveToXML("runinfo_tmp_zzzz.polyrun");
  212. addFileToZip(z, "runinfo_tmp_zzzz.polyrun", "runinfo.polyrun", true);
  213. //addFolderToZip(z, getArg("--project"), "");
  214. zipClose(z, "");
  215. OSBasics::removeItem("runinfo_tmp_zzzz.polyrun");
  216. return 0;
  217. }