internal static void SetStringSpecialCase(Node node, string value, FileStyle style)
        {
            if (value != null && value.ContainsNewLine())
            {
                node.Value = MultiLineStringNode.Terminator;
                var lines = value.SplitIntoLines();

                node.CapChildCount(lines.Length + 1);

                for (int i = 0; i < lines.Length; i++)
                {
                    var    newnode   = node.GetChildAddresedByStringLineNumber(i);
                    string lineValue = BaseStringRules.SerializeItem(lines[i], style);

                    if (lineValue.EndsWith(MultiLineStringNode.NoLineBreakIndicator))
                    {
                        lineValue = lineValue.Quote();
                    }

                    newnode.Value = lineValue;
                }

                node.GetChildAddresedByStringLineNumber(lines.Length).MakeTerminator();
                return;
            }
            else
            {
                node.ClearChildren();
                node.Value = BaseStringRules.SerializeItem(value, style);
            }
        }
예제 #2
0
        internal static void SetNodeData(Node node, object data, Type type, FileStyle style)
        {
            if (data == null)
            {
                throw new Exception("you can't serialize null");
            }

            // ensure the type is initialized. This is especially important if it's added as
            // a base type in the type's static constructor.
            System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);

            string dataAsString = data as string;

            if (type == typeof(string) && (dataAsString.ContainsNewLine() || node.ChildNodes.Count > 0))
            {
                BaseTypes.SetStringSpecialCase(node, dataAsString, style);
            }

            else if (BaseTypes.IsBaseType(type))
            {
                BaseTypes.SetBaseTypeNode(node, data, type, style);
            }

            else if (CollectionTypes.TrySetCollection(node, data, type, style))
            {
                return;
            }

            else
            {
                ComplexTypes.SetComplexNode(node, data, type, style);
            }
        }
예제 #3
0
        private static void SetListNode(Node node, object list, Type listType, FileStyle style)
        {
            Type elementType = listType.GetGenericArguments()[0];

            SetListNodeG.MakeGenericMethod(elementType)
            .Invoke(null, node, list, style);
        }
