コード例 #1
0
        public async Task BeginInstallFromJObject(ProgressCallback monitor, CancellationToken cancellationToken, JObject jObj, string tempPath)
        {
            profile = jObj.ToObject <Install>();

            string target = Options.GameRootPath;

            if (!Directory.Exists(target))
            {
                throw new DirectoryNotFoundException("The minecraft root is not found");
            }

            //I think we dont need to inject the launcher profiles, so we dont need this json :)
            //string launcherProfiles = Path.Combine(target, "launcher_profiles.json");
            //if (!File.Exists(launcherProfiles))
            //{
            //    throw new FileNotFoundException("There is no minecraft launcher profile");
            //}

            string versionRoot  = Path.Combine(target, "versions");
            string librariesDir = Path.Combine(target, "libraries");

            if (!Directory.Exists(librariesDir))
            {
                Directory.CreateDirectory(librariesDir);
            }

            //Extracting json
            monitor.SetState("提取json");
            string jsonPath = PathManager.GetJsonPath(Options.GameRootPath, profile.Version);

            if (!Directory.Exists(Path.GetDirectoryName(jsonPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(jsonPath));
            }
            File.Copy(tempPath + profile.Json, jsonPath, true);

            //Consider minecraft client jar
            monitor.SetState("检查游戏文件");
            string clientTarget = PathManager.GetJarPath(Options.GameRootPath, profile.Minecraft);

            if (!File.Exists(PathManager.GetJsonPath(Options.GameRootPath, profile.Minecraft)))
            {
                throw new FileNotFoundException("Minecraft json is not exists");
            }
            if (!File.Exists(clientTarget))
            {
                throw new FileNotFoundException("Minecraft jar is not exists");
            }

            var exc = await DownloadUtils.DownloadForgeJLibrariesAsync(monitor, Options.DownloadSource, cancellationToken, profile.Libraries, librariesDir);

            if (exc != null)
            {
                throw exc;
            }

            string[] mavenFolders = Directory.GetDirectories(tempPath + "\\maven");
            foreach (var item in mavenFolders)
            {
                DirectoryInfo info = new DirectoryInfo(item);
                FileHelper.CopyDirectory(item, librariesDir + '\\' + info.Name, true);
            }

            await Task.Factory.StartNew(() =>
            {
                PostProcessors postProcessors = new PostProcessors(profile, Options.IsClient, monitor);
                Exception procExc             = postProcessors.Process(tempPath, Options.GameRootPath, clientTarget, Options.Java);
                if (procExc != null)
                {
                    throw procExc;
                }
            });
        }
コード例 #2
0
        public static async Task <Exception> DownloadForgeJLibrariesAsync(ProgressCallback monitor, DownloadSource source, CancellationToken cancelToken, List <JLibrary> libs, string librariesDir)
        {
            try
            {
                foreach (var item in libs)
                {
                    monitor.SetDoneSize(0);
                    monitor.SetState(string.Format("补全库文件{0}", item.Name));
                    Exception exception = null;
                    for (int i = 1; i <= 3; i++)
                    {
                        try
                        {
                            string from         = GetDownloadUrl.DoURLReplace(source, item.Downloads.Artifact.URL);
                            string to           = Path.Combine(librariesDir, item.Downloads.Artifact.Path);
                            string buffFilename = to + ".downloadtask";

                            if (File.Exists(to))
                            {
                                goto a;
                            }
                            if (string.IsNullOrWhiteSpace(from))
                            {
                                goto a;
                            }
                            if (Path.IsPathRooted(to))
                            {
                                string dirName = Path.GetDirectoryName(to);
                                if (!Directory.Exists(dirName))
                                {
                                    Directory.CreateDirectory(dirName);
                                }
                            }
                            if (File.Exists(buffFilename))
                            {
                                File.Delete(buffFilename);
                            }

                            using (var getResult = await HttpRequesterAPI.HttpGetAsync(from, cancelToken))
                            {
                                getResult.EnsureSuccessStatusCode();
                                monitor.SetTotalSize(getResult.Content.Headers.ContentLength.GetValueOrDefault());
                                using (Stream responseStream = await getResult.Content.ReadAsStreamAsync())
                                {
                                    using (FileStream fs = new FileStream(buffFilename, FileMode.Create))
                                    {
                                        byte[] bArr = new byte[1024];
                                        int    size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length);

                                        while (size > 0)
                                        {
                                            if (cancelToken.IsCancellationRequested)
                                            {
                                                return(null);
                                            }
                                            fs.Write(bArr, 0, size);
                                            size = responseStream.Read(bArr, 0, (int)bArr.Length);
                                            monitor.IncreaseDoneSize(size);
                                        }
                                    }
                                }
                            }

                            //下载完成后转正
                            File.Move(buffFilename, to);
                            monitor.SetDone();

                            break;
                        }
                        catch (Exception e)
                        {
                            exception = e;
                            monitor.SetState(string.Format("重试第{0}次", i));

                            //继续重试
                            continue;
                        }
                    }
                    a :;
                }
                return(null);
            }
            catch (Exception e)
            {
                return(e);
            }
        }