Example #1
0
        /// <summary>
        /// Loads sounds from the specified directory. Accepts absolute path, path relative to
        /// GameData, and then path relative to DLL in that order
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="b2D"></param>
        /// <returns></returns>
        public int LoadSoundsFrom(string dir, bool b2D = true)
        {
            int counter = 0;

            if (Path.IsPathRooted(dir) && Directory.Exists(dir))
            {
                Log.Verbose("AudioPlayer: '{0}' seems to be a full path", dir);

                dir = Path.GetFullPath(dir).Replace('\\', '/');

                Log.Debug("AudioPlayer.LoadSoundsFrom: Path after transformation and replace: {0}", dir);
                dir = ConfigUtil.GetRelativeToGameData(dir);
            }
            else
            {
                dir = dir.TrimStart('\\', '/');

                // not a whole dir, so it could be relative to GameData such as:
                // [ksproot]/GameData/      dir = ScienceAlert/sounds
                //
                // or it could be relative to the DLL, such as:
                // sounds

                if (!Directory.Exists(Path.Combine(Path.GetFullPath(KSPUtil.ApplicationRootPath + "GameData"), dir))) // not relative to GameData
                {
                    Log.Verbose("AudioPlayer: '{0}' seems to be a plugin-relative path", dir);

                    // try relative to dll dir
                    string relDll = Path.Combine(ConfigUtil.GetDllDirectoryPath(), dir);
                    Log.Debug("trying relative path: " + relDll);

                    if (Directory.Exists(relDll))
                    {
                        dir = ConfigUtil.GetRelativeToGameData(relDll).Replace('\\', '/');
                    }
                    else
                    {
                        Log.Error("AudioPlayer: Couldn't find '{0}'", dir);
                        return(0);
                    }
                }
                else
                {
                    dir = dir.Replace('\\', '/');
                    Log.Verbose("AudioPlayer: '{0}' seems to be a GameData-relative path", dir);
                    Log.Debug("checked: " + Path.Combine(Path.GetFullPath(KSPUtil.ApplicationRootPath), dir));
                }
            }

            /*
             * [LOG 17:56:12.954] ScienceAlert, name: ScienceAlert/sounds/bubbles
             * [LOG 17:56:12.955] ScienceAlert, name: ScienceAlert/sounds/click1
             * [LOG 17:56:12.956] ScienceAlert, name: ScienceAlert/sounds/click2
             * [LOG 17:56:12.956] ScienceAlert, name: ScienceAlert/sounds/error*/

            GameDatabase.Instance.databaseAudio.ForEach(ac =>
            {
                // strip off the filename and just use the path (if any) of the clip's gamedata-relative url
                string urlDir = ac.name;
                int idx       = urlDir.LastIndexOf('/');

                if (idx >= 0)
                {
                    urlDir = urlDir.Substring(0, idx);
                }



                //if (ac.name.Contains(dir))
                if (string.Equals(urlDir, dir))
                {
                    if (sounds.ContainsKey(ac.name))
                    {
                        Log.Warning("AudioPlayer: Already have key '{0}'", ac.name);
                        return;
                    }
                    else
                    {
                        sounds.Add(ac.name, new PlayableSound(ac));
                        Log.Normal("{0} ready", ac.name);
                        ++counter;
                    }
                }
            });

            if (counter == 0)
            {
                Log.Warning("AudioPlayer: Didn't load any sounds from directory '{0}'", dir);
            }

            return(counter);
        }