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.

137 lines
6.2KB

  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 (!File.Exists(Settings.Default.GamePath + @"\Gamecraft.exe"))
  19. {
  20. MessageBox.Show("Gamecraft not found. Set the correct path in Settings.");
  21. return;
  22. }
  23. var tmp = Directory.CreateDirectory("temp");
  24. var plugins = Directory.CreateDirectory(Settings.Default.GamePath + @"\Plugins");
  25. string tmppath = tmp.FullName + "\\" + mod.Name;
  26. using (var client = GetClient())
  27. {
  28. await client.DownloadFileTaskAsync(mod.DownloadURL, tmppath);
  29. string disposition = client.ResponseHeaders["Content-Disposition"];
  30. string filename = disposition.Substring(disposition.IndexOf("filename=") + 10).Replace("\"", "");
  31. if (filename.EndsWith(".dll"))
  32. File.Move(tmppath, plugins.FullName + "\\" + mod.Name + ".dll"); //Force mod name to make uninstalls & identifying easier
  33. else if (filename.EndsWith(".zip"))
  34. {
  35. bool pluginOnly = true;
  36. using (var archive = ZipFile.OpenRead(tmppath))
  37. {
  38. bool modFound = false;
  39. foreach (var entry in archive.Entries)
  40. {
  41. if (entry.FullName == "Plugins/")
  42. pluginOnly = false;
  43. if (entry.FullName == "Plugins/" + mod.Name + ".dll")
  44. {
  45. modFound = true;
  46. pluginOnly = false; //The directory-only entry may be missing
  47. }
  48. else if (pluginOnly && entry.FullName == mod.Name + ".dll")
  49. modFound = true;
  50. if (!pluginOnly && modFound) break;
  51. }
  52. if (!modFound)
  53. 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)
  54. return;
  55. ExtractMod(archive, pluginOnly ? plugins.FullName : Settings.Default.GamePath, mod);
  56. }
  57. File.Delete(tmppath);
  58. }
  59. else
  60. {
  61. MessageBox.Show("Don't know how to install file: " + filename + "\nThe file remains in the temp folder near the mod manager");
  62. return;
  63. }
  64. GetInstalledMods(); //Update list
  65. }
  66. }
  67. public void ExtractMod(ZipArchive archive, string destinationDirectoryName, ModInfo mod)
  68. {
  69. LoadFileList(mod);
  70. DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
  71. string destinationDirectoryFullPath = di.FullName;
  72. foreach (ZipArchiveEntry file in archive.Entries)
  73. {
  74. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  75. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  76. {
  77. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  78. }
  79. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); //Sometimes there are no directory-only entries
  80. if (file.Name == "")
  81. {// Assuming Empty for Directory
  82. continue;
  83. }
  84. if (File.Exists(completeFileName) && !mod.ModFiles.Contains(completeFileName) && file.FullName != "Plugins/" + mod.Name + ".dll")
  85. { //If it's part of the mod files, then it's an update and it didn't originally exist in the game - the last condition is for dll->zip changes
  86. 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)
  87. continue;
  88. }
  89. mod.ModFiles.Add(completeFileName);
  90. file.ExtractToFile(completeFileName, true);
  91. }
  92. SaveFileList(mod);
  93. }
  94. public void SaveFileList(ModInfo mod)
  95. {
  96. if (mod.ModFiles != null)
  97. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  98. }
  99. public void LoadFileList(ModInfo mod)
  100. {
  101. if (File.Exists(mod.Name + ".json"))
  102. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  103. else
  104. mod.ModFiles = new HashSet<string>();
  105. }
  106. public void UninstallMod(ModInfo mod)
  107. {
  108. LoadFileList(mod);
  109. if (mod.ModFiles.Count == 0) //A single DLL
  110. File.Delete(Settings.Default.GamePath + @"\Plugins\" + mod.Name + ".dll");
  111. else //A ZIP
  112. {
  113. foreach (string file in mod.ModFiles)
  114. {
  115. File.Delete(file);
  116. var parent = Directory.GetParent(file);
  117. if (!parent.EnumerateFiles().Any())
  118. parent.Delete();
  119. }
  120. }
  121. File.Delete(mod.Name + ".json");
  122. mod.Version = null; //Not installed
  123. AddUpdateModInList(mod); //Update list
  124. }
  125. }
  126. }