Exemplo n.º 1
0
        /// <summary>
        /// Fetches the specified sound accessory
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AccessorySoundData GetSoundFile(SoundLocation type)
        {
            // Check cache
            switch (type)
            {
            case SoundLocation.Interior: if (Interior != null)
                {
                    return(Interior);
                }
                break;

            case SoundLocation.Exterior: if (Exterior != null)
                {
                    return(Exterior);
                }
                break;
            }

            // Load the file entry from the archive
            var entry = Archive.GetEntry(type == SoundLocation.Interior ? "interior.sii" : "exterior.sii");

            if (entry == null)
            {
                return(null);
            }

            // Parse the sii file
            var document = new SiiDocument(typeof(AccessorySoundData), typeof(SoundData), typeof(SoundEngineData));

            using (StreamReader reader = new StreamReader(entry.Open()))
                document.Load(reader.ReadToEnd().Trim());

            // return the main object
            var key  = new List <string>(document.Definitions.Keys).First();
            var item = document.GetDefinition <AccessorySoundData>(key);

            // Cache the sound data
            if (type == SoundLocation.Interior)
            {
                Interior = item;
            }
            else
            {
                Exterior = item;
            }

            // return
            return(item);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Imports the sound accessory objects into the AppDatabase
        /// </summary>
        /// <param name="db">An open AppData.db connection</param>
        /// <param name="package">The <see cref="SoundPackage"/> that will own these accessory objects</param>
        /// <param name="data">The accessory data object</param>
        /// <param name="location">The sound type</param>
        protected void ImportSounds(
            AppDatabase db,
            SoundPackage package,
            AccessorySoundData data,
            SoundLocation location,
            List <PropertyInfo> properties,
            List <string> files)
        {
            // Using reflection, we will now loop through each property
            // with the SoundAttribute attribute, and create an EngineSound
            // entity using that data.
            foreach (var prop in properties)
            {
                // Define local vars
                SoundAttribute attr = prop.GetCustomAttribute <SoundAttributeAttribute>().Attribute;
                SoundInfo      info = SoundInfo.Attributes[attr];

                // Skip if wrong sound type
                if (info.SoundType != Type)
                {
                    continue;
                }

                if (Type == SoundType.Engine)
                {
                    if (info.IsArray)
                    {
                        if (info.IsEngineSoundData)
                        {
                            var values = ((SoundEngineData[])prop.GetValue(data) ?? new SoundEngineData[] { });
                            foreach (var sound in values)
                            {
                                db.EngineSounds.Add(new EngineSound(sound, attr, location)
                                {
                                    PackageId = package.Id
                                });
                                files.Add(sound.Name);
                            }
                        }
                        else
                        {
                            var values = ((SoundData[])prop.GetValue(data) ?? new SoundData[] { });
                            foreach (var sound in values)
                            {
                                db.EngineSounds.Add(new EngineSound(sound, attr, location)
                                {
                                    PackageId = package.Id
                                });
                                files.Add(sound.Name);
                            }
                        }
                    }
                    else
                    {
                        if (info.IsEngineSoundData)
                        {
                            var sound = (SoundEngineData)prop.GetValue(data);
                            if (sound != null)
                            {
                                db.EngineSounds.Add(new EngineSound(sound, attr, location)
                                {
                                    PackageId = package.Id
                                });
                                files.Add(sound.Name);
                            }
                        }
                        else
                        {
                            var sound = (SoundData)prop.GetValue(data);
                            if (sound != null)
                            {
                                db.EngineSounds.Add(new EngineSound(sound, attr, location)
                                {
                                    PackageId = package.Id
                                });
                                files.Add(sound.Name);
                            }
                        }
                    }
                }
                else if (Type == SoundType.Truck)
                {
                    if (info.IsArray)
                    {
                        var values = ((SoundData[])prop.GetValue(data) ?? new SoundData[] { });
                        foreach (var sound in values)
                        {
                            db.TruckSounds.Add(new TruckSound(sound, attr, location)
                            {
                                PackageId = package.Id
                            });
                            files.Add(sound.Name);
                        }
                    }
                    else
                    {
                        var sound = (SoundData)prop.GetValue(data);
                        if (sound != null)
                        {
                            db.TruckSounds.Add(new TruckSound(sound, attr, location)
                            {
                                PackageId = package.Id
                            });
                            files.Add(sound.Name);
                        }
                    }
                }
            }
        }