Skip to content

Commit

Permalink
Feat: Add DBCStores access via Lua with getters for DBC entries
Browse files Browse the repository at this point in the history
  • Loading branch information
iThorgrim committed Jan 22, 2025
1 parent 4e068e4 commit 294cd09
Show file tree
Hide file tree
Showing 8 changed files with 948 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/LuaEngine/ElunaDBCRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "ElunaDBCRegistry.h"

std::vector<DBCDefinition> dbcRegistry = {
REGISTER_DBC(GemProperties, GemPropertiesEntry, sGemPropertiesStore),
REGISTER_DBC(Spell, SpellEntry, sSpellStore),
};
37 changes: 37 additions & 0 deletions src/LuaEngine/ElunaDBCRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef ELUNADBCREGISTRY_H
#define ELUNADBCREGISTRY_H

#include <string>
#include <vector>
#include <functional>
#include <typeinfo>

#include "DBCStores.h"
#include "LuaEngine.h"

struct DBCDefinition
{
std::string name;
void* storage;
const std::type_info& type;
std::function<const void*(uint32)> lookupFunction;
std::function<void(lua_State*, const void*)> pushFunction;
};

extern std::vector<DBCDefinition> dbcRegistry;

#define REGISTER_DBC(dbcName, entryType, store) \
{ \
#dbcName, \
reinterpret_cast<void*>(&store), \
typeid(DBCStorage<entryType>), \
[](uint32 id) -> const void* { \
return store.LookupEntry(id); \
}, \
[](lua_State* L, const void* entry) { \
auto cast_entry = static_cast<const entryType*>(entry); \
Eluna::Push(L, *cast_entry); \
} \
}

#endif // ELUNADBCREGISTRY_H
10 changes: 10 additions & 0 deletions src/LuaEngine/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,16 @@ void Eluna::Push(lua_State* luastate, ObjectGuid const guid)
ElunaTemplate<unsigned long long>::Push(luastate, new unsigned long long(guid.GetRawValue()));
}

void Eluna::Push(lua_State* luastate, GemPropertiesEntry const& gemProperties)
{
Push(luastate, &gemProperties);
}

void Eluna::Push(lua_State* luastate, SpellEntry const& spell)
{
Push(luastate, &spell);
}

