Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.17 KB | None | 0 0
  1.  
  2.  
  3.  
  4. function removeColorCoding ( name )
  5.     return type(name)=='string' and string.gsub ( name, '#%x%x%x%x%x%x', '' ) or name
  6. end
  7.  
  8. function isServer()
  9.                 return triggerClientEvent ~= nil    end
  10.  
  11.  
  12. function isClient()
  13.             return triggerServerEvent ~= nil    end
  14.            
  15. ResourceRoot = getResourceRootElement(getThisResource())
  16.  
  17.  
  18. of = {}
  19.  
  20. of.version = {}
  21.  
  22. of.version.type = "Work"
  23.  
  24. of.version.number = 3
  25.  
  26. of.version.string = "1.0 r18"
  27.  
  28.  
  29. of.class = {}
  30. of.class.__index = of.class;
  31.  
  32.  
  33. function of.class:new( inherit, elementData, element )
  34.     local object = { element = element }
  35.     object.__index = object
  36.    
  37.     setmetatable(object, inherit)
  38.    
  39.     if inherit then
  40.         object.__index = inherit
  41.     end
  42.    
  43.    
  44.     if elementData == true then
  45.         object.data = {}
  46.         setmetatable(object.data, { __index = function(w) return getElementData(self.element, w) end, __newindex = function(w,x,z) return setElementData(self.element, w, z) end })
  47.     end
  48.    
  49.     return object;
  50. end
  51.  
  52.  
  53. of.debug = of.class:new()
  54.  
  55.  
  56. function of.debug:var_dump(...)
  57.         local verbose = false
  58.     local firstLevel = true
  59.     local outputDirectly = true
  60.     local noNames = false
  61.     local indentation = "\t\t\t\t\t\t"
  62.     local depth = nil
  63.  
  64.     local name = nil
  65.     local output = {}
  66.     for k,v in ipairs(arg) do
  67.                 if type(v) == "string" and k < #arg and v:sub(1,1) == "-" then
  68.             local modifiers = v:sub(2)
  69.             if modifiers:find("v") ~= nil then
  70.                 verbose = true
  71.             end
  72.             if modifiers:find("s") ~= nil then
  73.                 outputDirectly = false
  74.             end
  75.             if modifiers:find("n") ~= nil then
  76.                 verbose = false
  77.             end
  78.             if modifiers:find("u") ~= nil then
  79.                 noNames = true
  80.             end
  81.             local s,e = modifiers:find("d%d+")
  82.             if s ~= nil then
  83.                 depth = tonumber(string.sub(modifiers,s+1,e))
  84.             end
  85.                 elseif type(v) == "string" and k < #arg and name == nil and not noNames then
  86.             name = v
  87.         else
  88.             if name ~= nil then
  89.                 name = ""..name..": "
  90.             else
  91.                 name = ""
  92.             end
  93.  
  94.             local o = ""
  95.             if type(v) == "string" then
  96.                 table.insert(output,name..type(v).."("..v:len()..") \""..v.."\"")
  97.             elseif type(v) == "userdata" then
  98.                 local elementType = "no valid MTA element"
  99.                 if isElement(v) then
  100.                     elementType = getElementType(v)
  101.                 end
  102.                 table.insert(output,name..type(v).."("..elementType..") \""..tostring(v).."\"")
  103.             elseif type(v) == "table" then
  104.                 local count = 0
  105.                 for key,value in pairs(v) do
  106.                     count = count + 1
  107.                 end
  108.                 table.insert(output,name..type(v).."("..count..") \""..tostring(v).."\"")
  109.                 if verbose and count > 0 and (depth == nil or depth > 0) then
  110.                     table.insert(output,"\t{")
  111.                     for key,value in pairs(v) do
  112.                                                 local newModifiers = "-s"
  113.                         if depth == nil then
  114.                             newModifiers = "-sv"
  115.                         elseif  depth > 1 then
  116.                             local newDepth = depth - 1
  117.                             newModifiers = "-svd"..newDepth
  118.                         end
  119.                         local keyString, keyTable = self:var_dump(newModifiers,key)
  120.                         local valueString, valueTable = self:var_dump(newModifiers,value)
  121.  
  122.                         if #keyTable == 1 and #valueTable == 1 then
  123.                             table.insert(output,indentation.."["..keyString.."]\t=>\t"..valueString)
  124.                         elseif #keyTable == 1 then
  125.                             table.insert(output,indentation.."["..keyString.."]\t=>")
  126.                             for k,v in ipairs(valueTable) do
  127.                                 table.insert(output,indentation..v)
  128.                             end
  129.                         elseif #valueTable == 1 then
  130.                             for k,v in ipairs(keyTable) do
  131.                                 if k == 1 then
  132.                                     table.insert(output,indentation.."["..v)
  133.                                 elseif k == #keyTable then
  134.                                     table.insert(output,indentation..v.."]")
  135.                                 else
  136.                                     table.insert(output,indentation..v)
  137.                                 end
  138.                             end
  139.                             table.insert(output,indentation.."\t=>\t"..valueString)
  140.                         else
  141.                             for k,v in ipairs(keyTable) do
  142.                                 if k == 1 then
  143.                                     table.insert(output,indentation.."["..v)
  144.                                 elseif k == #keyTable then
  145.                                     table.insert(output,indentation..v.."]")
  146.                                 else
  147.                                     table.insert(output,indentation..v)
  148.                                 end
  149.                             end
  150.                             for k,v in ipairs(valueTable) do
  151.                                 if k == 1 then
  152.                                     table.insert(output,indentation.." => "..v)
  153.                                 else
  154.                                     table.insert(output,indentation..v)
  155.                                 end
  156.                             end
  157.                         end
  158.                     end
  159.                     table.insert(output,"\t}")
  160.                 end
  161.             else
  162.                 table.insert(output,name..type(v).." \""..tostring(v).."\"")
  163.             end
  164.             name = nil
  165.         end
  166.     end
  167.     local string = ""
  168.     for k,v in ipairs(output) do
  169.         if outputDirectly then
  170.             outputConsole(v)
  171.         end
  172.         string = string..v
  173.     end
  174.     return string, output
  175. end
  176.  
  177. function of.debug:check(funcname, ...)
  178.     local arg = {...}
  179.  
  180.     if (type(funcname) ~= "string") then
  181.         error("Argument type mismatch at 'Check' ('funcname'). Expected 'string', got '"..type(funcname).."'.", 2)
  182.     end
  183.     if (#arg % 3 > 0) then
  184.         error("Argument number mismatch at 'Check'. Expected #arg % 3 to be 0, but it is "..(#arg % 3)..".", 2)
  185.     end
  186.  
  187.     for i=1, #arg-2, 3 do
  188.         if (type(arg[i]) ~= "string" and type(arg[i]) ~= "table") then
  189.             error("Argument type mismatch at 'Check' (arg #"..i.."). Expected 'string' or 'table', got '"..type(arg[i]).."'.", 2)
  190.         elseif (type(arg[i+2]) ~= "string") then
  191.             error("Argument type mismatch at 'Check' (arg #"..(i+2).."). Expected 'string', got '"..type(arg[i+2]).."'.", 2)
  192.         end
  193.  
  194.         if (type(arg[i]) == "table") then
  195.             local aType = type(arg[i+1])
  196.             for _, pType in next, arg[i] do
  197.                 if (aType == pType) then
  198.                     aType = nil
  199.                     break
  200.                 end
  201.             end
  202.             if (aType) then
  203.                 error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..table.concat(arg[i], "' or '").."', got '"..aType.."'.", 3)
  204.             end
  205.         elseif (type(arg[i+1]) ~= arg[i]) then
  206.             error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..arg[i].."', got '"..type(arg[i+1]).."'.", 3)
  207.         end
  208.     end
  209. end
  210.  
  211.  
  212. function of.debug:print(a, b, c, d, e)
  213.     return outputDebugScript(a,b,c,d,e)
  214. end
  215.  
  216.  
  217. of.store = of.class:new();
  218. of.store.storage = {};
  219.  
  220. function of.store:Set(ident, val)
  221.     if ident == nil or val == nil then
  222.         return false
  223.     end
  224.    
  225.     self.storage[ident] = val
  226.     return true
  227. end
  228.  
  229. function of.store:Get(ident)
  230.     if self.storage[ident] == nil then
  231.         return false
  232.     end
  233.     return self.storage[ident]
  234. end
  235.  
  236. function of.store:List()
  237.     return self.storage
  238. end
  239.  
  240.  
  241. Event = of.class:new()
  242.  
  243. function Event:Create(name, multi)
  244.     local object = of.class:new(self)
  245.     object.nam = name
  246.    
  247.     if multi == nil then
  248.         multi = true
  249.     end
  250.    
  251.     if addEvent(name, multi) then
  252.         return object; 
  253.     end
  254.     return false;
  255. end
  256.  
  257. function Event:Use(name)
  258.     local object = of.class:new(self)
  259.     object.nam = name
  260.     return object; 
  261. end
  262.  
  263. function Event:Handle(func, to)
  264.     if not to then
  265.         to = getRootElement();
  266.     end
  267.  
  268.     return addEventHandler(self.nam, to, func)
  269. end
  270.  
  271.  
  272. function Event:Trigger(src, ...)
  273.     local t = pack(...)
  274.     return triggerEvent(self.nam, src, unpack(t));
  275. end
  276.  
  277.  
  278. function Event:SideTrigger(src, ...)
  279.     if isClient() then
  280.         return triggerServerEvent(self.nam, src, ...);
  281.     else
  282.         if type(src) == "userdata" then
  283.             return triggerClientEvent(src, self.nam, ...);
  284.         else
  285.             return triggerClientEvent(self.nam, src, ...)
  286.         end
  287.     end
  288. end
  289.  
  290. Timer = of.class:new()
  291.  
  292. function Timer:Create(func, time, times, ...)
  293.     local object = of.class:new(self)
  294.     object.handler = setTimer(func, time, times, ...)
  295.    
  296.     if object.handler then
  297.         return object; 
  298.     end
  299.     return false;
  300. end
  301.  
  302. function Timer:Reset()
  303.     return resetTimer(self.handler);
  304. end
  305.  
  306.  
  307. function Timer:Kill()
  308.     return killTimer(self.handler)
  309. end
  310.  
  311.  
  312. Command = of.class:new()
  313.  
  314. function Command:Create(name, func, restricted, sensitive)
  315.     local object = of.class:new(self)
  316.     object.name = name
  317.    
  318.     if addCommandHandler(name, func, restricted, sensitive) then
  319.         return object; 
  320.     end
  321.     return false;
  322. end
  323.  
  324.  
  325. function Command:Use(name)
  326.     local object = of.class:new(self)
  327.     object.name = name
  328.     return object; 
  329. end
  330.  
  331. function Command:Remove(handler)
  332.     return removeCommandHandler(self.name, handler);
  333. end
  334.  
  335. function Command:Execute(arg1, arg2)
  336.     if isServer() then return executeCommandHandler(self.name, arg1, arg2); end
  337.     return executeCommandHandler(self.name, arg1)
  338. end
  339.  
  340. XML = of.class:new()
  341.  
  342. function XML:Load( filename )
  343.     local xml = {
  344.         rootname,
  345.         filename = filename,
  346.         nodes = { }
  347.     };
  348.    
  349.     local xmlroot = xmlLoadFile( filename );
  350.     if ( not xmlroot ) then return false end
  351.    
  352.     xml.rootname = xmlNodeGetName( xmlroot );
  353.    
  354.     local ParseNodes = function( nodesTable )
  355.         if ( nodesTable and type( nodesTable ) == "table" ) and #nodesTable > 0 then
  356.             local nodes = { };
  357.             for i = 1, #nodesTable do
  358.                 table.insert( nodes, XMLNode:new( nodesTable[ i ] ) );
  359.             end
  360.             return nodes;
  361.         end
  362.         return nil;
  363.     end
  364.  
  365.     local children = xmlNodeGetChildren( xmlroot );
  366.     if children then
  367.         xml.nodes = ParseNodes( children );
  368.     end
  369.    
  370.     xmlUnloadFile( xmlroot );
  371.    
  372.     local addNode;
  373.     addNode = function( node, parent )
  374.         local tempnode = xmlCreateChild( parent, node.Name );
  375.         for name, value in pairs( node.Attributes ) do
  376.             xmlNodeSetAttribute( tempnode, name, value );
  377.         end
  378.         if node.Value and type( node.Value ) == "string" and #node.Value > 0 then
  379.             xmlNodeSetValue( tempnode, node.Value );
  380.         end
  381.         if node.nodes ~= nil then
  382.             for i = 1, #node.nodes do
  383.                 addNode( node.nodes[ i ], tempnode );
  384.             end
  385.         end
  386.     end
  387.  
  388.    
  389.    
  390.     local save = function( filename )
  391.         filename = ( type( filename ) == "string" ) and filename or xml.filename;
  392.         local file = xmlCreateFile( filename, xml.rootname );
  393.         if file then
  394.             for i = 1, #xml.nodes do
  395.                 addNode( xml.nodes[ i ], file );
  396.             end
  397.             return xmlSaveFile( file );
  398.         end
  399.         return false;
  400.     end
  401.    
  402.     return {
  403.         Rootname = xml.rootname,
  404.         Filename = xml.filename,
  405.         Nodes = xml.nodes,
  406.         Save = save,
  407.     }
  408. end
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415. function wrongClassUsageMessage( object, method )
  416.     outputDebugString( "calling method incorrectly ( "..object.."Obj."..method.."() )! use "..object.."Obj:"..method.."()", 1 );
  417. end
  418.  
  419.  
  420. XMLNode = of.class:new()
  421.  
  422. function XMLNode:new( xmlnode )
  423.  
  424.        
  425.     local node = of.class:new(self)
  426.    
  427.     local ParseNodes = function( nodesTable )
  428.         if ( nodesTable and type( nodesTable ) == "table" ) and #nodesTable > 0 then
  429.             local nodes = { };
  430.             for i = 1, #nodesTable do
  431.                 table.insert( nodes, XMLNode:new( nodesTable[ i ] ) );
  432.             end
  433.             return nodes;
  434.         end
  435.         return nil;
  436.     end
  437.    
  438.    
  439.     if ( type( xmlnode ) == "userdata" ) then
  440.         node.Name = xmlNodeGetName( xmlnode );
  441.         node.Value = xmlNodeGetValue( xmlnode );
  442.         node.Attributes = { };
  443.         local attrbts = xmlNodeGetAttributes( xmlnode );
  444.         if ( attrbts and table.size( attrbts ) > 0 ) then
  445.             for name, value in pairs( attrbts ) do
  446.                 node.Attributes[ name ] = value;
  447.             end
  448.         end
  449.         node.Nodes = ParseNodes( xmlNodeGetChildren( xmlnode ) );
  450.     end
  451.    
  452.     return node;
  453. end
  454.  
  455. function XMLNode:Attribute( name, value )
  456.     if not self then wrongClassUsageMessage( "xmlNode", "Attribute" ); return end
  457.  
  458.     if ( name ) then
  459.         if ( value ) then
  460.             self.Attributes[ name ] = tostring( value );
  461.             return true;
  462.         else
  463.             return self.Attributes[ name ];
  464.         end
  465.     end
  466.     return false;
  467. end
  468.  
  469. function XMLNode:Name( newname )
  470.     if not self then wrongClassUsageMessage( "xmlNode", "Name" ); return end
  471.    
  472.     if ( newname ) then
  473.         self.name = newname;
  474.         return true;
  475.     else
  476.         return self.name;
  477.     end
  478. end
  479.  
  480. function XMLNode:Value( newvalue )
  481.     if not self then wrongClassUsageMessage( "xmlNode", "Value" ); return end
  482.    
  483.     if ( newname ) then
  484.         self.value = newvalue;
  485.         return true;
  486.     else
  487.         return self.value;
  488.     end
  489. end
  490.  
  491.  
  492.  
  493. MySQL = of.class:new();
  494.  
  495. function MySQL:Connect(host, user, pass, db, reconnect, socket)
  496.     local object = of.class:new(self);
  497.     object.connection = mysql_connect(host, user, pass, db, 3306, socket);
  498.     if reconnect == true then
  499.         object.reconnect = true;
  500.     end
  501.    
  502.     object.host = host;
  503.     object.user = user;
  504.     object.pass = pass;
  505.     object.db = db;
  506.  
  507.     if object.connection then
  508.         return object; 
  509.     end
  510.    
  511.     outputDebugScript(mysql_error(object.connection))
  512.     return false;
  513. end
  514.  
  515. function MySQL:Reconnect()
  516.     self.connection = mysql_connect(self.host, self.user, self.pass, self.db);
  517.     if self:Ping() then
  518.         return true;
  519.     end
  520.     return false;
  521. end
  522.  
  523. function MySQL:Query(str)
  524.     if self.reconnect then
  525.         if self:Ping() ~= true then
  526.             self:Reconnect()
  527.         end
  528.     end
  529.    
  530.     local object = of.class:new(MySQL.ret);
  531.     object.query = mysql_query(self.connection, str);
  532.    
  533.     if object.query then
  534.         return object;
  535.     end
  536.    
  537.     outputDebugString(self:Error())
  538.     return false;
  539. end
  540.  
  541. function MySQL:Escape(str)
  542.     if self.reconnect then
  543.         if self:Ping() ~= true then
  544.             self:Reconnect()
  545.         end
  546.     end
  547.    
  548.     return mysql_escape_string(self.connection, str);
  549. end
  550.  
  551. function MySQL:Error()
  552.     return mysql_error(self.connection)
  553. end
  554.  
  555. function MySQL:Ping()
  556.     return mysql_ping(self.connection)
  557. end
  558.  
  559. function MySQL:Errno()
  560.     return mysql_errno(self.connection)
  561. end
  562.  
  563. MySQL.ret = of.class:new();
  564.  
  565. function MySQL.ret:fetchAssoc()
  566.     return mysql_fetch_assoc(self.query)
  567. end
  568.  
  569. function MySQL.ret:freeResult()
  570.     return mysql_free_result(self.query)
  571. end
  572.  
  573. function MySQL.ret:numRows()
  574.     return mysql_num_rows(self.query)
  575. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement