Esempio n. 1
0
        /// <summary>
        /// Reads all data files from GM file reader stream.
        /// </summary>
        public static GMList <GMDataFile> ReadDataFiles(GMFileReader reader)
        {
            // Create a new list of data files.
            GMList <GMDataFile> dataFiles = new GMList <GMDataFile>();

            // Amount of data file ids.
            int num = reader.ReadGMInt();

            // Iterate through data files.
            for (int i = 0; i < num; i++)
            {
                // If the data file at index does not exists, continue.
                if (reader.ReadGMBool() == false)
                {
                    dataFiles.LastId++;
                    continue;
                }

                // Create a new data file object.
                GMDataFile dataFile = new GMDataFile();

                // Set data file id.
                dataFile.Id = i;

                // Read data file data.
                dataFile.Name = reader.ReadGMString();

                // Get version.
                int version = reader.ReadGMInt();

                // Check version.
                if (version != 440)
                {
                    throw new Exception("Unsupported Data File object version.");
                }

                // Read data file data.
                dataFile.FileName = reader.ReadGMString();

                // If data file exists, read it in.
                if (reader.ReadGMBool())
                {
                    dataFile.Data = reader.ReadGMBytes(reader.ReadGMInt());
                }

                // Read data file data.
                dataFile.ExportMode      = (ExportType)(reader.ReadGMInt());
                dataFile.OverwriteFile   = reader.ReadGMBool();
                dataFile.FreeDataMemory  = reader.ReadGMBool();
                dataFile.RemoveAtGameEnd = reader.ReadGMBool();

                // Add data file.
                dataFiles.Add(dataFile);
            }

            // Return data files.
            return(dataFiles);
        }
Esempio n. 2
0
        /// <summary>
        /// Writes object XML recursively
        /// </summary>
        /// <param name="xmlNode">The given xml node</param>
        /// <param name="node">The given GM node to set up</param>
        /// <param name="project">Project reference</param>
        private static void WriteNodeGMX(XmlTextWriter writer, GMNode node, ref GMProject project)
        {
            // If a child, write out value and return
            if (node.NodeType == GMNodeType.Child)
            {
                // If the child has an attribute or attributes
                if (node.Tag != null)
                {
                    // Get the attribute key and value
                    KeyValuePair <string, string> attribute = (KeyValuePair <string, string>)node.Tag;

                    // If a constant, use the name attribute and the constant name key, else use dattribute
                    if (node.ResourceType == GMResourceType.Constants)
                    {
                        XMLWriteFullElement(writer, GMXEnumString(node.ResourceSubType), node.FilePath,
                                            "name", attribute.Key);
                    }
                    else
                    {
                        XMLWriteFullElement(writer, GMXEnumString(node.ResourceSubType), node.FilePath,
                                            attribute.Key, attribute.Value);
                    }
                }
                else
                {
                    // If the child is a datafile, write out properties, else write standard
                    if (node.ResourceSubType == GMResourceSubType.DataFile)
                    {
                        // Get the data file instance
                        GMDataFile dataFile = project.DataFiles.Find(d => d.Id == node.Id && d.Group == node.FilePath);

                        // If the datafile was not found, return
                        if (dataFile == null)
                        {
                            return;
                        }

                        // Write the data file
                        writer.WriteStartElement(GMXEnumString(GMResourceSubType.DataFile));
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Name), dataFile.Name);
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Exists), GetGMXBool(dataFile.Exists));
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Size), dataFile.Size.ToString());
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.ExportAction), dataFile.ExportAction.ToString());
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.ExportDir), dataFile.ExportDirectory);
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Overwrite), GetGMXBool(dataFile.OverwriteFile));
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.FreeData), GetGMXBool(dataFile.FreeDataMemory));
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.RemoveEnd), GetGMXBool(dataFile.RemoveAtGameEnd));
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Store), GetGMXBool(dataFile.Store));

                        // Write the configs for the datafile
                        writer.WriteStartElement(GMXEnumString(GMResourceType.ConfigOptions));
                        foreach (GMConfig config in dataFile.Configs)
                        {
                            writer.WriteStartElement(GMXEnumString(GMResourceSubType.Config));
                            writer.WriteAttributeString(GMXEnumString(GMXDataFileProperty.Name), config.Name);
                            XMLWriteFullElement(writer, GMXEnumString(GMXConfigProperty.CopyToMask), config.CopyToMask.ToString());
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();

                        // Finish writing the datafile
                        XMLWriteFullElement(writer, GMXEnumString(GMXDataFileProperty.Filename), dataFile.FileName);
                        writer.WriteEndElement();
                    }
                    else
                    {
                        XMLWriteFullElement(writer, GMXEnumString(node.ResourceSubType), node.FilePath);
                    }
                }
                return;
            }

            // Start writing the parent element
            writer.WriteStartElement(GMXEnumString(node.ResourceType));

            // If not a parent node and has no attribute name, write the name attribute
            if (!(node.NodeType == GMNodeType.Parent && !HasNameAttribute(node.ResourceType)))
            {
                // If a constant node, write the number instead of the name
                if (node.ResourceType == GMResourceType.Constants)
                {
                    writer.WriteAttributeString("number", node.Nodes.Length.ToString());
                }
                // If a datafile node, write the number and name attributes
                else if (node.ResourceType == GMResourceType.DataFiles)
                {
                    writer.WriteAttributeString("number", project.LastDataFileId.ToString());
                    writer.WriteAttributeString("name", node.Name);
                }
                // Just write the name
                else
                {
                    writer.WriteAttributeString("name", node.NodeType == GMNodeType.Group ? node.Name : GetParentName(node.ResourceType));
                }
            }

            // If the node has children
            if (node.Nodes != null && node.Nodes.Length > 0)
            {
                // Write all children
                for (int i = 0; i < node.Nodes.Length; i++)
                {
                    WriteNodeGMX(writer, node.Nodes[i], ref project);
                }
            }

            // End writing the parent element
            writer.WriteEndElement();
        }
Esempio n. 3
0
        /// <summary>
        /// Reads data files from GM file.
        /// </summary>
        private GMList<GMDataFile> ReadDataFiles()
        {
            // Create a new list of data files.
            GMList<GMDataFile> dataFiles = new GMList<GMDataFile>();

            // Amount of data file ids.
            int num = ReadInt();

            // Iterate through data files.
            for (int i = 0; i < num; i++)
            {
                // If the data file at index does not exists, continue.
                if (ReadBool() == false)
                {
                    dataFiles.LastId++;
                    continue;
                }

                // Create a new data file object.
                GMDataFile dataFile = new GMDataFile();

                // Set data file id.
                dataFile.Id = i;

                // Read data file data.
                dataFile.Name = ReadString();

                // Get version.
                int version = ReadInt();

                // Check version.
                if (version != 440)
                    throw new Exception("Unsupported Data File object version.");

                // Read data file data.
                dataFile.FileName = ReadString();

                // If data file exists, read it in.
                if (ReadBool())
                    dataFile.Data = ReadBytes(ReadInt());

                // Read data file data.
                dataFile.ExportMode = (ExportType)(ReadInt());
                dataFile.OverwriteFile = ReadBool();
                dataFile.FreeDataMemory = ReadBool();
                dataFile.RemoveAtGameEnd = ReadBool();

                // Add data file.
                dataFiles.Add(dataFile);
            }

            // Return data files.
            return dataFiles;
        }
