mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-26 21:54:22 +01:00
Codechange: Replace all FILE * with FileHandle RAII class. (#12718)
This removes the need to manually ensure all files are closed.
This commit is contained in:
@@ -122,18 +122,19 @@ int8_t SaveHighScoreValueNetwork()
|
||||
/** Save HighScore table to file */
|
||||
void SaveToHighScore()
|
||||
{
|
||||
std::unique_ptr<FILE, FileDeleter> fp(fopen(_highscore_file.c_str(), "wb"));
|
||||
if (fp == nullptr) return;
|
||||
auto ofp = FileHandle::Open(_highscore_file, "wb");
|
||||
if (!ofp.has_value()) return;
|
||||
auto &fp = *ofp;
|
||||
|
||||
/* Does not iterate through the complete array!. */
|
||||
for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
||||
for (HighScore &hs : _highscore_table[i]) {
|
||||
/* This code is weird and old fashioned to keep compatibility with the old high score files. */
|
||||
uint8_t name_length = ClampTo<uint8_t>(hs.name.size());
|
||||
if (fwrite(&name_length, sizeof(name_length), 1, fp.get()) != 1 || // Write the string length of the name
|
||||
fwrite(hs.name.data(), name_length, 1, fp.get()) > 1 || // Yes... could be 0 bytes too
|
||||
fwrite(&hs.score, sizeof(hs.score), 1, fp.get()) != 1 ||
|
||||
fwrite(" ", 2, 1, fp.get()) != 1) { // Used to be hs.title, not saved anymore; compatibility
|
||||
if (fwrite(&name_length, sizeof(name_length), 1, fp) != 1 || // Write the string length of the name
|
||||
fwrite(hs.name.data(), name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
|
||||
fwrite(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
|
||||
fwrite(" ", 2, 1, fp) != 1) { // Used to be hs.title, not saved anymore; compatibility
|
||||
Debug(misc, 1, "Could not save highscore.");
|
||||
return;
|
||||
}
|
||||
@@ -146,8 +147,9 @@ void LoadFromHighScore()
|
||||
{
|
||||
std::fill(_highscore_table.begin(), _highscore_table.end(), HighScores{});
|
||||
|
||||
std::unique_ptr<FILE, FileDeleter> fp(fopen(_highscore_file.c_str(), "rb"));
|
||||
if (fp == nullptr) return;
|
||||
auto ofp = FileHandle::Open(_highscore_file, "rb");
|
||||
if (!ofp.has_value()) return;
|
||||
auto &fp = *ofp;
|
||||
|
||||
/* Does not iterate through the complete array!. */
|
||||
for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
||||
@@ -156,10 +158,10 @@ void LoadFromHighScore()
|
||||
uint8_t name_length;
|
||||
char buffer[std::numeric_limits<decltype(name_length)>::max() + 1];
|
||||
|
||||
if (fread(&name_length, sizeof(name_length), 1, fp.get()) != 1 ||
|
||||
fread(buffer, name_length, 1, fp.get()) > 1 || // Yes... could be 0 bytes too
|
||||
fread(&hs.score, sizeof(hs.score), 1, fp.get()) != 1 ||
|
||||
fseek(fp.get(), 2, SEEK_CUR) == -1) { // Used to be hs.title, not saved anymore; compatibility
|
||||
if (fread(&name_length, sizeof(name_length), 1, fp) != 1 ||
|
||||
fread(buffer, name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
|
||||
fread(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
|
||||
fseek(fp, 2, SEEK_CUR) == -1) { // Used to be hs.title, not saved anymore; compatibility
|
||||
Debug(misc, 1, "Highscore corrupted");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user