Advertisement
XDemonic

Config - API

Aug 29th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local io = require("io")
  3. local shell = require("shell")
  4. local Dir = shell.getWorkingDirectory()
  5.  
  6. local Conf = {}
  7.  
  8. ----API Functions---
  9.  
  10. function Conf.GetSide(Side)
  11.     if (Side == "bottom") or (Side == "down") then
  12.         return 0
  13.     elseif (Side == "top") or (Side == "up") then
  14.         return 1
  15.     elseif (Side == "back") or (Side == "north") then
  16.         return 2
  17.     elseif (Side == "front") or (Side == "south") then
  18.         return 3
  19.     elseif (Side == "right") or (Side == "west") then
  20.         return 4
  21.     elseif (Side == "left") or (Side == "east") then
  22.         return 5
  23.     end
  24. return nil
  25. end
  26.  
  27. function Conf.ConfigExists(File)
  28. fs.makeDirectory(Dir.."/Config")
  29. File = Dir.."/Config/"..File
  30.     if fs.exists(File) then
  31.         return true
  32.     else
  33.         return false
  34.     end
  35. end
  36.  
  37. function Conf.GetConfig(File, Line)
  38. File = Dir.."/Config/"..File
  39. if not fs.exists(File) then
  40. return nil
  41. end
  42.     local Lines = { }
  43.     local Index = 0
  44.     for LINE in io.lines(File) do
  45.         Lines[Index] = LINE
  46.         Index = Index + 1
  47.     end
  48.     if Lines[Line] == nil then
  49.         return nil
  50.     else
  51.         OUT = Lines[Line]
  52.         if(OUT == "true") then
  53.             return true
  54.         elseif(OUT == "false") then
  55.             return false
  56.         else
  57.             return OUT
  58.         end
  59.     end
  60. end
  61.  
  62. function Conf.EditConfig(File, Line, Name, Type)
  63.     File = Dir.."/Config/"..File
  64.     CreateConfig(File)
  65.     local Lines = {}
  66.     local Index = 0
  67.     for LINE in io.lines(File) do
  68.         Lines[Index] = LINE
  69.         Index = Index + 1
  70.     end
  71.     print("Lines: " ..Index)
  72.     local CorrectSyntax = false
  73.     local Input
  74.     while CorrectSyntax == false do
  75.         print(Name)
  76.         Input = io.read()
  77.         if Type == "Direction" then if IsDirection(Input) then CorrectSyntax = true else print("Incorrect Syntax, Must Be Direction! (down, up, north, south, west, east)") end end
  78.         if Type == "Facing" then if IsFacing(Input) then CorrectSyntax = true else print("Incorrect Syntax, Must Be Facing! (top, bottom, left, right, back, front)") end end
  79.         if Type == "Number" then if IsNumber(Input) then CorrectSyntax = true else print("Incorrect Syntax, Must Be Number!") end end
  80.         if Type == "Boolean" then if IsBoolean(Input) then CorrectSyntax = true else print("Incorrect Syntax, Must Be Boolean!") end end
  81.         if Type == "String" then CorrectSyntax = true end
  82.     end
  83.     FILE = fs.open(File, "w")
  84.     Index = -1
  85.     local NumberOfLines = #Lines
  86.     local ConfigLength
  87.     if NumberOfLines >= Line then ConfigLength = NumberOfLines else ConfigLength = Line end
  88.     for I = 0, ConfigLength do
  89.         if I == Line then
  90.         FILE:write(Input.."\n")
  91.         else
  92.         if Lines[I] == nil then FILE:write("\n") else FILE:write(Lines[I].."\n") end
  93.         end
  94.     end
  95.     FILE:close()
  96. end
  97.  
  98. -----------------
  99. -----------------
  100. -----------------
  101.  
  102. function IsNumber(str)
  103. if tonumber(str) ~= nil then
  104. return true
  105. end
  106. return false
  107. end
  108.  
  109. function IsFacing(str)
  110.     if str == "top" or str == "bottom" or str == "left" or str == "right" or str == "front" or str == "back" then
  111.     return true
  112.     end
  113.     return false
  114. end
  115.  
  116. function IsDirection(str)
  117.     if str == "down" or str == "up" or str == "north" or str == "south" or str == "west" or str == "east" then
  118.     return true
  119.     end
  120.     return false
  121. end
  122.  
  123. function IsBoolean(str)
  124.     if str == "true" or str == "false" then
  125.     return true
  126.     end
  127.     return false
  128. end
  129.  
  130. function CreateConfig(File)
  131.     if not fs.exists(File) then
  132.         local FILE = fs.open(File, "w")
  133.         FILE:write("==Config==\n")
  134.         FILE:close()
  135.     end
  136. end
  137.  
  138. return Conf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement