polybuild.cpp 11 KB

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