Esempio n. 1
0
        public void Save(XElement parentElement)
        {
            var newElement = new XElement("Level",
                                          new XAttribute("seed", Seed),
                                          new XAttribute("biome", Biome.Identifier),
                                          new XAttribute("type", Type.ToString()),
                                          new XAttribute("difficulty", Difficulty.ToString("G", CultureInfo.InvariantCulture)),
                                          new XAttribute("size", XMLExtensions.PointToString(Size)),
                                          new XAttribute("generationparams", GenerationParams.Identifier),
                                          new XAttribute("initialdepth", InitialDepth));

            if (HasBeaconStation)
            {
                newElement.Add(
                    new XAttribute("hasbeaconstation", HasBeaconStation.ToString()),
                    new XAttribute("isbeaconactive", IsBeaconActive.ToString()));
            }

            if (Type == LevelType.Outpost)
            {
                if (EventHistory.Any())
                {
                    newElement.Add(new XAttribute("eventhistory", string.Join(',', EventHistory.Select(p => p.Identifier))));
                }
                if (NonRepeatableEvents.Any())
                {
                    newElement.Add(new XAttribute("nonrepeatableevents", string.Join(',', NonRepeatableEvents.Select(p => p.Identifier))));
                }
            }
            parentElement.Add(newElement);
        }
 private void SanitizeProperty()
 {
     sanitizedProperty = NewProperties switch
     {
         float f => f.FormatSingleDecimal(),
         Point point => XMLExtensions.PointToString(point),
         Vector2 vector2 => vector2.FormatZeroDecimal(),
         Vector3 vector3 => vector3.FormatSingleDecimal(),
         Vector4 vector4 => vector4.FormatSingleDecimal(),
         Color color => XMLExtensions.ColorToString(color),
         Rectangle rectangle => XMLExtensions.RectToString(rectangle),
         _ => NewProperties.ToString()
     };
 }
Esempio n. 3
0
        public void Save(XElement parentElement)
        {
            var newElement = new XElement("Level",
                                          new XAttribute("seed", Seed),
                                          new XAttribute("biome", Biome.Identifier),
                                          new XAttribute("type", Type.ToString()),
                                          new XAttribute("difficulty", Difficulty.ToString("G", CultureInfo.InvariantCulture)),
                                          new XAttribute("size", XMLExtensions.PointToString(Size)),
                                          new XAttribute("generationparams", GenerationParams.Identifier));

            if (Type == LevelType.Outpost && EventHistory.Any())
            {
                newElement.Add(new XAttribute("eventhistory", string.Join(',', EventHistory.Select(p => p.Identifier))));
            }

            parentElement.Add(newElement);
        }
Esempio n. 4
0
        public static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault = false)
        {
            var saveProperties = GetProperties <Serialize>(obj);

            foreach (var property in saveProperties)
            {
                object value = property.GetValue(obj);
                if (value == null)
                {
                    continue;
                }

                if (!saveIfDefault)
                {
                    //only save
                    //  - if the attribute is saveable and it's different from the default value
                    //  - or can be changed in-game or in the editor
                    bool save = false;
                    foreach (var attribute in property.Attributes.OfType <Serialize>())
                    {
                        if ((attribute.isSaveable && !attribute.defaultValue.Equals(value)) ||
                            property.Attributes.OfType <Editable>().Any())
                        {
                            save = true;
                            break;
                        }
                    }

                    if (!save)
                    {
                        continue;
                    }
                }

                string stringValue;
                if (!supportedTypes.TryGetValue(value.GetType(), out string typeName))
                {
                    if (property.PropertyType.IsEnum)
                    {
                        stringValue = value.ToString();
                    }
                    else
                    {
                        DebugConsole.ThrowError("Failed to serialize the property \"" + property.Name + "\" of \"" + obj + "\" (type " + property.PropertyType + " not supported)");
                        continue;
                    }
                }
                else
                {
                    switch (typeName)
                    {
                    case "float":
                        //make sure the decimal point isn't converted to a comma or anything else
                        stringValue = ((float)value).ToString("G", CultureInfo.InvariantCulture);
                        break;

                    case "point":
                        stringValue = XMLExtensions.PointToString((Point)value);
                        break;

                    case "vector2":
                        stringValue = XMLExtensions.Vector2ToString((Vector2)value);
                        break;

                    case "vector3":
                        stringValue = XMLExtensions.Vector3ToString((Vector3)value);
                        break;

                    case "vector4":
                        stringValue = XMLExtensions.Vector4ToString((Vector4)value);
                        break;

                    case "color":
                        stringValue = XMLExtensions.ColorToString((Color)value);
                        break;

                    case "rectangle":
                        stringValue = XMLExtensions.RectToString((Rectangle)value);
                        break;

                    default:
                        stringValue = value.ToString();
                        break;
                    }
                }

                element.Attribute(property.Name)?.Remove();
                element.SetAttributeValue(property.NameToLowerInvariant, stringValue);
            }
        }