Added the Web Socket Chat Bot.

This commit is contained in:
Milutinke 2022-10-20 19:29:35 +02:00
parent 0e7423d1d9
commit ed910aa9c7
9 changed files with 1272 additions and 3 deletions

3
.gitmodules vendored
View file

@ -2,3 +2,6 @@
path = ConsoleInteractive path = ConsoleInteractive
url = https://github.com/breadbyte/ConsoleInteractive url = https://github.com/breadbyte/ConsoleInteractive
branch = main branch = main
[submodule "websocket-sharp"]
path = websocket-sharp
url = https://github.com/sta/websocket-sharp.git

File diff suppressed because it is too large Load diff

View file

@ -103,7 +103,7 @@ namespace MinecraftClient
&& IsHex(toparse[cursorpos + 5])) && IsHex(toparse[cursorpos + 5]))
{ {
//"abc\u0123abc" => "0123" => 0123 => Unicode char n°0123 => Add char to string //"abc\u0123abc" => "0123" => 0123 => Unicode char n°0123 => Add char to string
data.StringValue += char.ConvertFromUtf32(int.Parse(toparse.Substring(cursorpos + 2, 4), data.StringValue += char.ConvertFromUtf32(int.Parse(toparse.Substring(cursorpos + 2, 4),
System.Globalization.NumberStyles.HexNumber)); System.Globalization.NumberStyles.HexNumber));
cursorpos += 6; continue; cursorpos += 6; continue;
} }
@ -221,5 +221,60 @@ namespace MinecraftClient
|| toparse[cursorpos] == '\n')) || toparse[cursorpos] == '\n'))
cursorpos++; cursorpos++;
} }
// Original: https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs
private static bool NeedEscape(string src, int i)
{
char c = src[i];
return c < 32 || c == '"' || c == '\\'
// Broken lead surrogate
|| (c >= '\uD800' && c <= '\uDBFF' &&
(i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF'))
// Broken tail surrogate
|| (c >= '\uDC00' && c <= '\uDFFF' &&
(i == 0 || src[i - 1] < '\uD800' || src[i - 1] > '\uDBFF'))
// To produce valid JavaScript
|| c == '\u2028' || c == '\u2029'
// Escape "</" for <script> tags
|| (c == '/' && i > 0 && src[i - 1] == '<');
}
public static string EscapeString(string src)
{
StringBuilder sb = new StringBuilder();
int start = 0;
for (int i = 0; i < src.Length; i++)
{
if (NeedEscape(src, i))
{
sb.Append(src, start, i - start);
switch (src[i])
{
case '\b': sb.Append("\\b"); break;
case '\f': sb.Append("\\f"); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
case '\"': sb.Append("\\\""); break;
case '\\': sb.Append("\\\\"); break;
case '/': sb.Append("\\/"); break;
default:
sb.Append("\\u");
sb.Append(((int)src[i]).ToString("x04"));
break;
}
start = i + 1;
}
}
sb.Append(src, start, src.Length - start);
return sb.ToString();
}
} }
} }

View file

@ -273,6 +273,7 @@ namespace MinecraftClient
if (Config.ChatBot.RemoteControl.Enabled) { BotLoad(new RemoteControl()); } if (Config.ChatBot.RemoteControl.Enabled) { BotLoad(new RemoteControl()); }
if (Config.ChatBot.ReplayCapture.Enabled && reload) { BotLoad(new ReplayCapture()); } if (Config.ChatBot.ReplayCapture.Enabled && reload) { BotLoad(new ReplayCapture()); }
if (Config.ChatBot.ScriptScheduler.Enabled) { BotLoad(new ScriptScheduler()); } if (Config.ChatBot.ScriptScheduler.Enabled) { BotLoad(new ScriptScheduler()); }
if (Config.ChatBot.WebSocketBot.Enabled) { BotLoad(new WebSocketBot()); }
//Add your ChatBot here by uncommenting and adapting //Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot()); //BotLoad(new ChatBots.YourBot());
} }

View file

@ -39,6 +39,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" /> <PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Samboy063.Tomlet" Version="5.0.0" /> <PackageReference Include="Samboy063.Tomlet" Version="5.0.0" />
<PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" /> <PackageReference Include="SingleFileExtractor.Core" Version="1.0.1" />
<PackageReference Include="starksoft.aspen" Version="1.1.8"> <PackageReference Include="starksoft.aspen" Version="1.1.8">
@ -72,6 +73,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" /> <ProjectReference Include="..\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive\ConsoleInteractive.csproj" />
<ProjectReference Include="..\websocket-sharp\websocket-sharp\websocket-sharp.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Update="DefaultConfigResource.resx"> <EmbeddedResource Update="DefaultConfigResource.resx">

View file

@ -700,6 +700,17 @@ bot.scriptScheduler.running_inverval=Interval / Running action: {0}
bot.scriptScheduler.running_login=Login / Running action: {0} bot.scriptScheduler.running_login=Login / Running action: {0}
bot.scriptScheduler.task=triggeronfirstlogin: {0}\n triggeronlogin: {1}\n triggerontime: {2}\n triggeroninterval: {3}\n timevalue: {4}\n timeinterval: {5}\n action: {6} bot.scriptScheduler.task=triggeronfirstlogin: {0}\n triggeronlogin: {1}\n triggerontime: {2}\n triggeroninterval: {3}\n timevalue: {4}\n timeinterval: {5}\n action: {6}
# WebSocketBot
bot.WebSocketBot.session_id_changed=§bSession with an id §a{0}§b has been renamed to: §a{1}§b!
bot.WebSocketBot.session_authenticated=§bSession with an id §a{0}§b has been succesfully authenticated!
bot.WebSocketBot.failed_to_start.ip=§cFailed to start a server! The provided IP address is not a valid one!
bot.WebSocketBot.failed_to_start.port=§cFailed to start a server! The port number provided is out of the range, it must be 65535 or bellow it!
bot.WebSocketBot.failed_to_start.custom=§cFailed to start a server:\n\n{0}\n\n
bot.WebSocketBot.starting=Starting the Websocket server...
bot.WebSocketBot.started=§bServer started on ip §a{0}§b port: §a{1}
bot.WebSocketBot.new_session=§bNew session connected: §a{0}
bot.WebSocketBot.session_disconnected=§bSession with an id §a{0}§b has disconnected!
# TestBot # TestBot
bot.testBot.told=Bot: {0} told me : {1} bot.testBot.told=Bot: {0} told me : {1}
bot.testBot.said=Bot: {0} said : {1} bot.testBot.said=Bot: {0} said : {1}
@ -940,3 +951,9 @@ config.ChatBot.ReplayCapture.Backup_Interval=How long should replay file be auto
# ChatBot.ScriptScheduler # ChatBot.ScriptScheduler
config.ChatBot.ScriptScheduler=Schedule commands and scripts to launch on various events such as server join, date/time or time interval\n# See https://mccteam.github.io/guide/chat-bots.html#script-scheduler for more info config.ChatBot.ScriptScheduler=Schedule commands and scripts to launch on various events such as server join, date/time or time interval\n# See https://mccteam.github.io/guide/chat-bots.html#script-scheduler for more info
# ChatBot.WebSocketBot
config.ChatBot.WebSocketBot=Remotely control the client using Web Sockets.\n# This is useful if you want to implement an application that can remotely and asynchronously execute procedures in MCC.\n# Example implementation written in JavaScript: https://github.com/milutinke/MCC.js.git\n# The protocol specification will be available in the documentation soon.
config.ChatBot.WebSocketBot.Ip=The IP address that Websocket server will be bounded to.
config.ChatBot.WebSocketBot.Port=The Port that Websocket server will be bounded to.
config.ChatBot.WebSocketBot.Password=A password that will be used to authenticate on thw Websocket server (It is recommended to change the default password and to set a strong one).

View file

@ -1009,7 +1009,7 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Send Entity Action /// Send Entity Action
/// </summary> /// </summary>
private bool SendEntityAction(Protocol.EntityActionType entityAction) protected bool SendEntityAction(Protocol.EntityActionType entityAction)
{ {
return Handler.SendEntityAction(entityAction); return Handler.SendEntityAction(entityAction);
} }

View file

@ -567,7 +567,7 @@ namespace MinecraftClient
public enum ForgeConfigType { no, auto, force }; public enum ForgeConfigType { no, auto, force };
public enum TerminalColorDepthType { bit_4, bit_8, bit_24}; public enum TerminalColorDepthType { bit_4, bit_8, bit_24 };
} }
public struct AccountInfoConfig public struct AccountInfoConfig
@ -1152,6 +1152,13 @@ namespace MinecraftClient
get { return ChatBots.ScriptScheduler.Config; } get { return ChatBots.ScriptScheduler.Config; }
set { ChatBots.ScriptScheduler.Config = value; ChatBots.ScriptScheduler.Config.OnSettingUpdate(); } set { ChatBots.ScriptScheduler.Config = value; ChatBots.ScriptScheduler.Config.OnSettingUpdate(); }
} }
[TomlPrecedingComment("$config.ChatBot.WebSocketBot$")]
public ChatBots.WebSocketBot.Configs WebSocketBot
{
get { return ChatBots.WebSocketBot.Config!; }
set { ChatBots.WebSocketBot.Config = value; }
}
} }
} }

1
websocket-sharp Submodule

@ -0,0 +1 @@
Subproject commit 8e175845420d7dd707d7636747a71a0b5037ba91