* 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. */
| 44 | * The returned string is guaranteed to be at most NETWORK_REVISION_LENGTH bytes including '\0' terminator. |
| 45 | */ |
| 46 | std::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. |
no test coverage detected