VGearsApplication.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <cstdlib>
  16. #include <iostream>
  17. #include <boost/filesystem.hpp>
  18. #include <boost/program_options.hpp>
  19. #include <OgreConfigFile.h>
  20. #include <OgreArchiveManager.h>
  21. #include <OgreLogManager.h>
  22. #include <OgreConfigDialog.h>
  23. #include "common/VGearsApplication.h"
  24. #include "common/DataDirectory.h"
  25. #include "common/OgrePlugins.h"
  26. #include "VGearsGameState.h"
  27. #include "data/VGearsAFileManager.h"
  28. #include "data/VGearsBackgroundFileManager.h"
  29. #include "data/VGearsCameraMatrixFileManager.h"
  30. #include "data/VGearsHRCFileManager.h"
  31. #include "data/VGearsLZSFLevelFileManager.h"
  32. #include "data/VGearsPaletteFileManager.h"
  33. #include "data/VGearsPFileManager.h"
  34. #include "data/VGearsRSDFileManager.h"
  35. #include "data/VGearsTexCodec.h"
  36. #include "map/VGearsBackground2DFileManager.h"
  37. #include "map/VGearsWalkmeshFileManager.h"
  38. #include "data/FinalFantasy7/FF7ModelListFileManager.h"
  39. #include "data/VGearsLGPArchiveFactory.h"
  40. #include "data/VGearsTriggersFile.h"
  41. #include "data/VGearsMapListFile.h"
  42. #include "Version.h"
  43. #include "common/InstallStamp.h"
  44. #include <OgreControllerManager.h>
  45. #include <OgreDefaultHardwareBufferManager.h>
  46. #include <OgreMaterialManager.h>
  47. #include <OgreMeshManager.h>
  48. /**
  49. * Application singleton.
  50. */
  51. template<> VGears::Application* Ogre::Singleton<VGears::Application>::msSingleton = nullptr;
  52. /**
  53. * Directory with the Ogre plugins, found when V-Gears was built. Set by CMake (VGEARS_OGRE_PLUGIN_DIR).
  54. */
  55. #ifndef VGEARS_OGRE_PLUGIN_DIR
  56. #define VGEARS_OGRE_PLUGIN_DIR ""
  57. #endif
  58. namespace VGears{
  59. namespace bfs = boost::filesystem;
  60. namespace bpo = boost::program_options;
  61. typedef Ogre::ConfigFile::SettingsMultiMap SettingsMultiMap;
  62. const char* Application::CLI_SECTION_GENERIC("Generic options");
  63. const char* Application::CLI_HELP("help");
  64. const char* Application::CLI_HELP_DESCRIPTION("This help message");
  65. const char* Application::CLI_CONFIG_FILE("config-file");
  66. const char* Application::CLI_CONFIG_FILE_DESCRIPTION(
  67. "Path to Ogre config file containing selected rendersystem and resolution"
  68. );
  69. const char* Application::CLI_LOG_FILE("log-file");
  70. const char* Application::CLI_LOG_FILE_DESCRIPTION("Path to log file");
  71. const char* Application::CLI_PLUGINS_FILE("plugins-file");
  72. const char* Application::CLI_PLUGINS_FILE_DESCRIPTION(
  73. "Path to config file containing available plugins"
  74. );
  75. const char* Application::CLI_RESOURCES_FILE("resources-file");
  76. const char* Application::CLI_RESOURCES_FILE_DESCRIPTION(
  77. "path to config file containing available resources"
  78. );
  79. const char* Application::CLI_DATA_DIR("data-dir");
  80. const char* Application::CLI_DATA_DIR_DESCRIPTION(
  81. "Directory with the game data and configuration, where saves and logs are written. "
  82. "Defaults to $VGEARS_DATA_DIR, the current directory if it has the V-Gears files, or ~/.v-gears"
  83. );
  84. Application::Application(int argc, char *argv[]): _argc(argc), _argv(argv){}
  85. Application::~Application(){destroyComponents();}
  86. bool Application::initOgre(bool hideWindow){
  87. if(_initialized) return false;
  88. _initialized = true;
  89. if (_argc && _argv){
  90. if (!processCommandLine(_argc, _argv)) return false;
  91. }
  92. // No plugins file is passed to Ogre: the plugins are looked for in several places, see loadPlugins.
  93. _root = std::make_unique<Ogre::Root>("", config_filename_, log_filename_);
  94. loadPlugins();
  95. _overlay_system = std::make_unique<Ogre::OverlaySystem>();
  96. Ogre::Log* default_log(
  97. Ogre::LogManager::getSingleton().getDefaultLog()
  98. );
  99. assert(default_log);
  100. default_log->setLogDetail(Ogre::LL_BOREME);
  101. // configure
  102. // Show the configuration dialog and initialise the system
  103. // You can skip this and use root.restoreConfig() to load configuration
  104. // settings if you were sure there are valid ones saved in ogre.cfg
  105. // IVV ERROR COMPILING
  106. if(/* !_root->restoreConfig() &&*/ !_root->showConfigDialog(NULL)){
  107. return false;
  108. }
  109. if (hideWindow){
  110. Ogre::NameValuePairList list;
  111. list.emplace("hidden", "true");
  112. _root->initialise(false, getWindowTitle());
  113. //
  114. _render_window = _root->createRenderWindow(getWindowTitle(), 1, 1, false, &list);
  115. }
  116. else{
  117. _render_window = _root->initialise(false, getWindowTitle());
  118. // TODO: Get actual resolution from config
  119. _render_window = _root->createRenderWindow(getWindowTitle(), 1024, 768, false);
  120. }
  121. registerArchiveFactories();
  122. loadResourcesConfig();
  123. initComponents();
  124. return true;
  125. }
  126. bool Application::initOgreHeadless(){
  127. if (_initialized) return false;
  128. _initialized = true;
  129. _headless = true;
  130. // Ogre writes its messages to the log file only, not to the terminal: the programs that convert data have
  131. // their own output. Created before the root, which then doesn't create its own log.
  132. if (Ogre::LogManager::getSingletonPtr() == nullptr){
  133. _log_manager = std::make_unique<Ogre::LogManager>();
  134. _log_manager->createLog(log_filename_, true, false, false);
  135. }
  136. // No plugins file is passed to Ogre: the plugins are looked for in several places, see loadPlugins.
  137. _root = std::make_unique<Ogre::Root>("", config_filename_, log_filename_);
  138. loadPlugins(true);
  139. _overlay_system = std::make_unique<Ogre::OverlaySystem>();
  140. Ogre::LogManager::getSingleton().getDefaultLog()->setLogDetail(Ogre::LL_BOREME);
  141. // Without a render system nobody creates the hardware buffer manager, and meshes need one.
  142. _buffer_manager = std::make_unique<Ogre::DefaultHardwareBufferManager>();
  143. // What Ogre::Root::initialise and the render system do, except what needs a graphics driver: the
  144. // controllers (used by animated materials), a texture manager for the textures named by the materials,
  145. // the default material that new materials copy, and the built-in meshes.
  146. if (Ogre::ControllerManager::getSingletonPtr() == nullptr)
  147. _controller_manager = std::make_unique<Ogre::ControllerManager>();
  148. _texture_manager = std::make_unique<Ogre::DefaultTextureManager>();
  149. Ogre::MaterialManager::getSingleton().initialise();
  150. Ogre::MeshManager::getSingleton()._initialise();
  151. registerArchiveFactories();
  152. loadResourcesConfig();
  153. initComponents();
  154. return true;
  155. }
  156. void Application::loadPlugins(bool skip_render_systems){
  157. const bfs::path plugins_file(bfs::absolute(bfs::path(plugins_filename_)));
  158. OgrePlugins::Config config;
  159. if (!OgrePlugins::ReadConfig(plugins_file, config))
  160. throw std::runtime_error("The Ogre plugins file can't be read: " + plugins_file.string());
  161. const std::vector<bfs::path> directories(OgrePlugins::SearchDirectories(
  162. plugins_file.parent_path(), config.folder,
  163. DataDirectory::ExecutableDirectory(_argc > 0 && _argv ? _argv[0] : nullptr), VGEARS_OGRE_PLUGIN_DIR
  164. ));
  165. const String ogre_version(
  166. std::to_string(OGRE_VERSION_MAJOR) + "." + std::to_string(OGRE_VERSION_MINOR) + "."
  167. + std::to_string(OGRE_VERSION_PATCH)
  168. );
  169. for (const String& name : config.plugins){
  170. if (skip_render_systems && name.compare(0, 13, "RenderSystem_") == 0){
  171. Ogre::LogManager::getSingleton().logMessage("Not loading Ogre plugin " + name + ": no render system");
  172. continue;
  173. }
  174. const bfs::path file(OgrePlugins::Find(name, directories, ogre_version));
  175. if (file.empty()){
  176. String searched;
  177. for (const bfs::path& directory : directories) searched += "\n " + directory.string();
  178. throw std::runtime_error(
  179. "The Ogre plugin " + name + " (listed in " + plugins_file.string() + ") was not found in:"
  180. + searched
  181. );
  182. }
  183. Ogre::LogManager::getSingleton().logMessage("Loading Ogre plugin " + file.string());
  184. try{
  185. _root->loadPlugin(file.string());
  186. }
  187. catch (const Ogre::Exception& e){
  188. // The exception can point to strings of the plugin library, which is unloaded when the root is
  189. // destroyed, before the exception is caught in main. Copy the message now.
  190. throw std::runtime_error(
  191. "The Ogre plugin " + file.string() + " could not be loaded: " + e.getFullDescription()
  192. );
  193. }
  194. }
  195. }
  196. void Application::loadResourcesConfig(){
  197. // Load resource paths from config file
  198. Ogre::ConfigFile config_file;
  199. config_file.load(resources_filename_);
  200. // Go through all sections & settings in the file
  201. Ogre::ConfigFile::SectionIterator section(config_file.getSectionIterator());
  202. Ogre::String section_name, archive_type, archive_name;
  203. res_mgr_ = (&Ogre::ResourceGroupManager::getSingleton());
  204. while(section.hasMoreElements()){
  205. section_name = section.peekNextKey();
  206. SettingsMultiMap *settings(section.getNext());
  207. SettingsMultiMap::const_iterator it(settings->begin());
  208. SettingsMultiMap::const_iterator end(settings->end());
  209. while(it != end){
  210. archive_type = it->first;
  211. archive_name = it->second;
  212. res_mgr_->addResourceLocation(archive_name, archive_type, section_name, true);
  213. ++ it;
  214. }
  215. }
  216. }
  217. Ogre::Root* Application::getRoot(){return _root.get();}
  218. Ogre::RenderWindow* Application::getRenderWindow(){return _render_window;}
  219. const String& Application::getResourcesFilename(){
  220. return resources_filename_;
  221. }
  222. String Application::getWindowTitle() const {return VGEARS_VERSION_SIGNATURE;}
  223. bool Application::processCommandLine( int argc, char *argv[]){
  224. bfs::path self(argv[0]);
  225. const String self_stem(self.stem().string());
  226. const String config_filename(self_stem + EXT_CONFIG);
  227. const String log_filename(self_stem + EXT_LOG);
  228. #ifdef NDEBUG
  229. const String plugins_filename("plugins" + EXT_CONFIG);
  230. const String resources_filename("resources" + EXT_CONFIG);
  231. #else
  232. const String plugins_filename("plugins_d" + EXT_CONFIG);
  233. const String resources_filename("resources_d" + EXT_CONFIG);
  234. #endif
  235. bpo::options_description generic(CLI_SECTION_GENERIC);
  236. generic.add_options()
  237. (CLI_HELP, CLI_HELP_DESCRIPTION)
  238. ( CLI_CONFIG_FILE
  239. , bpo::value< String >()->default_value(config_filename)
  240. , CLI_CONFIG_FILE_DESCRIPTION
  241. )
  242. ( CLI_LOG_FILE
  243. , bpo::value< String >()->default_value(log_filename)
  244. , CLI_LOG_FILE_DESCRIPTION
  245. )
  246. ( CLI_PLUGINS_FILE
  247. , bpo::value< String >()->default_value(plugins_filename)
  248. , CLI_PLUGINS_FILE_DESCRIPTION
  249. )
  250. ( CLI_RESOURCES_FILE
  251. , bpo::value< String >()->default_value(resources_filename)
  252. , CLI_RESOURCES_FILE_DESCRIPTION
  253. )
  254. ( CLI_DATA_DIR
  255. , bpo::value< String >()
  256. , CLI_DATA_DIR_DESCRIPTION
  257. )
  258. ;
  259. bpo::variables_map vm;
  260. bpo::store(bpo::command_line_parser(argc, argv).options(generic).run(), vm);
  261. if (vm.count(CLI_HELP)){
  262. std::cout << "Usage: " << self_stem << "\n" << generic << "\n";
  263. return false;
  264. }
  265. bpo::notify(vm);
  266. // The game changes into the data directory, so the files given in the command line are made absolute first.
  267. // The default ones are left relative, to be found in the data directory.
  268. const bfs::path start_directory(bfs::current_path());
  269. auto file_option = [&](const char* name) -> String{
  270. const String value(vm[name].as<String>());
  271. if (vm[name].defaulted() || value.empty()) return value;
  272. return bfs::absolute(bfs::path(value), start_directory).lexically_normal().string();
  273. };
  274. config_filename_ = file_option(CLI_CONFIG_FILE);
  275. log_filename_ = file_option(CLI_LOG_FILE);
  276. plugins_filename_ = file_option(CLI_PLUGINS_FILE);
  277. resources_filename_ = file_option(CLI_RESOURCES_FILE);
  278. String requested_directory;
  279. if (vm.count(CLI_DATA_DIR)) requested_directory = vm[CLI_DATA_DIR].as<String>();
  280. else if (const char* env = std::getenv(DataDirectory::ENV_DATA_DIR)) requested_directory = env;
  281. const DataDirectory::Resolution data_directory(DataDirectory::Resolve(
  282. requested_directory, start_directory, DataDirectory::DefaultDirectory(),
  283. DataDirectory::TemplateCandidates(DataDirectory::ExecutableDirectory(argv[0]))
  284. ));
  285. if (!data_directory.error.empty()){
  286. std::cerr << "V-Gears can't start: " << data_directory.error << std::endl;
  287. return false;
  288. }
  289. if (data_directory.seeded){
  290. std::cout << "Copied " << data_directory.copied_files << " V-Gears files from "
  291. << data_directory.template_directory.string() << " to " << data_directory.directory.string()
  292. << ". Run v-gears-installer to add the data of the original game." << std::endl;
  293. }
  294. boost::system::error_code ec;
  295. bfs::current_path(data_directory.directory, ec);
  296. if (ec){
  297. std::cerr << "V-Gears can't start: can't use the data directory " << data_directory.directory.string()
  298. << ": " << ec.message() << std::endl;
  299. return false;
  300. }
  301. std::cout << "Data directory: " << data_directory.directory.string() << std::endl;
  302. // The record of the installations says if the game data must be installed again.
  303. bool recorded = false;
  304. for (const std::string& problem : InstallStamp::Problems(data_directory.directory, &recorded))
  305. std::cout << "Warning: " << problem << std::endl;
  306. if (!recorded && bfs::exists(data_directory.directory / "data" / "fields" / "_fields.xml", ec)){
  307. std::cout << "Warning: the game data was installed by an older version of v-gears-installer, and may be "
  308. "outdated. Install it again with v-gears-installer." << std::endl;
  309. }
  310. return true;
  311. }
  312. void Application::registerArchiveFactories(){
  313. Ogre::ArchiveManager::getSingleton().addArchiveFactory(
  314. new LGPArchiveFactory()
  315. );
  316. }
  317. void Application::initComponents(){
  318. VGears::TexCodec::install();
  319. VGears::TexCodec::initialise();
  320. createResourceManagers();
  321. }
  322. void Application::destroyComponents(){
  323. destroyResourceManagers();
  324. VGears::TexCodec::shutdown();
  325. VGears::TexCodec::uninstall();
  326. }
  327. void Application::createResourceManagers(){
  328. assert(_resource_managers.empty());
  329. _resource_managers.emplace_back(
  330. std::make_shared<VGears::CameraMatrixFileManager>()
  331. );
  332. _resource_managers.emplace_back(
  333. std::make_shared<VGears::PaletteFileManager>()
  334. );
  335. _resource_managers.emplace_back(
  336. std::make_shared<VGears::WalkmeshFileManager>()
  337. );
  338. _resource_managers.emplace_back(
  339. std::make_shared<VGears::ModelListFileManager>()
  340. );
  341. _resource_managers.emplace_back(
  342. std::make_shared<VGears::HRCFileManager>()
  343. );
  344. _resource_managers.emplace_back(
  345. std::make_shared<VGears::RSDFileManager>()
  346. );
  347. _resource_managers.emplace_back(
  348. std::make_shared<VGears::AFileManager>()
  349. );
  350. _resource_managers.emplace_back(
  351. std::make_shared<VGears::PFileManager>()
  352. );
  353. _resource_managers.emplace_back(
  354. std::make_shared<VGears::LZSFLevelFileManager>()
  355. );
  356. _resource_managers.emplace_back(
  357. std::make_shared<VGears::BackgroundFileManager>()
  358. );
  359. _resource_managers.emplace_back(
  360. std::make_shared<VGears::Background2DFileManager>()
  361. );
  362. _resource_managers.emplace_back(
  363. std::make_shared<VGears::TriggersFileManager>()
  364. );
  365. _resource_managers.emplace_back(
  366. std::make_shared<VGears::MapListFileManager>()
  367. );
  368. }
  369. void Application::destroyResourceManagers(){_resource_managers.clear();}
  370. }