Example #1
0
        /// <summary>
        /// 通用的Java检查,用于启动游戏
        /// </summary>
        /// <param name="version">Java的版本号</param>
        /// <returns>是否检查成功</returns>
        public static bool CheckJava(int version)
        {
            string java_path = string.Format(java_path_builder, version);

            if (!File.Exists(java_path + @"\bin\javaw.exe"))
            {
                try
                {
                    string tuna_path = $"https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/{version}/jre/x64/windows/";
                    if (!Environment.Is64BitOperatingSystem)
                    {
                        tuna_path = $"https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/{version}/jre/x86/windows/";
                    }
                    string javas = HTTP.GetHttpStringData(tuna_path);
                    Regex  regex = new Regex(@"<a\shref=""(?<path>.*)""\stitle="".*hotspot.*\.zip"">");
                    Match  match = regex.Match(javas);
                    if (!match.Success)
                    {
                        goto pass;
                    }
                    string filename    = match.Groups["path"].ToString();
                    string remote_path = tuna_path + filename;
                    HTTP   http        = HTTP.CreateHTTPDownLoad(tuna_path + filename, java_path + filename);
                    http.onHttpDownloadMessage += (sender, e) => ClientMainWindow.UpdateStatusStatic(e.message, e.process);
                    while (!http.Download())
                    {
                        if (MessageBox.Show($"Java{version}下载失败了,你要重试吗?\r\n" + java_path + filename, "错误", MessageBoxButton.YesNo) == MessageBoxResult.No)
                        {
                            goto pass;
                        }
                    }
                    ClientMainWindow.UpdateStatusStatic($"正在释放Java{version}文件...");
                    var zip           = ZipFile.Open(java_path + filename, ZipArchiveMode.Read);
                    var temp_dir_name = java_path + filename.Remove(filename.LastIndexOf("."));
                    zip.ExtractToDirectory(temp_dir_name);
                    zip.Dispose();
                    FileContorl.CopyDir(Directory.EnumerateDirectories(temp_dir_name).ToArray()[0], java_path);
                    Thread.Sleep(200);
                    Directory.Delete(temp_dir_name, true);
                    File.Delete(java_path + filename);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.StackTrace, e.Message);
                }
                return(true);

                pass :;
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #2
0
        public static bool CheckJavaFX()
        {
            string java16_path = string.Format(java_path_builder, 16);

            if (!File.Exists(java16_path + @"jmods\javafx.web.jmod"))
            {
                try
                {
                    string javafx_path = "https://gluonhq.com/download/javafx-16-jmods-windows/";
                    if (!Environment.Is64BitOperatingSystem)
                    {
                        javafx_path = "https://gluonhq.com/download/javafx-16-jmods-windows-x86/";
                    }
                    //"https://download2.gluonhq.com/openjfx/16/openjfx-16_windows-x64_bin-jmods.zip";
                    string javafx_local_path = java16_path + "javafx.zip";
                    HTTP   http = HTTP.CreateHTTPDownLoad(javafx_path, javafx_local_path);
                    http.onHttpDownloadMessage += (sender, e) => ClientMainWindow.UpdateStatusStatic(e.message, e.process);
                    while (!http.Download())
                    {
                        if (MessageBox.Show("JavaFx下载失败了,你要重试吗?\r\n" + javafx_local_path, "错误", MessageBoxButton.YesNo) == MessageBoxResult.No)
                        {
                            goto pass;
                        }
                    }
                    ClientMainWindow.UpdateStatusStatic("正在释放JavaFx文件");
                    var zip2 = ZipFile.Open(javafx_local_path, ZipArchiveMode.Read);
                    zip2.ExtractToDirectory(java16_path + "javafx");
                    zip2.Dispose();
                    if (!Directory.Exists(java16_path + "jmods"))
                    {
                        Directory.CreateDirectory(java16_path + "jmods");
                    }
                    FileContorl.CopyDir(Directory.EnumerateDirectories(java16_path + "javafx").ToArray()[0], java16_path + "jmods");
                    Thread.Sleep(200);
                    Directory.Delete(java16_path + "javafx", true);
                    File.Delete(javafx_local_path);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.StackTrace, e.Message);
                    goto pass;
                }
                return(true);

                pass :;
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #3
0
        /// <summary>
        /// 为一个目标文件夹更新内容
        /// </summary>
        /// <param name="internetPath">目标网络文件夹地址</param>
        /// <param name="localPath">目标本地文件夹地址</param>
        private int downloadFilesForFolder(string internetPath, string localPath, string customPath = null)
        {
            int    failedCount    = 0;
            string folder_setting = internetPath + "fold.json";

            try
            {
                string s    = HTTP.GetHttpStringData(folder_setting);
                string type = "Hash";
                if (s != null)
                {
                    var obj = JsonConvert.DeserializeObject(s) as JObject;
                    type = obj.GetValue("type").ToObject <string>();
                }
                if (type == "Hash")
                {
                    string md5sPath = internetPath + "md5s.json";
                    string md5s     = HTTP.GetHttpStringData(md5sPath);
                    if (md5s == null)
                    {
                        Task.Run(() => MessageBox.Show("远程服务器配置错误,md5s.json不存在,位于\n" + md5sPath));
                    }
                    //网络文件列表
                    Dictionary <string, string> dict_internet_files = JsonConvert.DeserializeObject <Dictionary <string, string> >(md5s);
                    //夜长梦多,从这里移除控制文件(出BUG了)
                    foreach (string controlFile in controlFiles)
                    {
                        if (dict_internet_files.ContainsKey(controlFile))
                        {
                            dict_internet_files.Remove(controlFile);
                        }
                    }
                    //本地文件列表
                    Dictionary <string, string> dict_local_files = new Dictionary <string, string>();
                    //自定义文件列表
                    Dictionary <string, string> dict_custom_files = new Dictionary <string, string>();
                    if (!Directory.Exists(localPath))
                    {
                        Directory.CreateDirectory(localPath);
                    }
                    string[] localFiles = Directory.GetFiles(localPath);
                    //自定义文件不启动时不检测和创建自定义目录
                    if (customPath != null && !Directory.Exists(customPath))
                    {
                        Directory.CreateDirectory(customPath);
                    }
                    string[] customFiles = null;
                    if (customPath != null)
                    {
                        customFiles = Directory.GetFiles(customPath);
                        for (int i = 0; i < customFiles.Length; i++)
                        {
                            UpdateMessage("计算本地自定义文件哈希值 {0} " + customFiles[i], i + 1, customFiles.Length);
                            dict_custom_files.Add(new FileInfo(customFiles[i]).Name, Md5.GetMD5HashFromFile(customFiles[i]));
                        }
                    }
                    //本地文件的哈希值检测
                    for (int i = 0; i < localFiles.Length; i++)
                    {
                        UpdateMessage("计算本地文件哈希值 {0} " + localFiles[i], i + 1, localFiles.Length);
                        string local_hash = Md5.GetMD5HashFromFile(localFiles[i]);
                        dict_local_files.Add(new FileInfo(localFiles[i]).Name, local_hash);
                    }
                    UpdateMessage("正在由本地向上匹配文件,请稍候...");
                    //步骤1 本地向上检查
                    foreach (KeyValuePair <string, string> local in dict_local_files)
                    {
                        try
                        {
                            //如果自定义文件和远程文件皆没有这个文件,删除
                            if (!(dict_internet_files.ContainsValue(local.Value) || dict_custom_files.ContainsValue(local.Value)))
                            {
                                File.Delete(localPath + local.Key);
                            }
                            //当文件名不匹配时
                            else if (!dict_internet_files.ContainsKey(local.Key))
                            {
                                foreach (KeyValuePair <string, string> kv in dict_internet_files)
                                {
                                    if (kv.Value == local.Value)
                                    {
                                        File.Move(local.Key, kv.Key);
                                        break;
                                    }
                                }
                            }
                        }
                        catch (IOException e)
                        {
                            new Thread(() => MessageBox.Show(e.StackTrace, e.Message)).Start();
                        }
                    }
                    UpdateMessage("正在由远程向下匹配文件,请稍候...");
                    //步骤2 远程向下检查
                    int dict_internet_files_enum_now = 0;
                    foreach (KeyValuePair <string, string> internet in dict_internet_files)
                    {
                        dict_internet_files_enum_now++;
                        if (IsControlFile(internet.Key))
                        {
                            continue;
                        }
                        if (!dict_local_files.ContainsValue(internet.Value))
                        {
                            SendDownloadMessage();
                            HTTP http = HTTP.CreateHTTPDownLoad(
                                internetPath + internet.Key,
                                localPath + internet.Key,
                                dict_internet_files_enum_now,
                                dict_internet_files.Count);
                            http.onHttpDownloadMessage += (sender, e) =>
                            {
                                UpdateMessage(e.message, e.process, 1);
                            };
                            if (!http.Download())
                            {
                                failedCount++;
                            }
                        }
                    }
                    //
                    //向自定义文件只在CustomFiles启动时可用
                    if (customFiles != null)
                    {
                        UpdateMessage("正在进行自定义文件的匹配,请稍候...");
                        int dict_custom_files_enum_now = 0;
                        //步骤3 本地检查
                        foreach (KeyValuePair <string, string> custom in dict_custom_files)
                        {
                            dict_custom_files_enum_now++;
                            if (!dict_local_files.ContainsValue(custom.Value))
                            {
                                UpdateMessage("正在复制文件 {0} " + customPath + custom.Key, dict_custom_files_enum_now, dict_custom_files.Count);
                                try
                                {
                                    if (File.Exists(localPath + custom.Key))
                                    {
                                        File.Delete(localPath + custom.Key);
                                    }
                                    File.Copy(customPath + custom.Key, localPath + custom.Key);
                                }
                                catch (Exception e)
                                {
                                    new Thread(() => MessageBox.Show(e.StackTrace, e.Message)).Start();
                                    failedCount++;
                                }
                            }
                        }
                    }
                }
                else if (type == "loose")
                {
                    string md5sPath = internetPath + "md5s.json";
                    string md5s     = HTTP.GetHttpStringData(md5sPath);
                    Dictionary <string, string> dict_internet_files = JsonConvert.DeserializeObject <Dictionary <string, string> >(md5s);
                    UpdateMessage("正在由远程向下匹配文件,请稍候...");
                    //步骤2 远程向下检查
                    int dict_internet_files_enum_now             = 0;
                    Dictionary <string, string> dict_local_files = new Dictionary <string, string>();
                    string[] localFiles = Directory.GetFiles(localPath);
                    for (int i = 0; i < localFiles.Length; i++)
                    {
                        UpdateMessage("计算本地文件哈希值 {0} " + localFiles[i], i + 1, localFiles.Length);
                        string local_hash = Tools.Md5.GetMD5HashFromFile(localFiles[i]);
                        dict_local_files.Add(new FileInfo(localFiles[i]).Name, local_hash);
                    }
                    foreach (KeyValuePair <string, string> internet in dict_internet_files)
                    {
                        dict_internet_files_enum_now++;
                        if (IsControlFile(internet.Key))
                        {
                            continue;
                        }
                        if (!dict_local_files.ContainsKey(internet.Key) || !dict_local_files.ContainsValue(internet.Value))
                        {
                            try
                            {
                                SendDownloadMessage();
                                if (File.Exists(internet.Key))
                                {
                                    File.Delete(internet.Key);
                                }
                                HTTP http = HTTP.CreateHTTPDownLoad(
                                    internetPath + internet.Key,
                                    localPath + internet.Key,
                                    dict_internet_files_enum_now,
                                    dict_internet_files.Count);
                                http.onHttpDownloadMessage += (sender, e) =>
                                {
                                    UpdateMessage(e.message, e.process, 1);
                                };
                                if (!http.Download())
                                {
                                    failedCount++;
                                }
                            }
                            catch (Exception e)
                            {
                                new Thread(() => MessageBox.Show(e.StackTrace, e.Message)).Start();
                                failedCount++;
                            }
                        }
                    }
                    UpdateMessage("正在由远程向下请求删除文件,请稍候...");
                    string fo_delete_path = internetPath + "delete.json";
                    string fo_delete_s    = Tools.HTTP.GetHttpStringData(fo_delete_path);
                    if (fo_delete_s != null)
                    {
                        JArray   file_list = JsonConvert.DeserializeObject(fo_delete_s) as JArray;
                        string[] files     = file_list.ToObject <string[]>();
                        foreach (string singal_file in files)
                        {
                            try
                            {
                                if (File.Exists(localPath + singal_file))
                                {
                                    File.Delete(localPath + singal_file);
                                }
                                //done++;
                            }
                            catch (Exception e)
                            {
                                //filed++;
                                new Thread(() => MessageBox.Show(e.StackTrace, e.Message)).Start();
                                failedCount++;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                new Thread(() => MessageBox.Show(e.StackTrace, e.Message)).Start();
                failedCount++;
            }
            return(failedCount);
        }