/// <summary> /// Reads sprites from Game Maker project file. /// </summary> public static GMList <GMSprite> ReadSpritesGMX(string directory, ref List <string> assets) { // A list of sprites GMList <GMSprite> sprites = new GMList <GMSprite>(); sprites.AutoIncrementIds = false; // Iterate through .gmx files in the directory foreach (string file in Directory.GetFiles(directory, "*.gmx")) { // Set name of the sprite 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 sprite properties Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (GMXSpriteProperty property in Enum.GetValues(typeof(GMXSpriteProperty))) { properties.Add(GMXEnumString(property), ""); } // Local variables and texture group strings List <GMImage> subImages = new List <GMImage>(); string textureGroup = GMXEnumString(GMXSpriteProperty.TextureGroup); string textureGroup0 = GMXEnumString(GMXSpriteProperty.TextureGroup0); // 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; // Read element reader.Read(); // If the element value is null or empty, continue if (String.IsNullOrEmpty(reader.Value)) { continue; } // If the element is a frame element create subimage, else normal property if (nodeName.ToLower() == GMXEnumString(GMXSpriteProperty.Frame).ToLower()) { // Create a sub image and set the image path GMImage subImage = new GMImage(); subImage.Compressed = false; subImage.FilePath = reader.Value; subImage.Data = GMUtilities.LoadBytesFromBitmap(directory + "\\" + subImage.FilePath); subImages.Add(subImage); } else { // Set the property value properties[nodeName] = reader.Value; } } } // Create a new sprite, set properties GMSprite sprite = new GMSprite(); sprite.Id = GetIdFromName(name); sprite.Name = name; sprite.OriginX = GMXInt(properties[GMXEnumString(GMXSpriteProperty.XOrigin)], sprite.OriginX); sprite.OriginY = GMXInt(properties[GMXEnumString(GMXSpriteProperty.YOrigin)], sprite.OriginY); sprite.ShapeMode = GMXInt(properties[GMXEnumString(GMXSpriteProperty.ColKind)], sprite.ShapeMode); sprite.AlphaTolerance = GMXInt(properties[GMXEnumString(GMXSpriteProperty.ColTolerance)], sprite.AlphaTolerance); sprite.UseSeperateCollisionMasks = GMXBool(properties[GMXEnumString(GMXSpriteProperty.SepMasks)], sprite.UseSeperateCollisionMasks); sprite.BoundingBoxMode = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxMode)], sprite.BoundingBoxMode); sprite.BoundingBoxLeft = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxLeft)], sprite.BoundingBoxLeft); sprite.BoundingBoxRight = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxRight)], sprite.BoundingBoxRight); sprite.BoundingBoxTop = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxTop)], sprite.BoundingBoxTop); sprite.BoundingBoxBottom = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxBottom)], sprite.BoundingBoxBottom); sprite.TileHorizontally = GMXBool(properties[GMXEnumString(GMXSpriteProperty.HTile)], sprite.TileHorizontally); sprite.TileVertically = GMXBool(properties[GMXEnumString(GMXSpriteProperty.VTile)], sprite.TileVertically); sprite.UsedFor3D = GMXBool(properties[GMXEnumString(GMXSpriteProperty.For3D)], sprite.UsedFor3D); sprite.Width = GMXInt(properties[GMXEnumString(GMXSpriteProperty.Width)], sprite.Width); sprite.Height = GMXInt(properties[GMXEnumString(GMXSpriteProperty.Height)], sprite.Height); properties[textureGroup] = properties[textureGroup] == "" ? "0" : properties[textureGroup]; properties[textureGroup0] = properties[textureGroup0] == "" ? "0" : properties[textureGroup0]; // The texture group does not equal zero set texture group 0 to the texture group value if (properties[textureGroup] != "0") { properties[textureGroup0] = properties[textureGroup]; } // The texture group zero does not equal zero set texture group to the texture group 0 value else if (properties[textureGroup0] != "0") { properties[textureGroup] = properties[textureGroup0]; } // Create a list of texture groups List <int> textureGroups = new List <int>(); for (int i = 0; properties.ContainsKey(textureGroup + i); i++) { textureGroups.Add(Convert.ToInt32(properties[textureGroup + i])); } // Set the subimage size for all subimages foreach (GMImage image in subImages) { image.Width = sprite.Width; image.Height = sprite.Height; } sprite.TextureGroups = textureGroups.ToArray(); sprite.SubImages = subImages.ToArray(); // Add the sprite sprites.Add(sprite); } // Return the list of sprites return(sprites); }
/// <summary> /// Reads all backgrounds from a background XML file /// </summary> /// <param name="directory">The XML (.GMX) file path</param> /// <param name="assets">A list of assets listed in the project GMX</param> /// <returns>A GM background</returns> public static GMList <GMBackground> ReadBackgroundsGMX(string directory, ref List <string> assets) { // A list of backgrounds GMList <GMBackground> backgrounds = new GMList <GMBackground>(); backgrounds.AutoIncrementIds = false; // Iterate through .gmx files in the directory foreach (string file in Directory.GetFiles(directory, "*.gmx")) { // Set name of the background 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 room properties Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (GMXBackgroundProperty property in Enum.GetValues(typeof(GMXBackgroundProperty))) { properties.Add(GMXEnumString(property), ""); } // Background image and texture group strings GMImage image = null; string textureGroup = GMXEnumString(GMXBackgroundProperty.TextureGroup); string textureGroup0 = GMXEnumString(GMXBackgroundProperty.TextureGroup0); // Create an XMLReader to read in the resource elements using (XmlReader reader = XmlReader.Create(file)) { // Move to a content node reader.MoveToContent(); // Read XML 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; // Read reader.Read(); // If the element value is null or empty, continue if (string.IsNullOrEmpty(reader.Value)) { continue; } // If the element is a frame element create subimage, else normal property if (nodeName.ToLower() == GMXEnumString(GMXBackgroundProperty.Data)) { // Create an image and set the image path image = new GMImage(); image.Compressed = false; image.FilePath = reader.Value; image.Data = GMUtilities.LoadBytesFromBitmap(directory + "\\" + image.FilePath); } else { // Set the property value properties[nodeName] = reader.Value; } } } // Create a new background and set its properties GMBackground background = new GMBackground(); background.Id = GetIdFromName(name); background.Name = name; background.UseAsTileSet = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.IsTileset)], background.UseAsTileSet); background.TileWidth = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileWidth)], background.TileWidth); background.TileHeight = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileHeight)], background.TileHeight); background.HorizontalOffset = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileXOff)], background.HorizontalOffset); background.VerticalOffset = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileYOff)], background.VerticalOffset); background.HorizontalSeperation = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileHSep)], background.HorizontalSeperation); background.VerticalSeperation = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileVSep)], background.VerticalSeperation); background.TileHorizontally = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.HTile)], background.TileHorizontally); background.TileVertically = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.VTile)], background.TileVertically); background.UsedFor3D = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.For3D)], background.UsedFor3D); background.Width = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.Width)], background.Width); background.Height = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.Height)], background.Height); properties[textureGroup] = properties[textureGroup] == "" ? "0" : properties[textureGroup]; properties[textureGroup0] = properties[textureGroup0] == "" ? "0" : properties[textureGroup0]; image.Width = background.Width; image.Height = background.Height; background.Image = image; // The texture group does not equal zero set texture group 0 to the texture group value if (properties[textureGroup] != "0") { properties[textureGroup0] = properties[textureGroup]; } // The texture group zero does not equal zero set texture group to the texture group 0 value else if (properties[textureGroup0] != "0") { properties[textureGroup] = properties[textureGroup0]; } // Create a list of texture groups List <int> textureGroups = new List <int>(); for (int i = 0; properties.ContainsKey(string.Concat(textureGroup, i)); i++) { textureGroups.Add(Convert.ToInt32(properties[string.Concat(textureGroup, i)])); } background.TextureGroups = textureGroups.ToArray(); // Add the background backgrounds.Add(background); } // Return the list of backgrounds return(backgrounds); }