Ejemplo n.º 1
0
        public void Write(string file, T obj)
        {
            WindowsUtils.CreateDirectoryForFile(file);

            using (StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
                foreach (KeyValuePair <string, PropertyInfo> prop in props)
                {
                    Type   type  = prop.Value.PropertyType;
                    object value = prop.Value.GetValue(obj);

                    if (!converters.TryGetValue(type, out ITypeConverter serializer))
                    {
                        serializer = BasicSerializerObj;
                    }

                    if (serializer.TryWriteType(type, value, out string converted))
                    {
                        if (converted != null)
                        {
                            writer.Write($"{prop.Key} {converted.Replace(Environment.NewLine, NewLineCustom)}");
                            writer.Write(NewLineReal);
                        }
                    }
                    else
                    {
                        throw new SerializationException($"Invalid serialization type, conversion failed for: {type}");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // Public methods

        public void WriteFile(int token, string path, string contents)
        {
            string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);

            WindowsUtils.CreateDirectoryForFile(fullPath);
            File.WriteAllText(fullPath, contents, Encoding.UTF8);
            fileCache[token, SanitizeCacheKey(path)] = contents;
        }
Ejemplo n.º 3
0
            public void WriteToFile(string path, bool createDirectory)
            {
                if (createDirectory)
                {
                    WindowsUtils.CreateDirectoryForFile(path);
                }

                File.WriteAllBytes(path, contents);
            }
Ejemplo n.º 4
0
        public void Write(string file, T obj)
        {
            LinkedList <string> errors = new LinkedList <string>();

            WindowsUtils.CreateDirectoryForFile(file);

            using (StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
                foreach (KeyValuePair <string, PropertyInfo> prop in props)
                {
                    Type   type  = prop.Value.PropertyType;
                    object value = prop.Value.GetValue(obj);

                    if (!converters.TryGetValue(type, out ITypeConverter serializer))
                    {
                        serializer = BasicSerializerObj;
                    }

                    if (serializer.TryWriteType(type, value, out string converted))
                    {
                        if (converted != null)
                        {
                            writer.Write(prop.Key);
                            writer.Write(' ');
                            writer.Write(EscapeLine(converted));
                            writer.Write(NewLineReal);
                        }
                    }
                    else
                    {
                        errors.AddLast($"Missing converter for type: {type}");
                    }
                }
            }

            if (errors.First != null)
            {
                throw new SerializationSoftException(errors.ToArray());
            }
        }