Techblox Mod Manager / Launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainModInstaller.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using GCMM.Properties;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace GCMM
  12. {
  13. partial class MainForm
  14. {
  15. public async Task InstallMod(ModInfo mod)
  16. {
  17. if (mod.DownloadURL == null) return;
  18. if (CheckNoExe())
  19. return;
  20. if (mod.Name != "TechbloxModdingAPI")
  21. await UpdateAPI();
  22. var tmp = Directory.CreateDirectory("temp");
  23. var plugins = Directory.CreateDirectory(GamePath(@"\Plugins"));
  24. string tmppath = tmp.FullName + Path.DirectorySeparatorChar + mod.Name;
  25. using (var client = GetClient())
  26. {
  27. await client.DownloadFileTaskAsync(mod.DownloadURL, tmppath);
  28. string disposition = client.ResponseHeaders["Content-Disposition"];
  29. string filename = disposition.Substring(disposition.IndexOf("filename=") + 10).Replace("\"", "");
  30. if (filename.EndsWith(".dll"))
  31. {
  32. string name = plugins.FullName + Path.DirectorySeparatorChar + mod.Name + ".dll"; //Force mod name to make uninstalls & identifying easier
  33. if (File.Exists(name))
  34. File.Delete(name);
  35. File.Move(tmppath, name);
  36. }
  37. else if (filename.EndsWith(".zip"))
  38. {
  39. bool pluginOnly = true;
  40. using (var archive = ZipFile.OpenRead(tmppath))
  41. {
  42. bool modFound = false;
  43. foreach (var entry in archive.Entries)
  44. {
  45. if (entry.FullName == "Plugins/")
  46. pluginOnly = false;
  47. if (entry.FullName == "Plugins/" + mod.Name + ".dll")
  48. {
  49. modFound = true;
  50. pluginOnly = false; //The directory-only entry may be missing
  51. }
  52. else if (pluginOnly && entry.FullName == mod.Name + ".dll")
  53. modFound = true;
  54. if (!pluginOnly && modFound) break;
  55. }
  56. if (!modFound)
  57. if (MessageBox.Show("The mod was not found in the downloaded archive. It likely means it's using a different name for the dll file. The mod manager will not be able to track this mod if you continue. Do you want to continue?", "Mod not found in archive", MessageBoxButtons.YesNo) == DialogResult.No)
  58. return;
  59. ExtractMod(archive, pluginOnly ? plugins.FullName : Settings.Default.GamePath, mod);
  60. }
  61. File.Delete(tmppath);
  62. }
  63. else
  64. {
  65. MessageBox.Show("Don't know how to install file: " + filename + "\nThe file remains in the temp folder near the mod manager");
  66. return;
  67. }
  68. GetInstalledMods(); //Update list
  69. }
  70. }
  71. public void ExtractMod(ZipArchive archive, string destinationDirectoryName, ModInfo mod)
  72. {
  73. LoadFileList(mod);
  74. DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
  75. string destinationDirectoryFullPath = di.FullName;
  76. bool? skipExisting = null;
  77. foreach (ZipArchiveEntry file in archive.Entries)
  78. {
  79. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  80. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  81. {
  82. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  83. }
  84. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); //Sometimes there are no directory-only entries
  85. if (file.Name == "")
  86. {// Assuming Empty for Directory
  87. continue;
  88. }
  89. if ((mod.Version == null || mod.ModFiles.Count != 0) //Negated: The mod is installed and we don't know about any of its files
  90. && File.Exists(completeFileName) // OR it's a new file
  91. && !mod.ModFiles.Contains(completeFileName) // OR it's known to be part of the mod already
  92. && file.FullName != "Plugins/" + mod.Name + ".dll") // OR it's the plugin's DLL (dll->zip release)
  93. {
  94. if (!skipExisting.HasValue)
  95. {
  96. var mbox = new CustomMessageBox("The mod zip contains a file that exists as part of the game. Do you want to skip this file?\n" + file.FullName + "\nOnly choose No if it's part of a previous installation of the mod.", "File is part of the game");
  97. var result = mbox.ShowDialog(this);
  98. if (mbox.ApplyToAll)
  99. skipExisting = result == DialogResult.Yes;
  100. if (result == DialogResult.Yes)
  101. continue;
  102. }
  103. else if (skipExisting.Value)
  104. continue;
  105. }
  106. mod.ModFiles.Add(completeFileName);
  107. file.ExtractToFile(completeFileName, true);
  108. }
  109. SaveFileList(mod);
  110. }
  111. public void SaveFileList(ModInfo mod)
  112. {
  113. if (mod.ModFiles != null)
  114. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  115. }
  116. public void LoadFileList(ModInfo mod)
  117. {
  118. if (File.Exists(mod.Name + ".json"))
  119. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  120. else
  121. mod.ModFiles = new HashSet<string>();
  122. }
  123. public void UninstallMod(ModInfo mod)
  124. {
  125. try
  126. {
  127. LoadFileList(mod);
  128. if (mod.ModFiles.Count == 0) //A single DLL
  129. File.Delete(GamePath(@"\Plugins\" + mod.Name + ".dll"));
  130. else //A ZIP
  131. {
  132. foreach (string file in mod.ModFiles)
  133. {
  134. if (!File.Exists(file)) continue; //If the folders don't exist then it errors
  135. File.Delete(file);
  136. var parent = Directory.GetParent(file);
  137. if (!parent.EnumerateFileSystemInfos().Any())
  138. parent.Delete(); //May delete the Plugins dir if empty
  139. }
  140. }
  141. File.Delete(mod.Name + ".json");
  142. mod.Version = null; //Not installed
  143. if (mod.Author != null)
  144. AddUpdateModInList(mod); //Update list
  145. else
  146. {
  147. mods.Remove(mod.Name);
  148. modlist.Items[mod.Name].Remove();
  149. }
  150. }
  151. catch (Exception e) when (e is UnauthorizedAccessException || e is IOException)
  152. {
  153. MessageBox.Show("Could not remove mod files! Make sure the game isn't running.\n" + e.Message);
  154. }
  155. }
  156. public async Task UpdateAPI()
  157. {
  158. if (!mods.ContainsKey("TechbloxModdingAPI"))
  159. return;
  160. var gcmapi = mods["TechbloxModdingAPI"];
  161. if (!gcmapi.Installed || gcmapi.Updatable)
  162. {
  163. if (MessageBox.Show(gcmapi.Installed ?
  164. "TechbloxModdingAPI will be updated as there's a new version available. It's needed for most mods to function."
  165. : "TechbloxModdingAPI will be installed as most mods need it to work. You can uninstall it if you're sure you don't need it.", "API needed",
  166. MessageBoxButtons.OKCancel) == DialogResult.OK)
  167. await InstallMod(gcmapi);
  168. }
  169. }
  170. }
  171. }