polybuild.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "polybuild.h"
  2. #include "string.h"
  3. #include "zip.h"
  4. #ifdef _WINDOWS
  5. #include <windows.h>
  6. #include <direct.h>
  7. #else
  8. #include <dirent.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #endif
  12. #include "physfs.h"
  13. #if defined(__APPLE__) && defined(__MACH__)
  14. #include <mach-o/dyld.h>
  15. #endif
  16. using std::vector;
  17. vector<BuildArg> args;
  18. #define MAXFILENAME (2048)
  19. String getArg(String argName) {
  20. /*
  21. if(argName == "--config")
  22. return "ExampleProject.xml";
  23. if(argName == "--out")
  24. return "ExampleProject.polyapp";
  25. */
  26. for(int i=0; i < args.size(); i++) {
  27. if(args[i].name == argName) {
  28. return args[i].value;
  29. }
  30. }
  31. return "";
  32. }
  33. uLong filetime(
  34. const char *f,
  35. tm_zip *tmzip,
  36. uLong *dt)
  37. {
  38. int ret=0;
  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. // printf("arg: %s=%s\n", arg.name.c_str(), arg.value.c_str());
  163. args.push_back(arg);
  164. }
  165. }
  166. if(getArg("--config") == "") {
  167. printf("\n\nInput config XML missing. Use --config=path to specify.\n\n");
  168. return 1;
  169. }
  170. if(getArg("--out") == "") {
  171. printf("\n\nOutput file not specified. Use --out=outfile.polyapp to specify.\n\n");
  172. return 1;
  173. }
  174. char dirPath[4099];
  175. #if defined(__APPLE__) && defined(__MACH__)
  176. getcwd(dirPath, sizeof(dirPath));
  177. #elif defined (_WINDOWS)
  178. TCHAR tdirpath[4099];
  179. GetCurrentDirectory(4098, (LPWSTR)tdirpath);
  180. wtoc(dirPath, tdirpath, 4098);
  181. #else
  182. getcwd(dirPath, sizeof(dirPath));
  183. #endif
  184. String currentPath = String(dirPath);
  185. String configPath = getArg("--config");
  186. String finalPath = configPath;
  187. if(configPath[0] != '/' && configPath[1] !=':') {
  188. #ifdef _WINDOWS
  189. finalPath = currentPath+"\\"+configPath;
  190. #else
  191. finalPath = currentPath+"/"+configPath;
  192. #endif
  193. }
  194. printf("Reading config file from %s\n", finalPath.c_str());
  195. Object configFile;
  196. if(!configFile.loadFromXML(finalPath)) {
  197. printf("Specified config file doesn't exist!\n");
  198. return 1;
  199. }
  200. printf("OK!\n");
  201. // start required params
  202. String entryPoint;
  203. int defaultWidth;
  204. int defaultHeight;
  205. int frameRate = 60;
  206. int antiAliasingLevel = 0;
  207. int anisotropyLevel = 0;
  208. bool vSync = false;
  209. bool fullScreen = false;
  210. float backgroundColorR = 0.2;
  211. float backgroundColorG = 0.2;
  212. float backgroundColorB = 0.2;
  213. String textureFiltering = "linear";
  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["textureFiltering"]) {
  241. printf("Filtering mode: %s\n", configFile.root["textureFiltering"]->stringVal.c_str());
  242. textureFiltering = configFile.root["textureFiltering"]->stringVal;
  243. }
  244. if(configFile.root["antiAliasingLevel"]) {
  245. printf("Anti-aliasing level: %d\n", configFile.root["antiAliasingLevel"]->intVal);
  246. antiAliasingLevel = configFile.root["antiAliasingLevel"]->intVal;
  247. }
  248. if(configFile.root["anisotropyLevel"]) {
  249. printf("Anisotropy level: %d\n", configFile.root["anisotropyLevel"]->intVal);
  250. anisotropyLevel = configFile.root["anisotropyLevel"]->intVal;
  251. }
  252. if(configFile.root["vSync"]) {
  253. vSync = configFile.root["vSync"]->boolVal;
  254. if(vSync) {
  255. printf("V-Sync: true\n");
  256. } else {
  257. printf("V-Sync: false\n");
  258. }
  259. }
  260. if(configFile.root["fullScreen"]) {
  261. fullScreen = configFile.root["fullScreen"]->boolVal;
  262. if(fullScreen) {
  263. printf("Full-screen: true\n");
  264. } else {
  265. printf("Full-screen: false\n");
  266. }
  267. }
  268. if(configFile.root["backgroundColor"]) {
  269. ObjectEntry *color = configFile.root["backgroundColor"];
  270. if((*color)["red"] && (*color)["green"] && (*color)["blue"]) {
  271. backgroundColorR = (*color)["red"]->NumberVal;
  272. backgroundColorG = (*color)["green"]->NumberVal;
  273. backgroundColorB = (*color)["blue"]->NumberVal;
  274. printf("Background color: %f %f %f\n", backgroundColorR, backgroundColorG, backgroundColorB);
  275. } else {
  276. printf("backgroundColor node specified, but missing all three color attributes (red,green,blue). Ignoring.\n");
  277. }
  278. }
  279. zipFile z = zipOpen(getArg("--out").c_str(), 0);
  280. Object runInfo;
  281. runInfo.root.name = "PolycodeApp";
  282. runInfo.root.addChild("entryPoint", entryPoint);
  283. runInfo.root.addChild("defaultHeight", defaultHeight);
  284. runInfo.root.addChild("defaultWidth", defaultWidth);
  285. runInfo.root.addChild("frameRate", frameRate);
  286. runInfo.root.addChild("antiAliasingLevel", antiAliasingLevel);
  287. runInfo.root.addChild("anisotropyLevel", anisotropyLevel);
  288. runInfo.root.addChild("vSync", vSync);
  289. runInfo.root.addChild("fullScreen", fullScreen);
  290. runInfo.root.addChild("textureFiltering", String(textureFiltering));
  291. ObjectEntry *color = runInfo.root.addChild("backgroundColor");
  292. color->addChild("red", backgroundColorR);
  293. color->addChild("green", backgroundColorG);
  294. color->addChild("blue", backgroundColorB);
  295. addFileToZip(z, entryPoint, entryPoint, false);
  296. if(configFile.root["fonts"]) {
  297. runInfo.root.addChild(configFile.root["fonts"]);
  298. }
  299. if(configFile.root["modules"]) {
  300. #ifdef _WINDOWS
  301. String modulesPath = installPath + "Modules\\";
  302. #else
  303. String modulesPath = installPath + "Modules/";
  304. #endif
  305. ObjectEntry *modules = configFile.root["modules"];
  306. if(modules) {
  307. for(int i=0; i < modules->length; i++) {
  308. printf("Adding module: %s\n", (*modules)[i]->stringVal.c_str());
  309. String modulePath = modulesPath + (*modules)[i]->stringVal;
  310. #ifdef _WINDOWS
  311. String moduleAPIPath = modulePath + "\\API";
  312. String moduleLibPath = modulePath + "\\Lib";
  313. moduleAPIPath = moduleAPIPath.replace("\\", "/");
  314. moduleAPIPath = moduleAPIPath.substr(2, moduleAPIPath.length() - 2);
  315. moduleLibPath = moduleLibPath.replace("\\", "/");
  316. moduleLibPath = moduleLibPath.substr(2, moduleLibPath.length() - 2);
  317. #else
  318. String moduleAPIPath = modulePath + "/API";
  319. String moduleLibPath = modulePath + "/Lib";
  320. #endif
  321. printf("Path:%s\n", moduleAPIPath.c_str());
  322. addFolderToZip(z, moduleAPIPath, "", false);
  323. addFolderToZip(z, moduleLibPath, "__lib", false);
  324. //String module = configFile.root["entryPoint"]->stringVal;
  325. }
  326. runInfo.root.addChild(configFile.root["modules"]);
  327. }
  328. }
  329. if(configFile.root["packedItems"]) {
  330. ObjectEntry *packed = configFile.root["packedItems"];
  331. if(packed) {
  332. for(int i=0; i < packed->length; i++) {
  333. ObjectEntry *entryPath = (*(*packed)[i])["path"];
  334. ObjectEntry *entryType = (*(*packed)[i])["type"];
  335. if(entryPath && entryType) {
  336. if(entryType->stringVal == "folder") {
  337. addFolderToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  338. } else {
  339. addFileToZip(z, entryPath->stringVal, entryPath->stringVal, false);
  340. }
  341. }
  342. }
  343. runInfo.root.addChild(configFile.root["packedItems"]);
  344. }
  345. }
  346. runInfo.saveToXML("runinfo_tmp_zzzz.polyrun");
  347. addFileToZip(z, "runinfo_tmp_zzzz.polyrun", "runinfo.polyrun", true);
  348. //addFolderToZip(z, getArg("--project"), "");
  349. zipClose(z, "");
  350. #ifdef _WINDOWS
  351. char *buffer = _getcwd(NULL, 0);
  352. String workingDir = String(buffer);
  353. free(buffer);
  354. OSBasics::removeItem(workingDir+"/runinfo_tmp_zzzz.polyrun");
  355. #else
  356. OSBasics::removeItem("runinfo_tmp_zzzz.polyrun");
  357. #endif
  358. return 0;
  359. }