Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework server.ini parsing to not depend on the real console #1659

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

dgelessus
Copy link
Contributor

Reworks how server.ini files are parsed, based on my console parsing refactoring in #1459. Advantages of this reworked code include:

  • Visible error messages (instead of silent failure) when something in the server.ini is obviously invalid: unknown option names, invalid integers, incorrectly formatted base-64 keys.
  • The server.ini handling is now decoupled from the console engine - only the parser is shared. This means you can no longer put console commands into the server.ini or set server.ini options from the in-game console.
  • The parsed server settings are stored in a struct and not directly into global variables. This is useful groundwork for possible future projects that need to parse more than one server.ini, such as a shard selector.

Also includes some bonus refactoring of how the Diffie-Hellman keys are stored and passed around.

Example of the new error messages:

Windows error dialog saying: "Error in server.ini file. Please check your URU installation. Line 5: Invalid key: Invalid base-64 data (did you forget to put quotes around the value?)"

ST::string Parse(const plFileName& fileName);
void Apply();

static ST::string Load(const plFileName& fileName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a preference for returning a boolean status value and having the error string fetch being a separate call.

Copy link
Contributor Author

@dgelessus dgelessus Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a boolean return and an out parameter for the error message be okay too?

static bool Load(const plFileName& fileName, ST::string& errorMsg);

pfServerIni::Load is a static method and I would rather not introduce a new global "last error" variable just for this one call...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I was thinking that the error should be a class member that can be fetched with a getter, and the client code would do something like the body of Load(). I'm not a big fan of out parameters in C++ - I know there are some in the engine already, but they are a code smell IMO. I would consider throwing an exception acceptable as well. I generally don't care much for those either, but this isn't performance sensitive code, and it feels like a more natural way of receiving an error as opposed to an error message return.

Copy link
Contributor Author

@dgelessus dgelessus Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Genuine question: why exactly do you prefer a separate GetErrorMsg function/method? To me, it seems like it just has lots of disadvantages (forced shared state for no good reason, every class needs its own error variable/getter, it composes badly, and it's easy for callers to miss that there is an error message at all). All of the alternatives (error message in return value, as an out parameter, or an exception) have less of these issues and just seem better to me, even if they're a little awkward in other ways. I think the ideal solution would be std::expected, but that only exists since C++23...

Exceptions would probably work well enough though. I guess I should define a custom exception specifically for this API, similar to e. g. plPXCookingException?

Copy link
Member

@Hoikas Hoikas Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, returning an error message and testing its emptiness isn't a great way to communicate success. There are plenty of libraries out there that use error codes and have strings for "Success," and it's not uncommon to see amusing situations where users get "Error: Success". I know this isn't an error code, but who knows who will come behind us and not realize an empty string is success. I would prefer an explicit, easily understandable success condition. Out parameters do that, but are C style. This is C++, where we have more idiomatic ways of doing things. Those can be members with a getter, exceptions, tuples (and, with C++17, structured bindings to unpack the return value), or, as you mention, std::expected in C++23.

Edit: And, yeah, I would probably make a custom exception similar to plPXCookingException.

@dgelessus dgelessus force-pushed the server_ini_parsing branch 2 times, most recently from ad7788f to c54e52e Compare February 1, 2025 23:12
The commands under the Server group are only meant for server.ini files
and aren't usable anywhere else. Similarly, server.ini files aren't
supposed to call any other console commands. This is now enforced by not
handling the server settings as real commands, but with a new dedicated
server.ini parser that handles only server settings and nothing else.

This new parser intentionally doesn't support some obscure features of
the full console, such as spaces as command group/name separators (e. g.
"Server Status" instead of "Server.Status") and console variables. This
is strictly speaking a breaking change, but unlikely to cause issues -
these features were never documented and are unused in practice.
@dgelessus dgelessus force-pushed the server_ini_parsing branch 2 times, most recently from bd58629 to e411269 Compare February 22, 2025 14:55
Copy link
Member

@Hoikas Hoikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting really close 👍

Use the methods FromData_BE and FromData_LE instead, which make the
endianness explicit (or rather, more explicit than an unnamed bool
parameter with a default value).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants