Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. //////////////////////////////////////
  2. //        GExtension (c) 2018       //
  3. //                                  //
  4. //  Created by Jakob 'ibot3' Müller //
  5. //                                  //
  6. //  You are not permitted to share, //
  7. //       trade, give away, sell     //
  8. //      or otherwise distribute     //
  9. //////////////////////////////////////
  10.  
  11. function GExtension:FromJson(json_string)
  12.     if not isstring(json_string) or string.Trim(json_string) == "" then
  13.         json_string = "{}"
  14.     end
  15.  
  16.     return util.JSONToTable(json_string)
  17. end
  18.  
  19. function GExtension:ToJson(table_from)
  20.     local json_string = "{}"
  21.  
  22.     if istable(table_from) then
  23.         json_string = util.TableToJSON(table_from)
  24.     end
  25.  
  26.     return json_string
  27. end
  28.  
  29. --[[function GExtension:FormatDate(unix)
  30.     return os.date(self.TimeFormat, tonumber(unix))
  31. end--]]
  32.  
  33. local charset = {}
  34.  
  35. -- qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890
  36. for i = 48,  57 do table.insert(charset, string.char(i)) end
  37. for i = 65,  90 do table.insert(charset, string.char(i)) end
  38. for i = 97, 122 do table.insert(charset, string.char(i)) end
  39.  
  40. function GExtension:RandomString(length)
  41.     math.randomseed(os.time())
  42.  
  43.     if length > 0 then
  44.         return self:RandomString(length - 1) .. charset[math.random(1, #charset)]
  45.     else
  46.         return ""
  47.     end
  48. end
  49.  
  50. function GExtension:FormatString(str, replaces)
  51.     if istable(replaces) then
  52.         for index, replace in pairs(replaces) do
  53.             str = string.Replace(tostring(str), "%" .. tostring(index) .. "%", tostring(replace))
  54.         end
  55.     end
  56.  
  57.     return str
  58. end
  59.  
  60. function GExtension:RemoveQuotes(str)
  61.     str = string.Replace(tostring(str), "'", "")
  62.     str = string.Replace(tostring(str), '"', "")
  63.     str = string.Replace(tostring(str), '`', "")
  64.     str = string.Replace(tostring(str), '\\', "")
  65.  
  66.     return str
  67. end
  68.  
  69. function  GExtension:HTMLSpecialChars(text)
  70.     text = string.Replace(text, "&", "&")
  71.     text = string.Replace(text, '"', """)
  72.     text = string.Replace(text, "'", "'")
  73.     text = string.Replace(text, "<", "&lt;")
  74.     text = string.Replace(text, ">", "&gt;")
  75.  
  76.     return text
  77. end
  78.  
  79. function GExtension:Hex2RGB(hex)
  80.     hex = hex:gsub("#","")
  81.     if(string.len(hex) == 3) then
  82.         return Color(tonumber("0x"..hex:sub(1,1)) * 17, tonumber("0x"..hex:sub(2,2)) * 17, tonumber("0x"..hex:sub(3,3)) * 17)
  83.     elseif(string.len(hex) == 6) then
  84.         return Color(tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)))
  85.     else
  86.         return Color(255,255,255)
  87.     end
  88. end
  89.  
  90. function GExtension:IsConsole(obj)
  91.     if type(obj) == "Entity" and (obj.EntIndex and obj:EntIndex() == 0) and !IsValid(obj) then
  92.         return true
  93.     else
  94.         return false
  95.     end
  96. end
  97.  
  98. function GExtension:CurrentTime()
  99.     return os.time()-- - (self.TimeOffset * 60 * 60)
  100. end
  101.  
  102. function GExtension:FormatDate(unix)
  103.     local offset = self.TimeOffset
  104.  
  105.     if offset == "" then
  106.         offset = "local"
  107.     end
  108.  
  109.     local format = "!" .. self.TimeFormat
  110.  
  111.     if offset == "local" then
  112.       local now = os.time()
  113.       local local_t = os.date("*t", now)
  114.       local utc_t = os.date("!*t", now)
  115.       local delta = (local_t.hour - utc_t.hour)*60 + (local_t.min - utc_t.min)
  116.       local h, m = math.modf( delta / 60)
  117.       offset = string.format("%+.4d", 100 * h + 60 * m)
  118.    end
  119.  
  120.    offset = offset or "GMT"
  121.    --format = string.gsub(format, "%%z", offset)
  122.  
  123.    if offset == "GMT" then
  124.       offset = "+0000"
  125.    end
  126.  
  127.    offset = string.gsub(offset, ":", "")
  128.  
  129.    local sign = 1
  130.  
  131.    if string.sub(offset, 1, 1) == "-" then
  132.       sign = -1
  133.       offset = string.sub(offset, 2)
  134.    elseif string.sub(offset, 1,1) == "+" then
  135.       offset = string.sub(offset, 2)
  136.    end
  137.  
  138.    offset = sign * (tonumber(string.sub(offset, 1, 2))*60 + tonumber(string.sub(offset, 3, 4)))*60
  139.  
  140.    return os.date(format, unix + offset)
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement