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.

193 lines
8.7KB

  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 TBMM
  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. UpdatePlayButtonColor();
  69. }
  70. public void ExtractMod(ZipArchive archive, string destinationDirectoryName, ModInfo mod)
  71. {
  72. LoadFileList(mod);
  73. DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
  74. string destinationDirectoryFullPath = di.FullName;
  75. bool? skipExisting = null;
  76. foreach (ZipArchiveEntry file in archive.Entries)
  77. {
  78. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  79. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  80. {
  81. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  82. }
  83. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); //Sometimes there are no directory-only entries
  84. if (file.Name == "")
  85. {// Assuming Empty for Directory
  86. continue;
  87. }
  88. if ((mod.Version == null || mod.ModFiles.Count != 0) //Negated: The mod is installed and we don't know about any of its files
  89. && File.Exists(completeFileName) // OR it's a new file
  90. && !mod.ModFiles.Contains(completeFileName) // OR it's known to be part of the mod already
  91. && file.FullName != "Plugins/" + mod.Name + ".dll") // OR it's the plugin's DLL (dll->zip release)
  92. {
  93. if (!skipExisting.HasValue)
  94. {
  95. 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");
  96. var result = mbox.ShowDialog(this);
  97. if (mbox.ApplyToAll)
  98. skipExisting = result == DialogResult.Yes;
  99. if (result == DialogResult.Yes)
  100. continue;
  101. }
  102. else if (skipExisting.Value)
  103. continue;
  104. }
  105. mod.ModFiles.Add(completeFileName);
  106. file.ExtractToFile(completeFileName, true);
  107. }
  108. SaveFileList(mod);
  109. }
  110. public void SaveFileList(ModInfo mod)
  111. {
  112. if (mod.ModFiles != null)
  113. {
  114. Directory.CreateDirectory(GamePath("\\ModInfo"));
  115. File.WriteAllText(GamePath($"\\ModInfo\\{mod.Name}.json"), JsonConvert.SerializeObject(mod.ModFiles));
  116. }
  117. }
  118. public void LoadFileList(ModInfo mod)
  119. {
  120. string[] paths =
  121. {
  122. GamePath($"\\ModInfo\\{mod.Name}.json"),
  123. mod.Name + ".json"
  124. };
  125. mod.ModFiles =
  126. paths.Where(File.Exists).Select(File.ReadAllText).Select(JsonConvert.DeserializeObject<HashSet<string>>)
  127. .FirstOrDefault() ?? new HashSet<string>();
  128. }
  129. public void UninstallMod(ModInfo mod)
  130. {
  131. try
  132. {
  133. LoadFileList(mod);
  134. if (mod.ModFiles.Count == 0) //A single DLL
  135. File.Delete(GamePath(@"\Plugins\" + mod.Name + ".dll"));
  136. else //A ZIP
  137. {
  138. foreach (string file in mod.ModFiles)
  139. {
  140. if (!File.Exists(file)) continue; //If the folders don't exist then it errors
  141. File.Delete(file);
  142. var parent = Directory.GetParent(file);
  143. if (!parent.EnumerateFileSystemInfos().Any())
  144. parent.Delete(); //May delete the Plugins dir if empty
  145. }
  146. }
  147. if (File.Exists(GamePath($"\\ModInfo\\{mod.Name}.json")))
  148. File.Delete(GamePath($"\\ModInfo\\{mod.Name}.json"));
  149. File.Delete(mod.Name + ".json");
  150. mod.Version = null; //Not installed
  151. if (mod.Author != null)
  152. AddUpdateModInList(mod); //Update list
  153. else
  154. {
  155. mods.Remove(mod.Name);
  156. modlist.Items[mod.Name].Remove();
  157. }
  158. }
  159. catch (Exception e) when (e is UnauthorizedAccessException || e is IOException)
  160. {
  161. MessageBox.Show("Could not remove mod files! Make sure the game isn't running.\n" + e.Message);
  162. }
  163. UpdatePlayButtonColor();
  164. }
  165. public async Task UpdateAPI()
  166. {
  167. if (!mods.ContainsKey("TechbloxModdingAPI"))
  168. return;
  169. var gcmapi = mods["TechbloxModdingAPI"];
  170. if (!gcmapi.Installed || gcmapi.Updatable)
  171. {
  172. if (MessageBox.Show(gcmapi.Installed ?
  173. "TechbloxModdingAPI will be updated as there's a new version available. It's needed for most mods to function."
  174. : "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",
  175. MessageBoxButtons.OKCancel) == DialogResult.OK)
  176. await InstallMod(gcmapi);
  177. }
  178. }
  179. }
  180. }