Files
ClassicAPI/src/config/CommandLine.cpp
T
Brues 8637bf05e5 feat(config): add -gluescript / -gamescript launch switches
Four switches that run Lua as the client starts:

  -gluescript      <code>   run at the glue screens
  -gluescriptFile  <path>   the file's contents, run at the glue screens
  -gamescript      <code>   run once in-game
  -gamescriptFile  <path>   the file's contents, run once in-game

None of these exist in 1.12. The strings appear nowhere in the binary, so
there is nothing to parse them and nothing to un-gate -- the feature is
ours end to end.

Registration time is too early for either state. The in-game module
callback runs from FUN_LOAD_SCRIPT_FUNCTIONS, before FrameXML loads, and
the glue callback runs from FUN_LOAD_GLUE_SCRIPT_FUNCTIONS, before
GlueXML does. A script running there could not touch a frame, call a
FrameXML function or see an addon, which is most of what one would be
written for. So each is armed at registration and run from a tick, where
the UI is demonstrably up. Glue uses FrameTick, whose hook is the UI
render root and so fires at the login and character-select screens; it is
re-armed by the glue module callback on every glue boot, so a script that
logs in automatically still works after a logout. Game uses WorldTick,
which is world-only, so its first tick is past FrameXML, past the addon
load pass and past enter-world init; it latches for the life of the
process, since "run once in-game" says once.

FUN_LUA_RUN_STRING resolves the Lua state itself rather than taking one,
so a single call site reaches whichever state is current and the two
paths differ only in when they fire. It installs the FrameScript error
handler as the pcall's errfunc, so a faulty script reports itself the way
any script error does instead of failing silently, and because every
chunk goes through the shared compile chokepoint these scripts get the
same 5.1 syntax support the rest of the client's Lua has.

A *File is read with Win32 rather than the engine's file layer. The
engine resolves relative paths against a loose-file index built once at
boot, so it cannot see a file created afterwards and cannot open an
absolute path at all -- and an absolute path is what goes in a shortcut.
Reading it directly also lets both switches share one execution path, so
inline code and file contents cannot diverge.

The switch parser moves to Config::CommandLine, shared with -config
rather than copied: the quoting and token-boundary rules are exactly
where a second copy would drift unnoticed. Lifting it also fixed a latent
overread, where a switch name at the very end of the command line was
compared past its terminator.

Verified in-game on Turtle: inline and file forms in both states; the
in-game script silent across /reload and a second login while the glue
script re-ran on returning to character select; a syntax error and a
missing file each reported without a crash; and -config still applying
through the shared parser.
2026-09-06 09:53:58 -05:00

124 lines
3.6 KiB
C++

// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
#include "CommandLine.h"
#include <windows.h>
namespace Config::CommandLine {
namespace {
bool IsSpace(char c) { return c == ' ' || c == '\t'; }
char Lower(char c) { return (c >= 'A' && c <= 'Z') ? static_cast<char>(c - 'A' + 'a') : c; }
// Stops at `a`'s terminator rather than reading `len` bytes unconditionally, so
// a switch name at the very end of the command line cannot read past it.
bool EqualsIgnoreCase(const char *a, const char *b, size_t len) {
for (size_t i = 0; i < len; ++i) {
if (a[i] == '\0' || Lower(a[i]) != Lower(b[i]))
return false;
}
return true;
}
size_t Length(const char *s) {
size_t n = 0;
while (s[n] != '\0')
++n;
return n;
}
// Start of the value token for `-name`, or null when the switch is absent.
const char *FindValue(const char *name) {
const char *cmdLine = ::GetCommandLineA();
if (cmdLine == nullptr || name == nullptr)
return nullptr;
const size_t nameLen = Length(name);
if (nameLen == 0)
return nullptr;
for (const char *p = cmdLine; *p != '\0'; ++p) {
if (*p != '-' && *p != '/')
continue;
// Only at a token boundary, so a path containing the name cannot match.
if (p != cmdLine && !IsSpace(p[-1]) && p[-1] != '"')
continue;
if (!EqualsIgnoreCase(p + 1, name, nameLen))
continue;
const char *after = p + 1 + nameLen;
if (*after == '=') {
++after;
} else if (IsSpace(*after)) {
while (IsSpace(*after))
++after;
} else {
continue; // a longer switch that merely starts with `name`
}
return after;
}
return nullptr;
}
// Walks the token at `p`, honoring double quotes so a value containing spaces
// arrives whole. `emit` takes each character and returns whether it was kept;
// the walk stops as a failure the moment one is not.
template <typename Emit> bool ReadToken(const char *p, Emit emit) {
const bool quoted = (*p == '"');
if (quoted)
++p;
size_t n = 0;
while (*p != '\0' && (quoted ? (*p != '"') : !IsSpace(*p))) {
if (!emit(*p++))
return false;
++n;
}
return n > 0;
}
} // namespace
bool Value(const char *name, char *out, size_t outSize) {
if (out == nullptr || outSize == 0)
return false;
out[0] = '\0';
const char *p = FindValue(name);
if (p == nullptr)
return false;
size_t n = 0;
const bool ok = ReadToken(p, [&](char c) {
if (n + 1 >= outSize)
return false;
out[n++] = c;
return true;
});
out[n] = '\0';
return ok;
}
bool Value(const char *name, std::string &out) {
out.clear();
const char *p = FindValue(name);
if (p == nullptr)
return false;
return ReadToken(p, [&](char c) {
out.push_back(c);
return true;
});
}
} // namespace Config::CommandLine