public static CompileSpecification Parse(GenericStructure gs)
 {
     var spec = new CompileSpecification { ID = gs["ID"] ?? "", Name = gs["Name"] ?? ""};
     var tools = gs.GetChildren("Tool");
     spec.Tools.AddRange(tools.Select(CompileTool.Parse));
     var presets = gs.GetChildren("Preset");
     spec.Presets.AddRange(presets.Select(CompilePreset.Parse));
     return spec;
 }
Esempio n. 2
0
 public void Read(GenericStructure gs)
 {
     ID = gs.PropertyInteger("ID");
     Name = gs["Name"];
     Engine = (Engine)Enum.Parse(typeof(Engine), gs["EngineID"]);
     Path = gs["Path"];
     Bsp = gs["Bsp"];
     Csg = gs["Csg"];
     Vis = gs["Vis"];
     Rad = gs["Rad"];
 }
Esempio n. 3
0
 public void Write(GenericStructure gs)
 {
     gs["ID"] = ID.ToString(CultureInfo.InvariantCulture);
     gs["Name"] = Name;
     gs["EngineID"] = Engine.ToString();
     gs["Path"] = Path;
     gs["Bsp"] = Bsp;
     gs["Csg"] = Csg;
     gs["Vis"] = Vis;
     gs["Rad"] = Rad;
 }
Esempio n. 4
0
 public static CompilePreset Parse(GenericStructure gs)
 {
     return new CompilePreset
     {
         Name = gs["Name"] ?? "",
         Description = gs["Description"] ?? "",
         Csg = gs["Csg"] ?? "",
         Bsp = gs["Bsp"] ?? "",
         Vis = gs["Vis"] ?? "",
         Rad = gs["Rad"] ?? ""
     };
 }
Esempio n. 5
0
 public static CompileTool Parse(GenericStructure gs)
 {
     var tool = new CompileTool
     {
         Name = gs["Name"] ?? "",
         Description = gs["Description"] ?? "",
         Order = gs.PropertyInteger("Order")
     };
     var parameters = gs.GetChildren("Parameter");
     tool.Parameters.AddRange(parameters.Select(CompileParameter.Parse));
     return tool;
 }
Esempio n. 6
0
        public static T Deserialise <T>(GenericStructure structure)
        {
            var obj = DeserialiseHelper(typeof(T), structure, new Dictionary <int, object>());

            if (obj is T)
            {
                return((T)obj);
            }
            obj = Convert.ChangeType(obj, typeof(T));
            if (obj is T)
            {
                return((T)obj);
            }
            return(default(T));
        }
Esempio n. 7
0
 public static CompilePreset Parse(GenericStructure gs)
 {
     return new CompilePreset
     {
         Name = gs["Name"] ?? "",
         Description = gs["Description"] ?? "",
         Csg = gs["Csg"] ?? "",
         Bsp = gs["Bsp"] ?? "",
         Vis = gs["Vis"] ?? "",
         Rad = gs["Rad"] ?? "",
         RunCsg = gs.PropertyBoolean("RunCsg", true),
         RunBsp = gs.PropertyBoolean("RunBsp", true),
         RunVis = gs.PropertyBoolean("RunVis", true),
         RunRad = gs.PropertyBoolean("RunRad", true)
     };
 }
