Advertisement
9551

nimg loaded APi backup

Oct 26th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.73 KB | None | 0 0
  1. --------------------NIMG LOADER API--------------------
  2. ---------------API FOR LOADING NIMG IMAGES-------------
  3. --------------------by 9551 DEV------------------------
  4. ---Copyright (c) 2021-2022 9551------------9551#0001---
  5. ---using this code in your project is fine!------------
  6. ---as long as you dont claim you made it---------------
  7. ---im cool with it, feel free to include---------------
  8. ---in your projects! discord: 9551#0001--------------
  9. ---you dont have to but giving credits is nice :)------
  10. -------------------------------------------------------
  11. -------------------------------------------------------
  12. local expect = require("cc.expect").expect
  13. local index = {}
  14. local indexAnimate = {}
  15. local indexBuffer = {}
  16. local chars = "0123456789abcdef"
  17. local saveCols, loadCols = {}, {}
  18. for i = 0, 15 do
  19. saveCols[2^i] = chars:sub(i + 1, i + 1)
  20. loadCols[chars:sub(i + 1, i + 1)] = 2^i
  21. end
  22. local decode = function(tbl)
  23. local output = setmetatable({},{
  24. __index=function(t,k)
  25. local new = {}
  26. t[k]=new
  27. return new
  28. end
  29. })
  30. output["offset"] = tbl["offset"]
  31. for k,v in pairs(tbl) do
  32. for ko,vo in pairs(v) do
  33. if type(vo) == "table" then
  34. output[k][ko] = {}
  35. if vo then
  36. output[k][ko].t = loadCols[vo.t]
  37. output[k][ko].b = loadCols[vo.b]
  38. output[k][ko].s = vo.s
  39. end
  40. end
  41. end
  42. end
  43. return setmetatable(output,getmetatable(tbl))
  44. end
  45. local encode = function(tbl)
  46. local output = setmetatable({},{
  47. __index=function(t,k)
  48. local new = {}
  49. t[k]=new
  50. return new
  51. end
  52. })
  53. output["offset"] = tbl["offset"]
  54. for k,v in pairs(tbl) do
  55. for ko,vo in pairs(v) do
  56. if type(vo) == "table" then
  57. output[k][ko] = {}
  58. if vo then
  59. output[k][ko].t = saveCols[vo.t]
  60. output[k][ko].b = saveCols[vo.b]
  61. output[k][ko].s = vo.s
  62. end
  63. end
  64. end
  65. end
  66. return setmetatable(output,getmetatable(tbl))
  67. end
  68. local function strech(tbl, x, y)
  69. if type(x) == "number" and type(y) == "number" then
  70. local strechmap = {}
  71. local strechmapy = {}
  72. local final
  73. local mulx = 1
  74. local muly = 1
  75. strechmap["offset"] = tbl.offset
  76. strechmapy["offset"] = tbl.offset
  77. for k, v in pairs(tbl) do
  78. if type(k) == "number" then
  79. for k2, v2 in pairs(v) do
  80. for i = 1, x do
  81. if x > 1 then
  82. mulx = x
  83. end
  84. if not strechmap[k * mulx + i] then
  85. strechmap[k * mulx + i] = {}
  86. end
  87. if not strechmap[k * mulx + i][k2] then
  88. strechmap[k * mulx + i][k2] = {}
  89. end
  90. strechmap[k * mulx + i][k2] = tbl[k][k2]
  91. end
  92. end
  93. end
  94. end
  95. if y > 1 then
  96. for k, v in pairs(strechmap) do
  97. if type(k) == "number" then
  98. for k2, v2 in pairs(v) do
  99. for i = 1, y do
  100. if y > 1 then
  101. muly = y
  102. end
  103. if not strechmapy[k] then
  104. strechmapy[k] = {}
  105. end
  106. if not strechmapy[k][k2 * muly + i] then
  107. strechmapy[k][k2 * muly + i] = {}
  108. end
  109. strechmapy[k][k2 * muly + i] = strechmap[k][k2]
  110. end
  111. end
  112. end
  113. end
  114. end
  115. if not next(strechmap) then
  116. strechmap = tbl
  117. end
  118. if y > 1 then
  119. final = strechmapy
  120. else
  121. final = strechmap
  122. end
  123. return setmetatable(final, getmetatable(tbl))
  124. else
  125. return tbl
  126. end
  127. end
  128. function index:strech(x, y)
  129. local tbl = self
  130. if not self then
  131. error("try using : instead of .", 0)
  132. end
  133. if type(x) == "number" and type(y) == "number" then
  134. local strechmap = {}
  135. local strechmapy = {}
  136. local final
  137. local mulx = 1
  138. local muly = 1
  139. strechmap["offset"] = tbl.offset
  140. strechmapy["offset"] = tbl.offset
  141. for k, v in pairs(tbl) do
  142. if type(k) == "number" then
  143. for k2, v2 in pairs(v) do
  144. for i = 1, x do
  145. if x > 1 then
  146. mulx = x
  147. end
  148. if not strechmap[k * mulx + i] then
  149. strechmap[k * mulx + i] = {}
  150. end
  151. if not strechmap[k * mulx + i][k2] then
  152. strechmap[k * mulx + i][k2] = {}
  153. end
  154. strechmap[k * mulx + i][k2] = tbl[k][k2]
  155. end
  156. end
  157. end
  158. end
  159. if y > 1 then
  160. for k, v in pairs(strechmap) do
  161. if type(k) == "number" then
  162. for k2, v2 in pairs(v) do
  163. for i = 1, y do
  164. if y > 1 then
  165. muly = y
  166. end
  167. if not strechmapy[k] then
  168. strechmapy[k] = {}
  169. end
  170. if not strechmapy[k][k2 * muly + i] then
  171. strechmapy[k][k2 * muly + i] = {}
  172. end
  173. strechmapy[k][k2 * muly + i] = strechmap[k][k2]
  174. end
  175. end
  176. end
  177. end
  178. end
  179. if not next(strechmap) then
  180. strechmap = tbl
  181. end
  182. if y > 1 then
  183. final = strechmapy
  184. else
  185. final = strechmap
  186. end
  187. return setmetatable(final, getmetatable(tbl))
  188. else
  189. return tbl
  190. end
  191. end
  192. function index:draw(termobj, x, y, ix, iy)
  193. expect(1, termobj, "table", "nil")
  194. expect(2, x, "number", "nil")
  195. expect(3, y, "number", "nil")
  196. local self = strech(self, ix, iy)
  197. local terms = termobj or term
  198. local x = x or 0
  199. local y = y or 0
  200. local obc = terms.getBackgroundColor()
  201. local otc = terms.getTextColor()
  202. for k, v in pairs(self) do
  203. if type(k) == "number" then
  204. for k2, v2 in pairs(v) do
  205. terms.setCursorPos(k - (self.offset[1]) + x, k2 - (self.offset[2] / 2) + y)
  206. terms.setBackgroundColor(v2.b)
  207. terms.setTextColor(v2.t)
  208. terms.write(v2.s)
  209. end
  210. end
  211. end
  212. terms.setBackgroundColor(obc)
  213. terms.setTextColor(otc)
  214. end
  215. function indexAnimate:drawImage(termobj, frame, x, y, ix, iy)
  216. expect(1, termobj, "table", "nil")
  217. expect(2, frame, "number")
  218. expect(3, x, "number", "nil")
  219. expect(4, y, "number", "nil")
  220. if not self then
  221. return false
  222. end
  223. local frame = frame or 1
  224. local frameToLoad = self[1][math.min(frame, #self[1])]
  225. local frameToLoad = strech(frameToLoad, ix, iy)
  226. if frameToLoad then
  227. local terms = termobj or term
  228. if not x then
  229. x = 0
  230. end
  231. if not y then
  232. y = 0
  233. end
  234. local obc = terms.getBackgroundColor()
  235. local otc = terms.getTextColor()
  236. for k, v in pairs(frameToLoad) do
  237. if type(k) == "number" then
  238. for k2, v2 in pairs(v) do
  239. terms.setCursorPos(k - (frameToLoad.offset[1]) + x, k2 - (frameToLoad.offset[2] / 2) + y)
  240. terms.setBackgroundColor(v2.b)
  241. terms.setTextColor(v2.t)
  242. terms.write(v2.s)
  243. end
  244. end
  245. end
  246. terms.setBackgroundColor(obc)
  247. terms.setTextColor(otc)
  248. end
  249. end
  250. function indexAnimate:animate(termobj, x, y, speed, buffer, clears, ix, iy)
  251. expect(1, termobj, "table")
  252. expect(2, x, "number")
  253. expect(3, y, "number")
  254. expect(4, speed, "number")
  255. expect(5, buffer, "boolean")
  256. expect(6, clears, "boolean")
  257. local terms = termobj or term.current()
  258. local tx, ty = terms.getSize()
  259. if buffer then
  260. terms = window.create(terms, 1, 1, tx, ty)
  261. end
  262. local x = x or 0
  263. local y = y or 0
  264. if not speed then
  265. speed = 1
  266. end
  267. if clears == nil then
  268. clears = false
  269. end
  270. if buffer == nil then
  271. buffer = false
  272. end
  273. local obc = terms.getBackgroundColor()
  274. local otc = terms.getTextColor()
  275. for k1, v1 in ipairs(self[1]) do
  276. local v1 = strech(v1, ix, iy)
  277. if buffer then
  278. terms.setVisible(false)
  279. end
  280. terms.setBackgroundColor(obc)
  281. terms.setTextColor(otc)
  282. if clears then
  283. terms.clear()
  284. end
  285. local count = 0
  286. local curcount = 0
  287. for k, v in pairs(v1) do
  288. count = count + 1
  289. end
  290. for k, v in pairs(v1) do
  291. if type(k) == "number" then
  292. for k2, v2 in pairs(v) do
  293. terms.setCursorPos(k - (v1.offset[1]) + x, k2 - (v1.offset[2] / 2) + y)
  294. terms.setBackgroundColor(v2.b)
  295. terms.setTextColor(v2.t)
  296. terms.write(v2.s)
  297. end
  298. end
  299. end
  300. if buffer then
  301. terms.setVisible(true)
  302. end
  303. curcount = curcount + 1
  304. if curcount < #self[1] then
  305. sleep(speed)
  306. end
  307. end
  308. terms.setBackgroundColor(obc)
  309. terms.setTextColor(otc)
  310. end
  311. function indexAnimate:slide(termobj, x, y, ix, iy)
  312. expect(1, termobj, "table")
  313. expect(2, x, "number")
  314. expect(3, y, "number")
  315. local animation = self[1]
  316. local loadFrame = self[2] + 1
  317. if not animation[loadFrame] then
  318. return setmetatable({animation, 0, false}, {__index = indexAnimate})
  319. end
  320. local terms = termobj or term
  321. local x = x or 0
  322. local y = y or 0
  323. local obc = terms.getBackgroundColor()
  324. local otc = terms.getTextColor()
  325. animation[loadFrame] = strech(animation[loadFrame], ix, iy)
  326. for k, v in pairs(animation[loadFrame]) do
  327. if type(k) == "number" then
  328. for k2, v2 in pairs(v) do
  329. terms.setCursorPos(
  330. k - (animation[loadFrame].offset[1]) + x,
  331. k2 - (animation[loadFrame].offset[2] / 2) + y
  332. )
  333. terms.setBackgroundColor(v2.b)
  334. terms.setTextColor(v2.t)
  335. terms.write(v2.s)
  336. end
  337. end
  338. end
  339. terms.setBackgroundColor(obc)
  340. terms.setTextColor(otc)
  341. return setmetatable({animation, loadFrame, true}, {__index = indexAnimate})
  342. end
  343. function indexAnimate:iterate()
  344. local currentImage = 0
  345. local allFramesRaw = self[1]
  346. return function()
  347. currentImage = currentImage + 1
  348. if allFramesRaw[currentImage] then
  349. return setmetatable(allFramesRaw[currentImage], {__index = index}), allFramesRaw, currentImage
  350. end
  351. end
  352. end
  353. local function loadImage(name)
  354. expect(1, name, "string")
  355. local name = name or ""
  356. if not fs.exists(name .. ".nimg") then
  357. error("not found. try using file name wihnout .nimg note that all files need to have .nimg extension to work", 2)
  358. end
  359. local file = fs.open(name .. ".nimg", "r")
  360. local image = textutils.unserialize(file.readAll())
  361. local decoded = decode(image)
  362. file.close()
  363. return setmetatable(decoded, {__index = index})
  364. end
  365. local function loadImageSet(name)
  366. local frames = {}
  367. expect(1, name, "string")
  368. local name = name or ""
  369. if not fs.isDir(name .. ".animg") then
  370. error("not found. try using file name wihnout .animg note that all image sets need to have .animg as extension to work!", 2)
  371. end
  372. for k, v in pairs(fs.list(name .. ".animg")) do
  373. local fn, ext = v:match("^(%d+).+(%..-)$")
  374. if ext == ".nimg" then
  375. local file = fs.open(name .. ".animg/" .. v, "r")
  376. frames[tonumber(fn)] = textutils.unserialise(file.readAll())
  377. frames[tonumber(fn)] = decode(frames[tonumber(fn)])
  378. file.close()
  379. end
  380. end
  381. return setmetatable({frames, 0}, {__index = indexAnimate})
  382. end
  383. function indexBuffer:startBuffer(noclear)
  384. expect(1, noclear, "boolean", "nil")
  385. self.setVisible(false)
  386. if not noclear then
  387. buffer.clear()
  388. end
  389. end
  390. function indexBuffer:endBuffer()
  391. self.setVisible(true)
  392. end
  393. local function createBuffer(termObj)
  394. expect(1, termObj, "table")
  395. local terms = termObj or term.current()
  396. local xs, ys = terms.getSize()
  397. return setmetatable(window.create(terms, 1, 1, xs, ys), {__index = indexBuffer})
  398. end
  399. local function getButtonH()
  400. return require("ButtonH")
  401. end
  402. local function downloadPastekage(pasteCode)
  403. --pasteges are packages for pastes:
  404. --they allow downloading a set of images by putting into an paste
  405. --pastekage format:
  406. --l1: **PASTEKAGE**
  407. --l2: [NAME]:PASTEBINCODE
  408. --l3: [NAME]:PASTEBINCODE
  409. --l4: etc...
  410. local pastecage, err = http.get("https://pastebin.com/raw/" .. pasteCode)
  411. if err then
  412. error(err, 0)
  413. end
  414. local rand = tostring(math.random(1, 1000000))
  415. local temp = fs.open("9551_NIMG_MAIN_API_TEMP_PASTKAGE_" .. rand, "w")
  416. temp.write(pastecage.readAll())
  417. temp.close()
  418. pastecage.close()
  419. local lc = 0
  420. for l in io.lines("9551_NIMG_MAIN_API_TEMP_PASTKAGE_" .. rand) do
  421. lc = lc + 1
  422. if lc == 1 then
  423. if l ~= "**PASTEKAGE**" then
  424. error("not an pastekage!", 0)
  425. end
  426. end
  427. if lc > 1 then
  428. local name, code = l:match("^%[(.+)%]:(.+)")
  429. local web, err = http.get("https://pastebin.com/raw/" .. code)
  430. if err then
  431. error(err, 0)
  432. end
  433. local file = fs.open(name, "w")
  434. file.write(web.readAll())
  435. file.close()
  436. web.close()
  437. end
  438. end
  439. fs.delete("9551_NIMG_MAIN_API_TEMP_PASTKAGE_" .. rand)
  440. end
  441. return {
  442. loadImage = loadImage,
  443. loadImageSet = loadImageSet,
  444. getButtonH = getButtonH,
  445. createBuffer = createBuffer,
  446. stretch2DMap = strech,
  447. downloadPastekage = downloadPastekage,
  448. encode = encode,
  449. decode = decode
  450. }
  451.  
  452.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement