This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mads's profile picture

[1.0+][API][OOP]OVAR API - Persistant Variables

Started by Mads, 30 March 2012 - 06:46 PM
Mads #1
Posted 30 March 2012 - 08:46 PM
This is my new API, called OVAR.
This is kind of like my older API, Consistant Variables, but it is more advanced, and flexible.

Functions
Spoiler


@var = ovar.new(name, value) --Will create a new variable named @name, and load it into the cache
@var = ovar.load(name) --Will load an earlier used variable into the cache
@var:save() --Will save the current state of the variable
@var:unload() --Will unload the variable from the cache


Examples
Spoiler

Creating a variable, and printing the type and value, and then unloading the variable
Spoiler

local x = ovar.new("test", 10)
x:save()
print(type(x.value).." "..x.value)
x:unload()

----------------------------------------
Output:
number 10

Loading a variable, changing it's value, and printing the type and value(Using the variable from before)
Spoiler

local x = ovar.load("test")
x.value = {"Hello ", "World!"} --Yes, it supports tables(Not tables inside tables :'( )
x:save()
print(type(x.value).." "..x.value[1]..x.value[2])
x:unload()

----------------------------------------
Output:
table Hello World!


Programs using this API
Spoiler

Example login program by me
Spoiler

function newUser(_name, _pass)
	if ovar.exists("forum_example_user_".._name) then
		return false
	end
	
	local user = ovar.new("forum_example_user_".._name, _pass)
	user:save()
	user:unload()
	return true
end

function login(_name, _pass)
	if not ovar.exists("forum_example_user_".._name) then
		return false
	end
	
	local pass = ovar.load("forum_example_user_".._name)
	if _pass == pass.value then
		pass:unload()
		return true
	end
end

function start()
	print("Options:n1. Loginn2. New User")
	io.write("Choice: ")
	local choice = tonumber(io.read())
	io.write("Username: ")
	local user = tostring(io.read())
	io.write("Password: ")
	local pass = tostring(io.read())
	
	if choice ~= 1 and choice ~= 2 then
		print("Unknown option")
		return
	elseif choice == 1 then
		if login(user, pass) then
			print("Success!")
			return
		else
			print("Username/password incorrect")
			return
		end
	elseif choice == 2 then
		if newUser(user, pass) then
			print("Success!")
		else
			print("User already exists")
		end
	end
end

newUser("admin", "admin")
start()


Code
Spoiler


--Copyright (C) Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
--[[
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local t = {}
local lv = {}
t.__index = t

function new(_name, _value)
	if lv[_name] then
		print("Variable is already loaded")
		return false
	end
	
	local _t = {}
	_t.name = _name
	_t.value = _value
	setmetatable(_t, t)
	lv[_name] = true
	return _t
end

function exists(_name)
	local file = io.open("vars/".._name..".txt", "r")
	if file ~= nil then
		file:close()
		return true
	else
		return false
	end
end

function load(_name)
	if lv[_name] then
		print("Variable is already loaded")
		return
	end

	local _t = {}
	_t.name = _name
	_t.value = dofile("vars/".._name..".txt")
	setmetatable(_t, t)
	lv[_name] = true
	return _t
end

function t:save()
	if lv[self.name] then	
		local saveStr = ""
		if type(self.value) == "table" then
			saveStr = "{"
			for i = 1, #self.value do
				if type(self.value[i]) == "string" then
					saveStr = saveStr..'"'
					saveStr = saveStr..tostring(self.value[i])
					saveStr = saveStr..'"'
				else
					saveStr = saveStr..tostring(self.value[i])
				end
				
				if i ~= #self.value then
					saveStr = saveStr..", "
				end
			end
			saveStr = saveStr.."}"
		else
			if type(self.value) == "string" then
				saveStr = saveStr..'"'
				saveStr = saveStr..tostring(self.value)
				saveStr = saveStr..'"'
			else
				saveStr = tostring(self.value)
			end
		end
		
		local file = io.open("vars/"..self.name..".txt", "w")
		file:write("return "..saveStr)
		file:close()
	else
		print("Variable is not loaded")
	end
end

function t:unload()
	if not lv[self.name] then
		print("Variable is not loaded")
		return false
	end
	
	lv[self.name] = false
end
nitebomber51 #2
Posted 03 April 2012 - 10:20 AM
hey man looking at your code i'm a little tired so i can't understand this part

if ovar.exists("forum_example_user_".._name) then
so at the "forum_example_user_" do we change that to somthing else or does it work "as is"
Mads #3
Posted 03 April 2012 - 05:21 PM
hey man looking at your code i'm a little tired so i can't understand this part

if ovar.exists("forum_example_user_".._name) then
so at the "forum_example_user_" do we change that to somthing else or does it work "as is"

It will works "as is"
nitebomber51 #4
Posted 04 April 2012 - 01:29 AM
hey man looking at your code i'm a little tired so i can't understand this part

if ovar.exists("forum_example_user_".._name) then
so at the "forum_example_user_" do we change that to somthing else or does it work "as is"

It will works "as is"
thanks
RODLON #5
Posted 03 May 2012 - 10:11 PM
Hey, does it works in computercraft 1.33?
I'm triyng to use your api but I'm getting errors when saving the variables:

r = ovar.new("adiuwusb",234)
r:save()
print(r.value)
r:unload()
ovar:84: attemp to index ? (a nil value)