Techblox Mod Manager / Launcher
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

153 lines
7.0KB

  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 (GetExe() == null)
  19. {
  20. MessageBox.Show("Gamecraft not found. Set the correct path in Settings.");
  21. return;
  22. }
  23. if (mod.Name != "GamecraftModdingAPI")
  24. await UpdateAPI();
  25. var tmp = Directory.CreateDirectory("temp");
  26. var plugins = Directory.CreateDirectory(GamePath(@"\Plugins"));
  27. string tmppath = tmp.FullName + Path.DirectorySeparatorChar + mod.Name;
  28. using (var client = GetClient())
  29. {
  30. await client.DownloadFileTaskAsync(mod.DownloadURL, tmppath);
  31. string disposition = client.ResponseHeaders["Content-Disposition"];
  32. string filename = disposition.Substring(disposition.IndexOf("filename=") + 10).Replace("\"", "");
  33. if (filename.EndsWith(".dll"))
  34. File.Move(tmppath, plugins.FullName + Path.DirectorySeparatorChar + mod.Name + ".dll"); //Force mod name to make uninstalls & identifying easier
  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 : Settings.Default.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. foreach (ZipArchiveEntry file in archive.Entries)
  75. {
  76. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  77. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  78. {
  79. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  80. }
  81. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); //Sometimes there are no directory-only entries
  82. if (file.Name == "")
  83. {// Assuming Empty for Directory
  84. continue;
  85. }
  86. if ((mod.Version == null || mod.ModFiles.Count != 0) //Negated: The mod is installed and we don't know about any of its files
  87. && File.Exists(completeFileName) // OR it's a new file
  88. && !mod.ModFiles.Contains(completeFileName) // OR it's known to be part of the mod already
  89. && file.FullName != "Plugins/" + mod.Name + ".dll") // OR it's the plugin's DLL (dll->zip release)
  90. {
  91. 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)
  92. continue;
  93. }
  94. mod.ModFiles.Add(completeFileName);
  95. file.ExtractToFile(completeFileName, true);
  96. }
  97. SaveFileList(mod);
  98. }
  99. public void SaveFileList(ModInfo mod)
  100. {
  101. if (mod.ModFiles != null)
  102. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  103. }
  104. public void LoadFileList(ModInfo mod)
  105. {
  106. if (File.Exists(mod.Name + ".json"))
  107. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  108. else
  109. mod.ModFiles = new HashSet<string>();
  110. }
  111. public void UninstallMod(ModInfo mod)
  112. {
  113. LoadFileList(mod);
  114. if (mod.ModFiles.Count == 0) //A single DLL
  115. File.Delete(GamePath(@"\Plugins\" + mod.Name + ".dll"));
  116. else //A ZIP
  117. {
  118. foreach (string file in mod.ModFiles)
  119. {
  120. File.Delete(file);
  121. var parent = Directory.GetParent(file);
  122. if (!parent.EnumerateFiles().Any())
  123. parent.Delete();
  124. }
  125. }
  126. File.Delete(mod.Name + ".json");
  127. mod.Version = null; //Not installed
  128. AddUpdateModInList(mod); //Update list
  129. }
  130. public async Task UpdateAPI()
  131. {
  132. var gcmapi = mods["GamecraftModdingAPI"];
  133. if (!gcmapi.Installed || gcmapi.Updatable)
  134. {
  135. 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)
  136. == DialogResult.OK)
  137. await InstallMod(gcmapi);
  138. }
  139. }
  140. }
  141. }