Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-19-2007, 08:59 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
TIniFile

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.
PHP Code:
// Loads an INI File into the object.
public function load(filename) {
  
// Declaring the temp variables
  
temp.file 0;
  
temp.0;
  
temp.key 0;
  
temp.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 (
0file.size(); ++) {
    
// 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.
      
file[i].pos("=");
      
// Skip the line if it doesn't exist
      
if (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 (== && 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(0e);
        
val file[i].substring(e+1, -1);
        
this.(@ key).(@ var) = val;
      }
    }
    else {
      
// Set the INI section.
      
key file[i].substring(1file[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.0;
  
temp.0;
  
temp.keys this.getdynamicvarnames();
  
temp.vars 0;
  
  for (
0keys.size(); ++) {
    
// 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 (
0vars.size(); ++) {
      
// 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(filename0);
}
// Clears all values for a certain INI section.
public function clearkey(keyname) {
  
temp.0;
  
temp.vars 0;
  
  
// Stop the function if its an actual object.
  
if (this.(@ keyname).type() != -1)
    return;
  
vars this.(@ keyname).getdynamicvarnames();
  for (
0vars.size(); ++) {
    
this.(@ keyname).(@ vars[i]) = null;
  }
}
// Copies variables from an INI section to an object (like object.loadvars())
public function copyvars(keynameobject) {
  
// 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.ithis.(@ 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:
PHP Code:
// 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 
PHP Code:
// 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
__________________

Last edited by Inverness; 08-19-2007 at 09:17 PM..
Reply With Quote
  #2  
Old 08-19-2007, 09:23 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Very nice
__________________
Reply With Quote
  #3  
Old 08-22-2007, 08:37 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
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
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #4  
Old 08-23-2007, 08:18 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
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.
Reply With Quote
  #5  
Old 08-28-2007, 01:24 PM
PrinceOfKenshin PrinceOfKenshin is offline
That guy with a beard
PrinceOfKenshin's Avatar
Join Date: May 2004
Location: Ontario, Canada
Posts: 819
PrinceOfKenshin has a spectacular aura aboutPrinceOfKenshin has a spectacular aura about
What is a Tinifile used for?
__________________


Quote:
Game Master (Server): Greetings PrinceOfKenshin, there are new posts on the forums that demand your urgent attention! God speed, the fate of the world is in your hands. If you fail, middle earth will forever be in the darkness and your children will be enslaved by McKain.
Reply With Quote
  #6  
Old 08-28-2007, 02:42 PM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by PrinceOfKenshin View Post
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.
Reply With Quote
  #7  
Old 08-28-2007, 09:23 PM
PrinceOfKenshin PrinceOfKenshin is offline
That guy with a beard
PrinceOfKenshin's Avatar
Join Date: May 2004
Location: Ontario, Canada
Posts: 819
PrinceOfKenshin has a spectacular aura aboutPrinceOfKenshin has a spectacular aura about
Quote:
Originally Posted by Twinny View Post
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?
__________________


Quote:
Game Master (Server): Greetings PrinceOfKenshin, there are new posts on the forums that demand your urgent attention! God speed, the fate of the world is in your hands. If you fail, middle earth will forever be in the darkness and your children will be enslaved by McKain.
Reply With Quote
  #8  
Old 08-28-2007, 09:47 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by PrinceOfKenshin View Post
kinda like a mudlib?
You can make a mudlib use ini files..
__________________
Reply With Quote
  #9  
Old 08-29-2007, 06:55 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by PrinceOfKenshin View Post
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.
Reply With Quote
  #10  
Old 08-29-2007, 01:20 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Twinny View Post
C&C3 Tiberium Wars just released an SDK....oh man.
!
__________________
Reply With Quote
  #11  
Old 12-28-2007, 12:26 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Simply excellent.
So much easier when the lines are commented with that happens on each of them.
__________________
Trying to be nice now...
Reply With Quote
  #12  
Old 12-28-2007, 01:44 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by bscharff View Post
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?
__________________
Reply With Quote
  #13  
Old 12-29-2007, 02:25 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Did you need to bump it again?
Quote:
Originally Posted by cbkbud View Post
Did you need to bump a 4 month old topic to say that?
__________________
Trying to be nice now...
Reply With Quote
  #14  
Old 12-29-2007, 02:41 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by bscharff View Post
Did you need to bump it again?
You obviously don't know the difference between replying to a topic and bumping the topic.
Quote:
Originally Posted by cbkbud View Post
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.
__________________
Reply With Quote
  #15  
Old 12-29-2007, 02:51 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Quote:
Originally Posted by Inverness View Post
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!
__________________
Trying to be nice now...
Reply With Quote
  #16  
Old 12-29-2007, 12:04 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by bscharff View Post
Thank you!
Way to avoid your lack of knowledge of what a bump is.
__________________
Reply With Quote
  #17  
Old 12-29-2007, 03:59 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Inverness View Post
Way to avoid your lack of knowledge of what a bump is.
Classic example of being owned.
__________________
Reply With Quote
  #18  
Old 12-29-2007, 10:54 PM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Quote:
Originally Posted by cbkbud View Post
Classic example of being owned.
Classic example of you constantly harassing me. Get a life.

That's a classic example of you getting owned.
__________________
Trying to be nice now...
Reply With Quote
  #19  
Old 12-30-2007, 01:49 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by bscharff View Post
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.
__________________
Reply With Quote
  #20  
Old 12-30-2007, 09:17 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
"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); ).
__________________
Trying to be nice now...

Last edited by bscharff; 12-30-2007 at 09:19 AM.. Reason: typo pl0x.
Reply With Quote
  #21  
Old 12-30-2007, 09:26 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
PHP Code:
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:
PHP Code:
function theFunction() {
  
temp.file = new TIniFile();
  
temp.file.load("data/random.ini");
  
// do stuff

__________________
Reply With Quote
  #22  
Old 12-30-2007, 09:36 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Thanks for the quick response!
Now I can say "great script!"
__________________
Trying to be nice now...
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.