2026-03-20 11:12:07 -07:00
|
|
|
#include "addons/lua_engine.hpp"
|
|
|
|
|
#include "game/game_handler.hpp"
|
feat: add core WoW Lua API functions for addon development
Add 13 WoW-compatible Lua API functions that addons can call:
Unit API: UnitName, UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax,
UnitLevel, UnitExists, UnitIsDead, UnitClass (supports "player",
"target", "focus", "pet" unit IDs)
Game API: GetMoney, IsInGroup, IsInRaid, GetPlayerMapPosition
Updated HelloWorld addon to demonstrate querying player state.
2026-03-20 11:17:15 -07:00
|
|
|
#include "game/entity.hpp"
|
2026-03-20 11:12:07 -07:00
|
|
|
#include "core/logger.hpp"
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include <lua.h>
|
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
#include <lualib.h>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace wowee::addons {
|
|
|
|
|
|
|
|
|
|
// Retrieve GameHandler pointer stored in Lua registry
|
|
|
|
|
static game::GameHandler* getGameHandler(lua_State* L) {
|
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "wowee_game_handler");
|
|
|
|
|
auto* gh = static_cast<game::GameHandler*>(lua_touserdata(L, -1));
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return gh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WoW-compatible print() — outputs to chat window instead of stdout
|
|
|
|
|
static int lua_wow_print(lua_State* L) {
|
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
|
std::string result;
|
|
|
|
|
for (int i = 1; i <= nargs; i++) {
|
|
|
|
|
if (i > 1) result += '\t';
|
|
|
|
|
// Lua 5.1: use lua_tostring (luaL_tolstring is 5.3+)
|
|
|
|
|
if (lua_isstring(L, i) || lua_isnumber(L, i)) {
|
|
|
|
|
const char* s = lua_tostring(L, i);
|
|
|
|
|
if (s) result += s;
|
|
|
|
|
} else if (lua_isboolean(L, i)) {
|
|
|
|
|
result += lua_toboolean(L, i) ? "true" : "false";
|
|
|
|
|
} else if (lua_isnil(L, i)) {
|
|
|
|
|
result += "nil";
|
|
|
|
|
} else {
|
|
|
|
|
result += lua_typename(L, lua_type(L, i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (gh) {
|
|
|
|
|
game::MessageChatData msg;
|
|
|
|
|
msg.type = game::ChatType::SYSTEM;
|
|
|
|
|
msg.language = game::ChatLanguage::UNIVERSAL;
|
|
|
|
|
msg.message = result;
|
|
|
|
|
gh->addLocalChatMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
LOG_INFO("[Lua] ", result);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WoW-compatible message() — same as print for now
|
|
|
|
|
static int lua_wow_message(lua_State* L) {
|
|
|
|
|
return lua_wow_print(L);
|
|
|
|
|
}
|
|
|
|
|
|
feat: add core WoW Lua API functions for addon development
Add 13 WoW-compatible Lua API functions that addons can call:
Unit API: UnitName, UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax,
UnitLevel, UnitExists, UnitIsDead, UnitClass (supports "player",
"target", "focus", "pet" unit IDs)
Game API: GetMoney, IsInGroup, IsInRaid, GetPlayerMapPosition
Updated HelloWorld addon to demonstrate querying player state.
2026-03-20 11:17:15 -07:00
|
|
|
// Helper: get player Unit from game handler
|
|
|
|
|
static game::Unit* getPlayerUnit(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (!gh) return nullptr;
|
|
|
|
|
auto entity = gh->getEntityManager().getEntity(gh->getPlayerGuid());
|
|
|
|
|
if (!entity) return nullptr;
|
|
|
|
|
return dynamic_cast<game::Unit*>(entity.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper: resolve "player", "target", "focus", "pet" unit IDs to entity
|
|
|
|
|
static game::Unit* resolveUnit(lua_State* L, const char* unitId) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (!gh || !unitId) return nullptr;
|
|
|
|
|
std::string uid(unitId);
|
|
|
|
|
for (char& c : uid) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
|
|
|
|
|
uint64_t guid = 0;
|
|
|
|
|
if (uid == "player") guid = gh->getPlayerGuid();
|
|
|
|
|
else if (uid == "target") guid = gh->getTargetGuid();
|
|
|
|
|
else if (uid == "focus") guid = gh->getFocusGuid();
|
|
|
|
|
else if (uid == "pet") guid = gh->getPetGuid();
|
|
|
|
|
else return nullptr;
|
|
|
|
|
|
|
|
|
|
if (guid == 0) return nullptr;
|
|
|
|
|
auto entity = gh->getEntityManager().getEntity(guid);
|
|
|
|
|
if (!entity) return nullptr;
|
|
|
|
|
return dynamic_cast<game::Unit*>(entity.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- WoW Unit API ---
|
|
|
|
|
|
|
|
|
|
static int lua_UnitName(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
if (unit && !unit->getName().empty()) {
|
|
|
|
|
lua_pushstring(L, unit->getName().c_str());
|
|
|
|
|
} else {
|
|
|
|
|
lua_pushstring(L, "Unknown");
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitHealth(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushnumber(L, unit ? unit->getHealth() : 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitHealthMax(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushnumber(L, unit ? unit->getMaxHealth() : 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitPower(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushnumber(L, unit ? unit->getPower() : 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitPowerMax(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushnumber(L, unit ? unit->getMaxPower() : 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitLevel(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushnumber(L, unit ? unit->getLevel() : 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitExists(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushboolean(L, unit != nullptr);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitIsDead(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
lua_pushboolean(L, unit && unit->getHealth() == 0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnitClass(lua_State* L) {
|
|
|
|
|
const char* uid = luaL_optstring(L, 1, "player");
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
auto* unit = resolveUnit(L, uid);
|
|
|
|
|
if (unit && gh) {
|
|
|
|
|
static const char* kClasses[] = {"", "Warrior","Paladin","Hunter","Rogue","Priest",
|
|
|
|
|
"Death Knight","Shaman","Mage","Warlock","","Druid"};
|
|
|
|
|
uint8_t classId = 0;
|
|
|
|
|
// For player, use character data; for others, use UNIT_FIELD_BYTES_0
|
|
|
|
|
std::string uidStr(uid);
|
|
|
|
|
for (char& c : uidStr) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
if (uidStr == "player") classId = gh->getPlayerClass();
|
|
|
|
|
const char* name = (classId < 12) ? kClasses[classId] : "Unknown";
|
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
|
lua_pushstring(L, name); // WoW returns localized + English
|
|
|
|
|
lua_pushnumber(L, classId);
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
lua_pushstring(L, "Unknown");
|
|
|
|
|
lua_pushstring(L, "Unknown");
|
|
|
|
|
lua_pushnumber(L, 0);
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Player/Game API ---
|
|
|
|
|
|
|
|
|
|
static int lua_GetMoney(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
lua_pushnumber(L, gh ? static_cast<double>(gh->getMoneyCopper()) : 0.0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_IsInGroup(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
lua_pushboolean(L, gh && gh->isInGroup());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_IsInRaid(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
lua_pushboolean(L, gh && gh->isInGroup() && gh->getPartyData().groupType == 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_GetPlayerMapPosition(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (gh) {
|
|
|
|
|
const auto& mi = gh->getMovementInfo();
|
|
|
|
|
lua_pushnumber(L, mi.x);
|
|
|
|
|
lua_pushnumber(L, mi.y);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
lua_pushnumber(L, 0);
|
|
|
|
|
lua_pushnumber(L, 0);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
feat: add action WoW API functions for Lua addons
Add 5 more essential WoW API functions for addon development:
- SendChatMessage(msg, type, lang, target) — send chat messages
(SAY, YELL, WHISPER, PARTY, GUILD, OFFICER, RAID, BG)
- CastSpellByName(name) — cast highest rank of named spell
- IsSpellKnown(spellId) — check if player knows a spell
- GetSpellCooldown(nameOrId) — get remaining cooldown
- HasTarget() — check if player has a target
Total WoW API surface: 18 functions across Unit, Game, and Action
categories. Addons can now query state, react to events, send
messages, and cast spells.
2026-03-20 11:34:04 -07:00
|
|
|
// --- Action API ---
|
|
|
|
|
|
|
|
|
|
static int lua_SendChatMessage(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (!gh) return 0;
|
|
|
|
|
const char* msg = luaL_checkstring(L, 1);
|
|
|
|
|
const char* chatType = luaL_optstring(L, 2, "SAY");
|
|
|
|
|
// language arg (3) ignored — server determines language
|
|
|
|
|
const char* target = luaL_optstring(L, 4, "");
|
|
|
|
|
|
|
|
|
|
std::string typeStr(chatType);
|
|
|
|
|
for (char& c : typeStr) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
|
|
|
|
|
|
|
|
|
game::ChatType ct = game::ChatType::SAY;
|
|
|
|
|
if (typeStr == "SAY") ct = game::ChatType::SAY;
|
|
|
|
|
else if (typeStr == "YELL") ct = game::ChatType::YELL;
|
|
|
|
|
else if (typeStr == "PARTY") ct = game::ChatType::PARTY;
|
|
|
|
|
else if (typeStr == "GUILD") ct = game::ChatType::GUILD;
|
|
|
|
|
else if (typeStr == "OFFICER") ct = game::ChatType::OFFICER;
|
|
|
|
|
else if (typeStr == "RAID") ct = game::ChatType::RAID;
|
|
|
|
|
else if (typeStr == "WHISPER") ct = game::ChatType::WHISPER;
|
|
|
|
|
else if (typeStr == "BATTLEGROUND") ct = game::ChatType::BATTLEGROUND;
|
|
|
|
|
|
|
|
|
|
std::string targetStr(target && *target ? target : "");
|
|
|
|
|
gh->sendChatMessage(ct, msg, targetStr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_CastSpellByName(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (!gh) return 0;
|
|
|
|
|
const char* name = luaL_checkstring(L, 1);
|
|
|
|
|
if (!name || !*name) return 0;
|
|
|
|
|
|
|
|
|
|
// Find highest rank of spell by name (same logic as /cast)
|
|
|
|
|
std::string nameLow(name);
|
|
|
|
|
for (char& c : nameLow) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
|
|
|
|
|
uint32_t bestId = 0;
|
|
|
|
|
int bestRank = -1;
|
|
|
|
|
for (uint32_t sid : gh->getKnownSpells()) {
|
|
|
|
|
std::string sn = gh->getSpellName(sid);
|
|
|
|
|
for (char& c : sn) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
if (sn != nameLow) continue;
|
|
|
|
|
int rank = 0;
|
|
|
|
|
const std::string& rk = gh->getSpellRank(sid);
|
|
|
|
|
if (!rk.empty()) {
|
|
|
|
|
std::string rkl = rk;
|
|
|
|
|
for (char& c : rkl) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
if (rkl.rfind("rank ", 0) == 0) {
|
|
|
|
|
try { rank = std::stoi(rkl.substr(5)); } catch (...) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rank > bestRank) { bestRank = rank; bestId = sid; }
|
|
|
|
|
}
|
|
|
|
|
if (bestId != 0) {
|
|
|
|
|
uint64_t target = gh->hasTarget() ? gh->getTargetGuid() : 0;
|
|
|
|
|
gh->castSpell(bestId, target);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_IsSpellKnown(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
uint32_t spellId = static_cast<uint32_t>(luaL_checknumber(L, 1));
|
|
|
|
|
lua_pushboolean(L, gh && gh->getKnownSpells().count(spellId));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_GetSpellCooldown(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
if (!gh) { lua_pushnumber(L, 0); lua_pushnumber(L, 0); return 2; }
|
|
|
|
|
// Accept spell name or ID
|
|
|
|
|
uint32_t spellId = 0;
|
|
|
|
|
if (lua_isnumber(L, 1)) {
|
|
|
|
|
spellId = static_cast<uint32_t>(lua_tonumber(L, 1));
|
|
|
|
|
} else {
|
|
|
|
|
const char* name = luaL_checkstring(L, 1);
|
|
|
|
|
std::string nameLow(name);
|
|
|
|
|
for (char& c : nameLow) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
for (uint32_t sid : gh->getKnownSpells()) {
|
|
|
|
|
std::string sn = gh->getSpellName(sid);
|
|
|
|
|
for (char& c : sn) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
|
|
|
if (sn == nameLow) { spellId = sid; break; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
float cd = gh->getSpellCooldown(spellId);
|
|
|
|
|
lua_pushnumber(L, 0); // start time (not tracked precisely, return 0)
|
|
|
|
|
lua_pushnumber(L, cd); // duration remaining
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_HasTarget(lua_State* L) {
|
|
|
|
|
auto* gh = getGameHandler(L);
|
|
|
|
|
lua_pushboolean(L, gh && gh->hasTarget());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:12:07 -07:00
|
|
|
// Stub for GetTime() — returns elapsed seconds
|
|
|
|
|
static int lua_wow_gettime(lua_State* L) {
|
|
|
|
|
static auto start = std::chrono::steady_clock::now();
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
double elapsed = std::chrono::duration<double>(now - start).count();
|
|
|
|
|
lua_pushnumber(L, elapsed);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaEngine::LuaEngine() = default;
|
|
|
|
|
|
|
|
|
|
LuaEngine::~LuaEngine() {
|
|
|
|
|
shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaEngine::initialize() {
|
|
|
|
|
if (L_) return true;
|
|
|
|
|
|
|
|
|
|
L_ = luaL_newstate();
|
|
|
|
|
if (!L_) {
|
|
|
|
|
LOG_ERROR("LuaEngine: failed to create Lua state");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open safe standard libraries (no io, os, debug, package)
|
|
|
|
|
luaopen_base(L_);
|
|
|
|
|
luaopen_table(L_);
|
|
|
|
|
luaopen_string(L_);
|
|
|
|
|
luaopen_math(L_);
|
|
|
|
|
|
|
|
|
|
// Remove unsafe globals from base library
|
|
|
|
|
const char* unsafeGlobals[] = {
|
|
|
|
|
"dofile", "loadfile", "load", "collectgarbage", "newproxy", nullptr
|
|
|
|
|
};
|
|
|
|
|
for (const char** g = unsafeGlobals; *g; ++g) {
|
|
|
|
|
lua_pushnil(L_);
|
|
|
|
|
lua_setglobal(L_, *g);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerCoreAPI();
|
2026-03-20 11:23:38 -07:00
|
|
|
registerEventAPI();
|
2026-03-20 11:12:07 -07:00
|
|
|
|
|
|
|
|
LOG_INFO("LuaEngine: initialized (Lua 5.1)");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaEngine::shutdown() {
|
|
|
|
|
if (L_) {
|
|
|
|
|
lua_close(L_);
|
|
|
|
|
L_ = nullptr;
|
|
|
|
|
LOG_INFO("LuaEngine: shut down");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaEngine::setGameHandler(game::GameHandler* handler) {
|
|
|
|
|
gameHandler_ = handler;
|
|
|
|
|
if (L_) {
|
|
|
|
|
lua_pushlightuserdata(L_, handler);
|
|
|
|
|
lua_setfield(L_, LUA_REGISTRYINDEX, "wowee_game_handler");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaEngine::registerCoreAPI() {
|
|
|
|
|
// Override print() to go to chat
|
|
|
|
|
lua_pushcfunction(L_, lua_wow_print);
|
|
|
|
|
lua_setglobal(L_, "print");
|
|
|
|
|
|
|
|
|
|
// WoW API stubs
|
|
|
|
|
lua_pushcfunction(L_, lua_wow_message);
|
|
|
|
|
lua_setglobal(L_, "message");
|
|
|
|
|
|
|
|
|
|
lua_pushcfunction(L_, lua_wow_gettime);
|
|
|
|
|
lua_setglobal(L_, "GetTime");
|
feat: add core WoW Lua API functions for addon development
Add 13 WoW-compatible Lua API functions that addons can call:
Unit API: UnitName, UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax,
UnitLevel, UnitExists, UnitIsDead, UnitClass (supports "player",
"target", "focus", "pet" unit IDs)
Game API: GetMoney, IsInGroup, IsInRaid, GetPlayerMapPosition
Updated HelloWorld addon to demonstrate querying player state.
2026-03-20 11:17:15 -07:00
|
|
|
|
|
|
|
|
// Unit API
|
|
|
|
|
static const struct { const char* name; lua_CFunction func; } unitAPI[] = {
|
|
|
|
|
{"UnitName", lua_UnitName},
|
|
|
|
|
{"UnitHealth", lua_UnitHealth},
|
|
|
|
|
{"UnitHealthMax", lua_UnitHealthMax},
|
|
|
|
|
{"UnitPower", lua_UnitPower},
|
|
|
|
|
{"UnitPowerMax", lua_UnitPowerMax},
|
|
|
|
|
{"UnitLevel", lua_UnitLevel},
|
|
|
|
|
{"UnitExists", lua_UnitExists},
|
|
|
|
|
{"UnitIsDead", lua_UnitIsDead},
|
|
|
|
|
{"UnitClass", lua_UnitClass},
|
|
|
|
|
{"GetMoney", lua_GetMoney},
|
|
|
|
|
{"IsInGroup", lua_IsInGroup},
|
|
|
|
|
{"IsInRaid", lua_IsInRaid},
|
|
|
|
|
{"GetPlayerMapPosition", lua_GetPlayerMapPosition},
|
feat: add action WoW API functions for Lua addons
Add 5 more essential WoW API functions for addon development:
- SendChatMessage(msg, type, lang, target) — send chat messages
(SAY, YELL, WHISPER, PARTY, GUILD, OFFICER, RAID, BG)
- CastSpellByName(name) — cast highest rank of named spell
- IsSpellKnown(spellId) — check if player knows a spell
- GetSpellCooldown(nameOrId) — get remaining cooldown
- HasTarget() — check if player has a target
Total WoW API surface: 18 functions across Unit, Game, and Action
categories. Addons can now query state, react to events, send
messages, and cast spells.
2026-03-20 11:34:04 -07:00
|
|
|
{"SendChatMessage", lua_SendChatMessage},
|
|
|
|
|
{"CastSpellByName", lua_CastSpellByName},
|
|
|
|
|
{"IsSpellKnown", lua_IsSpellKnown},
|
|
|
|
|
{"GetSpellCooldown", lua_GetSpellCooldown},
|
|
|
|
|
{"HasTarget", lua_HasTarget},
|
feat: add core WoW Lua API functions for addon development
Add 13 WoW-compatible Lua API functions that addons can call:
Unit API: UnitName, UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax,
UnitLevel, UnitExists, UnitIsDead, UnitClass (supports "player",
"target", "focus", "pet" unit IDs)
Game API: GetMoney, IsInGroup, IsInRaid, GetPlayerMapPosition
Updated HelloWorld addon to demonstrate querying player state.
2026-03-20 11:17:15 -07:00
|
|
|
};
|
|
|
|
|
for (const auto& [name, func] : unitAPI) {
|
|
|
|
|
lua_pushcfunction(L_, func);
|
|
|
|
|
lua_setglobal(L_, name);
|
|
|
|
|
}
|
2026-03-20 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:23:38 -07:00
|
|
|
// ---- Event System ----
|
|
|
|
|
// Lua-side: WoweeEvents table holds { ["EVENT_NAME"] = { handler1, handler2, ... } }
|
|
|
|
|
// RegisterEvent("EVENT", handler) adds a handler function
|
|
|
|
|
// UnregisterEvent("EVENT", handler) removes it
|
|
|
|
|
|
|
|
|
|
static int lua_RegisterEvent(lua_State* L) {
|
|
|
|
|
const char* eventName = luaL_checkstring(L, 1);
|
|
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
|
|
|
|
|
|
|
|
|
// Get or create the WoweeEvents table
|
|
|
|
|
lua_getglobal(L, "__WoweeEvents");
|
|
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
|
lua_setglobal(L, "__WoweeEvents");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get or create the handler list for this event
|
|
|
|
|
lua_getfield(L, -1, eventName);
|
|
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
|
lua_setfield(L, -3, eventName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Append the handler function to the list
|
|
|
|
|
int len = static_cast<int>(lua_objlen(L, -1));
|
|
|
|
|
lua_pushvalue(L, 2); // push the handler function
|
|
|
|
|
lua_rawseti(L, -2, len + 1);
|
|
|
|
|
|
|
|
|
|
lua_pop(L, 2); // pop handler list + WoweeEvents
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lua_UnregisterEvent(lua_State* L) {
|
|
|
|
|
const char* eventName = luaL_checkstring(L, 1);
|
|
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
|
|
|
|
|
|
|
|
|
lua_getglobal(L, "__WoweeEvents");
|
|
|
|
|
if (lua_isnil(L, -1)) { lua_pop(L, 1); return 0; }
|
|
|
|
|
|
|
|
|
|
lua_getfield(L, -1, eventName);
|
|
|
|
|
if (lua_isnil(L, -1)) { lua_pop(L, 2); return 0; }
|
|
|
|
|
|
|
|
|
|
// Remove matching handler from the list
|
|
|
|
|
int len = static_cast<int>(lua_objlen(L, -1));
|
|
|
|
|
for (int i = 1; i <= len; i++) {
|
|
|
|
|
lua_rawgeti(L, -1, i);
|
|
|
|
|
if (lua_rawequal(L, -1, 2)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
// Shift remaining elements down
|
|
|
|
|
for (int j = i; j < len; j++) {
|
|
|
|
|
lua_rawgeti(L, -1, j + 1);
|
|
|
|
|
lua_rawseti(L, -2, j);
|
|
|
|
|
}
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_rawseti(L, -2, len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaEngine::registerEventAPI() {
|
|
|
|
|
lua_pushcfunction(L_, lua_RegisterEvent);
|
|
|
|
|
lua_setglobal(L_, "RegisterEvent");
|
|
|
|
|
|
|
|
|
|
lua_pushcfunction(L_, lua_UnregisterEvent);
|
|
|
|
|
lua_setglobal(L_, "UnregisterEvent");
|
|
|
|
|
|
|
|
|
|
// Create the events table
|
|
|
|
|
lua_newtable(L_);
|
|
|
|
|
lua_setglobal(L_, "__WoweeEvents");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaEngine::fireEvent(const std::string& eventName,
|
|
|
|
|
const std::vector<std::string>& args) {
|
|
|
|
|
if (!L_) return;
|
|
|
|
|
|
|
|
|
|
lua_getglobal(L_, "__WoweeEvents");
|
|
|
|
|
if (lua_isnil(L_, -1)) { lua_pop(L_, 1); return; }
|
|
|
|
|
|
|
|
|
|
lua_getfield(L_, -1, eventName.c_str());
|
|
|
|
|
if (lua_isnil(L_, -1)) { lua_pop(L_, 2); return; }
|
|
|
|
|
|
|
|
|
|
int handlerCount = static_cast<int>(lua_objlen(L_, -1));
|
|
|
|
|
for (int i = 1; i <= handlerCount; i++) {
|
|
|
|
|
lua_rawgeti(L_, -1, i);
|
|
|
|
|
if (!lua_isfunction(L_, -1)) { lua_pop(L_, 1); continue; }
|
|
|
|
|
|
|
|
|
|
// Push arguments: event name first, then extra args
|
|
|
|
|
lua_pushstring(L_, eventName.c_str());
|
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
|
lua_pushstring(L_, arg.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nargs = 1 + static_cast<int>(args.size());
|
|
|
|
|
if (lua_pcall(L_, nargs, 0, 0) != 0) {
|
|
|
|
|
const char* err = lua_tostring(L_, -1);
|
|
|
|
|
LOG_ERROR("LuaEngine: event '", eventName, "' handler error: ",
|
|
|
|
|
err ? err : "(unknown)");
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L_, 2); // pop handler list + WoweeEvents
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 11:12:07 -07:00
|
|
|
bool LuaEngine::executeFile(const std::string& path) {
|
|
|
|
|
if (!L_) return false;
|
|
|
|
|
|
|
|
|
|
int err = luaL_dofile(L_, path.c_str());
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
const char* errMsg = lua_tostring(L_, -1);
|
|
|
|
|
std::string msg = errMsg ? errMsg : "(unknown error)";
|
|
|
|
|
LOG_ERROR("LuaEngine: error loading '", path, "': ", msg);
|
|
|
|
|
if (gameHandler_) {
|
|
|
|
|
game::MessageChatData errChat;
|
|
|
|
|
errChat.type = game::ChatType::SYSTEM;
|
|
|
|
|
errChat.language = game::ChatLanguage::UNIVERSAL;
|
|
|
|
|
errChat.message = "|cffff4040[Lua Error] " + msg + "|r";
|
|
|
|
|
gameHandler_->addLocalChatMessage(errChat);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaEngine::executeString(const std::string& code) {
|
|
|
|
|
if (!L_) return false;
|
|
|
|
|
|
|
|
|
|
int err = luaL_dostring(L_, code.c_str());
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
const char* errMsg = lua_tostring(L_, -1);
|
|
|
|
|
std::string msg = errMsg ? errMsg : "(unknown error)";
|
|
|
|
|
LOG_ERROR("LuaEngine: script error: ", msg);
|
|
|
|
|
if (gameHandler_) {
|
|
|
|
|
game::MessageChatData errChat;
|
|
|
|
|
errChat.type = game::ChatType::SYSTEM;
|
|
|
|
|
errChat.language = game::ChatLanguage::UNIVERSAL;
|
|
|
|
|
errChat.message = "|cffff4040[Lua Error] " + msg + "|r";
|
|
|
|
|
gameHandler_->addLocalChatMessage(errChat);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace wowee::addons
|