public static void SetMod(object state) { activeMod = GetActiveMod(); var op = state as SetMod; if (activeMod != null && Equals(activeMod, op.Mod) && activeMod.version == op.Mod.version) { return; } try { if (Directory.Exists(modDir)) { Directory.Delete(modDir, true); } log.Debug("Setting active mod to " + op.Mod.name + "."); FileSystem.CopyDirectory(Path.Combine(d2mpDir, op.Mod.name), modDir); File.WriteAllText(Path.Combine(modDir, "modname.txt"), JObject.FromObject(op.Mod).ToString(Formatting.Indented)); notifier.Notify(NotificationType.Success, "Active mod", "The current active mod has been set to " + op.Mod.name + "."); refreshMods(); //icon.DisplayBubble("Set active mod to " + op.Mod.name + "!"); } catch (Exception ex) { log.Error("Can't set mod " + op.Mod.name + ".", ex); notifier.Notify(NotificationType.Error, "Active mod", "Unable to set active mod, try closing Dota first."); //icon.DisplayBubble("Unable to set active mod, try closing Dota first."); } }
private static void SetMod(object state) { activeMod = GetActiveMod(); var op = state as SetMod; if (Equals(activeMod, op.Mod)) { return; } if (Directory.Exists(modDir)) { Directory.Delete(modDir, true); } log.Debug("Setting active mod to " + op.Mod.name + "."); FileSystem.CopyDirectory(Path.Combine(d2mpDir, op.Mod.name), modDir); File.WriteAllText(Path.Combine(modDir, "modname.txt"), JObject.FromObject(op.Mod).ToString(Formatting.Indented)); icon.DisplayBubble("Set active mod to " + op.Mod.name + "!"); }
internal static void manualInstallMod() { if (isInstalling) { notifier.Notify(NotificationType.Warning, "Already downloading a mod", "Please try again after a few seconds."); return; } isInstalling = true; using (var dlg = new OpenFileDialog() { CheckFileExists = true, CheckPathExists = true, DefaultExt = "zip", Filter = "Zip Files|*.zip", FilterIndex = 1, Multiselect = false, Title = "Choose the mod zip to install" }) { //user pressed ok if (dlg.ShowDialog() == DialogResult.OK) { //open the file in a stream using (var fileStream = dlg.OpenFile()) { ZipFile zip = new ZipFile(fileStream); //check integrity if (zip.TestArchive(true)) { //look for the map file. It contains the mod name ZipEntry map = zip.Cast<ZipEntry>().FirstOrDefault(a => a.Name.ToLower().EndsWith(".bsp")); if (map != null) { //look for the version file int entry = zip.FindEntry("addoninfo.txt", true); if (entry >= 0) { string allText = string.Empty; using (var infoStream = new StreamReader(zip.GetInputStream(entry))) allText = infoStream.ReadToEnd(); string version = modController.ReadAddonVersion(allText); if (!string.IsNullOrEmpty(version)) { Version v = new Version(version); string name = Path.GetFileNameWithoutExtension(map.Name).ToLower(); //check if this same mod is already installed and if it needs an update if (modController.clientMods.Any( a => a.name.ToLower().Equals(name) && new Version(a.version) >= v)) { MessageBox.Show("The mod you are trying to install is already installed or outdated.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { string targetDir = Path.Combine(d2mpDir, name); if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true); //Make the dir again Directory.CreateDirectory(targetDir); if (UnzipWithTemp(null, fileStream, targetDir)) { refreshMods(); log.Info("Mod manually installed!"); notifier.Notify(NotificationType.Success, "Mod installed", "The following mod has been installed successfully: " + name); var mod = new ClientMod() {name = name, version = v.ToString()}; var msg = new OnInstalledMod() {Mod = mod}; Send(JObject.FromObject(msg).ToString(Formatting.None)); var existing = modController.clientMods.FirstOrDefault(m => m.name == mod.name); if (existing != null) modController.clientMods.Remove(existing); modController.clientMods.Add(mod); } else { MessageBox.Show("The mod could not be installed. Read the log file for details.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } else { MessageBox.Show("Could not read the mod version from the zip file.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("No mod info was found in the zip file.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("No mod map was found in the zip file.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("The zip file you selected seems to be invalid.", "Mod Manual Install", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } } isInstalling = false; }
public static void SetMod(object state) { activeMod = GetActiveMod(); var op = state as SetMod; if (activeMod != null && Equals(activeMod, op.Mod) && activeMod.version == op.Mod.version) return; try { if (Directory.Exists(modDir)) Directory.Delete(modDir, true); log.Debug("Setting active mod to " + op.Mod.name + "."); FileSystem.CopyDirectory(Path.Combine(d2mpDir, op.Mod.name), modDir); File.WriteAllText(Path.Combine(modDir, "modname.txt"), JObject.FromObject(op.Mod).ToString(Formatting.Indented)); notifier.Notify(NotificationType.Success, "Active mod", "The current active mod has been set to " + op.Mod.name + "."); refreshMods(); //icon.DisplayBubble("Set active mod to " + op.Mod.name + "!"); } catch (Exception ex) { log.Error("Can't set mod " + op.Mod.name + ".", ex); notifier.Notify(NotificationType.Error, "Active mod", "Unable to set active mod, try closing Dota first."); //icon.DisplayBubble("Unable to set active mod, try closing Dota first."); } }
public static void DeleteMod(object state) { var op = state as DeleteMod; string targetDir = Path.Combine(d2mpDir, op.Mod.name); if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true); //close dota before removing active mod, otherwise we might crash if (activeMod != null && activeMod.name == op.Mod.name) { //if (Dota2Running()) KillDota2(); try { Directory.Delete(modDir, true); } finally { activeMod = GetActiveMod(); } } log.Debug("Server/user requested that we delete mod " + op.Mod.name + "."); var msg = new OnDeletedMod { Mod = op.Mod }; Send(JObject.FromObject(msg).ToString(Formatting.None)); var existing = modController.clientMods.FirstOrDefault(m => m.name == op.Mod.name); if (existing != null) modController.clientMods.Remove(existing); refreshMods(); }
public static void InstallMod(object state) { var op = state as InstallMod; if (isInstalling) { notifier.Notify(3, "Already downloading a mod", "Please try again after a few seconds."); //icon.DisplayBubble("Already downloading a mod!"); return; } isInstalling = true; notifier.Notify(5, "Downloading mod", "Downloading " + op.Mod.name + "..."); //icon.DisplayBubble("Attempting to download "+op.Mod.name+"..."); log.Info("Server requested that we install mod " + op.Mod.name + " from download " + op.url); //delete if already exists string targetDir = Path.Combine(d2mpDir, op.Mod.name); if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true); //Make the dir again Directory.CreateDirectory(targetDir); //Stream the ZIP to the folder try { using (var wc = new WebClient()) { int lastProgress = -1; wc.DownloadProgressChanged += (sender, e) => { notifier.reportProgress(e.ProgressPercentage); if (e.ProgressPercentage % 5 == 0 && e.ProgressPercentage > lastProgress) { lastProgress = e.ProgressPercentage; log.Info(String.Format("Downloading mod: {0}% complete.", e.ProgressPercentage)); } }; wc.DownloadDataCompleted += (sender, e) => { byte[] buffer = { 0 }; bool success = false; try { buffer = e.Result; success = true; } catch (Exception ex) { log.Error("Error while downloading", ex); notifier.Notify(4, "Error downloading mod", "The connection forcibly closed by the remote host. Please try again."); } if (success) { //close dota before installing, otherwise we crash //if (activeMod != null && activeMod.name == op.Mod.name && Dota2Running()) //{ // notifier.Notify(2, "Extracting mod", "Closing Dota"); // KillDota2(); //} notifier.Notify(2, "Extracting mod", "Download completed, extracting files..."); Stream s = new MemoryStream(buffer); if (UnzipFromStream(s, targetDir)) { refreshMods(); log.Info("Mod installed!"); notifier.Notify(1, "Mod installed", "The following mod has been installed successfully: " + op.Mod.name); //icon.DisplayBubble("Mod downloaded successfully: " + op.Mod.name + "."); var msg = new OnInstalledMod() { Mod = op.Mod }; Send(JObject.FromObject(msg).ToString(Formatting.None)); var existing = modController.clientMods.FirstOrDefault(m => m.name == op.Mod.name); if (existing != null) modController.clientMods.Remove(existing); modController.clientMods.Add(op.Mod); activeMod = GetActiveMod(); if (activeMod != null && activeMod.name == op.Mod.name) { try { Directory.Delete(modDir, true); } finally { activeMod = GetActiveMod(); } } } } isInstalling = false; }; wc.DownloadDataAsync(new Uri(op.url)); } } catch (Exception ex) { isInstalling = false; log.Error("Failed to download mod " + op.Mod.name + ".", ex); notifier.Notify(4, "Error downloading mod", "Failed to download mod " + op.Mod.name + "."); return; } }
private static void SetMod(object state) { activeMod = GetActiveMod(); var op = state as SetMod; if (Equals(activeMod, op.Mod)) return; if (Directory.Exists(modDir)) Directory.Delete(modDir, true); log.Debug("Setting active mod to " + op.Mod.name + "."); FileSystem.CopyDirectory(Path.Combine(d2mpDir, op.Mod.name), modDir); File.WriteAllText(Path.Combine(modDir, "modname.txt"), JObject.FromObject(op.Mod).ToString(Formatting.Indented)); icon.DisplayBubble("Set active mod to " + op.Mod.name + "!"); }