Esempio n. 8
0
        public void Read(GenericStructure gs)
        {
            ID = gs.PropertyInteger("ID");
            Name = gs["Name"];
            Specification = gs["Specification"];
            Engine = (Engine)Enum.Parse(typeof(Engine), gs["EngineID"]);
            DontRedirectOutput = gs.PropertyBoolean("DontRedirectOutput");
            Path = gs["Path"];
            Bsp = gs["Bsp"];
            Csg = gs["Csg"];
            Vis = gs["Vis"];
            Rad = gs["Rad"];
            IncludePathInEnvironment = gs.PropertyBoolean("IncludePathInEnvironment", true);

            WorkingDirectory = gs.PropertyEnum("WorkingDirectory", CompileWorkingDirectory.TemporaryDirectory);
            AfterCopyBsp = gs.PropertyBoolean("AfterCopyBsp");
            AfterRunGame = gs.PropertyBoolean("AfterRunGame");
            AfterAskBeforeRun = gs.PropertyBoolean("AfterAskBeforeRun");

            CopyBsp = gs.PropertyBoolean("CopyBsp");
            CopyRes = gs.PropertyBoolean("CopyRes");
            CopyLin = gs.PropertyBoolean("CopyLin");
            CopyMap = gs.PropertyBoolean("CopyMap");
            CopyPts = gs.PropertyBoolean("CopyPts");
            CopyLog = gs.PropertyBoolean("CopyLog");
            CopyErr = gs.PropertyBoolean("CopyErr");

            foreach (var prof in gs.GetChildren("Profile"))
            {
                var bp = new BuildProfile();
                bp.Read(prof);
                Profiles.Add(bp);
            }
        }
Esempio n. 9
0
File: Game.cs Progetto: silky/sledge
        public void Write(GenericStructure gs)
        {
            gs["ID"] = ID.ToString(CultureInfo.InvariantCulture);
            gs["Name"] = Name;
            gs["EngineID"] = Engine.ToString();
            gs["BuildID"] = BuildID.ToString(CultureInfo.InvariantCulture);
            gs["SteamInstall"] = SteamInstall.ToString(CultureInfo.InvariantCulture);
            gs["WonGameDir"] = WonGameDir;
            gs["SteamGameDir"] = SteamGameDir;
            gs["ModDir"] = ModDir;
            gs["UseHDModels"] = UseHDModels.ToString(CultureInfo.InvariantCulture);
            gs["BaseDir"] = BaseDir;
            gs["Executable"] = Executable;
            gs["ExecutableParameters"] = ExecutableParameters;
            gs["MapDir"] = MapDir;
            gs["Autosave"] = Autosave.ToString(CultureInfo.InvariantCulture);
            gs["UseCustomAutosaveDir"] = UseCustomAutosaveDir.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveDir"] = AutosaveDir;
            gs["AutosaveTime"] = AutosaveTime.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveLimit"] = AutosaveLimit.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveOnlyOnChanged"] = AutosaveOnlyOnChanged.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveTriggerFileChange"] = AutosaveTriggerFileSave.ToString(CultureInfo.InvariantCulture);
            gs["DefaultPointEntity"] = DefaultPointEntity;
            gs["DefaultBrushEntity"] = DefaultBrushEntity;
            gs["DefaultTextureScale"] = DefaultTextureScale.ToString(CultureInfo.InvariantCulture);
            gs["DefaultLightmapScale"] = DefaultLightmapScale.ToString(CultureInfo.InvariantCulture);
            gs["IncludeFgdDirectoriesInEnvironment"] = IncludeFgdDirectoriesInEnvironment.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSize"] = OverrideMapSize.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSizeLow"] = OverrideMapSizeLow.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSizeHigh"] = OverrideMapSizeHigh.ToString(CultureInfo.InvariantCulture);

            var additional = new GenericStructure("AdditionalPackages");
            var i = 1;
            foreach (var add in AdditionalPackages)
            {
                additional.AddProperty(i.ToString(CultureInfo.InvariantCulture), add);
                i++;
            }
            gs.Children.Add(additional);

            gs["PackageBlacklist"] = (PackageBlacklist ?? "").Replace("\r", "").Replace('\n', ';');
            gs["PackageWhitelist"] = (PackageWhitelist ?? "").Replace("\r", "").Replace('\n', ';');

            var fgds = new GenericStructure("Fgds");
            i = 1;
            foreach (var fgd in Fgds)
            {
                fgds.AddProperty(i.ToString(CultureInfo.InvariantCulture), fgd.Path);
                i++;
            }
            gs.Children.Add(fgds);
        }
