/// <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> /// Reads timelines from GM file. /// </summary> public static GMList <GMTimeline> ReadTimelines(GMFileReader reader) { // Get version. int version = reader.ReadGMInt(); // Check version. if (version != 500 && version != 800) { throw new Exception("Unsupported Pre-Timeline object version."); } // Create a new timeline list. GMList <GMTimeline> timelines = new GMList <GMTimeline>(); // Amount of timeline ids. int num = reader.ReadGMInt(); // Iterate through timelines for (int i = 0; i < num; i++) { // If version is 8.0, start inflate. if (version == 800) { reader.Decompress(); } // If the timeline at index does not exists, continue. if (reader.ReadGMBool() == false) { timelines.LastId++; reader.EndDecompress(); continue; } // Create a new timeline object. GMTimeline timeline = new GMTimeline(); // Set timeline id. timeline.Id = i; // Read timeline data. timeline.Name = reader.ReadGMString(); // If version is 8.0, get last changed. if (version == 800) { timeline.LastChanged = reader.ReadGMDouble(); } // Get version. int version2 = reader.ReadGMInt(); // Check version. if (version2 != 500) { throw new Exception("Unsupported Timeline object version."); } // Get number of timeline moments. timeline.Moments = new GMMoment[reader.ReadGMInt()]; // Iterate through moments. for (int j = 0; j < timeline.Moments.Length; j++) { // Create a new timeline moment object. GMMoment moment = new GMMoment(); // Moment step number moment.StepIndex = reader.ReadGMInt(); // Read moment actions. moment.Actions = GMAction.ReadActions(reader); // Add moment to timeline. timeline.Moments[j] = moment; } // End object inflate. reader.EndDecompress(); // Add timeline. timelines.Add(timeline); } // Return timelines. return(timelines); }
/// <summary> /// Reads objects from GM file /// </summary> public static GMList <GMObject> ReadObjects(GMFileReader reader) { // Get version int version = reader.ReadGMInt(); // Check version if (version != 400 && version != 800) { throw new Exception("Unsupported Pre-Object object version."); } // The number of objects GMList <GMObject> objects = new GMList <GMObject>(); // Amount of object ids int num = reader.ReadGMInt(); // Iterate through objects for (int i = 0; i < num; i++) { // If version is 8.0, start inflate if (version == 800) { reader.Decompress(); } // If the object at index does not exists, continue if (reader.ReadGMBool() == false) { objects.LastId++; reader.EndDecompress(); continue; } // Create a new script object GMObject obj = new GMObject(); // Set script id obj.Id = i; // Read script data. obj.Name = reader.ReadGMString(); // If version is 8.0, get last changed if (version == 800) { obj.LastChanged = reader.ReadGMDouble(); } // Get version int version2 = reader.ReadGMInt(); // Check version if (version2 != 430) { throw new Exception("Unsupported Object object version."); } // Read object data obj.SpriteId = reader.ReadGMInt(); obj.Solid = reader.ReadGMBool(); obj.Visible = reader.ReadGMBool(); obj.Depth = reader.ReadGMInt(); obj.Persistent = reader.ReadGMBool(); obj.Parent = reader.ReadGMInt(); obj.Mask = reader.ReadGMInt(); // Skip data reader.ReadGMBytes(4); // The amount of main event types int amount = 11; // If version 8.0, there are 12 main event types if (version == 800) { amount = 12; } // Iterate through events for (int j = 0; j < amount; j++) { // Not done processing bool done = false; // While not done processing events while (!done) { // Check for event int eventNum = reader.ReadGMInt(); // If the event exists if (eventNum != -1) { // Create new event GMEvent ev = new GMEvent(); // Set type of event ev.MainType = j; // If a collision type of event set other object id if (ev.MainType == (int)EventType.Collision) { ev.OtherId = eventNum; } else { ev.SubType = eventNum; } // Read event actions ev.Actions = GMAction.ReadActions(reader); // Add new event obj.Events[j].Add(ev); } else { done = true; } } } // End object inflate reader.EndDecompress(); // Add object to list objects.Add(obj); } // Return objects return(objects); }
/// <summary> /// Reads all actions from a GM file reader stream. /// </summary> public static GMAction[] ReadActions(GMFileReader reader) { // Get version. int version = reader.ReadGMInt(); // Check version. if (version != 400) { throw new Exception("Unsupported Pre-Action object version."); } // Create an array of actions. GMAction[] actions = new GMAction[reader.ReadGMInt()]; // Iterate through actions. for (int i = 0; i < actions.Length; i++) { // Create new action actions[i] = new GMAction(); // Get version. version = reader.ReadGMInt(); // Check version. if (version != 440) { throw new Exception("Unsupported Action object version."); } // Get action properties. actions[i].LibraryId = reader.ReadGMInt(); actions[i].ActionId = reader.ReadGMInt(); actions[i].ActionKind = reader.ReadGMInt(); actions[i].AllowRelative = reader.ReadGMBool(); actions[i].Question = reader.ReadGMBool(); actions[i].CanApplyTo = reader.ReadGMBool(); actions[i].ExecuteMode = reader.ReadGMInt(); // If the execute mode is a prefabbed function. if (actions[i].ExecuteMode == (int)ExecutionType.Function) { actions[i].ExecuteCode = reader.ReadGMString(); } else { reader.ReadGMBytes(reader.ReadGMInt()); } // If the execute mode is a script. if (actions[i].ExecuteMode == (int)ExecutionType.Code) { actions[i].ExecuteCode = reader.ReadGMString(); } else { reader.ReadGMBytes(reader.ReadGMInt()); } // Create an array of arguments. actions[i].Arguments = new GMArgument[reader.ReadGMInt()]; // Number of argument types. int[] argTypes = new int[reader.ReadGMInt()]; // Iterate through argument types for (int j = 0; j < argTypes.Length; j++) { // Read in argument type. argTypes[j] = reader.ReadGMInt(); } // Read action data. actions[i].AppliesTo = reader.ReadGMInt(); actions[i].Relative = reader.ReadGMBool(); // Get actual number of arguments, most likely 8. int argsNum = reader.ReadGMInt(); // Iterate through argument types. for (int k = 0; k < argsNum; k++) { // If the index is greater than or equal to the number of arguments, continue. if (k >= actions[i].Arguments.Length) { reader.ReadGMBytes(reader.ReadGMInt()); continue; } // Create a new argument object. actions[i].Arguments[k] = new GMArgument(); // Set what kind of argument. actions[i].Arguments[k].Type = argTypes[k]; // Resource value. actions[i].Arguments[k].Value = reader.ReadGMString(); } // If not checkbox has been checked. actions[i].Not = reader.ReadGMBool(); } // Return action object. return(actions); }
/// <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); }