PDA

View Full Version : TIniFile


Inverness
08-19-2007, 09:59 PM
Oi, heres a little thing I whipped together before the loadini() thing was added, but I don't think that even has all the features as this.

This the script for my TIniFile which I use for things like my NPC Dialog system. Also for saving and loading mud objects.

Added comments to the script.

// Loads an INI File into the object.
public function load(filename) {
// Declaring the temp variables
temp.file = 0;
temp.i = 0;
temp.key = 0;
temp.e = 0;
temp.var = 0;
temp.val = 0;

// Load all the lines from the file.
file.loadlines(filename);
// Stop the loading if the file has no lines.
if (file.size() < 1) {
return;
}
for (i = 0; i < file.size(); i ++) {
// If the line starts with a semi-colon, skip it. (For comments)
if (file[i].starts(";"))
continue;
// Checking if the line is an INI section header (key)
if (!file[i].starts("[") && !file[i].ends("]")) {
// Check to see if a section has been found, can't load variables without one.
if (key == null)
continue;
// Get the position of the equal sign on the line.
e = file[i].pos("=");
// Skip the line if it doesn't exist
if (e < 0)
continue;
// If the line starts with an equal sign, append to last variable loaded.
// This is good for long multi-line amounts of text.
if (e == 0 && var != null) {
val = file[i].substring(1, -1);
this.(@ key).(@ var) @= val;
}
// If the equal sign isn't at the beginning, set a variable in the right section
// <varname>=<value>
else {
var = file[i].substring(0, e);
val = file[i].substring(e+1, -1);
this.(@ key).(@ var) = val;
}
}
else {
// Set the INI section.
key = file[i].substring(1, file[i].length() - 2);
}
}
}
// Saves this object as an INI file
public function save(filename) {
// Declaring temp variables, and getting dynamic variables in the object
temp.output = 0;
temp.i = 0;
temp.e = 0;
temp.keys = this.getdynamicvarnames();
temp.vars = 0;

for (i = 0; i < keys.size(); i ++) {
// If the dynamic variable is actually a value, skip it
if (this.(@ keys[i]).type() != -1)
continue;
// Write the section header
output.add("[" @ keys[i] @ "]");
// Get the dynamic variables for the section
vars = this.(@ keys[i]).getdynamicvarnames();
for (e = 0; e < vars.size(); e ++) {
// Add a line for the variable if its not zero.
if (this.(@ keys[i]).(@ vars[e]) != null) {
output.add(vars[e] @ "=" @ this.(@ keys[i]).(@ vars[e]));
}
}
}
// write the lines
output.savelines(filename, 0);
}
// Clears all values for a certain INI section.
public function clearkey(keyname) {
temp.i = 0;
temp.vars = 0;

// Stop the function if its an actual object.
if (this.(@ keyname).type() != -1)
return;
vars = this.(@ keyname).getdynamicvarnames();
for (i = 0; i < vars.size(); i ++) {
this.(@ keyname).(@ vars[i]) = null;
}
}
// Copies variables from an INI section to an object (like object.loadvars())
public function copyvars(keyname, object) {
// Stop the function if 'object' isn't an actual object.
if (object.type() != 2)
return;
// Stop the function if this.keyname is an object.
if (this.(@ keyname).type() != -1)
return;
for (temp.i: this.(@ keyname).getdynamicvarnames()) {
object.(@ i) = this.(@ keyname).(@ i);
}
}


I like INI files because the text editor in RC color-codes them for you ^^

Heres files that it works with:

// A TMudObject
[items]
i0=gold,600,"0,",0,0
i1=sword,1,"0,",0,0
i2=longsword,1,"0,","name_pre,","Rusted,"
[object]
health=50
id=Inverness
mana=100
maxhealth=50
maxmana=100
type=player
version=3
weight=14400
[spells]
s0=flare
s1=fireball


// Feature example
[section1]
var1=value
var2=Multi
= Line
= Text.
= Do not forget your spaces =P
[section2]
var3=value
;This is a comment
var4=3.14

