polybuild.cpp 10 KB

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