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
...
 
@@ -31,7 +31,10 @@ namespace DesertPaintLab
 
{
 
	public class PlayerProfile
 
	{
 
        public string Name { get; private set; }
 
        public string Name
 
        {
 
            get; private set;
 
        }
 
		string directory;
 
		string reactFile;
 
        string reagentFile;
...
 
@@ -48,8 +51,10 @@ namespace DesertPaintLab
 
        SortedDictionary<string, PaintRecipe> ribbonRecipes;
 

	
 
        Settings settings = new Settings();
 
        public Settings ProfileSettings { 
 
            get { 
 
        public Settings ProfileSettings
 
        {
 
            get
 
            {
 
                return settings;
 
            }
 
        }
...
 
@@ -58,12 +63,16 @@ namespace DesertPaintLab
 

	
 
        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)
 
		{
...
 
@@ -86,42 +95,48 @@ namespace DesertPaintLab
 

	
 
        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)
 
                {
...
 
@@ -136,7 +151,8 @@ namespace DesertPaintLab
 

	
 
        public int RibbonCount
 
        {
 
            get {
 
            get
 
            {
 
                int count = 0;
 
                foreach (PaintRecipe recipe in this.ribbonRecipes.Values)
 
                {
...
 
@@ -179,6 +195,19 @@ namespace DesertPaintLab
 
            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;
...
 
@@ -188,42 +217,33 @@ namespace DesertPaintLab
 
				{
 
					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;	
 
							}							
 
						}
...
 
@@ -364,10 +384,18 @@ namespace DesertPaintLab
 
			{
 
				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;
0 comments (0 inline, 0 general)