Esempio n. 1
0
        /// <summary>
        /// Reads all GMX timelines from a directory
        /// </summary>
        /// <param name="file">The XML (.GMX) file path</param>
        /// <returns>A list of timelines</returns>
        public static GMList <GMTimeline> ReadTimelinesGMX(string directory, List <string> assets)
        {
            // A list of timelines
            GMList <GMTimeline> timelines = new GMList <GMTimeline>();

            timelines.AutoIncrementIds = false;

            // Iterate through .gmx files in the directory
            foreach (string file in Directory.GetFiles(directory, "*.gmx"))
            {
                // Set name of the timeline
                string name = GetResourceName(file);

                // If the file is not in the asset list, it has been orphaned, continue
                if (!assets.Contains(name))
                {
                    continue;
                }

                // Create a dictionary of object properties
                Dictionary <string, string> objectProperties = new Dictionary <string, string>();
                foreach (GMXObjectProperty property in Enum.GetValues(typeof(GMXObjectProperty)))
                {
                    objectProperties.Add(GMXEnumString(property), "");
                }

                // Create a dictionary of action properties
                Dictionary <string, string> actionProperties = new Dictionary <string, string>();
                foreach (GMXActionProperty property in Enum.GetValues(typeof(GMXActionProperty)))
                {
                    actionProperties.Add(GMXEnumString(property), "");
                }

                // Create a dictionary of argument properties
                Dictionary <string, string> argumentProperties = new Dictionary <string, string>();
                foreach (GMXArgumentProperty property in Enum.GetValues(typeof(GMXArgumentProperty)))
                {
                    argumentProperties.Add(GMXEnumString(property), "");
                }

                // Local variables
                List <GMMoment> moments = new List <GMMoment>();

                // Create an xml reader
                using (XmlReader reader = XmlReader.Create(file))
                {
                    // Seek to content
                    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;

                        // Create a new moment and get it's properties
                        GMMoment moment = new GMMoment();

                        // If the element is an event
                        if (nodeName.ToLower() == GMXEnumString(GMXObjectProperty.Event).ToLower())
                        {
                            // Action list
                            List <GMAction> actions = new List <GMAction>();

                            // Seek to content
                            reader.MoveToContent();

                            // Create a reader for the actions
                            using (XmlReader reader2 = reader.ReadSubtree())
                            {
                                // Argument list
                                List <GMArgument> arguments = new List <GMArgument>();

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

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

                                    // If the node is an argument
                                    if (nodeName2.ToLower() == EnumString.GetEnumString(GMXObjectProperty.Argument).ToLower())
                                    {
                                        // Seek to content
                                        reader2.MoveToContent();

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

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

                                                // Read element
                                                reader3.Read();

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

                                                // Set the property value
                                                argumentProperties[nodeName3] = reader3.Value;
                                            }

                                            // Create a new argument
                                            GMArgument argument = new GMArgument();
                                            argument.Type  = GMXInt(argumentProperties[GMXEnumString(GMXArgumentProperty.Kind)], argument.Type);
                                            argument.Value = GMXString(argumentProperties[GMXEnumString(GMXArgumentProperty.String)], argument.Value);

                                            // Add argument to the list
                                            arguments.Add(argument);
                                        }
                                    }

                                    // Read element
                                    reader2.Read();

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

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

                                // Create a new action
                                GMAction action = new GMAction();
                                action.LibraryId     = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.LibId)], action.LibraryId);
                                action.ActionId      = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.Id)], action.ActionId);
                                action.ActionKind    = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.Kind)], action.ActionKind);
                                action.AllowRelative = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.UseRelative)], action.AllowRelative);
                                action.Question      = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.IsQuestion)], action.Question);
                                action.CanApplyTo    = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.UseApplyTo)], action.CanApplyTo);
                                action.ExecuteMode   = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.ExeType)], action.ExecuteMode);
                                action.FunctionName  = GMXString(actionProperties[GMXEnumString(GMXActionProperty.FunctionName)], action.FunctionName);
                                action.ExecuteCode   = GMXString(actionProperties[GMXEnumString(GMXActionProperty.CodeString)], action.ExecuteCode);
                                action.AppliesToName = GMXString(actionProperties[GMXEnumString(GMXActionProperty.WhoName)], action.AppliesToName);
                                action.AppliesTo     = action.AppliesToName == "" ? -1 : GetIdFromName(action.AppliesToName);
                                action.Relative      = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.Relative)], action.Relative);
                                action.Not           = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.IsNot)], action.Not);
                                action.Arguments     = arguments.ToArray();

                                // Add action to the list
                                actions.Add(action);
                            }

                            // Set the events actions
                            moment.Actions = actions.ToArray();
                            moments.Add(moment);
                        }

                        if (nodeName.ToLower() == "step")
                        {
                            // Read element
                            reader.Read();

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

                            moment.StepIndex = GMXInt(reader.Value, moment.StepIndex);
                            moments.Add(moment);
                        }

                        // Set the property value
                        objectProperties[nodeName] = reader.Value;
                    }
                }

                // Create a new timeline, set properties
                GMTimeline timeline = new GMTimeline();
                timeline.Moments = moments.ToArray();

                // Add the timeline
                timelines.Add(timeline);
            }

            // Return the list of timelines
            return(timelines);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads all GMX objects from a directory
        /// </summary>
        /// <param name="file">The XML (.GMX) file path</param>
        /// <returns>A list of objects</returns>
        public static GMList <GMObject> ReadObjectsGMX(string directory, ref List <string> assets)
        {
            // A list of objects
            GMList <GMObject> objects = new GMList <GMObject>();

            objects.AutoIncrementIds = false;

            // Iterate through .gmx files in the directory
            foreach (string file in Directory.GetFiles(directory, "*.gmx"))
            {
                // Set name of the object
                string name = GetResourceName(file);

                // If the file is not in the asset list, it has been orphaned, continue
                if (!assets.Contains(name))
                {
                    continue;
                }

                // Create a dictionary of object properties
                Dictionary <string, string> objectProperties = new Dictionary <string, string>();
                foreach (GMXObjectProperty property in Enum.GetValues(typeof(GMXObjectProperty)))
                {
                    objectProperties.Add(GMXEnumString(property), "");
                }

                // Create a dictionary of action properties
                Dictionary <string, string> actionProperties = new Dictionary <string, string>();
                foreach (GMXActionProperty property in Enum.GetValues(typeof(GMXActionProperty)))
                {
                    actionProperties.Add(GMXEnumString(property), "");
                }

                // Create a dictionary of argument properties
                Dictionary <string, string> argumentProperties = new Dictionary <string, string>();
                foreach (GMXArgumentProperty property in Enum.GetValues(typeof(GMXArgumentProperty)))
                {
                    argumentProperties.Add(GMXEnumString(property), "");
                }

                // Local variables
                List <GMEvent>[] events        = new List <GMEvent> [12];
                List <GMPoint>   physicsPoints = new List <GMPoint>();

                // Create an xml reader
                using (XmlReader reader = XmlReader.Create(file))
                {
                    // Seek to content
                    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 the element is an event
                        if (nodeName.ToLower() == GMXEnumString(GMXObjectProperty.Event).ToLower())
                        {
                            // Create a new event and get it's properties
                            GMEvent gmEvent = new GMEvent();
                            int     type    = GMXInt(reader.GetAttribute(GMXEnumString(GMXEventProperty.Eventtype)), gmEvent.MainType);
                            gmEvent.MainType = type;

                            // If the event is a collision event, set the other name, else use subtype value
                            if (gmEvent.MainType == (int)EventType.Collision)
                            {
                                gmEvent.OtherName = GMXString(reader.GetAttribute(GMXEnumString(GMXEventProperty.EName)), gmEvent.OtherName);
                            }
                            else
                            {
                                gmEvent.SubType = GMXInt(reader.GetAttribute(GMXEnumString(GMXEventProperty.ENumb)), gmEvent.SubType);
                            }

                            // Action list
                            List <GMAction> actions = new List <GMAction>();

                            // Seek to content
                            reader.MoveToContent();

                            // Create a reader for the actions
                            using (XmlReader reader2 = reader.ReadSubtree())
                            {
                                // Argument list
                                List <GMArgument> arguments = new List <GMArgument>();

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

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

                                    // If the node is an argument
                                    if (nodeName2.ToLower() == EnumString.GetEnumString(GMXObjectProperty.Argument).ToLower())
                                    {
                                        // Seek to content
                                        reader2.MoveToContent();

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

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

                                                // Read element
                                                reader3.Read();

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

                                                // Set the property value
                                                argumentProperties[nodeName3] = reader3.Value;
                                            }

                                            // Create a new argument
                                            GMArgument argument = new GMArgument();
                                            argument.Type  = GMXInt(argumentProperties[GMXEnumString(GMXArgumentProperty.Kind)], argument.Type);
                                            argument.Value = GMXString(argumentProperties[GMXEnumString(GMXArgumentProperty.String)], argument.Value);

                                            // Add argument to the list
                                            arguments.Add(argument);
                                        }
                                    }

                                    // Read element
                                    reader2.Read();

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

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

                                // Create a new action
                                GMAction action = new GMAction();
                                action.LibraryId     = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.LibId)], action.LibraryId);
                                action.ActionId      = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.Id)], action.ActionId);
                                action.ActionKind    = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.Kind)], action.ActionKind);
                                action.AllowRelative = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.UseRelative)], action.AllowRelative);
                                action.Question      = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.IsQuestion)], action.Question);
                                action.CanApplyTo    = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.UseApplyTo)], action.CanApplyTo);
                                action.ExecuteMode   = GMXInt(actionProperties[GMXEnumString(GMXActionProperty.ExeType)], action.ExecuteMode);
                                action.FunctionName  = GMXString(actionProperties[GMXEnumString(GMXActionProperty.FunctionName)], action.FunctionName);
                                action.ExecuteCode   = GMXString(actionProperties[GMXEnumString(GMXActionProperty.CodeString)], action.ExecuteCode);
                                action.AppliesToName = GMXString(actionProperties[GMXEnumString(GMXActionProperty.WhoName)], action.AppliesToName);
                                action.AppliesTo     = GetIdFromName(action.AppliesToName);
                                action.Relative      = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.Relative)], action.Relative);
                                action.Not           = GMXBool(actionProperties[GMXEnumString(GMXActionProperty.IsNot)], action.Not);
                                action.Arguments     = arguments.ToArray();

                                // Add action to the list
                                actions.Add(action);
                            }

                            // Set the events actions
                            gmEvent.Actions = actions.ToArray();

                            // If the event type list has not been generated, create it
                            if (events[type] == null)
                            {
                                events[type] = new List <GMEvent>();
                            }

                            // Add the event of the event type
                            events[type].Add(gmEvent);
                        }

                        // Read element
                        reader.Read();

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

                        // Set the property value
                        objectProperties[nodeName] = reader.Value;
                    }
                }

                // Create a new object and set its properties
                GMObject obj = new GMObject();
                obj.Id         = GetIdFromName(name);
                obj.Name       = name;
                obj.SpriteName = GMXString(objectProperties[GMXEnumString(GMXObjectProperty.SpriteName)], obj.SpriteName);
                obj.SpriteId   = GetIdFromName(obj.SpriteName);
                obj.Solid      = GMXBool(objectProperties[GMXEnumString(GMXObjectProperty.Solid)], obj.Solid);
                obj.Visible    = GMXBool(objectProperties[GMXEnumString(GMXObjectProperty.Visible)], obj.Visible);
                obj.Depth      = GMXInt(objectProperties[GMXEnumString(GMXObjectProperty.Depth)], obj.Depth);
                obj.Persistent = GMXBool(objectProperties[GMXEnumString(GMXObjectProperty.Persistent)], obj.Persistent);
                obj.ParentName = GMXString(objectProperties[GMXEnumString(GMXObjectProperty.ParentName)], obj.ParentName);
                obj.Parent     = GetIdFromName(obj.ParentName);
                obj.MaskName   = GMXString(objectProperties[GMXEnumString(GMXObjectProperty.MaskName)], obj.MaskName);
                obj.Mask       = GetIdFromName(obj.MaskName);
                obj.Events     = events;

                // Add the object
                objects.Add(obj);
            }

            // Return the list of objects
            return(objects);
        }