Jump to content

[C#] .scp (WAD pseudo database file parser)


Guest Mythli

Recommended Posts

In the hope someone find this snippet usefull

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Text.RegularExpressions;
using System.Data;

namespace scpparser.Classes
{
   public class ScpParser
   {
       public String IncludeRoot;
       public String FileName;
       public List<String> ParsedFiles = new List<String>();

       private Regex IncludeSearchPattern = new Regex("^(?!//)#include\\\\s*(.*)");
       private Regex SectionStartSearchPattern = new Regex("^(?!//)\\\\[(\\\\w+)\\\\s*([^]]+)\\\\]$");
       private Regex ValuePairSearchPattern = new Regex("^(?!//)(\\\\w+)\\\\s*=\\\\s*(.*)$");

       private String currentSection;
       private String currentPrimaryKey;

       public Dictionary<String, Dictionary<String, List<String>>> ParseResult = new Dictionary<String, Dictionary<String, List<String>>>();
       public Dictionary<String, String> ParsedColumns = new Dictionary<String, String>();

       public ScpParser()
       {
       }

       public ScpParser(String IncludeRoot, String FileName) : this()
       {
           this.Open(IncludeRoot, FileName);
       }

       public void HandleInclude(String FileName)
       {
           FileName = Path.Combine(this.IncludeRoot, FileName);
           this.ParseFile(FileName);
       }

       public void HandleSection(String SectionName, String PrimaryKey)
       {
           this.currentSection = SectionName;
           this.currentPrimaryKey = PrimaryKey;
           this.ParseResult.Add(PrimaryKey, new Dictionary<String, List<String>>());
       }

       public void HandleValuePair(String ValueName, String Value)
       {
           if (String.IsNullOrEmpty(this.currentSection) || String.IsNullOrEmpty(this.currentPrimaryKey))
               return;

           if (!this.ParseResult[this.currentPrimaryKey].ContainsKey(ValueName))
           {
               this.ParseResult[this.currentPrimaryKey].Add(ValueName, new List<String>());
           }

           if (!this.ParsedColumns.ContainsKey(ValueName))
               this.ParsedColumns.Add(ValueName, "");

           this.ParseResult[this.currentPrimaryKey][ValueName].Add(Value);
       }

       public void ParseLine(String Line)
       {
           Match match = null;
           match = this.IncludeSearchPattern.Match(Line);
           if (match.Success)
           {
               this.HandleInclude(match.Groups[1].Value);
               return;
           }
           match = this.SectionStartSearchPattern.Match(Line);
           if (match.Success)
           {
               this.HandleSection(match.Groups[1].Value, match.Groups[2].Value);
               return;
           }
           match = this.ValuePairSearchPattern.Match(Line);
           if (match.Success)
           {
               this.HandleValuePair(match.Groups[1].Value, match.Groups[2].Value);
               return;
           }
       }

       public void ParseFile(String FileName)
       {

           using (StreamReader fStream = File.OpenText(FileName))
           {
               String line;
               while ((line = fStream.ReadLine()) != null)
               {
                   this.ParseLine(line);
               }
           }

           this.ParsedFiles.Add(FileName);
       }

       public void Open(String IncludeRoot, String FileName)
       {
           this.IncludeRoot = IncludeRoot;
           this.FileName = FileName;
           this.ParseFile(FileName);
       }
   }
}

Link to comment
Share on other sites

WAD is a very old emulator supporting patch 1.x if you want to call it so (it is more about playing with the wow world) for more info read http://www.gotwow.ic.cz/wowd/

"Database" files in WAD look like this:

#include scripts/extra/creatures_auctioneers.scp
...
#include scripts/afg/afgcreatures.scp
#include scripts/afg/afgqiraj.scp
#include scripts/chronos/chronos_vendors.scp
#include scripts/chronos/chronos_creatures.scp

[creature 0]
name=Spawn Points (Only GM can see it)
faction=35
level=255
attack=2000 2200
bounding_radius=1.5
combat_reach=2.25
damage=0 2
maxhealth=56
model=262
size=0.5
type=1
maxmana=0
money=127500

[creature 1]
name=Spawn Point (Only GM can see it)
faction=35
level=255
attack=2000 2200
bounding_radius=1.5
combat_reach=2.25
damage=0 2
maxhealth=56
model=262
size=0.5
type=1
maxmana=0
money=127500

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use