Exemplo n.º 1
0
        public object Deserialise()
        {
            int t = _Read();
            int u = 0;

            if (t > 100)
            {
                u = t;
                if (u <= obs.Count + 100)
                {
                    return(obs[u]);
                }
                t = _Read();
            }
            ObjectSerialiser os = (ObjectSerialiser)srs[(SerType)t];

            if (os != null)
            {
                if (u > 0)
                {
                    object r = obs[u] = os(null, null);             // allow for recursive structures: create and store first
                    return(obs[u] = os(r, this));                   // we need to do it again for strings
                }
                return(os(null, this));
            }
            else
            {
                throw new Exception("unknown type " + t);
            }
        }
Exemplo n.º 2
0
        /// <exclude/>
		public object Deserialise()
		{
			int t = _Read();
			int u = 0;
			if (t>100)
			{
				u = t;
				if (u<=obs.Count+100)
					return obs[u];
				t = _Read();
			} 
			ObjectSerialiser os = (ObjectSerialiser)srs[(SerType)t];
			if (os!=null)
			{
				if (u>0) 
				{ // ?? strange bug in mono: workaround in CSTools 4.5a leads to not chaining assignments here
					object r = os(null,null); // allow for recursive structures: create and store first
					obs[u] = r;
					r = os(r,this); // really deserialise it
					obs[u] = r;	// we need to store it again for strings
					return r;
				}
				return os(null,this); 
			}
			else
				throw new Exception("unknown type "+t);
		}
Exemplo n.º 3
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.º 4
0
        private static object InvokeGenericMember(object obj, string memberName, object[] parameters)
        {
            Type   ObjectSerialiser;
            object ReturnValue;

            ObjectSerialiser = GetGenericSerialiser(obj);
            ReturnValue      = ObjectSerialiser.InvokeMember(memberName, _BindingFlags, null, null, parameters);

            return(ReturnValue);
        }
Exemplo n.º 5
0
        public void Serialise(object o)
        {
            if (o == null)
            {
                _Write(SerType.Null);
                return;
            }
            if (o is Encoding)
            {
                _Write(SerType.Encoding);
                EncodingSerialise(o, this);
                return;
            }
            Type t = o.GetType();

            if (t.IsClass)
            {
                object p = obs[o];
                if (p != null)
                {
                    _Write((int)p);
                    return;
                }
                else
                {
                    int e = ++id;
                    _Write(e);
                    obs[o] = e;
                }
            }
            object so = tps[t];

            if (so != null)
            {
                SerType s = (SerType)so;
                _Write(s);
                ObjectSerialiser os = (ObjectSerialiser)srs[s];
                os(o, this);
            }
            else
            {
                throw new Exception("unknown type " + t.FullName);
            }
        }
        /// <summary>
        /// <see cref="Sif.Framework.Consumer.IGenericConsumer{T,PK}.Update(T)">Update</see>
        /// </summary>
        public virtual void Update(T obj)
        {
            if (!registrationService.Registered)
            {
                throw new InvalidOperationException("Consumer has not registered.");
            }

            string url  = EnvironmentUtils.ParseServiceUrl(environmentTemplate) + "/" + TypeName + "s" + "/" + obj.Id;
            string body = ObjectSerialiser.Serialise(obj);
            string xml  = HttpUtils.PutRequest(url, registrationService.AuthorisationToken, body);

            if (log.IsDebugEnabled)
            {
                log.Debug("XML from PUT request ...");
            }
            if (log.IsDebugEnabled)
            {
                log.Debug(xml);
            }
        }
        /// <summary>
        /// <see cref="Sif.Framework.Consumer.IGenericConsumer{T,PK}.Retrieve(PK)">Retrieve</see>
        /// </summary>
        public virtual T Retrieve(PK id)
        {
            if (!registrationService.Registered)
            {
                throw new InvalidOperationException("Consumer has not registered.");
            }

            string url = EnvironmentUtils.ParseServiceUrl(environmentTemplate) + "/" + TypeName + "s" + "/" + id;
            string xml = HttpUtils.GetRequest(url, registrationService.AuthorisationToken);

            if (log.IsDebugEnabled)
            {
                log.Debug("XML from GET request ...");
            }
            if (log.IsDebugEnabled)
            {
                log.Debug(xml);
            }
            return(ObjectSerialiser.Deserialise(xml));
        }
        /// <summary>
        /// <see cref="Sif.Framework.Consumer.IGenericConsumer{T,PK}.Retrieve(T)">Retrieve</see>
        /// </summary>
        public virtual ICollection <T> Retrieve(T obj)
        {
            if (!registrationService.Registered)
            {
                throw new InvalidOperationException("Consumer has not registered.");
            }

            string url  = EnvironmentUtils.ParseServiceUrl(environmentTemplate) + "/" + TypeName + "s";
            string body = ObjectSerialiser.Serialise(obj);
            string xml  = HttpUtils.PostRequest(url, registrationService.AuthorisationToken, body, "GET");

            if (log.IsDebugEnabled)
            {
                log.Debug("XML from POST request ...");
            }
            if (log.IsDebugEnabled)
            {
                log.Debug(xml);
            }

            return(ListSerialiser.Deserialise(xml));
        }
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
        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.º 11
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.º 12
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.º 13
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.º 14
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();
            }
        }