ValveCantCount

cobaltfix

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