Add Server connection records (close #199)

This commit is contained in:
2023-08-02 17:25:31 +08:00
parent 65e664f35f
commit fc3fd10081
2 changed files with 96 additions and 2 deletions

View File

@ -57,7 +57,7 @@
this.TxtToken = new System.Windows.Forms.TextBox();
this.LblToken = new System.Windows.Forms.Label();
this.LblConsoleTip = new System.Windows.Forms.Label();
this.TxtHost = new System.Windows.Forms.TextBox();
this.TxtHost = new System.Windows.Forms.ComboBox();
this.BtnQueryServerStatus = new System.Windows.Forms.Button();
this.LblHost = new System.Windows.Forms.Label();
this.GrpServerStatus.SuspendLayout();
@ -287,6 +287,7 @@
resources.ApplyResources(this.TxtHost, "TxtHost");
this.TxtHost.Name = "TxtHost";
this.TxtHost.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtHost_KeyDown);
this.TxtHost.SelectedIndexChanged += new System.EventHandler(this.TxtHost_SelectedIndexChanged);
//
// BtnQueryServerStatus
//
@ -361,7 +362,7 @@
private System.Windows.Forms.TextBox TxtToken;
private System.Windows.Forms.Label LblToken;
private System.Windows.Forms.Label LblConsoleTip;
private System.Windows.Forms.TextBox TxtHost;
private System.Windows.Forms.ComboBox TxtHost;
private System.Windows.Forms.Button BtnQueryServerStatus;
private System.Windows.Forms.Label LblHost;
}

View File

@ -38,11 +38,16 @@ namespace GrasscutterTools.Pages
{
internal partial class PageOpenCommand : BasePage
{
private const string TAG = nameof(PageOpenCommand);
public PageOpenCommand()
{
InitializeComponent();
if (DesignMode) return;
InitServerRecords();
TxtHost.Items.AddRange(ServerRecords.Select(it => it.Host).ToArray());
NUDRemotePlayerId.Value = Settings.Default.RemoteUid;
TxtHost.Text = Settings.Default.Host;
if (!string.IsNullOrEmpty(Settings.Default.Host) && !string.IsNullOrEmpty(Settings.Default.TokenCache))
@ -53,6 +58,61 @@ namespace GrasscutterTools.Pages
}
}
#region - -
private class ServerRecord
{
public string Tag { get; set; }
public string Host { get; set; }
public int Uid { get; set; }
public string Token { get; set; }
}
private readonly string ServerRecordsFilePath = Common.GetAppDataFile("Servers.json");
private List<ServerRecord> ServerRecords = new List<ServerRecord>
{
new ServerRecord
{
Host = "http://127.0.0.1:443",
Tag = "Localhost",
Token = "123456",
Uid = 10001,
}
};
private void InitServerRecords()
{
if (!File.Exists(ServerRecordsFilePath))
return;
try
{
Logger.I(TAG, "Loading ServerRecords json file from: " + ServerRecordsFilePath);
ServerRecords = JsonConvert.DeserializeObject<List<ServerRecord>>(File.ReadAllText(ServerRecordsFilePath));
}
catch (Exception ex)
{
Logger.W(TAG, "Parsing Servers.json failed.", ex);
}
}
private void SaveServerRecords()
{
try
{
if (ServerRecords.Count == 0)
return;
File.WriteAllText(ServerRecordsFilePath, JsonConvert.SerializeObject(ServerRecords));
}
catch (Exception ex)
{
Logger.W(TAG, "Save all server records failed.", ex);
}
}
#endregion
/// <summary>
/// 在运行按钮上显示提示,要求主窗口设置
/// </summary>
@ -122,6 +182,20 @@ namespace GrasscutterTools.Pages
if (e.KeyCode == Keys.Enter) BtnQueryServerStatus_Click(BtnQueryServerStatus, e);
}
/// <summary>
/// 地址栏选中项改变时触发
/// </summary>
private void TxtHost_SelectedIndexChanged(object sender, EventArgs e)
{
if (TxtHost.SelectedIndex >= 0 && TxtHost.SelectedIndex < ServerRecords.Count)
{
// 还原记录
var record = ServerRecords[TxtHost.SelectedIndex];
TxtToken.Text = record.Token;
NUDRemotePlayerId.Value = record.Uid;
}
}
/// <summary>
/// 点击查询服务器状态按钮时触发
/// </summary>
@ -247,6 +321,25 @@ namespace GrasscutterTools.Pages
GrpRemoteCommand.Enabled = false;
ShowTipInRunButton?.Invoke(Resources.ConnectedTip);
ButtonOpenGOODImport.Enabled = true;
var r = ServerRecords.Find(it => it.Host == TxtHost.Text);
if (r != null)
{
r.Token = Common.OC.Token;
r.Uid = (int)NUDRemotePlayerId.Value;
}
else
{
ServerRecords.Add(new ServerRecord
{
Host = Common.OC.Host,
Tag = "TODO",
Token = Common.OC.Token,
Uid = (int)NUDRemotePlayerId.Value
});
TxtHost.Items.Add(Common.OC.Host);
}
SaveServerRecords();
}
catch (Exception ex)
{