Exemple #1
0
        public void Load()
        {
            string[] list = new string[] { };

            var repoResponse = HTTPHelpers.GetHTTPResponseStreamAsync(RepositoryPackURL);

            repoResponse.Wait();

            if (repoResponse.Result.Status == HttpStatusCode.OK)
            {
                // Start pack processing
                var tempDict        = new Dictionary <string, Item>();
                var trackableStream = new TrackableStream(repoResponse.Result.Stream);
                trackableStream.OnProgress += (long current, long total) => {
                    RepositoryProgress?.Invoke(current, repoResponse.Result.Length);
                };
                using (var reader = ReaderFactory.Open(trackableStream))
                {
                    while (reader.MoveToNextEntry())
                    {
                        if (Regex.Match(reader.Entry.Key, @"^(?:\./)?list$", RegexOptions.IgnoreCase).Success)
                        {
                            list = Regex.Replace(StreamToString(reader.OpenEntryStream()), @"[\r\n]+", "\n").Split("\n"[0]);
                        }

                        if (Regex.Match(reader.Entry.Key, @"^(?:\./)?readme.md$", RegexOptions.IgnoreCase).Success)
                        {
                            Readme = StreamToString(reader.OpenEntryStream());
                        }

                        var match = Regex.Match(reader.Entry.Key, @"^(?:\./)?([^/]+)/(extract|link|md5|sha1|readme(?:\.(?:md|txt)?)?)$", RegexOptions.IgnoreCase);
                        if (match.Success)
                        {
                            var mod      = match.Groups[1].ToString();
                            var fileName = match.Groups[2].ToString();

                            Item item;

                            if (!tempDict.TryGetValue(mod, out item))
                            {
                                item = new Item(mod);
                                tempDict.Add(mod, item);
                            }

                            switch (fileName.ToLower())
                            {
                            case "extract":
                                item.setExtract(true);
                                break;

                            case "link":
                                item.setURL(StreamToString(reader.OpenEntryStream()).Trim());
                                break;

                            case "md5":
                                item.setMD5(StreamToString(reader.OpenEntryStream()).Trim());
                                break;

                            case "sha1":
                                item.setSHA1(StreamToString(reader.OpenEntryStream()).Trim());
                                break;

                            case "readme":
                            case "readme.txt":
                            case "readme.md":
                                item.setReadme(StreamToString(reader.OpenEntryStream()).Trim(), fileName.EndsWith(".md"));
                                break;
                            }
                        }
                    }
                }

                if (list.Length == 0)
                {
                    list = tempDict.Keys.ToArray();
                }

                foreach (var key in tempDict.Keys.ToArray())
                {
                    var item = tempDict[key];
                    if (list.Contains(key))
                    {
                        Items.Add(item);
                    }
                    tempDict.Remove(key);
                }
                tempDict.Clear();
                tempDict = null;
                Items.Sort((x, y) => x.Name.CompareTo(y.Name));
                RepositoryLoaded?.Invoke(Items.ToArray());
                return;
                // End pack processing
            }

            var taskList = HTTPHelpers.GetHTTPResponseStringAsync(RepositoryListURL);

            taskList.Wait();

            list = (taskList.Result ?? "").Split("\n"[0]);

            for (int i = 0; i < list.Length; i++)
            {
                var  mod         = list[i];
                Item item        = new Item(mod);
                var  taskExtract = HTTPHelpers.GetHTTPStatusCodeAsync($"{RepositoryURL}{mod}/extract");
                var  taskURL     = HTTPHelpers.GetHTTPResponseStringAsync($"{RepositoryURL}{mod}/link");
                var  taskMD5     = HTTPHelpers.GetHTTPResponseStringAsync($"{RepositoryURL}{mod}/md5");
                var  taskSHA1    = HTTPHelpers.GetHTTPResponseStringAsync($"{RepositoryURL}{mod}/sha1");

                taskExtract.Wait();
                taskURL.Wait();
                taskMD5.Wait();
                taskSHA1.Wait();

                item.setExtract(taskExtract.Result == HttpStatusCode.OK);
                item.setURL(taskURL.Result);
                item.setMD5(taskMD5.Result);
                item.setSHA1(taskSHA1.Result);

                for (var x = 0; x < HmodReadme.readmeFiles.Length; x++)
                {
                    var taskReadme = HTTPHelpers.GetHTTPResponseStringAsync($"{RepositoryURL}{mod}/{HmodReadme.readmeFiles[x]}");

                    taskReadme.Wait();

                    if (taskReadme.Result != null)
                    {
                        item.setReadme(taskReadme.Result, HmodReadme.readmeFiles[x].EndsWith(".md"));
                        break;
                    }
                }

                Items.Add(item);
                RepositoryProgress?.Invoke(i + 1, list.Length);
            }
            RepositoryLoaded?.Invoke(Items.ToArray());

            return;
        }