Exemplo n.º 1
0
        /// <summary>
        /// Reads all sounds from Game Maker project file stream.
        /// </summary>
        public static GMList <GMSound> ReadSounds(GMFileReader reader)
        {
            // Get version.
            int version = reader.ReadGMInt();

            // Check version.
            if (version != 400 && version != 800)
            {
                throw new Exception("Unsupported Pre-Sound object version.");
            }

            // Create a new sound list.
            GMList <GMSound> sounds = new GMList <GMSound>();

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

            // Iterate through sounds.
            for (int i = 0; i < num; i++)
            {
                // If version is 8.0, start inflate.
                if (version == 800)
                {
                    reader.Decompress();
                }

                // If the sound at index does not exists, continue.
                if (reader.ReadGMBool() == false)
                {
                    sounds.LastId++;
                    reader.EndDecompress();
                    continue;
                }

                // Create sound object.
                GMSound sound = new GMSound();

                // Set sound id.
                sound.Id = i;

                // Read sound data.
                sound.Name = reader.ReadGMString();

                // If version is 8.0, get last changed.
                if (version == 800)
                {
                    sound.LastChanged = reader.ReadGMDouble();
                }

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

                // Check version.
                if (version != 440 && version != 600 && version != 800)
                {
                    throw new Exception("Unsupported Sound object version.");
                }

                // Check version.
                if (version == 440)
                {
                    sound.Kind = reader.ReadGMInt();
                }
                else
                {
                    // Read sound data.
                    sound.Type = reader.ReadGMInt();
                }

                // Read sound data.
                sound.FileType = reader.ReadGMString();

                // Check version.
                if (version == 440)
                {
                    // If sound data exists, read it.
                    if ((int)sound.Kind != -1)
                    {
                        sound.Data = reader.ReadGMBytes(reader.ReadGMInt());
                    }

                    // Read sound data.
                    sound.AllowSoundEffects = reader.ReadGMBool();
                    sound.Buffers           = reader.ReadGMInt();
                    sound.LoadOnlyOnUse     = reader.ReadGMBool();
                }
                else
                {
                    // Read sound data.
                    sound.FileName = reader.ReadGMString();

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

                    // Read sound data.
                    sound.Effects = reader.ReadGMInt();
                    sound.Volume  = reader.ReadGMDouble();
                    sound.Pan     = reader.ReadGMDouble();
                    sound.Preload = reader.ReadGMBool();
                }

                // End object inflate.
                reader.EndDecompress();

                // Add sound.
                sounds.Add(sound);
            }

            // Return sounds.
            return(sounds);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads all GMX sounds from a directory
        /// </summary>
        /// <param name="file">The XML (.GMX) file path</param>
        /// <returns>A list of sounds</returns>
        public static GMList <GMSound> ReadSoundsGMX(string directory, ref List <string> assets)
        {
            // A list of sounds
            GMList <GMSound> sounds = new GMList <GMSound>();

            sounds.AutoIncrementIds = false;

            // Iterate through .gmx files in the directory
            foreach (string file in Directory.GetFiles(directory, "*.gmx"))
            {
                // Set name of the sound
                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 sound properties
                Dictionary <string, string> properties = new Dictionary <string, string>();
                foreach (GMXSoundProperty property in Enum.GetValues(typeof(GMXSoundProperty)))
                {
                    properties.Add(GMXEnumString(property), "");
                }

                // 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;
                        }

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

                // Create a new sound, set properties
                GMSound sound = new GMSound();
                sound.Id               = GetIdFromName(name);
                sound.Name             = name;
                sound.Kind             = GMXInt(properties[GMXEnumString(GMXSoundProperty.Kind)], sound.Kind);
                sound.Extension        = GMXString(properties[GMXEnumString(GMXSoundProperty.Extension)], sound.Extension);
                sound.FilePath         = GMXString(properties[GMXEnumString(GMXSoundProperty.Origname)], sound.FilePath);
                sound.Effects          = GMXInt(properties[GMXEnumString(GMXSoundProperty.Effects)], sound.Effects);
                sound.Volume           = GMXDouble(properties[GMXEnumString(GMXSoundProperty.Volume)], sound.Volume);
                sound.Pan              = GMXDouble(properties[GMXEnumString(GMXSoundProperty.Pan)], sound.Pan);
                sound.OGGQuality       = GMXInt(properties[GMXEnumString(GMXSoundProperty.OggQuality)], sound.OGGQuality);
                sound.Preload          = GMXBool(properties[GMXEnumString(GMXSoundProperty.Preload)], sound.Preload);
                sound.FileName         = GMXString(properties[GMXEnumString(GMXSoundProperty.Data)], sound.FileName);
                sound.SampleRate       = GMXInt(properties[GMXEnumString(GMXSoundProperty.SampleRate)], sound.SampleRate);
                sound.Type             = GMXInt(properties[GMXEnumString(GMXSoundProperty.Type)], sound.Type);
                sound.BitDepth         = GMXInt(properties[GMXEnumString(GMXSoundProperty.BitDepth)], sound.BitDepth);
                sound.Streamed         = GMXBool(properties[GMXEnumString(GMXSoundProperty.Streamed)], sound.Streamed);
                sound.UncompressOnLoad = GMXBool(properties[GMXEnumString(GMXSoundProperty.UncompressOnLoad)], sound.UncompressOnLoad);
                sound.Compressed       = GMXBool(properties[GMXEnumString(GMXSoundProperty.Compressed)], sound.Compressed);

                // Set bit rate
                if (properties.ContainsKey(GMXEnumString(GMXSoundProperty.BitRate)))
                {
                    sound.BitRate = GMXInt(properties[GMXEnumString(GMXSoundProperty.BitRate)], sound.BitRate);
                }
                else if (properties.ContainsKey(GMXEnumString(GMXSoundProperty.Mp3BitRate)))
                {
                    sound.BitRate = GMXInt(properties[GMXEnumString(GMXSoundProperty.Mp3BitRate)], sound.BitRate);
                }

                // Add the sound
                sounds.Add(sound);
            }

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