mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
The repo was internally inconsistent: the LICENSE file is GPL v3 but every src/ header carried the LGPL v3 notice and the README said "LGPL v3 or later". LGPL was also the wrong fit — this is an injected mod plus a bundled addon, not a library other code links against, so LGPL's linking exception buys nothing. It's also required: src/voicechat/Tts.cpp is ported from awesome_wotlk's VoiceChat.cpp, which is GPL v3, and a derivative of GPL code cannot be relicensed under more permissive terms. Dependencies stay compatible — MinHook (BSD-2) and the nampower-mirrored packet shapes (BSD) are GPL-compatible, and SAPI is covered by the system-library exception. Swept "GNU Lesser General Public License" -> "GNU General Public License" across all 257 src/ headers and changed the README license line to match the GPL v3 LICENSE file. Comment-only; no build impact.
102 lines
3.8 KiB
C++
102 lines
3.8 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 "cvar/Factory.h"
|
|
|
|
#include "Offsets.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
namespace CVar::Factory {
|
|
|
|
namespace {
|
|
|
|
// `FUN_REGISTER_CVAR` — verified ABI (see Offsets.h and the calibration
|
|
// against the engine's own call site at 0x005E2490 / the Lua
|
|
// Script_RegisterCVar path). MSVC `__fastcall` places the first two args in
|
|
// ECX/EDX and pushes the rest right-to-left, which reproduces the engine's
|
|
// exact push order: flags, defaultValue, callback, categoryId, hiddenBool,
|
|
// userData (with flags as the first stack arg / last push).
|
|
using RegisterCVar_t = void *(__fastcall *)(const char *name, int edxZero,
|
|
uint32_t flags,
|
|
const char *defaultValue,
|
|
ChangeCallback callback,
|
|
int categoryId, int hiddenBool,
|
|
void *userData);
|
|
|
|
using FindCVar_t = void *(__fastcall *)(const char *name);
|
|
|
|
// `FUN_SET_CVAR_VALUE` — `__thiscall(cvar /*ecx*/, value, a3, a4, a5, a6)`,
|
|
// i.e. ECX=cvar with `value` AND a3..a6 all passed on the stack (5 stack
|
|
// args; the function cleans 20 bytes / `RET 0x14`). NOT __fastcall — that
|
|
// would route `value` through EDX, push only 4 args, and leave the callee
|
|
// popping 4 bytes too many → ESP corruption → silent crash. Mirror the
|
|
// Script_SetCVar invocation (0x00488CA8): a3..a6 = 1, 0, 0, 1.
|
|
using SetCVarValue_t = char(__thiscall *)(void *cvar, const char *value,
|
|
int a3, int a4, int a5, int a6);
|
|
|
|
const char *ReadValue(void *cvar) {
|
|
if (cvar == nullptr)
|
|
return nullptr;
|
|
return *reinterpret_cast<const char *const *>(
|
|
static_cast<const uint8_t *>(cvar) + Offsets::OFF_CVAR_VALUE_STR);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Handle Register(const char *name, const char *defaultValue, uint32_t flags,
|
|
ChangeCallback callback, void *userData) {
|
|
if (name == nullptr)
|
|
return nullptr;
|
|
auto reg = reinterpret_cast<RegisterCVar_t>(Offsets::FUN_REGISTER_CVAR);
|
|
return reg(name, /*edxZero*/ 0, flags, defaultValue, callback,
|
|
Offsets::CVAR_SCRIPT_CATEGORY, /*hiddenBool*/ 0, userData);
|
|
}
|
|
|
|
Handle Find(const char *name) {
|
|
if (name == nullptr)
|
|
return nullptr;
|
|
auto find = reinterpret_cast<FindCVar_t>(Offsets::FUN_FIND_CVAR);
|
|
return find(name);
|
|
}
|
|
|
|
const char *GetString(Handle cvar) {
|
|
return ReadValue(cvar);
|
|
}
|
|
|
|
int GetInt(Handle cvar, int fallback) {
|
|
const char *value = ReadValue(cvar);
|
|
if (value == nullptr || value[0] == '\0')
|
|
return fallback;
|
|
return std::atoi(value);
|
|
}
|
|
|
|
void SetString(Handle cvar, const char *value) {
|
|
if (cvar == nullptr || value == nullptr)
|
|
return;
|
|
auto set = reinterpret_cast<SetCVarValue_t>(Offsets::FUN_SET_CVAR_VALUE);
|
|
set(cvar, value, 1, 0, 0, 1);
|
|
}
|
|
|
|
void SetInt(Handle cvar, int value) {
|
|
char buf[16];
|
|
// Hand-format to avoid pulling in a locale-aware path; value range for
|
|
// our cvars is small, 16 bytes is ample for a 32-bit int + sign + NUL.
|
|
std::snprintf(buf, sizeof buf, "%d", value);
|
|
SetString(cvar, buf);
|
|
}
|
|
|
|
} // namespace CVar::Factory
|