polybuild.cpp 10 KB

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