示例#1
0
        async Task DownloadMods(List <Tuple <Mod, Mod> > toDownload)
        {
            await OPdater.Download(toDownload.Select(x => x.Item1), (mod, recv, total) => {
                var index   = toDownload.IndexOf(x => (mod == x.Item1));
                var oldMod  = toDownload[index].Item2;
                var version = ((oldMod != null) ? string.Format("{0} -> {1}", oldMod.Version, mod.Version) : mod.Version);

                var progress = ((total > 0) ? ((double)recv / total) : 0);
                progress     = (index + progress) / toDownload.Count * 100;

                lock (OPdater) {
                    Console.Write("{0," + (1 - Console.WindowWidth) + "}",
                                  string.Format("[{0,3:0}%] {1} ({2}) [{3}/{4} KiB]",
                                                (int)progress, mod.Name, version,
                                                recv / 1024, ((total > 0) ? (total / 1024).ToString() : "???")));
                    Console.SetCursorPosition(0, Console.CursorTop);
                }
            });

            Console.WriteLine();
        }
示例#2
0
        async Task ServerUpdateCheckerLoop(CancellationToken ct = default(CancellationToken))
        {
            while (!ct.IsCancellationRequested)
            {
                if (await OPdater.IsUpdateAvailable())
                {
                    ModpackInfo latest = await OPdater.GetLatestModpackInfo();

                    var toDownload = new List <Tuple <Mod, Mod> >();
                    var toDelete   = new List <Mod>();
                    BuildModUpdateLists(latest, toDownload, null, toDelete);

                    if (Wrapper.Running && (Wrapper.Players.Count > 0))
                    {
                        // Write a nice message to the players telling them about the new update.

                        var url = "https://github.com/" + OPdater.Settings.Owner + "/" +
                                  OPdater.Settings.Repository + "/commits/" + OPdater.Settings.Branch;

                        var lines = new List <string>();
                        lines.Add(
                            @"{""text"":""[ UPDATE!! ]"",""color"":""red"",""bold"":""true""},"" ""," +
                            @"{""text"":"" Version " + latest.Version + @""",""color"":""yellow"",""bold"":""false""},"" ""," +
                            @"{""text"":""("",""color"":""yellow"",""bold"":""false""},{""text"":""View Online"",""color"":""aqua"",""underlined"":""true"",""clickEvent"":{""action"":""open_url"",""value"":""" + url + @"""}},{""text"":"")"",""color"":""yellow"",""underlined"":""false""}");

                        var newMods     = toDownload.Where(pair => (pair.Item2 == null)).Select(pair => pair.Item1.Name).ToList();
                        var changedMods = toDownload.Where(pair => (pair.Item2 != null)).Select(pair => pair.Item1.Name).ToList();

                        if (newMods.Count > 0)
                        {
                            lines.Add(@"{""text"":""[+]"",""color"":""green"",""bold"":""true""},"" ""," +
                                      @"{""text"":""" + string.Join(", ", newMods) + @""",""color"":""yellow"",""bold"":""false""}");
                        }
                        if (changedMods.Count > 0)
                        {
                            lines.Add(@"{""text"":""[:]"",""color"":""gray"",""bold"":""true""},"" ""," +
                                      @"{""text"":""" + string.Join(", ", changedMods) + @""",""color"":""yellow"",""bold"":""false""}");
                        }
                        if (toDelete.Count > 0)
                        {
                            lines.Add(@"{""text"":""[-]"",""color"":""green"",""bold"":""true""},"" ""," +
                                      @"{""text"":""" + string.Join(", ", toDelete) + @""",""color"":""yellow"",""bold"":""false""}");
                        }

                        foreach (var line in lines)
                        {
                            Wrapper.Input("/tellraw @a [" + line + "]");
                        }
                    }

                    if (toDownload.Count > 0)
                    {
                        await OPdater.Download(toDownload.Select(x => x.Item1), null);
                    }

                    if (Wrapper.Running)
                    {
                        Wrapper.AutoRestart = false;
                        await Wrapper.Stop();
                    }

                    await OPdater.CloneOrUpdate();

                    UpdateMods(toDownload, toDelete);
                }

                if (!Wrapper.Running)
                {
                    Wrapper.AutoRestart = true;
                    Wrapper.Start();
                }

                await Task.Delay(TimeSpan.FromMinutes(2), ct);
            }

            if (Wrapper.Running)
            {
                await Wrapper.Stop();
            }
        }