Add some documentation and add map support back

Part of https://github.com/ElunaLuaEngine/Eluna/issues/167
This commit is contained in:
Rochet2
2015-08-28 22:03:40 +03:00
parent 83e001b2ac
commit c80b80c206

View File

@@ -6,48 +6,62 @@
-- filename.ext files are loaded before normal .lua files -- filename.ext files are loaded before normal .lua files
--
-- This extension allows saving data to specific object for it's lifetime in current runtime session
-- Supports Map, Player, Creature, GameObject
--
-- SetData sets a value
-- obj:SetData(key, val)
--
-- GetData gets the data table or a specific value by key from it
-- local tbl = obj:GetData()
-- local val = obj:GetData(key)
--
local variableStores = { local variableStores = {
Map = {},
Player = {}, Player = {},
Creature = {}, Creature = {},
GameObject = {}, GameObject = {},
} }
local function DestroyData(event, obj) local function DestroyMapData(event, obj)
if event == 18 or event == 17 then local map = obj:GetMapId()
local map = obj:GetMapId() local inst = obj:GetInstanceId()
local inst = obj:GetInstanceId() for k,v in pairs(variableStores) do
for k,v in pairs(variableStores) do local mapdata = v[map]
if inst == 0 then if mapdata then
v[map] = nil mapdata[inst] = nil
else
local mapdata = v[map]
if mapdata then
mapdata[inst] = nil
end
end
end end
return
end end
local mapdata = variableStores[obj:GetObjectType()][obj:GetMapId()] end
local function DestroyObjData(event, obj)
local map = self:GetMapId()
local inst = self:GetInstanceId()
local otype = self:GetObjectType()
local guid = otype == "Map" and 1 or self:GetGUIDLow()
local mapdata = variableStores[otype][map]
if mapdata then if mapdata then
local instancedata = mapdata[obj:GetInstanceId()] local instancedata = mapdata[inst]
if instancedata then if instancedata then
instancedata[obj:GetGUIDLow()] = nil instancedata[guid] = nil
end end
end end
end end
local function GetData(self, field) local function GetData(self, field)
local varStore = variableStores[self:GetObjectType()]
local guid = self:GetGUIDLow()
local map = self:GetMapId() local map = self:GetMapId()
local inst = self:GetInstanceId() local inst = self:GetInstanceId()
local otype = self:GetObjectType()
local guid = otype == "Map" and 1 or self:GetGUIDLow()
local varStore = variableStores[otype]
varStore[map] = varStore[map] or {} varStore[map] = varStore[map] or {}
varStore[map][inst] = varStore[map][inst] or {} varStore[map][inst] = varStore[map][inst] or {}
varStore[map][inst][guid] = varStore[map][inst][guid] or {} varStore[map][inst][guid] = varStore[map][inst][guid] or {}
if field then if field ~= nil then
return varStore[map][inst][guid][field] return varStore[map][inst][guid][field]
else else
return varStore[map][inst][guid] return varStore[map][inst][guid]
@@ -55,10 +69,11 @@ local function GetData(self, field)
end end
local function SetData(self, field, val) local function SetData(self, field, val)
local varStore = variableStores[self:GetObjectType()]
local guid = self:GetGUIDLow()
local map = self:GetMapId() local map = self:GetMapId()
local inst = self:GetInstanceId() local inst = self:GetInstanceId()
local otype = self:GetObjectType()
local guid = otype == "Map" and 1 or self:GetGUIDLow()
local varStore = variableStores[otype]
varStore[map] = varStore[map] or {} varStore[map] = varStore[map] or {}
varStore[map][inst] = varStore[map][inst] or {} varStore[map][inst] = varStore[map][inst] or {}
@@ -72,9 +87,8 @@ for k,v in pairs(variableStores) do
_G[k].SetData = SetData _G[k].SetData = SetData
end end
RegisterPlayerEvent(3, DestroyData) -- login RegisterPlayerEvent(4, DestroyObjData) -- logout
RegisterPlayerEvent(4, DestroyData) -- logout RegisterServerEvent(31, DestroyObjData) -- creature delete
RegisterServerEvent(31, DestroyData) -- creature delete RegisterServerEvent(32, DestroyObjData) -- gameobject delete
RegisterServerEvent(32, DestroyData) -- gameobject delete RegisterServerEvent(17, DestroyMapData) -- map create
RegisterServerEvent(17, DestroyData) -- map create RegisterServerEvent(18, DestroyMapData) -- map destroy
RegisterServerEvent(18, DestroyData) -- map destroy