polybuild.cpp 12 KB

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