Support Open Command Plugin

Support Remove Command
Update version to 1.0.0
This commit is contained in:
2022-05-13 08:52:35 +08:00
parent e30a2f79cf
commit 0b7347e118
13 changed files with 3099 additions and 1918 deletions

View File

@ -25,6 +25,12 @@
<setting name="TextMapFileName" serializeAs="String">
<value>TextMapCHS</value>
</setting>
<setting name="RemoteUid" serializeAs="String">
<value>10001</value>
</setting>
<setting name="Host" serializeAs="String">
<value>https://127.0.0.1</value>
</setting>
</GrasscutterTools.Properties.Settings>
</userSettings>
</configuration>

View File

@ -0,0 +1,18 @@
using System.Threading.Tasks;
using GrasscutterTools.DispatchServer.Model;
using GrasscutterTools.Utils;
namespace GrasscutterTools.DispatchServer
{
public static class DispatchServerAPI
{
public static async Task<ServerStatus> QueryServerStatus(string host)
{
var response = await HttpHelper.GetAsync<ServerStatusResponse>(host + "/status/server");
return response?.Status;
}
}
}

View File

@ -0,0 +1,23 @@

using Newtonsoft.Json;
namespace GrasscutterTools.DispatchServer.Model
{
public class ServerStatus
{
[JsonProperty("playerCount")]
public int PlayerCount { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
}
public class ServerStatusResponse
{
[JsonProperty("retcode")]
public int RetCode { get; set; }
[JsonProperty("status")]
public ServerStatus Status { get; set; }
}
}

View File

@ -29,6 +29,7 @@ namespace GrasscutterTools
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
this.TxtCommand = new System.Windows.Forms.TextBox();
this.BtnCopy = new System.Windows.Forms.Button();
@ -159,14 +160,26 @@ namespace GrasscutterTools
this.LnkGithub = new System.Windows.Forms.LinkLabel();
this.LblSupportDescription = new System.Windows.Forms.Label();
this.TPRemoteCall = new System.Windows.Forms.TabPage();
this.LblRemoteTODO = new System.Windows.Forms.Label();
this.GrpRemoteCommand = new System.Windows.Forms.GroupBox();
this.LnkRCHelp = new System.Windows.Forms.LinkLabel();
this.LblSessionKey = new System.Windows.Forms.Label();
this.TxtSessionKey = new System.Windows.Forms.TextBox();
this.TxtHost = new System.Windows.Forms.TextBox();
this.LblHost = new System.Windows.Forms.Label();
this.BtnPingHost = new System.Windows.Forms.Button();
this.BtnQueryServerStatus = new System.Windows.Forms.Button();
this.NUDRemotePlayerId = new System.Windows.Forms.NumericUpDown();
this.LblRemotePlayerId = new System.Windows.Forms.Label();
this.LblServerVersionLabel = new System.Windows.Forms.Label();
this.LblServerVersion = new System.Windows.Forms.Label();
this.LblPlayerCountLabel = new System.Windows.Forms.Label();
this.LblPlayerCount = new System.Windows.Forms.Label();
this.GrpServerStatus = new System.Windows.Forms.GroupBox();
this.LblOpenCommandSupport = new System.Windows.Forms.Label();
this.LnkOpenCommandLabel = new System.Windows.Forms.LinkLabel();
this.LblVerificationCode = new System.Windows.Forms.Label();
this.NUDVerificationCode = new System.Windows.Forms.NumericUpDown();
this.BtnSendVerificationCode = new System.Windows.Forms.Button();
this.BtnConnectOpenCommand = new System.Windows.Forms.Button();
this.BtnInvokeOpenCommand = new System.Windows.Forms.Button();
this.LnkRCHelp = new System.Windows.Forms.LinkLabel();
this.TTip = new System.Windows.Forms.ToolTip(this.components);
this.GrpCommand.SuspendLayout();
this.TCMain.SuspendLayout();
this.TPHome.SuspendLayout();
@ -210,6 +223,9 @@ namespace GrasscutterTools
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.TPRemoteCall.SuspendLayout();
this.GrpRemoteCommand.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.NUDRemotePlayerId)).BeginInit();
this.GrpServerStatus.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.NUDVerificationCode)).BeginInit();
this.SuspendLayout();
//
// TxtCommand
@ -233,6 +249,7 @@ namespace GrasscutterTools
// GrpCommand
//
resources.ApplyResources(this.GrpCommand, "GrpCommand");
this.GrpCommand.Controls.Add(this.BtnInvokeOpenCommand);
this.GrpCommand.Controls.Add(this.BtnCopy);
this.GrpCommand.Controls.Add(this.ChkAutoCopy);
this.GrpCommand.Controls.Add(this.TxtCommand);
@ -1345,46 +1362,28 @@ namespace GrasscutterTools
//
// TPRemoteCall
//
this.TPRemoteCall.Controls.Add(this.LblRemoteTODO);
this.TPRemoteCall.Controls.Add(this.GrpServerStatus);
this.TPRemoteCall.Controls.Add(this.GrpRemoteCommand);
this.TPRemoteCall.Controls.Add(this.TxtHost);
this.TPRemoteCall.Controls.Add(this.BtnQueryServerStatus);
this.TPRemoteCall.Controls.Add(this.LblHost);
resources.ApplyResources(this.TPRemoteCall, "TPRemoteCall");
this.TPRemoteCall.Name = "TPRemoteCall";
this.TPRemoteCall.UseVisualStyleBackColor = true;
//
// LblRemoteTODO
//
resources.ApplyResources(this.LblRemoteTODO, "LblRemoteTODO");
this.LblRemoteTODO.Name = "LblRemoteTODO";
//
// GrpRemoteCommand
//
resources.ApplyResources(this.GrpRemoteCommand, "GrpRemoteCommand");
this.GrpRemoteCommand.Controls.Add(this.LnkRCHelp);
this.GrpRemoteCommand.Controls.Add(this.LblSessionKey);
this.GrpRemoteCommand.Controls.Add(this.TxtSessionKey);
this.GrpRemoteCommand.Controls.Add(this.TxtHost);
this.GrpRemoteCommand.Controls.Add(this.LblHost);
this.GrpRemoteCommand.Controls.Add(this.BtnPingHost);
this.GrpRemoteCommand.Controls.Add(this.BtnConnectOpenCommand);
this.GrpRemoteCommand.Controls.Add(this.BtnSendVerificationCode);
this.GrpRemoteCommand.Controls.Add(this.LblRemotePlayerId);
this.GrpRemoteCommand.Controls.Add(this.NUDVerificationCode);
this.GrpRemoteCommand.Controls.Add(this.LblVerificationCode);
this.GrpRemoteCommand.Controls.Add(this.NUDRemotePlayerId);
this.GrpRemoteCommand.Name = "GrpRemoteCommand";
this.GrpRemoteCommand.TabStop = false;
//
// LnkRCHelp
//
resources.ApplyResources(this.LnkRCHelp, "LnkRCHelp");
this.LnkRCHelp.Name = "LnkRCHelp";
this.LnkRCHelp.TabStop = true;
this.LnkRCHelp.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkRCHelp_LinkClicked);
//
// LblSessionKey
//
resources.ApplyResources(this.LblSessionKey, "LblSessionKey");
this.LblSessionKey.Name = "LblSessionKey";
//
// TxtSessionKey
//
resources.ApplyResources(this.TxtSessionKey, "TxtSessionKey");
this.TxtSessionKey.Name = "TxtSessionKey";
//
// TxtHost
//
resources.ApplyResources(this.TxtHost, "TxtHost");
@ -1395,12 +1394,129 @@ namespace GrasscutterTools
resources.ApplyResources(this.LblHost, "LblHost");
this.LblHost.Name = "LblHost";
//
// BtnPingHost
// BtnQueryServerStatus
//
resources.ApplyResources(this.BtnPingHost, "BtnPingHost");
this.BtnPingHost.Name = "BtnPingHost";
this.BtnPingHost.UseVisualStyleBackColor = true;
this.BtnPingHost.Click += new System.EventHandler(this.BtnPingHost_Click);
resources.ApplyResources(this.BtnQueryServerStatus, "BtnQueryServerStatus");
this.BtnQueryServerStatus.Name = "BtnQueryServerStatus";
this.BtnQueryServerStatus.UseVisualStyleBackColor = true;
this.BtnQueryServerStatus.Click += new System.EventHandler(this.BtnQueryServerStatus_Click);
//
// NUDRemotePlayerId
//
resources.ApplyResources(this.NUDRemotePlayerId, "NUDRemotePlayerId");
this.NUDRemotePlayerId.Maximum = new decimal(new int[] {
2147483647,
0,
0,
0});
this.NUDRemotePlayerId.Name = "NUDRemotePlayerId";
this.NUDRemotePlayerId.Value = new decimal(new int[] {
10001,
0,
0,
0});
//
// LblRemotePlayerId
//
resources.ApplyResources(this.LblRemotePlayerId, "LblRemotePlayerId");
this.LblRemotePlayerId.Name = "LblRemotePlayerId";
//
// LblServerVersionLabel
//
resources.ApplyResources(this.LblServerVersionLabel, "LblServerVersionLabel");
this.LblServerVersionLabel.Name = "LblServerVersionLabel";
//
// LblServerVersion
//
resources.ApplyResources(this.LblServerVersion, "LblServerVersion");
this.LblServerVersion.Name = "LblServerVersion";
//
// LblPlayerCountLabel
//
resources.ApplyResources(this.LblPlayerCountLabel, "LblPlayerCountLabel");
this.LblPlayerCountLabel.Name = "LblPlayerCountLabel";
//
// LblPlayerCount
//
resources.ApplyResources(this.LblPlayerCount, "LblPlayerCount");
this.LblPlayerCount.Name = "LblPlayerCount";
//
// GrpServerStatus
//
this.GrpServerStatus.Controls.Add(this.LnkOpenCommandLabel);
this.GrpServerStatus.Controls.Add(this.LblOpenCommandSupport);
this.GrpServerStatus.Controls.Add(this.LblServerVersion);
this.GrpServerStatus.Controls.Add(this.LblPlayerCount);
this.GrpServerStatus.Controls.Add(this.LblServerVersionLabel);
this.GrpServerStatus.Controls.Add(this.LblPlayerCountLabel);
resources.ApplyResources(this.GrpServerStatus, "GrpServerStatus");
this.GrpServerStatus.Name = "GrpServerStatus";
this.GrpServerStatus.TabStop = false;
//
// LblOpenCommandSupport
//
resources.ApplyResources(this.LblOpenCommandSupport, "LblOpenCommandSupport");
this.LblOpenCommandSupport.Name = "LblOpenCommandSupport";
//
// LnkOpenCommandLabel
//
resources.ApplyResources(this.LnkOpenCommandLabel, "LnkOpenCommandLabel");
this.LnkOpenCommandLabel.Name = "LnkOpenCommandLabel";
this.LnkOpenCommandLabel.TabStop = true;
this.LnkOpenCommandLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkOpenCommandLabel_LinkClicked);
//
// LblVerificationCode
//
resources.ApplyResources(this.LblVerificationCode, "LblVerificationCode");
this.LblVerificationCode.Name = "LblVerificationCode";
//
// NUDVerificationCode
//
resources.ApplyResources(this.NUDVerificationCode, "NUDVerificationCode");
this.NUDVerificationCode.Maximum = new decimal(new int[] {
9999,
0,
0,
0});
this.NUDVerificationCode.Minimum = new decimal(new int[] {
1000,
0,
0,
0});
this.NUDVerificationCode.Name = "NUDVerificationCode";
this.NUDVerificationCode.Value = new decimal(new int[] {
1000,
0,
0,
0});
//
// BtnSendVerificationCode
//
resources.ApplyResources(this.BtnSendVerificationCode, "BtnSendVerificationCode");
this.BtnSendVerificationCode.Name = "BtnSendVerificationCode";
this.BtnSendVerificationCode.UseVisualStyleBackColor = true;
this.BtnSendVerificationCode.Click += new System.EventHandler(this.BtnSendVerificationCode_Click);
//
// BtnConnectOpenCommand
//
resources.ApplyResources(this.BtnConnectOpenCommand, "BtnConnectOpenCommand");
this.BtnConnectOpenCommand.Name = "BtnConnectOpenCommand";
this.BtnConnectOpenCommand.UseVisualStyleBackColor = true;
this.BtnConnectOpenCommand.Click += new System.EventHandler(this.BtnConnectOpenCommand_Click);
//
// BtnInvokeOpenCommand
//
resources.ApplyResources(this.BtnInvokeOpenCommand, "BtnInvokeOpenCommand");
this.BtnInvokeOpenCommand.Name = "BtnInvokeOpenCommand";
this.BtnInvokeOpenCommand.UseVisualStyleBackColor = true;
this.BtnInvokeOpenCommand.Click += new System.EventHandler(this.BtnInvokeOpenCommand_Click);
//
// LnkRCHelp
//
resources.ApplyResources(this.LnkRCHelp, "LnkRCHelp");
this.LnkRCHelp.Name = "LnkRCHelp";
this.LnkRCHelp.TabStop = true;
this.LnkRCHelp.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LnkRCHelp_LinkClicked);
//
// FormMain
//
@ -1411,6 +1527,7 @@ namespace GrasscutterTools
this.Name = "FormMain";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FormMain_FormClosed);
this.Load += new System.EventHandler(this.FormMain_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormMain_KeyDown);
this.GrpCommand.ResumeLayout(false);
this.GrpCommand.PerformLayout();
this.TCMain.ResumeLayout(false);
@ -1473,6 +1590,10 @@ namespace GrasscutterTools
this.TPRemoteCall.PerformLayout();
this.GrpRemoteCommand.ResumeLayout(false);
this.GrpRemoteCommand.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.NUDRemotePlayerId)).EndInit();
this.GrpServerStatus.ResumeLayout(false);
this.GrpServerStatus.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.NUDVerificationCode)).EndInit();
this.ResumeLayout(false);
}
@ -1607,15 +1728,27 @@ namespace GrasscutterTools
private System.Windows.Forms.Label LblLanguage;
private System.Windows.Forms.RadioButton RbEntityOrnament;
private System.Windows.Forms.TabPage TPRemoteCall;
private System.Windows.Forms.Label LblRemoteTODO;
private System.Windows.Forms.GroupBox GrpRemoteCommand;
private System.Windows.Forms.LinkLabel LnkRCHelp;
private System.Windows.Forms.Label LblSessionKey;
private System.Windows.Forms.TextBox TxtSessionKey;
private System.Windows.Forms.TextBox TxtHost;
private System.Windows.Forms.Label LblHost;
private System.Windows.Forms.Button BtnPingHost;
private System.Windows.Forms.Button BtnQueryServerStatus;
private System.Windows.Forms.Button BtnOpenTextMap;
private System.Windows.Forms.Label LblRemotePlayerId;
private System.Windows.Forms.NumericUpDown NUDRemotePlayerId;
private System.Windows.Forms.Label LblPlayerCount;
private System.Windows.Forms.Label LblPlayerCountLabel;
private System.Windows.Forms.Label LblServerVersion;
private System.Windows.Forms.Label LblServerVersionLabel;
private System.Windows.Forms.GroupBox GrpServerStatus;
private System.Windows.Forms.LinkLabel LnkOpenCommandLabel;
private System.Windows.Forms.Label LblOpenCommandSupport;
private System.Windows.Forms.Button BtnConnectOpenCommand;
private System.Windows.Forms.Button BtnSendVerificationCode;
private System.Windows.Forms.NumericUpDown NUDVerificationCode;
private System.Windows.Forms.Label LblVerificationCode;
private System.Windows.Forms.Button BtnInvokeOpenCommand;
private System.Windows.Forms.LinkLabel LnkRCHelp;
private System.Windows.Forms.ToolTip TTip;
}
}

View File

@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GrasscutterTools.DispatchServer;
using GrasscutterTools.Game;
using GrasscutterTools.OpenCommand;
using GrasscutterTools.Properties;
namespace GrasscutterTools
@ -62,6 +65,7 @@ namespace GrasscutterTools
InitGiveItemRecord();
InitSpawnRecord();
InitOpenCommand();
}
catch (Exception ex)
{
@ -79,6 +83,7 @@ namespace GrasscutterTools
SaveCustomCommands();
SaveGiveItemRecord();
SaveSpawnRecord();
SaveOpenCommand();
}
catch (Exception ex)
{
@ -834,6 +839,8 @@ namespace GrasscutterTools
TxtCommand.Text = command;
if (ChkAutoCopy.Checked)
CopyCommand();
if (ModifierKeys == Keys.Control)
OnOpenCommandInvoke();
}
private void SetCommand(string command, string args)
@ -869,6 +876,19 @@ namespace GrasscutterTools
btn.Enabled = true;
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
OnOpenCommandInvoke();
}
}
private void ShowTip(string message, Control control)
{
TTip.Show(message, control, 0, control.Size.Height, 3000);
}
#endregion - -
#region - -
@ -897,16 +917,166 @@ namespace GrasscutterTools
#region - -
private void LnkRCHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
private void InitOpenCommand()
{
MessageBox.Show("TODO正在开发中", "TODO");
NUDRemotePlayerId.Value = Settings.Default.RemoteUid;
TxtHost.Text = Settings.Default.Host;
}
private void BtnPingHost_Click(object sender, EventArgs e)
private void SaveOpenCommand()
{
MessageBox.Show("TODO正在开发中", "TODO");
Settings.Default.RemoteUid = NUDRemotePlayerId.Value;
Settings.Default.Host = TxtHost.Text;
}
private OpenCommandAPI OC;
private async void BtnQueryServerStatus_Click(object sender, EventArgs e)
{
var btn = sender as Button;
btn.Enabled = false;
btn.Cursor = Cursors.WaitCursor;
try
{
var status = await DispatchServerAPI.QueryServerStatus(TxtHost.Text);
LblServerVersion.Text = status.Version;
LblPlayerCount.Text = status.PlayerCount.ToString();
OC = new OpenCommandAPI(TxtHost.Text);
if (await OC.Ping())
{
LblOpenCommandSupport.Text = "√";
LblOpenCommandSupport.ForeColor = Color.Green;
GrpRemoteCommand.Enabled = true;
}
else
{
LblOpenCommandSupport.Text = "×";
LblOpenCommandSupport.ForeColor = Color.Red;
GrpRemoteCommand.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btn.Cursor = Cursors.Default;
btn.Enabled = true;
}
}
private async void BtnSendVerificationCode_Click(object sender, EventArgs e)
{
var btn = sender as Button;
var t = btn.Text;
btn.Enabled = false;
NUDRemotePlayerId.Enabled = false;
try
{
btn.Text = "发送中...";
await OC.SendCode((int)NUDRemotePlayerId.Value);
BtnConnectOpenCommand.Enabled = true;
NUDVerificationCode.Enabled = true;
NUDVerificationCode.Focus();
for (int i = 60; i > 0 && !OC.CanInvoke; i--)
{
btn.Text = $"{i} 秒后可重发";
await Task.Delay(1000);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btn.Text = t;
btn.Enabled = true;
NUDRemotePlayerId.Enabled = true;
}
}
private async void BtnConnectOpenCommand_Click(object sender, EventArgs e)
{
var btn = sender as Button;
btn.Enabled = false;
try
{
await OC.Verify((int)NUDVerificationCode.Value);
GrpRemoteCommand.Enabled = false;
BtnInvokeOpenCommand.Focus();
ShowTip("现在你可以远程执行命令了哦!", BtnInvokeOpenCommand);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btn.Cursor = Cursors.Default;
btn.Enabled = true;
}
}
private void OnOpenCommandInvoke()
{
BtnInvokeOpenCommand_Click(BtnInvokeOpenCommand, EventArgs.Empty);
}
private async void BtnInvokeOpenCommand_Click(object sender, EventArgs e)
{
if (TxtCommand.Text.Length < 2)
{
ShowTip("命令不能为空", TxtCommand);
return;
}
if (OC == null || !OC.CanInvoke)
{
ShowTip("请先连接到支持[OpenCommand]的服务器", BtnInvokeOpenCommand);
TCMain.SelectedTab = TPRemoteCall;
return;
}
var cmd = TxtCommand.Text.Substring(1);
var btn = sender as Button;
btn.Enabled = false;
try
{
var msg = await OC.Invoke(cmd);
ShowTip(msg, btn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btn.Cursor = Cursors.Default;
btn.Enabled = true;
}
}
private void LnkOpenCommandLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
System.Diagnostics.Process.Start("https://github.com/jie65535/gc-opencommand-plugin");
}
catch (Exception)
{
MessageBox.Show("浏览器打开失败,你可以通过以下链接手动访问:\n"
+ "https://github.com/jie65535/gc-opencommand-plugin",
"错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void LnkRCHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("1. 填写正确的UID\n2. 向玩家发送验证码\n3. 输入正确的验证码\n4. 连接\n5. 享受", "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
}
}

File diff suppressed because it is too large Load Diff

View File

@ -192,6 +192,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DispatchServer\DispatchServerAPI.cs" />
<Compile Include="DispatchServer\Model\ServerStatus.cs" />
<Compile Include="FormGachaBannerEditor.cs">
<SubType>Form</SubType>
</Compile>
@ -217,10 +219,11 @@
<Compile Include="Game\ItemMap.cs" />
<Compile Include="Game\SetStatsCommand.cs" />
<Compile Include="Game\TextMapData.cs" />
<Compile Include="MojoConsole\MojoConsole.cs" />
<Compile Include="OpenCommand\OpenCommandAPI.cs" />
<Compile Include="MultiLanguage.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\HttpHelper.cs" />
<EmbeddedResource Include="FormGachaBannerEditor.en-us.resx">
<DependentUpon>FormGachaBannerEditor.cs</DependentUpon>
</EmbeddedResource>

View File

@ -1,89 +0,0 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GrasscutterTools.MojoConsole
{
internal class MojoConsole
{
static readonly HttpClient httpClient = new HttpClient();
public string Host { get; set; }
private string API => Host + "/mojoplus/api";
public string Key { get; set; }
public async Task<string> Invoke(string payload)
{
var response = await DoRequest(new Request(Key, "invoke", payload));
if (response.Code == 500)
throw new InvokeException(response.Payload);
return response.Payload;
}
public async Task<bool> Ping()
{
try
{
var response = await DoRequest(new Request(Key, "ping"));
return response?.Code == 200;
}
catch (Exception)
{
return false;
}
}
private async Task<Response> DoRequest(Request request)
{
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(API, content);
var responseString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Response>(responseString);
}
public class Request
{
public Request(string k, string method, string payload = "")
{
K = k;
Method = method;
Payload = payload;
}
[JsonProperty("k")]
public string K { get; set; }
[JsonProperty("request")]
public string Method { get; set; }
[JsonProperty("payload")]
public string Payload { get; set; }
}
public class Response
{
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("code")]
public int Code { get; set; }
[JsonProperty("payload")]
public string Payload { get; set; }
}
internal class InvokeException : Exception
{
public InvokeException(string message) : base(message)
{
}
}
}
}

View File

@ -0,0 +1,105 @@
using System;
using System.Threading.Tasks;
using GrasscutterTools.Utils;
using Newtonsoft.Json;
namespace GrasscutterTools.OpenCommand
{
public class OpenCommandAPI
{
public OpenCommandAPI(string host)
{
Host = host;
}
public string Host { get; }
private string API => Host + "/opencommand/api";
public string Token { get; set; }
public bool CanInvoke { get; private set; }
public async Task<bool> Ping()
{
try
{
var response = await DoRequest("ping");
return response.RetCode == 200;
}
catch (Exception)
{
return false;
}
}
public async Task SendCode(int playerId)
{
var response = await DoRequest("sendCode", playerId);
Token = response.Data as string;
}
public async Task Verify(int code)
{
await DoRequest("verify", code);
CanInvoke = true;
}
public async Task<string> Invoke(string command)
{
var response = await DoRequest("command", command);
return response.Data as string;
}
private async Task<Response> DoRequest(string action, object data = null)
{
var response = await HttpHelper.PostAsync<Response>(API, new Request(Token, action, data));
if (response.RetCode == 401)
Token = "";
if (response.RetCode != 200)
throw new InvokeException(response.Message);
return response;
}
public class Request
{
public Request(string token, string action, object data)
{
Token = token;
Action = action;
Data = data;
}
[JsonProperty("token")]
public string Token { get; set; }
[JsonProperty("action")]
public string Action { get; set; }
[JsonProperty("data")]
public object Data { get; set; }
}
public class Response
{
[JsonProperty("retcode")]
public int RetCode { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("data")]
public object Data { get; set; }
}
internal class InvokeException : Exception
{
public InvokeException(string message) : base(message)
{
}
}
}
}

View File

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.10.0")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]

View File

@ -82,5 +82,29 @@ namespace GrasscutterTools.Properties {
this["TextMapFileName"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("10001")]
public decimal RemoteUid {
get {
return ((decimal)(this["RemoteUid"]));
}
set {
this["RemoteUid"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("https://127.0.0.1")]
public string Host {
get {
return ((string)(this["Host"]));
}
set {
this["Host"] = value;
}
}
}
}

View File

@ -17,5 +17,11 @@
<Setting Name="TextMapFileName" Type="System.String" Scope="User">
<Value Profile="(Default)">TextMapCHS</Value>
</Setting>
<Setting Name="RemoteUid" Type="System.Decimal" Scope="User">
<Value Profile="(Default)">10001</Value>
</Setting>
<Setting Name="Host" Type="System.String" Scope="User">
<Value Profile="(Default)">https://127.0.0.1</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -0,0 +1,53 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GrasscutterTools.Utils
{
public static class HttpHelper
{
static readonly HttpClient httpClient = new HttpClient();
static HttpHelper()
{
ServicePointManager.ServerCertificateValidationCallback = (_, _1, _2, _3) => true;
}
public static async Task<T> GetAsync<T>(string url)
{
try
{
var responseMessage = await httpClient.GetAsync(url);
var responseString = await responseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseString);
}
catch (Exception ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
}
public static async Task<T> PostAsync<T>(string url, object obj)
{
try
{
var content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
var responseMessage = await httpClient.PostAsync(url, content);
var responseString = await responseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseString);
}
catch (Exception ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
}
}
}