Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace gms_pricer.classes
  10. {
  11.  
  12.     //class Category_674 {
  13.     //class M9SD {type = "trade_weapons";buy[] ={17500,"Coins"};sell[] ={7500,"Coins"};};
  14.     //class glock17_EP1 {type = "trade_weapons";buy[] ={500,"Coins"};sell[] ={200,"Coins"};};
  15.     //class Colt1911 {type = "trade_weapons";buy[] ={1000,"Coins"};sell[] ={500,"Coins"};};
  16.     //class M9 {type = "trade_weapons";buy[] ={900,"Coins"};sell[] ={5000,"Coins"};};
  17.     //class MakarovSD {type = "trade_weapons";buy[] ={5000,"Coins"};sell[] ={1000,"Coins"};};
  18.     //class revolver_gold_EP1 {type = "trade_weapons";buy[] ={20000,"Coins"};sell[] ={7500,"Coins"};};
  19.     //class Makarov {type = "trade_weapons";buy[] ={200,"Coins"};sell[] ={100,"Coins"};};
  20.     //class revolver_EP1 {type = "trade_weapons";buy[] ={1850,"Coins"};sell[] ={500,"Coins"};};
  21.     //};
  22.  
  23.  
  24.     /// <summary>
  25.     /// Supporting class to handle line structures for LINQ queries
  26.     /// </summary>
  27.     class SQFLine
  28.     {
  29.         public int LineNumber;
  30.         public string LineText;
  31.     }
  32.  
  33.  
  34.     class SQFFile
  35.     {
  36.         /// <summary>
  37.         /// Filename + path to file
  38.         /// </summary>
  39.         public string FileName { get; set; }
  40.  
  41.         /// <summary>
  42.         /// List of SQFCategories inside the file
  43.         /// </summary>
  44.         public List<SQFCategory> Categories { get; set; }
  45.  
  46.  
  47.         /// <summary>
  48.         /// Method to get a list of categories in the file
  49.         /// </summary>
  50.         /// <returns></returns>
  51.         public List<SQFCategory> GetCategories()
  52.         {
  53.             List<SQFCategory> returnValue = new List<SQFCategory>();
  54.             StreamReader sr = new StreamReader(@"C:\Users\admin\Documents\GitHub\BigTDayZEpoch\MPMissions\DayZ_Epoch_11_vp.Chernarus\Scripts\Trader_Items\Category\test.hpp");
  55.  
  56.             List<SQFLine> linesofFile = new List<SQFLine>();
  57.  
  58.             string line = string.Empty;
  59.             int a = 1;
  60.             while ((line = sr.ReadLine()) != null)
  61.             {
  62.                 SQFLine sqLine = new SQFLine();
  63.                 sqLine.LineNumber = a;
  64.                 sqLine.LineText = line;
  65.  
  66.                 // add to our master list
  67.                 linesofFile.Add(sqLine);
  68.                 a++;
  69.             }
  70.  
  71.             // parse out lines containing cats
  72.             List<SQFLine> catLines = (from l in linesofFile
  73.                                       where l.LineText.Contains("Category")
  74.                                      select l).ToList<SQFLine>();
  75.  
  76.             // parse out our obj classes
  77.             List<SQFLine> objLines = (from l in linesofFile
  78.                                       where l.LineText.Contains("class") && (!l.LineText.Contains("Category"))
  79.                                       select l).ToList<SQFLine>();
  80.  
  81.             // parse out lines containing props
  82.             List<SQFLine> propLines = (from l in linesofFile
  83.                                        where l.LineText.Contains("type") || l.LineText.Contains("buy") || l.LineText.Contains("sell")
  84.                                        select l).ToList<SQFLine>();
  85.  
  86.            
  87.  
  88.             // process our categories, objects, and props
  89.             List<SQFCategory> categories = new List<SQFCategory>();
  90.             foreach (SQFLine catLine in catLines)
  91.             {
  92.                 SQFCategory cat = new SQFCategory();
  93.                 // process cat
  94.                 if (cat.ParseCategoryInformation(catLines, catLine.LineNumber))
  95.                 {
  96.                     foreach (SQFLine objLine in objLines)
  97.                     {
  98.                         SQFobj obj = new SQFobj();
  99.                         if (obj.ParseObjectInformation(objLines, objLine.LineNumber))
  100.                         {
  101.                             // process props for obj
  102.                             obj.ParseObjProps(propLines);
  103.                         }
  104.                     }
  105.                                        
  106.                 }
  107.  
  108.                 returnValue.Add(cat);
  109.             }
  110.  
  111.             return returnValue;
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Main entry point for our object
  116.         /// </summary>
  117.         /// <param name="filename">This can be our current objects file name if not specified</param>
  118.         /// <returns>True False if process was successful</returns>
  119.         public bool parseFile(string filename)
  120.         {
  121.             bool returnValue = true; // assume true
  122.            
  123.            
  124.            
  125.  
  126.             return returnValue;
  127.  
  128.         }
  129.  
  130.     }
  131.  
  132.     /// <summary>
  133.     /// class for handling our individual categories in a hpp file
  134.     /// </summary>
  135.     class SQFCategory
  136.     {
  137.         /// <summary>
  138.         /// Class name of category ex Category_674
  139.         /// </summary>
  140.         public string CategoryName { get; set; }
  141.  
  142.         /// <summary>
  143.         /// List of our current category's items
  144.         /// </summary>
  145.         public List<SQFobj> SQFItems { get; set; }
  146.  
  147.         /// <summary>
  148.         /// The first line where we see the category
  149.         /// </summary>
  150.         public int StartLine { get; set; }
  151.  
  152.         /// <summary>
  153.         /// The Next reference to a category
  154.         /// </summary>
  155.         public int LastLine { get; set; }
  156.  
  157.         /// <summary>
  158.         /// Method to get list of our SQF objects in specified category
  159.         /// </summary>
  160.         /// <param name="CategoryName"></param>
  161.         /// <returns></returns>
  162.         public List<SQFobj> GetSQFObjects(string CategoryName)
  163.         {
  164.             List<SQFobj> returnValue = new List<SQFobj>();
  165.  
  166.             return returnValue;
  167.         }
  168.  
  169.         /// <summary>
  170.         /// Main entry point we parse all the info for our category here
  171.         /// </summary>
  172.         /// <param name="CatLines"></param>
  173.         /// <param name="start"></param>
  174.         /// <returns></returns>
  175.         public bool ParseCategoryInformation(List<SQFLine> CatLines, int start)
  176.         {
  177.             bool returnValue = true;
  178.  
  179.             SQFLine currentLine = (from l in CatLines
  180.                                    where l.LineNumber == start
  181.                                    select l).ToList<SQFLine>().First();
  182.             this.StartLine = currentLine.LineNumber;
  183.  
  184.             // failover for single class files
  185.             try
  186.             {
  187.                 SQFLine nextLine = (from l in CatLines
  188.                                     where l.LineNumber > start
  189.                                     select l).ToList<SQFLine>().First();
  190.                 this.LastLine = nextLine.LineNumber;
  191.             }
  192.             catch (InvalidOperationException ex)
  193.             {
  194.                 this.LastLine = 99999;
  195.             }
  196.             return returnValue;
  197.         }
  198.     }
  199.    
  200.  
  201.     /// <summary>
  202.     /// Class for handling our indvidual object records
  203.     /// </summary>
  204.     class SQFobj
  205.     {
  206.         /// <summary>
  207.         /// Item's Classname ex m9sd
  208.         /// </summary>
  209.         public string ClassName { get; set; }
  210.  
  211.         /// <summary>
  212.         /// Type of trade item ex trade_weapons
  213.         /// </summary>
  214.         public string Type { get; set; }
  215.  
  216.         /// <summary>
  217.         /// Cost to purchase item
  218.         /// </summary>
  219.         public int BuyPrice { get; set; }
  220.  
  221.         /// <summary>
  222.         /// Cost to sell item
  223.         /// </summary>
  224.         public int SellPrice { get; set; }
  225.  
  226.         public int StartLine { get; set; }
  227.         public int LastLine { get; set; }
  228.  
  229.         /// <summary>
  230.         /// Method to handle getting the range of our obj
  231.         /// </summary>
  232.         /// <param name="ObjLines"></param>
  233.         /// <param name="start"></param>
  234.         /// <returns>Result of process</returns>
  235.         public bool ParseObjectInformation(List<SQFLine> ObjLines, int start)
  236.         {
  237.             bool returnValue = true;
  238.  
  239.             SQFLine currentLine = (from l in ObjLines
  240.                                    where l.LineNumber == start
  241.                                    select l).ToList<SQFLine>().First();
  242.             this.StartLine = currentLine.LineNumber;
  243.             try
  244.             {
  245.                 SQFLine nextLine = (from l in ObjLines
  246.                                     where l.LineNumber > start
  247.                                     select l).ToList<SQFLine>().First();
  248.                 this.LastLine = nextLine.LineNumber;
  249.             }
  250.             catch (InvalidOperationException ex)
  251.             {
  252.                 this.LastLine = 99999;
  253.             }
  254.  
  255.             // parse out the name of our obj classname
  256.             string classNameMatchString = "\\s*(class)(\\s)(?<ClassName>[a-zA-Z0-9_]+)";
  257.             Regex regex = new Regex(classNameMatchString);
  258.             Match match = regex.Match(currentLine.LineText);
  259.  
  260.             if (match.Success)
  261.             {
  262.                 this.ClassName = match.Groups["ClassName"].Value.ToString();
  263.                 Console.WriteLine("Parsing " + this.ClassName);
  264.             }
  265.             else
  266.             {
  267.                 returnValue = false;
  268.             }
  269.            
  270.             return returnValue;
  271.         }
  272.  
  273.         /// <summary>
  274.         /// Method to handle parsing of the properties themselves in our obj range
  275.         /// </summary>
  276.         /// <param name="PropLines"></param>
  277.         public void ParseObjProps(List<SQFLine> PropLines)
  278.         {
  279.  
  280.             // get all of the prop lines that are in scope of our obj
  281.             List<SQFLine> linesInScope = (from l in PropLines
  282.                                          where l.LineNumber >= this.StartLine
  283.                                          && l.LineNumber < this.LastLine
  284.                                          select l).ToList<SQFLine>();
  285.  
  286.             // regex strings for matching
  287.            
  288.             string typeMatchString = "(type)\\s*(=)\\s*\"(?<Type>[a-zA-Z0-9_]+)";
  289.             string buyPriceMatchString = "(buy\\[\\])\\s*(=)\\s*(\\{)(?<Cost>[0-9]+)";
  290.             string sellPriceMatchString = "(sell\\[\\])\\s*(=)\\s*(\\{)(?<Cost>[0-9]+)";
  291.  
  292.             // disassemble the props for our obj
  293.             Regex typeRegex = new Regex(typeMatchString);
  294.             Regex buyRegex = new Regex(buyPriceMatchString);
  295.             Regex sellRegex = new Regex(sellPriceMatchString);
  296.            
  297.  
  298.             // get our matches.
  299.             foreach (SQFLine line in linesInScope)
  300.             {
  301.                 Match typeMatch = Regex.Match(line.LineText, typeMatchString);
  302.                 Match buyMatch = Regex.Match(line.LineText, buyPriceMatchString);
  303.                 Match sellMatch = Regex.Match(line.LineText, sellPriceMatchString);
  304.  
  305.                 if (typeMatch.Success)
  306.                 {
  307.                     this.Type = typeMatch.Groups["Type"].ToString();
  308.                     Console.WriteLine("Type: " + this.Type);
  309.                 }
  310.                 else if (buyMatch.Success)
  311.                 {
  312.                     this.BuyPrice = Convert.ToInt32(buyMatch.Groups["Cost"].ToString());
  313.                     Console.WriteLine("Cost: $" + this.BuyPrice.ToString());
  314.                 }
  315.                 else if (sellMatch.Success)
  316.                 {
  317.                     this.SellPrice = Convert.ToInt32(sellMatch.Groups["Cost"].ToString());
  318.                     Console.WriteLine("Sell: $" + this.SellPrice.ToString());
  319.                 }
  320.             }
  321.         }
  322.     }    
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement