mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
The engine sets an addon entry's loaded byte at the START of its load, before dependencies and before the addon's own files run, clearing it again only if a required dependency fails. That byte therefore means "load has begun" -- it is loadedOrLoading, not loaded. IsAddOnLoaded returned it for both values, so an addon checking itself at file scope got (true, true) where it should get (true, false). The old comment claimed the in-flight window was unobservable from Lua; it is plainly observable, since the addon's own files and every dependency pulled in during them execute inside it. Add AddOns::LoadState, which co-hooks the per-addon loader and keeps a stack of the names currently loading -- a stack because dependency loads nest, and the name is copied because LoadAddOn passes a transient Lua string. loadedOrLoading stays the engine byte; loaded now also requires the addon not to be mid-load. Known gap, documented: an addon asking about itself from its own ADDON_LOADED handler still reads (true, false), as that event fires just before the load call returns. Also mirror the stock LoadAddOn global into C_AddOns. It already returns the modern (loaded, value) shape -- 1/nil plus a locale-independent reason token -- so it registers as the engine's own handler.
98 lines
3.1 KiB
C++
98 lines
3.1 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/>.
|
|
|
|
// Tracks in-progress addon loads so `C_AddOns.IsAddOnLoaded` can report
|
|
// `loadedOrLoading` and `loaded` separately. See `LoadState.h` for why the
|
|
// engine's own byte can't answer both.
|
|
//
|
|
// Loads nest — an addon's dependencies load inside its own call — so this
|
|
// keeps a stack rather than a single name. The name is copied rather than
|
|
// stored by pointer: `LoadAddOn` passes a Lua string, which does not
|
|
// outlive the call.
|
|
|
|
#include "addons/LoadState.h"
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace AddOns::LoadState {
|
|
|
|
namespace {
|
|
|
|
// Dependency chains are shallow; anything deeper simply isn't tracked
|
|
// (the depth counter still balances, so the stack can't desync).
|
|
constexpr int kMaxDepth = 16;
|
|
constexpr int kMaxName = 64;
|
|
|
|
char g_stack[kMaxDepth][kMaxName];
|
|
int g_depth = 0;
|
|
|
|
bool EqualsIgnoreCase(const char *a, const char *b) {
|
|
for (; *a != '\0' && *b != '\0'; ++a, ++b) {
|
|
char ca = *a;
|
|
char cb = *b;
|
|
if (ca >= 'A' && ca <= 'Z')
|
|
ca = static_cast<char>(ca - 'A' + 'a');
|
|
if (cb >= 'A' && cb <= 'Z')
|
|
cb = static_cast<char>(cb - 'A' + 'a');
|
|
if (ca != cb)
|
|
return false;
|
|
}
|
|
return *a == *b;
|
|
}
|
|
|
|
using LoadOne_t = unsigned int(__fastcall *)(const char *name, char flag,
|
|
void *progressCtx);
|
|
LoadOne_t s_loadOne_o = nullptr;
|
|
|
|
unsigned int __fastcall LoadOne_h(const char *name, char flag, void *progressCtx) {
|
|
const int slot = g_depth;
|
|
if (slot >= 0 && slot < kMaxDepth) {
|
|
if (name != nullptr) {
|
|
std::strncpy(g_stack[slot], name, kMaxName - 1);
|
|
g_stack[slot][kMaxName - 1] = '\0';
|
|
} else {
|
|
g_stack[slot][0] = '\0';
|
|
}
|
|
}
|
|
++g_depth;
|
|
|
|
const unsigned int result = s_loadOne_o(name, flag, progressCtx);
|
|
|
|
--g_depth;
|
|
if (g_depth < 0)
|
|
g_depth = 0; // defensive: never let the stack run negative
|
|
return result;
|
|
}
|
|
|
|
const Game::HookAutoRegister _hookLoadOne{
|
|
Offsets::FUN_ADDON_LOAD_ONE,
|
|
reinterpret_cast<void *>(&LoadOne_h),
|
|
reinterpret_cast<void **>(&s_loadOne_o)};
|
|
|
|
} // namespace
|
|
|
|
bool IsLoading(const char *name) {
|
|
if (name == nullptr || *name == '\0')
|
|
return false;
|
|
const int depth = (g_depth < kMaxDepth) ? g_depth : kMaxDepth;
|
|
for (int i = 0; i < depth; ++i)
|
|
if (EqualsIgnoreCase(g_stack[i], name))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
} // namespace AddOns::LoadState
|