Esempio n. 10
0
        private static GenericStructure SerialiseHelper(object obj, List <object> encounteredObjects)
        {
            // Handle null
            if (Equals(obj, null))
            {
                return new GenericStructure("Serialise.Null")
                       {
                           Properties = { new GenericStructureProperty("Serialise.Null.Value", "null") }
                       }
            }
            ;

            if (encounteredObjects.Contains(obj))
            {
                var rf = new GenericStructure("Serialise.Reference");

                rf.AddProperty("Serialise.Reference.Index", (encounteredObjects.IndexOf(obj) + 1).ToString(CultureInfo.InvariantCulture));
                return(rf);
            }

            var ty = obj.GetType();

            // Handle primitive types
            if (ty.IsPrimitive || ty == typeof(string) || ty == typeof(decimal))
            {
                var name = "Primitives.";
                if (ty == typeof(bool))
                {
                    name += "Boolean";
                }
                else if (ty == typeof(char) || ty == typeof(string))
                {
                    name += "String";
                }
                else
                {
                    name += "Numeric";
                }
                return(new GenericStructure(name)
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", Convert.ToString(obj, CultureInfo.InvariantCulture)) }
                });
            }

            if (ty == typeof(DateTime))
            {
                return(new GenericStructure("Primitives.DateTime")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", ((DateTime)obj).ToString("u")) }
                });
            }

            if (ty == typeof(Color))
            {
                var color = (Color)obj;
                var col   = String.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", color.R, color.G, color.B, color.A);
                return(new GenericStructure("Primitives.Colour")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", col) }
                });
            }

            if (ty == typeof(Coordinate))
            {
                return(new GenericStructure("Primitives.Coordinate")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", obj.ToString()) }
                });
            }

            if (ty == typeof(Box))
            {
                var b = (Box)obj;
                return(new GenericStructure("Primitives.Box")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", b.Start + " " + b.End) }
                });
            }

            if (ty == typeof(Rectangle))
            {
                var r = (Rectangle)obj;
                return(new GenericStructure("Primitives.Rectangle")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", r.X + " " + r.Y + " " + r.Width + " " + r.Height) }
                });
            }

            if (ty == typeof(Plane))
            {
                var p = (Plane)obj;
                return(new GenericStructure("Primitives.Plane")
                {
                    Properties = { new GenericStructureProperty("Primitive.Value", p.Normal + " " + p.DistanceFromOrigin) }
                });
            }

            encounteredObjects.Add(obj);
            var index = encounteredObjects.Count;

            // Handle list
            var enumerable = obj as IEnumerable;

            if (enumerable != null)
            {
                var children = enumerable.OfType <object>().Select(x => SerialiseHelper(x, encounteredObjects));
                var list     = new GenericStructure("Serialise.List");
                list.AddProperty("Serialise.Reference", index.ToString(CultureInfo.InvariantCulture));
                list.Children.AddRange(children);
                return(list);
            }

            // Handle complex types
            var gs = new GenericStructure(ty.FullName);

            gs.AddProperty("Serialise.Reference", index.ToString(CultureInfo.InvariantCulture));
            foreach (var pi in ty.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!pi.CanRead)
                {
                    continue;
                }
                var val = pi.GetValue(obj, null);
                var pv  = SerialiseHelper(val, encounteredObjects);
                if (pv.Name.StartsWith("Primitives."))
                {
                    gs.AddProperty(pi.Name, pv["Primitive.Value"]);
                }
                else
                {
                    pv.Name = pi.Name;
                    gs.Children.Add(pv);
                }
            }
            return(gs);
        }
Esempio n. 11
0
 public void Write(GenericStructure gs)
 {
     foreach (var pi in GetType().GetProperties())
     {
         var val = pi.GetValue(this, null);
         gs.AddProperty(pi.Name, val == null ? "" : val.ToString());
     }
 }
