Exemplo n.º 1
0
        public static bool TryLoad <T>(FileDescriptor fileName, ProgressReporter progress, out T result, T @default, ObjectSerialiser serialiser)
        {
            if (!File.Exists(fileName))
            {
                result = @default;
                return(false);
            }

            try
            {
                using (FileStream sr = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    if (progress != null)
                    {
                        using (ProgressStream ps = new ProgressStream(sr, progress))
                        {
                            result = _Deserialise <T>(ps, fileName.Format, serialiser, progress);
                        }
                    }
                    else
                    {
                        result = _Deserialise <T>(sr, fileName.Format, serialiser, progress);
                    }
                }

                return(true);
            }
            catch
            {
                result = @default;
                return(false);
            }
        }
Exemplo n.º 2
0
        public PathwayToolsReader(string fn, ProgressReporter prog)
        {
            _fn = fn;
            FileStream     fs = File.OpenRead(fn);
            ProgressStream ps = new ProgressStream(fs, prog);

            _sr = new StreamReader(ps);
        }
Exemplo n.º 3
0
        public static T LoadAndResave <T>(FileDescriptor fileName, ProgressReporter prog, ObjectSerialiser serialiser)
            where T : new()
        {
            T result;

            if (!TryLoad <T>(fileName, prog, out result, default(T), serialiser))
            {
                result = new T();
            }

            Save <T>(fileName, result, null, prog);

            return(result);
        }
Exemplo n.º 4
0
 public ProgressStream(Stream stream, ProgressReporter progressHandler)
 {
     _stream          = stream;
     _progressHandler = progressHandler;
 }
Exemplo n.º 5
0
            private bool _disposedValue = false; // To detect redundant calls

            public ProgSection(ProgressReporter owner)
            {
                _owner = owner;
            }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a ProgressParallelHandler.
 /// </summary>
 /// <param name="progressReporter">Target to report to</param>
 /// <param name="count">How many operations will take place.</param>
 public ProgressParallelHandler(ProgressReporter progressReporter, int count)
 {
     this._progressReporter = progressReporter;
     this._count            = count;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Saves or loads settings depending on the <paramref name="save"/> parameter.
        /// When loading for the first time (i.e. if the default value is used) the file will also be saved.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="save"></param>
        /// <param name="fileName"></param>
        /// <param name="srcDest"></param>
        /// <param name="serialiser"></param>
        /// <param name="prog"></param>
        public static void SaveLoad <T>(bool save, FileDescriptor fileName, ref T srcDest, ObjectSerialiser serialiser, ProgressReporter prog)
        {
            if (save)
            {
                Save(fileName, srcDest, serialiser, prog);
            }
            else
            {
                T result;

                if (TryLoad(fileName, prog, out result, srcDest, serialiser))
                {
                    srcDest = result;
                    return;
                }

                if (!File.Exists(fileName))
                {
                    Save(fileName, srcDest, serialiser, prog);
                }
            }
        }
Exemplo n.º 8
0
        public static T LoadOrDefault <T>(FileDescriptor fileName, T @default, ObjectSerialiser serialiser, ProgressReporter prog)
        {
            T result;

            TryLoad(fileName, prog, out result, @default, serialiser);
            return(result);
        }
Exemplo n.º 9
0
        private static T _Deserialise <T>(Stream s, SerialisationFormat format, ObjectSerialiser serialiser, ProgressReporter prog)
        {
            switch (format)
            {
            case SerialisationFormat.MSerialiserBinary:
                return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Binary, new[] { serialiser }, null));

            case SerialisationFormat.MSerialiserText:
                return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Text, new[] { serialiser }, null));

            case SerialisationFormat.MSerialiserCompactBinary:
                return(MSerialiser.DeserialiseStream <T>(s, ETransmission.CompactBinary, new[] { serialiser }, null));

            case SerialisationFormat.MSerialiserFastBinary:
                return(MSerialiser.DeserialiseStream <T>(s, ETransmission.FastBinary, new[] { serialiser }, null));

            case SerialisationFormat.Xml:
                var xs = _CreateXmlSerialiser <T>();
                return((T)xs.Deserialize(s));

            case SerialisationFormat.DataContract:
                var dcs = _CreateDataContactSerialiser <T>();
                return((T)dcs.ReadObject(s));

            case SerialisationFormat.MsnrbfBinary:
                var bcs = new BinaryFormatter();
                // bcs.Binder = new TypeNameConverter(); - We don't do this anymore, backwards compatibility ends up taking most of the development time.
                return((T)bcs.Deserialize(s));

            case SerialisationFormat.Ini:
                return(IniSerialiser.Deserialise <T>(s));

            default:
                throw new InvalidOperationException("Invalid switch: " + format);
            }
        }
