Guest User

Program Info

a guest
Feb 17th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 0 0
  1. args = { ... }
  2.  
  3. term.setTextColour( colours.blue )
  4.  
  5. runType = ( args[1] == "run" or args[1] == "log" or args[1] == "data" or args[1] == "clear" ) and args[1] or print( "Usage: \"run [run type] [program name]\"" ) and error()
  6.  
  7. name = (args[2] or fs.exists( args[2] )) and args[2] or print( "Usage: \"run [run type] [program name]\"" ) and error()
  8.  
  9. table.remove( args, 1 )
  10. table.remove( args, 1 )
  11.  
  12. log = { }
  13. log.data = { }
  14. log.savedData = log.data
  15. log.saveLogExcludeTypes = { }
  16.  
  17. function log.add( type, ... )
  18.     for i = 1,#log.saveLogExcludeTypes do
  19.         if type == log.saveLogExcludeTypes[i] then return false end
  20.     end
  21.     if type == nil then
  22.         type = "Undefined"
  23.     end
  24.     arg = { ... }
  25.     table.insert( log.data, { type = type, unpack( arg ) } )
  26.     return true
  27. end
  28.  
  29. function log.save( tSave )
  30.     log.add( "System", "Saving Event Log" )
  31.     if #log.data > 1 then
  32.         table.insert( tSave, "--[[[Program Log] = Welcome to the program log")
  33.         for i = 1, #log.data do
  34.             log.toWrite = true
  35.             for m = 1,#log.saveLogExcludeTypes do
  36.                 if log.data[i]["type"] == log.saveLogExcludeTypes[m] then
  37.                     log.toWrite = false
  38.                 end
  39.             end
  40.             if log.toWrite then
  41.                 local str = ""
  42.                 for k = 1,#log.data[i] do
  43.                     str = str..tostring( log.data[i][k] ).." "
  44.                 end
  45.                 table.insert( tSave, tostring( log.data[i]["type"] ).." - "..str )
  46.             end
  47.         end
  48.         table.insert( tSave, "]]" )
  49.     end
  50.     return tSave
  51. end
  52.  
  53. function string.split(str,pat)
  54.     local t = {}
  55.     local fpat = "(.-)" .. pat
  56.     local last_end = 1
  57.     local s, e, cap = str:find(fpat, 1)
  58.     while s do
  59.         if s ~= 1 or cap ~= "" then
  60.     table.insert(t,cap)
  61.         end
  62.         last_end = e+1
  63.         s, e, cap = str:find(fpat, last_end)
  64.     end
  65.     if last_end <= #str then
  66.         cap = str:sub(last_end)
  67.         table.insert(t, cap)
  68.     end
  69.     return t
  70. end
  71.  
  72. function try( strName, name, ... )
  73.     local args = { ... }
  74.     log.add( "Trying", strName, "with args:", unpack ( args ) )
  75.     err = { pcall( name, unpack( args ) ) }
  76.     ok = err[1]
  77.     if not ok then
  78.         log.add( "Error", "with function:", strName, "with args:", unpack( args ) )
  79.         return false
  80.     end
  81.     return unpack( err )
  82. end
  83.  
  84. defaultProperties = { true, "", true, { } }
  85. programProperties = { [1] = "", [2] = "", [3] = "", [4] = "" }
  86.  
  87. function getProperties( tContent )
  88.     for i = 1,#programProperties do
  89.         programProperties[i] = defaultProperties[i]
  90.     end
  91.     for i = 1,#tContent do
  92.         if tContent[i] == "--[[[Properties] = File properties" then
  93.             local z = 1
  94.             while tContent[z+i] ~= "]]" do
  95.                 m = string.split( tContent[z+i], "=" )
  96.                 if m[2] ~= nil then
  97.                     if z == 1 then
  98.                         local m = m[2]
  99.                         if m == "false" then
  100.                             programProperties[1] = false
  101.                         else
  102.                             programProperties[1] = true
  103.                         end
  104.                     elseif z == 2 then
  105.                         log.saveLogExcludeTypes = string.split( m[2], "," )
  106.                         programProperties[3] = log.saveLogExcludeTypes
  107.                     elseif z == 3 then
  108.                         local m = m[2]
  109.                         if m == "false" then
  110.                             programProperties[3] = false
  111.                         else
  112.                             programProperties[3] = true
  113.                         end
  114.                     end
  115.                 end
  116.                 z = z+1
  117.             end
  118.             break
  119.         end
  120.     end
  121. end
  122.  
  123. function insertInformation( tFile )
  124.     if tFile[1] ~= "--"..name.." was run" then
  125.         table.insert( tFile, 1, "--Code:" )
  126.         table.insert( tFile, 1, "]]" )
  127.         table.insert( tFile, 1, "Save running data="..tostring( programProperties[3] ) )
  128.         table.insert( tFile, 1, "Exclude From Save Log="..tostring( programProperties[2] ) )
  129.         table.insert( tFile, 1, "Save Log="..tostring( programProperties[1] ) )
  130.         table.insert( tFile, 1, "--[[[Properties] = File properties" )
  131.     end
  132.     if tFile[1] ~= "--"..name.." was run" then
  133.         table.insert( tFile, 1, "]]" )
  134.         table.insert( tFile, 1, "times run = 1" )
  135.         table.insert( tFile, 1, "runtime = "..tostring( run.d ).." days, "..tostring( run.h ).." hours, "..tostring( run.m ).." minutes, "..tostring( run.s ).." seconds" )
  136.         table.insert( tFile, 1, "--[[[Info] = Information gathered while program was running" )
  137.         table.insert( tFile, 1, "--"..name.." was run")
  138.     else
  139.         if programProperties[3] then
  140.             tFile[3] = "runtime = "..tostring( run.d ).." days, "..tostring( run.h ).." hours, "..tostring( run.m ).." minutes, "..tostring( run.s ).." seconds"
  141.             tFile[4] = "times run = "..( type( tonumber( string.split( tFile[4], " = " )[2]) ) == "number" and tostring( tonumber( string.split( tFile[4], " = " )[2])+1 ) or "1" )
  142.         else
  143.             tFile[3] = "runtime = enable in properties"
  144.             tFile[4] = "times run = enable in properties"
  145.         end
  146.     end
  147.     oldTFile = tFile
  148.     tFile = {}
  149.     for i = 1,#oldTFile do
  150.         if oldTFile[i] ~= "--[[[Program Log] = Welcome to the program log" then
  151.             table.insert( tFile, oldTFile[i] )
  152.         else
  153.             break
  154.         end
  155.     end
  156.     if programProperties[1] == true then
  157.         tFile = log.save( tFile )
  158.     end
  159.     k = fs.open( name, "w" )
  160.     for i = 1,#tFile do
  161.         k.writeLine( tFile[i] )
  162.     end
  163.     k.close( )
  164.     return true
  165. end
  166.  
  167. function timer( )
  168.     local s = 0
  169.     local m = 0
  170.     local h = 0
  171.     local d = 0
  172.     local ms = 0
  173.     while true do
  174.         ms = ms+1
  175.         if ms >= 10 then
  176.             ms = 0
  177.             s = s+1
  178.         end
  179.         if s >= 60 then
  180.             s = 0
  181.             m = m+1
  182.         end
  183.         if m >= 60 then
  184.             m = 0
  185.             h = h+1
  186.         end
  187.         if h >= 24 then
  188.             h = 0
  189.             d = d+1
  190.         end
  191.         run.s = s
  192.         run.m = m
  193.         run.h = h
  194.         run.d = d
  195.         sleep( 0.1 )
  196.     end
  197. end
  198.  
  199. function event( )
  200.     while true do
  201.         ev = { os.pullEvent( "mouse_click" ) }
  202.         if ev[2] == 2 and ev[3] == 51 and ev[4] == 19 then
  203.             draw = true
  204.         elseif ev[2] == 1 and ev[3] == 51 and ev[4] == 18 and draw == true then
  205.             os.queueEvent( "terminate" )
  206.         elseif draw == true then
  207.             draw = false
  208.         end
  209.     end
  210. end
  211.  
  212. oldTerm = { }
  213.  
  214. oldTerm.write = _G["term"]["write"]
  215. oldTerm.setCursorPos = _G["term"]["setCursorPos"]
  216. oldTerm.clear = _G["term"]["clear"]
  217. oldTerm.setBackgroundColour = _G["term"]["setBackgroundColour"]
  218. oldTerm.setTextColour = _G["term"]["setTextColour"]
  219.  
  220. log.add( "System", "rewriting term library" )
  221.  
  222. function term.write( ... )
  223.     local arg = { ... }
  224.     x, y = term.getCursorPos( )
  225.     log.add( "Screen", "wrote", unpack( arg ), "at", x, y )
  226.     oldTerm.write( unpack( arg ) )
  227.     oldTerm.setCursorPos( 51, 19 )
  228.     oldTerm.write( "^" )
  229.     if draw then
  230.         oldTerm.setCursorPos( 50, 18 )
  231.         oldTerm.write( "_X" )
  232.     end
  233. end
  234.  
  235. function term.clear( )
  236.     log.add( "Screen", "cleared")
  237.     oldTerm.clear( )
  238. end
  239.  
  240. function term.setCursorPos( x, y )
  241.     log.add( "Screen", "Set cursor pos to", x, y )
  242.     oldTerm.setCursorPos( x, y )
  243. end
  244.  
  245. run = { }
  246. draw = false
  247.  
  248. runProg = function( )
  249.     try( "shell.run", shell.run, name, unpack( args ) )
  250. end
  251.  
  252. k = fs.open( name, "r" )
  253. t = { }
  254. m = k.readLine( )
  255. while m do
  256.     table.insert( t, m )
  257.     m = k.readLine( )
  258. end
  259. k.close()
  260.  
  261. if runType == "run" then
  262.     getProperties( t )
  263.     log.add( "System", "Running program" )
  264.     parallel.waitForAny( runProg, timer, event )
  265.     insertInformation( t )
  266. end
  267.  
  268. term.write = oldTerm.write
  269. term.clear = oldTerm.clear
  270. term.setCursorPos = oldTerm.setCursorPos
  271.  
  272. if runType == "log" then
  273.     m = { }
  274.     isInLog = false
  275.     for i = 1,#t do
  276.         if isInLog then
  277.             table.insert( m, t[i] )
  278.         end
  279.         if t[i] == "--[[[Program Log] = Welcome to the program log" then
  280.             isInLog = true
  281.         end
  282.     end
  283.     table.remove( m, #m )
  284.     term.clear( )
  285.     term.setCursorPos( 1, 1 )
  286.     y = 1
  287.     for i = 1,#m do
  288.         term.write( m[i] )
  289.         y = y+1
  290.         term.setCursorPos( 1, y )
  291.     end
  292. end
  293.  
  294. if runType == "clear" then
  295.     oldTFile = t
  296.     tFile = { }
  297.     for i = 1,#oldTFile do
  298.         if oldTFile[i] ~= "--[[[Program Log] = Welcome to the program log" then
  299.             table.insert( tFile, oldTFile[i] )
  300.         else
  301.             break
  302.         end
  303.     end
  304.     while tFile[1] ~= "--Code:" do
  305.         table.remove( tFile, 1 )
  306.     end
  307.     table.remove( tFile, 1 )
  308.     k = fs.open( name, "w" )
  309.     for i = 1,#tFile do
  310.         k.writeLine( tFile[i] )
  311.     end
  312.     k.close( )
  313. end
Advertisement
Add Comment
Please, Sign In to add comment