Advertisement
draugath

SystemNotes wrappers

Dec 28th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. declare("LoadSystemNotes2", function(id, character)
  2.     assert(id, "wrong number of arguments to 'LoadSystemNotes2'")
  3.     assert(type(id) == "number", "bad argument #1 to 'LoadSystemNotes2' (number expected, got "..type(id)..")")
  4.     if (character) then
  5.         assert(type(character) == "string", "bad argument #2 to 'LoadSystemNotes2' (string expected, got "..type(character)..")")
  6.     end
  7.  
  8.     local function loadnote(id, character)
  9.         local datafunc, err = loadfile("settings/"..character.."/system"..tostring(id).."notes.txt")
  10.         if (datafunc) then
  11.             local success, data = pcall(datafunc)
  12.             if (success and type(data) == "table" and data.data) then
  13.                 return data.data, data.timestamp
  14.             end
  15.         end
  16.     end
  17.  
  18.     local data, timestamp, latest_data
  19.     local latest_timestamp = 0
  20.     -- if character is provided, then attempt to get the requested data first
  21.     if (character) then
  22.         data = loadnote(id, character)
  23.         if (data) then return data end
  24.     end
  25.    
  26.     -- if no data or character is missing, scan all characters on the account for the latest data
  27.     for i = 1, 6 do
  28.         character = GetCharacterInfo(i)
  29.         if (character) then
  30.             data, timestamp = loadnote(id, character)
  31.             if (data and timestamp > latest_timestamp) then
  32.                 latest_data, latest_timestamp = data, timestamp
  33.             end
  34.         end
  35.     end
  36.     if (latest_data) then return latest_data end
  37.     return ""
  38. end)
  39.  
  40.  
  41. declare("SaveSystemNotes2", function(str, id)
  42.     assert(str and id, "wrong number of arguments to 'SaveSystemNotes2'")
  43.     assert(type(str) == "string", "bad argument #1 to 'SaveSystemNotes2' (string expected, got "..type(str)..")")
  44.     assert(type(id) == "number", "bad argument #2 to 'SaveSystemNotes2' (number expected, got "..type(id)..")")
  45.  
  46.     local tbl = {timestamp = os.time(), data = str}
  47.     SaveSystemNotes("return unspickle([["..spickle(tbl).."]])", id)
  48. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement