Changeset - 3154dda31272
[Not reviewed]
default
0 1 0
Jason Maltzen - 5 years ago 2019-11-02 02:23:43
jason@hiddenachievement.com
Slightly safer parsing of reaction files
1 file changed with 70 insertions and 42 deletions:
0 comments (0 inline, 0 general)
PlayerProfile.cs
Show inline comments
...
 
@@ -28,13 +28,16 @@ using System.Collections.Generic;
 
using System.Text.RegularExpressions;
 

	
 
namespace DesertPaintLab
 
{
 
	public class PlayerProfile
 
	{
 
        public string Name { get; private set; }
 
        public string Name
 
        {
 
            get; private set;
 
        }
 
		string directory;
 
		string reactFile;
 
        string reagentFile;
 
        string settingsFile;
 
		
 
        static Regex recipeHeaderRegex = new Regex(@"^--- Recipe: (?<colorname>(\w*\s)*\w+)\s*");
...
 
@@ -45,28 +48,34 @@ namespace DesertPaintLab
 
		//SortedDictionary<string, SortedDictionary<string, Reaction>> reactions =
 
		//	new SortedDictionary<string, SortedDictionary<string, Reaction>>();
 
        SortedDictionary<string, PaintRecipe> recipes;
 
        SortedDictionary<string, PaintRecipe> ribbonRecipes;
 

	
 
        Settings settings = new Settings();
 