예제 #4
0
        internal void AddStyle(FileStyle style)
        {
            if (style == null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            _styles.Add(style);
        }
예제 #5
0
        private static void SetListNodeGeneric <T>(Node node, List <T> list, FileStyle style)
        {
            node.CapChildCount(list.Count);

            for (int i = 0; i < list.Count; i++)
            {
                NodeManager.SetNodeData <T>(node.GetChildAddressedByListNumber(i), list[i], style);
            }
        }
예제 #6
0
        public HasFileStyleAttribute(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Style = new FileStyle(name);
        }
예제 #7
0
 public FileManager(FileStyle st, string path)
 {
     InitializeComponent();
     if (!DesignMode)
     {
         this.FileStyle   = st;
         usedFileFullName = path;
         LoadDefaultFile();
     }
 }
예제 #8
0
        public PropertyBuilder <TProperty> HasFileStyle(FileStyle fileStyle)
        {
            if (fileStyle == null)
            {
                throw new ArgumentNullException(nameof(fileStyle));
            }

            _propertyMetadata.AddStyle(fileStyle);

            return(this);
        }
예제 #9
0
        private static string SerializeEnum(object value, FileStyle style)
        {
            switch (style.EnumStyle)
            {
            case EnumStyle.name:
            default:
                return(value.ToString());

            case EnumStyle.number:
                return(((Enum)value).ToString("d"));
            }
        }
예제 #10
0
        // arrays are the only type in here that doesn't require non-generic generics trickery
        private static void SetArrayNode(Node node, object array, Type arrayType, FileStyle style)
        {
            Type elementType = arrayType.GetElementType();

            var boi = (Array)array;

            node.CapChildCount(boi.Length);

            for (int i = 0; i < boi.Length; i++)
            {
                NodeManager.SetNodeData(node.GetChildAddressedByListNumber(i), boi.GetValue(i), elementType, style);
            }
        }
예제 #11
0
        internal static void SetComplexNode(Node node, object item, Type type, FileStyle style)
        {
            // Clear the shortcut if there is any
            if (!node.Value.IsNullOrEmpty())
            {
                node.Value = "";
            }

            foreach (var m in type.GetValidMembers())
            {
                var child = node.GetChildAddressedByName(m.Name);
                NodeManager.SetNodeData(child, m.GetValue(item), m.MemberType, style);
            }
        }
예제 #12
0
        public static string SerializeBaseType(object data, Type type, FileStyle style)
        {
            if (RegisteredBaseTypeLogics.TryGetValue(type, out var baseTypeLogic))
            {
                return(baseTypeLogic.SerializeObject(data, style));
            }

            if (type.IsEnum)
            {
                return(SerializeEnum(data, style));
            }

            throw new Exception($"Cannot serialize base type {type} - are you sure it is a base type?");
        }
예제 #13
0
        internal static void SetNodeData(Node node, object data, Type type, FileStyle style)
        {
            if (data == null)
            {
                node.ClearChildren();
                node.Value = Utilities.NullIndicator;
                return;
            }
            else if (node.Value == Utilities.NullIndicator)
            {
                node.Value = String.Empty;
            }

            // ensure the type is initialized. This is especially important if it's added as
            // a base type in the type's static constructor.
            System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);

            // If we try to save a single-line string and find it is currently saved as a multi-line string, we do NOT remove the mutli-line formatting.
            // The reason for this is that there might be comments on the """s, and we want to preserve those comments.
            // Also, this happens in only two cases:
            //     1. A string that is usually single-line is manually switched to multi-line formatting by a user
            //     2. A string is saved as multi-line, then later saved as single-line
            // In case 1, we don't want to piss off the user; keep it how they like it.
            // In case 2, the string is probably going to be saved again later with multiple lines. It doesn't seem necessary to disrupt the structure
            // of the file for something temporary.
            string dataAsString = data as string;

            if (type == typeof(string) && (dataAsString.ContainsNewLine() || node.ChildNodes.Count > 0))
            {
                BaseTypes.SetStringSpecialCase(node, dataAsString, style);
            }

            else if (BaseTypes.IsBaseType(type))
            {
                BaseTypes.SetBaseTypeNode(node, data, type, style);
            }

            else if (CollectionTypes.TrySetCollection(node, data, type, style))
            {
                return;
            }

            else
            {
                ComplexTypes.SetComplexNode(node, data, type, style);
            }
        }
예제 #14
0
        public override string SerializeItem(bool value, FileStyle style)
        {
            switch (style.BoolStyle)
            {
            case BoolStyle.true_false:
            default:
                return(value ? "true" : "false");

            case BoolStyle.on_off:
                return(value ? "on" : "off");

            case BoolStyle.yes_no:
                return(value ? "yes" : "no");

            case BoolStyle.y_n:
                return(value ? "y" : "n");
            }
        }
예제 #15
0
        internal static void SetComplexNode(Node node, object item, Type type, FileStyle style)
        {
            // clear the shortcut if there is any
            if (!string.IsNullOrEmpty(node.Value))
            {
                node.Value = "";
            }

            foreach (var f in GetValidFields(type))
            {
                var child = node.GetChildAddressedByName(f.Name);
                NodeManager.SetNodeData(child, f.GetValue(item), f.FieldType, style);
            }

            foreach (var p in GetValidProperties(type))
            {
                var child = node.GetChildAddressedByName(p.Name);
                NodeManager.SetNodeData(child, p.GetValue(item), p.PropertyType, style);
            }
        }
예제 #16
0
        public override string SerializeItem(string value, FileStyle style)
        {
            if (value.IsNullOrEmpty())
            {
                return(String.Empty);
            }

            value = value.Replace("\t", "    "); // SUCC files cannot contain tabs. Prevent saving strings with tabs in them.

            if (
                style.AlwaysQuoteStrings ||
                value[0] == ' ' ||
                value[value.Length - 1] == ' ' ||
                value.IsQuoted() ||
                value == Utilities.NullIndicator
                )
            {
                value = value.Quote();
            }

            return(value);
        }
