If you want to tinker with what i was doing, here is the zip with the skin: In short this function converts this:
Code: Select all
[Variables]
color=255, 0, 0
background=20, 20, 20
font=250, 250, 250
font2=hello
test-test=2
Code: Select all
color="255,0,0"
background="20,20,20"
font="250,250,250"
font2="hello"
test-test="2"
I want to know if its good way to get those variables or is there some other very easy way of doing that. Let me know.
Function to convert *.inc file contents to table for lua to read:
Code: Select all
function ConvertVariablesList(file_path, output_variable)
-- Check if output_variable exists and is a table
if not output_variable or type(output_variable) ~= "table" then
print("Lua Error | ConvertVariablesList("..file_path..") Output variable is not a valid table.")
return
end
-- Open the selected file
local file = io.open(SKIN:GetVariable('@')..file_path, "r")
if not file then
print("Lua Error | ConvertVariablesList("..file_path..") couldn't open the file!")
return
end
-- Remove section indicator
local sectionIndicator = file:read("*line")
-- Convert rainmeter syntax into lua syntax
local line = file:read("*line")
while line do
-- Extract variable and value
local variable, value = line:match("^([^=]+)=(.+)$")
if variable and value then
value = value:gsub("%s*", "") -- Remove whitespace
output_variable[variable] = value
else
print("Lua Error | ConvertVariablesList("..file_path..") couldn't parse the variable '"..(variable or "").."'.")
file:close()
return
end
line = file:read("*line")
end
-- Close the file
file:close()
end
Code: Select all
function Initialize()
-- Set array for the variables
styles = {}
-- Collect data from the file
ConvertVariablesList('variables\\styles.inc', styles)
-- Example (color and background variables are from styles.inc file)
print(styles["color"], styles["background"])
end
Code: Select all
255,0,0 20,20,20
Code: Select all
function SaveVariablesList(file_path, input_variable)
-- Check if input_variable is a table
if type(input_variable) ~= "table" then
print("Lua Error | SaveVariablesList("..file_path..") is not table type variable!")
return
end
-- Open the file in write mode
local file = io.open(SKIN:GetVariable('@')..file_path, "w")
if not file then
print("Lua Error | SaveVariablesList("..file_path..") couldn't open the file!")
return
end
-- Write each variable and its value in Rainmeter syntax
for variable, value in pairs(input_variable) do
-- Handle errors during concatenation
local success, line = pcall(function()
return variable .. "=" .. value:gsub('"', '') .. "\n"
end)
if success then
file:write(line)
else
print("Lua Error | SaveVariablesList("..file_path..") Error occurred while processing variable '"..variable.."'.")
end
end
-- Close the file
file:close()
end
Example usage:
Code: Select all
function change4()
-- Specify the location and table which should be saved in *.inc file
SaveVariablesList('variables\\styles.inc', styles)
end