コード例 #1
0
 /// <summary>
 /// Awake is called when the script instance is being loaded.
 /// </summary>
 void Awake()
 {
     MocksResource = mocksResourceParam;
 }
コード例 #2
0
        static void UpdateMocks()
        {
            switch (MocksResource)
            {
            case MocksResource.MEMORY:
                if (mocks == null)
                {
                    mocks = new Dictionary <string, string>();
                }
                break;

            case MocksResource.FILE:
                string filePath = Instance.mocksFilePath;
                if (!File.Exists(filePath))
                {
                    filePath = Path.Combine(Application.persistentDataPath, Instance.mocksFilePath);
                    if (!File.Exists(filePath))
                    {
                        filePath = Path.Combine(Application.streamingAssetsPath, Instance.mocksFilePath);
                        if (!File.Exists(filePath))
                        {
                            UnityWebRequest copyFile = new UnityWebRequest(filePath, "GET");
                            copyFile.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
                            copyFile.SendWebRequest();
                            while (!copyFile.isDone)
                            {
                            }
                            if (!copyFile.isHttpError && !copyFile.isNetworkError)
                            {
                                if (!string.IsNullOrEmpty(copyFile.downloadHandler.text))
                                {
                                    File.WriteAllText(Path.Combine(Application.persistentDataPath, Instance.mocksFilePath), copyFile.downloadHandler.text);
                                    filePath = Path.Combine(Application.persistentDataPath, Instance.mocksFilePath);
                                }
                            }
                        }
                    }
                }


                if (File.Exists(filePath))
                {
                    ListKeyValueMocks mocksList = JsonUtility.FromJson <ListKeyValueMocks>(File.ReadAllText(filePath));
                    if (mocks == null)
                    {
                        mocks = new Dictionary <string, string>();
                    }
                    foreach (var p in mocksList.mocks.ToDictionary((pair) => pair.Key, (pair) => pair.Value))
                    {
                        if (!p.Value.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                        {
                            string filePathRequest = p.Value;

                            if (!File.Exists(filePathRequest))
                            {
                                filePathRequest = Path.Combine(Application.persistentDataPath, p.Value);
                                if (!File.Exists(filePathRequest))
                                {
                                    filePathRequest = Path.Combine(Application.streamingAssetsPath, p.Value);
                                    if (!File.Exists(filePathRequest))
                                    {
                                        UnityWebRequest copyFile = new UnityWebRequest(filePathRequest, "GET");
                                        copyFile.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
                                        copyFile.SendWebRequest();
                                        while (!copyFile.isDone)
                                        {
                                        }
                                        if (!copyFile.isHttpError && !copyFile.isNetworkError)
                                        {
                                            File.WriteAllText(Path.Combine(Application.persistentDataPath, p.Value), copyFile.downloadHandler.text);
                                            filePathRequest = Path.Combine(Application.persistentDataPath, p.Value);
                                        }
                                    }
                                }
                            }
                            mocks[p.Key] = File.ReadAllText(filePathRequest);
                        }
                    }
                }
                else
                {
                    ToolsDebug.Log($"Can't read mock data file from: {Instance.mocksFilePath}");
                    MocksResource = MocksResource.NONE;
                }
                break;

            case MocksResource.REMOTE_FILE:
                UnityWebRequest unityWebRequest             = new UnityWebRequest(Instance.mocksFilePath, UnityWebRequest.kHttpVerbGET, new DownloadHandlerBuffer(), null);
                unityWebRequest.SendWebRequest().completed += (dh) =>
                {
                    if (!unityWebRequest.isNetworkError && !unityWebRequest.isHttpError && !string.IsNullOrEmpty(unityWebRequest.downloadHandler?.text))
                    {
                        ListKeyValueMocks mocksList = JsonUtility.FromJson <ListKeyValueMocks>(unityWebRequest.downloadHandler?.text);
                        mocks = mocksList.mocks.ToDictionary((pair) => pair.Key, (pair) => pair.Value);
                        foreach (var p in mocks)
                        {
                            if (p.Value.EndsWith("json", StringComparison.OrdinalIgnoreCase))
                            {
                                UnityWebRequest uwr             = new UnityWebRequest(p.Value, UnityWebRequest.kHttpVerbGET, new DownloadHandlerBuffer(), null);
                                uwr.SendWebRequest().completed += (dhh) =>
                                {
                                    if (!uwr.isNetworkError && !uwr.isHttpError && !string.IsNullOrEmpty(uwr.downloadHandler?.text))
                                    {
                                        mocks[p.Key] = uwr.downloadHandler?.text;
                                    }
                                };
                            }
                        }
                    }
                    else
                    {
                        ToolsDebug.Log($"Can't read mock data remote file from: {Instance.mocksFilePath}");
                        MocksResource = MocksResource.NONE;
                    }
                };
                break;

            default:
                mocks = null;
                break;
            }
        }