Esempio n. 12
0
 public void Read(GenericStructure gs)
 {
     foreach (var pi in GetType().GetProperties().Where(x => x.CanWrite))
     {
         var val = gs[pi.Name] ?? "";
         if (pi.PropertyType == typeof (int))
         {
             int i;
             pi.SetValue(this, Int32.TryParse(val, out i) ? i : 0, null);
         }
         else
         {
             pi.SetValue(this, val, null);
         }
     }
 }
Esempio n. 13
0
 /// <summary>
 /// Parse a property string in the format: "key" "value", and add it to the structure
 /// </summary>
 /// <param name="gs">The structure to add the property to</param>
 /// <param name="prop">The property string to parse</param>
 private static void ParseProperty(GenericStructure gs, string prop)
 {
     var split = SplitWithQuotes(prop);
     gs.Properties.Add(new GenericStructureProperty(split[0], split[1]));
 }
Esempio n. 14
0
        /// <summary>
        /// Parse a structure, given the name of the structure
        /// </summary>
        /// <param name="reader">The TextReader to read from</param>
        /// <param name="name">The structure's name</param>
        /// <returns>The parsed structure</returns>
        private static GenericStructure ParseStructure(TextReader reader, string name)
        {
            var gs = new GenericStructure(SplitWithQuotes(name)[0]);
            var line = CleanLine(reader.ReadLine());
            if (line != "{") {
                return gs;
            }
            while ((line = CleanLine(reader.ReadLine())) != null)
            {
                if (line == "}") break;

                if (ValidStructPropertyString(line)) ParseProperty(gs, line);
                else if (ValidStructStartString(line)) gs.Children.Add(ParseStructure(reader, line));
            }
            return gs;
        }
Esempio n. 15
0
File: Game.cs Progetto: silky/sledge
        public void Read(GenericStructure gs)
        {
            ID = gs.PropertyInteger("ID");
            Name = gs["Name"];
            Engine = (Engine) Enum.Parse(typeof(Engine), gs["EngineID"]);
            BuildID = gs.PropertyInteger("BuildID");
            SteamInstall = gs.PropertyBoolean("SteamInstall");
            WonGameDir = gs["WonGameDir"];
            SteamGameDir = gs["SteamGameDir"];
            ModDir = gs["ModDir"];
            UseHDModels = gs.PropertyBoolean("UseHDModels", true);
            BaseDir = gs["BaseDir"];
            Executable = gs["Executable"];
            ExecutableParameters = gs["ExecutableParameters"];
            MapDir = gs["MapDir"];
            Autosave = gs.PropertyBoolean("Autosave");
            UseCustomAutosaveDir = gs.PropertyBoolean("UseCustomAutosaveDir");
            AutosaveDir = gs["AutosaveDir"];
            AutosaveTime = gs.PropertyInteger("AutosaveTime");
            AutosaveLimit = gs.PropertyInteger("AutosaveLimit");
            AutosaveOnlyOnChanged = gs.PropertyBoolean("AutosaveOnlyOnChanged", true);
            AutosaveTriggerFileSave = gs.PropertyBoolean("AutosaveTriggerFileChange", true);
            DefaultPointEntity = gs["DefaultPointEntity"];
            DefaultBrushEntity = gs["DefaultBrushEntity"];
            DefaultTextureScale = gs.PropertyDecimal("DefaultTextureScale");
            DefaultLightmapScale = gs.PropertyDecimal("DefaultLightmapScale");
            IncludeFgdDirectoriesInEnvironment = gs.PropertyBoolean("IncludeFgdDirectoriesInEnvironment", true);
            OverrideMapSize = gs.PropertyBoolean("OverrideMapSize");
            OverrideMapSizeLow = gs.PropertyInteger("OverrideMapSizeLow");
            OverrideMapSizeHigh = gs.PropertyInteger("OverrideMapSizeHigh");

            var additional = gs.Children.FirstOrDefault(x => x.Name == "AdditionalPackages");
            if (additional != null)
            {
                foreach (var key in additional.GetPropertyKeys())
                {
                    AdditionalPackages.Add(additional[key]);
                }
            }

            PackageBlacklist = (gs["PackageBlacklist"] ?? "").Replace(";", "\r\n");
            PackageWhitelist = (gs["PackageWhitelist"] ?? "").Replace(";", "\r\n");

            var fgds = gs.Children.FirstOrDefault(x => x.Name == "Fgds");
            if (fgds != null)
            {
                foreach (var key in fgds.GetPropertyKeys())
                {
                    Fgds.Add(new Fgd { Path = fgds[key] });
                }
            }
        }
