WorldInstaller.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <tinyxml.h>
  20. #include <OgreRoot.h>
  21. #include <OgreMesh.h>
  22. #include <OgreMeshSerializer.h>
  23. #include <boost/filesystem.hpp>
  24. #include "WorldInstaller.h"
  25. #include "ModelMaterials.h"
  26. #include "TexFile.h"
  27. #include "common/Lzs.h"
  28. #include "common/FinalFantasy7/FF7NameLookup.h"
  29. #include "common/VGearsStringUtil.h"
  30. #include "data/VGearsLGPArchive.h"
  31. #include "data/WorldMapWalkmesh.h"
  32. #include "data/VGearsHRCFileManager.h"
  33. #include "data/FF7Data.h"
  34. #include "decompiler/ControlFlow.h"
  35. #include "decompiler/DecompilerException.h"
  36. #include "decompiler/world/WorldEngine.h"
  37. std::string WorldInstaller::ELEMENT_MODELS_DIR("models/world/element");
  38. std::string WorldInstaller::TERRAIN_MODELS_DIR("models/world/terrain");
  39. WorldInstaller::WorldInstaller(
  40. const DiskImage& disk_image, const std::string output_dir,
  41. const bool keep_originals, Ogre::ResourceGroupManager* res_mgr
  42. ): disk_image_(disk_image), output_dir_(output_dir), data_dir_(output_dir + "data/"), keep_originals_(keep_originals), res_mgr_(res_mgr) {}
  43. WorldInstaller::~WorldInstaller(){}
  44. unsigned int WorldInstaller::Initialize(){
  45. if (wm_map_.size() > 0) wm_map_.clear();
  46. processed_maps_ = 0;
  47. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM0.MAP")));
  48. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM2.MAP")));
  49. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM3.MAP")));
  50. // The game reads the original map files at runtime (data/wm), so install a copy.
  51. boost::filesystem::create_directories(data_dir_ + "wm/");
  52. for (const char* map : {"WM0.MAP", "WM2.MAP", "WM3.MAP"}){
  53. boost::filesystem::copy_file(
  54. *disk_image_.filePath(std::string("data/wm/") + map), data_dir_ + "wm/" + map,
  55. boost::filesystem::copy_options::overwrite_existing
  56. );
  57. }
  58. return wm_map_.size();
  59. }
  60. void WorldInstaller::GenerateMaterials(){
  61. // First, populate the texture list with static data.
  62. texture_.push_back({0, "pond", 32, 32, 0, 352});
  63. texture_.push_back({1, "riv_m2", 32, 32, 128, 64});
  64. texture_.push_back({2, "was_gs", 64, 64, 64, 192});
  65. texture_.push_back({3, "wpcltr", 32, 128, 0, 256});
  66. texture_.push_back({4, "wpcltr2", 32, 32, 160, 64});
  67. texture_.push_back({5, "bzdun", 64, 64, 192, 192});
  68. texture_.push_back({6, "bone", 32, 32, 224, 384});
  69. texture_.push_back({7, "bone2", 32, 32, 224, 416});
  70. texture_.push_back({8, "bornwd", 64, 64, 160, 320});
  71. texture_.push_back({9, "bridge", 32, 64, 192, 0});
  72. texture_.push_back({10, "bridge2", 32, 32, 224, 0});
  73. texture_.push_back({11, "cave", 32, 32, 224, 448});
  74. texture_.push_back({12, "cave2", 32, 32, 224, 320});
  75. texture_.push_back({13, "cave_s", 32, 32, 160, 224});
  76. texture_.push_back({14, "cdl_cl2", 64, 32, 96, 96});
  77. texture_.push_back({15, "cf01", 64, 32, 192, 288});
  78. texture_.push_back({16, "clf_bgs", 64, 32, 192, 384});
  79. texture_.push_back({17, "clf_ggl", 64, 64, 128, 256});
  80. texture_.push_back({18, "clf_ggs", 64, 32, 192, 352});
  81. texture_.push_back({19, "clf_l", 64, 64, 0, 0});
  82. texture_.push_back({20, "clf_ld", 64, 64, 64, 0});
  83. texture_.push_back({21, "clf_lf", 64, 64, 128, 0});
  84. texture_.push_back({22, "clf_lg", 32, 64, 0, 96});
  85. texture_.push_back({23, "clf_lr", 32, 64, 128, 0});
  86. texture_.push_back({24, "clf_lsg", 32, 64, 64, 64});
  87. texture_.push_back({25, "clf_r", 32, 32, 0, 96});
  88. texture_.push_back({26, "clf_s", 64, 32, 192, 0});
  89. texture_.push_back({27, "clf_sd", 64, 32, 192, 32});
  90. texture_.push_back({28, "clf_sf", 64, 32, 0, 64});
  91. texture_.push_back({29, "clf_sg", 32, 32, 32, 96});
  92. texture_.push_back({30, "clf_sg2", 32, 32, 0, 160});
  93. texture_.push_back({31, "clf_sr", 32, 32, 32, 96});
  94. texture_.push_back({32, "clf_ss", 32, 32, 32, 128});
  95. texture_.push_back({33, "clf_ssd", 32, 32, 0, 224});
  96. texture_.push_back({34, "clf_ssw", 32, 32, 224, 32});
  97. texture_.push_back({35, "clf_sw", 32, 32, 192, 32});
  98. texture_.push_back({36, "clf_w02", 64, 64, 128, 64});
  99. texture_.push_back({37, "clf_w03", 64, 64, 192, 64});
  100. texture_.push_back({38, "clf_was", 64, 32, 64, 64});
  101. texture_.push_back({39, "clfeg", 32, 32, 192, 320});
  102. texture_.push_back({40, "clfegd", 32, 32, 0, 320});
  103. texture_.push_back({41, "clftop", 64, 32, 192, 64});
  104. texture_.push_back({42, "clftop2", 32, 32, 128, 64});
  105. texture_.push_back({43, "cndl_cl", 64, 32, 96, 128});
  106. texture_.push_back({44, "cndlf", 64, 64, 160, 64});
  107. texture_.push_back({45, "cndlf02", 64, 64, 208, 64});
  108. texture_.push_back({46, "comtr", 16, 32, 144, 96});
  109. texture_.push_back({47, "cosinn", 32, 32, 224, 416});
  110. texture_.push_back({48, "cosinn2", 32, 32, 192, 448});
  111. texture_.push_back({49, "csmk", 32, 32, 64, 64});
  112. texture_.push_back({50, "csmk2", 32, 32, 96, 64});
  113. texture_.push_back({51, "cstds01", 32, 32, 224, 160});
  114. texture_.push_back({52, "cstds02", 64, 64, 0, 448});
  115. texture_.push_back({53, "des01", 32, 32, 160, 320});
  116. texture_.push_back({54, "desert", 64, 64, 128, 128});
  117. texture_.push_back({55, "desor", 64, 32, 160, 64});
  118. texture_.push_back({56, "ds1", 32, 32, 0, 256});
  119. texture_.push_back({57, "ds_s1", 32, 32, 192, 288});
  120. texture_.push_back({58, "dsee1", 32, 32, 96, 288});
  121. texture_.push_back({59, "dsrt_d", 32, 32, 224, 0});
  122. texture_.push_back({60, "dsrt_e", 64, 128, 64, 128});
  123. texture_.push_back({61, "edes01", 32, 32, 224, 320});
  124. texture_.push_back({62, "elm01", 32, 32, 160, 0});
  125. texture_.push_back({63, "elm02", 32, 32, 64, 96});
  126. texture_.push_back({64, "elm_gro", 64, 64, 0, 96});
  127. texture_.push_back({65, "elm_r", 32, 32, 192, 0});
  128. texture_.push_back({66, "elm_r2", 32, 32, 224, 0});
  129. texture_.push_back({67, "fall1", 32, 32, 128, 256});
  130. texture_.push_back({68, "farm01", 32, 32, 160, 32});
  131. texture_.push_back({69, "farm02", 32, 32, 192, 32});
  132. texture_.push_back({70, "farm_g", 32, 32, 128, 64});
  133. texture_.push_back({71, "farm_r", 32, 16, 128, 48});
  134. texture_.push_back({72, "fld", 64, 64, 64, 96});
  135. texture_.push_back({73, "fld_02", 64, 64, 0, 64});
  136. texture_.push_back({74, "fld_s", 64, 64, 0, 160});
  137. texture_.push_back({75, "fld_s2", 32, 32, 224, 256});
  138. texture_.push_back({76, "fld_sw", 64, 64, 128, 192});
  139. texture_.push_back({77, "fld_v", 128, 128, 0, 128});
  140. texture_.push_back({78, "fld_vd", 32, 64, 96, 128});
  141. texture_.push_back({79, "fld_vd2", 32, 64, 192, 416});
  142. texture_.push_back({80, "fvedge", 32, 64, 0, 0});
  143. texture_.push_back({81, "gclf_d", 128, 64, 128, 128});
  144. texture_.push_back({82, "gclf_g", 32, 64, 224, 128});
  145. texture_.push_back({83, "gclfwa", 128, 64, 64, 320});
  146. texture_.push_back({84, "gclfwa2", 32, 64, 160, 320});
  147. texture_.push_back({85, "gclfwag", 32, 64, 32, 320});
  148. texture_.push_back({86, "gg_gro", 64, 64, 64, 448});
  149. texture_.push_back({87, "gg_mts", 64, 128, 0, 128});
  150. texture_.push_back({88, "ggmk", 64, 64, 128, 448});
  151. texture_.push_back({89, "ggmt", 128, 128, 0, 0});
  152. texture_.push_back({90, "ggmt_e", 128, 32, 0, 96});
  153. texture_.push_back({91, "ggmt_ed", 128, 32, 128, 96});
  154. texture_.push_back({92, "ggmt_eg", 32, 32, 96, 224});
  155. texture_.push_back({93, "ggmtd", 128, 128, 128, 0});
  156. texture_.push_back({94, "ggs_g", 32, 32, 32, 64});
  157. texture_.push_back({95, "ggshr", 32, 32, 192, 96});
  158. texture_.push_back({96, "ggshrg", 32, 32, 224, 96});
  159. texture_.push_back({97, "gia", 64, 32, 64, 224});
  160. texture_.push_back({98, "gia2", 64, 32, 0, 224});
  161. texture_.push_back({99, "gia_d", 64, 32, 128, 224});
  162. texture_.push_back({100, "gia_d2", 64, 32, 64, 224});
  163. texture_.push_back({101, "gia_g", 32, 32, 192, 224});
  164. texture_.push_back({102, "gia_g2", 32, 32, 128, 224});
  165. texture_.push_back({103, "gmt_eda", 32, 32, 0, 352});
  166. texture_.push_back({104, "gonclf", 128, 64, 96, 64});
  167. texture_.push_back({105, "gredge", 32, 32, 192, 192});
  168. texture_.push_back({106, "hyouga", 64, 64, 192, 64});
  169. texture_.push_back({107, "iceclf", 64, 32, 64, 96});
  170. texture_.push_back({108, "iceclfd", 64, 32, 128, 96});
  171. texture_.push_back({109, "iceclfg", 32, 32, 32, 224});
  172. texture_.push_back({110, "jun", 64, 64, 192, 192});
  173. texture_.push_back({111, "jun_d", 64, 64, 128, 192});
  174. texture_.push_back({112, "jun_e", 64, 16, 0, 240});
  175. texture_.push_back({113, "jun_gro", 64, 64, 0, 64});
  176. texture_.push_back({114, "junmk", 32, 32, 0, 96});
  177. texture_.push_back({115, "junn01", 32, 32, 160, 112});
  178. texture_.push_back({116, "junn02", 32, 32, 192, 112});
  179. texture_.push_back({117, "junn03", 32, 32, 224, 112});
  180. texture_.push_back({118, "junn04", 32, 32, 64, 128});
  181. texture_.push_back({119, "jutmpl01", 64, 64, 128, 192});
  182. texture_.push_back({120, "lake-e", 32, 32, 96, 192});
  183. texture_.push_back({121, "lake_ef", 32, 32, 128, 224});
  184. texture_.push_back({122, "lake_fl", 128, 32, 160, 224});
  185. texture_.push_back({123, "lostclf", 32, 64, 128, 384});
  186. texture_.push_back({124, "lostmt", 128, 32, 128, 448});
  187. texture_.push_back({125, "lostmtd", 128, 32, 128, 480});
  188. texture_.push_back({126, "lostmts", 64, 32, 160, 384});
  189. texture_.push_back({127, "lostwd_e", 32, 32, 64, 480});
  190. texture_.push_back({128, "lostwod", 64, 64, 0, 448});
  191. texture_.push_back({129, "lst1", 32, 32, 192, 256});
  192. texture_.push_back({130, "lstwd_e2", 32, 32, 96, 480});
  193. texture_.push_back({131, "mzes", 32, 32, 224, 128});
  194. texture_.push_back({132, "mzmt_e", 128, 64, 128, 64});
  195. texture_.push_back({133, "mzmt_ed", 128, 32, 128, 128});
  196. texture_.push_back({134, "mzmt_edw", 128, 32, 128, 160});
  197. texture_.push_back({135, "mzmt_ew", 128, 32, 0, 128});
  198. texture_.push_back({136, "mzmt_o", 128, 32, 64, 416});
  199. texture_.push_back({137, "mzmt_od", 128, 32, 64, 448});
  200. texture_.push_back({138, "mzmt_s", 128, 32, 0, 192});
  201. texture_.push_back({139, "mzmt_sd", 128, 32, 0, 160});
  202. texture_.push_back({140, "md01", 32, 32, 96, 16});
  203. texture_.push_back({141, "md02", 128, 128, 0, 0});
  204. texture_.push_back({142, "md03", 16, 16, 112, 64});
  205. texture_.push_back({143, "md04", 32, 32, 128, 16});
  206. texture_.push_back({144, "md05", 64, 16, 96, 0});
  207. texture_.push_back({145, "md06", 16, 32, 96, 48});
  208. texture_.push_back({146, "md07", 16, 16, 112, 48});
  209. texture_.push_back({147, "md_mt", 128, 128, 128, 0});
  210. texture_.push_back({148, "md_mtd", 128, 128, 0, 0});
  211. texture_.push_back({149, "md_mts", 64, 128, 64, 160});
  212. texture_.push_back({150, "md_snow", 128, 32, 128, 0});
  213. texture_.push_back({151, "md_snw2", 128, 32, 128, 32});
  214. texture_.push_back({152, "md_snwd", 128, 64, 0, 128});
  215. texture_.push_back({153, "md_snwe", 64, 64, 96, 320});
  216. texture_.push_back({154, "md_snws", 64, 64, 128, 128});
  217. texture_.push_back({155, "md_snwt", 128, 32, 0, 192});
  218. texture_.push_back({156, "md_snww", 32, 32, 224, 224});
  219. texture_.push_back({157, "md_sw_s", 128, 128, 0, 0});
  220. texture_.push_back({158, "md_swd2", 32, 32, 192, 256});
  221. texture_.push_back({159, "md_swnp", 128, 128, 0, 96});
  222. texture_.push_back({160, "mdsrt_e", 128, 32, 128, 192});
  223. texture_.push_back({161, "mdsrt_ed", 128, 32, 128, 224});
  224. texture_.push_back({162, "mdsrt_eg", 32, 32, 64, 224});
  225. texture_.push_back({163, "midil", 32, 32, 32, 192});
  226. texture_.push_back({164, "midild", 32, 32, 224, 192});
  227. texture_.push_back({165, "mt_ewg", 32, 32, 64, 96});
  228. texture_.push_back({166, "mt_road", 64, 64, 192, 128});
  229. texture_.push_back({167, "mt_se", 32, 32, 160, 416});
  230. texture_.push_back({168, "mt_se2", 64, 64, 128, 256});
  231. texture_.push_back({169, "mt_sg01", 32, 32, 0, 224});
  232. texture_.push_back({170, "mt_sg02", 32, 32, 32, 224});
  233. texture_.push_back({171, "mt_sg03", 32, 32, 0, 192});
  234. texture_.push_back({172, "mt_sg04", 32, 32, 160, 384});
  235. texture_.push_back({173, "mtcoin", 64, 64, 0, 256});
  236. texture_.push_back({174, "mtwas_e", 128, 32, 0, 224});
  237. texture_.push_back({175, "mtwas_ed", 128, 32, 0, 224});
  238. texture_.push_back({176, "ncol_gro", 64, 64, 64, 384});
  239. texture_.push_back({177, "ncole01", 32, 32, 224, 384});
  240. texture_.push_back({178, "ncole02", 32, 32, 192, 416});
  241. texture_.push_back({179, "nivl_gro", 64, 64, 128, 384});
  242. texture_.push_back({180, "nivl_mt", 128, 64, 0, 0});
  243. texture_.push_back({181, "nivl_top", 32, 32, 0, 64});
  244. texture_.push_back({182, "nivlr", 32, 32, 192, 384});
  245. texture_.push_back({183, "port", 32, 32, 96, 224});
  246. texture_.push_back({184, "port_d", 32, 32, 160, 0});
  247. texture_.push_back({185, "rzclf02", 64, 64, 128, 128});
  248. texture_.push_back({186, "rct_gro", 64, 128, 0, 416});
  249. texture_.push_back({187, "riv_cls", 64, 64, 64, 0});
  250. texture_.push_back({188, "riv_l1", 32, 32, 96, 320});
  251. texture_.push_back({189, "riv_m", 32, 32, 0, 224});
  252. texture_.push_back({190, "rivr", 32, 32, 64, 224});
  253. texture_.push_back({191, "rivrclf", 64, 64, 128, 192});
  254. texture_.push_back({192, "rivs1", 32, 32, 128, 320});
  255. texture_.push_back({193, "rivshr", 64, 64, 192, 192});
  256. texture_.push_back({194, "rivssr", 64, 32, 192, 224});
  257. texture_.push_back({195, "rivstrt", 32, 32, 192, 160});
  258. texture_.push_back({196, "rm1", 32, 32, 32, 288});
  259. texture_.push_back({197, "rocet", 32, 32, 128, 160});
  260. texture_.push_back({198, "rs_ss", 32, 32, 96, 224});
  261. texture_.push_back({199, "sango", 32, 32, 224, 320});
  262. texture_.push_back({200, "sango2", 32, 32, 224, 352});
  263. texture_.push_back({201, "sango3", 32, 32, 128, 384});
  264. texture_.push_back({202, "sango4", 64, 64, 0, 384});
  265. texture_.push_back({203, "sdun", 64, 64, 0, 160});
  266. texture_.push_back({204, "sdun02", 64, 64, 64, 160});
  267. texture_.push_back({205, "sh1", 32, 32, 32, 256});
  268. texture_.push_back({206, "sh_s1", 32, 32, 224, 288});
  269. texture_.push_back({207, "shedge", 32, 64, 160, 160});
  270. texture_.push_back({208, "shlm_1", 32, 32, 192, 320});
  271. texture_.push_back({209, "shol", 128, 128, 128, 96});
  272. texture_.push_back({210, "shol_s", 64, 64, 192, 192});
  273. texture_.push_back({211, "shor", 128, 128, 0, 0});
  274. texture_.push_back({212, "shor_s", 64, 64, 128, 192});
  275. texture_.push_back({213, "shor_s2", 32, 32, 224, 416});
  276. texture_.push_back({214, "shor_v", 32, 32, 192, 0});
  277. texture_.push_back({215, "silo", 32, 32, 224, 32});
  278. texture_.push_back({216, "slope", 128, 32, 0, 384});
  279. texture_.push_back({217, "snow_es", 32, 32, 192, 480});
  280. texture_.push_back({218, "snow_es2", 32, 32, 224, 480});
  281. texture_.push_back({219, "snow_es3", 32, 32, 224, 448});
  282. texture_.push_back({220, "snw_mt", 128, 128, 0, 0});
  283. texture_.push_back({221, "snw_mtd", 128, 128, 128, 0});
  284. texture_.push_back({222, "snw_mte", 64, 32, 0, 96});
  285. texture_.push_back({223, "snw_mted", 64, 32, 64, 96});
  286. texture_.push_back({224, "snw_mts", 64, 128, 64, 0});
  287. texture_.push_back({225, "snw_mts2", 64, 32, 128, 192});
  288. texture_.push_back({226, "snwfld", 64, 64, 0, 64});
  289. texture_.push_back({227, "snwfld_s", 64, 32, 128, 128});
  290. texture_.push_back({228, "snwgra", 64, 64, 192, 192});
  291. texture_.push_back({229, "snwhm01", 32, 32, 32, 0});
  292. texture_.push_back({230, "snwhm02", 32, 32, 32, 32});
  293. texture_.push_back({231, "snwods", 32, 32, 224, 192});
  294. texture_.push_back({232, "snwood", 64, 64, 192, 128});
  295. texture_.push_back({233, "snwtrk", 32, 64, 96, 256});
  296. texture_.push_back({234, "sse_s1", 32, 32, 32, 320});
  297. texture_.push_back({235, "ssee1", 32, 32, 64, 288});
  298. texture_.push_back({236, "sst1", 32, 32, 224, 256});
  299. texture_.push_back({237, "stown_r", 32, 32, 192, 256});
  300. texture_.push_back({238, "stw_gro", 64, 64, 0, 384});
  301. texture_.push_back({239, "subrg2", 32, 32, 224, 160});
  302. texture_.push_back({240, "susbrg", 64, 64, 192, 96});
  303. texture_.push_back({241, "sw_se", 64, 64, 0, 0});
  304. texture_.push_back({242, "swclf_l", 64, 64, 64, 128});
  305. texture_.push_back({243, "swclf_ld", 64, 64, 192, 128});
  306. texture_.push_back({244, "swclf_lg", 32, 64, 0, 192});
  307. texture_.push_back({245, "swclf_s", 64, 32, 128, 96});
  308. texture_.push_back({246, "swclf_sd", 64, 32, 192, 96});
  309. texture_.push_back({247, "swclf_sg", 32, 32, 32, 192});
  310. texture_.push_back({248, "swclf_wg", 32, 32, 192, 192});
  311. texture_.push_back({249, "swfld_s2", 64, 32, 128, 160});
  312. texture_.push_back({250, "swfld_s3", 32, 32, 160, 192});
  313. texture_.push_back({251, "swmd_cg", 32, 32, 128, 192});
  314. texture_.push_back({252, "swmd_clf", 64, 32, 64, 192});
  315. texture_.push_back({253, "swp1", 32, 32, 0, 288});
  316. texture_.push_back({254, "trk", 64, 64, 128, 0});
  317. texture_.push_back({255, "tyo_f", 128, 128, 128, 128});
  318. texture_.push_back({256, "tyosnw", 64, 128, 64, 384});
  319. texture_.push_back({257, "uf1", 32, 32, 160, 256});
  320. texture_.push_back({258, "utai01", 32, 32, 32, 96});
  321. texture_.push_back({259, "utai02", 32, 32, 224, 64});
  322. texture_.push_back({260, "utai_gro", 64, 64, 128, 96});
  323. texture_.push_back({261, "utaimt", 32, 32, 0, 128});
  324. texture_.push_back({262, "utaimtd", 32, 32, 96, 96});
  325. texture_.push_back({263, "utaimtg", 32, 32, 96, 128});
  326. texture_.push_back({264, "wa1", 32, 32, 192, 320});
  327. texture_.push_back({265, "wzs1", 32, 32, 128, 288});
  328. texture_.push_back({266, "wzshr", 32, 32, 160, 32});
  329. texture_.push_back({267, "wzshr2", 32, 32, 32, 128});
  330. texture_.push_back({268, "wzshrs", 32, 32, 32, 160});
  331. texture_.push_back({269, "was", 128, 128, 0, 96});
  332. texture_.push_back({270, "was_d", 64, 32, 0, 224});
  333. texture_.push_back({271, "was_g", 64, 64, 0, 192});
  334. texture_.push_back({272, "was_s", 128, 128, 128, 0});
  335. texture_.push_back({273, "wasfld", 64, 64, 64, 256});
  336. texture_.push_back({274, "wdedge", 64, 64, 64, 160});
  337. texture_.push_back({275, "we1", 32, 32, 96, 256});
  338. texture_.push_back({276, "we_s1", 32, 32, 160, 288});
  339. texture_.push_back({277, "wedged", 32, 64, 128, 160});
  340. texture_.push_back({278, "wod-e2", 32, 32, 64, 224});
  341. texture_.push_back({279, "wood", 64, 64, 192, 0});
  342. texture_.push_back({280, "wood_d", 64, 64, 192, 160});
  343. texture_.push_back({281, "wtrk", 32, 64, 64, 96});
  344. Ogre::MaterialSerializer mSer;
  345. for (Texture t : texture_){
  346. Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create(t.name, "General", true);
  347. Ogre::Technique* tech = mat->getTechnique(0);
  348. Ogre::Pass* pass = tech->getPass(0);
  349. pass->setDiffuse(0.7, 0.7, 0.7, 0.7);
  350. mSer.exportMaterial(mat, data_dir_ + "models/world/" + t.name + ".material");
  351. }
  352. }
  353. void WorldInstaller::ProcessModels(){
  354. // Open world_us.lgp
  355. File world_file(*disk_image_.filePath("data/wm/world_us.lgp"));
  356. boost::filesystem::create_directories(output_dir_ + "temp/world_models/");
  357. boost::filesystem::create_directories(data_dir_ + "models/world/");
  358. VGears::LGPArchive world_lgp(*disk_image_.filePath("data/wm/world_us.lgp"), "LGP");
  359. world_lgp.open(*disk_image_.filePath("data/wm/world_us.lgp"), true);
  360. world_lgp.load();
  361. VGears::LGPArchive::FileList file_list = world_lgp.GetFiles();
  362. std::vector<std::string> hrc_file_names;
  363. // First pass: extract all files so HRC sidecar dependencies (.rsd, .p, .tex) exist.
  364. for (int i = 0; i < file_list.size(); i ++){
  365. VGears::LGPArchive::FileEntry f = file_list.at(i);
  366. if (f.data_offset + f.data_size <= world_file.GetFileSize()){
  367. File w_lgp_file(&world_file, f.data_offset, f.data_size);
  368. std::string file_name = f.file_name;
  369. VGears::StringUtil::toLowerCase(file_name);
  370. w_lgp_file.WriteFile(output_dir_ + "temp/world_models/" + file_name);
  371. if (file_name.size() >= 7 && file_name.substr(4, 3) == "hrc") hrc_file_names.push_back(file_name);
  372. }
  373. }
  374. // Second pass: convert HRC models once all dependencies are available on disk.
  375. for (int i = 0; i < hrc_file_names.size(); i ++){
  376. const std::string& file_name = hrc_file_names.at(i);
  377. // TODO: Can this be done with declare resource?
  378. // TODO: If not, do aloop to save all the hrc files, then call this once, then
  379. // another loop to process them.
  380. res_mgr_->declareResource(output_dir_ + "temp/world_models/" + file_name, "Skeleton", "FFVII");
  381. res_mgr_->removeResourceLocation(output_dir_ + "temp/world_models/", "FFVII");
  382. res_mgr_->addResourceLocation(output_dir_ + "temp/world_models/", "FileSystem", "FFVII", true, true);
  383. try{
  384. // TODO: This needs work to assemble all the pieces. Similar to field models.
  385. Ogre::ResourcePtr hrc = VGears::HRCFileManager::GetSingleton().load(file_name, "FFVII");
  386. Ogre::String hrc_base_name;
  387. VGears::StringUtil::splitBase(file_name, hrc_base_name);
  388. auto mesh_name = VGears::NameLookup::model(hrc_base_name) + ".mesh";
  389. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  390. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  391. // TODO: a files??
  392. /*for (std::string anim : model.a){
  393. VGears::AFileManager &afl_mgr(VGears::AFileManager::GetSingleton());
  394. Ogre::String a_base_name;
  395. VGears::StringUtil::splitBase(anim, a_base_name);
  396. VGears::AFilePtr a
  397. = afl_mgr.load(a_base_name + ".a", "FFVII").staticCast<VGears::AFile>();
  398. // Convert the FF7 name to a more readable name set in the meta data.
  399. VGears::StringUtil::splitBase(anim, base_name);
  400. a->AddTo(skeleton, VGears::NameLookup::Animation(base_name));
  401. }*/
  402. ExportMesh(data_dir_ + "models/world/", mesh);
  403. // Models can share a skeleton name with different bones (also with field models), so drop this one
  404. // (with its skeleton and mesh) before converting the next.
  405. VGears::HRCFileManager::GetSingleton().remove(file_name, "FFVII");
  406. }
  407. catch (const std::exception& ex){
  408. std::cerr << "[ERROR] Exception converting world model " << file_name << ": " << ex.what() << std::endl;
  409. if (VGears::HRCFileManager::GetSingleton().resourceExists(file_name, "FFVII"))
  410. VGears::HRCFileManager::GetSingleton().remove(file_name, "FFVII");
  411. }
  412. }
  413. }
  414. bool WorldInstaller::ProcessMap(){
  415. if (processed_maps_ >= wm_map_.size()) return true;
  416. // Initialize the xml file.
  417. TiXmlDocument doc;
  418. std::unique_ptr<TiXmlElement> xml(new TiXmlElement("world_map"));
  419. // Script.
  420. std::unique_ptr<TiXmlElement> xml_script_element(new TiXmlElement("script"));
  421. xml_script_element->SetAttribute("file_name", std::to_string(processed_maps_) + "/script.lua");
  422. xml->LinkEndChild(xml_script_element.release());
  423. // Background.
  424. std::unique_ptr<TiXmlElement> xml_background_2d(new TiXmlElement("background2d"));
  425. xml_background_2d->SetAttribute("file_name", std::to_string(processed_maps_) + "/bg.xml");
  426. xml->LinkEndChild(xml_background_2d.release());
  427. // Texts.
  428. std::unique_ptr<TiXmlElement> xml_texts(new TiXmlElement("texts"));
  429. xml_texts->SetAttribute("file_name", std::to_string(processed_maps_) + "/text.xml");
  430. xml->LinkEndChild(xml_texts.release());
  431. // Walkmesh.
  432. std::unique_ptr<TiXmlElement> xml_walkmesh(new TiXmlElement("walkmesh"));
  433. xml_walkmesh->SetAttribute("file_name", std::to_string(processed_maps_) + "/wm.xml");
  434. xml->LinkEndChild(xml_walkmesh.release());
  435. // Terrain (don't close yet).
  436. std::unique_ptr<TiXmlElement> xml_terrain(new TiXmlElement("terrain"));
  437. // Initialize the walkmesh data.
  438. WorldMapWalkmesh walkmesh(processed_maps_, processed_maps_ == 0 ? true : false);
  439. // Read WM*.MAP file.
  440. Map map;
  441. Ogre::MeshSerializer mesh_serializer;
  442. boost::filesystem::path p(wm_map_[processed_maps_].GetFileName());
  443. std::string map_name = p.stem().native(); // w/o path or extension.
  444. for (int b = 0; b < wm_map_[processed_maps_].GetFileSize() / 0xB800; b ++){
  445. // Calculate offsets for walkmesh.
  446. // WM0 is 9x7 blocks, WM2 probably is too, WM3 no idea?
  447. int wm_offset_x = 32 * (b % 9);
  448. int wm_offset_y = 32 * (b / 9);
  449. Block block;
  450. for (int m = 0; m < 16; m ++){
  451. // Extract lzss compressed mesh to file.
  452. std::vector<unsigned char> lzss;
  453. wm_map_[processed_maps_].SetOffset(0xB800 * b + m * 4);
  454. u32 mesh_offset = 0xB800 * b + wm_map_[processed_maps_].readU32LE();
  455. wm_map_[processed_maps_].SetOffset(mesh_offset);
  456. u32 mesh_size = wm_map_[processed_maps_].readU32LE();
  457. // Add size to lzss
  458. wm_map_[processed_maps_].SetOffset(mesh_offset);
  459. for (int d = 0; d < 4; d ++) lzss.push_back(wm_map_[processed_maps_].readU8());
  460. for (int d = 0; d < mesh_size ; d ++) lzss.push_back(wm_map_[processed_maps_].readU8());
  461. // Extract the lzss file.
  462. std::vector<unsigned char> data = Lzs::Decompress(lzss);
  463. if (data.size() == 0) continue;
  464. std::string dat_name = output_dir_ + "temp/wm/" + map_name + "_"
  465. + std::to_string(b) + "_" + std::to_string(m) + ".dat";
  466. std::ofstream dat(dat_name, std::ios::out | std::ios::binary);
  467. if (!dat){
  468. std::cerr << "Cannot create temporary world map file " << dat_name << std::endl;
  469. processed_maps_ ++;
  470. if (processed_maps_ >= wm_map_.size()) return true;
  471. else return false;
  472. }
  473. for (int d = 0; d < data.size(); d ++) dat.write((char *) &data[d], sizeof(unsigned char));
  474. dat.close();
  475. // Open and read the decompressed file.
  476. File mesh_data(dat_name);
  477. block.mesh[m].triangle_count = mesh_data.readU16LE();
  478. block.mesh[m].vertex_count = mesh_data.readU16LE();
  479. for (int tri = 0; tri < block.mesh[m].triangle_count; tri ++){
  480. Triangle t;
  481. t.vertex_index[0] = mesh_data.readU8();
  482. t.vertex_index[1] = mesh_data.readU8();
  483. t.vertex_index[2] = mesh_data.readU8();
  484. u8 walkability_function = mesh_data.readU8();
  485. //t.walkability = walkability_function >> 5; // 5 bits
  486. //t.function_id = walkability_function & 0x7; // 3 bits
  487. t.walkability = walkability_function & 0x1F; // Lowest 5 bits
  488. t.function_id = walkability_function >> 3; // 3 bits
  489. // FIXME: I think function_id is wrong. It must be 3 bits
  490. t.vertex_coord[0].u = mesh_data.readU8();
  491. t.vertex_coord[0].v = mesh_data.readU8();
  492. t.vertex_coord[1].u = mesh_data.readU8();
  493. t.vertex_coord[1].v = mesh_data.readU8();
  494. t.vertex_coord[2].u = mesh_data.readU8();
  495. t.vertex_coord[2].v = mesh_data.readU8();
  496. u16 texture_location = mesh_data.readU16LE();
  497. t.texture = texture_location >> 9; // 9 bytes
  498. t.location = texture_location & ((1 << 7) - 1); // 7 bytes.
  499. block.mesh[m].triangles.push_back(t);
  500. }
  501. for (int ver = 0; ver < block.mesh[m].vertex_count; ver ++){
  502. Vertex v;
  503. v.x = mesh_data.readU16LE();
  504. v.z = mesh_data.readU16LE();
  505. v.y = mesh_data.readU16LE();
  506. v.unknown = mesh_data.readU16LE();
  507. block.mesh[m].vertices.push_back(v);
  508. }
  509. for (int nor = 0; nor < block.mesh[m].vertex_count; nor ++){
  510. Vertex v;
  511. v.x = mesh_data.readU16LE();
  512. v.z = mesh_data.readU16LE();
  513. v.y = mesh_data.readU16LE();
  514. v.unknown = mesh_data.readU16LE();
  515. block.mesh[m].normals.push_back(v);
  516. }
  517. // Add data to the walkmesh
  518. for (int tri = 0; tri < block.mesh[m].triangle_count; tri ++){
  519. Triangle t = block.mesh[m].triangles.at(tri);
  520. walkmesh.addTriangle(
  521. Ogre::Vector3(
  522. block.mesh[m].vertices.at(t.vertex_index[0]).x + wm_offset_x,
  523. block.mesh[m].vertices.at(t.vertex_index[0]).y + wm_offset_y,
  524. block.mesh[m].vertices.at(t.vertex_index[0]).z
  525. ), // a
  526. Ogre::Vector3(
  527. block.mesh[m].vertices.at(t.vertex_index[1]).x + wm_offset_x,
  528. block.mesh[m].vertices.at(t.vertex_index[1]).y + wm_offset_y,
  529. block.mesh[m].vertices.at(t.vertex_index[1]).z
  530. ), // b
  531. Ogre::Vector3(
  532. block.mesh[m].vertices.at(t.vertex_index[2]).x + wm_offset_x,
  533. block.mesh[m].vertices.at(t.vertex_index[2]).y + wm_offset_y,
  534. block.mesh[m].vertices.at(t.vertex_index[2]).z
  535. ), // c
  536. t.walkability,
  537. t.function_id
  538. );
  539. }
  540. }
  541. map.blocks.push_back(block);
  542. }
  543. // TODO: Generate material.
  544. // Generate manual meshes.
  545. for (int b = 0; b < map.blocks.size(); b ++){
  546. int wm_offset_x = 32 * (b % 9);
  547. int wm_offset_y = 32 * (b / 9);
  548. //std::cout << "[MAN] Block " << b << "/" << map.blocks.size() << std::endl;
  549. std::string name = map_name + "_" + std::to_string(b);
  550. std::string mat_name = texture_.at(0).name;
  551. Ogre::ManualObject man = Ogre::ManualObject(name);
  552. //man.setBoundingBox(Ogre::AxisAlignedBox({-100,-100,0}, {100,100,0})); //TODO;
  553. //= Ogre::Root::getSingleton().getSceneManager("Scene")->createManualObject(name);
  554. man.begin(mat_name, Ogre::RenderOperation::OT_TRIANGLE_LIST);
  555. for (int m = 0; m < 16; m ++){
  556. //std::cout << "[MAN] Mesh " << m << " triangles: "
  557. // << map.blocks[b].mesh[m].triangle_count << "\n";
  558. // Add vertices.
  559. for (int v = 0; v < map.blocks[b].mesh[m].vertex_count; v ++){
  560. man.position(
  561. map.blocks[b].mesh[m].vertices[v].x, map.blocks[b].mesh[m].vertices[v].y,
  562. map.blocks[b].mesh[m].vertices[v].z
  563. );
  564. man.normal(
  565. map.blocks[b].mesh[m].normals[v].x, map.blocks[b].mesh[m].normals[v].y,
  566. map.blocks[b].mesh[m].normals[v].z
  567. );
  568. // TODO: How to add texture coordinates at this point?
  569. //man->textureCoord(0, 0);
  570. }
  571. // Conform faces from vertices
  572. for (int t = 0; t < map.blocks[b].mesh[m].triangle_count; t ++){
  573. //std::cout << "Triangle " << t << " vertizes: "
  574. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[0] << ", "
  575. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[1] << ", "
  576. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[2] << std::endl;
  577. // TODO: El index deberia incrementar por triangulo por cada material
  578. man.setMaterialName(0, texture_.at(map.blocks[b].mesh[m].triangles[t].texture).name);
  579. man.triangle(
  580. map.blocks[b].mesh[m].triangles[t].vertex_index[0],
  581. map.blocks[b].mesh[m].triangles[t].vertex_index[1],
  582. map.blocks[b].mesh[m].triangles[t].vertex_index[2]
  583. );
  584. }
  585. }
  586. // Export the mesh.
  587. man.end();
  588. Ogre::MeshPtr mesh = man.convertToMesh(name);
  589. //std::cout << "[WI] Export mesh " << (data_dir_ + "wm/" + mesh->getName() + ".mesh")
  590. // << std::endl;
  591. mesh_serializer.exportMesh(
  592. mesh.getPointer(),
  593. data_dir_ + TERRAIN_MODELS_DIR + "/" + std::to_string(processed_maps_) + "/" + mesh->getName() + ".mesh"
  594. );
  595. // WM0 is te main worldmap, its shape changes according to the history progress, it has
  596. // 68 blocks but only 63 are used at a time
  597. if (processed_maps_ > 0 || b < 63){
  598. std::unique_ptr<TiXmlElement> xml_block(new TiXmlElement("block"));
  599. xml_block->SetAttribute("index", std::to_string(b));
  600. xml_block->SetAttribute(
  601. "file_name", "terrain/" + std::to_string(processed_maps_) + "/" + mesh->getName() + ".mesh"
  602. );
  603. xml_block->SetAttribute("position", std::to_string(wm_offset_x) + " " + std::to_string(wm_offset_y) + " 0");
  604. if (processed_maps_ == 0){
  605. // TODO: Correct history points for each block.
  606. switch (b){
  607. case 50: // Block 50 replaced by block 63
  608. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_63.mesh");
  609. xml_block->SetAttribute("alt_story_change", "1000");
  610. break;
  611. case 41: // Block 41 replaced by block 64
  612. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_64.mesh");
  613. xml_block->SetAttribute("alt_story_change", "1000");
  614. break;
  615. case 42: // Block 42 replaced by block 65
  616. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_65.mesh");
  617. xml_block->SetAttribute("alt_story_change", "1000");
  618. break;
  619. case 60: // Block 60 replaced by block 66
  620. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_66.mesh");
  621. xml_block->SetAttribute("alt_story_change", "1000");
  622. break;
  623. case 47: // Block 50 replaced by block 67
  624. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_67.mesh");
  625. xml_block->SetAttribute("alt_story_change", "1000");
  626. break;
  627. case 48: // Block 50 replaced by block 68
  628. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_68.mesh");
  629. xml_block->SetAttribute("alt_story_change", "1000");
  630. break;
  631. }
  632. }
  633. xml_terrain->LinkEndChild(xml_block.release());
  634. }
  635. }
  636. // Add other data to the XML file.
  637. // Close and save XML file,
  638. xml->LinkEndChild(xml_terrain.release());
  639. doc.LinkEndChild(xml.release());
  640. // TODO: Add models, entities...
  641. doc.SaveFile(
  642. data_dir_ + "/world/" + std::to_string(processed_maps_) + "/world" + std::to_string(processed_maps_) + ".xml"
  643. );
  644. // Generate the walkmesh xml file
  645. walkmesh.generate(data_dir_ + "/world/" + std::to_string(processed_maps_) + "/wm.xml");
  646. // TODO: Generate other files: background, script, texts...
  647. // Ready for next map or next step.
  648. processed_maps_ ++;
  649. if (processed_maps_ >= wm_map_.size()) return true;
  650. else return false;
  651. }
  652. void WorldInstaller::ExportEncounters(
  653. File& archive_file, const VGears::LGPArchive::FileEntry& entry
  654. ){
  655. // ENC_W.BIN: 160 bytes of chocobo encounters and battle data, then 64 records of 16 words.
  656. // Record = region * 4 + terrain group. Word 0: enabled (low byte bit 0) and rate (high byte,
  657. // the lower the more often). Words 1-6: standard battles, 7-10: special battles
  658. // (back attack x2, side, pincer) and 11-15: others. A word is a battle ID (10 bits) and a
  659. // probability out of 64 (6 bits).
  660. static const unsigned int HEADER_SIZE = 0xA0;
  661. static const unsigned int RECORD_COUNT = 64;
  662. static const unsigned int RECORD_SIZE = 32;
  663. if (entry.data_size < HEADER_SIZE + RECORD_COUNT * RECORD_SIZE){
  664. std::cerr << "ENC_W.BIN has an unexpected size." << std::endl;
  665. return;
  666. }
  667. std::vector<u8> data(entry.data_size);
  668. File file(&archive_file, entry.data_offset, entry.data_size);
  669. file.GetFileBuffer(data.data(), 0, entry.data_size);
  670. // Walkability types that select each of the 4 terrain groups of every region. This table is
  671. // in the executable of the game, not in the data files. The region of a triangle is
  672. // (location & 0x1F), and a region over 15 is 15.
  673. static const unsigned int TERRAIN_TYPES[16][4] = {
  674. {0, 9, 0, 17}, {0, 0, 0, 17}, {0, 9, 1, 17}, {0, 20, 0, 17},
  675. {0, 9, 8, 17}, {0, 0, 25, 17}, {0, 9, 19, 17}, {0, 0, 1, 17},
  676. {0, 0, 1, 17}, {0, 9, 14, 17}, {0, 9, 25, 17}, {0, 10, 9, 17},
  677. {0, 9, 25, 17}, {0, 0, 8, 11}, {0, 0, 8, 0}, {0, 0, 1, 0}
  678. };
  679. boost::filesystem::create_directories(data_dir_ + "/gamedata");
  680. TiXmlDocument doc;
  681. std::unique_ptr<TiXmlElement> root(new TiXmlElement("world_encounters"));
  682. for (unsigned int region = 0; region < 16; ++ region){
  683. for (unsigned int group = 0; group < 4; ++ group){
  684. const unsigned int index = region * 4 + group;
  685. const unsigned int offset = HEADER_SIZE + index * RECORD_SIZE;
  686. std::unique_ptr<TiXmlElement> record(new TiXmlElement("record"));
  687. record->SetAttribute("region", region);
  688. record->SetAttribute("terrain", TERRAIN_TYPES[region][group]);
  689. record->SetAttribute("enabled", data[offset] & 0x01);
  690. record->SetAttribute("rate", data[offset + 1]);
  691. std::string words;
  692. for (unsigned int w = 1; w < 16; ++ w){
  693. const unsigned int word = data[offset + w * 2] | (data[offset + w * 2 + 1] << 8);
  694. words += (w > 1 ? " " : "") + std::to_string(word);
  695. }
  696. record->SetAttribute("words", words);
  697. root->LinkEndChild(record.release());
  698. }
  699. }
  700. doc.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
  701. doc.LinkEndChild(root.release());
  702. doc.SaveFile(data_dir_ + "/gamedata/world_encounters.xml");
  703. }
  704. bool WorldInstaller::GenerateScripts(
  705. const std::vector<std::string>& field_names,
  706. const std::map<u16, float>& field_scale_factors,
  707. const std::vector<std::string>& converted_fields,
  708. std::string& error_message
  709. ){
  710. error_message.clear();
  711. if (field_names.empty() || converted_fields.empty()){
  712. error_message = "Field maps were not installed; enable field map extraction.";
  713. return false;
  714. }
  715. std::unique_ptr<std::string> archive_path
  716. = disk_image_.filePath("data/wm/world_us.lgp");
  717. if (!archive_path){
  718. error_message = "WORLD_US.LGP was not found on the game disc.";
  719. return false;
  720. }
  721. File archive_file(*archive_path);
  722. VGears::LGPArchive archive(*archive_path, "LGP");
  723. archive.open(*archive_path, true);
  724. archive.load();
  725. const VGears::LGPArchive::FileList& archive_entries = archive.GetFiles();
  726. std::vector<const VGears::LGPArchive::FileEntry*> event_entries;
  727. const VGears::LGPArchive::FileEntry* field_table_entry = nullptr;
  728. const VGears::LGPArchive::FileEntry* encounter_entry = nullptr;
  729. bool has_wm0 = false;
  730. for (const VGears::LGPArchive::FileEntry& entry : archive_entries){
  731. std::string lower_name = entry.file_name;
  732. VGears::StringUtil::toLowerCase(lower_name);
  733. if (
  734. lower_name.size() > 5 && lower_name.compare(0, 2, "wm") == 0
  735. && lower_name.compare(lower_name.size() - 3, 3, ".ev") == 0
  736. ){
  737. const std::string map_number
  738. = lower_name.substr(2, lower_name.size() - 5);
  739. if (!map_number.empty() && std::all_of(
  740. map_number.begin(), map_number.end(), [](char character){
  741. return character >= '0' && character <= '9';
  742. }
  743. )){
  744. event_entries.push_back(&entry);
  745. if (lower_name == "wm0.ev") has_wm0 = true;
  746. }
  747. }
  748. else if (lower_name == "field.tbl"){
  749. field_table_entry = &entry;
  750. }
  751. else if (lower_name == "enc_w.bin"){
  752. encounter_entry = &entry;
  753. }
  754. }
  755. if (encounter_entry != nullptr){
  756. ExportEncounters(archive_file, *encounter_entry);
  757. }
  758. if (event_entries.empty() || !has_wm0 || field_table_entry == nullptr){
  759. error_message = "WM0.EV, other world event files, or FIELD.TBL were not found inside WORLD_US.LGP.";
  760. return false;
  761. }
  762. File field_table_file(
  763. &archive_file, field_table_entry->data_offset, field_table_entry->data_size
  764. );
  765. std::vector<u8> field_table_data(field_table_entry->data_size);
  766. if (field_table_data.size() != 64 * 24){
  767. error_message = "FIELD.TBL has an unexpected size.";
  768. return false;
  769. }
  770. field_table_file.GetFileBuffer(
  771. field_table_data.data(), 0, field_table_entry->data_size
  772. );
  773. const std::string script_directory = data_dir_ + "/scripts/worldmap";
  774. boost::filesystem::create_directories(script_directory);
  775. for (const VGears::LGPArchive::FileEntry* event_entry : event_entries){
  776. File event_file(&archive_file, event_entry->data_offset, event_entry->data_size);
  777. std::vector<u8> event_data(event_entry->data_size);
  778. if (event_data.empty()){
  779. error_message = event_entry->file_name + " inside WORLD_US.LGP is empty.";
  780. return false;
  781. }
  782. event_file.GetFileBuffer(event_data.data(), 0, event_entry->data_size);
  783. std::string map_file_name = event_entry->file_name;
  784. VGears::StringUtil::toLowerCase(map_file_name);
  785. const std::string map_name = map_file_name.substr(0, map_file_name.size() - 3);
  786. std::ofstream script_file(script_directory + "/" + map_name + ".lua");
  787. if (!script_file){
  788. error_message = "Cannot create script output in " + script_directory + ".";
  789. return false;
  790. }
  791. script_file << "-- Decompiled from the user's " << event_entry->file_name << " data.\n\n";
  792. std::vector<bool> generated_functions(256, false);
  793. unsigned int generated_function_count = 0;
  794. for (int script_index = 0; script_index < 256; ++script_index){
  795. try{
  796. WorldEngine engine(script_index);
  797. InstVec instructions;
  798. std::unique_ptr<Disassembler> disassembler = engine.GetDisassembler(instructions);
  799. disassembler->Open(event_data);
  800. disassembler->Disassemble();
  801. if (instructions.empty()) continue;
  802. ControlFlow control_flow(instructions, engine);
  803. control_flow.CreateGroups();
  804. std::ostringstream generated_script;
  805. std::unique_ptr<CodeGenerator> code_generator
  806. = engine.GetCodeGenerator(instructions, generated_script);
  807. code_generator->Generate(instructions, control_flow.GetGraph());
  808. script_file << generated_script.str() << "\n";
  809. generated_functions[script_index] = true;
  810. ++generated_function_count;
  811. }
  812. catch (const DecompilerException&){
  813. std::cerr << "[WARNING] World function " << script_index << " of " << map_name
  814. << " could not be decompiled." << std::endl;
  815. script_file << "-- world function " << script_index
  816. << " could not be decompiled.\n";
  817. }
  818. catch (const std::exception& exception){
  819. std::cerr << "[WARNING] World function " << script_index << " of " << map_name
  820. << " failed: " << exception.what() << std::endl;
  821. script_file << "-- world function " << script_index
  822. << " failed: " << exception.what() << ".\n";
  823. }
  824. }
  825. script_file << "-- " << map_name << " mesh-function call-table mapping.\n";
  826. for (size_t function_index = 0; function_index < generated_functions.size(); ++function_index){
  827. if (!generated_functions[function_index]) continue;
  828. const size_t type_offset = function_index * 4;
  829. if (type_offset + 1 >= event_data.size()) continue;
  830. const uint16 function_type = static_cast<uint16>(event_data[type_offset])
  831. | static_cast<uint16>(event_data[type_offset + 1] << 8);
  832. if ((function_type >> 14) != 2) continue;
  833. const uint16 mesh_index = (function_type >> 4) & 0x03FF;
  834. const uint8 mesh_function_id = function_type & 0x0F;
  835. if (mesh_index >= 36 * 28 || mesh_function_id > 4) continue;
  836. script_file << "System.WorldMap.triangle_functions[" << mesh_index << "]"
  837. << " = System.WorldMap.triangle_functions[" << mesh_index << "] or {}\n"
  838. << "System.WorldMap.triangle_functions[" << mesh_index << "]["
  839. << static_cast<unsigned int>(mesh_function_id) << "] = " << function_index << "\n";
  840. }
  841. script_file << "System.WorldMap.field_table = {}\n";
  842. for (size_t record_index = 0; record_index < 64; ++record_index){
  843. for (size_t scenario = 0; scenario < 2; ++scenario){
  844. const size_t offset = record_index * 24 + scenario * 12;
  845. const uint16 raw_x = static_cast<uint16>(field_table_data[offset])
  846. | (static_cast<uint16>(field_table_data[offset + 1]) << 8);
  847. const uint16 raw_y = static_cast<uint16>(field_table_data[offset + 2])
  848. | (static_cast<uint16>(field_table_data[offset + 3]) << 8);
  849. const uint16 raw_triangle = static_cast<uint16>(field_table_data[offset + 4])
  850. | (static_cast<uint16>(field_table_data[offset + 5]) << 8);
  851. const uint16 field_id = static_cast<uint16>(field_table_data[offset + 6])
  852. | (static_cast<uint16>(field_table_data[offset + 7]) << 8);
  853. if (field_id >= field_names.size()) continue;
  854. const std::string& field_name = field_names[field_id];
  855. if (std::find(converted_fields.begin(), converted_fields.end(), field_name)
  856. == converted_fields.end()){
  857. continue;
  858. }
  859. const auto scale_factor = field_scale_factors.find(field_id);
  860. if (scale_factor == field_scale_factors.end() || scale_factor->second <= 0){
  861. continue;
  862. }
  863. const float downscale = 128.0f * scale_factor->second;
  864. const float x = static_cast<int16>(raw_x) / downscale;
  865. const float y = static_cast<int16>(raw_y) / downscale;
  866. const int16 triangle = static_cast<int16>(raw_triangle);
  867. const uint8 direction = field_table_data[offset + 8];
  868. const float direction_degrees
  869. = 360.0f * static_cast<float>(direction) / 256.0f;
  870. const size_t table_id = record_index + 1;
  871. script_file << "System.WorldMap.field_table[" << table_id << "]"
  872. << " = System.WorldMap.field_table[" << table_id << "] or {}\n"
  873. << "System.WorldMap.field_table[" << table_id << "][" << scenario << "] = {"
  874. << "field = \"" << field_name << "\", x = " << x << ", y = " << y
  875. << ", triangle = " << triangle << ", direction = "
  876. << direction_degrees << "}\n";
  877. }
  878. }
  879. script_file.flush();
  880. if (!script_file){
  881. error_message = "Failed while writing " + script_directory + "/" + map_name + ".lua.";
  882. return false;
  883. }
  884. if (generated_function_count == 0){
  885. error_message = "No functions in " + event_entry->file_name + " could be decompiled.";
  886. return false;
  887. }
  888. }
  889. return true;
  890. }
  891. void WorldInstaller::ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh){
  892. if (mesh.isNull()){
  893. std::cerr << "[ERROR] ExportMesh called with null mesh." << std::endl;
  894. return;
  895. }
  896. boost::filesystem::create_directories(outdir);
  897. // TODO: Share function with pc model exporter
  898. VGears::String base_mesh_name;
  899. VGears::StringUtil::splitFull(mesh->getName(), base_mesh_name);
  900. MakeMaterialsUnique(mesh, base_mesh_name);
  901. Ogre::MeshSerializer mesh_serializer;
  902. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  903. if (!skeleton.isNull()){
  904. Ogre::SkeletonSerializer skeleton_serializer;
  905. skeleton_serializer.exportSkeleton(skeleton.getPointer(), outdir + base_mesh_name + ".skeleton");
  906. mesh->setSkeletonName(base_mesh_name + ".skeleton");
  907. }
  908. mesh_serializer.exportMesh(mesh.getPointer(), outdir + mesh->getName());
  909. Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
  910. Ogre::MaterialSerializer mat_ser;
  911. size_t i(0);
  912. std::set<std::string> queued_materials;
  913. bool has_queued_materials = false;
  914. while (it.hasMoreElements()){
  915. Ogre::SubMesh *sub_mesh(it.getNext());
  916. Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName()));
  917. if (mat != nullptr){
  918. for (size_t techs = 0; techs < mat->getNumTechniques(); techs ++){
  919. Ogre::Technique* tech = mat->getTechnique(techs);
  920. if (tech){
  921. for (size_t pass_num = 0; pass_num < tech->getNumPasses(); pass_num ++){
  922. Ogre::Pass* pass = tech->getPass(pass_num);
  923. if (pass){
  924. for (
  925. size_t texture_unit_num = 0;
  926. texture_unit_num < pass->getNumTextureUnitStates();
  927. texture_unit_num ++
  928. ){
  929. Ogre::TextureUnitState* unit = pass->getTextureUnitState(texture_unit_num);
  930. if (unit && unit->getTextureName().empty() == false){
  931. // Materials are shared by name across all model types, so this one may have
  932. // already been converted for a field or battle model (its texture is then a png).
  933. if (!boost::filesystem::exists(output_dir_ + "temp/world_models/" + unit->getTextureName())){
  934. std::cerr << "[WARNING] Texture " << unit->getTextureName()
  935. << " not found in temp/world_models; skipping it." << std::endl;
  936. continue;
  937. }
  938. try{
  939. // Convert the texture from .tex to .png.
  940. TexFile tex(output_dir_ + "temp/world_models/" + unit->getTextureName());
  941. // Ensure the output material script references png files
  942. // rather than tex files.
  943. Ogre::String base_name;
  944. VGears::StringUtil::splitBase(unit->getTextureName(), base_name);
  945. unit->setTextureName(base_mesh_name + "_" + base_name + ".png");
  946. const auto plain_png = outdir + base_name + ".png";
  947. const auto model_png = outdir + base_mesh_name + "_" + base_name + ".png";
  948. tex.SavePng(plain_png, 0);
  949. boost::filesystem::copy_file(
  950. plain_png, model_png, boost::filesystem::copy_options::overwrite_existing
  951. );
  952. }
  953. catch (const std::exception& ex){
  954. std::cerr << "[ERROR] Texture export failed for "
  955. << unit->getTextureName() << ": " << ex.what() << std::endl;
  956. }
  957. }
  958. }
  959. }
  960. }
  961. }
  962. }
  963. // Queue each material once. Exporting with an empty queue throws in Ogre.
  964. if (queued_materials.insert(sub_mesh->getMaterialName()).second){
  965. mat_ser.queueForExport(mat);
  966. has_queued_materials = true;
  967. }
  968. }
  969. ++ i;
  970. }
  971. if (has_queued_materials) mat_ser.exportQueued(outdir + base_mesh_name + VGears::EXT_MATERIAL);
  972. }