It is currently April 19th, 2024, 7:31 am

[SOLVED] Check if Config is active

Discuss the use of Lua in Script measures.
prince1142003
Posts: 56
Joined: December 27th, 2011, 12:32 pm

[SOLVED] Check if Config is active

Post by prince1142003 »

Is there a way to check if a config is active in Lua?
Last edited by prince1142003 on May 6th, 2013, 1:35 am, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Check if Config is active

Post by jsmorley »

You can open the Rainmeter.ini file using the #SETTINGSPATH# variable to get to the right folder (so you account for both normal and portable installs), then read the file and look for the config in question, which will be in Rainmeter.ini as [ConfigName].

If the config is not there, it obviously isn't loaded.

If it is there, then you need to look for the next occurrence of "Active=". If the value of that key is "0", then the config is not loaded. If it is other than 0 (and it might be more than 1) it is loaded.

All that is a tad complicated, though. An alternative is to put a variable in the skin in question.

[Variables]
SkinLoaded=1

Then you can check for that variable in Lua. If the skin is loaded, you will get "1". If not, you will get "nil", which you can test for.
prince1142003
Posts: 56
Joined: December 27th, 2011, 12:32 pm

Re: Check if Config is active

Post by prince1142003 »

Thanks, I managed to get it working using the first method. I needed to check from any config if a config stored in a variable (that can change) is active, so the second method wouldn't really work.

Here's the code I'm using:

Code: Select all

function CheckIfConfigActive(Config)
	if Config~=nil then
		local iniTable = ReadIni(SKIN:GetVariable('SETTINGSPATH')..'Rainmeter.ini');
		if iniTable[Config:lower()] then
			return tonumber(iniTable[Config:lower()]['active'])>0;
		end
	end
	return false;
end

function ReadIni(inputfile)
	local file = assert(io.open(inputfile, 'r'), 'Unable to open ' .. inputfile)
	local tbl, section = {}
	local num = 0
	for line in file:lines() do
		num = num + 1
		if not line:match('^%s-;') then
			local key, command = line:match('^([^=]+)=(.+)')
			if line:match('^%s-%[.+') then
				section = line:match('^%s-%[([^%]]+)'):lower()
				if not tbl[section] then tbl[section] = {} end
			elseif key and command and section then
				tbl[section][key:lower():match('^s*(%S*)%s*$')] = command:match('^s*(.-)%s*$')
			elseif #line > 0 and section and not key or command then
				print(num .. ': Invalid property or value.')
			end
		end
	end
	if not section then print('No sections found in ' .. inputfile) end
	file:close()
	return tbl
end