Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function removeColorCoding ( name )
- return type(name)=='string' and string.gsub ( name, '#%x%x%x%x%x%x', '' ) or name
- end
- function isServer()
- return triggerClientEvent ~= nil end
- function isClient()
- return triggerServerEvent ~= nil end
- ResourceRoot = getResourceRootElement(getThisResource())
- of = {}
- of.version = {}
- of.version.type = "Work"
- of.version.number = 3
- of.version.string = "1.0 r18"
- of.class = {}
- of.class.__index = of.class;
- function of.class:new( inherit, elementData, element )
- local object = { element = element }
- object.__index = object
- setmetatable(object, inherit)
- if inherit then
- object.__index = inherit
- end
- if elementData == true then
- object.data = {}
- setmetatable(object.data, { __index = function(w) return getElementData(self.element, w) end, __newindex = function(w,x,z) return setElementData(self.element, w, z) end })
- end
- return object;
- end
- of.debug = of.class:new()
- function of.debug:var_dump(...)
- local verbose = false
- local firstLevel = true
- local outputDirectly = true
- local noNames = false
- local indentation = "\t\t\t\t\t\t"
- local depth = nil
- local name = nil
- local output = {}
- for k,v in ipairs(arg) do
- if type(v) == "string" and k < #arg and v:sub(1,1) == "-" then
- local modifiers = v:sub(2)
- if modifiers:find("v") ~= nil then
- verbose = true
- end
- if modifiers:find("s") ~= nil then
- outputDirectly = false
- end
- if modifiers:find("n") ~= nil then
- verbose = false
- end
- if modifiers:find("u") ~= nil then
- noNames = true
- end
- local s,e = modifiers:find("d%d+")
- if s ~= nil then
- depth = tonumber(string.sub(modifiers,s+1,e))
- end
- elseif type(v) == "string" and k < #arg and name == nil and not noNames then
- name = v
- else
- if name ~= nil then
- name = ""..name..": "
- else
- name = ""
- end
- local o = ""
- if type(v) == "string" then
- table.insert(output,name..type(v).."("..v:len()..") \""..v.."\"")
- elseif type(v) == "userdata" then
- local elementType = "no valid MTA element"
- if isElement(v) then
- elementType = getElementType(v)
- end
- table.insert(output,name..type(v).."("..elementType..") \""..tostring(v).."\"")
- elseif type(v) == "table" then
- local count = 0
- for key,value in pairs(v) do
- count = count + 1
- end
- table.insert(output,name..type(v).."("..count..") \""..tostring(v).."\"")
- if verbose and count > 0 and (depth == nil or depth > 0) then
- table.insert(output,"\t{")
- for key,value in pairs(v) do
- local newModifiers = "-s"
- if depth == nil then
- newModifiers = "-sv"
- elseif depth > 1 then
- local newDepth = depth - 1
- newModifiers = "-svd"..newDepth
- end
- local keyString, keyTable = self:var_dump(newModifiers,key)
- local valueString, valueTable = self:var_dump(newModifiers,value)
- if #keyTable == 1 and #valueTable == 1 then
- table.insert(output,indentation.."["..keyString.."]\t=>\t"..valueString)
- elseif #keyTable == 1 then
- table.insert(output,indentation.."["..keyString.."]\t=>")
- for k,v in ipairs(valueTable) do
- table.insert(output,indentation..v)
- end
- elseif #valueTable == 1 then
- for k,v in ipairs(keyTable) do
- if k == 1 then
- table.insert(output,indentation.."["..v)
- elseif k == #keyTable then
- table.insert(output,indentation..v.."]")
- else
- table.insert(output,indentation..v)
- end
- end
- table.insert(output,indentation.."\t=>\t"..valueString)
- else
- for k,v in ipairs(keyTable) do
- if k == 1 then
- table.insert(output,indentation.."["..v)
- elseif k == #keyTable then
- table.insert(output,indentation..v.."]")
- else
- table.insert(output,indentation..v)
- end
- end
- for k,v in ipairs(valueTable) do
- if k == 1 then
- table.insert(output,indentation.." => "..v)
- else
- table.insert(output,indentation..v)
- end
- end
- end
- end
- table.insert(output,"\t}")
- end
- else
- table.insert(output,name..type(v).." \""..tostring(v).."\"")
- end
- name = nil
- end
- end
- local string = ""
- for k,v in ipairs(output) do
- if outputDirectly then
- outputConsole(v)
- end
- string = string..v
- end
- return string, output
- end
- function of.debug:check(funcname, ...)
- local arg = {...}
- if (type(funcname) ~= "string") then
- error("Argument type mismatch at 'Check' ('funcname'). Expected 'string', got '"..type(funcname).."'.", 2)
- end
- if (#arg % 3 > 0) then
- error("Argument number mismatch at 'Check'. Expected #arg % 3 to be 0, but it is "..(#arg % 3)..".", 2)
- end
- for i=1, #arg-2, 3 do
- if (type(arg[i]) ~= "string" and type(arg[i]) ~= "table") then
- error("Argument type mismatch at 'Check' (arg #"..i.."). Expected 'string' or 'table', got '"..type(arg[i]).."'.", 2)
- elseif (type(arg[i+2]) ~= "string") then
- error("Argument type mismatch at 'Check' (arg #"..(i+2).."). Expected 'string', got '"..type(arg[i+2]).."'.", 2)
- end
- if (type(arg[i]) == "table") then
- local aType = type(arg[i+1])
- for _, pType in next, arg[i] do
- if (aType == pType) then
- aType = nil
- break
- end
- end
- if (aType) then
- error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..table.concat(arg[i], "' or '").."', got '"..aType.."'.", 3)
- end
- elseif (type(arg[i+1]) ~= arg[i]) then
- error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..arg[i].."', got '"..type(arg[i+1]).."'.", 3)
- end
- end
- end
- function of.debug:print(a, b, c, d, e)
- return outputDebugScript(a,b,c,d,e)
- end
- of.store = of.class:new();
- of.store.storage = {};
- function of.store:Set(ident, val)
- if ident == nil or val == nil then
- return false
- end
- self.storage[ident] = val
- return true
- end
- function of.store:Get(ident)
- if self.storage[ident] == nil then
- return false
- end
- return self.storage[ident]
- end
- function of.store:List()
- return self.storage
- end
- Event = of.class:new()
- function Event:Create(name, multi)
- local object = of.class:new(self)
- object.nam = name
- if multi == nil then
- multi = true
- end
- if addEvent(name, multi) then
- return object;
- end
- return false;
- end
- function Event:Use(name)
- local object = of.class:new(self)
- object.nam = name
- return object;
- end
- function Event:Handle(func, to)
- if not to then
- to = getRootElement();
- end
- return addEventHandler(self.nam, to, func)
- end
- function Event:Trigger(src, ...)
- local t = pack(...)
- return triggerEvent(self.nam, src, unpack(t));
- end
- function Event:SideTrigger(src, ...)
- if isClient() then
- return triggerServerEvent(self.nam, src, ...);
- else
- if type(src) == "userdata" then
- return triggerClientEvent(src, self.nam, ...);
- else
- return triggerClientEvent(self.nam, src, ...)
- end
- end
- end
- Timer = of.class:new()
- function Timer:Create(func, time, times, ...)
- local object = of.class:new(self)
- object.handler = setTimer(func, time, times, ...)
- if object.handler then
- return object;
- end
- return false;
- end
- function Timer:Reset()
- return resetTimer(self.handler);
- end
- function Timer:Kill()
- return killTimer(self.handler)
- end
- Command = of.class:new()
- function Command:Create(name, func, restricted, sensitive)
- local object = of.class:new(self)
- object.name = name
- if addCommandHandler(name, func, restricted, sensitive) then
- return object;
- end
- return false;
- end
- function Command:Use(name)
- local object = of.class:new(self)
- object.name = name
- return object;
- end
- function Command:Remove(handler)
- return removeCommandHandler(self.name, handler);
- end
- function Command:Execute(arg1, arg2)
- if isServer() then return executeCommandHandler(self.name, arg1, arg2); end
- return executeCommandHandler(self.name, arg1)
- end
- XML = of.class:new()
- function XML:Load( filename )
- local xml = {
- rootname,
- filename = filename,
- nodes = { }
- };
- local xmlroot = xmlLoadFile( filename );
- if ( not xmlroot ) then return false end
- xml.rootname = xmlNodeGetName( xmlroot );
- local ParseNodes = function( nodesTable )
- if ( nodesTable and type( nodesTable ) == "table" ) and #nodesTable > 0 then
- local nodes = { };
- for i = 1, #nodesTable do
- table.insert( nodes, XMLNode:new( nodesTable[ i ] ) );
- end
- return nodes;
- end
- return nil;
- end
- local children = xmlNodeGetChildren( xmlroot );
- if children then
- xml.nodes = ParseNodes( children );
- end
- xmlUnloadFile( xmlroot );
- local addNode;
- addNode = function( node, parent )
- local tempnode = xmlCreateChild( parent, node.Name );
- for name, value in pairs( node.Attributes ) do
- xmlNodeSetAttribute( tempnode, name, value );
- end
- if node.Value and type( node.Value ) == "string" and #node.Value > 0 then
- xmlNodeSetValue( tempnode, node.Value );
- end
- if node.nodes ~= nil then
- for i = 1, #node.nodes do
- addNode( node.nodes[ i ], tempnode );
- end
- end
- end
- local save = function( filename )
- filename = ( type( filename ) == "string" ) and filename or xml.filename;
- local file = xmlCreateFile( filename, xml.rootname );
- if file then
- for i = 1, #xml.nodes do
- addNode( xml.nodes[ i ], file );
- end
- return xmlSaveFile( file );
- end
- return false;
- end
- return {
- Rootname = xml.rootname,
- Filename = xml.filename,
- Nodes = xml.nodes,
- Save = save,
- }
- end
- function wrongClassUsageMessage( object, method )
- outputDebugString( "calling method incorrectly ( "..object.."Obj."..method.."() )! use "..object.."Obj:"..method.."()", 1 );
- end
- XMLNode = of.class:new()
- function XMLNode:new( xmlnode )
- local node = of.class:new(self)
- local ParseNodes = function( nodesTable )
- if ( nodesTable and type( nodesTable ) == "table" ) and #nodesTable > 0 then
- local nodes = { };
- for i = 1, #nodesTable do
- table.insert( nodes, XMLNode:new( nodesTable[ i ] ) );
- end
- return nodes;
- end
- return nil;
- end
- if ( type( xmlnode ) == "userdata" ) then
- node.Name = xmlNodeGetName( xmlnode );
- node.Value = xmlNodeGetValue( xmlnode );
- node.Attributes = { };
- local attrbts = xmlNodeGetAttributes( xmlnode );
- if ( attrbts and table.size( attrbts ) > 0 ) then
- for name, value in pairs( attrbts ) do
- node.Attributes[ name ] = value;
- end
- end
- node.Nodes = ParseNodes( xmlNodeGetChildren( xmlnode ) );
- end
- return node;
- end
- function XMLNode:Attribute( name, value )
- if not self then wrongClassUsageMessage( "xmlNode", "Attribute" ); return end
- if ( name ) then
- if ( value ) then
- self.Attributes[ name ] = tostring( value );
- return true;
- else
- return self.Attributes[ name ];
- end
- end
- return false;
- end
- function XMLNode:Name( newname )
- if not self then wrongClassUsageMessage( "xmlNode", "Name" ); return end
- if ( newname ) then
- self.name = newname;
- return true;
- else
- return self.name;
- end
- end
- function XMLNode:Value( newvalue )
- if not self then wrongClassUsageMessage( "xmlNode", "Value" ); return end
- if ( newname ) then
- self.value = newvalue;
- return true;
- else
- return self.value;
- end
- end
- MySQL = of.class:new();
- function MySQL:Connect(host, user, pass, db, reconnect, socket)
- local object = of.class:new(self);
- object.connection = mysql_connect(host, user, pass, db, 3306, socket);
- if reconnect == true then
- object.reconnect = true;
- end
- object.host = host;
- object.user = user;
- object.pass = pass;
- object.db = db;
- if object.connection then
- return object;
- end
- outputDebugScript(mysql_error(object.connection))
- return false;
- end
- function MySQL:Reconnect()
- self.connection = mysql_connect(self.host, self.user, self.pass, self.db);
- if self:Ping() then
- return true;
- end
- return false;
- end
- function MySQL:Query(str)
- if self.reconnect then
- if self:Ping() ~= true then
- self:Reconnect()
- end
- end
- local object = of.class:new(MySQL.ret);
- object.query = mysql_query(self.connection, str);
- if object.query then
- return object;
- end
- outputDebugString(self:Error())
- return false;
- end
- function MySQL:Escape(str)
- if self.reconnect then
- if self:Ping() ~= true then
- self:Reconnect()
- end
- end
- return mysql_escape_string(self.connection, str);
- end
- function MySQL:Error()
- return mysql_error(self.connection)
- end
- function MySQL:Ping()
- return mysql_ping(self.connection)
- end
- function MySQL:Errno()
- return mysql_errno(self.connection)
- end
- MySQL.ret = of.class:new();
- function MySQL.ret:fetchAssoc()
- return mysql_fetch_assoc(self.query)
- end
- function MySQL.ret:freeResult()
- return mysql_free_result(self.query)
- end
- function MySQL.ret:numRows()
- return mysql_num_rows(self.query)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement