ff7DataInstaller.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. #include "ff7DataInstaller.h"
  2. #include <boost/filesystem.hpp>
  3. #include <boost/program_options.hpp>
  4. #include <OgreConfigFile.h>
  5. #include <OgreArchiveManager.h>
  6. #include <OgreMeshSerializer.h>
  7. #include <OgreSkeletonSerializer.h>
  8. #include <OgreMeshManager.h>
  9. #include <OgreRenderWindow.h>
  10. #include <OgreHardwarePixelBuffer.h>
  11. #include "QGearsGameState.h"
  12. #include "data/QGearsAFileManager.h"
  13. #include "data/QGearsBackgroundFileManager.h"
  14. #include "data/QGearsCameraMatrixFileManager.h"
  15. #include "data/QGearsHRCFileManager.h"
  16. #include "data/QGearsLZSFLevelFileManager.h"
  17. #include "data/QGearsPaletteFileManager.h"
  18. #include "data/QGearsPFileManager.h"
  19. #include "data/QGearsRSDFileManager.h"
  20. #include "data/QGearsTexCodec.h"
  21. #include "map/QGearsBackground2DFileManager.h"
  22. #include "map/QGearsWalkmeshFileManager.h"
  23. #include "data/FF7ModelListFileManager.h"
  24. #include "data/QGearsLGPArchiveFactory.h"
  25. #include "common/FF7NameLookup.h"
  26. #include "data/QGearsTexCodec.h"
  27. #include "data/QGearsMapListFile.h"
  28. #include "decompiler/sudm.h"
  29. #include <memory>
  30. #include <QDir>
  31. #include "common/make_unique.h"
  32. FF7DataInstaller::FF7DataInstaller(std::string inputDir, std::string outputDir, std::function<void(std::string)> fnWriteOutputLine)
  33. #ifdef _DEBUG
  34. : mApp("plugins_d.cfg", "resources_d.cfg", "install_d.log"),
  35. #else
  36. : mApp("plugins.cfg", "resources.cfg", "install.log"),
  37. #endif
  38. mInputDir(inputDir),
  39. mOutputDir(outputDir),
  40. mfnWriteOutputLine(fnWriteOutputLine)
  41. {
  42. if (!mApp.initOgre(true))
  43. {
  44. throw std::runtime_error("Ogre init failure");
  45. }
  46. }
  47. FF7DataInstaller::~FF7DataInstaller()
  48. {
  49. }
  50. int FF7DataInstaller::CalcProgress()
  51. {
  52. // TODO: Make more accurate with mIteratorCounter and mProgressStepNumElements
  53. float curStep = mState / static_cast<float>(eMaxStates);
  54. curStep = curStep * 100.0f;
  55. return static_cast<int>(curStep);
  56. }
  57. int FF7DataInstaller::Progress()
  58. {
  59. switch (mState)
  60. {
  61. case eIdle:
  62. mfnWriteOutputLine("Loading flevel.lgp");
  63. mProgressStepNumElements = 1;
  64. mIteratorCounter = 0;
  65. mFieldsLgp = std::make_unique<ScopedLgp>(mApp.getRoot(), mInputDir + "field/flevel.lgp", "LGP", "FFVIIFields");
  66. mState = eInitCollectFieldSpawnPointsAndScaleFactors;
  67. return CalcProgress();
  68. case eInitCollectFieldSpawnPointsAndScaleFactors:
  69. mfnWriteOutputLine("Collecting spawn points and scale factors");
  70. InitCollectSpawnAndScaleFactors();
  71. return CalcProgress();
  72. case eCollectFieldSpawnPointsAndScaleFactors:
  73. CollectionFieldSpawnAndScaleFactors();
  74. return CalcProgress();
  75. case eConvertFieldsIteration:
  76. ConvertFieldsIteration();
  77. return CalcProgress();
  78. case eWriteMapListOfConvertedFieldsStart:
  79. mfnWriteOutputLine("Write fields");
  80. WriteMapsXmlBegin();
  81. return CalcProgress();
  82. case eWriteMapListOfConvertedFieldsIteration:
  83. WriteMapsXmlIteration();
  84. return CalcProgress();
  85. case eWriteMapListOfConvertedFieldsEnd:
  86. EndWriteMapsXml();
  87. return CalcProgress();
  88. case eConvertFieldModelsBegin:
  89. mfnWriteOutputLine("Converting field models");
  90. ConvertFieldModelsBegin();
  91. return CalcProgress();
  92. case eConvertFieldModelsIteration:
  93. ConvertFieldModelsIteration();
  94. return CalcProgress();
  95. case eDone:
  96. default:
  97. return 100;
  98. }
  99. }
  100. static std::string FieldModelDir()
  101. {
  102. return "models/ffvii/field/units";
  103. }
  104. // TOOD: Share with pc model exporter
  105. static void exportMesh(std::string outdir, const Ogre::MeshPtr &mesh)
  106. {
  107. QGears::String baseMeshName;
  108. QGears::StringUtil::splitFull(mesh->getName(), baseMeshName);
  109. Ogre::MeshSerializer mesh_ser;
  110. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  111. Ogre::SkeletonSerializer sk_ser;
  112. sk_ser.exportSkeleton(skeleton.getPointer(), outdir + baseMeshName + ".skeleton");
  113. mesh->setSkeletonName(FieldModelDir() + "/" + baseMeshName + ".skeleton");
  114. mesh_ser.exportMesh(mesh.getPointer(), outdir + mesh->getName());
  115. Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
  116. Ogre::MaterialSerializer mat_ser;
  117. size_t i(0);
  118. std::set<std::string> textures;
  119. while (it.hasMoreElements())
  120. {
  121. Ogre::SubMesh *sub_mesh(it.getNext());
  122. Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName()));
  123. if (!mat.isNull())
  124. {
  125. for (size_t techNum = 0; techNum < mat->getNumTechniques(); techNum++)
  126. {
  127. Ogre::Technique* tech = mat->getTechnique(techNum);
  128. if (tech)
  129. {
  130. for (size_t passNum = 0; passNum < tech->getNumPasses(); passNum++)
  131. {
  132. Ogre::Pass* pass = tech->getPass(passNum);
  133. if (pass)
  134. {
  135. for (size_t textureUnitNum = 0; textureUnitNum < pass->getNumTextureUnitStates(); textureUnitNum++)
  136. {
  137. Ogre::TextureUnitState* unit = pass->getTextureUnitState(textureUnitNum);
  138. if (unit)
  139. {
  140. if (unit->getTextureName().empty() == false)
  141. {
  142. // Ensure the output material script references png files rather than tex files
  143. textures.insert(unit->getTextureName());
  144. Ogre::String baseName;
  145. QGears::StringUtil::splitBase(unit->getTextureName(), baseName);
  146. unit->setTextureName(FieldModelDir() + "/" + baseMeshName + "_" + baseName + ".png");
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. mat_ser.queueForExport(mat);
  155. }
  156. ++i;
  157. }
  158. mat_ser.exportQueued(outdir + baseMeshName + QGears::EXT_MATERIAL);
  159. for (auto& textureName : textures)
  160. {
  161. Ogre::TexturePtr texturePtr = Ogre::TextureManager::getSingleton().load(textureName.c_str(), "FFVII");
  162. Ogre::Image image;
  163. texturePtr->convertToImage(image);
  164. Ogre::String baseName;
  165. QGears::StringUtil::splitBase(textureName, baseName);
  166. image.save(outdir + baseMeshName + "_" + baseName + ".png");
  167. }
  168. }
  169. static std::string FieldName(const std::string& name)
  170. {
  171. return "ffvii_" + name;
  172. }
  173. void FF7DataInstaller::CreateDir(const std::string& path)
  174. {
  175. QString target = QString::fromStdString(mOutputDir + path);
  176. QDir dir(target);
  177. if (!dir.mkpath("."))
  178. {
  179. throw std::runtime_error("Failed to mkpath");
  180. }
  181. }
  182. static std::string FieldMapDir()
  183. {
  184. return "maps/ffvii/field";
  185. }
  186. class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter
  187. {
  188. public:
  189. BaseFF7FieldScriptFormatter(const std::string& fieldName, const std::vector<std::string>& fieldIdToNameLookup, FieldSpawnPointsMap& spawnPoints)
  190. : mFieldName(fieldName), mFieldIdToNameLookup(fieldIdToNameLookup), mSpawnPoints(spawnPoints)
  191. {
  192. }
  193. virtual std::string SpawnPointName(unsigned int /*targetMapId*/, const std::string& entity, const std::string& funcName, unsigned int address) override
  194. {
  195. return mFieldName + "_" + entity + "_" + funcName + "_addr_" + std::to_string(address);
  196. }
  197. virtual std::string MapName(unsigned int mapId) override
  198. {
  199. return FieldName(mFieldIdToNameLookup[mapId]);
  200. }
  201. // Renames a variable, return empty string for generated name
  202. virtual std::string VarName(unsigned int bank, unsigned int addr) override
  203. {
  204. return QGears::FF7::NameLookup::FieldScriptVarName(bank, addr);
  205. }
  206. // Renames an entity
  207. virtual std::string EntityName(const std::string& entity) override
  208. {
  209. return QGears::FF7::NameLookup::FieldScriptEntityName(entity);
  210. }
  211. // Get name of char from its id, can't return empty
  212. virtual std::string CharName(int charId) override
  213. {
  214. return QGears::FF7::NameLookup::CharName(charId);
  215. }
  216. // Renames a function in an entity
  217. virtual std::string FunctionName(const std::string& entity, const std::string& funcName) override
  218. {
  219. return QGears::FF7::NameLookup::FieldScriptFunctionName(mFieldName, entity, funcName);
  220. }
  221. // Sets the header comment for a function in an entity
  222. virtual std::string FunctionComment(const std::string& entity, const std::string& funcName) override
  223. {
  224. return QGears::FF7::NameLookup::FieldScriptFunctionComment(mFieldName, entity, funcName);
  225. }
  226. protected:
  227. std::string mFieldName;
  228. FieldSpawnPointsMap& mSpawnPoints;
  229. const std::vector<std::string>& mFieldIdToNameLookup;
  230. };
  231. class FF7FieldScriptFormatter : public BaseFF7FieldScriptFormatter
  232. {
  233. public:
  234. FF7FieldScriptFormatter(const std::string& fieldName, const QGears::ModelListFilePtr& models, const std::vector<std::string>& fieldIdToNameLookup, FieldSpawnPointsMap& spawnPoints)
  235. : BaseFF7FieldScriptFormatter(fieldName, fieldIdToNameLookup, spawnPoints), mModelLoader(models)
  236. {
  237. }
  238. // Names an animation, can't return empty
  239. virtual std::string AnimationName(int charId, int id) override
  240. {
  241. // Get the animation file name, then look up the friendly name of the "raw" animation
  242. if (static_cast<unsigned int>(charId) >= mModelLoader->getModels().size())
  243. {
  244. std::cout << "ERROR CHAR ID IS OUT OF BOUNDS" << std::endl;
  245. return std::to_string(id);
  246. }
  247. const auto& modelInfo = mModelLoader->getModels().at(charId);
  248. if (static_cast<unsigned int>(id) >= modelInfo.animations.size())
  249. {
  250. std::cout << "ERROR ANIMATION ID IS OUT OF BOUNDS" << std::endl;
  251. return std::to_string(id);
  252. }
  253. const auto rawName = modelInfo.animations.at(id).name;
  254. // Trim off ".yos" or whatever other crazy extension the model loader adds in
  255. Ogre::String baseName;
  256. QGears::StringUtil::splitBase(rawName, baseName);
  257. QGears::StringUtil::toLowerCase(baseName);
  258. return QGears::FF7::NameLookup::animation(baseName);
  259. }
  260. private:
  261. const QGears::ModelListFilePtr& mModelLoader;
  262. };
  263. static std::string CreateGateWayScript(const std::string& gatewayEntityName, const std::string& targetMapName, const std::string& sourceSpawnPointName)
  264. {
  265. return "\nEntityContainer[ \"" + gatewayEntityName + "\" ] = {\n"
  266. "on_start = function(self)\n"
  267. " return 0\n"
  268. "end,\n"
  269. "on_enter_line = function(self, entity)\n"
  270. " return 0\n"
  271. "end,\n\n"
  272. "on_move_to_line = function(self, entity)\n"
  273. " return 0\n"
  274. "end,\n\n"
  275. "on_cross_line = function(self, entity)\n"
  276. " if entity == \"Cloud\" then\n"
  277. " if not FFVII.Data.DisableGateways then\n"
  278. " load_field_map_request(\"" + targetMapName + "\", \"" + sourceSpawnPointName + "\")\n"
  279. " end\n"
  280. " end\n\n"
  281. " return 0\n"
  282. "end,\n\n"
  283. "on_leave_line = function(self, entity)\n"
  284. " return 0\n"
  285. "end,\n"
  286. "}\n\n";
  287. }
  288. const int kInactiveGateWayId = 32767;
  289. static size_t FieldId(const std::string& name, const std::vector<std::string>& fieldIdToNameLookup)
  290. {
  291. for (size_t i = 0; i < fieldIdToNameLookup.size(); i++)
  292. {
  293. if (fieldIdToNameLookup[i] == name)
  294. {
  295. return i;
  296. }
  297. }
  298. throw std::runtime_error("No Id found for field name");
  299. }
  300. static float FieldScaleFactor(const FieldScaleFactorMap& scaleFactorMap, size_t fieldId)
  301. {
  302. auto it = scaleFactorMap.find(fieldId);
  303. if (it == std::end(scaleFactorMap))
  304. {
  305. throw std::runtime_error("Scale factor not found for field id");
  306. }
  307. return it->second;
  308. }
  309. static void FF7PcFieldToQGearsField(
  310. QGears::FLevelFilePtr& field,
  311. const std::string& outDir,
  312. const std::vector<std::string>& fieldIdToNameLookup,
  313. FieldSpawnPointsMap& spawnMap,
  314. const FieldScaleFactorMap& scaleFactorMap,
  315. ModelsAndAnimationsDb& modelAnimationDb,
  316. MapCollection& maps,
  317. std::function<void(std::string)> fnWriteOutputLine)
  318. {
  319. // Generate triggers script to insert into main decompiled FF7 field -> LUA script
  320. std::string gatewayScriptData;
  321. const QGears::TriggersFilePtr& triggers = field->getTriggers();
  322. const auto& gateways = triggers->GetGateways();
  323. for (size_t i = 0; i < gateways.size(); i++)
  324. {
  325. const QGears::TriggersFile::Gateway& gateway = gateways[i];
  326. if (gateway.destinationFieldId != kInactiveGateWayId)
  327. {
  328. const std::string gatewayEntityName = "Gateway" + std::to_string(i);
  329. gatewayScriptData += CreateGateWayScript(gatewayEntityName, FieldName(fieldIdToNameLookup.at(gateway.destinationFieldId)), "Spawn_" + field->getName() + "_" + std::to_string(i));
  330. }
  331. }
  332. const QGears::ModelListFilePtr& models = field->getModelList();
  333. SUDM::FF7::Field::DecompiledScript decompiled;
  334. FF7FieldScriptFormatter formatter(field->getName(), models, fieldIdToNameLookup, spawnMap);
  335. try
  336. {
  337. // Get the raw script bytes
  338. const std::vector<u8> rawFieldData = field->getRawScript();
  339. // Decompile to LUA
  340. decompiled = SUDM::FF7::Field::Decompile(field->getName(), rawFieldData, formatter, gatewayScriptData, "EntityContainer = {}\n\n");
  341. std::ofstream scriptFile(outDir + "/" + FieldMapDir() + "/" + field->getName() + "/script.lua");
  342. if (scriptFile.is_open())
  343. {
  344. scriptFile << decompiled.luaScript;
  345. }
  346. else
  347. {
  348. fnWriteOutputLine("[ERROR] Failed to open script file for writing");
  349. std::cerr << "Failed to open script file for writing" << std::endl;
  350. }
  351. }
  352. catch (const ::InternalDecompilerError& ex)
  353. {
  354. fnWriteOutputLine("[ERROR] internal decompiler error");
  355. std::cerr << "InternalDecompilerError: " << ex.what() << std::endl;
  356. }
  357. // Write out the map file which links all the other files together for a given field
  358. {
  359. TiXmlDocument doc;
  360. std::unique_ptr<TiXmlElement> element(new TiXmlElement("map"));
  361. // Script
  362. std::unique_ptr<TiXmlElement> xmlScriptElement(new TiXmlElement("script"));
  363. xmlScriptElement->SetAttribute("file_name", FieldMapDir() + "/" + field->getName() + "/script.lua");
  364. element->LinkEndChild(xmlScriptElement.release());
  365. // Background
  366. std::unique_ptr<TiXmlElement> xmlBackground2d(new TiXmlElement("background2d"));
  367. xmlBackground2d->SetAttribute("file_name", FieldMapDir() + "/" + field->getName() + "/bg.xml");
  368. element->LinkEndChild(xmlBackground2d.release());
  369. // walkmesh
  370. std::unique_ptr<TiXmlElement> xmlWalkmesh(new TiXmlElement("walkmesh"));
  371. xmlWalkmesh->SetAttribute("file_name", FieldMapDir() + "/" + field->getName() + "/wm.xml");
  372. element->LinkEndChild(xmlWalkmesh.release());
  373. // Angle player moves when "up" is pressed
  374. std::unique_ptr<TiXmlElement> xmlMovementRotation(new TiXmlElement("movement_rotation"));
  375. xmlMovementRotation->SetAttribute("degree", std::to_string(triggers->MovementRotation()));
  376. element->LinkEndChild(xmlMovementRotation.release());
  377. // Get this fields Id
  378. const size_t thisFieldId = FieldId(field->getName(), fieldIdToNameLookup);
  379. // Use that to find the pre-computed list of gateways in all other fields that link to this field
  380. auto spawnIterator = spawnMap.find(thisFieldId);
  381. Ogre::Vector3 firstEntityPoint = Ogre::Vector3::ZERO;
  382. // If not found that it probably just means no other fields have doors to this one
  383. if (spawnIterator != std::end(spawnMap))
  384. {
  385. const std::vector<SpawnPointDb::Record>& spawnPointRecords = spawnIterator->second.mGatewaysToThisField;
  386. for (size_t i = 0; i < spawnPointRecords.size(); i++)
  387. {
  388. const QGears::TriggersFile::Gateway& gateway = spawnPointRecords[i].mGateway;
  389. // entity_point
  390. std::unique_ptr<TiXmlElement> xmlEntityPoint(new TiXmlElement("entity_point"));
  391. if (spawnPointRecords[i].mFromScript)
  392. {
  393. // Spawn points from map jumps have a another algorithm
  394. xmlEntityPoint->SetAttribute("name", fieldIdToNameLookup.at(spawnPointRecords[i].mFieldId) + "_" + spawnPointRecords[i].mEntityName + "_" + spawnPointRecords[i].mScriptFunctionName + "_addr_" + std::to_string(spawnPointRecords[i].GatewayIndexOrMapJumpAddress));
  395. }
  396. else
  397. {
  398. // Must also include the gateway index for the case where 2 fields have more than one door linking to each other
  399. xmlEntityPoint->SetAttribute("name", "Spawn_" + fieldIdToNameLookup.at(spawnPointRecords[i].mFieldId) + "_" + std::to_string(spawnPointRecords[i].GatewayIndexOrMapJumpAddress));
  400. }
  401. const float downscaler_next = 128.0f * FieldScaleFactor(scaleFactorMap, gateway.destinationFieldId);
  402. // Position Z is actually the target walkmesh triangle ID, so this is tiny bit more
  403. // complex as we have to say "get the Z value of the triangle with that ID". Note that ID actually just means index.
  404. unsigned int triIndex = static_cast<unsigned int>(gateway.destination.z);
  405. if (triIndex >= field->getWalkmesh()->getTriangles().size())
  406. {
  407. std::cout << "WARNING MAP JUMP TRIANGLE OUT OF BOUNDS" << std::endl;
  408. triIndex = 0;
  409. }
  410. const float zOfTriangleWithId = field->getWalkmesh()->getTriangles().at(triIndex).a.z;
  411. const Ogre::Vector3 posVec(gateway.destination.x / downscaler_next, gateway.destination.y / downscaler_next, zOfTriangleWithId);
  412. if (posVec != Ogre::Vector3::ZERO && firstEntityPoint == Ogre::Vector3::ZERO)
  413. {
  414. firstEntityPoint = posVec;
  415. }
  416. xmlEntityPoint->SetAttribute("position", Ogre::StringConverter::toString(posVec));
  417. const float rotation = (360.0f * static_cast<float>(gateway.dir)) / 255.0f;
  418. xmlEntityPoint->SetAttribute("rotation", std::to_string(rotation));
  419. element->LinkEndChild(xmlEntityPoint.release());
  420. }
  421. }
  422. for (const auto& it : decompiled.entities)
  423. {
  424. const int charId = it.second;
  425. if (charId != -1)
  426. {
  427. const QGears::ModelListFile::ModelDescription& desc = models->getModels().at(charId);
  428. auto& animVec = modelAnimationDb.ModelAnimations(desc.hrc_name);
  429. for (auto& anim : desc.animations)
  430. {
  431. animVec.insert(modelAnimationDb.NormalizeAnimationName(anim.name));
  432. }
  433. std::unique_ptr<TiXmlElement> xmlEntityScript(new TiXmlElement("entity_model"));
  434. xmlEntityScript->SetAttribute("name", it.first);
  435. // TODO: Add to list of HRC's to convert, obtain name of target converted .mesh file
  436. auto lowerCaseHrcName = desc.hrc_name;
  437. QGears::StringUtil::toLowerCase(lowerCaseHrcName);
  438. xmlEntityScript->SetAttribute("file_name", FieldModelDir() + "/" + modelAnimationDb.ModelMetaDataName(lowerCaseHrcName));
  439. if (desc.type == QGears::ModelListFile::PLAYER)
  440. {
  441. // For player models we set the position in the xml because if a map is loaded manually this is where the player
  442. // will end up. Hence we set the position to the first entity_point that we have
  443. xmlEntityScript->SetAttribute("position", Ogre::StringConverter::toString(firstEntityPoint));
  444. }
  445. else
  446. {
  447. xmlEntityScript->SetAttribute("position", Ogre::StringConverter::toString(Ogre::Vector3::ZERO));
  448. }
  449. xmlEntityScript->SetAttribute("direction", "0");
  450. // TODO: This isn't quite right, the models animation translation seems to be inverted?
  451. xmlEntityScript->SetAttribute("scale", "0.03125 0.03125 0.03125");
  452. xmlEntityScript->SetAttribute("root_orientation", "0.7071067811865476 0.7071067811865476 0 0");
  453. element->LinkEndChild(xmlEntityScript.release());
  454. }
  455. else
  456. {
  457. std::unique_ptr<TiXmlElement> xmlEntityScript(new TiXmlElement("entity_script"));
  458. xmlEntityScript->SetAttribute("name", it.first);
  459. element->LinkEndChild(xmlEntityScript.release());
  460. }
  461. }
  462. const float downscaler_this = 128.0f * FieldScaleFactor(scaleFactorMap, thisFieldId);
  463. const auto& gateways = triggers->GetGateways();
  464. for (size_t i = 0; i < gateways.size(); i++)
  465. {
  466. const QGears::TriggersFile::Gateway& gateway = gateways[i];
  467. // if not inactive gateway
  468. if (gateway.destinationFieldId != kInactiveGateWayId)
  469. {
  470. std::unique_ptr<TiXmlElement> xmlEntityTrigger(new TiXmlElement("entity_trigger"));
  471. xmlEntityTrigger->SetAttribute("name", "Gateway" + std::to_string(i));
  472. xmlEntityTrigger->SetAttribute("point1",
  473. Ogre::StringConverter::toString(
  474. Ogre::Vector3(gateway.exit_line[0].x, gateway.exit_line[0].y, gateway.exit_line[0].z) / downscaler_this));
  475. xmlEntityTrigger->SetAttribute("point2",
  476. Ogre::StringConverter::toString(
  477. Ogre::Vector3(gateway.exit_line[1].x, gateway.exit_line[1].y, gateway.exit_line[1].z) / downscaler_this));
  478. // enabled hard coded to true
  479. xmlEntityTrigger->SetAttribute("enabled", "true");
  480. element->LinkEndChild(xmlEntityTrigger.release());
  481. }
  482. }
  483. doc.LinkEndChild(element.release());
  484. doc.SaveFile(outDir + "/" + FieldMapDir() + "/" + field->getName() + "/map.xml");
  485. const QGears::PaletteFilePtr& pal = field->getPalette();
  486. const QGears::BackgroundFilePtr& bg = field->getBackground();
  487. std::unique_ptr<Ogre::Image> bgImage(bg->createImage(pal));
  488. bgImage->save(outDir + "/" + FieldMapDir() + "/" + field->getName() + "/tiles.png");
  489. {
  490. TiXmlDocument doc;
  491. std::unique_ptr<TiXmlElement> element(new TiXmlElement("background2d"));
  492. // Magic constants
  493. const int kScaleUpFactor = 3;
  494. const int kPsxScreenWidth = 320;
  495. const int kPsxScreenHeight = 240;
  496. // Get texture atlas size
  497. const int width = bgImage->getWidth();
  498. const int height = bgImage->getHeight();
  499. const QGears::CameraMatrixFilePtr& camMatrix = field->getCameraMatrix();
  500. const Ogre::Vector3 position = camMatrix->getPosition() / FieldScaleFactor(scaleFactorMap, thisFieldId);
  501. const Ogre::Quaternion orientation = camMatrix->getOrientation();
  502. const Ogre::Degree fov = camMatrix->getFov(static_cast<float>(kPsxScreenHeight));
  503. const int min_x = triggers->getCameraRange().left * kScaleUpFactor;
  504. const int min_y = triggers->getCameraRange().top * kScaleUpFactor;
  505. const int max_x = triggers->getCameraRange().right * kScaleUpFactor;
  506. const int max_y = triggers->getCameraRange().bottom * kScaleUpFactor;
  507. element->SetAttribute("image", FieldMapDir() + "/" + field->getName() + "/tiles.png");
  508. element->SetAttribute("position", Ogre::StringConverter::toString(position));
  509. element->SetAttribute("orientation", Ogre::StringConverter::toString(orientation));
  510. element->SetAttribute("fov", Ogre::StringConverter::toString(fov));
  511. element->SetAttribute("range", std::to_string(min_x) + " " + std::to_string(min_y) + " " + std::to_string(max_x) + " " + std::to_string(max_y));
  512. element->SetAttribute("clip", std::to_string(kPsxScreenWidth * kScaleUpFactor) + " " + std::to_string(kPsxScreenHeight * kScaleUpFactor));
  513. // Write out the *_BG.XML data
  514. auto& layers = bg->getLayers();
  515. for (const auto& layer : layers)
  516. {
  517. // TODO: Should we only output tiles to construct enabled layers?
  518. // if (layer.enabled)
  519. {
  520. for (const QGears::BackgroundFile::SpriteData& sprite : layer.sprites)
  521. {
  522. std::unique_ptr<TiXmlElement> xmlElement(new TiXmlElement("tile"));
  523. xmlElement->SetAttribute("destination",
  524. Ogre::StringConverter::toString(sprite.dst.x * kScaleUpFactor) + " " +
  525. Ogre::StringConverter::toString(sprite.dst.y * kScaleUpFactor));
  526. xmlElement->SetAttribute("width", Ogre::StringConverter::toString(sprite.width * kScaleUpFactor));
  527. xmlElement->SetAttribute("height", Ogre::StringConverter::toString(sprite.height * kScaleUpFactor));
  528. // Each tile is added to a big texture atlas with hard coded size of 1024x1024, convert UV's to the 0.0f to 1.0f range
  529. const float u0 = static_cast<float>(sprite.src.x) / bgImage->getWidth();
  530. const float v0 = static_cast<float>(sprite.src.y) / bgImage->getHeight();
  531. const float u1 = static_cast<float>(sprite.src.x + sprite.width) / bgImage->getWidth();
  532. const float v1 = static_cast<float>(sprite.src.y + sprite.height) / bgImage->getHeight();
  533. xmlElement->SetAttribute("uv",
  534. Ogre::StringConverter::toString(u0) + " " + Ogre::StringConverter::toString(v0) + " " +
  535. Ogre::StringConverter::toString(u1) + " " + Ogre::StringConverter::toString(v1)
  536. );
  537. // TODO: Figure out how to scale this value correctly
  538. xmlElement->SetAttribute("depth", "999" /* Ogre::StringConverter::toString(static_cast<float>(sprite.depth) / (4096.0f/10.0f))*/);
  539. // TODO: Copied from DatFile::AddTile - add to common method
  540. Ogre::String blending_str = "";
  541. if (sprite.blending == 0)
  542. {
  543. blending_str = "alpha";
  544. }
  545. else if (sprite.blending == 1 || sprite.blending == 3) // 3 is source + 0.25 * destination
  546. {
  547. blending_str = "add";
  548. }
  549. else if (sprite.blending == 2)
  550. {
  551. blending_str = "subtract";
  552. }
  553. else
  554. {
  555. // TODO: Should probably throw to fail conversion
  556. blending_str = "unknown";
  557. }
  558. xmlElement->SetAttribute("blending", blending_str);
  559. element->LinkEndChild(xmlElement.release());
  560. }
  561. }
  562. }
  563. doc.LinkEndChild(element.release());
  564. doc.SaveFile(outDir + "/" + FieldMapDir() + "/" + field->getName() + "/bg.xml");
  565. }
  566. }
  567. {
  568. // Save out the walk mesh as XML
  569. const QGears::WalkmeshFilePtr& walkmesh = field->getWalkmesh();
  570. TiXmlDocument doc;
  571. std::unique_ptr<TiXmlElement> element(new TiXmlElement("walkmesh"));
  572. for (QGears::WalkmeshFile::Triangle& tri : walkmesh->getTriangles())
  573. {
  574. std::unique_ptr<TiXmlElement> xmlElement(new TiXmlElement("triangle"));
  575. xmlElement->SetAttribute("a", Ogre::StringConverter::toString(tri.a));
  576. xmlElement->SetAttribute("b", Ogre::StringConverter::toString(tri.b));
  577. xmlElement->SetAttribute("c", Ogre::StringConverter::toString(tri.c));
  578. xmlElement->SetAttribute("a_b", std::to_string(tri.access_side[0]));
  579. xmlElement->SetAttribute("b_c", std::to_string(tri.access_side[1]));
  580. xmlElement->SetAttribute("c_a", std::to_string(tri.access_side[2]));
  581. element->LinkEndChild(xmlElement.release());
  582. }
  583. doc.LinkEndChild(element.release());
  584. doc.SaveFile(outDir + "/" + FieldMapDir() + "/" + field->getName() + "/wm.xml");
  585. }
  586. maps.insert(field->getName());
  587. }
  588. class FF7FieldScriptGatewayCollector : public BaseFF7FieldScriptFormatter
  589. {
  590. public:
  591. FF7FieldScriptGatewayCollector(const std::string& fieldName, const std::vector<std::string>& fieldIdToNameLookup, FieldSpawnPointsMap& spawnPoints, size_t thisFieldId)
  592. : BaseFF7FieldScriptFormatter(fieldName, fieldIdToNameLookup, spawnPoints), mThisFieldId(thisFieldId)
  593. {
  594. }
  595. virtual void AddSpawnPoint(unsigned int targetMapId, const std::string& entity, const std::string& funcName, unsigned int address, int x, int y, int z, int angle) override
  596. {
  597. auto it = mSpawnPoints.find(targetMapId);
  598. QGears::TriggersFile::Gateway gw = {};
  599. gw.destinationFieldId = targetMapId;
  600. gw.destination.x = x;
  601. gw.destination.y = y;
  602. gw.destination.z = z;
  603. gw.dir = angle;
  604. if (it != std::end(mSpawnPoints))
  605. {
  606. // Add to the list of gateways that link to destinationFieldId
  607. SpawnPointDb::Record rec;
  608. rec.mFromScript = true;
  609. rec.mFieldId = mThisFieldId;
  610. rec.mGateway = gw;
  611. rec.mEntityName = entity;
  612. rec.mScriptFunctionName = funcName;
  613. rec.GatewayIndexOrMapJumpAddress = address;
  614. it->second.mGatewaysToThisField.push_back(rec);
  615. }
  616. else
  617. {
  618. // Create a new record for destinationFieldId
  619. SpawnPointDb db;
  620. db.mTargetFieldId = targetMapId;
  621. SpawnPointDb::Record rec;
  622. rec.mFromScript = true;
  623. rec.mFieldId = mThisFieldId;
  624. rec.mGateway = gw;
  625. rec.mEntityName = entity;
  626. rec.mScriptFunctionName = funcName;
  627. rec.GatewayIndexOrMapJumpAddress = address;
  628. db.mGatewaysToThisField.push_back(rec);
  629. mSpawnPoints.insert(std::make_pair(db.mTargetFieldId, db));
  630. }
  631. }
  632. private:
  633. size_t mThisFieldId;
  634. };
  635. static void CollectSpawnPoints(QGears::FLevelFilePtr& field, const std::vector<std::string>& fieldIdToNameLookup, FieldSpawnPointsMap& spawnPoints)
  636. {
  637. const size_t thisFieldId = FieldId(field->getName(), fieldIdToNameLookup);
  638. // Decompile the script just so we can collect up the MAPJUMP's we need this to construct the full list of entries
  639. // to each field
  640. try
  641. {
  642. // Get the raw script bytes
  643. SUDM::FF7::Field::DecompiledScript decompiled;
  644. const std::vector<u8> rawFieldData = field->getRawScript();
  645. // Decompile to LUA
  646. FF7FieldScriptGatewayCollector formatter(field->getName(), fieldIdToNameLookup, spawnPoints, thisFieldId);
  647. decompiled = SUDM::FF7::Field::Decompile(field->getName(), rawFieldData, formatter, "", "EntityContainer = {}\n\n");
  648. }
  649. catch (const ::InternalDecompilerError& ex)
  650. {
  651. std::cerr << "InternalDecompilerError: " << ex.what() << std::endl;
  652. }
  653. const QGears::TriggersFilePtr& triggers = field->getTriggers();
  654. const auto& gateways = triggers->GetGateways();
  655. for (size_t i = 0; i < gateways.size(); i++)
  656. {
  657. const QGears::TriggersFile::Gateway& gateway = gateways[i];
  658. if (gateway.destinationFieldId != kInactiveGateWayId)
  659. {
  660. auto it = spawnPoints.find(gateway.destinationFieldId);
  661. if (it != std::end(spawnPoints))
  662. {
  663. // Add to the list of gateways that link to destinationFieldId
  664. SpawnPointDb::Record rec;
  665. rec.mFieldId = thisFieldId;
  666. rec.mGateway = gateway;
  667. rec.GatewayIndexOrMapJumpAddress = i;
  668. it->second.mGatewaysToThisField.push_back(rec);
  669. }
  670. else
  671. {
  672. // Create a new record for destinationFieldId
  673. SpawnPointDb db;
  674. db.mTargetFieldId = gateway.destinationFieldId;
  675. SpawnPointDb::Record rec;
  676. rec.mFieldId = thisFieldId;
  677. rec.mGateway = gateway;
  678. rec.GatewayIndexOrMapJumpAddress = i;
  679. db.mGatewaysToThisField.push_back(rec);
  680. spawnPoints.insert(std::make_pair(db.mTargetFieldId, db));
  681. }
  682. }
  683. }
  684. }
  685. static bool IsAFieldFile(Ogre::String& resourceName)
  686. {
  687. return (!QGears::StringUtil::endsWith(resourceName, ".tex")
  688. && !QGears::StringUtil::endsWith(resourceName, ".tut")
  689. && !QGears::StringUtil::endsWith(resourceName, ".siz")
  690. && resourceName != "maplist");
  691. }
  692. // Exclude fields which cause fatal crash bugs :)
  693. // this can be removed when whatever is causing the crash(s) is fixed
  694. static bool WillCrash(Ogre::String& resourceName)
  695. {
  696. if (
  697. // crashes in back ground image generation
  698. resourceName == "bugin1a" ||
  699. // Hangs in decompliation
  700. resourceName == "frcyo" ||
  701. // bg image crash
  702. resourceName == "fr_e" ||
  703. resourceName == "las4_2" ||
  704. resourceName == "las4_3" ||
  705. resourceName == "lastmap" ||
  706. resourceName == "md_e1" ||
  707. resourceName == "trnad_3"
  708. )
  709. {
  710. // NOTE: Even so, conversion of all models will fail with a bone index out of bounds
  711. return true;
  712. }
  713. return false;
  714. }
  715. static bool IsTestField(Ogre::String& resourceName)
  716. {
  717. if (
  718. resourceName == "startmap" ||
  719. resourceName == "md1stin" ||
  720. resourceName == "md1_1" ||
  721. resourceName == "md1_2" ||
  722. resourceName == "md8_1" ||
  723. resourceName == "nmkin_1" ||
  724. resourceName == "nmkin_2" ||
  725. resourceName == "nmkin_3" ||
  726. resourceName == "nrthmk" ||
  727. resourceName == "elevtr1" ||
  728. resourceName == "tin_1" ||
  729. resourceName == "tin_2" ||
  730. resourceName == "rootmap" ||
  731. resourceName == "md8_4"
  732. )
  733. {
  734. return true;
  735. }
  736. return false;
  737. }
  738. static void CollectFieldScaleFactors(QGears::FLevelFilePtr& field, FieldScaleFactorMap& scaleFactors, const std::vector<std::string>& fieldIdToNameLookup)
  739. {
  740. scaleFactors[FieldId(field->getName(), fieldIdToNameLookup)] = ::SUDM::FF7::Field::ScaleFactor(field->getRawScript());
  741. }
  742. void FF7DataInstaller::InitCollectSpawnAndScaleFactors()
  743. {
  744. mProgressStepNumElements = 1;
  745. CreateDir(FieldMapDir());
  746. CreateDir(FieldModelDir());
  747. // List whats in the LGP archive
  748. mFLevelFileList = mApp.ResMgr()->listResourceNames("FFVIIFields", "*");
  749. // Load the map list field
  750. QGears::MapListFilePtr mapList = QGears::MapListFileManager::getSingleton().load("maplist", "FFVIIFields").staticCast<QGears::MapListFile>();
  751. mMapList = mapList->GetMapList();
  752. mIteratorCounter = 0;
  753. mConversionStep = 0;
  754. mState = eCollectFieldSpawnPointsAndScaleFactors;
  755. }
  756. void FF7DataInstaller::CollectionFieldSpawnAndScaleFactors()
  757. {
  758. // On the first pass collate required field information
  759. mProgressStepNumElements = mFLevelFileList->size();
  760. if (mIteratorCounter < mFLevelFileList->size())
  761. {
  762. auto resourceName = (*mFLevelFileList)[mIteratorCounter];
  763. // Exclude things that are not fields
  764. if (IsAFieldFile(resourceName) /*&& IsTestField(resourceName)*/)
  765. {
  766. mConversionStep++;
  767. if (mConversionStep == 1)
  768. {
  769. mField = QGears::LZSFLevelFileManager::getSingleton().load(resourceName, "FFVIIFields").staticCast<QGears::FLevelFile>();
  770. mfnWriteOutputLine("Load field " + resourceName);
  771. return;
  772. }
  773. if (mConversionStep == 2)
  774. {
  775. mfnWriteOutputLine("Read spawn points from scripts");
  776. CollectSpawnPoints(mField, mMapList, mCollectedSpawnPoints);
  777. return;
  778. }
  779. if (mConversionStep == 3)
  780. {
  781. mfnWriteOutputLine("Read scale factor");
  782. CollectFieldScaleFactors(mField, mCollectedScaleFactors, mMapList);
  783. mConversionStep = 0;
  784. mIteratorCounter++;
  785. return;
  786. }
  787. }
  788. else
  789. {
  790. mIteratorCounter++;
  791. }
  792. }
  793. else
  794. {
  795. mField.setNull();
  796. mIteratorCounter = 0;
  797. mState = eConvertFieldsIteration;
  798. }
  799. }
  800. void FF7DataInstaller::ConvertFieldsIteration()
  801. {
  802. // Now we can do the full conversion with the collated data
  803. mProgressStepNumElements = mFLevelFileList->size();
  804. if (mIteratorCounter < mFLevelFileList->size())
  805. {
  806. auto resourceName = (*mFLevelFileList)[mIteratorCounter];
  807. // Exclude things that are not fields
  808. if (IsAFieldFile(resourceName))
  809. {
  810. mIteratorCounter++;
  811. if (/*IsTestField(resourceName) &&*/
  812. !WillCrash(resourceName))
  813. {
  814. mfnWriteOutputLine("Converting field " + resourceName);
  815. std::cout << "Converting: " << resourceName << std::endl;
  816. CreateDir(FieldMapDir() + "/" + resourceName);
  817. QGears::FLevelFilePtr field = QGears::LZSFLevelFileManager::getSingleton().load(resourceName, "FFVIIFields").staticCast<QGears::FLevelFile>();
  818. FF7PcFieldToQGearsField(field, mOutputDir, mMapList, mCollectedSpawnPoints, mCollectedScaleFactors, mModelsAndAnimationsUsedByConvertedFields, mConvertedMapList, mfnWriteOutputLine);
  819. }
  820. else
  821. {
  822. mfnWriteOutputLine("[ERROR] Skip field " + resourceName + " due to crash or hang issue");
  823. std::cout << "Skip: " << resourceName << " as it has a crash or hang issue" << std::endl;
  824. }
  825. }
  826. else
  827. {
  828. mIteratorCounter++;
  829. }
  830. }
  831. else
  832. {
  833. mIteratorCounter = 0;
  834. mState = eWriteMapListOfConvertedFieldsStart;
  835. }
  836. }
  837. void FF7DataInstaller::WriteMapsXmlBegin()
  838. {
  839. // Write out maps.xml
  840. mProgressStepNumElements = 1;
  841. mDoc = std::make_unique<TiXmlDocument>();
  842. mElement = std::make_unique<TiXmlElement>("maps");
  843. mIteratorCounter = 0;
  844. mState = eWriteMapListOfConvertedFieldsIteration;
  845. mIteratorCounter = 0;
  846. mConvertedMapListIt = mConvertedMapList.begin();
  847. }
  848. void FF7DataInstaller::WriteMapsXmlIteration()
  849. {
  850. // TODO: Probably need to inject "empty" and "test" fields
  851. mProgressStepNumElements = mConvertedMapList.size();
  852. if (mConvertedMapListIt != mConvertedMapList.end())
  853. {
  854. auto map = *mConvertedMapListIt;
  855. mfnWriteOutputLine("Writing field " + FieldName(map));
  856. std::unique_ptr<TiXmlElement> xmlElement(new TiXmlElement("map"));
  857. xmlElement->SetAttribute("name", FieldName(map));
  858. xmlElement->SetAttribute("file_name", FieldMapDir() + "/" + map + "/map.xml");
  859. mElement->LinkEndChild(xmlElement.release());
  860. mIteratorCounter++;
  861. mConvertedMapListIt++;
  862. }
  863. else
  864. {
  865. mState = eWriteMapListOfConvertedFieldsEnd;
  866. }
  867. }
  868. void FF7DataInstaller::EndWriteMapsXml()
  869. {
  870. mProgressStepNumElements = 1;
  871. mDoc->LinkEndChild(mElement.release());
  872. mDoc->SaveFile(mOutputDir + "/maps.xml");
  873. mState = eConvertFieldModelsBegin;
  874. }
  875. void FF7DataInstaller::ConvertFieldModelsBegin()
  876. {
  877. // TODO: Convert models and animations in modelAnimationDb
  878. mProgressStepNumElements = 1;
  879. mFieldModelsLgp = std::make_unique<ScopedLgp>(mApp.getRoot(), mInputDir + "field/char.lgp", "LGP", "FFVII");
  880. mFieldModelFileList = mApp.ResMgr()->listResourceNames("FFVII", "*");
  881. mIteratorCounter = 0;
  882. mState = eConvertFieldModelsIteration;
  883. mIteratorCounter = 0;
  884. mConversionStep = 0;
  885. mModelAnimationMapIterator = mModelsAndAnimationsUsedByConvertedFields.mMap.begin();
  886. }
  887. void FF7DataInstaller::ConvertFieldModelsIteration()
  888. {
  889. mProgressStepNumElements = mModelsAndAnimationsUsedByConvertedFields.mMap.size();
  890. if (mModelAnimationMapIterator != mModelsAndAnimationsUsedByConvertedFields.mMap.end())
  891. {
  892. if (mConversionStep == 0)
  893. {
  894. mfnWriteOutputLine("Converting model " + mModelAnimationMapIterator->first);
  895. mConversionStep++;
  896. }
  897. else
  898. {
  899. try
  900. {
  901. Ogre::ResourcePtr hrc = QGears::HRCFileManager::getSingleton().load(mModelAnimationMapIterator->first, "FFVII");
  902. Ogre::String baseName;
  903. QGears::StringUtil::splitBase(mModelAnimationMapIterator->first, baseName);
  904. auto meshName = QGears::FF7::NameLookup::model(baseName) + ".mesh";
  905. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(meshName, "FFVII"));
  906. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  907. for (auto& anim : mModelAnimationMapIterator->second)
  908. {
  909. QGears::AFileManager &afl_mgr(QGears::AFileManager::getSingleton());
  910. QGears::AFilePtr a = afl_mgr.load(anim, "FFVII").staticCast<QGears::AFile>();
  911. // Convert the FF7 name to a more readable name set in the meta data
  912. Ogre::String baseName;
  913. QGears::StringUtil::splitBase(anim, baseName);
  914. a->addTo(skeleton, QGears::FF7::NameLookup::animation(baseName));
  915. }
  916. exportMesh(mOutputDir + "/" + FieldModelDir() + "/", mesh);
  917. }
  918. catch (const Ogre::Exception& ex)
  919. {
  920. mfnWriteOutputLine("[ERROR] converting model " + mModelAnimationMapIterator->first + " msg: " + ex.what());
  921. }
  922. catch (const std::exception& ex)
  923. {
  924. mfnWriteOutputLine("[ERROR] converting model " + mModelAnimationMapIterator->first + " msg: " + ex.what());
  925. }
  926. mIteratorCounter++;
  927. mModelAnimationMapIterator++;
  928. mConversionStep = 0;
  929. }
  930. }
  931. else
  932. {
  933. mState = eDone;
  934. }
  935. }