Exemplo n.º 1
0
        private static bool TypeIsValidSerializer(Type t)
        {
            if (t.IsValueType)
            {
                SerializationLogger.LogError($"(De)serializer {t.PrettyName()} is a value type, which is not allowed");
                return(false);
            }
            var constructor = t.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null, Type.EmptyTypes, null);

            if (constructor == null)
            {
                SerializationLogger.LogError($"(De)serializer {t.PrettyName()} doesn't have a parameterless constructor");
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        public override AContainer <BinaryStream> LoadContainer(string name, bool writable, bool errorIfNotExist = true)
        {
            // all saved in memory containers are considered as writable
            AContainer <BinaryStream> container = null;

            if (_containerPositions.TryGetValue(name, out PosLen containerPosLen))
            {
                _storage.Seek(containerPosLen.Position, SeekOrigin.Begin);
                var data = new byte[containerPosLen.Length];
                _storage.Read(data, 0, data.Length);
                var memStream = CreateMemoryStream(data, writable);
                var binStream = new BinaryStream(memStream, writable);
                container = new BinaryContainer(binStream, _serializers);
            }
            else if (errorIfNotExist)
            {
                SerializationLogger.LogError($"Container '{name}' does not exist in {this.PrettyTypeName()}");
            }
            return(container);
        }
Exemplo n.º 3
0
        public override AContainer <BinaryStream> LoadContainer(string name, bool writable = false, bool errorIfNotExist = true)
        {
            // all files are considered as writable
            string filePath = GetFilePath(name);

            if (!File.Exists(filePath))
            {
                if (errorIfNotExist)
                {
                    SerializationLogger.LogError($"File '{filePath}' does not exist");
                }
                return(null);
            }
            var data      = File.ReadAllBytes(filePath);
            var memStream = CreateMemoryStream(data, writable);
            var binStream = new BinaryStream(memStream, writable);
            var container = new BinaryContainer(binStream, _serializers);

            return(container);
        }
Exemplo n.º 4
0
        public static bool CreateFile(string filePath)
        {
            var directory = Directory.GetParent(filePath).FullName;

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            if (!Directory.Exists(directory))
            {
                SerializationLogger.LogError($"Unable to create directory '{directory}'");
                return(false);
            }
            File.WriteAllText(filePath, "");
            if (!File.Exists(filePath))
            {
                SerializationLogger.LogError($"Unable to create file '{filePath}'");
                return(false);
            }
            return(true);
        }