public static void AddAddonDownloadUri(IAddon addon, string uri) { throw new NotSupportedException(); //try //{ // var key = addon.KeyNames.FirstOrDefault(); // RestAddonUri item = null; // if (key != null) // { // item = new RestAddonUri // { // Hash = key.PubK.ToBase64().ComputeSha1Hash(), // Uri = uri // }; // } // var request = new RestRequest("/Addons/AddAddonDownloadUri", Method.POST).htua(_offset); // request.AddJsonBody(item); // var restResult = RestClient.Execute(request); // if (restResult.StatusCode == HttpStatusCode.OK) // { // return; // } // ; //} //catch (Exception exception) //{ // Debug.WriteLine(exception); //} }
/// <summary> /// Get a list of all available server types /// </summary> /// <returns></returns> private static Dictionary <string, Type> GetAvailableAddons() { List <Type> unsortedAddons = new List <Type>(); Dictionary <string, Type> addons = new Dictionary <string, Type>(); // Get unsorted list of all addons foreach ( Type addon in DynamicModuleLoader.ListClassesRecursiveOfType <IAddon>("Net.Bertware.Bukkitgui2.AddOn")) { unsortedAddons.Add(addon); } unsortedAddons.Sort((t1, t2) => { IAddon a1 = (IAddon)Activator.CreateInstance(t1); IAddon a2 = (IAddon)Activator.CreateInstance(t2); return(a2.Priority.CompareTo(a1.Priority)); }); foreach (Type addon in unsortedAddons) { if (!addons.ContainsKey(addon.Name)) { addons.Add(addon.Name, addon); } } Logger.Log(LogLevel.Info, "AddonManager", "Found " + addons.Count + " available addons"); return(addons); }
private void btnReload_Click(object sender, EventArgs e) { IAddon notifications = AddonManager.GetAddon("Notifications"); notifications.Dispose(); notifications.Initialize(); }
public async Task LoadSpecific(Bot bot, string module) { string s = CurrentDirectory + (module.EndsWith(".dll") ? module : module + ".dll"); Assembly addon = Assembly.Load(File.ReadAllBytes(s)); string newS = s.Split('/')[s.Split('/').Length - 1]; newS = newS.Remove(newS.Length - 4); IAddon currentAddon = addon.CreateInstance(newS + ".Addon") as IAddon; if (currentAddon != null) { RuntimeAddonInstance aInstance = new RuntimeAddonInstance(); aInstance = new RuntimeAddonInstance(await currentAddon.Create(aInstance), bot); foreach (IModule nm in aInstance.Modules) { if (bot.Events.GetModuleByName(nm.Name) != null) { Log.Warning("Module already loaded, stopping load"); return; } RuntimeModule newModule = new RuntimeModule(nm); await newModule.InstallAsync(bot); } Log.Done($"Loaded Add-On \"{newS}\" successfully"); } else { Log.Error($"failed to reload module \"{newS}\""); } }
public async Task Unload(Bot bot, string module) { string s = CurrentDirectory + module + ".dll"; Assembly addon = Assembly.Load(File.ReadAllBytes(s)); string newS = s.Split('/')[s.Split('/').Length - 1]; newS = newS.Remove(newS.Length - 4); IAddon currentAddon = addon.CreateInstance(newS + ".Addon") as IAddon; if (currentAddon != null) { RuntimeAddonInstance aInstance = new RuntimeAddonInstance(); aInstance = new RuntimeAddonInstance(await currentAddon.Create(aInstance), bot); foreach (IModule nm in aInstance.Modules) { IModule mod = bot.Events.GetModuleByName(nm.Name); if (mod != null) { await mod.UninstallAsync(bot); } } Log.Done($"Unloaded Add-On \"{newS}\" successfully"); } else { Log.Error($"failed to unload module \"{newS}\""); } }
internal void AddAddonUri(IAddon addon, string uri) { //Task.Run(() => //{ // var webapi = new AddonWebApi(); // webapi.AddAddonDownloadUri(addon,uri); //}); }
public async Task RemoveAddonAsync(IAddon addon) { if (addon == null) { return; } await addon.OnRemovedAsync(); _addons.Remove(addon); }
internal static async Task UploadAddonAsync(IAddon addon) { IEnumerable <RestAddonInfoResult> infos = await AddonWebApi.GetAddonInfosAsync(addon.KeyNames.Select(k => k.Hash).ToArray()); if (infos.Count() == 0 || infos.Any(a => a.easyinstall)) { return; } AddonWebApi.UploadAddon(addon); }
internal void AddOrUpdate(IAddon addon) { Debug.Assert(addon != null); var key = addon.Key; if (_addons.Contains(key)) { _addons.Remove(key); } _addons.Add(addon); }
/// <summary> /// Dynamicly load an addon (also registers it) /// </summary> /// <param name="addonType"></param> private static IAddon CreateAddon(IAddon addonType) { try { IAddon addon = (IAddon)Activator.CreateInstance(addonType.GetType()); RegisterAddon(addon); return(addon); } catch (Exception exception) { Logger.Log(LogLevel.Severe, "AddonManager", "Failed to create addon! " + addonType, exception.Message); return(null); } }
public DecoRedeedGump(IAddon addon) : base(150, 50) { m_Addon = addon; AddBackground(0, 0, 220, 170, 0x13BE); AddBackground(10, 10, 200, 150, 0xBB8); if (addon is Banner) AddHtmlLocalized( 20, 30, 180, 60, 1018318, false, false ); // Do you wish to re-deed this banner? else if (addon is FlamingHead) AddHtmlLocalized( 20, 30, 180, 60, 1018329, false, false ); // Do you wish to re-deed this skull? else AddHtmlLocalized( 20, 30, 180, 60, 1062839, false, false ); // Do you wish to re-deed this decoration? AddHtmlLocalized(55, 100, 160, 25, 1011011, false, false); // CONTINUE AddButton(20, 100, 0xFA5, 0xFA7, 1, GumpButtonType.Reply, 0); AddHtmlLocalized(55, 125, 160, 25, 1011012, false, false); // CANCEL AddButton(20, 125, 0xFA5, 0xFA7, 0, GumpButtonType.Reply, 0); }
void RunAddonOnce(IAddon addon) { if (addon.Dependence.Any()) { foreach (string name in addon.Dependence) { IAddon dependence = addons.FirstOrDefault(x => x.Name == name); RunAddonOnce(dependence); } } if (!runningAddons[addon.Name]) { addon.RunOnce(); runningAddons[addon.Name] = true; } }
/// <summary> /// Loads addons in ./modules folder /// </summary> public async Task Load(Bot bot) { if (!Directory.Exists(CurrentDirectory) || Directory.GetFiles(CurrentDirectory).Length == 0) { Log.Warning("No modules found, ignoring..."); Directory.CreateDirectory(CurrentDirectory); return; } string[] allFiles = Directory.GetFiles(CurrentDirectory); foreach (string s in allFiles) { string newS = s.Split('/')[s.Split('/').Length - 1]; newS = newS.Remove(newS.Length - 4); try { if (!s.EndsWith(".dll")) { continue; } Assembly addon = Assembly.Load(File.ReadAllBytes(s)); IAddon currentAddon = addon.CreateInstance(newS + ".Addon") as IAddon; if (currentAddon != null) { IAddonInstance aInstance = new RuntimeAddonInstance(); aInstance = await currentAddon.Create(aInstance); foreach (IModule nm in aInstance.Modules) { IModule newModule = new RuntimeModule(nm); await newModule.InstallAsync(bot); } Log.Done($"loaded Add-On {newS} successfully"); } } catch { Log.Warning($"Module {newS} is not compatible with this version (v{Bot.VersionNumber})"); } } }
/// <summary> /// Dynamicly load an addon (also registers it) /// </summary> /// <param name="name"></param> private static IAddon CreateAddon(string name) { if (string.IsNullOrEmpty(name)) { return(null); } try { IAddon addon = (IAddon)Activator.CreateInstance(Assembly.GetExecutingAssembly().GetType(name)); RegisterAddon(addon); return(addon); } catch (Exception exception) { Logger.Log(LogLevel.Severe, "AddonManager", "Failed to create addon! " + name, exception.Message); return(null); } }
internal async void DownloadAddonAsync(IAddon addon) { if (addon.IsEasyInstallable.HasValue && addon.IsEasyInstallable.Value && addon.KeyNames.Any() && addon.KeyNames.Any(k => !string.IsNullOrEmpty(k.Hash))) { var installedAddon = await Task.Run(() => { var targetFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.DoNotVerify) + Path.DirectorySeparatorChar + @"ArmaBrowser" + Path.DirectorySeparatorChar + "Arma 3" + Path.DirectorySeparatorChar + "Addons" + Path.DirectorySeparatorChar; var hash = addon.KeyNames.First(k => !string.IsNullOrEmpty(k.Hash)).Hash; AddonWebApi.DownloadAddon(addon, hash, targetFolder); var addons = _defaultDataRepository.GetInstalledAddons(targetFolder); var item = addons.FirstOrDefault(a => a.KeyNames.Any(k => k.Hash == hash)); if (item != null) { return(new Addon { Name = item.Name, DisplayText = string.Format("{0} ({1})", item.DisplayText, item.Name), Path = item.Path, ModName = item.ModName, Version = item.Version, KeyNames = item.KeyNames, IsInstalled = true, IsEasyInstallable = true, CanActived = addon.CanActived, IsActive = addon.CanActived }); } return(null); }); if (installedAddon != null) { var idx = _addons.IndexOf(addon); _addons[idx] = installedAddon; } } }
/// <summary> /// Initialize and register an addon /// </summary> /// <param name="addon"></param> private static void RegisterAddon(IAddon addon) { // initialize addon.Initialize(); // add AddonsDictionary.Add(addon.Name, addon); // if this addon has a tab, add it if (addon.HasTab) { TabsDictionary.Add(addon, addon.TabPage); } // if this addon has a settings page, add it if (addon.HasConfig) { SettingsDictionary.Add(addon, addon.ConfigPage); } }
public RewardDemolitionGump( IAddon addon, int question ) : base( 150, 50 ) { m_Addon = addon; Closable = true; Disposable = true; Dragable = true; Resizable = false; AddBackground( 0, 0, 220, 170, 0x13BE ); AddBackground( 10, 10, 200, 150, 0xBB8 ); AddHtmlLocalized( 20, 30, 180, 60, question, false, false ); // Do you wish to re-deed this decoration? AddHtmlLocalized( 55, 100, 150, 25, 1011011, false, false ); // CONTINUE AddButton( 20, 100, 0xFA5, 0xFA7, (int) Buttons.Confirm, GumpButtonType.Reply, 0 ); AddHtmlLocalized( 55, 125, 150, 25, 1011012, false, false ); // CANCEL AddButton( 20, 125, 0xFA5, 0xFA7, (int) Buttons.Cancel, GumpButtonType.Reply, 0 ); }
public RewardDemolitionGump(IAddon addon, int question) : base(150, 50) { m_Addon = addon; Closable = true; Disposable = true; Dragable = true; Resizable = false; AddBackground(0, 0, 220, 170, 0x13BE); AddBackground(10, 10, 200, 150, 0xBB8); AddHtmlLocalized(20, 30, 180, 60, question, false, false); // Do you wish to re-deed this decoration? AddHtmlLocalized(55, 100, 150, 25, 1011011, false, false); // CONTINUE AddButton(20, 100, 0xFA5, 0xFA7, (int)Buttons.Confirm, GumpButtonType.Reply, 0); AddHtmlLocalized(55, 125, 150, 25, 1011012, false, false); // CANCEL AddButton(20, 125, 0xFA5, 0xFA7, (int)Buttons.Cancel, GumpButtonType.Reply, 0); }
public static int ComputeHash(IAddon addon) { unchecked { var hash = -1; hash = (hash * 397) ^ addon.GetTypeHashCode(); IList comp; if (addon.GetPropertyValue("Components", out comp)) { hash = (hash * 397) ^ comp.Count; hash = comp.Cast <Item>().Aggregate(hash, (h, c) => (h * 397) ^ c.GetTypeHashCode()); hash = comp.Cast <Item>().Aggregate(hash, (h, c) => (h * 397) ^ c.ItemID); } return(hash); } }
/// <summary> /// Reload an addon /// </summary> /// <param name="name"></param> /// <remarks>Reloading addons is highly discouraged! Only use if no other way is possible</remarks> public static void ReloadAddon(string name) { if (string.IsNullOrEmpty(name)) { return; } try { IAddon addon = GetAddon(name); if (addon == null) { return; } if (addon.HasTab) { return; // we can't reload these yet! } if (addon.HasTab && TabsDictionary.ContainsKey(addon)) { TabsDictionary.Remove(addon); } if (addon.HasConfig && SettingsDictionary.ContainsKey(addon)) { SettingsDictionary.Remove(addon); } Settings.Settings settingsmanager = ((Settings.Settings)GetRequiredAddon(RequiredAddon.Settings)); settingsmanager.RemoveAddonSettings(addon); //unload settings addon.Dispose(); //dispose old addon addon = CreateAddon(addon); // create new one + initialize settingsmanager.AddAddonSettings(addon); // load settings } catch (Exception exception) { Logger.Log(LogLevel.Severe, "AddonManager", "Failed to reload addon! " + name, exception.Message); } }
public DecoRedeedGump(IAddon addon) : base(150, 50) { m_Addon = addon; AddBackground(0, 0, 220, 170, 0x13BE); AddBackground(10, 10, 200, 150, 0xBB8); if (addon is Banner) { AddHtmlLocalized(20, 30, 180, 60, 1018318, false, false); // Do you wish to re-deed this banner? } else if (addon is FlamingHead) { AddHtmlLocalized(20, 30, 180, 60, 1018329, false, false); // Do you wish to re-deed this skull? } else { AddHtmlLocalized(20, 30, 180, 60, 1062839, false, false); // Do you wish to re-deed this decoration? } AddHtmlLocalized(55, 100, 160, 25, 1011011, false, false); // CONTINUE AddButton(20, 100, 0xFA5, 0xFA7, 1, GumpButtonType.Reply, 0); AddHtmlLocalized(55, 125, 160, 25, 1011012, false, false); // CANCEL AddButton(20, 125, 0xFA5, 0xFA7, 0, GumpButtonType.Reply, 0); }
/// <summary> /// Add the settings control for a given addon /// </summary> /// <param name="addon"></param> /// <remarks>Reloading addons is highly discouraged! Only use if no other way is possible</remarks> public void AddAddonSettings(IAddon addon) { }
public void Setup() { fakeAddon = Substitute.For <IAddon>(); handler = new DefaultApplicationEventsHandler(fakeAddon); }
public void DownloadAddon(IAddon addon) { _context.DownloadAddonAsync(addon); }
internal static void DownloadAddon(IAddon addon, string addonPubKeyHash, string targetFolder) { // var request = new RestRequest("/addonfiles/" + addonPubKeyHash + ".zip", Method.GET); // var tmpfile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); // var md5Hash = ""; // //request.AddParameter("hash", addonPubKeyHash) // // .AddHeader("ACCEPT", "application/zip") // // .htua(); // request.ResponseWriter = (res, stream) => // { // int fileLen = -1; // var hLen = res.Headers.FirstOrDefault(h => "Content-Length".Equals(h.Name, StringComparison.OrdinalIgnoreCase)); // if (hLen != null) // int.TryParse(hLen.Value, out fileLen); // var md5Header = res.Headers.FirstOrDefault(h => "Content-MD5".Equals(h.Name, StringComparison.OrdinalIgnoreCase)); // if (md5Header != null) // md5Hash = md5Header.Value; // var s = stream;//as System.IO.Compression.GZipStream; // var myAddon = addon as Addon; // if (res.StatusCode == HttpStatusCode.OK // //&& "application/zip".Equals(res.ContentType, StringComparison.OrdinalIgnoreCase) // ) // { // try // { // var buffer = new byte[1024 * 8]; // var downloadLen = 0; // using (var tmpfs = new FileStream(tmpfile, FileMode.Open, FileAccess.Write)) // { // int len; // do // { // len = s.Read(buffer, 0, buffer.Length); // tmpfs.Write(buffer, 0, len); // downloadLen += len; // if (myAddon == null || downloadLen <= 0) continue; // var progress = Convert.ToInt32(downloadLen * 100L / fileLen); // myAddon.ProgressValue = progress > 0 ? progress : 1; // } while (len > 0); // tmpfs.Flush(); // if (myAddon != null) // myAddon.ProgressValue = 0; // } // } // finally // { // if (myAddon != null) // myAddon.ProgressValue = 0; // } // } // }; // RestClient.ClearHandlers(); // ExecuteRequest(request); // if (!string.IsNullOrEmpty(md5Hash)) // { // using (var md5 = MD5.Create()) // { // var calc = md5.FromFile(tmpfile); // if (calc != md5Hash) // { // Trace.WriteLine("MD5 Check Fail"); // } // } // } // if (File.Exists(tmpfile)) // { // try // { // using (var tmpfs = new FileStream(tmpfile, FileMode.Open, FileAccess.Read)) // using (ZipArchive archive = new ZipArchive(tmpfs, ZipArchiveMode.Read, true)) // { // archive.ExtractToDirectory(targetFolder); // } // } // finally // { // File.Delete(tmpfile); // } // } }
public DefaultApplicationEventsHandler(IAddon addon) : base(addon) { }
//public event EventHandler InjectionRequest2; public void InjectDependency(IAddon Addon) { LoadedAddons.Add(Addon); //logger.Debug("Addon injected: {0}", Addon.CoreName); }
/// <summary> /// Dynamicly load an addon (also registers it) /// </summary> /// <param name="addonType"></param> private static IAddon CreateAddon(IAddon addonType) { try { IAddon addon = (IAddon) Activator.CreateInstance(addonType.GetType()); RegisterAddon(addon); return addon; } catch (Exception exception) { Logger.Log(LogLevel.Severe, "AddonManager", "Failed to create addon! " + addonType, exception.Message); return null; } }
/// <summary> /// Initializes the base class with the addon instance to use. /// </summary> /// <param name="addon"> /// The addon to use. /// </param> protected ApplicationEventsHandlerBase(IAddon addon) { this.addon = addon; }
/// <summary> /// Remove the settings control for a given addon /// </summary> /// <param name="addon"></param> /// <remarks>Reloading addons is highly discouraged! Only use if no other way is possible</remarks> public void RemoveAddonSettings(IAddon addon) { }
internal static void UploadAddon(IAddon addon) { //try //{ // var key = addon.KeyNames.FirstOrDefault(); // if (key != null) // { // var hash = key.PubK.ToBase64().ComputeSha1Hash(); // IRestRequest request = new RestRequest("/Addons/UploadAddon", Method.POST); // var a = addon; // const string zipFilePath = "tmp.zip"; // if (File.Exists(zipFilePath)) File.Delete(zipFilePath); // ZipFile.CreateFromDirectory(a.Path, zipFilePath, CompressionLevel.Optimal, true); // try // { // using (var fs = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read, FileShare.None)) // { // Action<Stream> d = delegate(Stream stream) // { // var buffer = new byte[2048]; // // ReSharper disable AccessToDisposedClosure // var readLen = fs.Read(buffer, 0, buffer.Length); // var totalTransmittedCount = 0L; // var aAitem = addon as Addon; // try // { // while (readLen > 0) // { // stream.Write(buffer, 0, readLen); // stream.Flush(); // totalTransmittedCount += readLen; // if (aAitem != null) // { // var progress = Convert.ToInt32(totalTransmittedCount * 100L / fs.Length); // aAitem.ProgressValue = progress > 0 ? progress : 1; // } // if (totalTransmittedCount == fs.Length) // break; // readLen = fs.Read(buffer, 0, buffer.Length); // } // } // finally // { // if (aAitem != null) // aAitem.ProgressValue = 0; // } // // ReSharper restore AccessToDisposedClosure // }; // var headerFileParam = new FileParameter // { // ContentLength = fs.Length, // Name = hash, // FileName = addon.Name, // //ContentType = "application/zip", // //Writer = delegate(Stream stream) { fs.CopyTo(stream); } // }; // headerFileParam.Writer = d; // request.Files.Add(headerFileParam); // //request.AlwaysMultipartFormData = false; // request.AddParameter("hash", hash, ParameterType.GetOrPost); // request.AddParameter("ACCEPT", "text/plain", ParameterType.HttpHeader); // request = request.htua(_offset); // var restResult = ExecuteRequest(request); // if (restResult.StatusCode == HttpStatusCode.OK) // { // addon.IsEasyInstallable = true; // // ReSharper disable once RedundantJumpStatement // return; // } // } // } // finally // { // File.Delete(zipFilePath); // } // } //} //catch (Exception exception) //{ // Debug.WriteLine(exception); //} }
/// <summary> /// Adds or update addon in <see cref="AddonBag"/>. /// </summary> /// <param name="addonBag">The <see cref="AddonBag"/>.</param> /// <param name="addon">The addon to be added or updated with by key.</param> /// <exception cref="NullReferenceException"><paramref name="addon"/> is <see langword="null"/></exception> public static void AddOrUpdate(this AddonBag addonBag, IAddon addon) { addon.VerifyNotNull(nameof(addon)); addonBag.AddOrUpdate(addon); }
internal void Add(IAddon addon) { Debug.Assert(addon != null); _addons.Add(addon); }