]> git.lizzy.rs Git - minetest.git/commitdiff
Fix issue Minetest crash when custom font path is not exist
authorMuhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com>
Sat, 3 Jun 2017 02:51:48 +0000 (09:51 +0700)
committersfan5 <sfan5@live.de>
Wed, 8 Nov 2017 15:14:00 +0000 (16:14 +0100)
We try to use default fallback for both mono and main font when custom font path is not exist. This way, if Minetest is not corrupted, we could avoid crash.

src/fontengine.cpp
src/settings.cpp
src/settings.h

index 7ae0f32ed1dc014370a8255fba913b67d9299282..b40e1ef0f0bd483f36ea62e0e7bd0f251d10189c 100644 (file)
@@ -332,32 +332,70 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode)
                                font_path.c_str(), size, true, true, font_shadow,
                                font_shadow_alpha);
 
-               if (font != NULL) {
+               if (font) {
                        m_font_cache[mode][basesize] = font;
                        return;
                }
 
-               // try fallback font
-               errorstream << "FontEngine: failed to load: " << font_path << ", trying to fall back "
-                               "to fallback font" << std::endl;
+               if (font_config_prefix == "mono_") {
+                       const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
 
-               font_path = g_settings->get(font_config_prefix + "fallback_font_path");
+                       if (font_path != mono_font_path) {
+                               // try original mono font
+                               errorstream << "FontEngine: failed to load custom mono "
+                                               "font: " << font_path << ", trying to fall back to "
+                                               "original mono font" << std::endl;
 
-               font = gui::CGUITTFont::createTTFont(m_env,
-                       font_path.c_str(), size, true, true, font_shadow,
-                       font_shadow_alpha);
+                               font = gui::CGUITTFont::createTTFont(m_env,
+                                       mono_font_path.c_str(), size, true, true,
+                                       font_shadow, font_shadow_alpha);
 
-               if (font != NULL) {
-                       m_font_cache[mode][basesize] = font;
-                       return;
+                               if (font) {
+                                       m_font_cache[mode][basesize] = font;
+                                       return;
+                               }
+                       }
+               } else {
+                       // try fallback font
+                       errorstream << "FontEngine: failed to load: " << font_path <<
+                                       ", trying to fall back to fallback font" << std::endl;
+
+                       font_path = g_settings->get(font_config_prefix + "fallback_font_path");
+
+                       font = gui::CGUITTFont::createTTFont(m_env,
+                               font_path.c_str(), size, true, true, font_shadow,
+                               font_shadow_alpha);
+
+                       if (font) {
+                               m_font_cache[mode][basesize] = font;
+                               return;
+                       }
+
+                       const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
+
+                       if (font_path != fallback_font_path) {
+                               // try original fallback font
+                               errorstream << "FontEngine: failed to load custom fallback "
+                                               "font: " << font_path << ", trying to fall back to "
+                                               "original fallback font" << std::endl;
+
+                               font = gui::CGUITTFont::createTTFont(m_env,
+                                       fallback_font_path.c_str(), size, true, true,
+                                       font_shadow, font_shadow_alpha);
+
+                               if (font) {
+                                       m_font_cache[mode][basesize] = font;
+                                       return;
+                               }
+                       }
                }
 
                // give up
                errorstream << "FontEngine: failed to load freetype font: "
                                << font_path << std::endl;
-               errorstream << "minetest can not continue without a valid font. Please correct "
-                               "the 'font_path' setting or install the font file in the proper "
-                               "location" << std::endl;
+               errorstream << "minetest can not continue without a valid font. "
+                               "Please correct the 'font_path' setting or install the font "
+                               "file in the proper location" << std::endl;
                abort();
        }
 #endif
@@ -459,7 +497,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
                }
        }
 
-       if (font != NULL) {
+       if (font) {
                font->grab();
                m_font_cache[mode][basesize] = font;
        }
index ea54ab3488de1785b058eca7f3af6c47c03d9557..0c34eb10c921b7236db6423204ec7a5fd3d4a1b0 100644 (file)
@@ -361,6 +361,18 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const
 }
 
 
+const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
+{
+       MutexAutoLock lock(m_mutex);
+
+       SettingEntries::const_iterator n;
+       if ((n = m_defaults.find(name)) == m_defaults.end()) {
+               throw SettingNotFoundException("Setting [" + name + "] not found.");
+       }
+       return n->second;
+}
+
+
 Settings *Settings::getGroup(const std::string &name) const
 {
        const SettingsEntry &entry = getEntry(name);
@@ -379,6 +391,15 @@ const std::string &Settings::get(const std::string &name) const
 }
 
 
+const std::string &Settings::getDefault(const std::string &name) const
+{
+       const SettingsEntry &entry = getEntryDefault(name);
+       if (entry.is_group)
+               throw SettingNotFoundException("Setting [" + name + "] is a group.");
+       return entry.value;
+}
+
+
 bool Settings::getBool(const std::string &name) const
 {
        return is_yes(get(name));
@@ -571,6 +592,17 @@ bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
 }
 
 
+bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const
+{
+       try {
+               val = getEntryDefault(name);
+               return true;
+       } catch (SettingNotFoundException &e) {
+               return false;
+       }
+}
+
+
 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
 {
        try {
@@ -593,6 +625,17 @@ bool Settings::getNoEx(const std::string &name, std::string &val) const
 }
 
 
+bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const
+{
+       try {
+               val = getDefault(name);
+               return true;
+       } catch (SettingNotFoundException &e) {
+               return false;
+       }
+}
+
+
 bool Settings::getFlag(const std::string &name) const
 {
        try {
index e01e86475b83c5d9a4dfe769ed09dc58dcef751c..329a611408cc54800dcd2b147704958e27bde51b 100644 (file)
@@ -129,8 +129,10 @@ class Settings {
         ***********/
 
        const SettingsEntry &getEntry(const std::string &name) const;
+       const SettingsEntry &getEntryDefault(const std::string &name) const;
        Settings *getGroup(const std::string &name) const;
        const std::string &get(const std::string &name) const;
+       const std::string &getDefault(const std::string &name) const;
        bool getBool(const std::string &name) const;
        u16 getU16(const std::string &name) const;
        s16 getS16(const std::string &name) const;
@@ -160,8 +162,10 @@ class Settings {
         ***************************************/
 
        bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
+       bool getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const;
        bool getGroupNoEx(const std::string &name, Settings *&val) const;
        bool getNoEx(const std::string &name, std::string &val) const;
+       bool getDefaultNoEx(const std::string &name, std::string &val) const;
        bool getFlag(const std::string &name) const;
        bool getU16NoEx(const std::string &name, u16 &val) const;
        bool getS16NoEx(const std::string &name, s16 &val) const;