/// <summary> /// Determines what kind of role the node has /// </summary> /// <param name="node">The given xml node to check</param> /// <returns>If the node is a parent, group, or child</returns> private static GMNodeType GetNodeType(XmlNode node) { // If the node is a text type, it carries a value so it is a child node // Else if the node's parent is the asset node, it is major resource parent node // Else it is a group node return(node.NodeType == XmlNodeType.Text ? GMNodeType.Child : node.ParentNode.Name == EnumString.GetEnumString(GMResourceType.Assets) ? GMNodeType.Parent : GMNodeType.Group); }
/// <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); }
/// <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); }
/// <summary> /// Gets the GMX property string for the given string enum /// </summary> /// <param name="e">The numeration to extract the property srting from</param> /// <returns>A string representation of an enumeration elelment</returns> public static string GMXEnumString(Enum e) { return(EnumString.GetEnumString(e)); }
/// <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); }