Esempio n. 4
0
        /// <summary>
        /// Gets data files from project file
        /// </summary>
        /// <param name="path">File path to project file</param>
        public static GMList <GMDataFile> ReadDataFilesGMX(string path, out int lastDataFileId)
        {
            // Create a new list of data files
            GMList <GMDataFile> dataFiles = new GMList <GMDataFile>();

            dataFiles.AutoIncrementIds = false;

            // Create a dictionary of data file properties
            Dictionary <string, string> properties = new Dictionary <string, string>();

            foreach (GMXDataFileProperty property in Enum.GetValues(typeof(GMXDataFileProperty)))
            {
                properties.Add(GMXEnumString(property), "");
            }

            // Group this data file belongs to, needed because files can have duplicate names
            string group = "";

            lastDataFileId = -1;

            // List of configs
            List <GMConfig> configs = new List <GMConfig>();

            // Create an xml reader
            using (XmlReader reader = XmlReader.Create(path))
            {
                reader.MoveToContent();

                // Read the GMX file
                while (reader.Read())
                {
                    // If the node is not an element, continue
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    // Get the element name
                    string nodeName = reader.Name;

                    // If a datafiles parent or group
                    if (nodeName.ToLower() == GMXEnumString(GMResourceType.DataFiles))
                    {
                        // If the last data file id has not been set, do so now
                        if (lastDataFileId == -1)
                        {
                            int.TryParse(reader.GetAttribute("number"), out lastDataFileId);
                        }

                        // Set the group the datafile belongs to
                        group = reader.GetAttribute("name");
                    }

                    // If the node is a data file, read it in
                    if (nodeName.ToLower() == GMXEnumString(GMResourceSubType.DataFile))
                    {
                        // Seek to content
                        reader.MoveToContent();

                        // Create an xml reader
                        using (XmlReader reader2 = reader.ReadSubtree())
                        {
                            // Read in data file properties
                            while (reader2.Read())
                            {
                                // If the node is not an element, continue
                                if (reader2.NodeType != XmlNodeType.Element)
                                {
                                    continue;
                                }

                                // Get the element name
                                string nodeName2 = reader2.Name;

                                // If the node is a data file, read it in
                                if (nodeName2.ToLower() == GMXEnumString(GMResourceType.ConfigOptions).ToLower())
                                {
                                    reader2.MoveToContent();

                                    // Create an xml reader
                                    using (XmlReader reader3 = reader2.ReadSubtree())
                                    {
                                        // Read in data file properties
                                        while (reader3.Read())
                                        {
                                            // If the node is not an element, continue
                                            if (reader3.NodeType != XmlNodeType.Element)
                                            {
                                                continue;
                                            }

                                            // Get the element name
                                            string nodeName3 = reader3.Name;

                                            // If the node is a data file, read it in
                                            if (nodeName3.ToLower() == GMXEnumString(GMResourceSubType.Config).ToLower())
                                            {
                                                GMConfig config = new GMConfig();
                                                config.Name = GMXString(reader3.GetAttribute(GMXEnumString(GMXConfigProperty.Name)), config.Name);
                                                config.Id   = GetIdFromName(config.Name);

                                                reader3.MoveToContent();

                                                // Create an xml reader
                                                using (XmlReader reader4 = reader3.ReadSubtree())
                                                {
                                                    // Read in data file properties
                                                    while (reader4.Read())
                                                    {
                                                        // If the node is not an element, continue
                                                        if (reader4.NodeType != XmlNodeType.Element)
                                                        {
                                                            continue;
                                                        }

                                                        // Get the element name
                                                        string nodeName4 = reader4.Name;

                                                        // If the node is a data file, read it in
                                                        if (nodeName4.ToLower() == GMXEnumString(GMXConfigProperty.CopyToMask).ToLower())
                                                        {
                                                            // Read element
                                                            reader4.Read();

                                                            // If the element value is null or empty, continue
                                                            if (String.IsNullOrEmpty(reader4.Value))
                                                            {
                                                                continue;
                                                            }

                                                            // Finally read the config file data
                                                            config.CopyToMask = GMXString(reader4.Value, config.CopyToMask);
                                                            configs.Add(config);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                // Read element
                                reader2.Read();

                                // If the element value is null or empty, continue
                                if (String.IsNullOrEmpty(reader2.Value))
                                {
                                    continue;
                                }

                                // Set the property value
                                properties[nodeName2] = reader2.Value;
                            }

                            // Create a new data file
                            GMDataFile dataFile = new GMDataFile();
                            dataFile.Name            = GMXString(properties[GMXEnumString(GMXDataFileProperty.Name)], dataFile.Name);
                            dataFile.Id              = GetIdFromName(dataFile.Name);
                            dataFile.Exists          = GMXBool(properties[GMXEnumString(GMXDataFileProperty.Exists)], dataFile.Exists);
                            dataFile.Size            = GMXInt(properties[GMXEnumString(GMXDataFileProperty.Size)], dataFile.Size);
                            dataFile.ExportAction    = GMXInt(properties[GMXEnumString(GMXDataFileProperty.ExportAction)], dataFile.ExportAction);
                            dataFile.ExportDirectory = GMXString(properties[GMXEnumString(GMXDataFileProperty.ExportDir)], dataFile.ExportDirectory);
                            dataFile.OverwriteFile   = GMXBool(properties[GMXEnumString(GMXDataFileProperty.Overwrite)], dataFile.OverwriteFile);
                            dataFile.FreeDataMemory  = GMXBool(properties[GMXEnumString(GMXDataFileProperty.FreeData)], dataFile.FreeDataMemory);
                            dataFile.RemoveAtGameEnd = GMXBool(properties[GMXEnumString(GMXDataFileProperty.RemoveEnd)], dataFile.RemoveAtGameEnd);
                            dataFile.Store           = GMXBool(properties[GMXEnumString(GMXDataFileProperty.Store)], dataFile.Store);
                            dataFile.FileName        = GMXString(properties[GMXEnumString(GMXDataFileProperty.Filename)], dataFile.FileName);
                            dataFile.Group           = group == "" ? EnumString.GetEnumString(GMResourceType.DataFiles) : group;
                            dataFile.Configs         = configs.ToArray();
                            configs.Clear();
                            group = "";

                            // Add data file to the list
                            dataFiles.Add(dataFile);
                        }
                    }
                }
            }

            // Return the list of data files
            return(dataFiles);
        }