Computercraft thingies
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.lua 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. function percentOf(val1, val2)
  2. return math.ceil(val2 * (val1 / 100))
  3. end
  4. function expect(var1, var2)
  5. if var1 ~= var2 then
  6. error("Expected "..tostring(var2)..", got "..tostring(var1))
  7. end
  8. end
  9. function printTable(tbl, depth)
  10. if depth == nil then
  11. depth = 0
  12. end
  13. local pre = ""
  14. for i = 1, depth do
  15. pre = pre .. " "
  16. end
  17. local str = ""
  18. for key, val in pairs(tbl) do
  19. if type(val) == "table" then
  20. str = str .. pre .. key .. ": {\n"
  21. .. printTable(val, depth + 1)
  22. .. pre .. "}\n"
  23. else
  24. str = str .. pre .. key .. ": " .. tostring(val) .. "\n"
  25. end
  26. end
  27. return str
  28. end
  29. function writeFile(path, str)
  30. local h = fs.open(path, "w")
  31. h.write(str)
  32. h.close()
  33. end
  34. function appendFile(path, str)
  35. local h = fs.open(path, "a")
  36. h.write(str)
  37. h.close()
  38. end
  39. function newObject()
  40. local self = {}
  41. self.getset = function(name, getter, setter)
  42. self[name] = function(val)
  43. if val == nil then
  44. if getter == nil then
  45. error("No getter.")
  46. end
  47. return getter()
  48. else
  49. if setter == nil then
  50. error("No setter.")
  51. end
  52. setter(val)
  53. return self
  54. end
  55. end
  56. end
  57. return self
  58. end
  59. function makeEventListener(self)
  60. util.expect(type(self), "table")
  61. local callbacks = {}
  62. local anyListeners = {}
  63. self.emit = function(name, ...)
  64. local funcs = callbacks[name]
  65. if funcs == nil then
  66. return
  67. end
  68. for key, func in pairs(funcs) do
  69. func(...)
  70. end
  71. for key, func in pairs(anyListeners) do
  72. func(name, ...)
  73. end
  74. return self
  75. end
  76. self.on = function(name, func)
  77. if callbacks[name] == nil then
  78. callbacks[name] = {}
  79. end
  80. table.insert(callbacks[name], func)
  81. return self
  82. end
  83. self.onany = function(func)
  84. table.insert(anyListeners, func)
  85. return self
  86. end
  87. self.removeListener = function(name, listener)
  88. local funcs = callbacks[name]
  89. if funcs == nil then
  90. return
  91. end
  92. for key, func in pairs(funcs) do
  93. if func == listener then
  94. table.remove(funcs, key)
  95. end
  96. end
  97. return self
  98. end
  99. self.once = function(name, func)
  100. self.on(name, function(...)
  101. func(...)
  102. self.removeListener(name, func)
  103. end)
  104. return self
  105. end
  106. self.removeAllListeners = function()
  107. callbacks = {}
  108. anyListeners = {}
  109. return self
  110. end
  111. end
  112. function abstract()
  113. error("This method is abstract and must be overridden.")
  114. end