VGearsApplication.cpp 16 KB

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