| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include "ff7DataInstaller.h"
- #include <boost/filesystem.hpp>
- #include <boost/program_options.hpp>
- #include <OgreConfigFile.h>
- #include <OgreArchiveManager.h>
- #include <OgreMeshSerializer.h>
- #include <OgreSkeletonSerializer.h>
- #include <OgreMeshManager.h>
- #include <OgreRenderWindow.h>
- #include <OgreHardwarePixelBuffer.h>
- #include "QGearsGameState.h"
- #include "data/QGearsAFileManager.h"
- #include "data/QGearsBackgroundFileManager.h"
- #include "data/QGearsCameraMatrixFileManager.h"
- #include "data/QGearsHRCFileManager.h"
- #include "data/QGearsLZSFLevelFileManager.h"
- #include "data/QGearsPaletteFileManager.h"
- #include "data/QGearsPFileManager.h"
- #include "data/QGearsRSDFileManager.h"
- #include "data/QGearsTexCodec.h"
- #include "map/QGearsBackground2DFileManager.h"
- #include "map/QGearsWalkmeshFileManager.h"
- #include "data/FF7ModelListFileManager.h"
- #include "data/QGearsLGPArchiveFactory.h"
- #include "common/QGearsStringUtil.h"
- #include "common/FF7NameLookup.h"
- #include "data/QGearsTexCodec.h"
- #include "decompiler/sudm.h"
- FF7DataInstaller::FF7DataInstaller()
- #ifdef _DEBUG
- : mApp("plugins_d.cfg", "resources_d.cfg", "install_d.log")
- #else
- : mApp("plugins.cfg", "resources.cfg", "install.log")
- #endif
- {
- mApp.initOgre(true);
- }
- FF7DataInstaller::~FF7DataInstaller()
- {
- }
- void FF7DataInstaller::Convert(std::string inputDir, std::string outputDir, const std::vector<std::string>& files)
- {
- for (const auto& file : files)
- {
- if (file == "field/char.lgp")
- {
- /*
- auto fullPath = inputDir + file;
- mApp.getRoot()->addResourceLocation(fullPath, "LGP", "FFVII");
- ConvertFieldModels(fullPath, outputDir);
- mApp.getRoot()->removeResourceLocation(fullPath, "FFVII");*/
- }
- else if (file == "field/flevel.lgp")
- {
- auto fullPath = inputDir + file;
- mApp.getRoot()->addResourceLocation(fullPath, "LGP", "FFVIIFields");
- ConvertFields(fullPath, outputDir);
- mApp.getRoot()->removeResourceLocation(fullPath, "FFVIIFields");
- }
- }
- }
- // TOOD: Share with pc model exporter
- static void exportMesh(std::string outdir, const Ogre::MeshPtr &mesh)
- {
- Ogre::MeshSerializer mesh_ser;
- mesh_ser.exportMesh(mesh.getPointer(), outdir + mesh->getName());
- Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
- Ogre::SkeletonSerializer sk_ser;
- sk_ser.exportSkeleton(skeleton.getPointer(), outdir + skeleton->getName());
- Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
- Ogre::MaterialSerializer mat_ser;
- size_t i(0);
- while (it.hasMoreElements())
- {
- Ogre::SubMesh *sub_mesh(it.getNext());
- Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName()));
- if (!mat.isNull())
- {
- mat_ser.queueForExport(mat);
- }
- ++i;
- }
- QGears::String base_name;
- QGears::StringUtil::splitFull(mesh->getName(), base_name);
- mat_ser.exportQueued(outdir + base_name + QGears::EXT_MATERIAL);
- }
- void FF7DataInstaller::ConvertFieldModels(std::string archive, std::string outDir)
- {
- Ogre::StringVectorPtr resources = mApp.ResMgr()->listResourceNames("FFVII", "*.hrc");
- for (auto& resourceName : *resources)
- {
- if (QGears::StringUtil::endsWith(resourceName, ".hrc"))
- {
- //try
- {
- Ogre::ResourcePtr hrc = QGears::HRCFileManager::getSingleton().load(resourceName, "FFVII");
- Ogre::String baseName;
- QGears::StringUtil::splitBase(resourceName, baseName);
- auto meshName = QGears::FF7::NameLookup::model(baseName) + ".mesh";
- Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(meshName, "FFVII"));
- Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
- exportMesh(outDir, mesh);
- }
- // catch (const Ogre::Exception& ex)
- {
- // std::cout << "ERROR converting: " << resourceName << " Exception: " << ex.what() << std::endl;
- }
- }
- }
- /*
- TODO:
- To figure out what .a files relate to which .hrc files, we need to enumerate every field
- and check its model loader data. These are always Idle, Walk, Run and then field specific
- ordering.
- Model loader is section 3 of the field file.
- */
- }
- class FF7FieldScriptFormatter : public SUDM::IScriptFormatter
- {
- public:
- virtual bool ExcludeFunction(const std::string& ) override
- {
- // Include everything for now
- return false;
- }
- virtual std::string RenameIdentifer(const std::string& name) override
- {
- // Don't rename anything for now
- return name;
- }
- };
- static void FF7PcFieldToQGearsField(QGears::FLevelFilePtr& field, const std::string& outDir)
- {
- // Save out the tiles as a PNG image
-
- const QGears::BackgroundFilePtr& bg = field->getBackground();
- try
- {
- // Get the raw script bytes
- const std::vector<u8> rawFieldData = field->getRawScript();
- // Decompile to LUA
- FF7FieldScriptFormatter formatter;
- std::string luaScript = SUDM::FF7::Field::Decompile(field->getName(), rawFieldData, formatter);
- std::cout << luaScript << std::endl;
- }
- catch (const ::InternalDecompilerError& ex)
- {
- std::cerr << "InternalDecompilerError: " << ex.what() << std::endl;
- }
-
- const QGears::PaletteFilePtr& pal = field->getPalette();
- std::unique_ptr<Ogre::Image> bgImage(bg->createImage(pal));
- bgImage->save(outDir + "/" + field->getName() + ".png");
- {
- TiXmlDocument doc;
- std::unique_ptr<TiXmlElement> element(new TiXmlElement("background2d"));
- auto& layers = bg->getLayers();
- const QGears::CameraMatrixFilePtr& camMatrix = field->getCameraMatrix();
- // TODO: Write out the *_BG.XML data
- for (auto& layer : layers)
- {
- // if (layer.enabled)
- {
-
- for (QGears::BackgroundFile::SpriteData& sprite : layer.sprites)
- {
- std::unique_ptr<TiXmlElement> xmlElement(new TiXmlElement("tile"));
- xmlElement->SetAttribute("destination", Ogre::StringConverter::toString(sprite.dst.x * 3) + " " + Ogre::StringConverter::toString(sprite.dst.y * 3));
- // Where to get UVs??
- //sprite.dst.x; layer.width
- //sprite.dst.y;
- // sprite.depth;
- // Where to get blend mode??
- element->LinkEndChild(xmlElement.release());
- }
-
- }
- }
- doc.LinkEndChild(element.release());
- doc.SaveFile(outDir + "/" + field->getName() + "_bg.xml");
- }
- {
- // Save out the walk mesh as XML
- const QGears::WalkmeshFilePtr& walkmesh = field->getWalkmesh();
- TiXmlDocument doc;
- std::unique_ptr<TiXmlElement> element(new TiXmlElement("walkmesh"));
- for (QGears::WalkmeshFile::Triangle& tri : walkmesh->getTriangles())
- {
- std::unique_ptr<TiXmlElement> xmlElement(new TiXmlElement("triangle"));
- xmlElement->SetAttribute("a", Ogre::StringConverter::toString(tri.a));
- xmlElement->SetAttribute("b", Ogre::StringConverter::toString(tri.b));
- xmlElement->SetAttribute("c", Ogre::StringConverter::toString(tri.c));
- xmlElement->SetAttribute("a_b", std::to_string(tri.access_side[0]));
- xmlElement->SetAttribute("b_c", std::to_string(tri.access_side[1]));
- xmlElement->SetAttribute("c_a", std::to_string(tri.access_side[2]));
- element->LinkEndChild(xmlElement.release());
- }
- doc.LinkEndChild(element.release());
- doc.SaveFile(outDir + "/" + field->getName() + "_wm.xml");
- }
- }
- void FF7DataInstaller::ConvertFields(std::string archive, std::string outDir)
- {
- // Everything in here is a field
- Ogre::StringVectorPtr resources = mApp.ResMgr()->listResourceNames("FFVIIFields", "*");
- for (auto& resourceName : *resources)
- {
- if (!QGears::StringUtil::endsWith(resourceName, ".tex")
- && !QGears::StringUtil::endsWith(resourceName, ".tut")
- && !QGears::StringUtil::endsWith(resourceName, ".siz")
- && resourceName != "maplist" && resourceName == "md1_2")
- {
- //try
- {
- QGears::FLevelFilePtr field = QGears::LZSFLevelFileManager::getSingleton().load(resourceName, "FFVIIFields").staticCast<QGears::FLevelFile>();
- FF7PcFieldToQGearsField(field, outDir);
- }
- /*
- catch (const Ogre::Exception& ex)
- {
- std::cout << "ERROR converting: " << resourceName << " Exception: " << ex.what() << std::endl;
- }
- catch (const std::exception& ex)
- {
- std::cout << "ERROR converting: " << resourceName << " Exception: " << ex.what() << std::endl;
- }*/
- }
- }
- }
|