Files
ClassicAPI/src/script/LoadFile.cpp
T
SkeewirtandBrues 5df5ce00cf fix(xml): resolve <Script file> subdirectory paths relative to the XML file (#80)
* fix(xml): resolve <Script file> subdirectory paths relative to the XML file

The XML file processor joins <Include file> onto the including file's
directory, but <Script file> only when the value has no backslash
(strchr at 0x006EE06E); a value with one is loaded from the game root.
So <Script file="widgets\AceGUIWidget-Button.lua"/> inside
Libs\AceGUI-3.0\AceGUI-3.0.xml fails with "Error loading
widgets\AceGUIWidget-Button.lua" - all 23 AceGUI-3.0 widget files in
Zygor Guides Viewer 2.0, and every other addon that embeds AceGUI-3.0.
Later clients resolve it relative to the XML file.

Co-hook the XML file processor (FUN_XML_LOAD_FILE) to track the file being
processed, and at the <Script> call into FUN_LUA_LOAD_FILE (return address
RET_XML_SCRIPT_FILE_LOAD) retry a backslash path that does not exist as
given against that file's directory, only when the joined file exists.
Root-relative paths that load on vanilla are unchanged. FUN_LUA_LOAD_FILE
is already co-hooked by AddOns::SavedVarsFirst, which now calls
Xml::ScriptPath::Resolve first.

Verified in-game (1.12.1 client via VanillaFixes, Zygor Guides Viewer 2.0):
a probe addon's <Script file="sub\File.lua"/> loads from its XML file's
folder, and Zygor's AceGUI-3.0 option pages, built from those widget
files, work.

* refactor(xml): share the Lua file-loader hook; collapse .. in Script paths

FUN_LUA_LOAD_FILE now has one owner, Script::LoadFile, which runs path
rewriters and then suppressors. Xml::ScriptPath is a rewriter and
AddOns::SavedVarsFirst a suppressor, so neither module reaches into the
other's hook.

Xml::ScriptPath joins onto the XML file's folder with each \..\
collapsed first, as FUN_XML_LOAD_FILE does before its own joins; the
tracked pointer is the caller's uncollapsed path, so an XML file reached
through <Include file="..\x.xml"/> joined onto a path with .. in it.

The user docs no longer name later clients or what 1.12 does.

---------

Co-authored-by: Brues <5278969+brues-code@users.noreply.github.com>
2026-10-17 15:52:58 -05:00

65 lines
2.2 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 "script/LoadFile.h"
#include "Game.h"
#include "Offsets.h"
#include <intrin.h> // _ReturnAddress
namespace Script::LoadFile {
namespace {
// Chained at static-init time by the subscriber constructors, before `DllMain`.
// Zero-initialized (namespace static) ahead of any dynamic init.
AutoRewrite *g_rewriters = nullptr;
AutoSuppress *g_suppressors = nullptr;
using LoadFile_t = uint32_t(__fastcall *)(const char *path, void *ctx, void *status);
LoadFile_t g_orig = nullptr;
// Longer than any path the engine builds (its buffers are 0x104).
constexpr size_t kPathBuf = 0x208;
uint32_t __fastcall Hook(const char *path, void *ctx, void *status) {
const uintptr_t retAddr = reinterpret_cast<uintptr_t>(_ReturnAddress());
char buf[kPathBuf];
for (AutoRewrite *r = g_rewriters; r != nullptr && path != nullptr; r = r->next)
path = r->fn(path, retAddr, buf, sizeof buf);
if (path != nullptr)
for (AutoSuppress *s = g_suppressors; s != nullptr; s = s->next)
if (s->fn(path))
return 0;
return g_orig(path, ctx, status);
}
const Game::HookAutoRegister _hook{
Offsets::FUN_LUA_LOAD_FILE, reinterpret_cast<void *>(&Hook),
reinterpret_cast<void **>(&g_orig)};
} // namespace
AutoRewrite::AutoRewrite(Rewriter f) : fn(f), next(g_rewriters) { g_rewriters = this; }
AutoSuppress::AutoSuppress(Suppressor f) : fn(f), next(g_suppressors) {
g_suppressors = this;
}
uint32_t Original(const char *path, void *ctx, void *status) {
return g_orig != nullptr ? g_orig(path, ctx, status) : 0;
}
} // namespace Script::LoadFile