Esempio n. 16
0
        public void Write(GenericStructure gs)
        {
            gs["ID"] = ID.ToString(CultureInfo.InvariantCulture);
            gs["Name"] = Name;
            gs["Specification"] = Specification;
            gs["EngineID"] = Engine.ToString();
            gs["DontRedirectOutput"] = DontRedirectOutput.ToString(CultureInfo.InvariantCulture);
            gs["Path"] = Path;
            gs["Bsp"] = Bsp;
            gs["Csg"] = Csg;
            gs["Vis"] = Vis;
            gs["Rad"] = Rad;
            gs["IncludePathInEnvironment"] = IncludePathInEnvironment.ToString(CultureInfo.InvariantCulture);

            gs["WorkingDirectory"] = WorkingDirectory.ToString();
            gs["AfterCopyBsp"] = AfterCopyBsp.ToString(CultureInfo.InvariantCulture);
            gs["AfterRunGame"] = AfterRunGame.ToString(CultureInfo.InvariantCulture);
            gs["AfterAskBeforeRun"] = AfterAskBeforeRun.ToString(CultureInfo.InvariantCulture);

            gs["CopyBsp"] = CopyBsp.ToString(CultureInfo.InvariantCulture);
            gs["CopyRes"] = CopyRes.ToString(CultureInfo.InvariantCulture);
            gs["CopyLin"] = CopyLin.ToString(CultureInfo.InvariantCulture);
            gs["CopyMap"] = CopyMap.ToString(CultureInfo.InvariantCulture);
            gs["CopyPts"] = CopyPts.ToString(CultureInfo.InvariantCulture);
            gs["CopyLog"] = CopyLog.ToString(CultureInfo.InvariantCulture);
            gs["CopyErr"] = CopyErr.ToString(CultureInfo.InvariantCulture);

            foreach (var bp in Profiles)
            {
                var prof = new GenericStructure("Profile");
                bp.Write(prof);
                gs.Children.Add(prof);
            }
        }
Esempio n. 17
0
        public static void Write()
        {
            var newSettings = Serialise.SerialiseSettings().Select(s => new Setting { Key = s.Key, Value = s.Value });
            Settings.Clear();
            Settings.AddRange(newSettings);

            var root = new GenericStructure("Sledge");

            // Settings
            var settings = new GenericStructure("Settings");
            foreach (var setting in Settings)
            {
                settings.AddProperty(setting.Key, setting.Value);
            }
            root.Children.Add(settings);

            // Recent Files
            var recents = new GenericStructure("RecentFiles");
            var i = 1;
            foreach (var file in RecentFiles.OrderBy(x => x.Order).Select(x => x.Location).Where(File.Exists))
            {
                recents.AddProperty(i.ToString(CultureInfo.InvariantCulture), file);
                i++;
            }
            root.Children.Add(recents);

            // Games > Fgds/Wads
            foreach (var game in Games)
            {
                var g = new GenericStructure("Game");
                game.Write(g);
                root.Children.Add(g);
            }

            // Builds
            foreach (var build in Builds)
            {
                var b = new GenericStructure("Build");
                build.Write(b);
                root.Children.Add(b);
            }

            File.WriteAllText(SettingsFile, root.ToString());
        }
