|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Media; |
| 9 | + |
| 10 | +namespace nms_mod_manager |
| 11 | +{ |
| 12 | + class FileHandler |
| 13 | + { |
| 14 | + string path = AppDomain.CurrentDomain.BaseDirectory; |
| 15 | + string disablemods = AppDomain.CurrentDomain.BaseDirectory + "GAMEDATA\\PCBANKS\\disablemods.txt"; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Sanity checks exeecuted when starting up or refreshing the application. |
| 19 | + /// </summary> |
| 20 | + public int FolderCheck() |
| 21 | + { |
| 22 | + if (Directory.Exists(path + "GAMEDATA\\PCBANKS") == false || Directory.Exists(path + "Binaries") == false) |
| 23 | + { |
| 24 | + //Game folders not detected |
| 25 | + return 0; |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + //Check if the mods folders exist, if not create them |
| 30 | + Directory.CreateDirectory($"{path}GAMEDATA\\PCBANKS\\MODS"); |
| 31 | + Directory.CreateDirectory($"{path}mods"); |
| 32 | + |
| 33 | + //Check if the lock exists |
| 34 | + if (File.Exists(path + "GAMEDATA\\PCBANKS\\disablemods.txt")) |
| 35 | + { |
| 36 | + //Tell the Window to display a dialog |
| 37 | + return 2; |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + // If 'mods' is empty and mods are present in 'GAMEDATA\PCBANKS\MODS\ |
| 42 | + if (IsDirectoryEmpty($"{path}mods\\") && IsDirectoryEmpty($"{path}GAMEDATA\\PCBANKS\\MODS\\") == false) |
| 43 | + { |
| 44 | + //Copy the mods files from the game folder to the 'mods' folder |
| 45 | + CopyAllFiles(); |
| 46 | + return 3; |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + //Tell the Window it's all good |
| 51 | + return 1; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Copies all the mods from the game folder to the 'mods' folder |
| 59 | + /// </summary> |
| 60 | + public void CopyAllFiles() |
| 61 | + { |
| 62 | + DirectoryInfo enableDir = new DirectoryInfo($"{path}GAMEDATA\\PCBANKS\\MODS"); |
| 63 | + FileInfo[] enableFiles = enableDir.GetFiles("*.pak"); |
| 64 | + foreach (FileInfo file in enableFiles) |
| 65 | + { |
| 66 | + if (File.Exists($"{path}mods\\{file.Name}") == false) |
| 67 | + { |
| 68 | + Console.WriteLine($"{file.FullName} has been copied."); |
| 69 | + File.Copy(file.FullName, $"{path}mods\\{file.Name}"); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Copies the specified mod inside the game directory. |
| 76 | + /// </summary> |
| 77 | + /// <param name="name"> |
| 78 | + /// A string containing the mod's filename. |
| 79 | + /// </param> |
| 80 | + public void Copy(string name) |
| 81 | + { |
| 82 | + string modPath = path + "mods\\" + name; |
| 83 | + string storePath = path + "GAMEDATA\\PCBANKS\\MODS\\" + name; |
| 84 | + if (File.Exists(storePath) == false) |
| 85 | + { |
| 86 | + File.Copy(modPath, storePath); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Deletes the specified mod from the game directory. |
| 92 | + /// </summary> |
| 93 | + /// <param name="name"> |
| 94 | + /// A string containing the mod's filename. |
| 95 | + /// </param> |
| 96 | + public void Delete(string name) |
| 97 | + { |
| 98 | + string storePath = path + "GAMEDATA\\PCBANKS\\MODS\\" + name; |
| 99 | + if (File.Exists(storePath = path + "GAMEDATA\\PCBANKS\\MODS\\" + name)) |
| 100 | + { |
| 101 | + File.Delete(storePath); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Checks if a given directory is empty. |
| 107 | + /// </summary> |
| 108 | + /// <param name="path">Absolute path to the given directory.</param> |
| 109 | + /// <returns></returns> |
| 110 | + public bool IsDirectoryEmpty(string path) |
| 111 | + { |
| 112 | + return !Directory.EnumerateFileSystemEntries(path).Any(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments