public static void ReadProperties(Instance instance, XmlNode propsNode)
        {
            var error = CreateErrorHandler(nameof(ReadProperties));

            if (propsNode.Name != "Properties")
            {
                throw error("Provided XmlNode's class should be 'Properties'!");
            }

            foreach (XmlNode propNode in propsNode.ChildNodes)
            {
                if (propNode.NodeType == XmlNodeType.Comment)
                {
                    continue;
                }

                string  propType = propNode.Name;
                XmlNode propName = propNode.Attributes.GetNamedItem("name");

                if (propName == null)
                {
                    if (propNode.Name == "Item")
                    {
                        continue;
                    }

                    throw error("Got a property node without a 'name' attribute!");
                }

                IXmlPropertyToken tokenHandler = XmlPropertyTokens.GetHandler(propType);

                if (tokenHandler != null)
                {
                    Property prop = new Property()
                    {
                        Name     = propName.InnerText,
                        Instance = instance,
                        XmlToken = propType
                    };

                    if (!tokenHandler.ReadProperty(prop, propNode) && RobloxFile.LogErrors)
                    {
                        var readError = error($"Could not read property: {prop.GetFullName()}!");
                        Console.Error.WriteLine(readError.Message);
                    }

                    instance.AddProperty(ref prop);
                }
                else if (RobloxFile.LogErrors)
                {
                    var tokenError = error($"No {nameof(IXmlPropertyToken)} found for property type: {propType}!");
                    Console.Error.WriteLine(tokenError.Message);
                }
            }
        }
Esempio n. 2
0
        public static void ReadProperties(Instance instance, XmlNode propsNode)
        {
            var error = createErrorHandler("ReadProperties");

            if (propsNode.Name != "Properties")
            {
                throw error("Provided XmlNode's class should be 'Properties'!");
            }

            foreach (XmlNode propNode in propsNode.ChildNodes)
            {
                if (propNode.NodeType == XmlNodeType.Comment)
                {
                    continue;
                }

                string  propType = propNode.Name;
                XmlNode propName = propNode.Attributes.GetNamedItem("name");

                if (propName == null)
                {
                    if (propNode.Name == "Item")
                    {
                        continue;
                    }

                    throw error("Got a property node without a 'name' attribute!");
                }

                IXmlPropertyToken tokenHandler = XmlPropertyTokens.GetHandler(propType);

                if (tokenHandler != null)
                {
                    Property prop = new Property()
                    {
                        Name     = propName.InnerText,
                        Instance = instance,
                        XmlToken = propType
                    };

                    if (!tokenHandler.ReadProperty(prop, propNode))
                    {
                        Console.WriteLine("Could not read property: " + prop.GetFullName() + '!');
                    }

                    instance.AddProperty(ref prop);
                }
                else
                {
                    Console.WriteLine("No IXmlPropertyToken found for property type: " + propType + '!');
                }
            }
        }
Esempio n. 3
0
        public static XmlNode WriteSharedStrings(XmlDocument doc, XmlRobloxFile file)
        {
            XmlElement sharedStrings = doc.CreateElement("SharedStrings");

            var binaryWriter = XmlPropertyTokens.GetHandler <BinaryStringToken>();
            var binaryBuffer = new Property("SharedString", PropertyType.String);

            foreach (string key in file.SharedStrings)
            {
                XmlElement sharedString = doc.CreateElement("SharedString");
                sharedString.SetAttribute("md5", key);

                binaryBuffer.RawBuffer = SharedString.Find(key);
                binaryWriter.WriteProperty(binaryBuffer, doc, sharedString);

                sharedStrings.AppendChild(sharedString);
            }

            return(sharedStrings);
        }
        public static XmlNode WriteProperty(Property prop, XmlDocument doc, XmlRobloxFile file)
        {
            if (prop.Name == "Archivable")
            {
                return(null);
            }

            string propType = prop.XmlToken;

            if (propType == null)
            {
                propType = "";
            }

            if (propType.Length == 0)
            {
                propType = GetEnumName(prop.Type);

                switch (prop.Type)
                {
                case PropertyType.CFrame:
                case PropertyType.Quaternion:
                    propType = "CoordinateFrame";
                    break;

                case PropertyType.Enum:
                    propType = "token";
                    break;

                case PropertyType.Rect:
                    propType = "Rect2D";
                    break;

                case PropertyType.Int:
                case PropertyType.Bool:
                case PropertyType.Float:
                case PropertyType.Int64:
                case PropertyType.Double:
                    propType = propType.ToLower(CultureInfo.InvariantCulture);
                    break;

                case PropertyType.String:
                    propType = (prop.HasRawBuffer ? "BinaryString" : "string");
                    break;

                default: break;
                }
            }

            IXmlPropertyToken handler = XmlPropertyTokens.GetHandler(propType);

            if (handler == null)
            {
                if (RobloxFile.LogErrors)
                {
                    Console.Error.WriteLine("XmlDataWriter.WriteProperty: No token handler found for property type: {0}", propType);
                }

                return(null);
            }

            if (prop.Type == PropertyType.SharedString)
            {
                SharedString str = prop.CastValue <SharedString>();

                if (str == null)
                {
                    byte[] value = prop.CastValue <byte[]>();
                    str = SharedString.FromBuffer(value);
                }

                if (str.ComputedKey == null)
                {
                    var newShared = SharedString.FromBuffer(str.SharedValue);
                    str.Key = newShared.ComputedKey;
                }

                file.SharedStrings.Add(str.Key);
            }

            XmlElement propElement = doc.CreateElement(propType);

            propElement.SetAttribute("name", prop.Name);

            XmlNode propNode = propElement;

            handler.WriteProperty(prop, doc, propNode);

            if (propNode.ParentNode != null)
            {
                XmlNode newNode = propNode.ParentNode;
                newNode.RemoveChild(propNode);
                propNode = newNode;
            }

            return(propNode);
        }