MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / SaveToHighScore

Function SaveToHighScore

src/highscore.cpp:116–136  ·  view source on GitHub ↗

Save HighScore table to file */

Source from the content-addressed store, hash-verified

114
115/** Save HighScore table to file */
116void SaveToHighScore()
117{
118 auto ofp = FileHandle::Open(_highscore_file, "wb");
119 if (!ofp.has_value()) return;
120 auto &fp = *ofp;
121
122 /* Does not iterate through the complete array!. */
123 for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
124 for (HighScore &hs : _highscore_table[i]) {
125 /* This code is weird and old fashioned to keep compatibility with the old high score files. */
126 uint8_t name_length = ClampTo<uint8_t>(hs.name.size());
127 if (fwrite(&name_length, sizeof(name_length), 1, fp) != 1 || // Write the string length of the name
128 fwrite(hs.name.data(), name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
129 fwrite(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
130 fwrite(" ", 2, 1, fp) != 1) { // Used to be hs.title, not saved anymore; compatibility
131 Debug(misc, 1, "Could not save highscore.");
132 return;
133 }
134 }
135 }
136}
137
138/** Initialize the highscore table to 0 and if any file exists, load in values */
139void LoadFromHighScore()

Callers 1

PostMainLoopFunction · 0.85

Calls 2

sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected