-- vymdt.01.2010.11.20.0000
-- Created by: Ryan F. Mercer -Open source
--===========================================================================
-- ShareData.lua
--===========================================================================
--[[
Manages shared variables for different mods. ShareData uses the LuaEvent system to set and get variable values that are supposed to be shared between lua contexts.
You can store variables with just a key, which will be written to the global context, or you can store them with your own context key.
For example, if your mod is called MyMod and you want to share variables in a namespace called "MyMod", you should supply "MyMod" as context.
It is highly recommended to use the more user-friendly share() function from SaveUtils to share data. Just add ShareData as an InGameUIAddin and it will be accessible in your whole mod.
]]
--===========================================================================
--[[
Global Variables.
]]
g_super = {};
g_context = {};
--===========================================================================
--[[
Finds out if a variable has been shared by a mod.
Arguments:
key: String or number. The key for the shared variable
tbl: Table. The table to write the return value to.
context: String. The key of the context table the variable is stored in. Defaults to global context if not given
Returns:
Doesn't really return anything but appends a "true" value to tbl if the key is already shared and a false value otherwise
]]
function onHasShared( key, tbl, context )
-- do type checking in case people supply bad arguments
local t = type( key );
if not (t == "number" or (t == "string" and key ~= "")) then
print( "onHasShared(): Invalid first argument (key) of type "..t..", expected number, or unempty-string." );
return false; --error.
end
local t = type( tbl );
if not t == "table" then
print( "onHasShared(): Invalid second argument (tbl) of type "..t..", expected table." );
return false; --error.
end
local t = type( context );
if not (t == "nil" or (t == "string" and context ~= "")) then
print( "onHasShared(): Invalid third argument (context) of type "..t..", expected nil, or unempty-string." );
return false; --error.
end
-- end type checking
-- if no context is supplied, default to the global context
if context == nil then
if g_super[key] ~= nil then
table.insert( tbl, true );
else
table.insert( tbl, false );
end
else
if type( g_context[context] ) == "table" and g_context[context][key] ~= nil then
table.insert( tbl, true );
else
table.insert( tbl, false );
end
end
end
LuaEvents.HasShared.Add( onHasShared );
--===========================================================================
--[[
Sets the value of a shared variable. If desired, this function also writes the current value of a shared variable into the return table.
Arguments:
key: String or number. The key for the shared variable. If nil, a numeric key will be automatically chosen
value: number, string, boolean or table. The value of the shared variable.
context: Table. Optional. The context to store this variable in. Defaults to global context if not given
tbl: Table. Optional. The table to write the current value of the shared variable to, if any.
Returns:
Doesn't really return anything but inserts the shared value into tbl[key] if tbl is given and the key is already shared
]]
function onSetShared( key, value, context, tbl )
-- do type checking in case people supply bad arguments
local t = type( key );
if not (t == "nil" or t == "number" or (t == "string" and key ~= "")) then
print( "onSetSession(): Invalid first argument (key) of type "..t..", expected nil, number, or unempty-string." );
return false; --error.
end
local t = type( value );
if not (t ~= "function" and t ~= "userdata" and t ~= "thread") then
print( "onSetSession(): Invalid second argument (value) of type "..t..", expected nil, number, string, boolean, or table." );
return false; --error.
end
local t = type( context );
if not (t == "nil" or (t == "string" and context ~= "")) then
print( "onSetSession(): Invalid third argument (context) of type "..t..", expected nil, or unempty-string." );
return false; --error.
end
local t = type( tbl );
if not (t == "nil" or t == "table") then
print( "onSetSession(): Invalid fourth argument (tbl) of type "..t..", expected nil, or table." );
return false; --error.
end
-- end type checking
-- if no context is supplied, default to the global context
if context ~= nil then
g_context[context] = g_context[context] or {};
if key ~= nil then
if tbl ~= nil then
tbl[key] = g_context[context][key];
end
g_context[context][key] = value;
else
table.insert( g_context[context], value ); --automatic integer key.
end
else
if key ~= nil then
if tbl ~= nil then
tbl[key] = g_super[key];
end
g_super[key] = value;
else
table.insert( g_super, value ); --automatic integer key.
end
end
end
LuaEvents.SetShared.Add( onSetShared );
--===========================================================================
--[[
Returns the value of a shared variable.
Arguments:
key: String, number or nil. The key of the shared variable. If nil, this will return all variables in the given context.
tbl: Table. The table to write the return value to
context: String or bool true. If string, the key of the context table the variable is stored in. If true, the function returns the a copy of all context tables instead. Defaults to the global context if nil.
Returns:
Doesn't really return anything but inserts the shared value into tbl[key] if tbl is given and the key is shared. A special case exists if context == true, which will insert the table containing all the shared contexts into tbl.
]]
function onGetShared( key, tbl, context )
-- do type checking in case people supply bad arguments
local t = type( key );
if not (t == "nil" or t == "number" or (t == "string" and key ~= "")) then
print( "onGetSession(): Invalid first argument (key) of type "..t..", expected nil, number, or unempty-string." );
return false; --error.
end
local t = type( tbl );
if not t == "table" then
print( "onGetSession(): Invalid second argument (tbl) of type "..t..", expected table." );
return false; --error.
end
local t = type( context );
if not (t == "nil" or (t == "string" and context ~= "") or context == true) then
print( "onGetSession(): Invalid third argument (context) of type "..t..", expected nil, unempty-string, or boolean true" );
return false; --error.
end
-- end type checking
-- default to the global context if none is given
if context ~= nil then
-- check special case: If context is true, return a copy of all shared contexts.
if context ~= true then
if key ~= nil then
if type( g_context[context] ) == "table" and g_context[context][key] ~= nil then
tbl[key] = g_context[context][key];
end
else
for k,v in pairs( g_context[context] ) do
tbl[k] = v;
end
end
else
for k,v in pairs( g_context ) do
tbl[k] = v;
end
end
else
if key ~= nil then
if g_super[key] ~= nil then
tbl[key] = g_super[key];
end
else
for k,v in pairs( g_super ) do
tbl[k] = v;
end
end
end
end
LuaEvents.GetShared.Add( onGetShared );
--===========================================================================
--END ShareData.lua
--===========================================================================
-- Created by: Ryan F. Mercer -Open source