コード例 #1
0
    /// <summary>
    /// Sets the callback function of the BackgroundWorker.
    /// </summary>
    /// <param name="task">The callback function that contains the task that should be performed asynchronously.</param>
    /// <param name="finished">The callback function that should be called with the result of the task function.</param>
    /// <param name="progressChanged">The callback function for the progress changed handling.</param>
    /// <param name="supportCancellation">Flag to determine if the asynchronous task could be canceled.</param>
    public void SetCallbackFunctions(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false)
    {
        if (task == null)
        {
            throw new ArgumentNullException();
        }

        mResultCall          = finished;
        mRunCall             = task;
        mProgressChangedCall = progressChanged;
        mException           = null;

        mWorker = new BackgroundWorker();
        mWorker.WorkerSupportsCancellation = supportCancellation;

        if (mResultCall != null)
        {
            mWorker.DoWork += new DoWorkEventHandler(Run);
        }
        if (mResultCall != null)
        {
            mWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Finish);
        }

        if (progressChanged != null)
        {
            mWorker.WorkerReportsProgress = true;
            mWorker.ProgressChanged      += new ProgressChangedEventHandler(ProgressChanged);
        }
    }
コード例 #2
0
    /// <summary>
    /// Sets the download callback function of the BackgroundWorker.
    /// </summary>
    /// <param name="url">Download link</param>
    /// <param name="downloadPath">Path and filename to download to.</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public void SetDownloadCallbackFunctions(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged = null)
    {
        mURL                 = url;
        mDownloadPath        = downloadPath;
        mDownloadFinished    = finished;
        mProgressChangedCall = progressChanged;
        mException           = null;

        mWebClient             = new WebClient();
        mWebClient.Credentials = CredentialCache.DefaultCredentials;

        if (finished != null)
        {
            mWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished);
        }

        if (progressChanged != null)
        {
            mWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
        }
    }
コード例 #3
0
        /// <summary>
        /// Starts a async download of a mod from KSP Spaceport.
        /// </summary>
        /// <param name="modInfo"></param>
        /// <param name="finished"></param>
        /// <param name="progressChanged"></param>
        public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler<bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
        {
            // get save path 
            int start = downloadURL.LastIndexOf("/") + 1;
            string filename = downloadURL.Substring(start, downloadURL.Length - start);
            modInfo.LocalPath = Path.Combine(downloadURL, filename);

            AsyncTask<bool> asyncJob = new AsyncTask<bool>();
            asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged);
            asyncJob.RunDownload(); ;
        }
コード例 #4
0
        /// <summary>
        /// Starts a async download of a mod from KSP Spaceport.
        /// </summary>
        /// <param name="modInfo"></param>
        /// <param name="finished"></param>
        /// <param name="progressChanged"></param>
        public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
        {
            // get save path
            int    start    = downloadURL.LastIndexOf("/") + 1;
            string filename = downloadURL.Substring(start, downloadURL.Length - start);

            modInfo.LocalPath = Path.Combine(downloadURL, filename);

            AsyncTask <bool> asyncJob = new AsyncTask <bool>();

            asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged);
            asyncJob.RunDownload();;
        }
コード例 #5
0
 /// <summary>
 /// Instantiates the AsyncTask class for a async download.
 /// </summary>
 /// <param name="url">Download link</param>
 /// <param name="downloadPath">Path and filename to download to.</param>
 /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
 /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
 public AsyncTask(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged)
 {
     SetDownloadCallbackFunctions(url, downloadPath, finished, progressChanged);
 }
コード例 #6
0
    /// <summary>
    /// Starts a asynchrony download.
    /// </summary>
    /// <param name="url">Download link</param>
    /// <param name="downloadPath">Path and filename to download to.</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;bool&gt;(bool result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public static void RunDownload(string url, string downloadPath, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null)
    {
        AsyncTask <bool> asyncTask = new AsyncTask <bool>(url, downloadPath, finished, progressChanged);

        asyncTask.RunDownload();
    }