Esempio n. 18
0
        public void Write(GenericStructure gs)
        {
            gs["ID"] = ID.ToString(CultureInfo.InvariantCulture);
            gs["Name"] = Name;
            gs["EngineID"] = Engine.ToString();
            gs["BuildID"] = BuildID.ToString(CultureInfo.InvariantCulture);
            gs["SteamInstall"] = SteamInstall.ToString(CultureInfo.InvariantCulture);
            gs["WonGameDir"] = WonGameDir;
            gs["SteamGameDir"] = SteamGameDir;
            gs["ModDir"] = ModDir;
            gs["BaseDir"] = BaseDir;
            gs["Executable"] = Executable;
            gs["ExecutableParameters"] = ExecutableParameters;
            gs["MapDir"] = MapDir;
            gs["Autosave"] = Autosave.ToString(CultureInfo.InvariantCulture);
            gs["UseCustomAutosaveDir"] = UseCustomAutosaveDir.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveDir"] = AutosaveDir;
            gs["AutosaveTime"] = AutosaveTime.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveLimit"] = AutosaveLimit.ToString(CultureInfo.InvariantCulture);
            gs["AutosaveOnlyOnChanged"] = AutosaveOnlyOnChanged.ToString(CultureInfo.InvariantCulture);
            gs["DefaultPointEntity"] = DefaultPointEntity;
            gs["DefaultBrushEntity"] = DefaultBrushEntity;
            gs["DefaultTextureScale"] = DefaultTextureScale.ToString(CultureInfo.InvariantCulture);
            gs["DefaultLightmapScale"] = DefaultLightmapScale.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSize"] = OverrideMapSize.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSizeLow"] = OverrideMapSizeLow.ToString(CultureInfo.InvariantCulture);
            gs["OverrideMapSizeHigh"] = OverrideMapSizeHigh.ToString(CultureInfo.InvariantCulture);

            var wads = new GenericStructure("Wads");
            var i = 1;
            foreach (var wad in Wads)
            {
                wads.AddProperty(i.ToString(CultureInfo.InvariantCulture), wad.Path);
                i++;
            }
            gs.Children.Add(wads);

            var fgds = new GenericStructure("Fgds");
            i = 1;
            foreach (var fgd in Fgds)
            {
                fgds.AddProperty(i.ToString(CultureInfo.InvariantCulture), fgd.Path);
                i++;
            }
            gs.Children.Add(fgds);
        }
