polybuild.cpp 11 KB

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