static int CheckIntegerRange(lua_State* luastate, int narg, int min, int max)
{
double value = luaL_checknumber(luastate, narg);
Expand Down
2 changes: 2 additions & 0 deletions src/LuaEngine/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class ELUNA_GAME_API Eluna
static void Push(lua_State* luastate, Pet const* pet);
static void Push(lua_State* luastate, TempSummon const* summon);
static void Push(lua_State* luastate, ObjectGuid const guid);
static void Push(lua_State* luastate, GemPropertiesEntry const& gemProperties);
static void Push(lua_State* luastate, SpellEntry const& spell);
template<typename T>
static void Push(lua_State* luastate, T const* ptr)
{
Expand Down
119 changes: 119 additions & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ extern "C"
#include "ItemTemplateMethods.h"
#include "RollMethods.h"

// DBCStores includes
#include "GemPropertiesEntryMethods.h"
#include "SpellEntryMethods.h"

luaL_Reg GlobalMethods[] =
{
// Hooks
Expand Down Expand Up @@ -160,6 +164,7 @@ luaL_Reg GlobalMethods[] =
{ "StopGameEvent", &LuaGlobalFunctions::StopGameEvent },
{ "HttpRequest", &LuaGlobalFunctions::HttpRequest },
{ "SetOwnerHalaa", &LuaGlobalFunctions::SetOwnerHalaa },
{ "LookupEntry", &LuaGlobalFunctions::LookupEntry },

{ NULL, NULL }
};
Expand Down Expand Up @@ -1290,6 +1295,114 @@ ElunaRegister<Roll> RollMethods[] =
{ NULL, NULL }
};

ElunaRegister<GemPropertiesEntry> GemPropertiesEntryMethods[] =
{
// Getters
{ "GetId", &LuaGemPropertiesEntry::GetId },
{ "GetSpellItemEnchantement", &LuaGemPropertiesEntry::GetSpellItemEnchantement },

{ NULL, NULL }
};

ElunaRegister<SpellEntry> SpellEntryMethods[] =
{
// Getters
{ "GetId", &LuaSpellEntry::GetId },
{ "GetCategory", &LuaSpellEntry::GetCategory },
{ "GetDispel", &LuaSpellEntry::GetDispel },
{ "GetMechanic", &LuaSpellEntry::GetMechanic },
{ "GetAttributes", &LuaSpellEntry::GetAttributes },
{ "GetAttributesEx", &LuaSpellEntry::GetAttributesEx },
{ "GetAttributesEx2", &LuaSpellEntry::GetAttributesEx2 },
{ "GetAttributesEx3", &LuaSpellEntry::GetAttributesEx3 },
{ "GetAttributesEx4", &LuaSpellEntry::GetAttributesEx4 },
{ "GetAttributesEx5", &LuaSpellEntry::GetAttributesEx5 },
{ "GetAttributesEx6", &LuaSpellEntry::GetAttributesEx6 },
{ "GetAttributesEx7", &LuaSpellEntry::GetAttributesEx7 },
{ "GetStances", &LuaSpellEntry::GetStances },
{ "GetStancesNot", &LuaSpellEntry::GetStancesNot },
{ "GetTargets", &LuaSpellEntry::GetTargets },
{ "GetTargetCreatureType", &LuaSpellEntry::GetTargetCreatureType },
{ "GetRequiresSpellFocus", &LuaSpellEntry::GetRequiresSpellFocus },
{ "GetFacingCasterFlags", &LuaSpellEntry::GetFacingCasterFlags },
{ "GetCasterAuraState", &LuaSpellEntry::GetCasterAuraState },
{ "GetTargetAuraState", &LuaSpellEntry::GetTargetAuraState },
{ "GetCasterAuraStateNot", &LuaSpellEntry::GetCasterAuraStateNot },
{ "GetTargetAuraStateNot", &LuaSpellEntry::GetTargetAuraStateNot },
{ "GetCasterAuraSpell", &LuaSpellEntry::GetCasterAuraSpell },
{ "GetTargetAuraSpell", &LuaSpellEntry::GetTargetAuraSpell },
{ "GetExcludeCasterAuraSpell", &LuaSpellEntry::GetExcludeCasterAuraSpell },
{ "GetExcludeTargetAuraSpell", &LuaSpellEntry::GetExcludeTargetAuraSpell },
{ "GetCastingTimeIndex", &LuaSpellEntry::GetCastingTimeIndex },
{ "GetRecoveryTime", &LuaSpellEntry::GetRecoveryTime },
{ "GetCategoryRecoveryTime", &LuaSpellEntry::GetCategoryRecoveryTime },
{ "GetInterruptFlags", &LuaSpellEntry::GetInterruptFlags },
{ "GetAuraInterruptFlags", &LuaSpellEntry::GetAuraInterruptFlags },
{ "GetChannelInterruptFlags", &LuaSpellEntry::GetChannelInterruptFlags },
{ "GetProcFlags", &LuaSpellEntry::GetProcFlags },
{ "GetProcChance", &LuaSpellEntry::GetProcChance },
{ "GetProcCharges", &LuaSpellEntry::GetProcCharges },
{ "GetMaxLevel", &LuaSpellEntry::GetMaxLevel },
{ "GetBaseLevel", &LuaSpellEntry::GetBaseLevel },
{ "GetSpellLevel", &LuaSpellEntry::GetSpellLevel },
{ "GetDurationIndex", &LuaSpellEntry::GetDurationIndex },
{ "GetPowerType", &LuaSpellEntry::GetPowerType },
{ "GetManaCost", &LuaSpellEntry::GetManaCost },
{ "GetManaCostPerlevel", &LuaSpellEntry::GetManaCostPerlevel },
{ "GetManaPerSecond", &LuaSpellEntry::GetManaPerSecond },
{ "GetManaPerSecondPerLevel", &LuaSpellEntry::GetManaPerSecondPerLevel },
{ "GetRangeIndex", &LuaSpellEntry::GetRangeIndex },
{ "GetSpeed", &LuaSpellEntry::GetSpeed },
{ "GetStackAmount", &LuaSpellEntry::GetStackAmount },
{ "GetTotem", &LuaSpellEntry::GetTotem },
{ "GetReagent", &LuaSpellEntry::GetReagent },
{ "GetReagentCount", &LuaSpellEntry::GetReagentCount },
{ "GetEquippedItemClass", &LuaSpellEntry::GetEquippedItemClass },
{ "GetEquippedItemSubClassMask", &LuaSpellEntry::GetEquippedItemSubClassMask },
{ "GetEquippedItemInventoryTypeMask", &LuaSpellEntry::GetEquippedItemInventoryTypeMask },
{ "GetEffect", &LuaSpellEntry::GetEffect },
{ "GetEffectDieSides", &LuaSpellEntry::GetEffectDieSides },
{ "GetEffectRealPointsPerLevel", &LuaSpellEntry::GetEffectRealPointsPerLevel },
{ "GetEffectBasePoints", &LuaSpellEntry::GetEffectBasePoints },
{ "GetEffectMechanic", &LuaSpellEntry::GetEffectMechanic },
{ "GetEffectImplicitTargetA", &LuaSpellEntry::GetEffectImplicitTargetA },
{ "GetEffectImplicitTargetB", &LuaSpellEntry::GetEffectImplicitTargetB },
{ "GetEffectRadiusIndex", &LuaSpellEntry::GetEffectRadiusIndex },
{ "GetEffectApplyAuraName", &LuaSpellEntry::GetEffectApplyAuraName },
{ "GetEffectAmplitude", &LuaSpellEntry::GetEffectAmplitude },
{ "GetEffectValueMultiplier", &LuaSpellEntry::GetEffectValueMultiplier },
{ "GetEffectChainTarget", &LuaSpellEntry::GetEffectChainTarget },
{ "GetEffectItemType", &LuaSpellEntry::GetEffectItemType },
{ "GetEffectMiscValue", &LuaSpellEntry::GetEffectMiscValue },
{ "GetEffectMiscValueB", &LuaSpellEntry::GetEffectMiscValueB },
{ "GetEffectTriggerSpell", &LuaSpellEntry::GetEffectTriggerSpell },
{ "GetEffectPointsPerComboPoint", &LuaSpellEntry::GetEffectPointsPerComboPoint },
{ "GetEffectSpellClassMask", &LuaSpellEntry::GetEffectSpellClassMask },
{ "GetSpellVisual", &LuaSpellEntry::GetSpellVisual },
{ "GetSpellIconID", &LuaSpellEntry::GetSpellIconID },
{ "GetActiveIconID", &LuaSpellEntry::GetActiveIconID },
{ "GetSpellPriority", &LuaSpellEntry::GetSpellPriority },
{ "GetSpellName", &LuaSpellEntry::GetSpellName },
{ "GetRank", &LuaSpellEntry::GetRank },
{ "GetManaCostPercentage", &LuaSpellEntry::GetManaCostPercentage },
{ "GetStartRecoveryCategory", &LuaSpellEntry::GetStartRecoveryCategory },
{ "GetStartRecoveryTime", &LuaSpellEntry::GetStartRecoveryTime },
{ "GetMaxTargetLevel", &LuaSpellEntry::GetMaxTargetLevel },
{ "GetSpellFamilyName", &LuaSpellEntry::GetSpellFamilyName },
{ "GetSpellFamilyFlags", &LuaSpellEntry::GetSpellFamilyFlags },
{ "GetMaxAffectedTargets", &LuaSpellEntry::GetMaxAffectedTargets },
{ "GetDmgClass", &LuaSpellEntry::GetDmgClass },
{ "GetPreventionType", &LuaSpellEntry::GetPreventionType },
{ "GetEffectDamageMultiplier", &LuaSpellEntry::GetEffectDamageMultiplier },
{ "GetTotemCategory", &LuaSpellEntry::GetTotemCategory },
{ "GetAreaGroupId", &LuaSpellEntry::GetAreaGroupId },
{ "GetSchoolMask", &LuaSpellEntry::GetSchoolMask },
{ "GetRuneCostID", &LuaSpellEntry::GetRuneCostID },
{ "GetEffectBonusMultiplier", &LuaSpellEntry::GetEffectBonusMultiplier },

{ NULL, NULL }
};

// fix compile error about accessing vehicle destructor
template<> int ElunaTemplate<Vehicle>::CollectGarbage(lua_State* L)
{
Expand Down Expand Up @@ -1435,6 +1548,12 @@ void RegisterFunctions(Eluna* E)
ElunaTemplate<Roll>::Register(E, "Roll");
ElunaTemplate<Roll>::SetMethods(E, RollMethods);

ElunaTemplate<GemPropertiesEntry>::Register(E, "GemPropertiesEntry");
ElunaTemplate<GemPropertiesEntry>::SetMethods(E, GemPropertiesEntryMethods);

ElunaTemplate<SpellEntry>::Register(E, "SpellEntry");
ElunaTemplate<SpellEntry>::SetMethods(E, SpellEntryMethods);

ElunaTemplate<long long>::Register(E, "long long", true);

ElunaTemplate<unsigned long long>::Register(E, "unsigned long long", true);
Expand Down
40 changes: 40 additions & 0 deletions src/LuaEngine/methods/GemPropertiesEntryMethods.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#ifndef GEMPROPERTIESENTRYMETHODS_H
#define GEMPROPERTIESENTRYMETHODS_H

namespace LuaGemPropertiesEntry
{

/**
* Returns the ID of a [GemPropertiesEntry].
*
* This method retrieves the ID from a given GemPropertiesEntry instance
* and pushes it onto the Lua stack.
*
* @return uint32 id : The ID of the specified GemPropertiesEntry.
*/
int GetId(lua_State* L, GemPropertiesEntry* gemProperties)
{
Eluna::Push(L, gemProperties->ID);
return 1;
}

/**
* Returns the spell item enchantment of a [GemPropertiesEntry].
*
* This function retrieves the `spellitemenchantement` attribute from the provided `GemPropertiesEntry`.
*
* @return uint32 spellitemenchantement : The spell item enchantment ID.
*/
int GetSpellItemEnchantement(lua_State* L, GemPropertiesEntry* entry)
{
Eluna::Push(L, entry->spellitemenchantement);
return 1;
}
}
#endif
34 changes: 34 additions & 0 deletions src/LuaEngine/methods/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
#define GLOBALMETHODS_H

#include "BindingMap.h"
#include "ElunaDBCRegistry.h"

#include "BanMgr.h"
#include "GameTime.h"
#include "SharedDefines.h"
#include "OutdoorPvPMgr.h"
#include "../../../../src/server/scripts/OutdoorPvP/OutdoorPvPNA.h"


enum BanMode
{
BAN_ACCOUNT = 1,
Expand Down Expand Up @@ -3143,5 +3145,37 @@ namespace LuaGlobalFunctions

return 0;
}

/**
* Returns the instance of the specified DBC (DatabaseClient) store.
*
* This function retrieves the DBC store associated with the provided name
* and pushes it onto the Lua stack.
*
* @param const char* dbcName : The name of the DBC store to retrieve.
* @param uint32 id : The ID used to look up within the specified DBC store.
*
* @return [DBCStore] store : The requested DBC store instance.
*/
int LookupEntry(lua_State* L)
{
const char* dbcName = Eluna::CHECKVAL<const char*>(L, 1);
uint32 id = Eluna::CHECKVAL<uint32>(L, 2);

for (const auto& dbc : dbcRegistry)
{
if (dbc.name == dbcName)
{
const void* entry = dbc.lookupFunction(id);
if (!entry)
return 0;

dbc.pushFunction(L, entry);
return 1;
}
}

return luaL_error(L, "Invalid DBC name: %s", dbcName);
}
}
#endif
Loading

0 comments on commit 294cd09

Please sign in to comment.