Esempio n. 19
0
        public void Read(GenericStructure gs)
        {
            ID = gs.PropertyInteger("ID");
            Name = gs["Name"];
            Engine = (Engine) Enum.Parse(typeof(Engine), gs["EngineID"]);
            BuildID = gs.PropertyInteger("BuildID");
            SteamInstall = gs.PropertyBoolean("SteamInstall");
            WonGameDir = gs["WonGameDir"];
            SteamGameDir = gs["SteamGameDir"];
            ModDir = gs["ModDir"];
            BaseDir = gs["BaseDir"];
            Executable = gs["Executable"];
            ExecutableParameters = gs["ExecutableParameters"];
            MapDir = gs["MapDir"];
            Autosave = gs.PropertyBoolean("Autosave");
            UseCustomAutosaveDir = gs.PropertyBoolean("UseCustomAutosaveDir");
            AutosaveDir = gs["AutosaveDir"];
            AutosaveTime = gs.PropertyInteger("AutosaveTime");
            AutosaveLimit = gs.PropertyInteger("AutosaveLimit");
            AutosaveOnlyOnChanged = gs.PropertyBoolean("AutosaveOnlyOnChanged");
            DefaultPointEntity = gs["DefaultPointEntity"];
            DefaultBrushEntity = gs["DefaultBrushEntity"];
            DefaultTextureScale = gs.PropertyDecimal("DefaultTextureScale");
            DefaultLightmapScale = gs.PropertyDecimal("DefaultLightmapScale");
            OverrideMapSize = gs.PropertyBoolean("OverrideMapSize");
            OverrideMapSizeLow = gs.PropertyInteger("OverrideMapSizeLow");
            OverrideMapSizeHigh = gs.PropertyInteger("OverrideMapSizeHigh");

            var wads = gs.Children.FirstOrDefault(x => x.Name == "Wads");
            if (wads != null)
            {
                foreach (var key in wads.GetPropertyKeys())
                {
                    Wads.Add(new Wad { Path = wads[key] });
                }
            }

            var fgds = gs.Children.FirstOrDefault(x => x.Name == "Fgds");
            if (fgds != null)
            {
                foreach (var key in fgds.GetPropertyKeys())
                {
                    Fgds.Add(new Fgd { Path = fgds[key] });
                }
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Parse a property string in the format: "key" "value", and add it to the structure
        /// </summary>
        /// <param name="gs">The structure to add the property to</param>
        /// <param name="prop">The property string to parse</param>
        private static void ParseProperty(GenericStructure gs, string prop)
        {
            var split = prop.SplitWithQuotes();

            gs.Properties.Add(new GenericStructureProperty(split[0], (split[1] ?? "").Replace('`', '"')));
        }
Esempio n. 21
0
        private static object DeserialiseHelper(Type bindingType, GenericStructure structure, Dictionary <int, object> encounteredObjects)
        {
            // Null values
            if (structure.Name == "Serialise.Null" || structure["Serialise.Null.Value"] == "null")
            {
                return(bindingType.IsValueType ? Activator.CreateInstance(bindingType) : null);
            }

            // Referenced values
            var indexProp = structure.Properties.FirstOrDefault(x => x.Key == "Serialise.Reference.Index");

            if (indexProp != null)
            {
                return(encounteredObjects[int.Parse(indexProp.Value)]);
            }

            // Primitive objects
            if (structure.Name.StartsWith("Primitives."))
            {
                return(ConvertPrimitive(structure));
            }

            //var instance = Activator.CreateInstance(bindingType);
            var refProp = structure.Properties.FirstOrDefault(x => x.Key == "Serialise.Reference");
            var refVal  = refProp != null?int.Parse(refProp.Value) : -1;

            // List objects
            if (structure.Name == "Serialise.List" || typeof(IEnumerable).IsAssignableFrom(bindingType))
            {
                var list = Activator.CreateInstance(bindingType);
                if (refVal >= 0)
                {
                    encounteredObjects[refVal] = list;
                }
                DeserialiseList(list, bindingType, structure, encounteredObjects);
                return(list);
            }

            // Complex types
            var ctor     = bindingType.GetConstructor(Type.EmptyTypes) ?? bindingType.GetConstructors().First();
            var args     = ctor.GetParameters().Select(x => x.ParameterType.IsValueType ? Activator.CreateInstance(x.ParameterType) : null).ToArray();
            var instance = ctor.Invoke(args);

            if (refVal >= 0)
            {
                encounteredObjects[refVal] = instance;
            }

            foreach (var pi in bindingType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!pi.CanWrite)
                {
                    continue;
                }
                var prop  = structure.Properties.FirstOrDefault(x => x.Key == pi.Name);
                var child = structure.Children.FirstOrDefault(x => x.Name == pi.Name);
                if (prop != null)
                {
                    var prim = ConvertPrimitive(pi.PropertyType, prop.Value);
                    pi.SetValue(instance, Convert.ChangeType(prim, pi.PropertyType), null);
                }
                else if (child != null)
                {
                    var obj = DeserialiseHelper(pi.PropertyType, child, encounteredObjects);
                    pi.SetValue(instance, obj, null);
                }
            }

            return(instance);
        }