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.

132 lines
5.6KB

  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. modFound = true;
  45. if (!pluginOnly && modFound) break;
  46. }
  47. if (!modFound && !pluginOnly)
  48. 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)
  49. return;
  50. ExtractMod(archive, pluginOnly ? plugins.FullName : Settings.Default.GamePath, mod);
  51. }
  52. File.Delete(tmppath);
  53. }
  54. else
  55. {
  56. MessageBox.Show("Don't know how to install file: " + filename);
  57. return;
  58. }
  59. GetInstalledMods(); //Update list
  60. }
  61. }
  62. public void ExtractMod(ZipArchive archive, string destinationDirectoryName, ModInfo mod)
  63. {
  64. LoadFileList(mod);
  65. DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
  66. string destinationDirectoryFullPath = di.FullName;
  67. foreach (ZipArchiveEntry file in archive.Entries)
  68. {
  69. string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
  70. if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
  71. {
  72. throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
  73. }
  74. if (file.Name == "")
  75. {// Assuming Empty for Directory
  76. Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
  77. continue;
  78. }
  79. if (File.Exists(completeFileName) && !mod.ModFiles.Contains(completeFileName))
  80. { //If it's part of the mod files, then it's an update and it didn't originally exist in the game
  81. MessageBox.Show("The mod zip contains a file that exists as part of the game. Skipping file: " + file.FullName);
  82. continue;
  83. }
  84. mod.ModFiles.Add(completeFileName);
  85. file.ExtractToFile(completeFileName, true);
  86. }
  87. SaveFileList(mod);
  88. }
  89. public void SaveFileList(ModInfo mod)
  90. {
  91. if (mod.ModFiles != null)
  92. File.WriteAllText(mod.Name + ".json", JsonConvert.SerializeObject(mod.ModFiles));
  93. }
  94. public void LoadFileList(ModInfo mod)
  95. {
  96. if (File.Exists(mod.Name + ".json"))
  97. mod.ModFiles = JsonConvert.DeserializeObject<HashSet<string>>(File.ReadAllText(mod.Name + ".json"));
  98. else
  99. mod.ModFiles = new HashSet<string>();
  100. }
  101. public void UninstallMod(ModInfo mod)
  102. {
  103. LoadFileList(mod);
  104. if (mod.ModFiles.Count == 0) //A single DLL
  105. File.Delete(Settings.Default.GamePath + @"\Plugins\" + mod.Name + ".dll");
  106. else //A ZIP
  107. {
  108. foreach (string file in mod.ModFiles)
  109. {
  110. File.Delete(file);
  111. var parent = Directory.GetParent(file);
  112. if (!parent.EnumerateFiles().Any())
  113. parent.Delete();
  114. }
  115. }
  116. File.Delete(mod.Name + ".json");
  117. mod.Version = null; //Not installed
  118. AddUpdateModInList(mod); //Update list
  119. }
  120. }
  121. }