Пример #1
0
        /// <summary>Loads the given config into an object from the xml file in the config folder</summary>
        /// <typeparam name="T">The object type to be returned and loaded</typeparam>
        /// <param name="Filename">The name of the config</param>
        /// <returns>Loads the given config into an object from the xml file in the config folder</returns>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="TypeLoadException" />
        public static T LoadConfig <T>(String Filename)
        {
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ".xml";
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);
            T Out = default;

            if (File.Exists(Filepath))
            {
                IConfigDeserializer <T> deserializer;
                FileStream reader = null;

                Lock.WaitOne();
#if !DEBUG
                try {
#endif
                deserializer = ConfigLoader._SerializerFactory.GetDeserializer <T>();
                reader       = new FileStream(Filepath, FileMode.Open);

                Out = deserializer.Deserialize(reader);
                reader.Close();
#if !DEBUG
            }
            catch (Exception ex) {
                if (reader != null)
                {
                    reader.Close();
                }

                ErrorLoading(Filepath, ex);
            }
#endif
                Lock.Set();
            }

            if (Out == null)
            {
                Out = Activator.CreateInstance <T>();

                if (Out.GetType().GetInterface(nameof(INewConfig)) != null)
                {
                    var Temp = (INewConfig)Out;
                    Temp.SetNewInformation();
                }

#if Debug
                Task.Run(() => SaveConfig(Out, Filename));
#else
                SaveConfig(Out, Filename);
#endif
            }

            return(Out);
        }
Пример #2
0
        /// <summary>Save the given object into a file</summary>
        /// <param name="ConfigObject">The object to save</param>
        /// <param name="Filename">The filename the objects should get</param>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="IOException" />
        /// <exception cref="InvalidOperationException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="System.Security.SecurityException" />
        public static void SaveConfig(Object ConfigObject, String Filename)
        {
            //Get file info on the file to save to
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ConfigOptions.ConfigExtension;
            var             FI       = new FileInfo(Filepath);
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);

            //Check parent directory
            DirectoryInfo DI = FI.Directory;

            //Create if missing
            if (!DI.Exists)
            {
                DI.Create();
            }

            //Setup
            IConfigSerializer <Object> serializer = null;
            FileStream writer = null;

            Lock.WaitOne();
#if !DEBUG
            try {
#endif
            //Assign serializer and stream
            serializer = ConfigLoader._SerializerFactory.GetSerializer(ConfigObject.GetType());
            writer     = new FileStream(FI.FullName, FileMode.Create);

            //Serialize object
            serializer.Serialize(ConfigObject, writer);

            //Flush and close
            writer.Flush();
            writer.Close();
#if !DEBUG
        }
#pragma warning disable 168
        catch (Exception ex) {
#pragma warning restore 168
            //Close stream if open
            if (writer != null)
            {
                writer.Close();
            }

            ErrorSaving(Filepath, ex);
        }
#endif

            Lock.Set();
        }
Пример #3
0
        /// <summary>Loads the specified object from file storage or creates a new instance</summary>
        /// <param name="T">The type of the object to retrieve</param>
        /// <param name="Filename">the name of the config</param>
        /// <returns>A <see cref="Object"/> is returned</returns>
        /// <exception cref="ArgumentException" />
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentOutOfRangeException" />
        /// <exception cref="COMException" />
        /// <exception cref="DirectoryNotFoundException" />
        /// <exception cref="FileNotFoundException" />
        /// <exception cref="InvalidComObjectException" />
        /// <exception cref="IOException" />
        /// <exception cref="MethodAccessException" />
        /// <exception cref="MemberAccessException" />
        /// <exception cref="MissingMethodException" />
        /// <exception cref="NotSupportedException" />
        /// <exception cref="PathTooLongException" />
        /// <exception cref="SecurityException" />
        /// <exception cref="UnauthorizedAccessException" />
        /// <exception cref="TargetInvocationException" />
        /// <exception cref="TypeLoadException" />
        public static Object LoadConfig(Type T, String Filename)
        {
            //Create the filepath of where the config file could be found
            String          Filepath = ConfigOptions.ConfigFolder + Filename + ConfigOptions.ConfigExtension;
            EventWaitHandle Lock     = ConfigLoader.GetLock(Filepath);
            Object          Out      = null;

            //Check if file exists
            if (File.Exists(Filepath))
            {
                //Setup
                IConfigDeserializer <Object> deserializer;
                FileStream reader = null;

                Lock.WaitOne();
#if !DEBUG
                try {
#endif
                //Create the deserializer and stream
                deserializer = ConfigLoader._SerializerFactory.GetDeserializer(T);
                reader       = new FileStream(Filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Deserialize file into object
                Out = deserializer.Deserialize(reader);
                reader.Close();
#if !DEBUG
            }
            catch (Exception ex) {
                //Close of the stream, write error report
                if (reader != null)
                {
                    reader.Close();
                }

                ErrorLoading(Filepath, ex);
            }
#endif
                Lock.Set();
            }

            //Checks if object has not been succesfully been deserialized.
            if (Out == null)
            {
                //Create new instance of the object
                Out = Activator.CreateInstance(T);

                //Check if the object has the given object
                if (Out.GetType().GetInterface(nameof(INewConfig)) != null)
                {
                    var Temp = (INewConfig)Out;
                    Temp.SetNewInformation();
                }

#if !DEBUG
                Task.Run(() => SaveConfig(Out, Filename));
#else
                SaveConfig(Out, Filename);
#endif
            }

            return(Out);
        }