polybuild.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. finalPath = finalPath.replace(":", "");
  183. finalPath = finalPath.replace("\\", "/");
  184. finalPath = finalPath.substr(1, finalPath.length() - 1);
  185. printf("Reading config file from %s\n", finalPath.c_str());
  186. Object configFile;
  187. if(!configFile.loadFromXML(finalPath)) {
  188. printf("Specified config file doesn't exist!\n");
  189. return 1;
  190. }
  191. printf("OK!\n");
  192. // start required params
  193. String entryPoint;
  194. int defaultWidth;
  195. int defaultHeight;
  196. int frameRate = 60;
  197. int antiAliasingLevel = 0;
  198. bool fullScreen = false;
  199. float backgroundColorR = 0.2;
  200. float backgroundColorG = 0.2;
  201. float backgroundColorB = 0.2;
  202. if(configFile.root["entryPoint"]) {
  203. printf("Entry point: %s\n", configFile.root["entryPoint"]->stringVal.c_str());
  204. entryPoint = configFile.root["entryPoint"]->stringVal;
  205. } else {
  206. printf("Required parameter: \"entryPoint\" is missing from config file!\n");
  207. return 1;
  208. }
  209. if(configFile.root["defaultWidth"]) {
  210. printf("Width: %d\n", configFile.root["defaultWidth"]->intVal);
  211. defaultWidth = configFile.root["defaultWidth"]->intVal;
  212. } else {
  213. printf("Required parameter: \"defaultWidth\" is missing from config file!\n");
  214. return 1;
  215. }
  216. if(configFile.root["defaultHeight"]) {
  217. printf("Height: %d\n", configFile.root["defaultHeight"]->intVal);
  218. defaultHeight = configFile.root["defaultHeight"]->intVal;
  219. } else {
  220. printf("Required parameter: \"defaultHeight\" is missing from config file!\n");
  221. return 1;
  222. }
  223. // start optional params
  224. if(configFile.root["frameRate"]) {
  225. printf("Frame rate: %d\n", configFile.root["frameRate"]->intVal);
  226. frameRate = configFile.root["frameRate"]->intVal;
  227. }
  228. if(configFile.root["antiAliasingLevel"]) {
  229. printf("Anti-aliasing level: %d\n", configFile.root["antiAliasingLevel"]->intVal);
  230. antiAliasingLevel = configFile.root["antiAliasingLevel"]->intVal;
  231. }
  232. if(configFile.root["fullScreen"]) {
  233. fullScreen = configFile.root["fullScreen"]->boolVal;
  234. if(fullScreen) {
  235. printf("Full-screen: true\n");
  236. } else {
  237. printf("Full-screen: false\n");
  238. }
  239. }
  240. if(configFile.root["backgroundColor"]) {
  241. ObjectEntry *color = configFile.root["backgroundColor"];
  242. if((*color)["red"] && (*color)["green"] && (*color)["blue"]) {
  243. backgroundColorR = (*color)["red"]->NumberVal;
  244. backgroundColorG = (*color)["green"]->NumberVal;
  245. backgroundColorB = (*color)["blue"]->NumberVal;
  246. printf("Background color: %f %f %f\n", backgroundColorR, backgroundColorG, backgroundColorB);
  247. } else {
  248. printf("backgroundColor node specified, but missing all three color attributes (red,green,blue). Ignoring.\n");
  249. }
  250. }
  251. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  252. Object runInfo;
  253. runInfo.root.name = "PolycodeApp";
  254. runInfo.root.addChild("entryPoint", entryPoint);
  255. runInfo.root.addChild("defaultHeight", defaultHeight);
  256. runInfo.root.addChild("defaultWidth", defaultWidth);
  257. runInfo.root.addChild("frameRate", frameRate);
  258. runInfo.root.addChild("antiAliasingLevel", antiAliasingLevel);
  259. runInfo.root.addChild("fullScreen", fullScreen);
  260. ObjectEntry *color = runInfo.root.addChild("backgroundColor");
  261. color->addChild("red", backgroundColorR);
  262. color->addChild("green", backgroundColorG);
  263. color->addChild("blue", backgroundColorB);
  264. addFileToZip(z, entryPoint, entryPoint, false);
  265. if(configFile.root["modules"]) {
  266. #ifdef _WINDOWS
  267. String modulesPath = installPath + "Modules\\";
  268. #else
  269. String modulesPath = installPath + "Modules/";
  270. #endif
  271. ObjectEntry *modules = configFile.root["modules"];
  272. if(modules) {
  273. for(int i=0; i < modules->length; i++) {
  274. printf("Adding module: %s\n", (*modules)[i]->stringVal.c_str());
  275. String modulePath = modulesPath + (*modules)[i]->stringVal;
  276. #ifdef _WINDOWS
  277. String moduleAPIPath = modulePath + "\\API";
  278. String moduleLibPath = modulePath + "\\Lib";
  279. moduleAPIPath = moduleAPIPath.replace("\\", "/");
  280. moduleAPIPath = moduleAPIPath.substr(2, moduleAPIPath.length() - 2);
  281. moduleLibPath = moduleLibPath.replace("\\", "/");
  282. moduleLibPath = moduleLibPath.substr(2, moduleLibPath.length() - 2);
  283. #else
  284. String moduleAPIPath = modulePath + "/API";
  285. String moduleLibPath = modulePath + "/Lib";
  286. #endif
  287. printf("Path:%s\n", moduleAPIPath.c_str());
  288. addFolderToZip(z, moduleAPIPath, "", false);
  289. addFolderToZip(z, moduleLibPath, "__lib", false);
  290. //String module = configFile.root["entryPoint"]->stringVal;
  291. }
  292. runInfo.root.addChild(configFile.root["modules"]);
  293. }
  294. }
  295. if(configFile.root["packedItems"]) {
  296. ObjectEntry *packed = configFile.root["packedItems"];
  297. if(packed) {
  298. for(int i=0; i < packed->length; i++) {
  299. ObjectEntry *entryPath = (*(*packed)[i])["path"];
  300. ObjectEntry *entryType = (*(*packed)[i])["type"];
  301. if(entryPath && entryType) {
  302. if(entryType->stringVal == "folder") {
  303. addFolderToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  304. } else {
  305. addFileToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  306. }
  307. }
  308. }
  309. runInfo.root.addChild(configFile.root["packedItems"]);
  310. }
  311. }
  312. runInfo.saveToXML("runinfo_tmp_zzzz.polyrun");
  313. addFileToZip(z, "runinfo_tmp_zzzz.polyrun", "runinfo.polyrun", true);
  314. //addFolderToZip(z, getArg("--project"), "");
  315. zipClose(z, "");
  316. OSBasics::removeItem("runinfo_tmp_zzzz.polyrun");
  317. return 0;
  318. }