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.

157 line
7.1KB

  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 != "GamecraftModdingAPI")
  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. 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 (MessageBox.Show("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", MessageBoxButtons.YesNo) == DialogResult.Yes)
  94. continue;
  95. }
  96. mod.ModFiles.Add(completeFileName);
  97. file.ExtractToFile(completeFileName, true);
  98. }
  99. SaveFileList(mod);
  100. }
  101. public void SaveFileList(ModInfo mod)
  102. {
  103. if (mod.ModFiles != null)
  104. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  105. }
  106. public void LoadFileList(ModInfo mod)
  107. {
  108. if (File.Exists(mod.Name + ".json"))
  109. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  110. else
  111. mod.ModFiles = new HashSet<string>();
  112. }
  113. public void UninstallMod(ModInfo mod)
  114. {
  115. LoadFileList(mod);
  116. if (mod.ModFiles.Count == 0) //A single DLL
  117. File.Delete(GamePath(@"\Plugins\" + mod.Name + ".dll"));
  118. else //A ZIP
  119. {
  120. foreach (string file in mod.ModFiles)
  121. {
  122. File.Delete(file);
  123. var parent = Directory.GetParent(file);
  124. if (!parent.EnumerateFiles().Any())
  125. parent.Delete(); //May delete the Plugins dir if empty
  126. }
  127. }
  128. File.Delete(mod.Name + ".json");
  129. mod.Version = null; //Not installed
  130. AddUpdateModInList(mod); //Update list
  131. }
  132. public async Task UpdateAPI()
  133. {
  134. if (!mods.ContainsKey("GamecraftModdingAPI"))
  135. return;
  136. var gcmapi = mods["GamecraftModdingAPI"];
  137. if (!gcmapi.Installed || gcmapi.Updatable)
  138. {
  139. if (MessageBox.Show($"GamecraftModdingAPI will be {(gcmapi.Installed ? "updated" : "installed")} as most mods need it to work. You can uninstall it if you're sure you don't need it.", "API needed", MessageBoxButtons.OKCancel)
  140. == DialogResult.OK)
  141. await InstallMod(gcmapi);
  142. }
  143. }
  144. }
  145. }