RaUI/Source/OnLineUpgradeConfig/frmUpdateFile.cs
zilinsoft 690d2651f5 ## 📅2025-08-16 星期六更新
### rycUpdate    V1.0.2506.0401
- *.[修复]修复文件替换失败,不会失败提示的BUG。
### RaUI    V4.0.2508.1601
- *.[新增]ChromeTabControl控件支持设置SizeMode属性。
- *.[新增]HotkeyTextBox新增支持传入热键文本进行自动转换。
- *.[新增]ryQuickSQL类新增GetJsonData方法。
- *.[新增]ryQuickSQL类新增DateToTimeStamp属性,导出时自动将时间类型转为时间戳。
- *.[新增]自动更新模块新增支持版本类型,区分正式版和测试版。
- *.[改进]HotkeyTextBox控件渲染方式改成全画布渲染。
- *.[改进]TextBoxEx2开启多行模式后,空文本改成在第一行显示。
- *.[修复]修复截图功能某些情况下会报错的BUG。
- *.[修复]修复HotkeyValue类处理多功能键文本时转换错误的BUG。
- *.[修复]修复RyComboBox控件在某些情况下边框会丢失的BUG。
- *.[修复]修复Hosts类针对删除hosts规则处理出错的BUG。
- *.[修复]修复当升级文件Url无法访问时,升级模块会无限期等待的BUG。
2025-08-16 14:27:11 +08:00

