-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ChanyaVRC/dev-chatbox
Implement `OscChatbox`. #21
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\vrcosclib\vrcosclib.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using BuildSoft.VRChat.Osc.Chatbox; | ||
|
||
while (true) | ||
{ | ||
Console.Write("Text: "); | ||
string? text = Console.ReadLine(); | ||
if (text == null) | ||
{ | ||
Console.WriteLine("Cannot Send Text."); | ||
continue; | ||
} | ||
if (text != "") | ||
{ | ||
OscChatbox.SetIsTyping(true); | ||
await Task.Delay(1000); | ||
OscChatbox.SendMessage(text, true); | ||
OscChatbox.SetIsTyping(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Text; | ||
using BuildSoft.OscCore; | ||
using BuildSoft.VRChat.Osc.Test; | ||
using NUnit.Framework; | ||
|
||
namespace BuildSoft.VRChat.Osc.Chatbox.Test; | ||
|
||
[TestOf(typeof(OscChatbox))] | ||
public class OscChatboxTest | ||
{ | ||
private OscServer _server = null!; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_server = new OscServer(OscUtility.SendPort); | ||
|
||
OscParameter.Parameters.Clear(); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
_server.Dispose(); | ||
} | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
|
||
} | ||
|
||
[OneTimeTearDown] | ||
public void OneTimeTearDown() | ||
{ | ||
|
||
} | ||
|
||
|
||
[TestCase("", true)] | ||
[TestCase("ASCII", true)] | ||
[TestCase("ASCII", false)] | ||
[TestCase("UTF-8", true)] | ||
[TestCase("😂😭😪😥😰😅😓😩😫😨😱", true)] | ||
public async Task SendMessageTest(string message, bool direct) | ||
{ | ||
OscMessageValues value = null!; | ||
void valueReadMethod(OscMessageValues v) => value = v; | ||
_server.TryAddMethod(OscChatbox.InputAddress, valueReadMethod); | ||
byte[] recievedMessage = new byte[2024]; | ||
|
||
|
||
OscChatbox.SendMessage(message, direct); | ||
await TestUtility.LoopWhile(() => value == null, TestUtility.LatencyTimeout); | ||
int length = value.ReadStringElementBytes(0, recievedMessage); | ||
bool recievedDirect = value.ReadBooleanElement(1); | ||
|
||
|
||
Assert.AreEqual(message, Encoding.UTF8.GetString(recievedMessage, 0, length)); | ||
Assert.AreEqual(direct, recievedDirect); | ||
} | ||
|
||
[TestCase(true)] | ||
[TestCase(false)] | ||
public async Task SetIsTypingTest(bool isTyping) | ||
{ | ||
OscMessageValues value = null!; | ||
void valueReadMethod(OscMessageValues v) => value = v; | ||
_server.TryAddMethod(OscChatbox.TypingAddress, valueReadMethod); | ||
|
||
|
||
OscChatbox.SetIsTyping(isTyping); | ||
await TestUtility.LoopWhile(() => value == null, TestUtility.LatencyTimeout); | ||
bool recievedIsTyping = value.ReadBooleanElement(0); | ||
|
||
|
||
Assert.AreEqual(isTyping, recievedIsTyping); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using BuildSoft.OscCore; | ||
|
||
namespace BuildSoft.VRChat.Osc.Chatbox; | ||
public static class OscChatbox | ||
{ | ||
public static string InputAddress = "/chatbox/input"; | ||
public static string TypingAddress = "/chatbox/typing"; | ||
public static void SendMessage(string message, bool direct) | ||
{ | ||
OscClient client = OscUtility.Client; | ||
OscWriter writer = client.Writer; | ||
var socket = client.Socket; | ||
|
||
writer.Reset(); | ||
writer.Write(InputAddress); | ||
writer.Write(direct ? ",sT" : ",sF"); | ||
writer.WriteUtfString(message); | ||
socket.Send(writer.Buffer, writer.Length, SocketFlags.None); | ||
} | ||
|
||
public static void SetIsTyping(bool isTyping) | ||
{ | ||
OscParameter.SendValue(TypingAddress, isTyping); | ||
} | ||
|
||
private static void WriteUtfString(this OscWriter writer, string data) | ||
{ | ||
var utf8String = Encoding.UTF8.GetBytes(data); | ||
Array.Resize(ref utf8String, utf8String.Length + (4 - utf8String.Length % 4)); | ||
|
||
for (int i = 3; i < utf8String.Length; i += 4) | ||
{ | ||
int data1 = | ||
utf8String[i - 3] << 8 * 3 | ||
| utf8String[i - 2] << 8 * 2 | ||
| utf8String[i - 1] << 8 * 1 | ||
| utf8String[i - 0] << 8 * 0; | ||
writer.Write(data1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters