Advertisement
Handoncloud

Storage (abandoned)...

Feb 10th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.11 KB | None | 0 0
  1. --[====[
  2.  
  3. Storage
  4. * Maintainer: Profiver
  5. * Version: 1.4.0 (final)
  6.  
  7. --]====]
  8. do
  9. local Utils_CallbackFire,
  10. Utils_getTypeOf,
  11. Utils_getMetatableOf,
  12. Utils_isCallable,
  13. Utils_setMetatableOf,
  14. Utils_next,
  15. Utils_toNumber,
  16. Utils_toString,
  17. Uint_dumpUInt,
  18. Uint_parseUInt,
  19. Table_unpack,
  20. Bit32_band,
  21. Bit32_rshift,
  22. Math_max,
  23. String_bytes,
  24. String_char,
  25. String_find,
  26. String_sub,
  27. Storage_API,
  28. Storage_Messages,
  29. Storage_assertf,
  30. Storage_errorf,
  31. Storage_formatMsg,
  32. Class_createEmptyClass,
  33. Class_createInstanceOf,
  34. Class_isInstanceOf,
  35. Class_setConstructorOf,
  36. Class_setPrototypeOf,
  37. LUA_BOOLF,
  38. LUA_BOOLT,
  39. LUA_NUMBER,
  40. LUA_TABLE,
  41. LUA_STRING,
  42. LUA_NIL,
  43. Buffer_BufferScanner,
  44. Buffer_toBuffer,
  45. Public_File,
  46. Public_PlayerData,
  47. Storage_events,
  48. Storage_linkEvents;
  49.  
  50. Utils_getTypeOf = type;
  51. Utils_getMetatableOf = getmetatable;
  52. Utils_next = next;
  53. Utils_setMetatableOf = setmetatable;
  54. Utils_toNumber = tonumber;
  55. Utils_toString = tostring;
  56. Bit32_band = bit32.band;
  57. Bit32_rshift = bit32.rshift;
  58. Math_max = math.max;
  59. String_bytes = string.byte;
  60. String_char = string.char;
  61. String_find = string.find;
  62. String_sub = string.sub;
  63. Table_unpack = table.unpack;
  64.  
  65. LUA_BOOLF, LUA_BOOLT, LUA_NUMBER, LUA_TABLE, LUA_STRING, LUA_NIL = 0, 1, 2, 3, 4, 5;
  66.  
  67. function Uint_dumpUInt (n)
  68. -- Get range of bits
  69. local r, s = 0, n;
  70. while (s >= 0) do s = s - 0xFF; r = r + 8; end; r = Math_max(0, r - 8);
  71.  
  72. local b, bytes, i = 0, {}, 1;
  73. repeat
  74. bytes[i] = Bit32_band(Bit32_rshift(n, b), 0xFF);
  75. b, i = b + 8, i + 1;
  76. until b > r;
  77.  
  78. return Table_unpack(bytes);
  79. end
  80.  
  81. function UInt_parseUInt (s)
  82. local l, r = #s, 0;
  83. for i = 1, l do r = r + (String_bytes(s, i) * (16 ^ (l - i))) end;
  84. return r;
  85. end
  86.  
  87. function Class_createEmptyClass () return Utils_setMetatableOf({ protometa = {} }, {}) end
  88. function Class_createInstanceOf (Class) return Utils_setMetatableOf({}, Class.protometa) end
  89. function Class_isInstanceOf (Class, v) return Utils_getMetatableOf(v) == Class.protometa end
  90. function Class_setConstructorOf (Class, c) Utils_getMetatableOf(Class).__call = c end
  91. function Class_setPrototypeOf (Class, p) Class.protometa.__index = p end
  92.  
  93. function Buffer_toBuffer (v)
  94. if (v == nil) then return String_char(LUA_NIL) end;
  95. local tp = Utils_getTypeOf(v);
  96.  
  97. if (tp == 'number') then return String_char(LUA_NUMBER)..Utils_toString(v)..String_char(0) end;
  98. if (tp == 'boolean') then return String_char(v and LUA_BOOLT or LUA_BOOLF) end;
  99. if (tp == 'table') then
  100. local f = '';
  101. for k1, v1 in Utils_next, v do f = f..(Buffer_toBuffer(k1)..Buffer_toBuffer(v1)) end;
  102. return String_char(LUA_TABLE)..f..String_char(0x28);
  103. end
  104. if (tp == 'string') then
  105. local len = String_char(Uint_dumpUInt(#v));
  106. return String_char(LUA_STRING)..#len..len..v;
  107. end
  108.  
  109. Storage_errorf(Storage_Messages.UnsupportedLuaType);
  110. end
  111.  
  112. Buffer_BufferScanner = Class_createEmptyClass();
  113. Class_setPrototypeOf(Buffer_BufferScanner, {
  114. scan = function (self)
  115. -- self.src:byte(self.idx);
  116. local tp = String_bytes(self.src, self.idx);
  117. self.idx = self.idx + 1;
  118. if (tp <= LUA_BOOLT) then return tp == LUA_BOOLT end;
  119. if (tp == LUA_NUMBER) then
  120. local j = String_find(self.src, String_char(0), self.idx);
  121. local c = String_sub(self.src, self.idx, j - 1);
  122. self.idx = j + 1; return Utils_toNumber(c);
  123. end
  124. if (tp == LUA_TABLE) then
  125. local t = {};
  126. while (true) do
  127. local b = String_bytes(self.src, self.idx);
  128. if (b == 0x28) then self.idx = self.idx + 1; return t; else
  129. local k, v = self:scan(), self:scan(); t[k] = v;
  130. end
  131. end
  132. end
  133. if (tp == LUA_STRING) then
  134. local l = String_bytes(self.src, self.idx);
  135. local bi = self.idx + l;
  136. local len = UInt_parseUInt(String_sub(self.src, self.idx + 1, self.idx + l));
  137. local bj = bi + len;
  138. self.idx = bj + 1;
  139. return String_sub(self.src, bi, bj);
  140. end
  141. end
  142. });
  143. Class_setConstructorOf(Buffer_BufferScanner, function (C, src)
  144. local instance = Class_createInstanceOf(C);
  145. instance.idx = 1;
  146. instance.src = src;
  147. return instance;
  148. end);
  149.  
  150. Utils_CallbackFire = Class_createEmptyClass();
  151. Class_setPrototypeOf(Utils_CallbackFire, {
  152. _changeState = function (self, state, ...)
  153. self._vals = {...}; self._state = state; self:_fire();
  154. end,
  155.  
  156. _fire = function (self)
  157. local stck = self._cbs[self._state];
  158. local vals = self._vals;
  159. while (true) do
  160. local k = Utils_next(stck);
  161. if (not k) then break end;
  162. stck[k] = nil;
  163. if (Utils_isCallable(k)) then k(Table_unpack(vals)) end;
  164. end
  165. end,
  166.  
  167. action = function (self, onComplete, onError)
  168. local cbs = self._cbs;
  169. local state = self._state;
  170. if (onComplete) then
  171. if (state == 0) then onComplete(Table_unpack(self._vals)) end;
  172. local stck = cbs[0];
  173. stck[#stck + 1] = onComplete;
  174. end
  175. if (onError) then
  176. if (state == 1) then onError(Table_unpack(self._vals)) end;
  177. local stck = cbs[1];
  178. stck[#stck + 1] = onError;
  179. end
  180. end
  181. });
  182. Class_setConstructorOf(Utils_CallbackFire, function (C, exec)
  183. local instance = Class_createInstanceOf(C);
  184. instance._cbs = { [0] = {}, [1] = {} };
  185. instance._state = 2;
  186.  
  187. local function complete (...) instance:_changeState(0, ...); end
  188. local function fail (...) instance:_changeState(1, ...); end
  189.  
  190. exec(complete, fail);
  191. end);
  192.  
  193. Storage_Messages = {
  194. UnsupportedLuaType = 'value of type %1 cannot be stored'
  195. };
  196.  
  197. function Storage_assertf (test, msg, ...) if not test then Storage_errorf(msg, ...) end end
  198. function Storage_errorf (msg, ...) error(Storage_formatMsg(msg, ...)) end
  199.  
  200. function Storage_linkEvents ()
  201. local root = _G;
  202. for k, v in Utils_next, Storage_events do Storage_events[k] = v end;
  203. end
  204.  
  205. storage = {
  206. events = Storage_events,
  207. linkEvents = Storage_linkEvents,
  208. File = Public_File,
  209. PlayerData = Public_PlayerData
  210. };
  211.  
  212. function Utils_isCallable (v)
  213. if (Utils_getTypeOf(v) == 'function') then return true end;
  214. local t = Utils_getMetatableOf(v);
  215. return (not not t) and Utils_getTypeOf(t.__call) == 'function';
  216. end
  217.  
  218. print(
  219. next(Buffer_BufferScanner(
  220. Buffer_toBuffer{0, 'wowow'}
  221. ):scan())
  222. );
  223. end
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. --[====[
  232. Storage
  233. * Maintainer: Profiver.
  234. * Version: 1.2.0.
  235. ]====]
  236. do
  237. local library;
  238.  
  239. local API_STORAGE = 0;
  240. local VIRTUAL_STORAGE = 1;
  241.  
  242. local tfm_get_room = tfm.get.room;
  243. local room_playerlist = tfm.get.room.playerList;
  244.  
  245. local bit32_band = bit32.band;
  246. local bit32_rshift = bit32.rshift;
  247. local uint_dump;
  248. local uint_parse;
  249.  
  250. local BUFFER_BFALSE = 0;
  251. local BUFFER_BTRUE = 1;
  252. local BUFFER_NUMBER = 2;
  253. local BUFFER_TABLE = 3;
  254. local BUFFER_STRING = 4;
  255. local BUFFER_NIL = 5;
  256. local BUFFER_END_NUMBER = 0;
  257. local BUFFER_END_TABLE = 12;
  258. local buffer_advance;
  259. local buffer_parse;
  260. local buffer_posi;
  261. local buffer_scan;
  262. local buffer_tostring;
  263.  
  264. local class_super;
  265. local class_create;
  266. local class_instantiate;
  267. local class_isinstance;
  268. local class_setconstructor;
  269. local class_setprototype;
  270. local class_updatesuper;
  271.  
  272. local table_unpack = table.unpack;
  273.  
  274. local string_byte = string.byte;
  275. local string_char = string.char;
  276. local string_find = string.find;
  277. local string_gmatch = string.gmatch;
  278. local string_sub = string.sub;
  279.  
  280. local format;
  281.  
  282. local getmetatable = getmetatable;
  283. local setmetatable = setmetatable;
  284. local tonumber = tonumber;
  285. local tostring = tostring;
  286. local type = type;
  287. local next = next;
  288. local print = print;
  289. local math_max = math.max;
  290. local iscallable;
  291. local root = _G;
  292. local useapi;
  293.  
  294. local CallbackHandler;
  295. local PlayerData;
  296. local ModuleFile;
  297.  
  298. local Messages;
  299.  
  300. local events = {};
  301.  
  302. -- Class handling. Optionally includes inheritance,
  303. -- which is not needed this time.
  304.  
  305. -- class_create() creates a template class.
  306. function class_create ()
  307. local staticFields = { _proto = {} };
  308. return setmetatable(staticFields, {});
  309. end
  310.  
  311. -- class_instantiate() returns an instance of a class.
  312. function class_instantiate (Class)
  313. return setmetatable({}, Class._proto);
  314. end
  315.  
  316. -- class_setconstructor() sets the constructor function
  317. -- of a class. It's directly called in the class, i.e.:
  318. -- Class(...args)
  319. function class_setconstructor (Class, constructor)
  320. local staticMeta = getmetatable(Class);
  321. staticMeta.__call = constructor;
  322. end
  323.  
  324. -- class_super() constructs the superior class of a class.
  325. -- The superior class MUST return an instance, which will then
  326. -- be re-prototyped by the child class here and returned again.
  327. function class_super (Class,...)
  328. local staticMeta = getmetatable(Class);
  329. return setmetatable(staticMeta.__index(...), Class._proto);
  330. end
  331.  
  332. -- class_setprototype() sets the field base of a class, which is distributed
  333. -- to the instance metatable.
  334. -- This base is accessible by the instances of that class.
  335. function class_setprototype (Class, prototype)
  336. local protoMeta = Class._proto;
  337. protoMeta.__index = prototype;
  338.  
  339. local SuperClass = getmetatable(Class).__index;
  340.  
  341. if SuperClass then
  342. setmetatable(protoMeta.__index, SuperClass._proto);
  343. end
  344. end
  345.  
  346. -- class_updatesuper() sets the super class ("inherited class") of a class.
  347. -- The super class is accessible thru child class __index meta field.
  348. function class_updatesuper (ChildClass, SuperClass)
  349. -- Allows the child class to index the super class.
  350. local childStaticMeta = getmetatable(ChildClass);
  351. childStaticMeta.__index = SuperClass;
  352.  
  353. local childProtoMeta = ChildClass._proto;
  354. setmetatable(childProtoMeta.__index, SuperClass._proto);
  355. end
  356.  
  357.  
  358. -- class_isinstance() checks if a value is instance of a class.
  359. function class_isinstance (val, Class)
  360. local protoMeta = Class._proto;
  361. local valMeta = getmetatable(val);
  362. if not valMeta then return false end; -- skip
  363.  
  364. -- Go checking for the protoype of inherited classes too.
  365. while protoMeta do
  366. if protoMeta == valMeta then
  367. return true;
  368. end
  369. protoMeta = getmetatable(protoMeta.__index);
  370. protoMeta = protoMeta and protoMeta.__index;
  371. end
  372.  
  373. return false;
  374. end
  375.  
  376.  
  377. -- Buffer utils.
  378.  
  379. function buffer_tostring (val)
  380. if val == nil return string_char(BUFFER_NIL) end;
  381.  
  382. local tp = type(val);
  383.  
  384. if tp == 'number' then
  385. return string_char(BUFFER_NUMBER)..tostring(val)..
  386. string_char(BUFFER_END_NUMBER);
  387. end
  388.  
  389. if tp == 'string' then
  390. -- String resume:
  391. -- byte lengthBytes, uint length, byte[] bytes
  392. local bytelen = uint_dump(#val);
  393. return ((string_char(BUFFER_STRING)..#bytelen)..bytelen)..val;
  394. end
  395.  
  396. if tp == 'table' then
  397. local f = '';
  398. for k, v in next, val do
  399. local p = buffer_tostring(k)..buffer_tostring(v);
  400. f = f..p;
  401. end
  402. return string_char(BUFFER_TABLE)..f..
  403. string_char(BUFFER_END_TABLE);
  404. end
  405.  
  406. end
  407.  
  408.  
  409. PlayerData =
  410.  
  411.  
  412. -- State callback handler utility.
  413.  
  414. CallbackHandler = class_create();
  415. class_setprototype(CallbackHandler, {
  416. _execcallbacks = function (self)
  417. local stack = self._callbacks[self._state];
  418.  
  419. -- Call every callback and go removing them.
  420. while true do
  421. local k = next(stack);
  422. if not k then break end;
  423.  
  424. stack[k] = nil;
  425.  
  426. if iscallable(k) then
  427. k(table_unpack(self._values));
  428. end
  429. end
  430. end,
  431.  
  432. action = function (self, onComplete, onError)
  433. local callbacks = self._callbacks;
  434. local state = self._state;
  435.  
  436. if onComplete then
  437. if state == 0 then onComplete(table_unpack(self._values)) end;
  438. local stack = callbacks[0];
  439. stack[#stack + 1] = onComplete;
  440. end
  441. if onError then
  442. if state == 1 then onError(table_unpack(self._values)) end;
  443. local stack = callbacks[1];
  444. stack[#stack + 1] = onError;
  445. end
  446. end
  447. });
  448. class_setconstructor(CallbackHandler, function (C, executor)
  449. local instance = class_instantiate(C);
  450. instance._callbacks = { [0] = {}, [1] = {} };
  451. instance._state = 2;
  452.  
  453. local function complete (...)
  454. instance._values = {...};
  455. instance._state = 0;
  456. instance:_execcallbacks();
  457. end
  458.  
  459. local function finisherror (...)
  460. instance._values = {...};
  461. instance._state = 1;
  462. instance:_execcallbacks();
  463. end
  464.  
  465. executor(complete, finisherror);
  466. return instance;
  467. end);
  468.  
  469.  
  470. -- Unsigned integer composition and decomposition utils.
  471.  
  472. function uint_parse (s)
  473. local bytelen = #s;
  474. local ret = 0;
  475.  
  476. for i = 1, bytelen do
  477. ret = ret + (string_byte(s, i) * (16 ^ (bytelen - i)));
  478. end
  479.  
  480. return ret;
  481. end
  482.  
  483. function uint_dump (n)
  484. -- Pre-calculates the neccessary range of bytes/bits to
  485. -- represent the number.
  486. local bitrange = 0;
  487. local s = n;
  488. while s >= 0 do
  489. s = s - 0xFF;
  490. bitrange = bitrange + 8;
  491. end
  492.  
  493. bitrange = math_max(0, bitrange - 8);
  494.  
  495. -- Now go recognizing the each byte (big-endian order).
  496.  
  497. local b = 0;
  498. local bytes = {};
  499. local posi = 1;
  500.  
  501. repeat
  502. bytes[posi] = bit32_band(bit32_rshift(n, b), 0xFF);
  503. b = b + 8;
  504. posi = posi + 1;
  505. until b > bitrange;
  506.  
  507. return table_unpack(bytes);
  508. end
  509.  
  510.  
  511. -- Unclassified utils.
  512.  
  513. function format (s, ...)
  514. -- No extra encoding support.
  515. local args = {...};
  516. local f = '';
  517. local i = 1;
  518. local l = #s;
  519. while i <= l do
  520. local a, j, c = string_find(s, '%%(.)', i);
  521. if c then
  522. f = f..(string_sub(s, i, a - 1)..args[tonumber(c)]);
  523. i = j + 1;
  524. else
  525. f = f..string_sub(s, i);
  526. break;
  527. end
  528. end
  529. return '(storage) '..f;
  530. end
  531.  
  532. function iscallable (value)
  533. if type(value) == 'function' then
  534. return true;
  535. end
  536.  
  537. local meta = getmetatable(value);
  538. return meta and (type(meta.__call) == 'function');
  539. end
  540.  
  541. function useapi ()
  542. return library.storageMode == API_STORAGE;
  543. end
  544.  
  545.  
  546. Messagess = {
  547. TypeNotSupported = '%1 type cannot be stored'
  548. };
  549.  
  550. library = {
  551. API_STORAGE = API_STORAGE,
  552. VIRTUAL_STORAGE = VIRTUAL_STORAGE,
  553.  
  554. ModuleFile = ModuleFile,
  555. PlayerData = PlayerData,
  556.  
  557. storageMode = (tfm_get_room.name:byte(2) == 3) and
  558. VIRTUAL_STORAGE or API_STORAGE,
  559.  
  560. placeStorageEvents = function ()
  561. for k, v in next, events do
  562. root[k] = v;
  563. end
  564. end
  565. };
  566.  
  567. root.storage = library;
  568. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement