internal static string DownloadRemoteSettings(string managerID, BuildTarget platform, CASInitSettings settings, DependencyManager deps)
        {
            const string title = "Update CAS remote settings";

            var editorSettings = CASEditorSettings.Load();

            #region Create request URL
            #region Hash
            var managerIdBytes = new UTF8Encoding().GetBytes(managerID);
            var suffix         = new byte[] { 48, 77, 101, 68, 105, 65, 116, 73, 111, 78, 104, 65, 115, 72 };
            if (platform == BuildTarget.iOS)
            {
                suffix[0] = 49;
            }
            var sourceBytes = new byte[managerID.Length + suffix.Length];
            Array.Copy(managerIdBytes, 0, sourceBytes, 0, managerIdBytes.Length);
            Array.Copy(suffix, 0, sourceBytes, managerIdBytes.Length, suffix.Length);

            var           hashBytes   = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(sourceBytes);
            StringBuilder hashBuilder = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                hashBuilder.Append(Convert.ToString(hashBytes[i], 16).PadLeft(2, '0'));
            }
            var hash = hashBuilder.ToString().PadLeft(32, '0');
            #endregion

            var urlBuilder = new StringBuilder("https://psvpromo.psvgamestudio.com/Scr/cas.php?platform=")
                             .Append(platform == BuildTarget.Android ? 0 : 1)
                             .Append("&bundle=").Append(UnityWebRequest.EscapeURL(managerID))
                             .Append("&hash=").Append(hash)
                             .Append("&lang=").Append(SystemLanguage.English)
                             .Append("&appDev=2")
                             .Append("&appV=").Append(PlayerSettings.bundleVersion)
                             .Append("&coppa=").Append(( int )settings.defaultAudienceTagged)
                             .Append("&adTypes=").Append(( int )settings.allowedAdFlags)
                             .Append("&nets=").Append(DependencyManager.GetActiveMediationPattern(deps))
                             .Append("&orient=").Append(Utils.GetOrientationId())
                             .Append("&framework=Unity_").Append(Application.unityVersion);
            if (deps != null)
            {
                var buildCode = deps.GetInstalledBuildCode();
                if (buildCode > 0)
                {
                    urlBuilder.Append("&sdk=").Append(buildCode);
                }
            }
            if (string.IsNullOrEmpty(editorSettings.mostPopularCountryOfUsers))
            {
                urlBuilder.Append("&country=").Append("US");
            }
            else
            {
                urlBuilder.Append("&country=").Append(editorSettings.mostPopularCountryOfUsers);
            }
            if (platform == BuildTarget.Android)
            {
                urlBuilder.Append("&appVC=").Append(PlayerSettings.Android.bundleVersionCode);
            }

            #endregion

            using (var loader = UnityWebRequest.Get(urlBuilder.ToString()))
            {
                try
                {
                    loader.SendWebRequest();
                    while (!loader.isDone)
                    {
                        if (EditorUtility.DisplayCancelableProgressBar(title, managerID,
                                                                       Mathf.Repeat(( float )EditorApplication.timeSinceStartup * 0.2f, 1.0f)))
                        {
                            loader.Dispose();
                            throw new Exception("Update CAS Settings canceled");
                        }
                    }
                    if (string.IsNullOrEmpty(loader.error))
                    {
                        var content = loader.downloadHandler.text.Trim();
                        if (string.IsNullOrEmpty(content))
                        {
                            throw new Exception("ManagerID [" + managerID + "] is not registered in CAS.");
                        }

                        EditorUtility.DisplayProgressBar(title, "Write CAS settings", 0.7f);
                        var data = JsonUtility.FromJson <AdmobAppIdData>(content);
                        Utils.WriteToFile(content, Utils.GetNativeSettingsPath(platform, managerID));
                        return(data.admob_app_id);
                    }
                    throw new Exception("Server response " + loader.responseCode + ": " + loader.error);
                }
                finally
                {
                    EditorUtility.ClearProgressBar();
                }
            }
        }
        private static string UpdateRemoteSettingsAndGetAppId(CASInitSettings settings, BuildTarget platform, DependencyManager deps)
        {
            string appId = null;
            string updateSettingsError = "";

            for (int i = 0; i < settings.managersCount; i++)
            {
                var managerId = settings.GetManagerId(i);
                if (managerId == null || managerId.Length < 5)
                {
                    continue;
                }
                try
                {
                    string newAppId = DownloadRemoteSettings(managerId, platform, settings, deps);
                    if (!string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(newAppId))
                    {
                        continue;
                    }
                    if (newAppId.Contains('~'))
                    {
                        appId = newAppId;
                        continue;
                    }
                    if (i == 0)
                    {
                        Debug.LogError(Utils.logTag + "CAS id [" + managerId +
                                       "] has an error in server settings. Please contact support!");
                    }
                }
                catch (Exception e)
                {
                    updateSettingsError = e.Message;
                }
            }
            if (!string.IsNullOrEmpty(appId) || settings.IsTestAdMode())
            {
                return(appId);
            }

            const string title          = "Update CAS remote settings";
            int          dialogResponse = 0;
            var          targetId       = settings.GetManagerId(0);

            var message = updateSettingsError +
                          "\nPlease try using a real identifier in the first place else contact support." +
                          "\n- Warning! -" +
                          "\n1. Continue build the app for release with current settings can reduce monetization revenue." +
                          "\n2. When build to testing your app, make sure you use Test Ads mode rather than live ads. " +
                          "Failure to do so can lead to suspension of your account.";

            Debug.LogError(Utils.logTag + message);
            if (!Application.isBatchMode)
            {
                dialogResponse = EditorUtility.DisplayDialogComplex(title, message,
                                                                    "Continue", "Cancel Build", "Select settings file");
            }

            if (dialogResponse == 0)
            {
                var cachePath = Utils.GetNativeSettingsPath(platform, targetId);
                if (File.Exists(cachePath))
                {
                    return(Utils.GetAdmobAppIdFromJson(File.ReadAllText(cachePath)));
                }
                return(null);
            }
            if (dialogResponse == 1)
            {
                Utils.StopBuildWithMessage("Build canceled", BuildTarget.NoTarget);
                return(null);
            }
            return(Utils.SelectSettingsFileAndGetAppId(targetId, platform));
        }