Exemplo n.º 10
0
        private static void _Serialise <T>(T data, Stream s, SerialisationFormat format, ObjectSerialiser serialiser, ProgressReporter prog)
        {
            switch (format)
            {
            case SerialisationFormat.MSerialiserBinary:
                MSerialiser.SerialiseStream(s, data, ETransmission.Binary, new[] { serialiser }, null);
                return;

            case SerialisationFormat.MSerialiserText:
                MSerialiser.SerialiseStream(s, data, ETransmission.Text, new[] { serialiser }, null);
                return;

            case SerialisationFormat.MSerialiserCompactBinary:
                MSerialiser.SerialiseStream(s, data, ETransmission.CompactBinary, new[] { serialiser }, null);
                return;

            case SerialisationFormat.MSerialiserFastBinary:
                MSerialiser.SerialiseStream(s, data, ETransmission.FastBinary, new[] { serialiser }, null);
                return;

            case SerialisationFormat.Xml:
                var xs = _CreateXmlSerialiser <T>();
                xs.Serialize(s, data);
                break;

            case SerialisationFormat.DataContract:
                var dcs = _CreateDataContactSerialiser <T>();
                dcs.WriteObject(s, (object)data);
                break;

            case SerialisationFormat.MsnrbfBinary:
                var bcs = new BinaryFormatter();
                bcs.Serialize(s, data);
                break;

            case SerialisationFormat.Ini:
                IniSerialiser.Serialise(s, data);
                break;

            default:
                throw new InvalidOperationException("Invalid switch: " + format);
            }
        }
Exemplo n.º 11
0
        public static void Save <T>(FileDescriptor fileName, T data, ObjectSerialiser serialiser, ProgressReporter prog)
        {
            // Because we move files we can write over readonly files, so prevent this here
            if (File.Exists(fileName) && new FileInfo(fileName).IsReadOnly)
            {
                throw new InvalidOperationException($"Will not overwrite file because it is marked as readonly. Filename: {fileName}");
            }

            // Can't cancel halfway through writing a file
            prog.DisableThrowOnCancel();

            try
            {
                // Create backup
                // Need to check main settings is initialised because this function is used when loading the main settings themselves
                if (MainSettings.Instance != null && MainSettings.Instance.General.AutoBackup)
                {
                    if (File.Exists(fileName))
                    {
                        string bakFile = fileName + ".bak";

                        if (File.Exists(bakFile))
                        {
                            File.Delete(bakFile);
                        }

                        File.Move(fileName, bakFile);
                    }
                }

                if (data == null)
                {
                    // Special case for deleting files
                    File.Delete(fileName);
                    return;
                }

                // Save to a temporary file (in case we get an error we don't want to destroy the original by saving over it with a corrupt copy)
                string tempFile = fileName + ".tmp";

                try
                {
                    using (Stream sw = new FileStream(tempFile, FileMode.Create, FileAccess.Write))
                    {
                        _Serialise <T>(data, sw, fileName.Format, serialiser, prog);
                    }
                }
                catch
                {
                    File.Delete(tempFile);
                    throw;
                }

                // Move the temp file to the new location
                File.Delete(fileName);
                File.Move(tempFile, fileName);
            }
            finally
            {
                prog.ReenableThrowOnCancel();
            }
        }