TBMM/GCMM/MainModInstaller.cs

59 lines
2.2 KiB
C#
Raw Normal View History

using GCMM.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GCMM
{
partial class MainForm
{
public async Task InstallMod(ModInfo mod)
{
if (mod.DownloadURL == null) return;
if (!File.Exists(Settings.Default.GamePath + @"\Gamecraft.exe"))
{
MessageBox.Show("Gamecraft not found. Set the correct path in Settings.");
return;
}
var tmp = Directory.CreateDirectory("temp");
var plugins = Directory.CreateDirectory(Settings.Default.GamePath + @"\Plugins");
string tmppath = tmp.FullName + "\\" + mod.Name;
using (var client = GetClient())
{
await client.DownloadFileTaskAsync(mod.DownloadURL, tmppath);
string disposition = client.ResponseHeaders["Content-Disposition"];
string filename = disposition.Substring(disposition.IndexOf("filename=") + 10).Replace("\"", "");
if (filename.EndsWith(".dll"))
File.Move(tmppath, plugins.FullName + "\\" + filename);
else if (filename.EndsWith(".zip"))
{
bool pluginOnly = true;
using (var archive = ZipFile.OpenRead(tmppath))
{
foreach (var entry in archive.Entries)
{
if (entry.FullName == "Plugins/")
{
pluginOnly = false;
break;
}
}
archive.ExtractToDirectory(pluginOnly ? plugins.FullName : Settings.Default.GamePath, true);
}
}
else
{
MessageBox.Show("Don't know how to install file: " + filename);
return;
}
GetInstalledMods(); //Update list
}
}
}
}