Jump to content

Recommended Posts

Posted

Hi all,

I'd like to save some variables from a Lua script to use them after the server restart. 

 

Stupid exhample:

-Lua script has the Variable "Lives" to 4, then saves it to somewhere (how???)

-the server restarts

-Lua reload from somewhere (how???) the Variable "Lives" (so set it back to 4 just as saved)

 

Can anybody help? 😄 thanks for reading

Posted

Here is an example how to save and load a variable to/from files.
LFS and IO need to be remove from sanitization , do this at your own risk and always sanitize them after you don't need it anymore. This is security risk if left desanitized !

 

test = {}
test.workDir = = lfs.writedir() .. [[Scripts\]]
test.saveFilename = "mysavefile.lua"

test.saveFilePath = test.workDir .. test.saveFilename

-- Writing the path to the save file used above to dcs.log
env.info('-- TEST : SAVE Dir: ' .. test.saveFilePath)

-- My variables
test.myVariable = 0
test.myOtherVariable = 0

function test.load()
	-- Load the save file: (file must exist)
	assert(loadfile(test.saveFilePath))()
end



-- Call to change the variables or for testing!
function test.changeVariables()
	test.myVariable = 1
	test.myOtherVariable = 1
end


-- call this function to save to file
function test.save()

	wFile = io.open(test.saveFilePath, 'w')
	
	wFile:write('test = test or {}\n')
	wFile:write('test.myVariable = ' ..test.myVariable.. '\n')
	wFile:write('test.myOtherVariable = ' ..test.myOtherVariable.. '\n')
	
	wFile:close()
	wFile = nil
	
end

 

  • Like 1
Posted

This is my class for just the task, with push, pop, compare methods etc... You will have to replace some of the dependencies (a serializer and a ED SSE flag wrapper I think), let me know if you need help with how to use it.

	-- new 2022 pFlags
		P_FLAG = {}
		local pflags = {}
		local set_flag = jsb.set_flag_nl

		function pflags:pop(flag)
			if self[flag] then
				local tflag = self[flag]
				self[flag] = nil
				self:save()
				set_flag(flag, nil)
				return tflag
			end
		end

		function pflags:get(flag)
			return self[flag]
		end

		function pflags:cmp(flag, str)
			return self[flag] and self[flag] == str and true or false
		end

		function pflags:multi_add(flags)
			for flag, value in pairs (flags or {}) do
				self:add(flag, value)
				set_flag(flag, value)
			end
			self:save()
		end

		function pflags:add(flag, value)
			if not value and type(flag) == 'table' then
				self:multi_add(flag)
				return
			end
			self[flag] = value
			set_flag(flag, value)
			self:save()
		end

		function pflags:inc(flag, value)
			if self[flag] then
				if value then
					self[flag] = value
				else
					self[flag] = self[flag] + 1
				end
				set_flag(flag, self[flag])
				self:save()
			end
		end

		function pflags:reduce(flag, value)
			if self[flag] then
				if value then
					self[flag] = value
				else
					self[flag] = self[flag] - 1
				end
				set_flag(flag, self[flag])
				self:save()
			end
		end

		local jsb_tbl = jsb.tbl

		function pflags:save()
			if dont_save then return end
			local save_file = io.open((dPath or jsbFM.dPath()) .. "persistent_flags.lua", "w")
			if save_file then
				save_file:write(jsb_tbl(self, "pflags"))
				save_file:close()
			end
		end

		function pflags:load()
			local save_file = envLoad((dPath or jsbFM.dPath()) .. "persistent_flags.lua")
			if save_file and save_file.pflags then
				for flag, value in pairs (save_file.pflags) do
					self:add(flag, value)
				end
			end
		end

		P_FLAG.get = function() return pflags end
	--

 

  • Like 1

Creator & Developer of XSAF ::An AI model that wants to kill you, and needs no help from humans.

Discord: PravusJSB#9484   twitch.tv/pravusjsb  https://www.patreon.com/XSAF  https://discord.gg/pC9EBe8vWU https://bmc.link/johnsbeaslu

Work with me on Fiverr: https://www.fiverr.com/pravusjsb

Posted

Usage example

		if pOpt.o.pflag and file_exists((dPath or jsbFM.dPath()) .. "persistent_flags.lua") then
			pflags:load() -- only one load per session needed
			mtm = pflags:get("smtm")
		elseif pOpt.o.pflag then
			_log('Error calling the pFlags file :: NO PERSISTENCE FILE FOUND !')
			pflags:add('fresh_start', 1)
		else
			_log("Persistence flags are disabled.")
		end

 

  • Like 1

Creator & Developer of XSAF ::An AI model that wants to kill you, and needs no help from humans.

Discord: PravusJSB#9484   twitch.tv/pravusjsb  https://www.patreon.com/XSAF  https://discord.gg/pC9EBe8vWU https://bmc.link/johnsbeaslu

Work with me on Fiverr: https://www.fiverr.com/pravusjsb

  • 4 weeks later...
Posted
On 6/9/2023 at 12:28 PM, Kanelbolle said:

Here is an example how to save and load a variable to/from files.
LFS and IO need to be remove from sanitization , do this at your own risk and always sanitize them after you don't need it anymore. This is security risk if left desanitized !

 

test = {}
test.workDir = = lfs.writedir() .. [[Scripts\]]
test.saveFilename = "mysavefile.lua"

test.saveFilePath = test.workDir .. test.saveFilename

-- Writing the path to the save file used above to dcs.log
env.info('-- TEST : SAVE Dir: ' .. test.saveFilePath)

-- My variables
test.myVariable = 0
test.myOtherVariable = 0

function test.load()
	-- Load the save file: (file must exist)
	assert(loadfile(test.saveFilePath))()
end



-- Call to change the variables or for testing!
function test.changeVariables()
	test.myVariable = 1
	test.myOtherVariable = 1
end


-- call this function to save to file
function test.save()

	wFile = io.open(test.saveFilePath, 'w')
	
	wFile:write('test = test or {}\n')
	wFile:write('test.myVariable = ' ..test.myVariable.. '\n')
	wFile:write('test.myOtherVariable = ' ..test.myOtherVariable.. '\n')
	
	wFile:close()
	wFile = nil
	
end

 

What do you mean with

  • Remove LFS and IO from "Sanitization"? How?
  • "Sanitize"? Antivirus Scan?
Posted (edited)
9 minutes ago, DarK_FeneR996 said:

What do you mean with

  • Remove LFS and IO from "Sanitization"? How?
  • "Sanitize"? Antivirus Scan?

Here is a part of the Readme from my SPGG script:

 

Step 1 - Removing LFS and IO sanitation

It is not recommended to use this script if you are not aware of the risks of using “lfs” and “io” in DCS lua scripts. Scripts can access your filesystem if you enable LFS!

This functionality is off by default in DCS to protect the players from malicious code.

Use at your own risk!

Enable “lfs” and “io” in DCS Game Folder -> disk:\DCS World OpenBeta\Scripts\MissionScripting.lua

Change from:

do
sanitizeModule('os')
anitizeModule('io')
sanitizeModule('lfs')
G['require'] = nil
G['loadlib'] = nil
G['package'] = nil
end
 

Change to:

do
sanitizeModule('os')
--sanitizeModule('io')
--sanitizeModule('lfs')
G['require'] = nil
G['loadlib'] = nil
G['package'] = nil
end

Edited by Kanelbolle
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...