using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Touka
{
    public class TKGFileTools
    {
        /// 
        /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的文件
        ///
        /// only for android .properties
        /// 
        /// 文件名称
        /// 
        public static string UnityWebRequestFile(string fileName)
        {
            string url = GetFileURL(fileName);
            TKGDebugger.LogError("Warning: If an exception occurs, check whether the 「Assets/StreamingAssets/Configs/tkg_config.properties」 file exists : " + url);
            UnityWebRequest request = UnityWebRequest.Get(url);
            request.SendWebRequest();//读取数据
            while (true)
            {
                if (request.downloadHandler.isDone)//是否读取完数据
                {
                    return request.downloadHandler.text;
                }
            }
        }
        /// 
        /// get file url
        /// 
        /// 
        /// 
        public static string GetFileURL(string fileName)
        {
            string url = "";
            #region 分平台判断 StreamingAssets 路径
            //如果在编译器或者单机中
            #endregion
#if UNITY_EDITOR || UNITY_STANDALONE
            url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
            //否则如果在Iphone下
#elif UNITY_IPHONE
        url = "file://" + Application.dataPath + "/Raw/"+ fileName;
            //否则如果在android下
#elif UNITY_ANDROID
        url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
            return url;
        }
    }
}