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.

181 lines
8.3KB

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