Advertisement
Krythic

Example

Dec 13th, 2021
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Xml;
  6. using VoidwalkerEngine.Framework.Game.Systems.GameItems;
  7. using VoidwalkerEngine.Framework.Game.Systems.GameItems.Affixes;
  8. using VoidwalkerEngine.Framework.Game.Systems.GameItems.Enchanting;
  9. using VoidwalkerEngine.Framework.Game.Systems.GameItems.Templates;
  10. using VoidwalkerEngine.Framework.Game.Systems.StatusEffects;
  11. using VoidwalkerEngine.Framework.Logic;
  12. using VoidwalkerEngine.Framework.Maths;
  13. using VoidwalkerEngine.Framework.Parsers;
  14. using VoidwalkerEngine.Framework.Utilities;
  15.  
  16. namespace VoidwalkerEngine.Framework.Collections
  17. {
  18.     public class GameDatabase
  19.     {
  20.         public List<StatusEffect> StatusEffects { get; set; }
  21.         public List<Enchantment> Enchantments { get; set; }
  22.         public Dictionary<string, GameItem> GameItems { get; set; }
  23.         public List<EpicItemTemplate> EpicItemTemplates { get; set; }
  24.         public List<DynamicMagicAffix> MagicAffixes { get; set; }
  25.  
  26.         public GameDatabase()
  27.         {
  28.             this.StatusEffects = new List<StatusEffect>();
  29.             this.Enchantments = new List<Enchantment>();
  30.             this.GameItems = new Dictionary<string, GameItem>();
  31.             this.EpicItemTemplates = new List<EpicItemTemplate>();
  32.             this.MagicAffixes = new List<DynamicMagicAffix>();
  33.         }
  34.  
  35.         public List<GameItem> FindGameItems(GameItemType type)
  36.         {
  37.             List<GameItem> results = new List<GameItem>();
  38.             foreach (GameItem item in this.GameItems.Values)
  39.             {
  40.                 if (item != null && item.ItemType == type)
  41.                 {
  42.                     results.Add(item);
  43.                 }
  44.             }
  45.             return results;
  46.         }
  47.  
  48.         public List<Enchantment> GetEnchantments(List<string> identifiers)
  49.         {
  50.             List<Enchantment> results = new List<Enchantment>();
  51.             foreach (string identifier in identifiers)
  52.             {
  53.                 Enchantment enchant = GetEnchantment(identifier);
  54.                 if (enchant != null)
  55.                 {
  56.                     results.Add(enchant);
  57.                 }
  58.             }
  59.             return results;
  60.         }
  61.  
  62.         public Enchantment GetEnchantment(string identifier)
  63.         {
  64.             foreach (Enchantment enchantment in Enchantments)
  65.             {
  66.                 if (enchantment.Identifier.Equals(identifier))
  67.                 {
  68.                     return enchantment;
  69.                 }
  70.             }
  71.             return null;
  72.         }
  73.  
  74.         public void Load(string path)
  75.         {
  76.             if (path != null && File.Exists(path))
  77.             {
  78.                 byte[] fileData = File.ReadAllBytes(path);
  79.                 /**
  80.                  * Check to see if the database is an encrypted format.
  81.                  * And if it is, move to decrypt it using the database
  82.                  * encryption password.
  83.                  */
  84.                 if (path.EndsWith(".bin"))
  85.                 {
  86.                     VoidwalkerCipher decryptionCipher = new VoidwalkerCipher("Krythic");
  87.                     decryptionCipher.DecryptBytes(fileData);
  88.                 }
  89.                 // Check database xml header to make sure it's valid
  90.                 if (VoidwalkerSerialization.IsValidXmlHeader(fileData))
  91.                 {
  92.                     MemoryStream stream = new MemoryStream(fileData);
  93.                     XmlReaderSettings settings = new XmlReaderSettings();
  94.                     settings.IgnoreComments = true;
  95.                     XmlReader reader = XmlReader.Create(stream, settings);
  96.                     while (reader.Read())
  97.                     {
  98.                         string localName = reader.LocalName;
  99.                         switch (localName)
  100.                         {
  101.                             case "GameItem":
  102.                                 ParseGameItem(reader);
  103.                                 break;
  104.  
  105.                             case "DynamicMagicAffix":
  106.                                 ParseMagicAffix(reader);
  107.                                 break;
  108.  
  109.                             default:
  110.                                 break;
  111.                         }
  112.                     }
  113.                 }
  114.                 else
  115.                 {
  116.                     throw new System.Exception("Database file is corrupted; text is not valid xml!");
  117.                 }
  118.             }
  119.         }
  120.  
  121.         /// <summary>
  122.         /// Current API Keywords:
  123.         /// Weapons, Chestwear, Footwear, Handwear, Headwear, Rings, Amulets
  124.         /// </summary>
  125.         /// <param name="name"></param>
  126.         /// <param name="value"></param>
  127.         /// <param name="constraints"></param>
  128.         public void ParseGameItemConstraint(string name, string value, List<GameItemType> constraints)
  129.         {
  130.             switch (name)
  131.             {
  132.                 case "Weapons":
  133.                     if (Boolean.Parse(value))
  134.                     {
  135.                         constraints.AddRange(new GameItemType[] {
  136.                             GameItemType.OneHandedSword, GameItemType.OneHandedMace, GameItemType.OneHandedAxe,
  137.                             GameItemType.TwoHandedSword, GameItemType.TwoHandedMace, GameItemType.TwoHandedAxe,
  138.                             GameItemType.Bow, GameItemType.Dagger, GameItemType.Fist, GameItemType.Staff, GameItemType.Wand,
  139.                         });
  140.                     }
  141.                     break;
  142.  
  143.                 case "Chestwear":
  144.                     if (Boolean.Parse(value))
  145.                     {
  146.                         constraints.AddRange(new GameItemType[] {
  147.                             GameItemType.LightChestwear, GameItemType.MediumChestwear, GameItemType.HeavyChestwear,
  148.                         });
  149.                     }
  150.                     break;
  151.  
  152.                 case "Footwear":
  153.                     if (Boolean.Parse(value))
  154.                     {
  155.                         constraints.AddRange(new GameItemType[] {
  156.                             GameItemType.LightFootwear, GameItemType.MediumFootwear, GameItemType.HeavyFootwear,
  157.                         });
  158.                     }
  159.                     break;
  160.  
  161.                 case "Handwear":
  162.                     if (Boolean.Parse(value))
  163.                     {
  164.                         constraints.AddRange(new GameItemType[] {
  165.                             GameItemType.LightHandwear, GameItemType.MediumHandwear, GameItemType.HeavyHandwear,
  166.                         });
  167.                     }
  168.                     break;
  169.  
  170.                 case "Headwear":
  171.                     if (Boolean.Parse(value))
  172.                     {
  173.                         constraints.AddRange(new GameItemType[] {
  174.                             GameItemType.LightHeadwear, GameItemType.MediumHeadwear, GameItemType.HeavyHeadwear,
  175.                         });
  176.                     }
  177.                     break;
  178.  
  179.                 case "Rings":
  180.                     if (Boolean.Parse(value))
  181.                     {
  182.                         constraints.Add(GameItemType.Ring);
  183.                     }
  184.                     break;
  185.  
  186.                 case "Amulets":
  187.                     if (Boolean.Parse(value))
  188.                     {
  189.                         constraints.Add(GameItemType.Amulet);
  190.                     }
  191.                     break;
  192.  
  193.                 case "HeldItems":
  194.                     if (Boolean.Parse(value))
  195.                     {
  196.                         constraints.AddRange(new GameItemType[] {
  197.                             GameItemType.Shield, GameItemType.Phylactery, GameItemType.Focus,
  198.                             GameItemType.Tome, GameItemType.Quiver, GameItemType.Instrument
  199.                         });
  200.                     }
  201.                     break;
  202.  
  203.                 default:
  204.                     Console.WriteLine("Unknown GameItemType Constraint keyword: '" + name + "'");
  205.                     break;
  206.             }
  207.         }
  208.  
  209.         public void ParseMagicAffix(XmlReader reader)
  210.         {
  211.             DynamicMagicAffix affix = new DynamicMagicAffix();
  212.             if (reader.HasAttributes)
  213.             {
  214.                 int attributeCount = reader.AttributeCount;
  215.                 for (int i = 0; i < attributeCount; i++)
  216.                 {
  217.                     reader.MoveToNextAttribute();
  218.                     string attributeName = reader.Name;
  219.                     switch (attributeName)
  220.                     {
  221.                         case "Name":
  222.                             affix.Name = reader.Value;
  223.                             break;
  224.  
  225.                         case "AffixType":
  226.                             affix.AffixType = VoidwalkerParser.ParseAffixType(reader.Value);
  227.                             break;
  228.  
  229.                         case "Property":
  230.                             affix.Property = VoidwalkerParser.ParseGamePropertyType(reader.Value);
  231.                             break;
  232.  
  233.                         case "Level":
  234.                             affix.Level = Int32.Parse(reader.Value);
  235.                             break;
  236.  
  237.                         case "ValueRange":
  238.                             affix.ValueRange = Range.Parse(reader.Value, '-', Range.Empty);
  239.                             break;
  240.  
  241.                         case "RangeMin":
  242.                             affix.RangeMin = Range.Parse(reader.Value, '-', Range.Empty);
  243.                             break;
  244.  
  245.                         case "RangeMax":
  246.                             affix.RangeMax = Range.Parse(reader.Value, '-', Range.Empty);
  247.                             break;
  248.  
  249.                         case "Constraints":
  250.                             affix.Constraints = VoidwalkerParser.ParseGameItemConstraints(reader.Value);
  251.                             break;
  252.  
  253.                         case "Weight":
  254.                             affix.Weight = Int32.Parse(reader.Value);
  255.                             break;
  256.  
  257.                         default:
  258.                             break;
  259.                     }
  260.                 }
  261.                 if (affix != null)
  262.                 {
  263.                     this.MagicAffixes.Add(affix);
  264.                 }
  265.             }
  266.             while (reader.Read() && !reader.LocalName.Equals("DynamicMagicAffix"))
  267.             {
  268.                 string localName = reader.LocalName;
  269.                 switch (localName)
  270.                 {
  271.                     case "Constraints":
  272.                         if (affix.Constraints == null)
  273.                         {
  274.                             affix.Constraints = new List<GameItemType>();
  275.                         }
  276.                         if (reader.HasAttributes)
  277.                         {
  278.                             int attributeCount = reader.AttributeCount;
  279.                             for (int i = 0; i < attributeCount; i++)
  280.                             {
  281.                                 reader.MoveToNextAttribute();
  282.                                 string attributeName = reader.Name;
  283.                                 ParseGameItemConstraint(attributeName, reader.Value, affix.Constraints);
  284.                             }
  285.                         }
  286.                         break;
  287.  
  288.                     default:
  289.                         break;
  290.                 }
  291.             }
  292.         }
  293.  
  294.         public void ParseGameItem(XmlReader reader)
  295.         {
  296.             GameItem gameItem = new GameItem();
  297.             if (reader.HasAttributes)
  298.             {
  299.                 int attributeCount = reader.AttributeCount;
  300.                 for (int i = 0; i < attributeCount; i++)
  301.                 {
  302.                     reader.MoveToNextAttribute();
  303.                     string attributeName = reader.Name;
  304.                     switch (attributeName)
  305.                     {
  306.                         case "ID":
  307.                             gameItem.Identifier = reader.Value;
  308.                             break;
  309.  
  310.                         case "ItemType":
  311.                             gameItem.ItemType = VoidwalkerParser.ParseGameItemType(reader.Value);
  312.                             break;
  313.  
  314.                         case "Rarity":
  315.                             gameItem.Rarity = VoidwalkerParser.ParseItemRarity(reader.Value);
  316.                             break;
  317.  
  318.                         case "Header":
  319.                             gameItem.Header = reader.Value;
  320.                             break;
  321.  
  322.                         case "BaseName":
  323.                             gameItem.BaseName = reader.Value;
  324.                             break;
  325.  
  326.                         case "BaseDamage":
  327.                             gameItem.BaseDamage = Int32.Parse(reader.Value);
  328.                             break;
  329.  
  330.                         case "BaseDurability":
  331.                             gameItem.BaseDurability = Int32.Parse(reader.Value);
  332.                             break;
  333.  
  334.                         case "BaseSellPrice":
  335.                             gameItem.BaseSellPrice = Int32.Parse(reader.Value);
  336.                             break;
  337.  
  338.                         default:
  339.                             break;
  340.                     }
  341.                 }
  342.             }
  343.             /**
  344.              * Parse for Content
  345.              */
  346.             while (reader.Read() && !reader.LocalName.Equals("GameItem"))
  347.             {
  348.                 string localName = reader.LocalName;
  349.                 switch (localName)
  350.                 {
  351.                     case "GameItemProperty":
  352.                         if (gameItem.Properties == null)
  353.                         {
  354.                             gameItem.Properties = new List<GameItemProperty>();
  355.                         }
  356.                         if (reader.HasAttributes)
  357.                         {
  358.                             GameItemProperty property = new GameItemProperty();
  359.                             int attributeCount = reader.AttributeCount;
  360.                             for (int i = 0; i < attributeCount; i++)
  361.                             {
  362.                                 reader.MoveToNextAttribute();
  363.                                 string attributeName = reader.Name;
  364.                                 switch (attributeName)
  365.                                 {
  366.                                     case "Property":
  367.                                         property.Property = VoidwalkerParser.ParseGamePropertyType(reader.Value);
  368.                                         break;
  369.  
  370.                                     case "Value":
  371.                                         property.Value = Int32.Parse(reader.Value, NumberStyles.AllowLeadingSign);
  372.                                         break;
  373.  
  374.                                     case "Range":
  375.                                         property.Range = Range.Parse(reader.Value, '-', Range.Empty);
  376.                                         break;
  377.  
  378.                                     default:
  379.                                         break;
  380.                                 }
  381.                             }
  382.                             gameItem.Properties.Add(property);
  383.                         }
  384.                         break;
  385.  
  386.                     default:
  387.                         break;
  388.                 }
  389.             }
  390.             this.GameItems.Add(gameItem.Identifier, gameItem);
  391.         }
  392.     }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement