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.

140 lines
6.4KB

  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 ((mod.Version == null || mod.ModFiles.Count != 0) //Negated: The mod is installed and we don't know about any of its files
  85. && File.Exists(completeFileName) // OR it's a new file
  86. && !mod.ModFiles.Contains(completeFileName) // OR it's known to be part of the mod already
  87. && file.FullName != "Plugins/" + mod.Name + ".dll") // OR it's the plugin's DLL (dll->zip release)
  88. {
  89. 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)
  90. continue;
  91. }
  92. mod.ModFiles.Add(completeFileName);
  93. file.ExtractToFile(completeFileName, true);
  94. }
  95. SaveFileList(mod);
  96. }
  97. public void SaveFileList(ModInfo mod)
  98. {
  99. if (mod.ModFiles != null)
  100. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  101. }
  102. public void LoadFileList(ModInfo mod)
  103. {
  104. if (File.Exists(mod.Name + ".json"))
  105. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  106. else
  107. mod.ModFiles = new HashSet<string>();
  108. }
  109. public void UninstallMod(ModInfo mod)
  110. {
  111. LoadFileList(mod);
  112. if (mod.ModFiles.Count == 0) //A single DLL
  113. File.Delete(Settings.Default.GamePath + @"\Plugins\" + mod.Name + ".dll");
  114. else //A ZIP
  115. {
  116. foreach (string file in mod.ModFiles)
  117. {
  118. File.Delete(file);
  119. var parent = Directory.GetParent(file);
  120. if (!parent.EnumerateFiles().Any())
  121. parent.Delete();
  122. }
  123. }
  124. File.Delete(mod.Name + ".json");
  125. mod.Version = null; //Not installed
  126. AddUpdateModInList(mod); //Update list
  127. }
  128. }
  129. }