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
...
 
@@ -22,130 +22,146 @@
 

	
 
using Gtk;
 
using System;
 
using System.IO;
 
using System.IO.Compression;
 
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*");
 
        static Regex recipeIngredientRegex = new Regex(@"(?<ingredient>(\w+\s)?\w+)\s*\|\s*(?<quantity>\d+)\s*");
 

	
 
        ReactionSet reactions = new ReactionSet();
 
        // ingredient -> [ingredient, reaction]
 
		//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");
 
            this.reagentFile = System.IO.Path.Combine(directory, "ingredients.txt");
 
            this.settingsFile = System.IO.Path.Combine(directory, "settings");
 
            this.recipes = new SortedDictionary<string, PaintRecipe>();
 
            this.ribbonRecipes = new SortedDictionary<string, PaintRecipe>();
 
            foreach (PaintColor color in Palette.Colors)
 
            {
 
                this.recipes.Add(color.Name, new PaintRecipe());
 
            }
 
            foreach (PaintColor color in Palette.Colors)
 
            {
 
                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;
 
                    }
 
                }
 
                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;
 
                    }
 
                }
 
                return count;
 
            }
 
        }
 
		
...
 
@@ -170,69 +186,73 @@ namespace DesertPaintLab
 
			{
 
                string destFile = System.IO.Path.Combine(directory, file.Name);
 
				System.IO.File.Copy(file.FullName, destFile, true);
 
                if (!File.Exists(destFile))
 
                {
 
                    LastError = "Failed to copy template file " + file.Name + ".";
 
                    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;	
 
							}							
 
						}
 
					}
 
				}
 
			}
 
		}
 
		
 
		public bool SaveToPP(string ppFile)
 
		{
 
			Reaction reaction1, reaction2;
 
			using (StreamWriter writer = new StreamWriter(ppFile))
...
 
@@ -355,28 +375,36 @@ namespace DesertPaintLab
 
                return false;
 
            }
 
			ReagentManager.InitializeReactions(ref reactions);
 
            if (!File.Exists(reactFile))
 
            {
 
                LastError = "Failed to find profile reactions file.";
 
                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()
 
		{
 
            settings.Save(settingsFile);
 
			Reaction reaction;
 
			using (StreamWriter writer = new StreamWriter(reactFile, false))
 
			{
 
                foreach (string reagentName1 in ReagentManager.Names)
0 comments (0 inline, 0 general)