Cuberite Forum
INI Arrays - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: INI Arrays (/thread-1578.html)



INI Arrays - tryy3 - 09-12-2014

Hello, I am wondering if its possible to get Arrays with the cIniFile class?
I Know INI files can have arrays but looking at the docs, there is no functions for arrays.

Is there a different way to get arrays from INI files?


RE: INI Arrays - LO1ZB - 09-12-2014

What do you want to save in the INI Array?
Why not use a SQLLite database?


RE: INI Arrays - tryy3 - 09-12-2014

Its for a config, I need to save a motd message, and the motd needs to be multiple lines.


RE: INI Arrays - xoft - 09-12-2014

You could use several approaches here:
- Store the strings in a single value in an INI file, separated by a separator: "value1|value2|value3", then use StringSplitAndTrim() on the string value you read from the INI file
- Use a config file that is a Lua file. Such a config file must not be stored in the plugin's folder, but then is quite free to use any Lua to provide the configuration; then use Lua's dofile() function to read the config. The only downside is that it's difficult-to-impossible to make changes to such a file programmatically. Gallery plugin uses a Lua config file, you can use that as a reference.


RE: INI Arrays - tryy3 - 09-12-2014

I was thinking of using a seperator, only problem is that for a motd, the line would be really bad to read.
I was playing around with the lxp but I never figured out a good way to make it parse the XML file to a Table so that didn't work either.

Is there a reason why the cIniFile class doesn't have a function for arrays/tables?


RE: INI Arrays - tigerw - 09-13-2014

The server has never needed to use it, I suppose.


RE: INI Arrays - tryy3 - 09-13-2014

Is there any plans to add support for it?
Could be useful for message configs and similiar?


RE: INI Arrays - Seadragon91 - 09-13-2014

Hey,

Does this fit to you?
With GetNumValues() from cIniFile you can get the amount of values from a key, then you can foor loop over it. I use this nice idea in my skyblock plugin for the challenenges.

Messages.ini
Code:
[motd]
1=First motd
2=Second motd


[First motd]
1=Line 1
2=Line 2


[Second motd]
1=Line 1
2=Line 2
3=Line 3

TestPlugin.lua
PLUGIN = nil

function Initialize(Plugin)
    Plugin:SetName("TestPlugin")
    Plugin:SetVersion(1)

    PLUGIN = Plugin
   
    local MessagesIni = cIniFile()
    MessagesIni:ReadFile(PLUGIN:GetLocalDirectory() .. "/Messages.ini")
    
    local amountMOTD = MessagesIni:GetNumValues("motd")
    for i = 1, amountMOTD do
        LOG(MessagesIni:GetValue("motd", i))
        local motd = MessagesIni:GetValue("motd", i)
        local amountLines = MessagesIni:GetNumValues(motd)
        for line = 1, amountLines do
            LOG(MessagesIni:GetValue(motd, line))
        end
        LOG("")
    end
end

regards,
Seadragon91

Edit: Renamed l to line


RE: INI Arrays - tryy3 - 09-13-2014

Could work for the project I am working on, but would be nicer to have a real function for it.
In case you have a lot of lines and you have to change multiple lines, you might forget to edit the number, then the for loop will fail.


RE: INI Arrays - xoft - 09-13-2014

Seadragon91: just a small note, it's a bad idea to use "l" as the name for a variable, because of its visual similarity with "1". Those of us with a bit worse eyes will thank you for not doing it Smile