Skip to content

Commit 9526c68

Browse files
srifqisfan5
authored andcommittedNov 8, 2017
Fix issue Minetest crash when custom font path is not exist
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.
1 parent d693f95 commit 9526c68

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-15
lines changed
 

‎src/fontengine.cpp

+53-15
Original file line numberDiff line numberDiff line change
@@ -332,32 +332,70 @@ void FontEngine::initFont(unsigned int basesize, FontMode mode)
332332
font_path.c_str(), size, true, true, font_shadow,
333333
font_shadow_alpha);
334334

335-
if (font != NULL) {
335+
if (font) {
336336
m_font_cache[mode][basesize] = font;
337337
return;
338338
}
339339

340-
// try fallback font
341-
errorstream << "FontEngine: failed to load: " << font_path << ", trying to fall back "
342-
"to fallback font" << std::endl;
340+
if (font_config_prefix == "mono_") {
341+
const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
343342

344-
font_path = g_settings->get(font_config_prefix + "fallback_font_path");
343+
if (font_path != mono_font_path) {
344+
// try original mono font
345+
errorstream << "FontEngine: failed to load custom mono "
346+
"font: " << font_path << ", trying to fall back to "
347+
"original mono font" << std::endl;
345348

346-
font = gui::CGUITTFont::createTTFont(m_env,
347-
font_path.c_str(), size, true, true, font_shadow,
348-
font_shadow_alpha);
349+
font = gui::CGUITTFont::createTTFont(m_env,
350+
mono_font_path.c_str(), size, true, true,
351+
font_shadow, font_shadow_alpha);
349352

350-
if (font != NULL) {
351-
m_font_cache[mode][basesize] = font;
352-
return;
353+
if (font) {
354+
m_font_cache[mode][basesize] = font;
355+
return;
356+
}
357+
}
358+
} else {
359+
// try fallback font
360+
errorstream << "FontEngine: failed to load: " << font_path <<
361+
", trying to fall back to fallback font" << std::endl;
362+
363+
font_path = g_settings->get(font_config_prefix + "fallback_font_path");
364+
365+
font = gui::CGUITTFont::createTTFont(m_env,
366+
font_path.c_str(), size, true, true, font_shadow,
367+
font_shadow_alpha);
368+
369+
if (font) {
370+
m_font_cache[mode][basesize] = font;
371+
return;
372+
}
373+
374+
const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
375+
376+
if (font_path != fallback_font_path) {
377+
// try original fallback font
378+
errorstream << "FontEngine: failed to load custom fallback "
379+
"font: " << font_path << ", trying to fall back to "
380+
"original fallback font" << std::endl;
381+
382+
font = gui::CGUITTFont::createTTFont(m_env,
383+
fallback_font_path.c_str(), size, true, true,
384+
font_shadow, font_shadow_alpha);
385+
386+
if (font) {
387+
m_font_cache[mode][basesize] = font;
388+
return;
389+
}
390+
}
353391
}
354392

355393
// give up
356394
errorstream << "FontEngine: failed to load freetype font: "
357395
<< font_path << std::endl;
358-
errorstream << "minetest can not continue without a valid font. Please correct "
359-
"the 'font_path' setting or install the font file in the proper "
360-
"location" << std::endl;
396+
errorstream << "minetest can not continue without a valid font. "
397+
"Please correct the 'font_path' setting or install the font "
398+
"file in the proper location" << std::endl;
361399
abort();
362400
}
363401
#endif
@@ -459,7 +497,7 @@ void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
459497
}
460498
}
461499

462-
if (font != NULL) {
500+
if (font) {
463501
font->grab();
464502
m_font_cache[mode][basesize] = font;
465503
}

‎src/settings.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const
361361
}
362362

363363

364+
const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
365+
{
366+
MutexAutoLock lock(m_mutex);
367+
368+
SettingEntries::const_iterator n;
369+
if ((n = m_defaults.find(name)) == m_defaults.end()) {
370+
throw SettingNotFoundException("Setting [" + name + "] not found.");
371+
}
372+
return n->second;
373+
}
374+
375+
364376
Settings *Settings::getGroup(const std::string &name) const
365377
{
366378
const SettingsEntry &entry = getEntry(name);
@@ -379,6 +391,15 @@ const std::string &Settings::get(const std::string &name) const
379391
}
380392

381393

394+
const std::string &Settings::getDefault(const std::string &name) const
395+
{
396+
const SettingsEntry &entry = getEntryDefault(name);
397+
if (entry.is_group)
398+
throw SettingNotFoundException("Setting [" + name + "] is a group.");
399+
return entry.value;
400+
}
401+
402+
382403
bool Settings::getBool(const std::string &name) const
383404
{
384405
return is_yes(get(name));
@@ -571,6 +592,17 @@ bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
571592
}
572593

573594

595+
bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const
596+
{
597+
try {
598+
val = getEntryDefault(name);
599+
return true;
600+
} catch (SettingNotFoundException &e) {
601+
return false;
602+
}
603+
}
604+
605+
574606
bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
575607
{
576608
try {
@@ -593,6 +625,17 @@ bool Settings::getNoEx(const std::string &name, std::string &val) const
593625
}
594626

595627

628+
bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const
629+
{
630+
try {
631+
val = getDefault(name);
632+
return true;
633+
} catch (SettingNotFoundException &e) {
634+
return false;
635+
}
636+
}
637+
638+
596639
bool Settings::getFlag(const std::string &name) const
597640
{
598641
try {

‎src/settings.h

+4
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ class Settings {
129129
***********/
130130

131131
const SettingsEntry &getEntry(const std::string &name) const;
132+
const SettingsEntry &getEntryDefault(const std::string &name) const;
132133
Settings *getGroup(const std::string &name) const;
133134
const std::string &get(const std::string &name) const;
135+
const std::string &getDefault(const std::string &name) const;
134136
bool getBool(const std::string &name) const;
135137
u16 getU16(const std::string &name) const;
136138
s16 getS16(const std::string &name) const;
@@ -160,8 +162,10 @@ class Settings {
160162
***************************************/
161163

162164
bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
165+
bool getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const;
163166
bool getGroupNoEx(const std::string &name, Settings *&val) const;
164167
bool getNoEx(const std::string &name, std::string &val) const;
168+
bool getDefaultNoEx(const std::string &name, std::string &val) const;
165169
bool getFlag(const std::string &name) const;
166170
bool getU16NoEx(const std::string &name, u16 &val) const;
167171
bool getS16NoEx(const std::string &name, s16 &val) const;

0 commit comments

Comments
 (0)
Please sign in to comment.