예제 #17
0
        private static void SetDictionaryNodeGeneric <TKey, TValue>(Node node, Dictionary <TKey, TValue> dictionary, FileStyle style, bool forceArrayMode = false)
        {
            bool keyIsBase = BaseTypesManager.IsBaseType(typeof(TKey));

            if (keyIsBase && !forceArrayMode && !style.AlwaysArrayDictionaries)
            {
                // we might have switched between standard and array dictionary storage, and if so, children need to be reset
                if (node.ChildNodeType != NodeChildrenType.key)
                {
                    node.ClearChildren(newChildrenType: NodeChildrenType.key);
                }

                var CurrentKeys = new List <string>(capacity: dictionary.Count);
                foreach (var key in dictionary.Keys)
                {
                    var value = dictionary[key];

                    string keyAsText = BaseTypesManager.SerializeBaseType <TKey>(key, style);

                    if (!Utilities.IsValidKey(keyAsText))
                    {
                        SetDictionaryNodeGeneric(node, dictionary, style, forceArrayMode: true);
                        return;
                    }

                    CurrentKeys.Add(keyAsText);
                    KeyNode child = node.GetChildAddressedByName(keyAsText);
                    NodeManager.SetNodeData <TValue>(child, value, style);
                }

                // make sure that old data in the file is deleted when a new dictionary is saved.
                // node.ClearChildren() is not used because we want to keep comments and whitespace intact as much as possible.
                foreach (var key in node.GetChildKeys())
                {
                    if (!CurrentKeys.Contains(key))
                    {
                        node.RemoveChild(key);
                    }
                }
            }
            else // save dictionary as KeyValuePair<TKey, TValue>[]
            {
                // we might have switched between standard and array dictionary storage, and if so, children need to be reset
                if (node.ChildNodeType != NodeChildrenType.list)
                {
                    node.ClearChildren(newChildrenType: NodeChildrenType.list);
                }

                var array = GetWritableKeyValuePairArray(dictionary);
                NodeManager.SetNodeData(node, array, array.GetType(), style);
            }
        }
예제 #18
0
        public static void SetDictionaryNode(Node node, object dictionary, Type dictionaryType, FileStyle style, bool forceArrayMode = false)
        {
            Type keyType   = dictionaryType.GetGenericArguments()[0];
            Type valueType = dictionaryType.GetGenericArguments()[1];

            SetDictionary_.GetBoundGenericMethod(keyType, valueType)
            .Invoke(null, node, dictionary, style, forceArrayMode);
        }
예제 #19
0
 internal static void SetNodeData <T>(Node node, T data, FileStyle style) => SetNodeData(node, data, typeof(T), style);
예제 #20
0
 public DataManager(string text, FileStyle style)
 {
     InitializeComponent();
     this.Text = text;
     Style     = style;
 }
예제 #21
0
 public abstract string SerializeObject(object value, FileStyle style);
예제 #22
0
        public static void SetHashSetNode(Node node, object hashset, Type hashsetType, FileStyle style)
        {
            Type elementType = hashsetType.GetGenericArguments()[0];

            SetHashSetNode_.GetBoundGenericMethod(elementType)
            .Invoke(null, node, hashset, style);
        }
예제 #23
0
 /// <inheritdoc/>
 public ReadableWritableDataFile(bool autoSave, FileStyle style)
 {
     AutoSave = autoSave;
     Style    = style;
 }
예제 #24
0
        internal static bool TrySetCollection(Node node, object data, Type collectionType, FileStyle style)
        {
            if (collectionType.IsArray)
            {
                SetArrayNode(node, data, collectionType, style);
            }
            else if (collectionType.IsList())
            {
                SetListNode(node, data, collectionType, style);
            }
            else if (collectionType.IsHashSet())
            {
                SetHashSetNode(node, data, collectionType, style);
            }
            else if (collectionType.IsDictionary())
            {
                SetDictionaryNode(node, data, collectionType, style);
            }

            else if (node.ChildNodeType == NodeChildrenType.list)
            {
                throw new FormatException($"{collectionType} is not a supported collection type");
            }
            else
            {
                return(false);
            }

            return(true);
        }