コード例 #7
0
 /// <summary>
 /// Instantiates the AsyncTask class
 /// </summary>
 /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName&lt;T_RETURN_VALUE&gt;()")</param>
 /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;T_RETURN_VALUE&gt;(T_RETURN_VALUE result, Exception ex)")</param>
 /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
 /// <param name="supportCancellation">Flag to determine if cancellation is needed.</param>
 public AsyncTask(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false)
 {
     SetCallbackFunctions(task, finished, progressChanged, supportCancellation);
 }
コード例 #8
0
        /// <summary>
        /// Processes the passed node. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="node">The node to process.</param>
        /// <param name="processedNodeCount">For internal use only!</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        private static void ProcessNode(ModNode node, ref int processedNodeCount, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null)
        {
            if (node.Checked)
            {
                if (!File.Exists(node.ZipRoot.Key))
                {
                    if (!silent)
                    {
                        Messenger.AddInfo(string.Format(Messages.MSG_CANT_INSTALL_MODNODE_0_ZIP_MISSING, node.Destination));
                    }
                }
                else
                {
                    if (!node.IsFile)
                    {
                        CreateDirectory(node, silent);
                    }
                    else
                    {
                        ExtractFile(node, node.Destination, silent, overrideOn);
                    }
                }
            }
            else
            {
                if (!node.IsFile)
                {
                    RemoveDirectory(node, silent);
                }
                else
                {
                    RemoveFile(node, silent);
                }
            }

            if (progressChanged != null)
            {
                progressChanged(processedNodeCount += 1);
            }

            foreach (ModNode child in node.Nodes)
            {
                ProcessNode(child, ref processedNodeCount, silent, overrideOn, progressChanged);
            }
        }
コード例 #9
0
    /// <summary>
    /// This function calls the passed task function asynchronously.
    /// </summary>
    /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName&lt;T_RETURN_VALUE&gt;()")</param>
    /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName&lt;T_RETURN_VALUE&gt;(T_RETURN_VALUE result, Exception ex)")</param>
    /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param>
    public static void DoWork(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished = null, AsyncProgressChangedHandler progressChanged = null)
    {
        AsyncTask <T_RETURN_VALUE> asyncTask = new AsyncTask <T_RETURN_VALUE>(task, finished, progressChanged);

        asyncTask.Run();
    }
コード例 #10
0
        /// <summary>
        /// Processes all passed nodes. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="modArray">The NodeArray to process.</param>
        /// <param name="processedNodeCount">For internal use only!</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        /// <returns>Count of processed nodes.</returns>
        private static int ProcessNodes(ModNode[] modArray, ref int processedNodeCount, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null)
        {
            foreach (ModNode node in modArray)
            {
                if (node.HasDestination)
                {
                    if (!silent)
                    {
                        Messenger.AddInfo(string.Format(Messages.MSG_ROOT_IDENTIFIED, node.Text));
                    }

                    ProcessNode(node, ref processedNodeCount, silent, overrideOn, progressChanged);
                }
                else if (node.HasDestinationForChilds)
                {
                    if (progressChanged != null)
                    {
                        progressChanged(processedNodeCount += 1);
                    }

                    ModNode[] nodes = new ModNode[node.Nodes.Count];
                    for (int i = 0; i < node.Nodes.Count; ++i)
                    {
                        nodes[i] = (ModNode)node.Nodes[i];
                    }

                    ProcessNodes(nodes, ref processedNodeCount, silent, overrideOn, progressChanged);
                }
            }

            return(processedNodeCount);
        }
コード例 #11
0
        /// <summary>
        /// Processes all passed nodes. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="mod">The mod to process.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        /// <returns>Count of processed nodes.</returns>
        public static int ProcessMod(ModNode mod, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null, int processedNodeCount = 0)
        {
            if (!silent)
            {
                Messenger.AddInfo(Constants.SEPARATOR);
                Messenger.AddInfo(string.Format(Messages.MSG_START_PROCESSING_0, mod.Name));
                Messenger.AddInfo(Constants.SEPARATOR);
            }

            int processedNode = processedNodeCount;

            try
            {
                processedNode = ProcessNodes(new ModNode[] { mod }, ref processedNode, silent, overrideOn, progressChanged);
            }
            catch (Exception ex)
            {
                Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_PROCESSING_MOD_0, mod.Name), ex);
            }

            DeleteNotProcessedDirectorys(silent);

            if (!silent)
            {
                Messenger.AddInfo(Constants.SEPARATOR);
            }

            return(processedNode);
        }
