Advertisement
Pirnogion

OC/sysutls

Aug 23rd, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. local component  = require "component"
  2. local computer   = require "computer"
  3. local filesystem = require "filesystem"
  4. local shell      = require "shell"
  5. --local s = require "serialization"
  6.  
  7. local system_utils
  8.  
  9. local function getAllHDD()
  10.     local args, options = shell.parse(threedot)
  11.  
  12.     local candidates = {}
  13.     for address in component.list("filesystem", true) do
  14.       local dev = component.proxy(address)
  15.       if not dev.isReadOnly() and dev.address ~= computer.tmpAddress() then
  16.         table.insert(candidates, dev)
  17.       end
  18.     end
  19.  
  20.     return candidates
  21. end
  22.  
  23. --Check system requirements
  24. local function checkMemory(require_memory)
  25.     local MEMORY_MATCH = true
  26.  
  27.     local totalMemory = computer.totalMemory()
  28.     if ( totalMemory >= require_memory) then
  29.         return MEMORY_MATCH, totalMemory
  30.     end
  31.  
  32.     return not MEMORY_MATCH, totalMemory
  33. end
  34.  
  35. local function checkHDD(require_hdd_size)
  36.     local HDD_SIZE_MATCH = true
  37.  
  38.     local hdds = getAllHDD()
  39.     local totalHDDSize = 0
  40.     for i = 1, #hdds do
  41.         totalHDDSize = totalHDDSize + hdds[i].spaceTotal()
  42.     end
  43.  
  44.     if ( totalHDDSize >= require_hdd_size ) then
  45.         return HDD_SIZE_MATCH, totalHDDSize
  46.     end
  47.  
  48.     return not HDD_SIZE_MATCH, totalHDDSize
  49. end
  50.  
  51. local function checkGpuTier(address, tier)
  52.     local GPU_TIER_LIST = {
  53.         [1] = 1,
  54.         [4] = 2,
  55.         [8] = 3
  56.     }
  57.  
  58.     local componentProxy = component.proxy(address)
  59.     if ( GPU_TIER_LIST[componentProxy.maxDepth()] >= tier ) then
  60.         return true
  61.     end
  62.  
  63.     return false
  64. end
  65.  
  66. local function checkScreenTier(address, tier)
  67.     local SCREEN_TIER_LIST = {
  68.         --[1] = 1, --Not really!
  69.         [false] = 2,
  70.         [true] = 3
  71.     }
  72.  
  73.     local componentProxy = component.proxy(address)
  74.     componentProxy.setPrecise(true)
  75.  
  76.     if ( SCREEN_TIER_LIST[ componentProxy.isPrecise() ] >= tier ) then
  77.         componentProxy.setPrecise(false)
  78.         return true
  79.     end
  80.  
  81.     componentProxy.setPrecise(false)
  82.     return false
  83. end
  84.  
  85. local function checkHologramTier(address, tier)
  86.     local HOLOGRAM_TIER_LIST = {
  87.         [1] = 1,
  88.         [2] = 2
  89.     }
  90.  
  91.     local componentProxy = component.proxy(address)
  92.  
  93.     if ( HOLOGRAM_TIER_LIST[ componentProxy.maxDepth() ] >= tier ) then
  94.         return true
  95.     end
  96.  
  97.     return false
  98. end
  99.  
  100. local LEVELED_COMPONENTS = {
  101.     ["gpu"] = checkGpuTier,
  102.     ["screen"] = checkScreenTier,
  103.     ["hologram"] = checkHologramTier
  104. }
  105.  
  106. local function checkRequirementComponent(all_components, component_type, count, tier)
  107.     local COMPONENTS_MATCH = true
  108.  
  109.     local aviableComponentCount = 0
  110.     for address, componentType in pairs(all_components) do
  111.         if ( componentType == component_type ) then
  112.             local checkTierFunction = LEVELED_COMPONENTS[component_type]
  113.             if ( checkTierFunction == nil or checkTierFunction(address, tier) ) then
  114.                 aviableComponentCount = aviableComponentCount + 1
  115.             end
  116.         end
  117.     end
  118.  
  119.     if (aviableComponentCount >= count) then
  120.         return COMPONENTS_MATCH, aviableComponentCount
  121.     end
  122.  
  123.     return not COMPONENTS_MATCH, aviableComponentCount
  124. end
  125.  
  126. function system_utils.checkSystemRequirements(system_requirements_list)
  127.     local allAviableComponents = component.list()
  128.     local report = {}
  129.     local result = true
  130.     for componentType, componentRequirements in pairs(system_requirements_list.requirementComponents) do
  131.         report[componentType] = { checkRequirementComponent(allAviableComponents, componentType, componentRequirements.count, componentRequirements.tier) }
  132.         result = result and report[componentType][1]
  133.     end
  134.  
  135.     report["hdd_size"] = { checkHDD(system_requirements_list.requirementOthers.hdd_size) }
  136.     report["memory_size"] = { checkMemory(system_requirements_list.requirementOthers.memory_size) }
  137.  
  138.     return result, report
  139. end
  140.  
  141. return system_utils
  142.  
  143. --[[ Example
  144. local list = {}
  145. --Component type = { count, tier }
  146. list.requirementComponents = {
  147.     ["gpu"]      = {["count"] = 1, ["tier"] = 3},
  148.     ["internet"] = {["count"] = 1, ["tier"] = 0},
  149.     ["modem"]    = {["count"] = 1, ["tier"] = 0},
  150.     ["screen"]   = {["count"] = 1, ["tier"] = 3},
  151.     ["keyboard"] = {["count"] = 1, ["tier"] = 0},
  152.     ["hologram"] = {["count"] = 1, ["tier"] = 2}
  153. }
  154.  
  155. list.requirementOthers = {
  156.     ["memory_size"] = 1048576,
  157.     ["hdd_size"]    = 2097152
  158. }
  159.  
  160. local result, rList = checkSystemRequirements(list)
  161. print( result, s.serialize(rList) )
  162. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement