Harriet.Models.Core.DirectoryNames.GetCharacterScriptDirectory C# (CSharp) Method

GetCharacterScriptDirectory() public static method

キャラのスクリプトが含まれるディレクトリへのパスを取得する
public static GetCharacterScriptDirectory ( string characterName, bool isAbsolutePath = true ) : string
characterName string キャラ名
isAbsolutePath bool 絶対パスで取得するかどうか
return string
        public static string GetCharacterScriptDirectory(string characterName, bool isAbsolutePath=true)
        {
            if (!isAbsolutePath)
            {
                return Path.Combine(CharacterDirectory, characterName, ScriptDirectory);
            }
            else
            {
                return Path.Combine(
                    Environment.CurrentDirectory,
                    CharacterDirectory, 
                    characterName, 
                    ScriptDirectory
                    );
            }

        }

Usage Example

コード例 #1
0
        /// <summary>キャラを定められた仕様にもとづいてロードする。失敗すると例外が飛んでくる</summary>
        public static IHarrietCharacter LoadCharacter(string characterName)
        {
            var engine = Python.CreateEngine();
            //名前参照にexeのディレクトリとキャラのディレクトリを追加
            var paths = engine.GetSearchPaths();

            paths.Add(Environment.CurrentDirectory);
            paths.Add(DirectoryNames.GetCharacterScriptDirectory(characterName));
            paths.Add(DirectoryNames.GetCharacterLoadedDirectory(characterName));
            engine.SetSearchPaths(paths);

            string path = GetInitializeScriptPath(characterName);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"file '{path}' was not found: this is needed to load character data");
            }
            engine.ExecuteFile(path, engine.Runtime.Globals);

            dynamic loadFunction;
            bool    result = engine.Runtime.Globals.TryGetVariable(CharacterLoadFunction, out loadFunction);

            if (!result)
            {
                throw new InvalidOperationException($"'{CharacterLoadFunction}' function does not exist in '{path}'");
            }
            PythonFunction function = loadFunction as PythonFunction;

            if (function == null)
            {
                throw new InvalidOperationException($"'{CharacterLoadFunction}' defined in '{path}' is not function");
            }

            IHarrietCharacter character = loadFunction() as IHarrietCharacter;

            if (character == null)
            {
                throw new InvalidOperationException($"{CharacterLoadFunction} result does not implements IHarrietCharacter");
            }

            return(character);
        }