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.

59 lines
2.2KB

  1. using GCMM.Properties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace GCMM
  11. {
  12. partial class MainForm
  13. {
  14. public async Task InstallMod(ModInfo mod)
  15. {
  16. if (mod.DownloadURL == null) return;
  17. if (!File.Exists(Settings.Default.GamePath + @"\Gamecraft.exe"))
  18. {
  19. MessageBox.Show("Gamecraft not found. Set the correct path in Settings.");
  20. return;
  21. }
  22. var tmp = Directory.CreateDirectory("temp");
  23. var plugins = Directory.CreateDirectory(Settings.Default.GamePath + @"\Plugins");
  24. string tmppath = tmp.FullName + "\\" + 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. File.Move(tmppath, plugins.FullName + "\\" + filename);
  32. else if (filename.EndsWith(".zip"))
  33. {
  34. bool pluginOnly = true;
  35. using (var archive = ZipFile.OpenRead(tmppath))
  36. {
  37. foreach (var entry in archive.Entries)
  38. {
  39. if (entry.FullName == "Plugins/")
  40. {
  41. pluginOnly = false;
  42. break;
  43. }
  44. }
  45. archive.ExtractToDirectory(pluginOnly ? plugins.FullName : Settings.Default.GamePath, true);
  46. }
  47. }
  48. else
  49. {
  50. MessageBox.Show("Don't know how to install file: " + filename);
  51. return;
  52. }
  53. GetInstalledMods(); //Update list
  54. }
  55. }
  56. }
  57. }