Thread: MUD's
View Single Post
  #2  
Old 01-06-2008, 09:30 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
Well, there's different ways to make MUD's, but Inverness have taught me alot of what I can of MUDs :o

Well, for Ini files..

Make a wnpc or somewhere to store this script (To make custom objects):
PHP Code:
function onCreated()
{
  
this.scriptlogmissingfunctions false;
  
this.types NULL;
  
// Files
  
maketype("IniFile"0, {"file_ini"});
}
public function 
maketype(typenamesuperclasses)
{
  
temp.0;

  if (
makevar("T" typename) != null) {
    
makevar("T" typename).destroy();
  }
  
this.(@ "type_" typename) = new TStaticVar("T" typename);
  if (
super.type() == 2) {
    
this.(@ "type_" typename).joinedclasses super.joinedclasses;
  }
  for (
iclasses) {
    
this.(@ "type_" typename).join(i);
  }
  if (
this.types.index("T" typename) < 0) {
    
this.types.add("T" typename);
  }

And I usually put this in a class name that I used when creating the TIniFile custom object above:

("file_ini" as used above)
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);
  }

Then you can load an ini file by doing:

PHP Code:
function onCreated() {
  
temp.foo = new TIniFile();
  
temp.foo.load("mud/test.ini");

  echo(
temp.foo.section1.var1); // readed -> object.section.var
  // output = value1

And "mud/test.ini"

NPC Code:

[section1]
var1=value1
var2=value2



All credits to Inverness though

And I would suggest you to check out his two mudlibs that he released, it's worth learning from
__________________

Last edited by Chompy; 01-06-2008 at 09:46 PM..
Reply With Quote