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

Global function hook (I've missed something obvious!)

Started by oreillynz, 05 April 2012 - 10:38 AM
oreillynz #1
Posted 05 April 2012 - 12:38 PM
- SOLVED -



I've just about finished hacking together an advanced persistent variable library, and thought it'd be simple to add 'drop in' support for encryption and decryption of the files.

So I've got two global variables, which by default are set to a "do nothing" function that returns the serialized string unaltered… but I can't work out why they don't get called. There's no debug output at all… and I was just about ready to contribute this in the API forum (It's always that last "2 second" feature that refuses to work..)


The initial state should be correctly set by this:

persistant_encryption = _doNothing
persistant_decryption = _doNothing

function _doNothing( value )
	print("Do nothing function")
	return value
end

… and take effect here (mid function):

	if not ( persistant_encryption == null ) then
		print("Encrypting")
		as_string = persistant_encryption( as_string )
	end


The full code for anyone interested:
(using the string utils API and immibis' awesome serialize function)
Spoiler


os.loadAPI("/strutils/StrUtils")
os.loadAPI("/immibis/serialize")


persistant_path = "/var"
if fs.exists( persistant_path ) == false then fs.makeDir( persistant_path ) end


persistant_encryption = _doNothing
persistant_decryption = _doNothing


function _doNothing( value )
	print("Do nothing function")
	return value
end


function _getPersistantVariablePath( path, name )
	-- will create path if required
	local complete_path = persistant_path..'/'..path
	local path_fragments = StrUtils.seperate(complete_path, "/")
	local current_path = ""
	for k,v in pairs( path_fragments ) do
		current_path = current_path.."/"..v
		if not fs.exists( current_path ) then fs.makeDir( current_path ) end
	end	
	return complete_path.."/"..name..".var"
end


function exists( path, name )
	local path = _getPersistantVariablePath( path, name )
	return fs.exists( path )
end


function delete( path, name )
	local variable_path = _getPersistantVariablePath( path, name )
	if exists( path, name ) then fs.delete( variable_path )
	else return "Variable doesn't exist"
	end
end


function update( path, name, value )
	local variable_path = _getPersistantVariablePath( path, name )
	local as_string = serialize.serialize( value )
	if not ( persistant_encryption == null ) then
		print("Encrypting")
		as_string = persistant_encryption( as_string )
	end
	local persistant_file = io.open( variable_path, "w" )
	if persistant_file then
		persistant_file:write( as_string )
		persistant_file:close()
	else return "Error opening file" end
end


function read( path, name )
	local variable_path = _getPersistantVariablePath( path, name )
	if fs.exists( variable_path ) then
		local persistant_file = io.open( variable_path, "r" )
		if persistant_file then
			content = persistant_file:read("*a")
			persistant_file:close()
			if not ( persistant_decryption == null ) then
				print("Decrypting")
				content = persistant_decryption( content )
			end
			return serialize.deserialize( content )
		else return "Error opening file" end
	else return "Variable doesn't exist"
	end
end


Xtansia #2
Posted 05 April 2012 - 01:06 PM

-- At this point _doNothing is nil as it has not been assigned to anything yet.
-- This is not O.O.P, Using a variable before it is set means it is nil.

persistant_encryption = _doNothing
persistant_decryption = _doNothing

function _doNothing( value )
	print("Do nothing function")
	return value
end

-- Only now does _doNothing contain the function.
-- So just move the top two lines to here.


-- The correct keyword for having no value is nil not null same meaning just Lua recognizes nil not null.
	if not ( persistant_encryption == null ) then
		print("Encrypting")
		as_string = persistant_encryption( as_string )
	end
oreillynz #3
Posted 05 April 2012 - 01:17 PM
Ah! Thank you!

It's working like a charm now.