예제 #1
0
        /// <summary>
        /// Attempts to write data to a local file.
        /// </summary>
        public BvException TryWrite(string data)
        {
            BvException exception = null;
            TextWriter  writer    = null;

            lock (fileLock)
            {
                try
                {
                    writer = MyAPIGateway.Utilities.WriteFileInLocalStorage(file, typeof(LocalFileIO));
                    writer.WriteLine(data);
                    writer.Flush();
                }
                catch (Exception e)
                {
                    exception = new BvException($"IO Error. Unable to write to {file}.", e);
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }

            return(exception);
        }
예제 #2
0
        /// <summary>
        /// Saves a given configuration to the save file in parallel.
        /// </summary>
        public void SaveStart(ConfigData cfg, bool silent = false)
        {
            if (!SaveInProgress)
            {
                if (!silent)
                {
                    Main.SendChatMessage("Saving configuration...");
                }
                SaveInProgress = true;

                taskPool.EnqueueTask(() =>
                {
                    cfg.Validate();
                    BvException exception = TrySave(cfg);

                    if (exception != null)
                    {
                        taskPool.EnqueueAction(() => SaveFinish(false, silent));
                        throw exception;
                    }
                    else
                    {
                        taskPool.EnqueueAction(() => SaveFinish(true, silent));
                    }
                });
            }
            else
            {
                Main.SendChatMessage("Save operation already in progress.");
            }
        }
예제 #3
0
        /// <summary>
        /// Attempts to synchronously update log with message and adds a time stamp.
        /// </summary>
        public bool TryWriteToLog(string message)
        {
            if (Accessible)
            {
                message = $"[{DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:ms")}] {message}";
                BvException exception = logFile.TryAppend(message);

                if (exception != null)
                {
                    Main.SendChatMessage("Unable to update log; please check your file access permissions.");
                    Accessible = false;
                    throw exception;
                }
                else
                {
                    Main.SendChatMessage("Log updated.");
                    Accessible = true;
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }
예제 #4
0
        /// <summary>
        /// Attempts to retrieve local file data.
        /// </summary>
        public BvException TryRead(out string data)
        {
            BvException exception = null;
            TextReader  reader    = null;

            data = null;

            lock (fileLock)
            {
                try
                {
                    reader = MyAPIGateway.Utilities.ReadFileInLocalStorage(file, typeof(LocalFileIO));
                    data   = reader.ReadToEnd();
                }
                catch (Exception e)
                {
                    data      = null;
                    exception = new BvException($"IO Error. Unable to read from {file}.", e);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }

            return(exception);
        }
예제 #5
0
        /// <summary>
        /// Attempts to save current configuration to a file.
        /// </summary>
        private BvException TrySave(ConfigData cfg)
        {
            string      xmlOut    = null;
            BvException exception = TrySerializeToXml(cfg, out xmlOut);

            if (exception == null && xmlOut != null)
            {
                exception = cfgFile.TryWrite(xmlOut);
            }

            return(exception);
        }
예제 #6
0
        /// <summary>
        /// Creates a duplicate of the config file starting with a new file name starting with "old_"
        /// if one exists.
        /// </summary>
        private void Backup()
        {
            if (MyAPIGateway.Utilities.FileExistsInLocalStorage(cfgFile.file, typeof(ConfigIO)))
            {
                BvException exception = cfgFile.TryDuplicate($"old_" + cfgFile.file);

                if (exception != null)
                {
                    throw exception;
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Saves the current configuration synchronously.
        /// </summary>
        public void Save(ConfigData cfg)
        {
            if (!SaveInProgress)
            {
                cfg.Validate();
                BvException exception = TrySave(cfg);

                if (exception != null)
                {
                    throw exception;
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Creates a local duplicate of a file with a given name.
        /// </summary>
        public BvException TryDuplicate(string newName)
        {
            string      data;
            BvException exception = TryRead(out data);
            LocalFileIO newFile;

            if (exception == null && data != null)
            {
                newFile   = new LocalFileIO(newName);
                exception = newFile.TryWrite(data);
            }

            return(exception);
        }
예제 #9
0
        /// <summary>
        /// Attempts to deserialize an Xml string to an object of a given type.
        /// </summary>
        private static BvException TryDeserializeXml <T>(string xmlIn, out T obj)
        {
            BvException exception = null;

            obj = default(T);

            try
            {
                obj = MyAPIGateway.Utilities.SerializeFromXML <T>(xmlIn);
            }
            catch (Exception e)
            {
                exception = new BvException("IO Error. Unable to interpret XML.", e);
            }

            return(exception);
        }
예제 #10
0
        /// <summary>
        /// Attempts to serialize an object to an Xml string.
        /// </summary>
        private static BvException TrySerializeToXml <T>(T obj, out string xmlOut)
        {
            BvException exception = null;

            xmlOut = null;

            try
            {
                xmlOut = MyAPIGateway.Utilities.SerializeToXML(obj);
            }
            catch (Exception e)
            {
                exception = new BvException("IO Error. Failed to generate Xml.", e);
            }

            return(exception);
        }
예제 #11
0
        /// <summary>
        /// Attempts to append string to an existing local file.
        /// </summary>
        public BvException TryAppend(string data)
        {
            string      current;
            BvException exception = TryRead(out current);

            if (exception == null && current != null)
            {
                current  += data;
                exception = TryWrite(current);
            }
            else
            {
                exception = TryWrite(data);
            }

            return(exception);
        }
예제 #12
0
        /// <summary>
        /// Checks a task queue for invalid tasks and tasks with exceptions, logs and throws exceptions
        /// as needed and rebuilds queue with remaining valid tasks.
        /// </summary>
        private void UpdateRunningTasks()
        {
            Task                 task;
            Queue <Task>         currentTasks      = new Queue <Task>();
            List <Exception>     taskExceptions    = new List <Exception>(); //unknown exceptions
            List <BvException>   knownExceptions   = new List <BvException>();
            BvException          bvException       = null;
            BvAggregateException unknownExceptions = null;

            while (tasksRunning.Count > 0)
            {
                if (tasksRunning.TryDequeue(out task) && task.valid)
                {
                    if (task.Exceptions != null)
                    {
                        if (task.Exceptions.Length > 0)
                        {
                            foreach (Exception exception in task.Exceptions)
                            {
                                if (TryGetBvException(exception, out bvException))
                                {
                                    knownExceptions.Add(bvException);
                                }
                                else
                                {
                                    taskExceptions.Add(exception);
                                }
                            }
                        }
                    }
                    else if (!task.IsComplete)
                    {
                        currentTasks.Enqueue(task);
                    }
                }
            }

            tasksRunning = currentTasks;

            if (taskExceptions.Count > 0)
            {
                unknownExceptions = new BvAggregateException(taskExceptions);
            }

            errorCallback(knownExceptions, unknownExceptions);
        }
예제 #13
0
        /// <summary>
        /// Attempts to update log in parallel with message and adds a time stamp.
        /// </summary>
        public void TryWriteToLogStart(string message)
        {
            if (Accessible)
            {
                message = $"[{DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:ms")}] {message}";

                taskPool.EnqueueTask(() =>
                {
                    BvException exception = logFile.TryAppend(message);

                    if (exception != null)
                    {
                        taskPool.EnqueueAction(() => TryWriteToLogFinish(false));
                        throw exception;
                    }
                    else
                    {
                        taskPool.EnqueueAction(() => TryWriteToLogFinish(true));
                    }
                });
            }
        }
예제 #14
0
        /// <summary>
        /// Attempts to load config file and creates a new one if it can't.
        /// </summary>
        private BvException TryLoad(out ConfigData cfg)
        {
            string      data;
            BvException exception = cfgFile.TryRead(out data);

            cfg = null;

            if (exception != null || data == null)
            {
                return(exception);
            }
            else
            {
                exception = TryDeserializeXml(data, out cfg);
            }

            if (exception != null)
            {
                Backup();
                TrySave(ConfigData.Defaults);
            }

            return(exception);
        }
예제 #15
0
 /// <summary>
 /// Attempts to cast an Exception as BvException and returns true if successful.
 /// </summary>
 private static bool TryGetBvException(Exception exception, out BvException bvException)
 {
     bvException = exception as BvException;
     return(bvException != null);
 }