ValveCantCount

Untitled

Nov 9th, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.28 KB | None | 0 0
  1. cobalt = {
  2. graphics = { },
  3. filesystem = { },
  4. event = { },
  5. window = { },
  6. _mainloop = true,
  7. keyboard = {
  8. keysdown = { },
  9. },
  10. rednet = {
  11.  
  12. },
  13. application = {
  14. backColour = colours.black,
  15. foreColour = colours.white,
  16. },
  17. math = { },
  18. mouse = { },
  19. table = { },
  20. state = "Main",
  21. version = "1.1_3",
  22. updatespeed = 0.1,
  23. monitor = true
  24. }
  25.  
  26. cobalt.surface = dofile( "cobalt-lib/surface" )
  27.  
  28. resetTimer = os.startTimer
  29. cobalt.updatetimer = os.startTimer( cobalt.updatespeed )
  30. local callbacks = { }
  31. function cobalt.debug( item )
  32. local f = fs.open( ".cobaltdebug", "w" )
  33. f.write(item)
  34. f.close()
  35. cobalt.exit()
  36. end
  37.  
  38. function cobalt.newCallback( event, func )
  39. callbacks[event] = func
  40. end
  41.  
  42.  
  43. local function getFile( file, path )
  44. local dl = http.get( "https://raw.githubusercontent.com/ebernerd/Cobalt/master/"..file )
  45. if dl then
  46. local h = dl.readAll()
  47. dl.close()
  48. return h
  49. end
  50. return false
  51. end
  52.  
  53. if not fs.isDir( ".err-logs" ) then
  54. pcall(function() fs.makeDir( ".err-logs" ) end)
  55. end
  56.  
  57. local args = { ... }
  58. if args[1] == "version" then
  59. print( "Cobalt " .. cobalt.version )
  60. v = getFile( "version" ):sub(1,-2)
  61. if v then
  62. if cobalt.version ~= v then
  63. print( "Your Cobalt version is out of date!\nYour Version: " .. cobalt.version .. " | Current: " .. v )
  64. print( "\nUse the command \"cobalt update\" to receive the newest update!" )
  65. end
  66. else
  67. print( "Cobalt version checking is not available at this time. Is HTTP and HTTPS enabled?" )
  68. end
  69. elseif args[1] == "update" then
  70. shell.run( "pastebin run h5h4fm3t" )
  71. elseif args[1] == "help" then
  72. print( "You can find help resources here: ")
  73. print( "https://github.com/ebernerd/Cobalt/wiki")
  74. elseif args[1] == "examples" then
  75. if fs.isDir( "cobalt-examples" ) then
  76. if args[2] then
  77. if fs.exists( "cobalt-examples/" .. args[2] ) then
  78. shell.run( "cobalt-examples/" .. args[2] )
  79. else
  80. print( "List of Cobalt examples: ")
  81. for i, v in pairs( fs.list( "cobalt-examples" ) ) do
  82. print( "-" .. v )
  83. end
  84. end
  85. else
  86. print( "List of Cobalt examples: ")
  87. for i, v in pairs( fs.list( "cobalt-examples" ) ) do
  88. print( "-" .. v )
  89. end
  90. end
  91. else
  92. print( "Download the Cobalt examples here" )
  93. print( "http://github.com/ebernerd")
  94. end
  95. end
  96.  
  97. cobalt.g = cobalt.graphics
  98. cobalt.k = cobalt.keyboard
  99. cobalt.fs = cobalt.filesystem
  100. cobalt.e = cobalt.event
  101. cobalt.w = cobalt.window
  102. cobalt.m = cobalt.mouse
  103.  
  104. local graphics_lighten = {
  105. [colours.black] = colours.grey,
  106. [colours.grey] = colours.lightGrey,
  107. [colours.lightGrey] = colours.white,
  108. [colours.white] = colours.white,
  109.  
  110. [colours.blue] = colours.cyan,
  111. [colours.cyan] = colours.lightBlue,
  112. [colours.lightBlue] = colours.white,
  113.  
  114. [colours.green] = colours.lime,
  115. [colours.lime] = colours.white,
  116.  
  117. [colours.brown] = colours.orange,
  118. [colours.orange] = colours.yellow,
  119. [colours.yellow] = colours.white,
  120.  
  121. [colours.red] = colours.pink,
  122. [colours.pink] = colours.white,
  123.  
  124. [colours.purple] = colours.magenta,
  125. [colours.magenta] = colours.white,
  126.  
  127. }
  128.  
  129. local graphics_darken = {
  130. [colours.white] = colours.lightGrey,
  131. [colours.lightGrey] = colours.grey,
  132. [colours.grey] = colours.grey,
  133.  
  134. [colours.lightBlue] = colours.cyan,
  135. [colours.cyan] = colours.blue,
  136.  
  137. [colours.lime] = colours.green,
  138.  
  139. [colours.yellow] = colours.orange,
  140. [colours.orange] = colours.brown,
  141.  
  142. [colours.pink] = colours.red,
  143. [colours.magenta] = colours.purple,
  144. }
  145.  
  146. --[=[ GRAPHICS FUNCTIONS ]=]--
  147. function cobalt.graphics.print( text, x, y, colour, backColour )
  148. local x = x or 1
  149. local y = y or 1
  150. text = tostring(text) or "Some Text"
  151. if cobalt.application.view then
  152. cobalt.application.view:drawText( cobalt.math.round(x), cobalt.math.round(y), text, colour, backColour )
  153. end
  154. end
  155. function cobalt.graphics.rect( mode, x, y, w, h, colour )
  156. if cobalt.application.view then
  157. if mode == "fill" then
  158. cobalt.application.view:fillRect( cobalt.math.round(x), cobalt.math.round(y), cobalt.math.round(w+x), cobalt.math.round(h+y), " ", colour )
  159. elseif mode == "line" then
  160. cobalt.application.view:drawRect( cobalt.math.round(x), cobalt.math.round(y), cobalt.math.round(w+x), cobalt.math.round(h+y), " ", colour )
  161. end
  162. end
  163. end
  164. function cobalt.graphics.center( text, y, offset, lim, colour, backColour )
  165. local text = tostring(text) or "Some Text"
  166. local y = y or 1
  167. local offset = offset or 0
  168. local lim = lim or cobalt.window.getWidth()
  169. if lim > cobalt.window.getWidth() then
  170. lim = cobalt.window.getWidth()
  171. end
  172. if cobalt.application.view then
  173. local t = {}
  174. for i in string.gmatch( text, "%S+" ) do
  175. t[#t+1] = i
  176. end
  177. local lines = {[1] = ""}
  178. local line = 1
  179. for i=1, #t do
  180. if #tostring(lines[line].." "..t[i]) > lim then
  181. lines[line] = lines[line] .. "\n"
  182. line = line + 1
  183. lines[line] = " " .. t[i]
  184. else
  185. lines[line] = lines[line] .. " " .. t[i]
  186. end
  187. end
  188. y = y or math.ceil(dy/2-#lines/2)
  189. for i = 1, #lines do
  190. cobalt.application.view:drawText( cobalt.math.round(cobalt.window.getWidth()/2-#lines[i]/2), cobalt.math.round(y+(i-1)), lines[i], colour, backColour )
  191. end
  192. end
  193. end
  194.  
  195. cobalt.graphics.rectangle = cobalt.graphics.rect
  196.  
  197. function cobalt.graphics.pixel( x, y, colour )
  198. --if cobalt.application.view then
  199. cobalt.application.view:drawPixel( cobalt.math.round(x), cobalt.math.round(y), " ", colour, colour )
  200. --end
  201. end
  202.  
  203. function cobalt.graphics.line( x1, y1, x2, y2, backColour, colour )
  204. if cobalt.application.view then
  205. cobalt.application.view:drawLine( cobalt.math.round(x1), cobalt.math.round(y1), cobalt.math.round(x2), cobalt.math.round(y2), " ", backColour, colour )
  206. end
  207. end
  208.  
  209. function cobalt.graphics.lighten( colour )
  210. return graphics_lighten[colour]
  211. end
  212.  
  213. function cobalt.graphics.darken( colour )
  214. return graphics_darken[colour]
  215. end
  216.  
  217.  
  218. function cobalt.graphics.clear()
  219. return cobalt.application.view:clear()
  220. end
  221.  
  222. function cobalt.graphics.reset()
  223. cobalt.application.view = nil
  224. cobalt.application.view = cobalt.surface.create( cobalt.window.getWidth(), cobalt.window.getHeight(), " ", cobalt.application.backColour, cobalt.application.foreColour )
  225. end
  226.  
  227. --[=[WINDOW FUNCTIONS]=]--
  228. function cobalt.window.getWidth()
  229. local x, _ = term.getSize()
  230. return x
  231. end
  232.  
  233. function cobalt.window.getHeight()
  234. local _, y = term.getSize()
  235. return y
  236. end
  237.  
  238. cobalt.window.getSize = term.getSize
  239.  
  240. --[=[ MOUSE FUNCTIONS ]=]--
  241. function cobalt._mousepressed( x, y, button )
  242. cobalt.mouse[button] = true
  243.  
  244. if cobalt.mousepressed then cobalt.mousepressed( x, y, button ) end
  245. end
  246.  
  247. function cobalt._mousereleased( x, y, button )
  248. cobalt.mouse[button] = false
  249.  
  250. if cobalt.mousereleased then cobalt.mousereleased( x, y, button ) end
  251. end
  252.  
  253. function cobalt.mouse.isDown( button )
  254. return cobalt.mouse[button]
  255. end
  256.  
  257.  
  258. --[=[KEYBOARD FUNCTIONS]=]--
  259. function cobalt._keypressed( key, keycode )
  260. if key then
  261. cobalt.keyboard.keysdown[key] = true
  262. end
  263. if keycode then
  264. cobalt.keyboard.keysdown[keycode] = true
  265. end
  266.  
  267.  
  268. if cobalt.keypressed then cobalt.keypressed( key, keycode ) end
  269. end
  270.  
  271. function cobalt._keyreleased( key, keycode )
  272. if key then
  273. cobalt.keyboard.keysdown[key] = nil
  274. end
  275. if keycode then
  276. cobalt.keyboard.keysdown[keycode] = nil
  277. end
  278.  
  279. if cobalt.keyreleased then cobalt.keyreleased( key, keycode ) end
  280. end
  281.  
  282. function cobalt.keyboard.isDown( key )
  283. if not key then
  284. if #cobalt.keyboard.keysdown > 0 then
  285. return true
  286. end
  287. else
  288. if cobalt.keyboard.keysdown[key] then
  289. return true
  290. end
  291. end
  292. return false
  293. end
  294.  
  295.  
  296. function cobalt.math.round(num, idp)
  297. local mult = 10^(idp or 0)
  298. return math.floor(num * mult + 0.5) / mult
  299. end
  300.  
  301. function cobalt.math.clamp( val, lower, upper )
  302. if lower > upper then lower, upper = upper, lower end
  303. return math.max( lower, math.min( upper, val ) )
  304. end
  305.  
  306.  
  307. --[=[REDNET FUNCTIONS]=]--
  308. function cobalt.rednet.receive(time, ret)
  309.  
  310. end
  311.  
  312. --[=[STATE FUNCTIONS]=]--
  313. function cobalt.getState()
  314. return cobalt.state
  315. end
  316.  
  317. function cobalt.getPercentage( str )
  318. if type(str) == "string" then
  319. if str:sub( #str ) == "%" then
  320. local perc = str:sub( 1, #str-1 )
  321. perc = tonumber(perc)
  322. if perc > 100 or perc < 0 then
  323. error( "Invalid percentage" )
  324. else
  325. return perc/100
  326. end
  327. else
  328. error("Expected number or percentage")
  329. end
  330. end
  331. end
  332.  
  333. function cobalt.setPercentage( perc )
  334. return perc --Dunno if this will work.
  335. end
  336.  
  337.  
  338. --[=[FILESYSTEM FUNCTIONS]=]--
  339. cobalt.filesystem.list = fs.list
  340. cobalt.filesystem.getDirectoryItems = fs.list
  341. cobalt.filesystem.isFile = fs.exists
  342. cobalt.filesystem.isDirectory = fs.isDir
  343. --cobalt.filesystem.currentDir = shell.dir()
  344.  
  345. cobalt.raw = false
  346.  
  347. function cobalt.exit( ... )
  348. local msgs = { ... }
  349. cobalt._mainloop = false
  350. for i = 1, 10 do
  351. term.setBackgroundColour(colours.black)
  352. term.setTextColour( colours.white )
  353. term.clear()
  354. term.setCursorPos( 1, 1 )
  355. end
  356. if #msgs >= 1 then
  357. for i =1, #msgs do
  358. print( msgs[i] )
  359. end
  360. end
  361. return true
  362. end
  363.  
  364. function cobalt.halt()
  365. cobalt._mainloop = false
  366. end
  367.  
  368. local function cerr( reason )
  369.  
  370.  
  371. if cobalt.onError then
  372. local ok, err2 = pcall(cobalt.onError, reason)
  373. if not ok then
  374. cobalt.application.view = nil
  375. cobalt.application.view = cobalt.surface.create( cobalt.window.getWidth(), cobalt.window.getHeight(), " ", colours.blue, colours.white )
  376. cobalt.graphics.center( "Cobalt Error Report", 2 )
  377. cobalt.graphics.center( "Your error handler errored. Nice job. Here's why:", 4, 0, 40 )
  378. cobalt.graphics.center( err2 or "No further information", 8, 0, 40, nil, colours.yellow )
  379. local saveLoc = ".err-logs/" .. os.day() .. "-" .. os.time()
  380. local f = fs.open( saveLoc, "w" )
  381. local saveStr = "err-handler-exception: " .. err2 .. "\nprogram-exception: " .. reason
  382. if f then
  383. f.write( saveStr or "No further information" )
  384. f.close()
  385. cobalt.graphics.center( "This error has been saved to \""..saveLoc.."\" for future use.", 12, 0, 40 )
  386. else
  387. cobalt.graphics.center( "This error could not be logged due to lack of hard disk space.", 12, 0, 40 )
  388. end
  389. cobalt.application.view:render()
  390. sleep( 0.5 )
  391. cobalt.graphics.center( "Press any key to exit.", cobalt.window.getHeight()-1 )
  392. cobalt.application.view:render()
  393. os.pullEvent( "key_up" )
  394. cobalt.exit()
  395. end
  396. else
  397. cobalt.application.view = nil
  398. cobalt.application.view = cobalt.surface.create( cobalt.window.getWidth(), cobalt.window.getHeight(), " ", colours.blue, colours.white )
  399. cobalt.graphics.center( "Cobalt Error Report", 2 )
  400. cobalt.graphics.center( "Your program has encountered an error, which has been traced back to:", 4, 0, 40 )
  401. cobalt.graphics.center( reason or "No further information", 8, 0, 40, nil, colours.yellow )
  402. local saveLoc = ".err-logs/" .. os.day() .. "-" .. os.time()
  403. local f = fs.open( saveLoc, "w" )
  404. if f then
  405. f.write( reason or "No further information" )
  406. f.close()
  407. cobalt.graphics.center( "This error has been saved to \""..saveLoc.."\" for future use.", 12, 0, 40 )
  408. else
  409. cobalt.graphics.center( "This error could not be logged due to lack of hard disk space.", 12, 0, 40 )
  410. end
  411. cobalt.application.view:render()
  412. sleep( 0.5 )
  413. cobalt.graphics.center( "Press any key to exit.", cobalt.window.getHeight()-1 )
  414. cobalt.application.view:render()
  415. os.pullEvent( "key_up" )
  416. cobalt.exit()
  417. end
  418. cobalt.halt()
  419.  
  420. end
  421.  
  422. local function update()
  423. if cobalt.update then
  424. local ok, err = pcall(cobalt.update, cobalt.updatespeed)
  425. if not ok then
  426. cerr( err )
  427. end
  428. end
  429. cobalt.updatetimer = resetTimer( cobalt.updatespeed )
  430. if cobalt.draw then
  431. if cobalt.application.view then
  432. cobalt.application.view:clear(" ", cobalt.application.backColour, cobalt.application.foreColour );
  433. end cobalt.draw();
  434. if cobalt.application.view then
  435. cobalt.application.view:render()
  436. end
  437. resetTimer = function(a) return nil end
  438. end
  439. end
  440.  
  441. function cobalt.initLoop( runOnce )
  442. term.clear()
  443. term.setCursorPos( 1, 1 )
  444. cobalt.graphics.reset()
  445.  
  446. monitorFunc = function(b,c,a) end
  447.  
  448. if cobalt.monitor then
  449. monitorFunc = cobalt.mousereleased
  450. end
  451.  
  452. while cobalt._mainloop do
  453.  
  454. local e, a, b, c, d, e
  455. if cobalt.raw then
  456. e, a, b, c, d, e = os.pullEventRaw()
  457. else
  458. e, a, b, c, d, e = os.pullEvent()
  459. end
  460. if cobalt.on then
  461. local ok, err = pcall(cobalt.on, e, a, b, c, d, e)
  462. if not ok then
  463. cerr( err )
  464. end
  465. end
  466. if callbacks[event] then
  467. local ok, err = pcall(callbacks[event], a, b, c, d, e )
  468. if not ok then
  469. cerr( err )
  470. end
  471. end
  472. if e == "char" then
  473. if cobalt.textinput then
  474. local ok, err = pcall(cobalt.textinput, a)
  475. if not ok then
  476. cerr( err )
  477. end
  478. end
  479. elseif e == "key" then
  480. local ok, err = pcall(cobalt._keypressed, a, keys.getName( a ) )
  481. if not ok then
  482. cerr( err )
  483. end
  484. elseif e == "key_up" then
  485. local ok, err = pcall(cobalt._keyreleased, a, keys.getName( a ) )
  486. if not ok then
  487. cerr( err )
  488. end
  489. elseif e == "mouse_click" then
  490. if cobalt.mousepressed then
  491. local ok, err = pcall(cobalt.mousepressed, b, c, a)
  492. if not ok then
  493. cerr( err )
  494. end
  495. monitorFunc(b,c,a)
  496. cobalt.draw()
  497. if cobalt.application.view then
  498. cobalt.application.view:render()
  499. end
  500. end --b, c, a to keep love.mousepressed syntax as( x, y, button ), not ( button, x, y )
  501. elseif e == "mouse_up" then
  502. if cobalt.mousereleased then
  503. local ok, err = pcall(cobalt.mousereleased, b, c, a )
  504. if not ok then
  505. cerr( err )
  506. end
  507. end
  508. elseif e == "rednet_message" then
  509. if cobalt.rednetreceive then
  510. local ok, err = pcall(cobalt.rednetreceive, a, b, c)
  511. if not ok then
  512. cerr( err )
  513. end
  514. end
  515. elseif e == "mouse_drag" then
  516. if cobalt.mousedrag then
  517. local ok, err = pcall(cobalt.mousedrag, b, c )
  518. if not ok then
  519. cerr( err )
  520. end
  521. end
  522. elseif e == "timer" then
  523. if a == cobalt.updatetimer then
  524. local ok, err = pcall( update )
  525. if not ok then
  526. cerr( err )
  527. end
  528. end
  529. elseif e == "paste" then
  530. if cobalt.paste then
  531. local ok, err = pcall(cobalt.paste, a)
  532. if not ok then
  533. cerr( err )
  534. end
  535. end
  536. end
  537.  
  538.  
  539. if runOnce then cobalt._mainloop = false; return end
  540. end
  541. end
  542.  
  543. function cobalt.initOnce()
  544. cobalt.initLoop( true )
  545. end
  546.  
  547. cobalt.init = cobalt.initLoop
  548.  
  549.  
  550. return cobalt
Add Comment
Please, Sign In to add comment