Posted 03 June 2012 - 06:47 PM
Hello people, Today, I have a new API to show you guys!
Introduction
I was writing a program, when I noticed that I needed a register to save my variables. So I build one!
What does it do First it makes a new file named : "registerFile" in the map "saves\[World name]\computer\[computer ID]\register". In this file, all the variables will be saved, so not one file for every variable! The file looks like this :
This API comes with a few commands to make easier to handle.
Features
Mediafire link : http://www.mediafire...ipm89gy1ypt4rss
Commands
varRegister.getVersion() – return the current version
varRegister.saveVar(variablename, input) – This saves a variable, or if it already exists, it will overwrite it. for example :
varRegister.loadVar(variable) – This loads the variable, it will return as an string,it will return nil when nothing is found. for example :
varRegister.ifExists(variable) – returns a boolean if the variable exists, for example :
varRegister.list() – This will return all variables in an array, for example :
This will print all the variables, you can also pick a single variable :
Code
Installation
Introduction
I was writing a program, when I noticed that I needed a register to save my variables. So I build one!
What does it do First it makes a new file named : "registerFile" in the map "saves\[World name]\computer\[computer ID]\register". In this file, all the variables will be saved, so not one file for every variable! The file looks like this :
variable1 = something
lol = Lauging Out Loud
count = 181
This API comes with a few commands to make easier to handle.
Features
- It uses only one file!
- The file and the map will be created by itself.
- You can acces all the variables on anytime and with any program you want.
- It's possible to insert a new variable or change a variable manual.
- Easy to use!
- Converts and decode variables that can break register [Ver 0.1.1]
- Place the file into : ".minecraft\mods\ComputerCraft\lua\rom\apis\"
- The maximum length of a variable is 35.
- I don't know really how to start the API, because it starts automatic on my terminal? But I quess you need to do : os.loadAPI("/rom/apis/varRegister")
- This is still in beta.
- Please, if you found any error or problem, send me a message or post it here!
Mediafire link : http://www.mediafire...ipm89gy1ypt4rss
Commands
varRegister.getVersion() – return the current version
varRegister.saveVar(variablename, input) – This saves a variable, or if it already exists, it will overwrite it. for example :
varRegister.saveVar("count", "1")
varRegister.loadVar(variable) – This loads the variable, it will return as an string,it will return nil when nothing is found. for example :
if varRegister.loadVar("count") == "20" then print "Yeah, variable 'count' == 20 :D/>/>" else print "Ahhhh the variable isn't 20 :)/>/>" end
varRegister.ifExists(variable) – returns a boolean if the variable exists, for example :
if not varRegister.ifExists("count") then varRegister.saveVar("count", "1") else varRegister.saveVar("count", (tonumber(varRegister.loadVar("count"))+1)) end
varRegister.list() – This will return all variables in an array, for example :
local list = varRegister.list() for k,l in pairs(list) do print(k.." = "..l) end
This will print all the variables, you can also pick a single variable :
local list = varRegister.list() print(list["count"])
Code
Spoiler
function getVersion()
return "0.1.1"
end
local function makeLocation()
if not fs.isDir("register") then fs.makeDir("register") end
if not fs.exists("register/registerFile") then
createMap = fs.open("register/registerFile", "w")
createMap.close()
end
end
local function decodeData(data)
for i=1,20,1 do
if string.sub(data, i, (i)) == "=" then
return string.sub(data, 1, (i-2)), string.sub(data, (i+2), string.len(data))
end
end
end
local function convertData(cleanData)
local chars = {"=", "/n", " "}
local changeTo = {"$&1", "$&2", "$&3"}
local newData = cleanData
for i=1, table.getn(chars), 1 do
newData = string.gsub(newData, chars[i], changeTo[i])
end
return newData
end
local function convertBack(convertedData)
local chars = {"$&1", "$&2", "$&3"}
local changeTo = {"=", "/n", " "}
local newData = convertedData
for i=1, table.getn(chars), 1 do
newData = string.gsub(newData, chars[i], changeTo[i])
end
return newData
end
local function changeVar(data, varInput)
for i=1,35,1 do
if string.sub(data, i, (i)) == "=" then
return string.sub(data, 1, (i)).." "..convertData(varInput)
end
end
end
function ifExists(varName)
makeLocation()
registerFile = fs.open("register/registerFile", "r")
check = registerFile.readLine() or ""
while check ~= "" and check ~= nil do
if convertData(decodeData(check)) == convertData(varName) then
return true
end
check = registerFile.readLine() or ""
end
registerFile.close()
return false
end
function list()
makeLocation()
local registerFile = fs.open("register/registerFile", "r")
local varList = {}
local check = registerFile.readLine() or ""
while check ~= "" and check ~= nil do
variable, answer = decodeData(check)
varList[convertBack(variable)] = convertBack(answer)
check = registerFile.readLine() or ""
end
registerFile.close()
return varList
end
function loadVar(varName)
makeLocation()
local registerFile = fs.open("register/registerFile", "r")
local check = registerFile.readLine() or ""
while check ~= "" and check ~= nil do
variable, answer = decodeData(check)
if convertBack(variable) == varName then
return convertBack(answer)
end
check = registerFile.readLine() or ""
end
registerFile.close()
return nil
end
function saveVar(varName, varInput)
if varName ~= nul and varInput ~= nul then
makeLocation()
if ifExists(varName) then
local registerFile = fs.open("register/registerFile", "r")
local check = registerFile.readLine() or ""
local newFile = ""
while check ~= "" and check ~= nil do
if convertData(decodeData(check)) == convertData(varName) then
newFile = newFile..changeVar(check, varInput).."\n"
else
newFile = newFile..check.."\n"
end
check = registerFile.readLine() or ""
end
registerFile.close()
local save = fs.open("register/registerFile", "w")
save.write(newFile)
save.close()
else
local oldFile = fs.open("register/registerFile", "r")
oldFile = oldFile.readAll()
local newFile = fs.open("register/registerFile", "w")
if oldFile ~= "" then
newFile.write(oldFile.."\n"..convertData(varName).." = "..convertData(varInput))
else
newFile.write(oldFile..convertData(varName).." = "..convertData(varInput))
end
newFile.close()
return true
end
else
print "Missing arguments, please enter in order : varName, varInput"
return false
end
end
Installation
- Download the file from mediafire.
- go to this derictory "%appdata%\.minecraft\mods\ComputerCraft\lua\rom\apis" and place the file you downloaded from mediafire.
- Write the following line into your code : os.loadAPI("/rom/apis/varRegister")
- Have fun writing applications with this ^^.
- Fix some simple bugs
- Improve the performance
- Add more commands
- Add suggestions?