1using System; 2using System.IO; 3using System.Text; 4 5namespace OpenBve { 6 internal static partial class ManagedContent { 7 8 // --- static functions --- 9 10 /// <summary>Gets the directory of the specified package if installed.</summary> 11 /// <param name="package">The package name.</param> 12 /// <returns>The directory of the specified package, or a null reference if not installed.</returns> 13 /// <remarks>If multiple instances of the package exists, this function returns the first one it encounters.</remarks> 14 internal static string GetInstalledPackageDirectory(string package) { 15 for (int i = 0; i < Program.FileSystem.ManagedContentFolders.Length; i++) { 16 string directory = OpenBveApi.Path.CombineDirectory(Program.FileSystem.ManagedContentFolders[i], package); 17 if (Directory.Exists(directory)) { 18 return directory; 19 } 20 } 21 return null; 22 } 23 24 /// <summary>Gets the currently installed version of the specified package.</summary> 25 /// <param name="package">The package name.</param> 26 /// <returns>The currently installed version of the specified package, or a null reference if the package is not installed.</returns> 27 internal static string GetInstalledPackageVersion(string package) { 28 foreach (string lookupDirectory in Program.FileSystem.ManagedContentFolders) { 29 string packageDirectory = OpenBveApi.Path.CombineDirectory(lookupDirectory, package); 30 if (Directory.Exists(packageDirectory)) { 31 string file = OpenBveApi.Path.CombineFile(packageDirectory, "package.cfg"); 32 if (File.Exists(file)) { 33 string[] lines = File.ReadAllLines(file, Encoding.UTF8); 34 for (int j = 0; j < lines.Length; j++) { 35 lines[j] = lines[j].Trim(); 36 int equals = lines[j].IndexOf('='); 37 if (equals >= 0) { 38 string key = lines[j].Substring(0, equals).TrimEnd(); 39 if (key.Equals("version", StringComparison.OrdinalIgnoreCase)) { 40 string value = lines[j].Substring(equals + 1).TrimStart(); 41 return value; 42 } 43 } 44 } 45 } 46 return "0.0"; 47 } 48 } 49 return null; 50 } 51 52 /// <summary>Checks whether the specified package is installed and protected from modification or deletion.</summary> 53 /// <param name="package">The package name.</param> 54 /// <returns>Whether the specified package is protected.</returns> 55 internal static bool IsInstalledPackageProtected(string package) { 56 foreach (string lookupDirectory in Program.FileSystem.ManagedContentFolders) { 57 string packageDirectory = OpenBveApi.Path.CombineDirectory(lookupDirectory, package); 58 if (Directory.Exists(packageDirectory)) { 59 string file = OpenBveApi.Path.CombineFile(packageDirectory, "package.cfg"); 60 if (File.Exists(file)) { 61 string[] lines = File.ReadAllLines(file, Encoding.UTF8); 62 for (int j = 0; j < lines.Length; j++) { 63 lines[j] = lines[j].Trim(); 64 int equals = lines[j].IndexOf('='); 65 if (equals >= 0) { 66 string key = lines[j].Substring(0, equals).TrimEnd(); 67 if (key.Equals("protected", StringComparison.OrdinalIgnoreCase)) { 68 string value = lines[j].Substring(equals + 1).TrimStart(); 69 if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) { 70 return true; 71 } 72 } 73 } 74 } 75 } 76 } 77 } 78 return false; 79 } 80 81 /// <summary>Removes the specified directory if it exists.</summary> 82 /// <param name="path">The path to the directory.</param> 83 private static void RemoveDirectory(string path) { 84 if (Directory.Exists(path)) { 85 Directory.Delete(path, true); 86 } 87 } 88 89 /// <summary>Creates the specified directory if it does not already exist.</summary> 90 /// <param name="path">The path to the directory.</param> 91 private static void CreateDirectory(string path) { 92 if (!Directory.Exists(path)) { 93 Directory.CreateDirectory(path); 94 } 95 } 96 97 } 98}