Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace gms_pricer.classes
- {
- //class Category_674 {
- //class M9SD {type = "trade_weapons";buy[] ={17500,"Coins"};sell[] ={7500,"Coins"};};
- //class glock17_EP1 {type = "trade_weapons";buy[] ={500,"Coins"};sell[] ={200,"Coins"};};
- //class Colt1911 {type = "trade_weapons";buy[] ={1000,"Coins"};sell[] ={500,"Coins"};};
- //class M9 {type = "trade_weapons";buy[] ={900,"Coins"};sell[] ={5000,"Coins"};};
- //class MakarovSD {type = "trade_weapons";buy[] ={5000,"Coins"};sell[] ={1000,"Coins"};};
- //class revolver_gold_EP1 {type = "trade_weapons";buy[] ={20000,"Coins"};sell[] ={7500,"Coins"};};
- //class Makarov {type = "trade_weapons";buy[] ={200,"Coins"};sell[] ={100,"Coins"};};
- //class revolver_EP1 {type = "trade_weapons";buy[] ={1850,"Coins"};sell[] ={500,"Coins"};};
- //};
- /// <summary>
- /// Supporting class to handle line structures for LINQ queries
- /// </summary>
- class SQFLine
- {
- public int LineNumber;
- public string LineText;
- }
- class SQFFile
- {
- /// <summary>
- /// Filename + path to file
- /// </summary>
- public string FileName { get; set; }
- /// <summary>
- /// List of SQFCategories inside the file
- /// </summary>
- public List<SQFCategory> Categories { get; set; }
- /// <summary>
- /// Method to get a list of categories in the file
- /// </summary>
- /// <returns></returns>
- public List<SQFCategory> GetCategories()
- {
- List<SQFCategory> returnValue = new List<SQFCategory>();
- StreamReader sr = new StreamReader(@"C:\Users\admin\Documents\GitHub\BigTDayZEpoch\MPMissions\DayZ_Epoch_11_vp.Chernarus\Scripts\Trader_Items\Category\test.hpp");
- List<SQFLine> linesofFile = new List<SQFLine>();
- string line = string.Empty;
- int a = 1;
- while ((line = sr.ReadLine()) != null)
- {
- SQFLine sqLine = new SQFLine();
- sqLine.LineNumber = a;
- sqLine.LineText = line;
- // add to our master list
- linesofFile.Add(sqLine);
- a++;
- }
- // parse out lines containing cats
- List<SQFLine> catLines = (from l in linesofFile
- where l.LineText.Contains("Category")
- select l).ToList<SQFLine>();
- // parse out our obj classes
- List<SQFLine> objLines = (from l in linesofFile
- where l.LineText.Contains("class") && (!l.LineText.Contains("Category"))
- select l).ToList<SQFLine>();
- // parse out lines containing props
- List<SQFLine> propLines = (from l in linesofFile
- where l.LineText.Contains("type") || l.LineText.Contains("buy") || l.LineText.Contains("sell")
- select l).ToList<SQFLine>();
- // process our categories, objects, and props
- List<SQFCategory> categories = new List<SQFCategory>();
- foreach (SQFLine catLine in catLines)
- {
- SQFCategory cat = new SQFCategory();
- // process cat
- if (cat.ParseCategoryInformation(catLines, catLine.LineNumber))
- {
- foreach (SQFLine objLine in objLines)
- {
- SQFobj obj = new SQFobj();
- if (obj.ParseObjectInformation(objLines, objLine.LineNumber))
- {
- // process props for obj
- obj.ParseObjProps(propLines);
- }
- }
- }
- returnValue.Add(cat);
- }
- return returnValue;
- }
- /// <summary>
- /// Main entry point for our object
- /// </summary>
- /// <param name="filename">This can be our current objects file name if not specified</param>
- /// <returns>True False if process was successful</returns>
- public bool parseFile(string filename)
- {
- bool returnValue = true; // assume true
- return returnValue;
- }
- }
- /// <summary>
- /// class for handling our individual categories in a hpp file
- /// </summary>
- class SQFCategory
- {
- /// <summary>
- /// Class name of category ex Category_674
- /// </summary>
- public string CategoryName { get; set; }
- /// <summary>
- /// List of our current category's items
- /// </summary>
- public List<SQFobj> SQFItems { get; set; }
- /// <summary>
- /// The first line where we see the category
- /// </summary>
- public int StartLine { get; set; }
- /// <summary>
- /// The Next reference to a category
- /// </summary>
- public int LastLine { get; set; }
- /// <summary>
- /// Method to get list of our SQF objects in specified category
- /// </summary>
- /// <param name="CategoryName"></param>
- /// <returns></returns>
- public List<SQFobj> GetSQFObjects(string CategoryName)
- {
- List<SQFobj> returnValue = new List<SQFobj>();
- return returnValue;
- }
- /// <summary>
- /// Main entry point we parse all the info for our category here
- /// </summary>
- /// <param name="CatLines"></param>
- /// <param name="start"></param>
- /// <returns></returns>
- public bool ParseCategoryInformation(List<SQFLine> CatLines, int start)
- {
- bool returnValue = true;
- SQFLine currentLine = (from l in CatLines
- where l.LineNumber == start
- select l).ToList<SQFLine>().First();
- this.StartLine = currentLine.LineNumber;
- // failover for single class files
- try
- {
- SQFLine nextLine = (from l in CatLines
- where l.LineNumber > start
- select l).ToList<SQFLine>().First();
- this.LastLine = nextLine.LineNumber;
- }
- catch (InvalidOperationException ex)
- {
- this.LastLine = 99999;
- }
- return returnValue;
- }
- }
- /// <summary>
- /// Class for handling our indvidual object records
- /// </summary>
- class SQFobj
- {
- /// <summary>
- /// Item's Classname ex m9sd
- /// </summary>
- public string ClassName { get; set; }
- /// <summary>
- /// Type of trade item ex trade_weapons
- /// </summary>
- public string Type { get; set; }
- /// <summary>
- /// Cost to purchase item
- /// </summary>
- public int BuyPrice { get; set; }
- /// <summary>
- /// Cost to sell item
- /// </summary>
- public int SellPrice { get; set; }
- public int StartLine { get; set; }
- public int LastLine { get; set; }
- /// <summary>
- /// Method to handle getting the range of our obj
- /// </summary>
- /// <param name="ObjLines"></param>
- /// <param name="start"></param>
- /// <returns>Result of process</returns>
- public bool ParseObjectInformation(List<SQFLine> ObjLines, int start)
- {
- bool returnValue = true;
- SQFLine currentLine = (from l in ObjLines
- where l.LineNumber == start
- select l).ToList<SQFLine>().First();
- this.StartLine = currentLine.LineNumber;
- try
- {
- SQFLine nextLine = (from l in ObjLines
- where l.LineNumber > start
- select l).ToList<SQFLine>().First();
- this.LastLine = nextLine.LineNumber;
- }
- catch (InvalidOperationException ex)
- {
- this.LastLine = 99999;
- }
- // parse out the name of our obj classname
- string classNameMatchString = "\\s*(class)(\\s)(?<ClassName>[a-zA-Z0-9_]+)";
- Regex regex = new Regex(classNameMatchString);
- Match match = regex.Match(currentLine.LineText);
- if (match.Success)
- {
- this.ClassName = match.Groups["ClassName"].Value.ToString();
- Console.WriteLine("Parsing " + this.ClassName);
- }
- else
- {
- returnValue = false;
- }
- return returnValue;
- }
- /// <summary>
- /// Method to handle parsing of the properties themselves in our obj range
- /// </summary>
- /// <param name="PropLines"></param>
- public void ParseObjProps(List<SQFLine> PropLines)
- {
- // get all of the prop lines that are in scope of our obj
- List<SQFLine> linesInScope = (from l in PropLines
- where l.LineNumber >= this.StartLine
- && l.LineNumber < this.LastLine
- select l).ToList<SQFLine>();
- // regex strings for matching
- string typeMatchString = "(type)\\s*(=)\\s*\"(?<Type>[a-zA-Z0-9_]+)";
- string buyPriceMatchString = "(buy\\[\\])\\s*(=)\\s*(\\{)(?<Cost>[0-9]+)";
- string sellPriceMatchString = "(sell\\[\\])\\s*(=)\\s*(\\{)(?<Cost>[0-9]+)";
- // disassemble the props for our obj
- Regex typeRegex = new Regex(typeMatchString);
- Regex buyRegex = new Regex(buyPriceMatchString);
- Regex sellRegex = new Regex(sellPriceMatchString);
- // get our matches.
- foreach (SQFLine line in linesInScope)
- {
- Match typeMatch = Regex.Match(line.LineText, typeMatchString);
- Match buyMatch = Regex.Match(line.LineText, buyPriceMatchString);
- Match sellMatch = Regex.Match(line.LineText, sellPriceMatchString);
- if (typeMatch.Success)
- {
- this.Type = typeMatch.Groups["Type"].ToString();
- Console.WriteLine("Type: " + this.Type);
- }
- else if (buyMatch.Success)
- {
- this.BuyPrice = Convert.ToInt32(buyMatch.Groups["Cost"].ToString());
- Console.WriteLine("Cost: $" + this.BuyPrice.ToString());
- }
- else if (sellMatch.Success)
- {
- this.SellPrice = Convert.ToInt32(sellMatch.Groups["Cost"].ToString());
- Console.WriteLine("Sell: $" + this.SellPrice.ToString());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement