using System; using System.Collections.Generic; using System.Linq; using System.Text; using ScrewTurn.Wiki.PluginFramework; using System.Net; using System.IO; namespace ScrewTurn.Wiki.Plugins.PluginPack { /// /// Plugin with the sole purpose of removing PluginPack.dll in favor of separate DLLs. /// public class Updater : IFormatterProviderV30 { private static bool AlreadyRun = false; private static readonly ComponentInformation _info = new ComponentInformation("Updater Plugin", "Threeplicate Srl", "3.0.2.538", "http://www.screwturn.eu", null); /// /// Initializes the Storage Provider. /// /// The Host of the Component. /// The Configuration data, if any. /// If or are null. /// If is not valid or is incorrect. public void Init(IHostV30 host, string config) { if(host == null) throw new ArgumentNullException("host"); if(config == null) throw new ArgumentNullException("config"); if(AlreadyRun) return; // 1. Delete PluginPack.dll // 2. Download all other DLLs string root = "http://www.screwturn.eu/Version/PluginPack/"; string[] dllNames = new string[] { "DownloadCounterPlugin.dll", "FootnotesPlugin.dll", "MultilanguageContentPlugin.dll", "RssFeedDisplayPlugin.dll", "UnfuddleTicketsPlugin.dll" }; string[] providerNames = new string[] { "ScrewTurn.Wiki.Plugins.PluginPack.DownloadCounter", "ScrewTurn.Wiki.Plugins.PluginPack.Footnotes", "ScrewTurn.Wiki.Plugins.PluginPack.MultilanguageContentPlugin", "ScrewTurn.Wiki.Plugins.PluginPack.RssFeedDisplay", "ScrewTurn.Wiki.Plugins.PluginPack.UnfuddleTickets", }; Dictionary assemblies = new Dictionary(dllNames.Length); try { foreach(string dll in dllNames) { host.LogEntry("Downloading " + dll, LogEntryType.General, null, this); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(root + dll); req.AllowAutoRedirect = true; HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if(resp.StatusCode == HttpStatusCode.OK) { BinaryReader reader = new BinaryReader(resp.GetResponseStream()); byte[] content = reader.ReadBytes((int)resp.ContentLength); reader.Close(); assemblies.Add(dll, content); } else { throw new InvalidOperationException("Response status code for " + dll + ":" + resp.StatusCode.ToString()); } } foreach(string dll in dllNames) { host.GetSettingsStorageProvider().StorePluginAssembly(dll, assemblies[dll]); } foreach(string dll in dllNames) { LoadProvider(dll); } host.GetSettingsStorageProvider().DeletePluginAssembly("PluginPack.dll"); AlreadyRun = true; } catch(Exception ex) { host.LogEntry("Error occurred during automatic DLL updating with Updater Plugin\n" + ex.ToString(), LogEntryType.Error, null, this); } } private void LoadProvider(string dll) { Type loader = Type.GetType("ScrewTurn.Wiki.ProviderLoader, ScrewTurn.Wiki.Core"); var method = loader.GetMethod("LoadFromAuto"); method.Invoke(null, new[] { dll }); } /// /// Method invoked on shutdown. /// /// This method might not be invoked in some cases. public void Shutdown() { } /// /// Specifies whether or not to execute Phase 1. /// public bool PerformPhase1 { get { return false; } } /// /// Specifies whether or not to execute Phase 2. /// public bool PerformPhase2 { get { return false; } } /// /// Specifies whether or not to execute Phase 3. /// public bool PerformPhase3 { get { return false; } } /// /// Gets the execution priority of the provider (0 lowest, 100 highest). /// public int ExecutionPriority { get { return 50; } } /// /// Performs a Formatting phase. /// /// The raw content to Format. /// The Context information. /// The Phase. /// The Formatted content. public string Format(string raw, ContextInformation context, FormattingPhase phase) { return raw; } /// /// Prepares the title of an item for display (always during phase 3). /// /// The input title. /// The context information. /// The prepared title (no markup allowed). public string PrepareTitle(string title, ContextInformation context) { return title; } /// /// Gets the Information about the Provider. /// public ComponentInformation Information { get { return _info; } } /// /// Gets a brief summary of the configuration string format, in HTML. Returns null if no configuration is needed. /// public string ConfigHelpHtml { get { return null; } } } }