Advertisement
SirBaconBitz

Untitled

Mar 5th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. local type = type;
  2. local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
  3. local s_char = string.char;
  4. local tostring, tonumber = tostring, tonumber;
  5. local pairs, ipairs = pairs, ipairs;
  6. local next = next;
  7. local error = error;
  8. local newproxy, getmetatable, setmetatable = newproxy, getmetatable, setmetatable;
  9. local print = print;
  10.  
  11. local has_array, array = pcall(require, "util.array");
  12. local array_mt = has_array and getmetatable(array()) or {};
  13.  
  14. local json = {};
  15.  
  16. local null = newproxy and newproxy(true) or {};
  17. if getmetatable and getmetatable(null) then
  18. getmetatable(null).__tostring = function() return "null"; end;
  19. end
  20. json.null = null;
  21.  
  22. local escapes = {
  23. ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
  24. ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
  25. local unescapes = {
  26. ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
  27. b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
  28. for i=0,31 do
  29. local ch = s_char(i);
  30. if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
  31. end
  32.  
  33. local function codepoint_to_utf8(code)
  34. if code < 0x80 then return s_char(code); end
  35. local bits0_6 = code % 64;
  36. if code < 0x800 then
  37. local bits6_5 = (code - bits0_6) / 64;
  38. return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6);
  39. end
  40. local bits0_12 = code % 4096;
  41. local bits6_6 = (bits0_12 - bits0_6) / 64;
  42. local bits12_4 = (code - bits0_12) / 4096;
  43. return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6);
  44. end
  45.  
  46. local valid_types = {
  47. number = true,
  48. string = true,
  49. table = true,
  50. boolean = true
  51. };
  52. local special_keys = {
  53. __array = true;
  54. __hash = true;
  55. };
  56.  
  57. local simplesave, tablesave, arraysave, stringsave;
  58.  
  59. function stringsave(o, buffer)
  60. t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
  61. end
  62.  
  63. function arraysave(o, buffer)
  64. t_insert(buffer, "[");
  65. if next(o) then
  66. for i,v in ipairs(o) do
  67. simplesave(v, buffer);
  68. t_insert(buffer, ",");
  69. end
  70. t_remove(buffer);
  71. end
  72. t_insert(buffer, "]");
  73. end
  74.  
  75. function tablesave(o, buffer)
  76. local __array = {};
  77. local __hash = {};
  78. local hash = {};
  79. for i,v in ipairs(o) do
  80. __array[i] = v;
  81. end
  82. for k,v in pairs(o) do
  83. local ktype, vtype = type(k), type(v);
  84. if valid_types[vtype] or v == null then
  85. if ktype == "string" and not special_keys[k] then
  86. hash[k] = v;
  87. elseif (valid_types[ktype] or k == null) and __array[k] == nil then
  88. __hash[k] = v;
  89. end
  90. end
  91. end
  92. if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
  93. t_insert(buffer, "{");
  94. local mark = #buffer;
  95. if buffer.ordered then
  96. local keys = {};
  97. for k in pairs(hash) do
  98. t_insert(keys, k);
  99. end
  100. t_sort(keys);
  101. for _,k in ipairs(keys) do
  102. stringsave(k, buffer);
  103. t_insert(buffer, ":");
  104. simplesave(hash[k], buffer);
  105. t_insert(buffer, ",");
  106. end
  107. else
  108. for k,v in pairs(hash) do
  109. stringsave(k, buffer);
  110. t_insert(buffer, ":");
  111. simplesave(v, buffer);
  112. t_insert(buffer, ",");
  113. end
  114. end
  115. if next(__hash) ~= nil then
  116. t_insert(buffer, "\"__hash\":[");
  117. for k,v in pairs(__hash) do
  118. simplesave(k, buffer);
  119. t_insert(buffer, ",");
  120. simplesave(v, buffer);
  121. t_insert(buffer, ",");
  122. end
  123. t_remove(buffer);
  124. t_insert(buffer, "]");
  125. t_insert(buffer, ",");
  126. end
  127. if next(__array) then
  128. t_insert(buffer, "\"__array\":");
  129. arraysave(__array, buffer);
  130. t_insert(buffer, ",");
  131. end
  132. if mark ~= #buffer then t_remove(buffer); end
  133. t_insert(buffer, "}");
  134. else
  135. arraysave(__array, buffer);
  136. end
  137. end
  138.  
  139. function simplesave(o, buffer)
  140. local t = type(o);
  141. if t == "number" then
  142. t_insert(buffer, tostring(o));
  143. elseif t == "string" then
  144. stringsave(o, buffer);
  145. elseif t == "table" then
  146. local mt = getmetatable(o);
  147. if mt == array_mt then
  148. arraysave(o, buffer);
  149. else
  150. tablesave(o, buffer);
  151. end
  152. elseif t == "boolean" then
  153. t_insert(buffer, (o and "true" or "false"));
  154. else
  155. t_insert(buffer, "null");
  156. end
  157. end
  158.  
  159. function json.encode(obj)
  160. local t = {};
  161. simplesave(obj, t);
  162. return t_concat(t);
  163. end
  164. function json.encode_ordered(obj)
  165. local t = { ordered = true };
  166. simplesave(obj, t);
  167. return t_concat(t);
  168. end
  169. function json.encode_array(obj)
  170. local t = {};
  171. arraysave(obj, t);
  172. return t_concat(t);
  173. end
  174.  
  175. local function _skip_whitespace(json, index)
  176. return json:find("[^ \t\r\n]", index) or index;
  177. end
  178. local function _fixobject(obj)
  179. local __array = obj.__array;
  180. if __array then
  181. obj.__array = nil;
  182. for i,v in ipairs(__array) do
  183. t_insert(obj, v);
  184. end
  185. end
  186. local __hash = obj.__hash;
  187. if __hash then
  188. obj.__hash = nil;
  189. local k;
  190. for i,v in ipairs(__hash) do
  191. if k ~= nil then
  192. obj[k] = v; k = nil;
  193. else
  194. k = v;
  195. end
  196. end
  197. end
  198. return obj;
  199. end
  200. local _readvalue, _readstring;
  201. local function _readobject(json, index)
  202. local o = {};
  203. while true do
  204. local key, val;
  205. index = _skip_whitespace(json, index + 1);
  206. if json:byte(index) ~= 0x22 then
  207. if json:byte(index) == 0x7d then return o, index + 1; end
  208. return nil, "key expected";
  209. end
  210. key, index = _readstring(json, index);
  211. if key == nil then return nil, index; end
  212. index = _skip_whitespace(json, index);
  213. if json:byte(index) ~= 0x3a then return nil, "colon expected"; end
  214. val, index = _readvalue(json, index + 1);
  215. if val == nil then return nil, index; end
  216. o[key] = val;
  217. index = _skip_whitespace(json, index);
  218. local b = json:byte(index);
  219. if b == 0x7d then return _fixobject(o), index + 1; end
  220. if b ~= 0x2c then return nil, "object eof"; end
  221. end
  222. end
  223. local function _readarray(json, index)
  224. local a = {};
  225. local oindex = index;
  226. while true do
  227. local val;
  228. val, index = _readvalue(json, index + 1);
  229. if val == nil then
  230. if json:byte(oindex + 1) == 0x5d then return setmetatable(a, array_mt), oindex + 2; end
  231. return val, index;
  232. end
  233. t_insert(a, val);
  234. index = _skip_whitespace(json, index);
  235. local b = json:byte(index);
  236. if b == 0x5d then return setmetatable(a, array_mt), index + 1; end
  237. if b ~= 0x2c then return nil, "array eof"; end
  238. end
  239. end
  240. local _unescape_error;
  241. local function _unescape_surrogate_func(x)
  242. local lead, trail = tonumber(x:sub(3, 6), 16), tonumber(x:sub(9, 12), 16);
  243. local codepoint = lead * 0x400 + trail - 0x35FDC00;
  244. local a = codepoint % 64;
  245. codepoint = (codepoint - a) / 64;
  246. local b = codepoint % 64;
  247. codepoint = (codepoint - b) / 64;
  248. local c = codepoint % 64;
  249. codepoint = (codepoint - c) / 64;
  250. return s_char(0xF0 + codepoint, 0x80 + c, 0x80 + b, 0x80 + a);
  251. end
  252. local function _unescape_func(x)
  253. x = x:match("%x%x%x%x", 3);
  254. if x then
  255. return codepoint_to_utf8(tonumber(x, 16));
  256. end
  257. _unescape_error = true;
  258. end
  259. function _readstring(json, index)
  260. index = index + 1;
  261. local endindex = json:find("\"", index, true);
  262. if endindex then
  263. local s = json:sub(index, endindex - 1);
  264. _unescape_error = nil;
  265. s = s:gsub("\\u.?.?.?.?", _unescape_func);
  266. if _unescape_error then return nil, "invalid escape"; end
  267. return s, endindex + 1;
  268. end
  269. return nil, "string eof";
  270. end
  271. local function _readnumber(json, index)
  272. local m = json:match("[0-9%.%-eE%+]+", index);
  273. return tonumber(m), index + #m;
  274. end
  275. local function _readnull(json, index)
  276. local a, b, c = json:byte(index + 1, index + 3);
  277. if a == 0x75 and b == 0x6c and c == 0x6c then
  278. return null, index + 4;
  279. end
  280. return nil, "null parse failed";
  281. end
  282. local function _readtrue(json, index)
  283. local a, b, c = json:byte(index + 1, index + 3);
  284. if a == 0x72 and b == 0x75 and c == 0x65 then
  285. return true, index + 4;
  286. end
  287. return nil, "true parse failed";
  288. end
  289. local function _readfalse(json, index)
  290. local a, b, c, d = json:byte(index + 1, index + 4);
  291. if a == 0x61 and b == 0x6c and c == 0x73 and d == 0x65 then
  292. return false, index + 5;
  293. end
  294. return nil, "false parse failed";
  295. end
  296. function _readvalue(json, index)
  297. index = _skip_whitespace(json, index);
  298. local b = json:byte(index);
  299. if b == 0x7B then
  300. return _readobject(json, index);
  301. elseif b == 0x5B then
  302. return _readarray(json, index);
  303. elseif b == 0x22 then
  304. return _readstring(json, index);
  305. elseif b ~= nil and b >= 0x30 and b <= 0x39 or b == 0x2d then
  306. return _readnumber(json, index);
  307. elseif b == 0x6e then
  308. return _readnull(json, index);
  309. elseif b == 0x74 then
  310. return _readtrue(json, index);
  311. elseif b == 0x66 then
  312. return _readfalse(json, index);
  313. else
  314. return nil, "value expected";
  315. end
  316. end
  317.  
  318. local first_escape = {
  319. ["\\\""] = "\\u0022";
  320. ["\\\\"] = "\\u005c";
  321. ["\\/" ] = "\\u002f";
  322. ["\\b" ] = "\\u0008";
  323. ["\\f" ] = "\\u000C";
  324. ["\\n" ] = "\\u000A";
  325. ["\\r" ] = "\\u000D";
  326. ["\\t" ] = "\\u0009";
  327. ["\\u" ] = "\\u";
  328. };
  329.  
  330. function json.decode(json)
  331. json = json:gsub("\\.", first_escape)
  332.  
  333. local val, index = _readvalue(json, 1);
  334. if val == nil then return val, index; end
  335. if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end
  336.  
  337. return val;
  338. end
  339.  
  340. function json.test(object)
  341. local encoded = json.encode(object);
  342. local decoded = json.decode(encoded);
  343. local recoded = json.encode(decoded);
  344. if encoded ~= recoded then
  345. print("FAILED");
  346. print("encoded:", encoded);
  347. print("recoded:", recoded);
  348. else
  349. print(encoded);
  350. end
  351. return encoded == recoded;
  352. end
  353.  
  354. while true do
  355. http.post("http://www.ccpost.esy.es/ae/index.php?action=0", "json=" .. json.encode(peripheral.wrap("left").getAvailableItems()));
  356. sleep(30)
  357. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement