/// <summary> /// Builds a config message given the <see cref="DirectoryConfigPair"/>. /// </summary> /// <param name="pair"> /// The <see cref="DirectoryConfigPair"/>. /// </param> /// <returns> /// A message describing the conflicts in the given <see cref="DirectoryConfigPair"/>. /// </returns> private static string BuildConflictMessage(DirectoryConfigPair pair) { const string defaultMessage = "No conflicts detected."; if (!pair.FileExistsConflict && !pair.ConfigEntryConflict) { return(defaultMessage); } if (pair.FileExistsConflict && pair.ConfigEntryConflict) { return("The livery is already installed. (Will ignore this livery upon installation)"); } if (pair.FileExistsConflict) { return("The texture directory already exists but is not configured. (Resolvable)"); } if (pair.ConfigEntryConflict) { return("The texture is configured but corresponding directory is missing. (Resolvable)"); } return(defaultMessage); }
/// <summary> /// Inserts a new config entry into the given <paramref name="configFile"/> using the given <paramref name="pair"/> and FLTSIM <paramref name="number"/>. /// </summary> /// <param name="configFile"> /// The config file. /// </param> /// <param name="pair"> /// The <see cref="DirectoryConfigPair"/>. /// </param> /// <param name="number"> /// The FLTSIM number. /// </param> private static void InsertNewConfigEntry(string configFile, DirectoryConfigPair pair, int number) { // Get the config file data string configData = File.ReadAllText(configFile); // Check if the FLTSIM entry already exists string liveryHeader = $"[FLTSIM.{number}]"; if (configData.ToUpper().Contains(liveryHeader)) { throw new Exception($"The header \"{liveryHeader}\" already exists."); } // Get each line for the config file List <string> configLines = configData.Split("\r\n").ToList(); // Get the index of the blank line under the last FLTSIM entry int insertIndex = 0; bool nextBlankLine = false; foreach (string line in configLines) { // Increment insertIndex++; // If the current line is [FLTSIM.whatever the number is but - 1], // then our next blank line is where we want to insert into if (!nextBlankLine && line.ToUpper() == $"[FLTSIM.{number - 1}]") { nextBlankLine = true; } // Found the line we want, no need to keep going if (nextBlankLine && string.IsNullOrEmpty(line.Trim())) { break; } } // Get out new config lines string[] liveryConfigLines = pair.ConfigEntry.Split("\r\n"); // Set the header for (int i = 0; i < liveryConfigLines.Length; i++) { // Ignore lines that don't match if (!liveryConfigLines[i].ToUpper().StartsWith("[FLTSIM.")) { continue; } // Matching line found liveryConfigLines[i] = liveryHeader; } // Insert our lines configLines.InsertRange(insertIndex, (string.Join("\r\n", liveryConfigLines) + "\r\n").Split("\r\n")); // Convert back to a string string newConfigString = string.Join("\r\n", configLines); // Save the file File.WriteAllText(configFile, newConfigString); }