diff --git a/Source/GrasscutterTools/Pages/PageOpenCommand.Designer.cs b/Source/GrasscutterTools/Pages/PageOpenCommand.Designer.cs index 490e186..dab6d6b 100644 --- a/Source/GrasscutterTools/Pages/PageOpenCommand.Designer.cs +++ b/Source/GrasscutterTools/Pages/PageOpenCommand.Designer.cs @@ -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; } diff --git a/Source/GrasscutterTools/Pages/PageOpenCommand.cs b/Source/GrasscutterTools/Pages/PageOpenCommand.cs index 91b12c1..a0bd8fb 100644 --- a/Source/GrasscutterTools/Pages/PageOpenCommand.cs +++ b/Source/GrasscutterTools/Pages/PageOpenCommand.cs @@ -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 ServerRecords = new List + { + 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>(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 + + /// /// 在运行按钮上显示提示,要求主窗口设置 /// @@ -122,6 +182,20 @@ namespace GrasscutterTools.Pages if (e.KeyCode == Keys.Enter) BtnQueryServerStatus_Click(BtnQueryServerStatus, e); } + /// + /// 地址栏选中项改变时触发 + /// + 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; + } + } + /// /// 点击查询服务器状态按钮时触发 /// @@ -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) {