コード例 #1
0
        public static UpdateSourceBase[] FindUpdateSources(string searchDirectory)
        {
            var updateSourcesPath      = Path.Combine(searchDirectory, "UpdateSources");
            var updateSourcesPathDebug = Path.Combine(searchDirectory, "UpdateSourcesDebug");

            var updateSources = new string[0];

            Console.WriteLine("Looking for update sources...");

            if (File.Exists(updateSourcesPathDebug) && File.ReadAllText(updateSourcesPathDebug).Trim().Length > 0)
            {
                Console.WriteLine("Loading sources from UpdateSourcesDebug file at " + updateSourcesPathDebug);

                updateSources = File.ReadAllLines(updateSourcesPathDebug).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
            }
            else
            {
                try
                {
                    updateSources = new WebClient().DownloadString(PathTools.AdjustFormat(Resources.UpdateSourcesUrl)).Split().Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                    Console.WriteLine($"Found {updateSources.Length} sources");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to get update sources: " + ex.Message);
                }

                // If any sources were found, save them to the cache. If not, try loading the cached sources
                if (updateSources.Any())
                {
                    File.WriteAllLines(updateSourcesPath, updateSources);
                }
                else if (File.Exists(updateSourcesPath) && File.ReadAllText(updateSourcesPath).Length > 0)
                {
                    Console.WriteLine("Loading cached sources from UpdateSources file at " + updateSourcesPath);

                    updateSources = File.ReadAllLines(updateSourcesPath).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                }
            }

            return(GetUpdateSources(updateSources.Select(PathTools.AdjustFormat).ToArray()));
        }