607 lines
26 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Server;
using ryCommon;
using Config;
using System.Threading;
using Server.User;
using BrightIdeasSoftware;
using Newtonsoft.Json.Linq;
using WinAPI;
using RyWeb;
using System.IO.Compression;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Diagnostics;
namespace LiveUpdate
{
public partial class frmUpdateFile :Form
{
/// <summary>
/// 更新文件夹
/// </summary>
public string UpdateFilesDir { get; set; } = "D:\\xinSoft\\wwwroot\\UpdateFiles";
/// <summary>
/// 更新文件完整路径
/// </summary>
string update_fullpath;
/// <summary>
/// 更新文件的文件名(含扩展名)
/// </summary>
private string update_filename { get; set; } = "";
public frmUpdateFile()
{
InitializeComponent();
OlvFileName.AspectGetter= delegate (object x) { return ((UpdateFile)x).FileName; };
}
private void BtnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
public double ConvertDouble(string Str, double defValue)
{
try
{
return Convert.ToDouble(Str);
}
catch
{
return defValue;
}
}
public DateTime ConvertDateTime(string Str, DateTime defValue)
{
try
{
return Convert.ToDateTime(Str);
}
catch
{
return defValue;
}
}
private void LoadUpdateConfig(string filePath)
{
if (filePath.Length == 0)
{
update_filename = "无";
Text = "软件更新信息设置(无)";
return;
}
else
{
update_filename = System.IO.Path.GetFileName(filePath);
Text = "软件更新信息设置(" + System.IO.Path.GetFileName(filePath) + ")";
}
update_fullpath = filePath;
var json_filename_noext = System.IO.Path.GetFileNameWithoutExtension(update_filename);
if(json_filename_noext!= "3H3Process")
{
BtnCopyToNormal.Visible = true;
ChkCopyZip.Visible = true;
}
else
{
BtnCopyToNormal.Visible = false;
ChkCopyZip.Visible = false;
}
var config_folder = UpdateFilesDir + "\\" + json_filename_noext + "_Config";
var updatelogs_folder = UpdateFilesDir + "\\WebUpdateLogs";
Json json= new Json(RyFiles.ReadAllText(filePath));
cmbNewVer.Text = json.GetJsonValue("SoftVer", "");
CbbVersionType.Text = json.GetJsonValue("VersionType", "");
cmbNewDbVer.Text = json.GetJsonValue("DbVer", "");
cmbProcUpdateUrl.Text = json.GetJsonValue("FileUpdateUrl", "");
dtNewProcDate.Value = ConvertDateTime(json.GetJsonValue("SoftDate", ""), DateTime.Now);
dtNewDataDate.Value = ConvertDateTime(json.GetJsonValue("DbDate", ""), DateTime.Now);
txtUpdateDes.Text = json.GetJsonValue("SoftUpdateDes", "");
if(json.GetJsonValue("SoftUpdateDesUrl", "").Length>0)
{ ChkUseHtml.Checked = true; }
else { ChkUseHtml.Checked = false; }
TxtUpdateDes_Html.Text=RyFiles.ReadAllText(updatelogs_folder+"\\"+ json_filename_noext+ ".html_bak");
txtDbUpdateDes.Text = json.GetJsonValue("DbUpdateDes", "");
var user_list = json.GetJsonValue("UpdateUserList", "");
txtUserList.SelectedIndex = -1;
for (int i = 0; i < txtUserList.Items.Count; i++)
{
var item = (UserGroup)txtUserList.Items[i];
if (item.UserList == user_list)
{
txtUserList.SelectedIndex = i;
break;
}
}
if (txtUserList.SelectedIndex == -1)
{
txtUserList.Items.Insert(0, new UserGroup()
{
Desc = "默认",
UserList = user_list,
NickNameList = DyAPI.API.UserAPI.GetListNickName(user_list)
});
if (!IsLoadALL)
{
txtUserList.Items.Insert(0, new UserGroup()
{
Desc = "所有用户",
UserList = "",
NickNameList = ""
});
}
txtUserList.SelectedIndex = txtUserList.Items.Count-1;
}
Json json_config = new Json(RyFiles.ReadAllText(config_folder + "\\Config.json"));
txtKillProcList.Text = json_config.GetJsonValue("KillProcList", "");
txtDelFileList.Text = json_config.GetJsonValue("DelFileList", "");
txtMainProcName.Text = json_config.GetJsonValue("MainProcName", "");
txtMainProcPram.Text = json_config.GetJsonValue("MainProcPram", "");
}
private void frmUpdateFile_Load(object sender, EventArgs e)
{
//var aa= ProcessExt.GetPath(3424);
UpdateFilesDir = Application.StartupPath + "\\";
GetSetting();
DirectoryInfo directory = new DirectoryInfo(UpdateFilesDir);
try
{
System.IO.Directory.CreateDirectory(UpdateFilesDir);
}
catch { }
LvUpdateFiles.ClearObjects();
List<UpdateFile> list = new List<UpdateFile>();
foreach (FileInfo info in directory.GetFiles("*.json"))
{
list.Add(new UpdateFile() { FileName=info.Name, Path=info.FullName });
}
LvUpdateFiles.AddObjects(list);
if (list.Count > 0)
{
LvUpdateFiles.SelectedObject = list[0];
}
update_fullpath = UpdateFilesDir +"\\3H3Process.json";
LoadUpdateConfig(update_fullpath);
}
private void SaveConfig(string filename)
{
var user = (UserGroup)txtUserList.SelectedItem;
var json_filename_noext = System.IO.Path.GetFileNameWithoutExtension(filename);
var config_folder = UpdateFilesDir + "\\" + json_filename_noext + "_Config";
var updatelogs_folder = UpdateFilesDir + "\\WebUpdateLogs";
var ProcUpdateUrl = cmbProcUpdateUrl.Text;
var iPos= ProcUpdateUrl.LastIndexOf("/");
var root_url = "http://192.168.1.191/OnlineUpgrade";
if (iPos>0)
{
root_url = ProcUpdateUrl.Substring(0, iPos);
}
JObject json = new JObject
{
["SoftVer"] = cmbNewVer.Text,
["VersionType"] = CbbVersionType.Text,
["DbVer"] = cmbNewDbVer.Text,
["FileUpdateUrl"] = ProcUpdateUrl,
["DbUpdateUrl"] = cmbDbUpdateUrl.Text,
["SoftDate"] = dtNewProcDate.Value.Date.ToString("yyyy-MM-dd"),
["DbDate"] = dtNewDataDate.Value.Date.ToString("yyyy-MM-dd"),
["SoftUpdateDes"] = txtUpdateDes.Text,
["SoftUpdateDesUrl"] = ChkUseHtml.Checked ? (root_url+"/WebUpdateLogs/" + json_filename_noext + ".html?rd=" + DateTime.Now.ToString("yyyyMMddHHmmss")) : "",
["DbUpdateDes"] = txtDbUpdateDes.Text,
["UpdateUserList"] = user == null ? "" : user.UserList,
["ConfigUrl"] = root_url+"/" + json_filename_noext + "_Config/Config.json"
};
RyFiles.WriteAllText(UpdateFilesDir + "\\" + json_filename_noext + ".json", json.ToString());
try
{
JObject json_config = new JObject();
json_config["KillProcList"] = txtKillProcList.Text;
json_config["KillProcList2"] = txtKillProcList.Text;
json_config["DelFileList"] = txtDelFileList.Text;
json_config["MainProcName"] = txtMainProcName.Text;
json_config["MainProcPram"] = txtMainProcPram.Text;
RyFiles.WriteAllText(config_folder + "\\Config.json", json_config.ToString());
}
catch { }
ryCommon.RyFiles.WriteAllText(updatelogs_folder + "\\" + json_filename_noext + ".html_bak", TxtUpdateDes_Html.Text.Replace("\r", "").Replace("\n", "\r\n"), Encoding.UTF8);
string html = ryCommon.RyFiles.ReadAllText(updatelogs_folder + "\\_mod.html");
ryCommon.RyFiles.WriteAllText(updatelogs_folder + "\\" + json_filename_noext + ".html", html.Replace("<%content%>", TxtUpdateDes_Html.Text), Encoding.UTF8);
}
private void BtnOK_Click(object sender, EventArgs e)
{
//if (IsNumeric(cmbNewDbVer.Text) == false)
//{
// MessageBox.Show("【数据库版本】必须为数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
if (cmbProcUpdateUrl.Text== "")
{
MessageBox.Show("【软件更新地址】不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//if (cmbDbUpdateUrl.Text.Length==0)
//{
// MessageBox.Show("【数据库更新地址】不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
SaveConfig(update_filename);
var user = (UserGroup)txtUserList.SelectedItem;
if (TxtUpdateDes_Html.Text.Length>0 && user!=null)
{
switch (MessageBox.Show("是否要马上向选择的用户进行定向推送?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
{
case DialogResult.Yes:
QuickWeb web = new QuickWeb();
web.Post("http://gs2.dangyou.com:1881/process", "a=send&touser=" +web.UrlEncode(user.UserList.Replace(",",";")) + "&type="+ ryMessageConst.MsType.MustUpdate +
"&msg=" + web.UrlEncode("http://192.168.1.191/OnlineUpgrade/" + System.IO.Path.GetFileName(update_fullpath)));
break;
}
}
MessageBox.Show("保存信息成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//this.Close();
}
private void frmUpdateFile_Resize(object sender, EventArgs e)
{
tabControl1.Refresh();
}
private void BtnOneKeyUpdate_Click(object sender, EventArgs e)
{
try
{
var from_zip_name = System.IO.Path.GetFileName(cmbProcUpdateUrl.Text);
if (System.IO.File.Exists(UpdateFilesDir + "\\" + from_zip_name))
{
//System.IO.Compression.ZipFile.cr
var MainProcName = txtMainProcName.Text.ToLower();
var ZipArchive = ZipFile.OpenRead(UpdateFilesDir + "\\" + from_zip_name);
bool have_data = false;
for (int i = 0; i < ZipArchive.Entries.Count; i++)
{
var entry = ZipArchive.Entries[i];
if(entry.FullName.ToLower()== MainProcName)
{
RyFiles.CreateDirectory(Application.StartupPath + "\\UserDb\\Update\\");
entry.ExtractToFile(Application.StartupPath+"\\UserDb\\Update\\"+ MainProcName, true);
string path = Application.StartupPath + "\\UserDb\\Update\\" + MainProcName;
if (File.Exists(path))
{
byte[] buffer = System.IO.File.ReadAllBytes(path);
Assembly assembly = Assembly.Load(buffer);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;
//Console.WriteLine(assemblyName.FullName);
string a = version.Major.ToString();
string b = version.Minor.ToString();
string c = version.Build.ToString("0000");
string d = version.Revision.ToString("0000");
cmbNewVer.Text = a + "." + b + "." + c + "." +d;
System.IO.FileInfo fi = new FileInfo(path);
dtNewProcDate.Value = fi.LastWriteTime;
have_data = true;
}
break;
}
}
ZipArchive.Dispose();
RyFiles.DeleteFile(Application.StartupPath + "\\UserDb\\Update");
if (!have_data)
{
MessageBox.Show("更新版本号失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("更新包不存在,一键更新失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch(Exception ex) {
MessageBox.Show("更新版本号失败。\r\n"+ ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button4_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
string path = Application.StartupPath + "\\Db\\SoftDes.txt";
string FileContent = "软件更新至 %date% %time%(%week%)";
txtUpdateDes.Text = GetFileContent(path, FileContent);
}
if (checkBox2.Checked)
{
string path = Application.StartupPath + "\\Db\\DbDes.txt";
string FileContent = "数据库更新至 %date% %time%(%week%)";
txtDbUpdateDes.Text = GetFileContent(path, FileContent);
}
BtnOneKeyUpdate.PerformClick();
BtnOK.PerformClick();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtUpdateDes.SelectedText = DateTime.Now.ToString("HH:mm");
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtUpdateDes.SelectedText = DateTime.Now.ToString("yyyy-MM-dd");
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtUpdateDes.SelectedText = DateTime.Now.ToString("dddd");
}
private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtDbUpdateDes.SelectedText = DateTime.Now.ToString("HH:mm");
}
private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtDbUpdateDes.SelectedText = DateTime.Now.ToString("yyyy-MM-dd");
}
private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
txtDbUpdateDes.SelectedText = DateTime.Now.ToString("dddd");
}
private string GetFileContent(string path, string defValue)
{
string FileContent = defValue;
if (System.IO.File.Exists(path))
{
FileContent = System.IO.File.ReadAllText(path,Encoding.Default);
}
string tmpStr = FileContent.Replace("%date%", DateTime.Now.ToString("yyyy-MM-dd"));
tmpStr = tmpStr.Replace("%time%", DateTime.Now.ToString("HH:mm"));
tmpStr = tmpStr.Replace("%week%", DateTime.Now.ToString("dddd"));
tmpStr = tmpStr.Replace("%softdate%", dtNewProcDate.Value.ToString("yyyy-MM-dd"));
tmpStr = tmpStr.Replace("%dbdate%", dtNewDataDate.Value.ToString("yyyy-MM-dd"));
tmpStr = tmpStr.Replace("%softver%", cmbNewVer.Text);
return tmpStr;
}
private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string path = Application.StartupPath + "\\Db\\DbDes.txt";
string FileContent = "数据库更新至 %date% %time%(%week%)";
txtDbUpdateDes.Text = GetFileContent(path, FileContent);
}
private void linkLabel8_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string path = Application.StartupPath + "\\Db\\SoftDes.txt";
string FileContent = "软件更新至 %date% %time%(%week%)";
txtUpdateDes.Text = GetFileContent(path, FileContent);
}
private void btnSelectUser_Click(object sender, EventArgs e)
{
var user = (UserGroup)txtUserList.SelectedItem;
if (user == null) { return; }
frmUserList frm = new frmUserList();
frm.userList = user.UserList;
frm.LoadDb("", user.UserList);
if (frm.ShowDialog(this) == DialogResult.OK)
{
txtUserList.Font = new Font(txtUserList.Font, FontStyle.Regular);
txtUserList.Text = frm.userNickList;
var user_list = frm.userList;
txtUserList.SelectedIndex = -1;
for (int i = 0; i < txtUserList.Items.Count; i++)
{
var item = (UserGroup)txtUserList.Items[i];
if (item.UserList == user_list)
{
txtUserList.SelectedIndex = i;
break;
}
}
if (txtUserList.SelectedIndex == -1)
{
txtUserList.Items.Insert(0, new UserGroup()
{
Desc = "指定用户",
UserList = user_list,
NickNameList = DyAPI.API.UserAPI.GetListNickName(user_list)
});
txtUserList.SelectedIndex = 0;
}
//txtUserList.Text = frm.userNickList;
}
}
class UpdateFile
{
/// <summary>
/// 文件名
/// </summary>
public string FileName { get; set; } = "";
/// <summary>
/// 升级文件路径
/// </summary>
public string Path { get; set; } = "";
}
private void LvUpdateFiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (LvUpdateFiles.SelectedObject == null) { return; }
var item = (UpdateFile)LvUpdateFiles.SelectedObject;
LoadUpdateConfig(item.Path);
}
private void BtnCopyToNormal_Click(object sender, EventArgs e)
{
var CopyZip = ChkCopyZip.Checked;
BtnCopyToNormal.Enabled = false;
Thread th = new Thread(Start);
th.Start();
void Start()
{
ShowState("正在同步配置...");
var error = 0;
if (CopyZip)
{
ShowState("正在复制升级包...");
var from_zip_name = System.IO.Path.GetFileName(cmbProcUpdateUrl.Text);
if (System.IO.File.Exists(UpdateFilesDir + "\\" + from_zip_name))
{
RyFiles.DeleteFile(UpdateFilesDir + "\\process.zip");
if (!RyFiles.CopyBigFile(UpdateFilesDir + "\\" + from_zip_name, UpdateFilesDir + "\\process.zip"))
{
error++;
}
}
else
{
MessageBox.Show("更新包不存在,同步失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ShowState("正在更新配置...");
string html = ryCommon.RyFiles.ReadAllText(UpdateFilesDir + "\\WebUpdateLogs\\_mod.html");
ryCommon.RyFiles.WriteAllText(UpdateFilesDir + "\\WebUpdateLogs\\3H3Process.html", html.Replace("<%content%>", TxtUpdateDes_Html.Text), Encoding.UTF8);
ryCommon.RyFiles.WriteAllText(UpdateFilesDir + "\\WebUpdateLogs\\3H3Process.html_bak", TxtUpdateDes_Html.Text, Encoding.UTF8);
Json json = new Json(RyFiles.ReadAllText(UpdateFilesDir + "\\3H3Process.json"));
json.SetJsonValue("SoftVer", cmbNewVer.Text);
json.SetJsonValue("SoftDate", dtNewProcDate.Value.Date.ToString("yyyy-MM-dd"));
RyFiles.WriteAllText(UpdateFilesDir + "\\3H3Process.json", json.Text);
RyFiles.CopyBigFile(UpdateFilesDir + "\\"+ System.IO.Path.GetFileNameWithoutExtension(update_filename) + "_Config\\Config.json", UpdateFilesDir + "\\3H3Process_Config\\Config.json");
ShowState("同步完成...");
this.Invoke(new Action(() =>
{
if (error > 0)
{
MessageBox.Show("同步发生错误,请检查配置。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("同步成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
BtnCopyToNormal.Enabled = true;
}));
void ShowState(string text)
{
this.Invoke(new Action(() =>
{
LblState.Text = "状态:" + text;
}));
}
}
}
private bool IsLoadALL = false;
public void GetSetting()
{
var userlist = (UserGroup)txtUserList.SelectedItem;
IsLoadALL = false;
txtUserList.Items.Clear();
var selected_index = -1;
try
{
JObject jo = JObject.Parse(RyFiles.ReadAllText(Application.StartupPath + "\\UserDb\\UserSet.dat"));
JArray jarr = jo.GetJsonValue("list", new JArray());
for (int i = 0; i < jarr.Count; i++)
{
var item = jarr[i];
var user = item.GetJsonValue("UserList", "");
if(selected_index==-1 && userlist !=null && userlist.UserList==user)
{
selected_index=i;
}
txtUserList.Items.Add(new UserGroup()
{
Desc = item.GetJsonValue("Desc", ""),
UserList = user,
NickNameList = DyAPI.API.UserAPI.GetListNickName(user)
});
}
}
catch { }
if (selected_index == -1)
{
if (userlist != null)
{
txtUserList.Items.Insert(0, new UserGroup()
{
Desc = "默认",
UserList = userlist.UserList,
NickNameList = DyAPI.API.UserAPI.GetListNickName(userlist.UserList)
});
selected_index = 1;
}
else { selected_index = 0; }
}
else
{
selected_index ++;
}
txtUserList.Items.Insert(0, new UserGroup()
{
Desc = "所有用户",
UserList = "",
NickNameList = ""
});
IsLoadALL = true;
txtUserList.SelectedIndex = selected_index;
}
private void BtnUsersView_Click(object sender, EventArgs e)
{
FrmBetaUsers frm = new FrmBetaUsers();
frm.Icon = Icon;
frm.ShowDialog();
frm.Dispose();
GetSetting();
}
private UpdateFile LastSelected { get; set; } = null;
private void LvUpdateFiles_SelectionChanged(object sender, EventArgs e)
{
if(LvUpdateFiles.SelectedObject==null)
{
LvUpdateFiles.SelectedObject = LastSelected;
}
else
{
LastSelected = (UpdateFile)LvUpdateFiles.SelectedObject;
}
}
private void BtnPasteFromMD_Click(object sender, EventArgs e)
{
var split = Clipboard.GetText().Replace("\r","").Split('\n');
var txt = "";
for (int i = 0; i < split.Length; i++)
{
var line=split[i];
if (line.Length == 0) { continue; }
if (line.StartsWith("- "))
{
txt += "<p>"+System.Web.HttpUtility.HtmlEncode(line.Substring(2).Trim())+ "</p>\r\n";
}
else if (line.StartsWith("#### "))
{
txt += "<h4>" + System.Web.HttpUtility.HtmlEncode(line.Substring(5).Trim()) + "</h4>\r\n";
}
}
if (TxtUpdateDes_Html.SelectionStart == -1)
{
TxtUpdateDes_Html.Text = txt.Trim();
}
else
{
TxtUpdateDes_Html.SelectedText= txt.Trim();
}
}
private void BtnDefUpdateLog_Click(object sender, EventArgs e)
{
TxtUpdateDes_Html.Text ="<p>[改进]一些功能改进。</p>\r\n<p>[修复]修复了一些BUG。</p>";
}
}
}