Advertisement
killer64

Untitled

Jul 4th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. function savePrefs(name,data) -- saves data to a file
  2. local file=io.open(name,"w")
  3. file:write(serialize(data))
  4. file:close()
  5. end
  6.  
  7. function readPrefs(name) -- reads data from a file
  8. local file=fs.open(name,"r")
  9. if not file then return false end
  10. local dat=unserialize(file.readAll())
  11. file.close()
  12. return dat
  13. end
  14.  
  15. -- base converter
  16. -- usage:
  17. -- bcv(number,charlist,returnTable?)
  18. function bcv(N,C,TF)
  19. if not C then
  20. return string.format("%X",N) -- default to hex
  21. end
  22. local Str=""
  23. local Tbl={}
  24. repeat
  25. local D=(N%(#C))+1
  26. N=math.floor(N/(#C))
  27. Str=string.sub(C,D,D)..Str
  28. table.insert(Tbl,1,D)
  29. until N==0
  30. if TF then
  31. return Tbl
  32. end
  33. return Str
  34. end
  35.  
  36. function serializeImpl(t) -- converts anything into a string
  37. local sType = type(t)
  38. if sType == "table" then
  39. local lstcnt=0
  40. for k,v in pairs(t) do
  41. lstcnt = lstcnt + 1
  42. end
  43. local result = "{"
  44. local aset=1
  45. local comma=false
  46. for k,v in pairs(t) do
  47. comma=true
  48. if k==aset then
  49. result = result..serializeImpl(v)..","
  50. aset=aset+1
  51. else
  52. result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  53. end
  54. end
  55. if comma then
  56. result=string.sub(result,1,-2)
  57. end
  58. result = result.."}"
  59. return result
  60. elseif sType == "string" then
  61. return string.format("%q",t) -- improved from textutils
  62. elseif sType == "number" or sType == "boolean" or sType == "nil" then
  63. return tostring(t)
  64. elseif sType == "function" then -- function i added
  65. local status,data=pcall(string.dump,t) -- convert the function into a string
  66. if status then
  67. return 'func('..string.format("%q",data)..')' -- format it so it dosent screw up syntax
  68. else
  69. error()
  70. end
  71. else
  72. error()
  73. end
  74. end
  75.  
  76. function split(T,func) -- splits a table
  77. if func then
  78. T=func(T) -- advanced function
  79. end
  80. local Out={}
  81. if type(T)=="table" then
  82. for k,v in pairs(T) do
  83. Out[split(k)]=split(v) -- set the values for the new table
  84. end
  85. else
  86. Out=T
  87. end
  88. return Out
  89. end
  90.  
  91. function serialize( t ) -- TODO: combine with serializeImpl
  92. t=split(t)
  93. return serializeImpl( t, tTracking )
  94. end
  95.  
  96. function unserialize( s ) -- converts a string back into its original form
  97. if type(s)~="string" then
  98. error("String exepcted. got "..type(s),2)
  99. end
  100. local func, e = loadstring( "return "..s, "serialize" )
  101. local funcs={} -- a table to store all the functions generated by f() (for securety)
  102. if not func then
  103. error("Invalid string.")
  104. end
  105. setfenv( func, { -- make sure nothing can be called within the function
  106. func=function(S) -- puts function requests into the funcs table
  107. local new={}
  108. funcs[new]=S
  109. return new
  110. end,
  111. })
  112. return split(func(),function(val) -- apply functions if any
  113. if funcs[val] then
  114. return loadstring(funcs[val])
  115. else
  116. return val
  117. end
  118. end)
  119. end
  120.  
  121. function zfill(N) -- ensures a hax number has 2 digits
  122. N=bcv(N)
  123. Zs=""
  124. if #N==1 then
  125. Zs="0"
  126. end
  127. return Zs..N
  128. end
  129.  
  130. function sure(N,n) -- make sure there is no errors when decoding a hex stream
  131. if (l2-n)<1 then N="0" end
  132. return N
  133. end
  134.  
  135. function hexToStr(S) -- converts a hex stream back into a character
  136. Out=""
  137. for l1=1,#S,2 do
  138. l2=(#S-l1)+1
  139. CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
  140. Out=string.char(CNum)..Out
  141. end
  142. return Out
  143. end
  144.  
  145. function strToHex(S) -- converts a string into a hex stream (most usefull when you dont want a string to screw up formatting)
  146. local O=""
  147. for char in string.gmatch(S,".") do
  148. O=O..zfill(string.byte(char))
  149. end
  150. return O
  151. end
  152.  
  153. function compress( ... ) -- serializes all of its arguments into a table and catches errors
  154. local tArgs={ ... }
  155. local E,D=pcall(infutil.serialize,tArgs)
  156. if E then
  157. return D
  158. else
  159. return false
  160. end
  161. end
  162.  
  163. function decompress(S) -- convert a serialized table into a normal one and catches errors
  164. local E,D,C=pcall(unserialize,S)
  165. if C or not E then
  166. return false
  167. else
  168. return type(D)=="table" and D
  169. end
  170. end
  171.  
  172. function rev(T) -- reverses a tables indexes
  173. local oT={}
  174. for k,v in pairs(T) do
  175. oT[(#T-k)+1]=v
  176. end
  177. return oT
  178. end
  179.  
  180. function receive(ID,nTimeout) -- receive a message from a specific id
  181. local timer = nil
  182. if nTimeout then
  183. timer = os.startTimer( nTimeout )
  184. end
  185. while true do
  186. local e, p1, p2, p3 = os.pullEvent()
  187. if e == "rednet_message" and p1 == ID then
  188. return p2
  189. elseif e == "timer" and p1 == timer then
  190. return
  191. end
  192. end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement