public static TintColorInformation ParseString(string input) { TintColorInformation tintColorInformation = new TintColorInformation(); string[] tintColorParts = input.Split(' '); if (bool.TryParse(tintColorParts[0], out bool value)) { tintColorInformation.HasTint = value; } else { Globals.ConsoleError($"Tried to parse {tintColorParts[0]} into a boolean for tint color info while parsing: {input}"); return(null); } if (tintColorInformation.HasTint) { string[] rgbColors = tintColorParts.Skip(1).ToArray(); if (rgbColors.Length % 3 != 0) { Globals.ConsoleError($"Invalid number of RGB colors - should be divisible by 3: {input}"); return(null); } for (int i = 0; i < rgbColors.Length; i += 3) { if (!int.TryParse(rgbColors[i], out int redValue)) { Globals.ConsoleError($"Tried to parse {rgbColors[i]} into a red value while parsing: {input}"); return(null); } if (!int.TryParse(rgbColors[i + 1], out int greenValue)) { Globals.ConsoleError($"Tried to parse {rgbColors[i + 1]} into a red value while parsing: {input}"); return(null); } if (!int.TryParse(rgbColors[i + 2], out int blueValue)) { Globals.ConsoleError($"Tried to parse {rgbColors[i + 2]} into a blue value while parsing: {input}"); return(null); } tintColorInformation.RGBInfo.Add(new RGBInformation(redValue, greenValue, blueValue)); } } return(tintColorInformation); }
/// <summary> /// Parses a string from the Crops data file into a CropGrowthInformation object /// </summary> /// <param name="input"></param> /// <returns></returns> public static CropGrowthInformation ParseString(string input) { CropGrowthInformation cropGrowthInfo = new CropGrowthInformation(); int result; string[] fields = input.Split('/'); if (fields.Length != 9) { Globals.ConsoleError($"Invalid string passed when parsing crop info: {input}"); return(null); } // Growth stages string[] growthStages = fields[(int)CropGrowthFields.GrowthStages].Split(' '); foreach (string growthStage in growthStages) { if (int.TryParse(growthStage, out result)) { cropGrowthInfo.GrowthStages.Add(result); } else { Globals.ConsoleError($"Tried to parse {growthStage} into a growth stage when parsing: {input}"); return(null); } } // Seasons string[] seasonStrings = fields[(int)CropGrowthFields.Seasons].Split(' '); foreach (string seasonString in seasonStrings) { switch (seasonString.ToLower()) { case "spring": cropGrowthInfo.GrowingSeasons.Add(Seasons.Spring); break; case "summer": cropGrowthInfo.GrowingSeasons.Add(Seasons.Summer); break; case "fall": cropGrowthInfo.GrowingSeasons.Add(Seasons.Fall); break; case "winter": cropGrowthInfo.GrowingSeasons.Add(Seasons.Winter); break; default: Globals.ConsoleError($"Tries to parse {seasonString} into a season when parsing: {input}"); return(null); } } // Graphic id string graphicId = fields[(int)CropGrowthFields.GraphicId]; if (int.TryParse(graphicId, out result)) { cropGrowthInfo.GraphicId = result; } else { Globals.ConsoleError($"Tried to parse {result} into a graphic id when parsing: {input}"); return(null); } // Crop Id string cropId = fields[(int)CropGrowthFields.CropId]; if (int.TryParse(cropId, out result)) { cropGrowthInfo.CropId = result; } else { Globals.ConsoleError($"Tried to parse {result} into a crop id when parsing: {input}"); return(null); } // Amount per harvest string daysToRegrow = fields[(int)CropGrowthFields.DaysToRegrow]; if (int.TryParse(daysToRegrow, out result)) { cropGrowthInfo.DaysToRegrow = result; } else { Globals.ConsoleError($"Tried to parse {result} into the amount per harvest when parsing: {input}"); return(null); } // Can scythe string canScythe = fields[(int)CropGrowthFields.CanScythe]; if (int.TryParse(canScythe, out result)) { cropGrowthInfo.CanScythe = result == 1; } else { Globals.ConsoleError($"Tried to parse {result} into the CanScythe flag id when parsing: {input}"); return(null); } // Extra crop info string extraCropInfoString = fields[(int)CropGrowthFields.ExtraCropInfo]; cropGrowthInfo.ExtraCropInfo = ExtraCropInformation.ParseString(extraCropInfoString); if (cropGrowthInfo.ExtraCropInfo == null) { return(null); } // Is trellis crop string isTrellisCropString = fields[(int)CropGrowthFields.IsTrellisCrop]; if (bool.TryParse(isTrellisCropString, out bool boolResult)) { cropGrowthInfo.IsTrellisCrop = boolResult; } else { Globals.ConsoleError($"Tried to parse {isTrellisCropString} into the isTrellisCrop boolean when parsing: {input}"); return(null); } // Tint color info string tintColorInfoString = fields[(int)CropGrowthFields.TintColorInfo]; cropGrowthInfo.TintColorInfo = TintColorInformation.ParseString(tintColorInfoString); if (cropGrowthInfo.TintColorInfo == null) { return(null); } return(cropGrowthInfo); }