        public Settings ProfileSettings { 
 
            get { 
 
        public Settings ProfileSettings
 
        {
 
            get
 
            {
 
                return settings;
 
            }
 
        }
 

	
 
        static PlayerProfile current = null;
 

	
 
        static PlayerProfile Current
 
        {
 
            get {
 
            get
 
            {
 
                return current;
 
            }
 
        }
 

	
 
        public string LastError { get; private set; }
 
        public string LastError
 
        {
 
            get; private set;
 
        }
 
		
 
		public PlayerProfile(string name, string directory)
 
		{
 
			this.Name = name;
 
			this.directory = directory;
 
			this.reactFile = System.IO.Path.Combine(directory, "dp_reactions.txt");
...
 
@@ -83,48 +92,54 @@ namespace DesertPaintLab
 
                this.ribbonRecipes.Add(color.Name, new PaintRecipe());
 
            }
 
		}
 

	
 
        public string Directory
 
        {
 
            get {
 
            get
 
            {
 
                return this.directory;
 
            }
 
        }
 

	
 
        public ReactionSet Reactions
 
        {
 
            get {
 
            get
 
            {
 
                return this.reactions;
 
            }
 
        }
 

	
 
        public string ReagentFile
 
        {
 
            get {
 
            get
 
            {
 
                return this.reagentFile;
 
            }
 
        }
 

	
 
        public SortedDictionary<string, PaintRecipe> Recipes
 
        {
 
            get {
 
            get
 
            {
 
                return this.recipes;
 
            }
 
        }
 

	
 
        public SortedDictionary<string, PaintRecipe> RibbonRecipes
 
        {
 
            get {
 
            get
 
            {
 
                return this.ribbonRecipes;
 
            }
 
        }
 

	
 
        public int RecipeCount
 
        {
 
            get {
 
            get
 
            {
 
                int count = 0;
 
                foreach (PaintRecipe recipe in this.recipes.Values)
 
                {
 
                    if (recipe.IsValidForConcentration(PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION))
 
                    {
 
                        ++count;
...
 
@@ -133,13 +148,14 @@ namespace DesertPaintLab
 
                return count;
 
            }
 
        }
 

	
 
        public int RibbonCount
 
        {
 
            get {
 
            get
 
            {
 
                int count = 0;
 
                foreach (PaintRecipe recipe in this.ribbonRecipes.Values)
 
                {
 
                    if (recipe.IsValidForConcentration(PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION))
 
                    {
 
                        ++count;
...
 
@@ -176,57 +192,61 @@ namespace DesertPaintLab
 
                    return false;
 
                }					
 
			}
 
            return true;
 
		}
 
		
 
        private void WriteReaction(StreamWriter writer, string reagent1, string reagent2, string r, string g, string b)
 
        {
 
            writer.Write(reagent1);
 
            writer.Write(" ");
 
            writer.Write(reagent2);
 
            writer.Write(" ");
 
            writer.Write(r);
 
            writer.Write(" ");
 
            writer.Write(g);
 
            writer.Write(" ");
 
            writer.WriteLine(b);
 
        }
 

	
 
		public void ConvertFromPP(string ppFile, string dpFile)
 
		{
 
			string line;
 
			using (StreamReader reader = new StreamReader(ppFile))
 
			{
 
				using (StreamWriter writer = new StreamWriter(dpFile))
 
				{
 
					while ((line = reader.ReadLine()) != null) 
 
                	{
 
						string[] tokens = line.Split(null);
 
						if ((tokens.Length > 0) && (tokens[0] != "//"))
 
                        string[] tokens = line.Split('|');
 
                        //if ((tokens.Length > 0) && (tokens [0] != "//"))
 
                        if ((tokens.Length != 5) && (tokens[0].Trim() != "//"))
 
						{							
 
                            string reagent1 = tokens[0].Trim();
 
                            string reagent2 = tokens[1].Trim();
 
                            string colorCode = tokens[2].Trim();
 
                            string change1 = tokens[3].Trim();
 
                            string change2 = tokens[4].Trim();
 
							// Write reaction.
 
							writer.Write(tokens[0] + " " + tokens[2] + " ");
 
							switch (tokens[4])
 
                            switch (colorCode)
 
							{
 
							case "W":
 
								writer.WriteLine(tokens[6] + " " + tokens[6] + " " + tokens[6]);
 
                                WriteReaction(writer, reagent1, reagent2, change1, change1, change1);
 
                                WriteReaction(writer, reagent2, reagent1, change2, change2, change2);
 
								break;
 
							case "R":
 
								writer.WriteLine(tokens[6] + " 0 0");
 
                                WriteReaction(writer, reagent1, reagent2, change1, "0", "0");
 
                                WriteReaction(writer, reagent2, reagent1, change2, "0", "0");
 
								break;
 
							case "G":
 
								writer.WriteLine("0 " + tokens[6] + " 0");
 
                                WriteReaction(writer, reagent1, reagent2, "0", change1, "0");
 
                                WriteReaction(writer, reagent2, reagent1, "0", change2, "0");
 
								break;
 
							case "B":
 
								writer.WriteLine("0 0 " + tokens[6]);
 
								break;	
 
							}
 
							
 
							// Write reverse reaction.
 
							writer.Write(tokens[2] + " " + tokens[0] + " ");
 
							switch (tokens[4])
 
							{
 
							case "W":
 
								writer.WriteLine(tokens[8] + " " + tokens[8] + " " + tokens[8]);
 
								break;
 
							case "R":
 
								writer.WriteLine(tokens[8] + " 0 0");
 
								break;
 
							case "G":
 
								writer.WriteLine("0 " + tokens[8] + " 0");
 
								break;
 
							case "B":
 
								writer.WriteLine("0 0 " + tokens[8]);
 
                                WriteReaction(writer, reagent1, reagent2, "0", "0", change1);
 
                                WriteReaction(writer, reagent2, reagent1, "0", "0", change2);
 
								break;	
 
							}							
 
						}
 
					}
 
				}
 
			}
...
 
@@ -361,16 +381,24 @@ namespace DesertPaintLab
 
                return false;
 
            }
 
			using (StreamReader reader = new StreamReader(reactFile))
 
			{
 
				while ((line = reader.ReadLine()) != null) 
 
               	{
 
					string[] tokens = line.Split(null);
 
                    Reagent reagent1 = ReagentManager.GetReagent(tokens[0]);
 
                    Reagent reagent2 = ReagentManager.GetReagent(tokens[1]);
 
					reactions.Set(reagent1, reagent2, new Reaction(int.Parse(tokens[2]), int.Parse(tokens[3]), int.Parse(tokens[4])));
 
                    string[] tokens = line.Split(' ');
 
                    if (tokens.Length == 5)
 
                    {
 
                        Reagent reagent1 = ReagentManager.GetReagent(tokens[0].Trim());
 
                        Reagent reagent2 = ReagentManager.GetReagent(tokens[1].Trim());
 
                        Reaction reaction = new Reaction(
 
                            int.Parse(tokens[2].Trim()),
 
                            int.Parse(tokens[3].Trim()),
 
                            int.Parse(tokens[4].Trim())
 
                            );
 
                        reactions.Set(reagent1, reagent2, reaction);
 
                    }
 
				}
 
			}
 
            return true;
 
		}
 
		
 
		public void Save()
0 comments (0 inline, 0 general)