예제 #25
0
 private static void SetBaseTypeNode(Node node, object data, Type type, FileStyle style)
 {
     node.ClearChildren(NodeChildrenType.none);
     node.Value = BaseTypesManager.SerializeBaseType(data, type, style);
 }
예제 #26
0
        private FileProccessingResult ProccessStyle(
            byte[] bytes,
            Image <Rgba32> originalImage,
            IImageFormat imageFormat,
            FileStyle style)
        {
            Stopwatch stopwatch = null;

            if (_logger.IsEnabled(LogLevel.Information))
            {
                stopwatch = Stopwatch.StartNew();
            }

            var outputStream = new MemoryStream();

            var originalWidth  = originalImage.Width;
            var originalHeight = originalImage.Height;

            int width;
            int height;

            var imageStyle = style as ImageStyle;

            if (imageStyle == null)
            {
                width  = originalImage.Width;
                height = originalImage.Height;

                outputStream = new MemoryStream(bytes, false);
            }
            else
            {
                using (var image = originalImage.Clone())
                {
                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        stopwatch.Stop();
                        _logger.LogInformation("Cloned '{styleName}' in '{elapsed}'ms", style.Name, stopwatch.Elapsed.TotalMilliseconds);
                        stopwatch.Restart();
                    }

                    _imageTransformer.Transform(image, imageStyle);

                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        stopwatch.Stop();
                        _logger.LogInformation("Transformed '{styleName}' in '{elapsed}'ms", style.Name, stopwatch.Elapsed.TotalMilliseconds);
                        stopwatch.Restart();
                    }

                    image.Save(outputStream, GetEncoder(imageFormat, imageStyle));

                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        stopwatch.Stop();
                        _logger.LogInformation("Saved '{styleName}' in '{elapsed}'ms", style.Name, stopwatch.Elapsed.TotalMilliseconds);
                    }

                    width  = image.Width;
                    height = image.Height;
                }
            }

            return(new FileProccessingResult
            {
                Style = style,
                Stream = outputStream,
                Meta = new ImageProccessingResultMeta
                {
                    OriginalWidth = originalWidth,
                    OriginalHeight = originalHeight,
                    Width = width,
                    Height = height
                }
            });
        }
예제 #27
0
        public static void SetCollectionNode(Node node, object data, Type collectionType, FileStyle style)
        {
            if (Arrays.IsArrayType(collectionType))
            {
                Arrays.SetArrayNode(node, data, collectionType, style);
            }

            else if (Lists.IsListType(collectionType))
            {
                Lists.SetListNode(node, data, collectionType, style);
            }

            else if (HashSets.IsHashSetType(collectionType))
            {
                HashSets.SetHashSetNode(node, data, collectionType, style);
            }

            else if (Dictionaries.IsDictionaryType(collectionType))
            {
                Dictionaries.SetDictionaryNode(node, data, collectionType, style);
            }

            else
            {
                throw new Exception("Unsupported type!");
            }
        }
예제 #28
0
 public static string SerializeBaseType <T>(T data, FileStyle style) => SerializeBaseType(data, typeof(T), style);
예제 #29
0
        private static void SetHashSetNodeGeneric <T>(Node node, HashSet <T> hashset, FileStyle style)
        {
            node.CapChildCount(hashset.Count);

            int i = 0;

            foreach (var item in hashset)
            {
                NodeManager.SetNodeData <T>(node.GetChildAddressedByListNumber(i), item, style);
                i++;
            }
        }
예제 #30
0
 /// <summary>
 /// Creates a DataFile in memory with some preexisting SUCC content and a custom FileStyle.
 /// </summary>
 public MemoryDataFile(string rawFileText, FileStyle style, bool autoSave = true) : base(autoSave, style)
 {
     MemoryTextData = rawFileText;
     this.ReloadAllData();
 }