Our MMORPG gets a version, set once in CMake for the C++ client and all the servers: the client shows it in the corner of its login screen, the servers print it as they start, and Prometheus gets it as a build info metric. The login server checks each client's version and turns away one that doesn't speak its protocol, saying which version to get. A Python script lists the C++ libraries FetchContent pins that have newer versions, moves them on and pins each by its SHA-256, and when raylib 6.2 breaks our tests with a double free, we see why updates go through CI. Dependabot and a GitHub Actions workflow open pull requests with the newest versions each week, and a changelog says what each version changed. This is episode 59 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep059/end.
- 2:28
ep059/step-1One versionBrowseChanges - 6:30
ep059/step-2Turning other versions awayBrowseChanges - 12:26
ep059/step-3Libraries, pinnedBrowseChanges - 15:37
ep059/step-4Libraries, pinnedBrowseChanges - 18:48
ep059/step-5When an update breaksBrowseChanges - 20:39
ep059/step-6Once a weekBrowseChanges - 23:58
ep059/step-7The changelogBrowseChanges
Transcript
Intro
0:00 Welcome back to episode fifty-nine, where we're going to give the game a version, which the client shows and the login server checks, and keep the libraries it's built on up to date. First, a quick look at what we'll have by the end.
0:13 A client built before today gets the worlds from the servers at the end of the episode, and logs in, and the login server turns it away. The client is too old for the servers, which are version zero point fifty-nine point zero, and the message says to get the new one.
0:29 The series has a website, where the code is linked on GitHub, as open source, and each episode gets a page, with the code at each of its steps.
Last time
0:38 A quick reminder of where we left off, first. Accounts got characters of their own last time, each a row in MySQL with an id and a name, up to four in each world. The login server answers a login with a session, which stands in for the password from then on. The client logs in first, then shows the worlds, then our characters in the world we pick, each drawn in 3D, and turned with a drag.
Today
1:02 Here's the client's login screen as last episode left it. Nothing on it says which version of the game this is, so a player reporting a bug can't tell us which code they were running.
1:14 Last episode changed how logging in works, so here's the client from the episode before it, against the servers as last episode left them. It asks the login server for the worlds first, the way it did then, and that still works. The login server takes its name and password, and answers with a session, a field this client's code doesn't have. The token it reads from the reply is empty, so it connects to the world without one, and the world closes the connection. All the player is told is that the connection was lost, and nothing says their client is too old.
1:48 So today is about two things most projects pick up once people other than their developers use them. The game gets a version, one for the client and all the servers, which the client shows and the login and world servers print as they start. The login server checks the client's version, and turns away one that doesn't speak its protocol, with a message that says which version to get. Then the libraries: a script lists each library's newest version, moves the ones that are behind to it, and pins each by a hash of what it downloads. And once a week, GitHub opens pull requests with the newest versions, for CI to check. Right, let's get started with the version, which lives in CMake.
One version
2:29 The top CMake file has given the project a version since episode one, zero point one point zero, and apart from the API docs' header, nothing has read it. It's the natural place for ours, as the client and all the servers are built from this file, and they speak one protocol. A version is three numbers, major, minor and patch, which is semantic versioning. While the major version is zero, semantic versioning keeps it for software still taking shape, where anything can change at any time. So each episode's code becomes a minor version, which may change the protocol, and today's is zero point fifty-nine point zero. A patch is for fixing a minor version without changing what goes between the client and the servers. The code needs the version as a constant, so CMake is going to write it into a header for us, from a template, as it configures the build. The template is a header like any other, with its guard, and string view, the standard library's read-only view of a string that's kept somewhere else. The constant is k version, a string view, constexpr so it's known as the code compiles, and inline, so there's one of it however many files include the header. Project version, between at signs, is what CMake replaces with the version from the project line. Over it, we say where it comes from: the project line in the top CMake file, which CMake writes in here as it configures the build. In the protocol's CMake file, configure file reads the template and writes the header into the build folder, next to the protocol that protobuf generates, which the protocol library already has on its include path. Its last option tells it to replace the names between at signs, and to leave anything with a dollar sign, which looks like a CMake variable, as it is. Over it goes a line on what it is: the version, from project, as a header next to the generated protocol.
4:19 A player who reports a bug needs to say which version they're playing, so the client is going to show it. In the login screen, we include our version header, and set a margin between the window's corner and the text, which we note is in pixels. At the top of build, which the client calls each frame while we're logging in, the text is nano MMO and the version. Over it, we write where it goes and why: in the corner, as most other games show it, so a player can tell which one they have. The main viewport is the window, and we measure the text, to put it at the window's bottom right, inside the margin. It goes on ImGui's background draw list, which is drawn behind the windows, so the text needs no window of its own, and it's in the colour ImGui gives disabled text, a grey that stays out of the way. In the client's main, we include the version header, and the version is the first line the client prints, before anything has a chance to fail. The login server's main includes it too, and prints its version first, before it loads its certificate or reaches MySQL. And the world server prints its own as its main starts. Their logs end up in Loki, so the first line of each run says which code wrote what follows. The world server's metrics get the version too, so we include string, and our version header, and in the constructor's body, we register a gauge, nanommo build info, the way Prometheus reports its own version. The version goes in a label, as a string, which is what a label's value has to be, and the gauge's value is one. Over it, we say why it's a label: a query can tell the worlds a deploy has reached from the others, as a deploy hands the worlds over one at a time.
5:56 We build the client at this step and run it. Its version is in the bottom right corner of the login screen, zero point fifty-nine point zero, in grey.
6:07 And the servers, which run in Docker Compose at this step, from the Dockerfile's images. The login server and the world server each print their version as they start, which their logs show first. Then curl fetches the world server's metrics, from its metrics port, and grep keeps nanommo build info, its help line and its type, with the version in its label, and a value of one.
Turning other versions away
6:31 Now for the rule on which clients the login server lets in. In the header's template, under the version, we declare compatible, which takes the client's version and the servers', as string views. A client can play on servers whose major and minor versions match its own, as a patch fixes what's there and leaves the protocol alone. Anything that isn't three numbers with dots between them can't play at all, and we write that rule over it, for whoever calls it. Then a new source file, version, which includes its header first. From the standard library it takes array, optional, string view and vector, and from Abseil, numbers, for simple a to i, and str split. In an unnamed namespace, so it's private to this file, parse turns major dot minor dot patch into an array of three numbers. An optional holds a value or nothing, and parse returns nothing for what isn't a version. Str split cuts the version at its dots, into views of the original string, without copying it, and anything other than three parts isn't a version. Simple a to i reads a part as a number, and says whether it could. So a letter, or a part left empty, makes it nothing, and so does a number below zero. Compatible parses both versions, the client's into a and the servers' into b, so each is an optional array of three numbers, or nothing. It's true when both are versions, with a matching major and minor, while the patch, the third number, can differ.
8:01 Now, a quick test that the rule lets in the versions it should and turns away the rest, in version test, which includes our header and GoogleTest. A client and servers of one version can play, and so can two patches of a minor version, either way round, as a patch on the servers or the client changes nothing on the wire. Each expectation is a call to compatible, with the client's version first. An older minor version, a newer one, and another major version can't play, whichever side is newer, as the protocol may have changed either way. Neither can an empty version, which is what a client from before versions sends, nor one without its patch, nor one with a letter in it. The last test is for our own version, and over it, we note why it's there. With zero point sixty in the CMake file, without its patch, the login server would turn each client away, its own too. That's an easy slip to make, as CMake takes zero point sixty as a version as well, and nothing would complain until the first player logged in. So the test checks that our own version is compatible with our own version, and it would fail first. In the protocol's CMake file, version goes into the protocol library, and its test into the protocol's tests.
9:14 We build the tests at this step in Docker, and run the version tests, with a filter that picks them out of the protocol's tests. Grep keeps the lines of the tests that pass, and the summary, and all four pass.
9:27 Next, the login, and in our proto file, the login request gets a twelfth field, the client's version, as a string. A client from before versions sends none, and protobuf reads a field that isn't there as its default, an empty string, which is what the server gets from it. Over the field, we write what it's for, and that a client from before versions sends none. A field's number is what goes on the wire, so it takes the next one, and a program that doesn't know a field skips it. In the login screen, a request to log in, or to make an account, now carries the client's version. The bots log in through the login server too, so theirs carries it as well, from the version header. Without it, a deploy of this step would turn away our own load test. In the login server, we include str cat and the version header. A request without a session is a login or a new account, and before its password is checked, its version has to be compatible with the login server's. Over the check, we say why it's there: a client of another version would log in, then fail to make sense of what the world sends it, so it's told which version to get instead. An empty version means a client from before versions, so the error says it's too old for the servers, and gives their version, which str cat joins into the message, and says to get the new one. Otherwise, the error names both versions, and the one to get. Either way, the reply carries the error and no session, so the client goes back to its login and shows it. The requests with a session come after a login that passed this check, so they skip it.
10:58 Then a test from the login server's side, which includes string view, for the helper's new parameter, str cat, to build the errors it expects, and the version header. The helper that logs in gets a third argument, so its arguments go one to a line, and the third is a version, our own by default. The helper puts it in the request, so the tests we had log in as today's client. The first new test makes an account with our version, which has to work, so we see the login that passes next to the one that doesn't. Then it logs in to that account as version zero point one point zero, with the right password. The reply isn't ok, it has no session, and its error names zero point one point zero, the servers' version, and the version to get. The second asks for a new account with an empty version, as a client from before versions would, and gets the error that says it's too old. Both tests check the whole message, as the client shows it word for word, so a change to its wording shows up here first.
11:56 We run the login server's tests like that, against MySQL and Redis, which Compose starts next to them. The filter picks the login tests, and grep keeps the ones that pass, and the two new ones pass, along with the six we had.
12:11 Now, the client from episode fifty-seven again, against the servers at this step. The worlds come back as they did before, and this time the login is refused, with the error that the client is too old for the servers, which are zero point fifty-nine point zero, and to get the new one.
Libraries, pinned
12:27 The other half of today is the libraries the game is built on. The top CMake file fetches thirteen of them from GitHub with FetchContent, each pinned to a release, a tag or a commit, so that a build doesn't change from one day to the next. Pinning also leaves us on whatever was newest when each was added, without the fixes that came out since, security fixes too. Dependabot, which we'll set up later on, can't read FetchContent, so we write a small Python script to check them. It starts with a shebang, so it runs as a program, and a docstring that says what it does. It uses nothing outside Python's standard library, so it runs on CI's machines as they are: arg parse for its options, json for GitHub's answers, os for the environment, re for regular expressions, and url lib to make the requests. A regular expression finds each FetchContent declare, with its name, and a URL on GitHub, with the owner and the repository. Most of the URLs are an archive GitHub makes of a tag, or of a commit, and the others are a file a project attached to one of its tagged downloads, with the tag in its path. Over it, we note both kinds, archives and release files. A commit is forty hexadecimal digits, and a tag that's a version is letters, then numbers between dots or dashes, such as googletest's, abseil's dated ones, or asio's, which has dashes. A release candidate, say a tag ending in r c one, doesn't match. GitHub, the function, asks GitHub's REST API for a path, and sends a token when the environment has one, as CI will, since GitHub answers sixty requests an hour without one, and far more with one. Then it opens the URL, and reads the answer as JSON. Numbers pulls the numbers out of a version, so comparing the lists compares the versions: Dear ImGui's one point ninety-three comes after one point ninety-two point nine. Newest finds a library's newest version, in the form it's pinned in. For a commit, it's the newest commit on the default branch. Otherwise, it's the latest release, which leaves out drafts and pre-releases. A library without releases gets a four oh four there, so we look at its tags instead, and take the largest one that's a version. A commit is newer when it's another commit, and a version when its numbers are larger. Short cuts a commit to its first seven characters for printing, as git does. Main starts with arg parse, which gives the script a dash dash help, from the docstring, and refuses options it doesn't know. Then it reads the CMake file, and for each declare it finds, asks for the newest version, and whether that's newer than what's pinned. It prints a line for each: the library's name, what it's pinned to, and the newest, when that's newer, or newest when it's at its newest already. At the bottom, main runs when the file runs as a script, and not when another file imports it.
15:15 We run it from the repository's root, where the CMake file is. Five of the thirteen are behind: abseil by more than a year, raylib, Dear ImGui, hiredis, and httplib by seven minor versions. Protobuf's newest release is the one we have, as its version thirty-seven is still a release candidate, and asio, which has tags and no releases, is at its newest tag.
15:38 Listing them is half the job, and with dash dash update, the script moves each library to its newest version, and pins it by a hash of what it downloads. We add that to the docstring, which dash dash help prints. Hash lib is Python's module for hashes such as SHA two five six. The regular expression keeps what comes before the URL as the head, along with the indent before URL, and the URL hash after it, when there is one. SHA two five six downloads a URL and hashes what's there. It reads the whole archive into memory, a few megabytes at most, and returns the hash in hexadecimal, the way CMake writes it. Main keeps its parser in a variable now, to add dash dash update, a flag that's true when it's given, as store true says, and update holds whether it was. Check runs for each declare, with what main did for each before. It finds the pinned version and the newest, works out whether that's newer, and prints its line. Without dash dash update, or when the library is at its newest and pinned by its hash already, it leaves the declare as it was. Otherwise the URL takes the newest version in place of the pinned one, and under it, at the URL's indent, comes URL hash, with the SHA two five six of what the URL holds now. A tag on GitHub can be moved to other code, and a release's file can be swapped. CMake checks each download against its hash, so either would stop the build with a hash mismatch, instead of building code we didn't check, and that's what we write over it. Re's sub runs check on each match, and joins what it returns into the new text, which dash dash update writes back over the CMake file.
17:17 We run it with dash dash update, which takes a few seconds longer, as the script downloads each library to hash it. Git's diff stat says the CMake file is all that changed.
17:29 In the CMake file, abseil's URL has its new version, its release of August, and under it, its hash. GoogleTest stays at one point eighteen, pinned by its hash. Raylib moves on to six point two, Dear ImGui to one point ninety-three, and rlImGui, which joins the two, stays on its commit. Protobuf, asio, Tracy and prometheus-cpp keep their versions, and hiredis, the Redis client, moves on to one point five. Then nlohmann's json stays, httplib moves on to zero point sixty-four, and doxygen awesome, which styles the API docs, stays. Each declare has a URL hash now, like abseil's, so the next build downloads what we checked, or stops.
When an update breaks
18:14 So, let's check that the game still works with the new versions. We build the game at this step in Docker, and run all its tests, as CI's test stage does. It takes a while, as abseil and raylib compile from their new sources, and protobuf compiles again against the new abseil. It builds, and ten of the client's tests abort, out of two hundred and eleven: the ones that load a model with bones, such as the skeleton and the players' figures. The C library says why, as it stops each one: a double free, or memory it hands out got corrupted.
18:49 In episode twenty-eight, LeakSanitizer found that raylib's unload model left a model's pose and bone matrices behind, so we wrote unload with bones, which frees them after it. Raylib six point two's unload model frees them, so we were freeing them a second time. That's a double free, and the C library aborts the program, as freeing memory twice can corrupt what it hands out next. A fix in a library can break the workaround we wrote for its bug, and the game still builds, then fails as it runs, so an update goes through the tests first. Raylib six point two also made the bone counts unsigned, as a count can't be below zero. Our loops count bones with an int, which also stands for no bone, as minus one. Comparing a signed int with an unsigned count turns the int into an unsigned one, so minus one would compare as a huge number. So where we read the bind pose, the loop casts the count to an int, with static cast. Then unload with bones goes, along with the line on why we had it. A model without the clips we need is unloaded with unload model now, and the loop that finds the right hand and the feet casts its count too. The destructor unloads each character with unload model, as it unloads the weapons. In the figures' test, its bone named casts the count, and its model is unloaded with unload model alone. So does bone named in figures, which finds a bone of Mixamo's rig by its name. In moves, the loop that finds the joints a move turns casts it, And in the soundscape, the count is cast once, into bones, and the footfalls check the feet against it before they read the clip.
20:26 We build and run the tests again in Docker, at this step, with the Mixamo characters in the build, as before, so each test that loads a model runs. All two hundred and eleven pass with the new libraries.
Once a week
20:39 Running the script by hand works for as long as somebody remembers to. Dependabot is GitHub's bot for this: it reads a repository's dependencies, and opens a pull request when there's a newer version, which CI checks like any other. Its settings go in Dependabot's config file, in the dot github folder, and version two is the file's format. Over it, we write what it covers: the actions, the images, and the website's and the storyteller's packages. Each kind gets a pull request of its minor and patch updates together, and one for each major update. And the C++ libraries are the script's, as FetchContent isn't something Dependabot can read. First come the actions our CI workflows use, such as checkout and cache, looked at once a week. A group puts updates together in one pull request, here the minor and patch updates of all the actions. A major update is the kind that can break things, so each one gets a pull request of its own, which passes or fails on its own. Then the images our Dockerfiles start from, in the four folders that have one: the game's, the website's, the storyteller's and the runner's, which directories lists, with a group of their own. Then the images the compose files run, such as MySQL, Redis and Grafana, which Dependabot finds in each compose file at the root, grouped as the services. Then the website's PHP packages, which composer keeps in its lock file, so the pull request changes the versions there, grouped as the website. And last, the storyteller's Python packages, from the requirements file its Dockerfile installs, grouped like the others. Weekly means Monday, unless we say otherwise, so the pull requests are there at the start of the week. For those, a workflow of our own, dependencies, runs the script once a week, and over its triggers, we write what it does, and where. Its name is what the Actions tab lists it by, and on is what starts it. The schedule is cron's, at twenty-three past five on Mondays, in UTC, away from the top of the hour, when GitHub's runners are busiest. Workflow dispatch lets us start it by hand too, from the Actions tab or the GitHub CLI. It runs in the public repository and nowhere else, as the updates that pass go into the game in an episode, and each fork would open pull requests of its own. Its token gets the permissions it needs, to push a branch, to open a pull request, and to start a workflow. The job's environment hands the token to the script and to the GitHub CLI, and names the branch and the title. After checking out the code, it runs the script with dash dash update, and tee keeps what it prints in a file as well. When nothing changed, each library is at its newest, and the job ends there. Otherwise, it commits the change to a branch, dependencies slash cmake, as the GitHub Actions bot, and force pushes it, so next week's run replaces this week's. The GitHub CLI opens a pull request, with the script's lines in its description, or brings the open one up to date. Last, it starts CI on the branch by hand, with the GitHub CLI's workflow run. A push or a pull request made with the workflow's own token starts no other workflow, so that workflows can't start each other forever. A run started by hand is the exception, and that's how CI checks the update, which we note over it.
The changelog
23:59 Last, a change log, as players want to know what changed, and so does whoever runs the servers, before they deploy. It lists each version's changes, the newest first, and opens with what a version number means here. While the major version is zero, each episode's code is a minor version, and the login server lets in a client of its own major and minor version. Then today's, zero point fifty-nine point zero, with its date. It has the version in the corner, the logs and the metrics, the login server turning other versions away, the script, the libraries it moved on, raylib's unload, and the pull requests each week. A release adds its version here as it changes the one in the CMake file, and gets a git tag, v and the version, so the code of any version is easy to find. A player who reads zero point fifty-nine point zero in the corner can look it up here, and see what their client has.
Wrap-up
24:55 So, today, the game got a version, set in the CMake file, which the client shows in its corner, the login and world servers print as they start, and the world's metrics carry. The login server checks each client's version, and turns away one of another major or minor version, with a message that says which to get. A script lists the newest version of each C++ library, and moves the ones that are behind on to it, each pinned by its hash, and the new raylib meant dropping a workaround we no longer need. And once a week, Dependabot and our own workflow open pull requests with the newest versions, for CI to check.
Playing it
26:25 If there's something you'd like to see in a later episode, let me know in the comments, and if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.