Wolvan

Wolvan's Super Controller - Client

Mar 30th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | None | 0 0
  1. Arg = {...}
  2. os.pullEvent=os.pullEventRaw
  3. -- Start of persistent variables
  4. --Copyright (C) 2012 Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
  5. function exists(name)
  6.     if fs.exists('.vars/'..name..'.txt') then
  7.         return true
  8.     end
  9. end
  10.  
  11. function getType(name)
  12.     if exists(name) then
  13.         local file = io.open('.vars/'..name..'.txt', 'r')
  14.         local line = file:read('*l')
  15.         file:close()
  16.         return line
  17.     else
  18.         print('var.getType(): Variable does not exist')
  19.         error()
  20.     end
  21. end
  22.  
  23. function getValue(name)
  24.     if exists(name) then
  25.         if getType(name) ~= 'table' then
  26.             local file = io.open('.vars/'..name..'.txt', 'r')
  27.             local line = file:read('*l')
  28.             line = file:read('*l')
  29.             file:close()
  30.             if getType(name) == 'number' then
  31.                 return tonumber(line)
  32.             elseif getType(name) == 'string' then
  33.                 return line
  34.             elseif getType(name) == 'boolean' then
  35.                 if line == 'true' then
  36.                     return true
  37.                 elseif line == 'false' then
  38.                     return false
  39.                 end
  40.             elseif getType(name) == 'nil' then
  41.                 return nil
  42.             end
  43.         else
  44.             local file = io.open('.vars/'..name..'.txt', 'r')
  45.             local line = file:read('*l')
  46.             local lines = {}
  47.             local returnTable = {}
  48.             local i = 1
  49.             line = file:read('*l')
  50.             while line ~= nil do
  51.                 lines[i] = line
  52.                 line = file:read('*l')
  53.                 i = i + 1
  54.             end
  55.  
  56.             for _i = 1, #lines do
  57.                 local pos = string.find(lines[_i], '_')
  58.                 local _type = string.sub(lines[_i], 1, pos - 1)
  59.                 local value = string.sub(lines[_i], pos + 1)
  60.                 if _type == 'number' then
  61.                     returnTable[_i] = tonumber(value)
  62.                 elseif _type == 'string' then
  63.                     returnTable[_i] = value
  64.                 elseif _type == 'boolean' then
  65.                     if value == 'true' then
  66.                         returnTable[_i] = true
  67.                     elseif value == 'false' then
  68.                         returnTable[_i] = false
  69.                     end
  70.                 elseif _type == 'nil' then
  71.                     returnTable[_i] = nil
  72.                 elseif _type == 'table' then
  73.                     print('var.getValue(): You cannot have a table inside a table')
  74.                 end
  75.             end
  76.  
  77.             return returnTable
  78.         end
  79.     else
  80.         print('var.getValue(): Variable does not exist')
  81.         error()
  82.     end
  83. end
  84.  
  85. function setType(name, _type)
  86.     if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  87.         print('var.setType(): Unsupported type, or the variable already exists')
  88.         error()
  89.     else
  90.         local file = io.open('.vars/'..name..'.txt', 'w')
  91.         file:write(_type)
  92.         file:close()
  93.     end
  94. end
  95.  
  96. function setValue(name, value)
  97.     if exists(name) then
  98.         if tostring(type(value)) ~= getType(name) then
  99.             print('var.setValue(): Wrong type: '..type(value))
  100.             error()
  101.         end
  102.         if getType(name) ~= 'table' then
  103.             local file = io.open('.vars/'..name..'.txt', 'r')
  104.             local firstLine = file:read('*l')
  105.             file:close()
  106.             local file = io.open('.vars/'..name..'.txt', 'w')
  107.             file:write(firstLine..'\n'..tostring(value))
  108.             file:close()
  109.         else
  110.             local file = io.open('.vars/'..name..'.txt', 'r')
  111.             local firstLine = file:read('*l')
  112.             file:close()
  113.             local file = io.open('.vars/'..name..'.txt', 'w')
  114.             file:write(firstLine..'\n')
  115.             for i = 1, #value do
  116.                 file:write(type(value[i])..'_'..tostring(value[i])..' \n')
  117.             end
  118.             file:close()
  119.         end
  120.     else
  121.         print('var.setValue(): Variable does not exist')
  122.         error()
  123.     end
  124. end
  125.  
  126. function create(name, _type)
  127.     if not fs.exists('.vars') then fs.makeDir('.vars') end
  128.     if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  129.         print('var.create(): Unsupported type, or the variable already exists')
  130.         error()
  131.     else
  132.         local file = io.open('.vars/'..name..'.txt', 'w')
  133.         file:write(_type)
  134.         file:close()
  135.     end
  136. end
  137.  
  138. function delete(name)
  139.     if exists(name) then
  140.         fs.delete('.vars/'..name..'.txt')
  141.     else
  142.         print('var.delete(): Variable does not exist')
  143.         error()
  144.     end
  145. end
  146. -- End of persistant variables
  147. function Use()
  148. print("Use: [FILENAME] <reconf>")
  149. print("<> optional")
  150. end
  151. function FC()
  152. shell.run("clear")
  153. print("Welcome to the First Time Configuration")
  154. if exists("pass") then
  155. print("Please type the password")
  156. write("Password: ")
  157. pass3 = read('*')
  158. if pass3 == getValue("pass") then
  159. shell.run("clear")
  160. writeconf()
  161. else
  162. print("Wrong password")
  163. sleep(2)
  164. os.reboot()
  165. end
  166. else
  167. writeconf()
  168. end
  169. end
  170. function writeconf()
  171. print("")
  172. print("Where is the Redstone-Input located?")
  173. side = read()
  174. print("How's the terminal called?")
  175. write("Name: ")
  176. termn = read()
  177. print("Redstone has to be on to start PC?")
  178. write("y/n: ")
  179. redon = read()
  180. print("Please set a config pass")
  181. write("Password: ")
  182. pass1 = read("*")
  183. write("Confirm: ")
  184. pass2 = read("*")
  185. if pass1 == pass2 then
  186. if redon == "Y" then redon = "y" end
  187. if redon == "N" then redon = "n" end
  188. shell.run("clear")
  189. print("Are these options correct?")
  190. print("")
  191. print("Side: "..side)
  192. print("Name of the terminal: "..termn)
  193. print("Redstone on to start: "..redon)
  194. print("")
  195. write("y/n: ")
  196. confirm = read()
  197. if confirm == "Y" or confirm == "y" then
  198. if exists ("pass") then delete("pass") end
  199. if exists ("fconf") then delete("fconf") end
  200. if exists ("stside") then delete("stside") end
  201. if exists ("stredon") then delete("stredon") end
  202. if exists ("termn") then delete("termn") end
  203. print("Saving config")
  204. create("pass", "string")
  205. create("fconf", "string")
  206. create("stside", "string")
  207. create("stredon", "string")
  208. create("termn", "string")
  209. setValue("stredon", redon)
  210. setValue("stside", side)
  211. setValue("termn", termn)
  212. setValue("pass", pass1)
  213. setValue("fconf", "true")
  214. os.reboot()
  215. else
  216. os.reboot()
  217. end
  218. else
  219. print("Passwords do not match")
  220. sleep(2)
  221. os.reboot()
  222. end
  223. end
  224. if #Arg == 0 then
  225. if exists("fconf") then
  226. if getValue("fconf") == "true" then
  227. shell.run("clear")
  228. if getValue("stredon") == "n" then
  229. print("Welcome to the terminal "..getValue("termn"))
  230. else
  231. if rs.getInput(getValue("stside")) == false then
  232. os.shutdown()
  233. end
  234. print("Welcome to terminal "..getValue("termn"))
  235. end
  236. else
  237. FC()
  238. end
  239. else
  240. FC()
  241. end
  242. elseif #Arg == 1 and Arg[1] == "reconf" then
  243. FC()
  244. else
  245. Use()
  246. end
Advertisement
Add Comment
Please, Sign In to add comment