コード例 #12
0
        /// <summary>
        /// Processes the passed node. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="node">The node to process.</param>
        /// <param name="processedNodeCount">For internal use only!</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        private static void ProcessNode(ModNode node, ref int processedNodeCount, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null)
        {
            if (node.Checked)
            {
                if (!File.Exists(node.ZipRoot.Key))
                {
                    if (!silent)
                        Messenger.AddInfo(string.Format(Messages.MSG_CANT_INSTALL_MODNODE_0_ZIP_MISSING, node.Destination));
                }
                else
                { 
                    if (!node.IsFile)
                        CreateDirectory(node, silent);
                    else
                        ExtractFile(node, node.Destination, silent, overrideOn);
                }
            }
            else
            {
                if (!node.IsFile)
                    RemoveDirectory(node, silent);
                else
                    RemoveFile(node, silent);
            }

            if (progressChanged != null)
                progressChanged(processedNodeCount += 1);

            foreach (ModNode child in node.Nodes)
                ProcessNode(child, ref processedNodeCount, silent, overrideOn, progressChanged);
        }
コード例 #13
0
        /// <summary>
        /// Processes all passed nodes. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="modArray">The NodeArray to process.</param>
        /// <param name="processedNodeCount">For internal use only!</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        /// <returns>Count of processed nodes.</returns>
        private static int ProcessNodes(ModNode[] modArray, ref int processedNodeCount, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null)
        {
            foreach (ModNode node in modArray)
            {
                if (node.HasDestination)
                {
                    if (!silent)
                        Messenger.AddInfo(string.Format(Messages.MSG_ROOT_IDENTIFIED, node.Text));

                    ProcessNode(node, ref processedNodeCount, silent, overrideOn, progressChanged);
                }
                else if (node.HasDestinationForChilds)
                {
                    if (progressChanged != null)
                        progressChanged(processedNodeCount += 1);

                    ModNode[] nodes = new ModNode[node.Nodes.Count];
                    for (int i = 0; i < node.Nodes.Count; ++i)
                        nodes[i] = (ModNode)node.Nodes[i];

                    ProcessNodes(nodes, ref processedNodeCount, silent, overrideOn, progressChanged);
                }
            }

            return processedNodeCount;
        }
コード例 #14
0
        /// <summary>
        /// Processes all passed nodes. (Adds/Removes the MOD to/from the KSP install folders).
        /// </summary>
        /// <param name="mod">The mod to process.</param>
        /// <param name="silent">Determines if info messages should be added displayed.</param>
        /// <param name="overrideOn">Flag to set override of files on.</param>
        /// <param name="progressChanged">Callback function when the progress of the processing changed.</param>
        /// <returns>Count of processed nodes.</returns>
        public static int ProcessMod(ModNode mod, bool silent = false, bool overrideOn = false, AsyncProgressChangedHandler progressChanged = null, int processedNodeCount = 0)
        {
            if (!silent)
            {
                Messenger.AddInfo(Constants.SEPARATOR);
                Messenger.AddInfo(string.Format(Messages.MSG_START_PROCESSING_0, mod.Name));
                Messenger.AddInfo(Constants.SEPARATOR);
            }

            int processedNode = processedNodeCount;
            try
            {
                processedNode = ProcessNodes(new ModNode[] { mod }, ref processedNode, silent, overrideOn, progressChanged);
            }
            catch (Exception ex)
            {
                Messenger.AddError(string.Format(Messages.MSG_ERROR_DURING_PROCESSING_MOD_0, mod.Name), ex);
            }

            DeleteNotProcessedDirectorys(silent);

            if (!silent)
                Messenger.AddInfo(Constants.SEPARATOR);

            return processedNode;
        }