polybuild.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "polybuild.h"
  2. #include "zip.h"
  3. #if defined(__APPLE__) && defined(__MACH__)
  4. #include <mach-o/dyld.h>
  5. #endif
  6. vector<BuildArg> args;
  7. #define MAXFILENAME (256)
  8. String getArg(String argName) {
  9. /*
  10. if(argName == "--config")
  11. return "ExampleProject.xml";
  12. if(argName == "--out")
  13. return "ExampleProject.polyapp";
  14. */
  15. for(int i=0; i < args.size(); i++) {
  16. if(args[i].name == argName) {
  17. return args[i].value;
  18. }
  19. }
  20. return "";
  21. }
  22. uLong filetime(
  23. const char *f,
  24. tm_zip *tmzip,
  25. uLong *dt)
  26. {
  27. int ret=0;
  28. struct stat s; /* results of stat() */
  29. struct tm* filedate;
  30. time_t tm_t=0;
  31. if (strcmp(f,"-")!=0)
  32. {
  33. char name[MAXFILENAME+1];
  34. int len = strlen(f);
  35. if (len > MAXFILENAME)
  36. len = MAXFILENAME;
  37. strncpy(name, f,MAXFILENAME-1);
  38. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  39. name[ MAXFILENAME ] = '\0';
  40. if (name[len - 1] == '/')
  41. name[len - 1] = '\0';
  42. /* not all systems allow stat'ing a file with / appended */
  43. #ifdef _WINDOWS
  44. #else
  45. if (stat(name,&s)==0)
  46. {
  47. tm_t = s.st_mtime;
  48. ret = 1;
  49. }
  50. #endif
  51. }
  52. filedate = localtime(&tm_t);
  53. tmzip->tm_sec = filedate->tm_sec;
  54. tmzip->tm_min = filedate->tm_min;
  55. tmzip->tm_hour = filedate->tm_hour;
  56. tmzip->tm_mday = filedate->tm_mday;
  57. tmzip->tm_mon = filedate->tm_mon ;
  58. tmzip->tm_year = filedate->tm_year;
  59. return ret;
  60. }
  61. void addFileToZip(zipFile z, String filePath, String pathInZip, bool silent) {
  62. if(!silent)
  63. printf("Packaging %s as %s\n", filePath.c_str(), pathInZip.c_str());
  64. zip_fileinfo zi;
  65. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  66. zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  67. zi.dosDate = 0;
  68. zi.internal_fa = 0;
  69. zi.external_fa = 0;
  70. filetime(filePath.c_str(),&zi.tmz_date,&zi.dosDate);
  71. zipOpenNewFileInZip(z, pathInZip.c_str(), &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 2);
  72. FILE *f = fopen(filePath.c_str(), "rb");
  73. fseek(f, 0, SEEK_END);
  74. long fileSize = ftell(f);
  75. fseek(f, 0, SEEK_SET);
  76. char *buf = (char*) malloc(fileSize);
  77. fread(buf, fileSize, 1, f);
  78. zipWriteInFileInZip(z, buf, fileSize);
  79. free(buf);
  80. fclose(f);
  81. zipCloseFileInZip(z);
  82. }
  83. void addFolderToZip(zipFile z, String folderPath, String parentFolder, bool silent) {
  84. std::vector<OSFileEntry> files = OSBasics::parseFolder(folderPath, false);
  85. for(int i=0; i < files.size(); i++) {
  86. if(files[i].type == OSFileEntry::TYPE_FILE) {
  87. String pathInZip;
  88. if(parentFolder == "") {
  89. pathInZip = files[i].name;
  90. } else {
  91. pathInZip = parentFolder + "/" + files[i].name;
  92. }
  93. addFileToZip(z, files[i].fullPath, pathInZip, silent);
  94. } else {
  95. if(parentFolder == "") {
  96. addFolderToZip(z, files[i].fullPath.c_str(), files[i].name, silent);
  97. } else {
  98. addFolderToZip(z, files[i].fullPath.c_str(), parentFolder + "/" + files[i].name, silent);
  99. }
  100. }
  101. }
  102. }
  103. #ifdef _WINDOWS
  104. void wtoc(char* Dest, TCHAR* Source, int SourceSize)
  105. {
  106. for(int i = 0; i < SourceSize; ++i)
  107. Dest[i] = (char)Source[i];
  108. }
  109. #endif
  110. int main(int argc, char **argv) {
  111. #if defined(__APPLE__) && defined(__MACH__)
  112. uint32_t bufsize = 2048;
  113. char path[bufsize];
  114. _NSGetExecutablePath(path, &bufsize);
  115. String basePath = path;
  116. vector<String> cpts = basePath.split("/");
  117. String installPath = "";
  118. for(int i=0; i < cpts.size() - 2; i++) {
  119. installPath = installPath + cpts[i];
  120. installPath += String("/");
  121. }
  122. #elif defined (_WINDOWS)
  123. char path[2049];
  124. TCHAR tpath[2049];
  125. GetModuleFileName(NULL, (LPWSTR)tpath, 2048);
  126. wtoc(path, tpath, 2048);
  127. String basePath = path;
  128. vector<String> cpts = basePath.split("\\");
  129. String installPath = "";
  130. for(int i=0; i < cpts.size() - 2; i++) {
  131. installPath = installPath + cpts[i];
  132. installPath += String("\\");
  133. }
  134. #else
  135. String basePath = PHYSFS_getBaseDir();
  136. vector<String> cpts = basePath.split("/");
  137. String installPath = "";
  138. for(int i=0; i < cpts.size() - 2; i++) {
  139. installPath = installPath + cpts[i];
  140. installPath += String("/");
  141. }
  142. #endif
  143. printf("Polycode build tool v0.1.1\n");
  144. for(int i=0; i < argc; i++) {
  145. String argString = String(argv[i]);
  146. vector<String> bits = argString.split("=");
  147. if(bits.size() == 2) {
  148. BuildArg arg;
  149. arg.name = bits[0];
  150. arg.value = bits[1];
  151. args.push_back(arg);
  152. }
  153. }
  154. if(getArg("--config") == "") {
  155. printf("\n\nInput config XML missing. Use --config=path to specify.\n\n");
  156. return 1;
  157. }
  158. if(getArg("--out") == "") {
  159. printf("\n\nOutput file not specified. Use --out=outfile.polyapp to specify.\n\n");
  160. return 1;
  161. }
  162. char dirPath[4099];
  163. #if defined(__APPLE__) && defined(__MACH__)
  164. getcwd(dirPath, sizeof(dirPath));
  165. #elif defined (_WINDOWS)
  166. TCHAR tdirpath[4099];
  167. GetCurrentDirectory(4098, (LPWSTR)tdirpath);
  168. wtoc(dirPath, tdirpath, 4098);
  169. #else
  170. getcwd(dirPath, sizeof(dirPath));
  171. #endif
  172. String currentPath = String(dirPath);
  173. String configPath = getArg("--config");
  174. String finalPath = configPath;
  175. if(configPath[0] != '/') {
  176. #ifdef _WINDOWS
  177. finalPath = currentPath+"\\"+configPath;
  178. #else
  179. finalPath = currentPath+"/"+configPath;
  180. #endif
  181. }
  182. #ifdef _WINDOWS
  183. finalPath = finalPath.replace(":", "");
  184. finalPath = finalPath.replace("\\", "/");
  185. finalPath = finalPath.substr(1, finalPath.length() - 1);
  186. #endif
  187. printf("Reading config file from %s\n", finalPath.c_str());
  188. Object configFile;
  189. if(!configFile.loadFromXML(finalPath)) {
  190. printf("Specified config file doesn't exist!\n");
  191. return 1;
  192. }
  193. printf("OK!\n");
  194. // start required params
  195. String entryPoint;
  196. int defaultWidth;
  197. int defaultHeight;
  198. int frameRate = 60;
  199. int antiAliasingLevel = 0;
  200. bool fullScreen = false;
  201. float backgroundColorR = 0.2;
  202. float backgroundColorG = 0.2;
  203. float backgroundColorB = 0.2;
  204. if(configFile.root["entryPoint"]) {
  205. printf("Entry point: %s\n", configFile.root["entryPoint"]->stringVal.c_str());
  206. entryPoint = configFile.root["entryPoint"]->stringVal;
  207. } else {
  208. printf("Required parameter: \"entryPoint\" is missing from config file!\n");
  209. return 1;
  210. }
  211. if(configFile.root["defaultWidth"]) {
  212. printf("Width: %d\n", configFile.root["defaultWidth"]->intVal);
  213. defaultWidth = configFile.root["defaultWidth"]->intVal;
  214. } else {
  215. printf("Required parameter: \"defaultWidth\" is missing from config file!\n");
  216. return 1;
  217. }
  218. if(configFile.root["defaultHeight"]) {
  219. printf("Height: %d\n", configFile.root["defaultHeight"]->intVal);
  220. defaultHeight = configFile.root["defaultHeight"]->intVal;
  221. } else {
  222. printf("Required parameter: \"defaultHeight\" is missing from config file!\n");
  223. return 1;
  224. }
  225. // start optional params
  226. if(configFile.root["frameRate"]) {
  227. printf("Frame rate: %d\n", configFile.root["frameRate"]->intVal);
  228. frameRate = configFile.root["frameRate"]->intVal;
  229. }
  230. if(configFile.root["antiAliasingLevel"]) {
  231. printf("Anti-aliasing level: %d\n", configFile.root["antiAliasingLevel"]->intVal);
  232. antiAliasingLevel = configFile.root["antiAliasingLevel"]->intVal;
  233. }
  234. if(configFile.root["fullScreen"]) {
  235. fullScreen = configFile.root["fullScreen"]->boolVal;
  236. if(fullScreen) {
  237. printf("Full-screen: true\n");
  238. } else {
  239. printf("Full-screen: false\n");
  240. }
  241. }
  242. if(configFile.root["backgroundColor"]) {
  243. ObjectEntry *color = configFile.root["backgroundColor"];
  244. if((*color)["red"] && (*color)["green"] && (*color)["blue"]) {
  245. backgroundColorR = (*color)["red"]->NumberVal;
  246. backgroundColorG = (*color)["green"]->NumberVal;
  247. backgroundColorB = (*color)["blue"]->NumberVal;
  248. printf("Background color: %f %f %f\n", backgroundColorR, backgroundColorG, backgroundColorB);
  249. } else {
  250. printf("backgroundColor node specified, but missing all three color attributes (red,green,blue). Ignoring.\n");
  251. }
  252. }
  253. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  254. Object runInfo;
  255. runInfo.root.name = "PolycodeApp";
  256. runInfo.root.addChild("entryPoint", entryPoint);
  257. runInfo.root.addChild("defaultHeight", defaultHeight);
  258. runInfo.root.addChild("defaultWidth", defaultWidth);
  259. runInfo.root.addChild("frameRate", frameRate);
  260. runInfo.root.addChild("antiAliasingLevel", antiAliasingLevel);
  261. runInfo.root.addChild("fullScreen", fullScreen);
  262. ObjectEntry *color = runInfo.root.addChild("backgroundColor");
  263. color->addChild("red", backgroundColorR);
  264. color->addChild("green", backgroundColorG);
  265. color->addChild("blue", backgroundColorB);
  266. addFileToZip(z, entryPoint, entryPoint, false);
  267. if(configFile.root["modules"]) {
  268. #ifdef _WINDOWS
  269. String modulesPath = installPath + "Modules\\";
  270. #else
  271. String modulesPath = installPath + "Modules/";
  272. #endif
  273. ObjectEntry *modules = configFile.root["modules"];
  274. if(modules) {
  275. for(int i=0; i < modules->length; i++) {
  276. printf("Adding module: %s\n", (*modules)[i]->stringVal.c_str());
  277. String modulePath = modulesPath + (*modules)[i]->stringVal;
  278. #ifdef _WINDOWS
  279. String moduleAPIPath = modulePath + "\\API";
  280. String moduleLibPath = modulePath + "\\Lib";
  281. moduleAPIPath = moduleAPIPath.replace("\\", "/");
  282. moduleAPIPath = moduleAPIPath.substr(2, moduleAPIPath.length() - 2);
  283. moduleLibPath = moduleLibPath.replace("\\", "/");
  284. moduleLibPath = moduleLibPath.substr(2, moduleLibPath.length() - 2);
  285. #else
  286. String moduleAPIPath = modulePath + "/API";
  287. String moduleLibPath = modulePath + "/Lib";
  288. #endif
  289. printf("Path:%s\n", moduleAPIPath.c_str());
  290. addFolderToZip(z, moduleAPIPath, "", false);
  291. addFolderToZip(z, moduleLibPath, "__lib", false);
  292. //String module = configFile.root["entryPoint"]->stringVal;
  293. }
  294. runInfo.root.addChild(configFile.root["modules"]);
  295. }
  296. }
  297. if(configFile.root["packedItems"]) {
  298. ObjectEntry *packed = configFile.root["packedItems"];
  299. if(packed) {
  300. for(int i=0; i < packed->length; i++) {
  301. ObjectEntry *entryPath = (*(*packed)[i])["path"];
  302. ObjectEntry *entryType = (*(*packed)[i])["type"];
  303. if(entryPath && entryType) {
  304. if(entryType->stringVal == "folder") {
  305. addFolderToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  306. } else {
  307. addFileToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  308. }
  309. }
  310. }
  311. runInfo.root.addChild(configFile.root["packedItems"]);
  312. }
  313. }
  314. runInfo.saveToXML("runinfo_tmp_zzzz.polyrun");
  315. addFileToZip(z, "runinfo_tmp_zzzz.polyrun", "runinfo.polyrun", true);
  316. //addFolderToZip(z, getArg("--project"), "");
  317. zipClose(z, "");
  318. OSBasics::removeItem("runinfo_tmp_zzzz.polyrun");
  319. return 0;
  320. }