If you haven't caught on yet, stuff can be read with inifileobject.sectionname.variable

Chompy
08-19-2007, 10:23 PM
Very nice :)

coreys
08-22-2007, 09:37 PM
Awesome. This actually showed me how ini files worked, I never knew and Stan always too mean to explain it to me. >=( Sheesh, not that complicated :(

Nice work though :D

Twinny
08-23-2007, 09:18 AM
I got used to ini files from my days of modding Command and Conquer. I feel sorry for anyone who has not made modifications to Tiberian Sun. So easy and so awesome.

PrinceOfKenshin
08-28-2007, 02:24 PM
What is a Tinifile used for?

Twinny
08-28-2007, 03:42 PM
What is a Tinifile used for?

Ini files are a great way to organise cleartext settings/attributes in a file. This system allows you to load up an ini file and access it's content easily.

PrinceOfKenshin
08-28-2007, 10:23 PM
Ini files are a great way to organise cleartext settings/attributes in a file. This system allows you to load up an ini file and access it's content easily.
kinda like a mudlib?

Chompy
08-28-2007, 10:47 PM
kinda like a mudlib?

You can make a mudlib use ini files..

Twinny
08-29-2007, 07:55 AM
kinda like a mudlib?

In the end, you could do the whole mud library in one ini file. I prefer individual files though...depends on your style in the end ^^.

On another note, C&C3 Tiberium Wars just released an SDK....oh man.

Chompy
08-29-2007, 02:20 PM
C&C3 Tiberium Wars just released an SDK....oh man.

:D!

bscharff
12-28-2007, 12:26 AM
Simply excellent.
So much easier when the lines are commented with that happens on each of them.

cbk1994
12-28-2007, 01:44 AM
Simply excellent.
So much easier when the lines are commented with that happens on each of them.

Did you need to bump a 4 month old topic to say that?

bscharff
12-29-2007, 02:25 AM
Did you need to bump it again?
Did you need to bump a 4 month old topic to say that?

Inverness
12-29-2007, 02:41 AM
Did you need to bump it again?You obviously don't know the difference between replying to a topic and bumping the topic.
Did you need to bump a 4 month old topic to say that?Get off his case, hes allowed to make his own comments if he wants to. You obviously don't understand when "bumps" are warranted. And considering this is a Code Gallery he can comment whenever he likes.

bscharff
12-29-2007, 02:51 AM
Get off his case, hes allowed to make his own comments if he wants to. You obviously don't understand when "bumps" are warranted. And considering this is a Code Gallery he can comment whenever he likes.

Thank you!

Inverness
12-29-2007, 12:04 PM
Thank you!Way to avoid your lack of knowledge of what a bump is.

cbk1994
12-29-2007, 03:59 PM
Way to avoid your lack of knowledge of what a bump is.

Classic example of being owned.

bscharff
12-29-2007, 10:54 PM
Classic example of being owned.

Classic example of you constantly harassing me. Get a life.

That's a classic example of you getting owned.

Inverness
12-30-2007, 01:49 AM
Classic example of you constantly harassing me. Get a life.

That's a classic example of you getting owned.No its not actually, and you still avoided my statement.

bscharff
12-30-2007, 09:17 AM
"I've never used the term 'bump' "
Is that what you were wanting?

Anyways, can your script be used like loadvars can ( obj.load(str); ) or does it have to be assigned ( var = load(str); )

It was giving me an error when using it in the first form ( obj.load(str); ).

Inverness
12-30-2007, 09:26 AM
function theFunction() {
temp.obj = new TStaticVar();
temp.obj.join("ClassWithTheScript");
temp.obj.load(thepath);
}

I have it set on the server so I can use:

function theFunction() {
temp.file = new TIniFile();
temp.file.load("data/random.ini");
// do stuff
}

bscharff
12-30-2007, 09:36 AM
Thanks for the quick response!
Now I can say "great script!"
:)