From b2cc191bc665d13f50360e5508234e653669a372 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Fri, 28 Apr 2017 00:09:14 +0200 Subject: [PATCH] Integrate the EmbeddedResourceManager into FlightGear ${CMAKE_SOURCE_DIR}/src/EmbeddedResources/FlightGear-resources.xml (currently empty) is automatically "compiled" into ${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.[ch]xx by fgrcc inside the build directory. These files are incorporated into the FlightGear build (FlightGear-resources.cxx is linked into FlightGear). When the XML embedded resource declaration file added here, FlightGear-resources.xml, is compiled, fgrcc is passed the --root=${CMAKE_SOURCE_DIR} option, so that files referred to in FlightGear-resources.xml are looked up relatively to the root directory of the FlightGear repository. One could use a second XML embedded resource declaration file compiled with a different --root option to grab files from FGData, for instance. I would name such a file FGData-resources.xml to be consistent with the current naming scheme. Note: this --root option applies to the paths of real files. Don't confuse it with the 'prefix' attribute of elements inside XML resource declaration files (such as FlightGear-resources.xml), which applies to the virtual path of each resource defined beneath. The commands in src/Main/CMakeLists.txt ensure that FlightGear-resources.xml is recompiled with fgrcc whenever it is changed, and obviously also when FlightGear-resources.cxx or FlightGear-resources.hxx is missing. However, CMake doesn't know how to parse fgrcc XML resource declaration files, therefore when a resource is modified but the XML file it is declared in is not (here, FlightGear-resources.xml), you have to trigger yourself a recompilation of the XML resource declaration file to see the new resource contents inside FlightGear. The easiest ways to do so are: - either update the timestamp of the XML resource declaration file; - or remove one or both of the generated files (FlightGear-resources.cxx and FlightGear-resources.hxx here). The EmbeddedResourceManager is created in fgMainInit() just after Options::processOptions() set the language that was either requested by the user or obtained from the system (locales). Resources from FlightGear-resources.cxx are added to it, after which EmbeddedResourceManager::selectLocale() is called with the user's preferred locale (obtained with FGLocale::getPreferredLanguage()). Upon reset (fgStartNewReset()), EmbeddedResourceManager::selectLocale() is called in a similar way after Options::processOptions(), however in this case the EmbeddedResourceManager instance doesn't have to be recreated. --- src/EmbeddedResources/FlightGear-resources.xml | 7 +++++++ src/Main/CMakeLists.txt | 10 ++++++++++ src/Main/fg_init.cxx | 11 ++++++++++- src/Main/main.cxx | 13 +++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/EmbeddedResources/FlightGear-resources.xml diff --git a/src/EmbeddedResources/FlightGear-resources.xml b/src/EmbeddedResources/FlightGear-resources.xml new file mode 100644 index 000000000..1cb61f497 --- /dev/null +++ b/src/EmbeddedResources/FlightGear-resources.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt index f1c22e075..68a86f09b 100644 --- a/src/Main/CMakeLists.txt +++ b/src/Main/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES subsystemFactory.cxx screensaver_control.cxx ${RESOURCE_FILE} + ${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.cxx ) set(HEADERS @@ -40,8 +41,17 @@ set(HEADERS subsystemFactory.hxx AircraftDirVisitorBase.hxx screensaver_control.hxx + ${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.hxx ) +add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.cxx + ${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.hxx + COMMAND fgrcc --root=${CMAKE_SOURCE_DIR} --output-cpp-file=${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.cxx --init-func-name=initFlightGearEmbeddedResources --output-header-file=${CMAKE_BINARY_DIR}/src/EmbeddedResources/FlightGear-resources.hxx --output-header-identifier=_FG_FLIGHTGEAR_EMBEDDED_RESOURCES ${CMAKE_SOURCE_DIR}/src/EmbeddedResources/FlightGear-resources.xml + DEPENDS + fgrcc ${CMAKE_SOURCE_DIR}/src/EmbeddedResources/FlightGear-resources.xml + ) + get_property(FG_SOURCES GLOBAL PROPERTY FG_SOURCES) get_property(FG_HEADERS GLOBAL PROPERTY FG_HEADERS) diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index edb464ef2..8bd45ded9 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -107,6 +108,7 @@ #include #include #include +#include
#include #include #include @@ -1117,7 +1119,14 @@ void fgStartNewReset() fgInitGeneral(); // all of this? flightgear::Options::sharedInstance()->processOptions(); - + + const auto& resMgr = simgear::EmbeddedResourceManager::instance(); + // The language was (re)set in processOptions() + const string locale = globals->get_locale()->getPreferredLanguage(); + resMgr->selectLocale(locale); + SG_LOG(SG_GENERAL, SG_INFO, + "EmbeddedResourceManager: selected locale '" << locale << "'"); + // PRESERVED properties over-write state from options, intentionally if ( copyProperties(preserved, globals->get_props()) ) { SG_LOG( SG_GENERAL, SG_INFO, "Preserved state restored successfully" ); diff --git a/src/Main/main.cxx b/src/Main/main.cxx index b7ee1e67e..c72db1c3a 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -56,6 +56,7 @@ extern bool global_crashRptEnabled; #include #include +#include
#include #include #include @@ -80,6 +81,9 @@ extern bool global_crashRptEnabled; #include "subsystemFactory.hxx" #include "options.hxx" +#include +#include + #if defined(HAVE_QT) #include #endif @@ -537,6 +541,15 @@ int fgMainInit( int argc, char **argv ) return EXIT_SUCCESS; } + const auto& resMgr = simgear::EmbeddedResourceManager::createInstance(); + initFlightGearEmbeddedResources(); + // The language was set in processOptions() + const std::string locale = globals->get_locale()->getPreferredLanguage(); + // Must always be done after all resources have been added to 'resMgr' + resMgr->selectLocale(locale); + SG_LOG(SG_GENERAL, SG_INFO, + "EmbeddedResourceManager: selected locale '" << locale << "'"); + // Initialize the Window/Graphics environment. fgOSInit(&argc, argv); _bootstrap_OSInit++;