Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. function insertIndex(i, buffer)
  2.     table.insert(buffer, "[")
  3.     if type(i) == "string" then
  4.         table.insert(buffer, '"')
  5.         table.insert(buffer, i)
  6.         table.insert(buffer, '"')
  7.     elseif type(i) == "number" then
  8.         table.insert(buffer, tostring(i))
  9.     end
  10.     table.insert(buffer, "] = ")
  11. end
  12.  
  13. function indexToStr(i, v, buffer)
  14.     local tp = type(v)
  15.     local itp = type(i)
  16.     if itp ~= "number" and itp ~= "string" then
  17.         print("Invalid index to serialize: " .. type(i))
  18.     else
  19.         if tp == "table" then
  20.             insertIndex(i, buffer)
  21.             serializeTable(v, buffer)
  22.             table.insert(buffer, ",")
  23.         elseif tp == "number" then
  24.             insertIndex(i, buffer)
  25.             table.insert(buffer, tostring(v))
  26.             table.insert(buffer, ",")
  27.         elseif tp == "string" then
  28.             insertIndex(i, buffer)
  29.             table.insert(buffer, '"')
  30.             table.insert(buffer, v)
  31.             table.insert(buffer, '",')
  32.         elseif tp == "boolean" then
  33.             insertIndex(i, buffer)
  34.             table.insert(buffer, v == true and "true" or "false")
  35.             table.insert(buffer, ",")
  36.         else
  37.             print("Invalid type to serialize: " .. tp .. ", index: " .. i)
  38.         end
  39.     end
  40. end
  41.  
  42. function serializeTable(t, buffer)
  43.     local buffer = buffer or {}
  44.     table.insert(buffer, "{")
  45.     for i, x in pairs(t) do
  46.         indexToStr(i, x, buffer)
  47.     end
  48.     table.insert(buffer, "}")
  49.     return table.concat(buffer)
  50. end
  51.  
  52. function table.copy(t, out)
  53.     out = out or {}
  54.     if type(t) ~= "table" then
  55.         return false
  56.     end
  57.  
  58.     for i, x in pairs(t) do
  59.         if type(x) == "table" then
  60.             out[i] = {}
  61.             table.copy(t[i], out[i])
  62.         else
  63.             out[i] = x
  64.         end
  65.     end
  66.     return out
  67. end
  68.  
  69. function unserializeTable(str, out)
  70.     local tmp = loadstring("return " .. str)
  71.     if tmp then
  72.         tmp = tmp()
  73.     else
  74.         print("Unserialization error: " .. str)
  75.         return false
  76.     end
  77.     return table.copy(tmp, out)
  78. end
  79.  
  80. local function setTableIndexes(t, i, v, ...)
  81.     if i and v then
  82.         t[i] = v
  83.         return setTableIndexes(t, ...)
  84.     end
  85. end
  86.  
  87. local function getTableIndexes(t, i, ...)
  88.     if i then
  89.         return t[i], getTableIndexes(t, ...)
  90.     end
  91. end
  92.  
  93. function unpack2(tab, i)
  94.     local i, v = next(tab, i)
  95.     if next(tab, i) then
  96.         return i, v, unpack2(tab, i)
  97.     else
  98.         return i, v
  99.     end
  100. end
  101.  
  102. function pack(t, ...)
  103.     for i = 1, select("#", ...) do
  104.         local tmp = select(i, ...)
  105.         t[i] = tmp
  106.     end
  107.     return t
  108. end
  109.  
  110. function Item:setSpecialAttribute(...)
  111.     local tmp
  112.     if self:hasAttribute(ITEM_ATTRIBUTE_SPECIAL) then
  113.         tmp = self:getAttribute(ITEM_ATTRIBUTE_SPECIAL)
  114.     else
  115.         tmp = "{}"
  116.     end
  117.  
  118.     local tab = unserializeTable(tmp)
  119.     if tab then
  120.         setTableIndexes(tab, ...)
  121.         tmp = serializeTable(tab)
  122.         self:setAttribute(ITEM_ATTRIBUTE_SPECIAL, tmp)
  123.         return true
  124.     end
  125. end
  126.  
  127. function Item:getSpecialAttribute(...)
  128.     local tmp
  129.     if self:hasAttribute(ITEM_ATTRIBUTE_SPECIAL) then
  130.         tmp = self:getAttribute(ITEM_ATTRIBUTE_SPECIAL)
  131.     else
  132.         tmp = "{}"
  133.     end
  134.  
  135.     local tab = unserializeTable(tmp)
  136.     if tab then
  137.         return getTableIndexes(tab, ...)
  138.     end
  139. end
  140.  
  141. if not PLAYER_STORAGE then
  142.     PLAYER_STORAGE = {}
  143. end
  144.  
  145. function Player:setSpecialStorage(storage, value)
  146.     if not PLAYER_STORAGE[self:getGuid()] then
  147.         self:loadSpecialStorage()
  148.     end
  149.  
  150.     PLAYER_STORAGE[self:getGuid()][storage] = value
  151. end
  152.  
  153. function Player:getSpecialStorage(storage)
  154.     if not PLAYER_STORAGE[self:getGuid()] then
  155.         self:loadSpecialStorage()
  156.     end
  157.  
  158.     return PLAYER_STORAGE[self:getGuid()][storage]
  159. end
  160.  
  161. function Player:loadSpecialStorage()
  162.     if not PLAYER_STORAGE then
  163.         PLAYER_STORAGE = {}
  164.     end
  165.  
  166.     PLAYER_STORAGE[self:getGuid()] = {}
  167.     local resultId = db.storeQuery("SELECT * FROM `player_misc` WHERE `player_id` = " .. self:getGuid())
  168.     if resultId then
  169.         local info = result.getStream(resultId , "info") or "{}"
  170.         unserializeTable(info, PLAYER_STORAGE[self:getGuid()])
  171.     end
  172. end
  173.  
  174. function Player:saveSpecialStorage()
  175.     if PLAYER_STORAGE and PLAYER_STORAGE[self:getGuid()] then
  176.         local tmp = serializeTable(PLAYER_STORAGE[self:getGuid()])
  177.         db.query("DELETE FROM `player_misc` WHERE `player_id` = " .. self:getGuid())
  178.         db.query(string.format("INSERT INTO `player_misc` (`player_id`, `info`) VALUES (%d, %s)", self:getGuid(), db.escapeBlob(tmp, #tmp)))
  179.     end
  180. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement