Skip to content

Commit

Permalink
Added Web Socket Chat Bot
Browse files Browse the repository at this point in the history
Web Socket Chat Bot
  • Loading branch information
milutinke authored Jun 3, 2023
2 parents 9855e2e + 95f6c57 commit 051ee70
Show file tree
Hide file tree
Showing 9 changed files with 1,670 additions and 25 deletions.
1,341 changes: 1,341 additions & 0 deletions MinecraftClient/ChatBots/WebSocketBot.cs

Large diffs are not rendered by default.

200 changes: 176 additions & 24 deletions MinecraftClient/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ public static JSONData ParseJson(string json)
/// </summary>
public class JSONData
{
public enum DataType { Object, Array, String };
public enum DataType
{
Object,
Array,
String
};

private readonly DataType type;
public DataType Type { get { return type; } }

public DataType Type
{
get { return type; }
}

public Dictionary<string, JSONData> Properties;
public List<JSONData> DataArray;
public string StringValue;

public JSONData(DataType datatype)
{
type = datatype;
Expand Down Expand Up @@ -63,12 +75,21 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
if (toparse[cursorpos] == '"')
{
JSONData propertyname = String2Data(toparse, ref cursorpos);
if (toparse[cursorpos] == ':') { cursorpos++; } else { /* parse error ? */ }
if (toparse[cursorpos] == ':')
{
cursorpos++;
}
else
{
/* parse error ? */
}

JSONData propertyData = String2Data(toparse, ref cursorpos);
data.Properties[propertyname.StringValue] = propertyData;
}
else cursorpos++;
}

cursorpos++;
break;

Expand All @@ -79,10 +100,15 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
SkipSpaces(toparse, ref cursorpos);
while (toparse[cursorpos] != ']')
{
if (toparse[cursorpos] == ',') { cursorpos++; }
if (toparse[cursorpos] == ',')
{
cursorpos++;
}

JSONData arrayItem = String2Data(toparse, ref cursorpos);
data.DataArray.Add(arrayItem);
}

cursorpos++;
break;

Expand All @@ -103,9 +129,11 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
&& IsHex(toparse[cursorpos + 5]))
{
//"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));
cursorpos += 6; continue;
cursorpos += 6;
continue;
}
else if (toparse[cursorpos + 1] == 'n')
{
Expand All @@ -127,12 +155,20 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
}
else cursorpos++; //Normal character escapement \"
}
catch (IndexOutOfRangeException) { cursorpos++; } // \u01<end of string>
catch (ArgumentOutOfRangeException) { cursorpos++; } // Unicode index 0123 was invalid
catch (IndexOutOfRangeException)
{
cursorpos++;
} // \u01<end of string>
catch (ArgumentOutOfRangeException)
{
cursorpos++;
} // Unicode index 0123 was invalid
}

data.StringValue += toparse[cursorpos];
cursorpos++;
}

cursorpos++;
break;

Expand All @@ -151,47 +187,93 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
case '-':
data = new JSONData(JSONData.DataType.String);
StringBuilder sb = new();
while ((toparse[cursorpos] >= '0' && toparse[cursorpos] <= '9') || toparse[cursorpos] == '.' || toparse[cursorpos] == '-')
while ((toparse[cursorpos] >= '0' && toparse[cursorpos] <= '9') || toparse[cursorpos] == '.' ||
toparse[cursorpos] == '-')
{
sb.Append(toparse[cursorpos]);
cursorpos++;
}

data.StringValue = sb.ToString();
break;

//Boolean : true
case 't':
data = new JSONData(JSONData.DataType.String);
cursorpos++;
if (toparse[cursorpos] == 'r') { cursorpos++; }
if (toparse[cursorpos] == 'u') { cursorpos++; }
if (toparse[cursorpos] == 'e') { cursorpos++; data.StringValue = "true"; }
if (toparse[cursorpos] == 'r')
{
cursorpos++;
}

if (toparse[cursorpos] == 'u')
{
cursorpos++;
}

if (toparse[cursorpos] == 'e')
{
cursorpos++;
data.StringValue = "true";
}

break;

//Boolean : false
case 'f':
data = new JSONData(JSONData.DataType.String);
cursorpos++;
if (toparse[cursorpos] == 'a') { cursorpos++; }
if (toparse[cursorpos] == 'l') { cursorpos++; }
if (toparse[cursorpos] == 's') { cursorpos++; }
if (toparse[cursorpos] == 'e') { cursorpos++; data.StringValue = "false"; }
if (toparse[cursorpos] == 'a')
{
cursorpos++;
}

if (toparse[cursorpos] == 'l')
{
cursorpos++;
}

if (toparse[cursorpos] == 's')
{
cursorpos++;
}

if (toparse[cursorpos] == 'e')
{
cursorpos++;
data.StringValue = "false";
}

break;

//Null field
case 'n':
data = new JSONData(JSONData.DataType.String);
cursorpos++;
if (toparse[cursorpos] == 'u') { cursorpos++; }
if (toparse[cursorpos] == 'l') { cursorpos++; }
if (toparse[cursorpos] == 'l') { cursorpos++; data.StringValue = "null"; }
if (toparse[cursorpos] == 'u')
{
cursorpos++;
}

if (toparse[cursorpos] == 'l')
{
cursorpos++;
}

if (toparse[cursorpos] == 'l')
{
cursorpos++;
data.StringValue = "null";
}

break;

//Unknown data
default:
cursorpos++;
return String2Data(toparse, ref cursorpos);
}

SkipSpaces(toparse, ref cursorpos);
return data;
}
Expand All @@ -206,7 +288,10 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
/// </summary>
/// <param name="c">Char to test</param>
/// <returns>True if hexadecimal</returns>
private static bool IsHex(char c) { return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')); }
private static bool IsHex(char c)
{
return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'));
}

/// <summary>
/// Advance the cursor to skip white spaces and line breaks
Expand All @@ -216,10 +301,77 @@ private static JSONData String2Data(string toparse, ref int cursorpos)
private static void SkipSpaces(string toparse, ref int cursorpos)
{
while (cursorpos < toparse.Length
&& (char.IsWhiteSpace(toparse[cursorpos])
|| toparse[cursorpos] == '\r'
|| toparse[cursorpos] == '\n'))
&& (char.IsWhiteSpace(toparse[cursorpos])
|| toparse[cursorpos] == '\r'
|| toparse[cursorpos] == '\n'))
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)
{
var c = src[i];
return c < 32 || c == '"' || c == '\\'
// Broken lead surrogate
|| (c is >= '\uD800' and <= '\uDBFF' &&
(i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF'))
// Broken tail surrogate
|| (c is >= '\uDC00' and <= '\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)
{
var sb = new StringBuilder();
var start = 0;

for (var i = 0; i < src.Length; i++)
{
if (!NeedEscape(src, i)) continue;
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();
}
}
}
}
1 change: 1 addition & 0 deletions MinecraftClient/McClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ private void RegisterBots(bool reload = false)
if (Config.ChatBot.ScriptScheduler.Enabled) { BotLoad(new ScriptScheduler()); }
if (Config.ChatBot.TelegramBridge.Enabled) { BotLoad(new TelegramBridge()); }
if (Config.ChatBot.ItemsCollector.Enabled) { BotLoad(new ItemsCollector()); }
if (Config.ChatBot.WebSocketBot.Enabled) { BotLoad(new WebSocketBot()); }
//Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot());
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions MinecraftClient/Resources/ConfigComments/ConfigComments.resx
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,19 @@ If the connection to the Minecraft game server is blocked by the firewall, set E
<data name="ChatBot.ItemsCollector" xml:space="preserve">
<value>A Chat Bot that collects items on the ground</value>
</data>
<data name="ChatBot.WebSocketBot" xml:space="preserve">
<value>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.</value>
</data>
<data name="ChatBot.WebSocketBot.Ip" xml:space="preserve">
<value>The IP address that Websocket server will be bound to.</value>
</data>
<data name="ChatBot.WebSocketBot.Port" xml:space="preserve">
<value>The Port that Websocket server will be bounded to.</value>
</data>
<data name="ChatBot.WebSocketBot.Password" xml:space="preserve">
<value>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).</value>
</data>
<data name="ChatBot.WebSocketBot.DebugMode" xml:space="preserve">
<value>This setting is for developers who are developing a library that uses this chat bot to remotely execute procedures/commands/functions.</value>
</data>
</root>
Loading

0 comments on commit 051ee70

Please sign in to comment.