polybuild.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. char path[2049];
  113. _NSGetExecutablePath(path, 2048);
  114. String basePath = path;
  115. vector<String> cpts = basePath.split("/");
  116. String installPath = "";
  117. for(int i=0; i < cpts.size() - 2; i++) {
  118. installPath = installPath + cpts[i];
  119. installPath += String("/");
  120. }
  121. #else
  122. char path[2049];
  123. TCHAR tpath[2049];
  124. GetModuleFileName(NULL, (LPWSTR)tpath, 2048);
  125. wtoc(path, tpath, 2048);
  126. String basePath = path;
  127. vector<String> cpts = basePath.split("\\");
  128. String installPath = "";
  129. for(int i=0; i < cpts.size() - 2; i++) {
  130. installPath = installPath + cpts[i];
  131. installPath += String("\\");
  132. }
  133. #endif
  134. printf("Polycode build tool v0.1.1\n");
  135. for(int i=0; i < argc; i++) {
  136. String argString = String(argv[i]);
  137. vector<String> bits = argString.split("=");
  138. if(bits.size() == 2) {
  139. BuildArg arg;
  140. arg.name = bits[0];
  141. arg.value = bits[1];
  142. args.push_back(arg);
  143. }
  144. }
  145. if(getArg("--config") == "") {
  146. printf("\n\nInput config XML missing. Use --config=path to specify.\n\n");
  147. return 1;
  148. }
  149. if(getArg("--out") == "") {
  150. printf("\n\nOutput file not specified. Use --out=outfile.polyapp to specify.\n\n");
  151. return 1;
  152. }
  153. char dirPath[4099];
  154. #if defined(__APPLE__) && defined(__MACH__)
  155. _getcwd(dirPath, sizeof(dirPath));
  156. #else
  157. TCHAR tdirpath[4099];
  158. GetCurrentDirectory(4098, (LPWSTR)tdirpath);
  159. wtoc(dirPath, tdirpath, 4098);
  160. #endif
  161. String currentPath = String(dirPath);
  162. String configPath = getArg("--config");
  163. String finalPath = configPath;
  164. if(configPath[0] != '/') {
  165. #ifdef _WINDOWS
  166. finalPath = currentPath+"\\"+configPath;
  167. #else
  168. finalPath = currentPath+"/"+configPath;
  169. #endif
  170. }
  171. finalPath = finalPath.replace(":", "");
  172. finalPath = finalPath.replace("\\", "/");
  173. finalPath = finalPath.substr(1, finalPath.length() - 1);
  174. printf("Reading config file from %s\n", finalPath.c_str());
  175. Object configFile;
  176. if(!configFile.loadFromXML(finalPath)) {
  177. printf("Specified config file doesn't exist!\n");
  178. return 1;
  179. }
  180. printf("OK!\n");
  181. // start required params
  182. String entryPoint;
  183. int defaultWidth;
  184. int defaultHeight;
  185. int frameRate = 60;
  186. int antiAliasingLevel = 0;
  187. bool fullScreen = false;
  188. float backgroundColorR = 0.2;
  189. float backgroundColorG = 0.2;
  190. float backgroundColorB = 0.2;
  191. if(configFile.root["entryPoint"]) {
  192. printf("Entry point: %s\n", configFile.root["entryPoint"]->stringVal.c_str());
  193. entryPoint = configFile.root["entryPoint"]->stringVal;
  194. } else {
  195. printf("Required parameter: \"entryPoint\" is missing from config file!\n");
  196. return 1;
  197. }
  198. if(configFile.root["defaultWidth"]) {
  199. printf("Width: %d\n", configFile.root["defaultWidth"]->intVal);
  200. defaultWidth = configFile.root["defaultWidth"]->intVal;
  201. } else {
  202. printf("Required parameter: \"defaultWidth\" is missing from config file!\n");
  203. return 1;
  204. }
  205. if(configFile.root["defaultHeight"]) {
  206. printf("Height: %d\n", configFile.root["defaultHeight"]->intVal);
  207. defaultHeight = configFile.root["defaultHeight"]->intVal;
  208. } else {
  209. printf("Required parameter: \"defaultHeight\" is missing from config file!\n");
  210. return 1;
  211. }
  212. // start optional params
  213. if(configFile.root["frameRate"]) {
  214. printf("Frame rate: %d\n", configFile.root["frameRate"]->intVal);
  215. frameRate = configFile.root["frameRate"]->intVal;
  216. }
  217. if(configFile.root["antiAliasingLevel"]) {
  218. printf("Anti-aliasing level: %d\n", configFile.root["antiAliasingLevel"]->intVal);
  219. antiAliasingLevel = configFile.root["antiAliasingLevel"]->intVal;
  220. }
  221. if(configFile.root["fullScreen"]) {
  222. fullScreen = configFile.root["fullScreen"]->boolVal;
  223. if(fullScreen) {
  224. printf("Full-screen: true\n");
  225. } else {
  226. printf("Full-screen: false\n");
  227. }
  228. }
  229. if(configFile.root["backgroundColor"]) {
  230. ObjectEntry *color = configFile.root["backgroundColor"];
  231. if((*color)["red"] && (*color)["green"] && (*color)["blue"]) {
  232. backgroundColorR = (*color)["red"]->NumberVal;
  233. backgroundColorG = (*color)["green"]->NumberVal;
  234. backgroundColorB = (*color)["blue"]->NumberVal;
  235. printf("Background color: %f %f %f\n", backgroundColorR, backgroundColorG, backgroundColorB);
  236. } else {
  237. printf("backgroundColor node specified, but missing all three color attributes (red,green,blue). Ignoring.\n");
  238. }
  239. }
  240. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  241. Object runInfo;
  242. runInfo.root.name = "PolycodeApp";
  243. runInfo.root.addChild("entryPoint", entryPoint);
  244. runInfo.root.addChild("defaultHeight", defaultHeight);
  245. runInfo.root.addChild("defaultWidth", defaultWidth);
  246. runInfo.root.addChild("frameRate", frameRate);
  247. runInfo.root.addChild("antiAliasingLevel", antiAliasingLevel);
  248. runInfo.root.addChild("fullScreen", fullScreen);
  249. ObjectEntry *color = runInfo.root.addChild("backgroundColor");
  250. color->addChild("red", backgroundColorR);
  251. color->addChild("green", backgroundColorG);
  252. color->addChild("blue", backgroundColorB);
  253. addFileToZip(z, entryPoint, entryPoint, false);
  254. if(configFile.root["modules"]) {
  255. #ifdef _WINDOWS
  256. String modulesPath = installPath + "Modules\\";
  257. #else
  258. String modulesPath = installPath + "Modules/";
  259. #endif
  260. ObjectEntry *modules = configFile.root["modules"];
  261. if(modules) {
  262. for(int i=0; i < modules->length; i++) {
  263. printf("Adding module: %s\n", (*modules)[i]->stringVal.c_str());
  264. String modulePath = modulesPath + (*modules)[i]->stringVal;
  265. #ifdef _WINDOWS
  266. String moduleAPIPath = modulePath + "\\API";
  267. String moduleLibPath = modulePath + "\\Lib";
  268. moduleAPIPath = moduleAPIPath.replace("\\", "/");
  269. moduleAPIPath = moduleAPIPath.substr(2, moduleAPIPath.length() - 2);
  270. moduleLibPath = moduleLibPath.replace("\\", "/");
  271. moduleLibPath = moduleLibPath.substr(2, moduleLibPath.length() - 2);
  272. #else
  273. String moduleAPIPath = modulePath + "/API";
  274. String moduleLibPath = modulePath + "/Lib";
  275. #endif
  276. printf("Path:%s\n", moduleAPIPath.c_str());
  277. addFolderToZip(z, moduleAPIPath, "", false);
  278. addFolderToZip(z, moduleLibPath, "__lib", false);
  279. //String module = configFile.root["entryPoint"]->stringVal;
  280. }
  281. runInfo.root.addChild(configFile.root["modules"]);
  282. }
  283. }
  284. if(configFile.root["packedItems"]) {
  285. ObjectEntry *packed = configFile.root["packedItems"];
  286. if(packed) {
  287. for(int i=0; i < packed->length; i++) {
  288. ObjectEntry *entryPath = (*(*packed)[i])["path"];
  289. ObjectEntry *entryType = (*(*packed)[i])["type"];
  290. if(entryPath && entryType) {
  291. if(entryType->stringVal == "folder") {
  292. addFolderToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  293. } else {
  294. addFileToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  295. }
  296. }
  297. }
  298. runInfo.root.addChild(configFile.root["packedItems"]);
  299. }
  300. }
  301. runInfo.saveToXML("runinfo_tmp_zzzz.polyrun");
  302. addFileToZip(z, "runinfo_tmp_zzzz.polyrun", "runinfo.polyrun", true);
  303. //addFolderToZip(z, getArg("--project"), "");
  304. zipClose(z, "");
  305. OSBasics::removeItem("runinfo_tmp_zzzz.polyrun");
  306. return 0;
  307. }