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

Function GetNetworkRevisionString

src/network/core/network_game_info.cpp:46–85  ·  view source on GitHub ↗

* Get the network version string used by this build. * The returned string is guaranteed to be at most NETWORK_REVISION_LENGTH bytes including '\0' terminator. */

Source from the content-addressed store, hash-verified

44 * The returned string is guaranteed to be at most NETWORK_REVISION_LENGTH bytes including '\0' terminator.
45 */
46std::string_view GetNetworkRevisionString()
47{
48 static std::string network_revision;
49
50 if (network_revision.empty()) {
51#if not defined(NETWORK_INTERNAL_H)
52# error("network_internal.h must be included, otherwise the debug related preprocessor tokens won't be picked up correctly.")
53#elif not defined(ENABLE_NETWORK_SYNC_EVERY_FRAME)
54 /* Just a standard build. */
55 network_revision = _openttd_revision;
56#elif defined(NETWORK_SEND_DOUBLE_SEED)
57 /* Build for debugging that sends both parts of the seeds and by doing that practically syncs every frame. */
58 network_revision = fmt::format("dbg_seed-{}", _openttd_revision);
59#else
60 /* Build for debugging that sends the first part of the seed every frame, practically syncing every frame. */
61 network_revision = fmt::format("dbg_sync-{}", _openttd_revision);
62#endif
63 if (_openttd_revision_tagged) {
64 /* Tagged; do not mangle further, though ensure it's not too long. */
65 if (network_revision.size() >= NETWORK_REVISION_LENGTH) network_revision.resize(NETWORK_REVISION_LENGTH - 1);
66 } else {
67 /* Not tagged; add the githash suffix while ensuring the string does not become too long. */
68 assert(_openttd_revision_modified < 3);
69 std::string githash_suffix = fmt::format("-{}{}", "gum"[_openttd_revision_modified], _openttd_revision_hash);
70 if (githash_suffix.size() > GITHASH_SUFFIX_LEN) githash_suffix.resize(GITHASH_SUFFIX_LEN);
71
72 /* Where did the hash start in the original string? Overwrite from that position, unless that would create a too long string. */
73 size_t hash_end = network_revision.find_last_of('-');
74 if (hash_end == std::string::npos) hash_end = network_revision.size();
75 if (hash_end + githash_suffix.size() >= NETWORK_REVISION_LENGTH) hash_end = NETWORK_REVISION_LENGTH - githash_suffix.size() - 1;
76
77 /* Replace the git hash in revision string. */
78 network_revision.replace(hash_end, std::string::npos, githash_suffix);
79 }
80 assert(network_revision.size() < NETWORK_REVISION_LENGTH); // size does not include terminator, constant does, hence strictly less than
81 Debug(net, 3, "Network revision name: {}", network_revision);
82 }
83
84 return network_revision;
85}
86
87/**
88 * Extract the git hash from the revision string.

Callers 6

SendWelcomeMethod · 0.85
SendJoinMethod · 0.85
NetworkHTTPInitializeFunction · 0.85
HttpThreadFunction · 0.85

Calls 4

resizeMethod · 0.80
formatFunction · 0.50
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected