1
0
Fork 0

Codechange: use std::string for the screenshot name/path

pull/10933/head
Rubidium 2023-05-25 18:23:26 +02:00 committed by rubidium42
parent 0e56a73fb8
commit 87ccff16b5
3 changed files with 17 additions and 22 deletions

View File

@ -417,7 +417,7 @@ bool CrashLog::WriteScreenshot()
std::string filename = this->CreateFileName("", false); std::string filename = this->CreateFileName("", false);
bool res = MakeScreenshot(SC_CRASHLOG, filename); bool res = MakeScreenshot(SC_CRASHLOG, filename);
if (res) this->screenshot_filename = _full_screenshot_name; if (res) this->screenshot_filename = _full_screenshot_path;
return res; return res;
} }

View File

@ -40,8 +40,8 @@ static const char * const HEIGHTMAP_NAME = "heightmap"; ///< Default filename
std::string _screenshot_format_name; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format). std::string _screenshot_format_name; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format).
uint _num_screenshot_formats; ///< Number of available screenshot formats. uint _num_screenshot_formats; ///< Number of available screenshot formats.
uint _cur_screenshot_format; ///< Index of the currently selected screenshot format in #_screenshot_formats. uint _cur_screenshot_format; ///< Index of the currently selected screenshot format in #_screenshot_formats.
static char _screenshot_name[128]; ///< Filename of the screenshot file. static std::string _screenshot_name; ///< Filename of the screenshot file.
char _full_screenshot_name[MAX_PATH]; ///< Pathname of the screenshot file. std::string _full_screenshot_path; ///< Pathname of the screenshot file.
uint _heightmap_highest_peak; ///< When saving a heightmap, this contains the highest peak on the map. uint _heightmap_highest_peak; ///< When saving a heightmap, this contains the highest peak on the map.
/** /**
@ -668,43 +668,39 @@ static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, ui
*/ */
static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false) static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false)
{ {
bool generate = StrEmpty(_screenshot_name); bool generate = _screenshot_name.empty();
if (generate) { if (generate) {
if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) { if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
strecpy(_screenshot_name, default_fn, lastof(_screenshot_name)); _screenshot_name = default_fn;
} else { } else {
strecpy(_screenshot_name, GenerateDefaultSaveName().c_str(), lastof(_screenshot_name)); _screenshot_name = GenerateDefaultSaveName();
} }
} }
size_t len = strlen(_screenshot_name);
/* Handle user-specified filenames ending in # with automatic numbering */ /* Handle user-specified filenames ending in # with automatic numbering */
if (StrEndsWith(_screenshot_name, "#")) { if (StrEndsWith(_screenshot_name, "#")) {
generate = true; generate = true;
len -= 1; _screenshot_name.pop_back();
_screenshot_name[len] = '\0';
} }
size_t len = _screenshot_name.size();
/* Add extension to screenshot file */ /* Add extension to screenshot file */
seprintf(&_screenshot_name[len], lastof(_screenshot_name), ".%s", ext); _screenshot_name += fmt::format(".{}", ext);
const char *screenshot_dir = crashlog ? _personal_dir.c_str() : FiosGetScreenshotDir(); const char *screenshot_dir = crashlog ? _personal_dir.c_str() : FiosGetScreenshotDir();
for (uint serial = 1;; serial++) { for (uint serial = 1;; serial++) {
if (seprintf(_full_screenshot_name, lastof(_full_screenshot_name), "%s%s", screenshot_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) { _full_screenshot_path = fmt::format("{}{}", screenshot_dir, _screenshot_name);
/* We need more characters than MAX_PATH -> end with error */
_full_screenshot_name[0] = '\0';
break;
}
if (!generate) break; // allow overwriting of non-automatic filenames if (!generate) break; // allow overwriting of non-automatic filenames
if (!FileExists(_full_screenshot_name)) break; if (!FileExists(_full_screenshot_path)) break;
/* If file exists try another one with same name, but just with a higher index */ /* If file exists try another one with same name, but just with a higher index */
seprintf(&_screenshot_name[len], lastof(_screenshot_name) - len, "#%u.%s", serial, ext); _screenshot_name.erase(len);
_screenshot_name += fmt::format("#{}.{}", serial, ext);
} }
return _full_screenshot_name; return _full_screenshot_path.c_str();
} }
/** Make a screenshot of the current screen. */ /** Make a screenshot of the current screen. */
@ -922,8 +918,7 @@ static bool RealMakeScreenshot(ScreenshotType t, std::string name, uint32 width,
SetScreenshotWindowVisibility(false); SetScreenshotWindowVisibility(false);
} }
_screenshot_name[0] = '\0'; _screenshot_name = name;
if (!name.empty()) strecpy(_screenshot_name, name.c_str(), lastof(_screenshot_name));
bool ret; bool ret;
switch (t) { switch (t) {

View File

@ -34,6 +34,6 @@ bool MakeMinimapWorldScreenshot();
extern std::string _screenshot_format_name; extern std::string _screenshot_format_name;
extern uint _num_screenshot_formats; extern uint _num_screenshot_formats;
extern uint _cur_screenshot_format; extern uint _cur_screenshot_format;
extern char _full_screenshot_name[MAX_PATH]; extern std::string _full_screenshot_path;
#endif /* SCREENSHOT_H */ #endif /* SCREENSHOT_H */