Exemple #1
0
        protected IEnumerator GetServersFromRepositories()
        {
            Plugin.log.Warn("Starting GetServersFromRepositories");
            if (Config.Instance?.ServerRepositories == null)
            {
                yield break;
            }
            List <RepositoryServer> repoServers = new List <RepositoryServer>();
            int repositoriesUsed = 0;
            int serversAdded     = 0;

            foreach (string serverRepoPath in Config.Instance.ServerRepositories)
            {
                Uri repoUri = null;
                try
                {
                    repoUri = new Uri(serverRepoPath, UriKind.Absolute);
                }
                catch (Exception ex)
                {
                    Plugin.log.Warn($"Invalid server repository URL: {serverRepoPath}");
                    Plugin.log.Debug(ex);
                    continue;
                }
                UnityWebRequest www = UnityWebRequest.Get(repoUri);
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Plugin.log.Warn($"Error getting Server Repository: {serverRepoPath}");
                    Plugin.log.Debug(www.error);
                }
                else
                {
                    string serverRepoJsonStr = www.downloadHandler.text;
                    try
                    {
                        ServerRepository repo           = ServerRepository.FromJson(serverRepoJsonStr);
                        bool             repositoryUsed = false;
                        foreach (var server in repo.Servers)
                        {
                            Plugin.log.Debug($"Server: {server.ToString()}");
                            if (server.IsValid)
                            {
                                repoServers.Add(server);
                                serversAdded++;
                                repositoryUsed = true;
                            }
                            else
                            {
                                Plugin.log.Warn($"Invalid server ({server.ToString()}) in repository {repo.RepositoryName}");
                            }
                        }
                        if (repositoryUsed)
                        {
                            repositoriesUsed++;
                        }
                    }
                    catch (Exception ex)
                    {
                        Plugin.log.Warn($"Error parsing ServerRepository from {serverRepoPath}");
                    }
                }
            }
            if (serversAdded > 0)
            {
                RepositoryServers = repoServers.ToArray();
                Plugin.log.Debug($"Finished getting {(serversAdded == 1 ? $"{serversAdded} server" : $"{serversAdded} servers")} from {(repositoriesUsed == 1 ? $"{repositoriesUsed} server" : $"{repositoriesUsed} servers")}.");
            }