1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-09 22:55:42 +00:00

Add bindings for vp_say and vp_console_message

This commit is contained in:
Oliver Booth 2022-11-27 20:10:33 +00:00
parent 693e90bff1
commit 99fc7120c7
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 150 additions and 5 deletions

View File

@ -1,3 +1,4 @@
using System.Drawing;
using System.Numerics;
using VpSharp.Extensions;
using VpSharp.Internal;
@ -163,6 +164,101 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
return Task.CompletedTask;
}
/// <summary>
/// Sends a console message to the avatar with no name.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="fontStyle">The font style of the message.</param>
/// <param name="color">The text color of the message.</param>
/// <returns>The message which was sent.</returns>
/// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception>
/// <exception cref="InvalidOperationException">
/// <para>An attempt was made to send a message while not connected to a world.</para>
/// -or-
/// <para>An attempt was made to send a message to an avatar that is not in the world.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="message" /> is empty, or consists of only whitespace.</para>
/// -or-
/// <para><paramref name="message" /> is too long to send.</para>
/// </exception>
public Task<VirtualParadiseMessage> SendMessageAsync(string message, FontStyle fontStyle, Color color)
{
// ReSharper disable once InconsistentlySynchronizedField
return SendMessageAsync(null, message, fontStyle, color);
}
/// <summary>
/// Sends a console message to the avatar.
/// </summary>
/// <param name="name">The apparent author of the message.</param>
/// <param name="message">The message to send.</param>
/// <param name="fontStyle">The font style of the message.</param>
/// <param name="color">The text color of the message.</param>
/// <returns>The message which was sent.</returns>
/// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception>
/// <exception cref="InvalidOperationException">
/// <para>An attempt was made to send a message while not connected to a world.</para>
/// -or-
/// <para>An attempt was made to send a message to an avatar that is not in the world.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="message" /> is empty, or consists of only whitespace.</para>
/// -or-
/// <para><paramref name="message" /> is too long to send.</para>
/// </exception>
/// <remarks>Passing <see langword="null" /> to <paramref name="name" /> will hide the name from the recipient.</remarks>
public Task<VirtualParadiseMessage> SendMessageAsync(string? name, string message, FontStyle fontStyle, Color color)
{
ArgumentNullException.ThrowIfNull(message);
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException("Message cannot be empty.");
}
VirtualParadiseAvatar avatar;
lock (_client.Lock)
{
var reason = (ReasonCode)vp_console_message(
_client.NativeInstanceHandle,
Session,
name ?? string.Empty,
message,
(int)fontStyle,
color.R,
color.G,
color.B
);
if (reason != ReasonCode.Success)
{
switch (reason)
{
case ReasonCode.NotInWorld when _client.CurrentAvatar is null:
throw new InvalidOperationException("A connection to the world server is required to send messages.");
case ReasonCode.NotInWorld:
throw new InvalidOperationException("The recipient avatar is not in this world.");
case ReasonCode.StringTooLong:
throw new ArgumentException("The message is too long to send.");
}
}
avatar = _client.CurrentAvatar!;
}
return Task.FromResult(new VirtualParadiseMessage(
MessageType.ConsoleMessage,
name,
message,
avatar,
fontStyle,
color
));
}
/// <summary>
/// Sends a URI to this avatar.
/// </summary>

View File

@ -9,7 +9,7 @@ public sealed class VirtualParadiseMessage
{
internal VirtualParadiseMessage(
MessageType type,
string name,
string? name,
string content,
VirtualParadiseAvatar author,
FontStyle style,
@ -38,8 +38,8 @@ public sealed class VirtualParadiseMessage
/// <summary>
/// Gets the message name.
/// </summary>
/// <value>The message name.This will always be equal to the name of the <see cref="Author" /> for chat messages.</value>
public string Name { get; }
/// <value>The message name. This will always be equal to the name of the <see cref="Author" /> for chat messages.</value>
public string? Name { get; }
/// <summary>
/// Gets the message color.
@ -58,4 +58,4 @@ public sealed class VirtualParadiseMessage
/// </summary>
/// <value>The type of this message.</value>
public MessageType Type { get; }
}
}

View File

@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
@ -524,6 +524,55 @@ public sealed partial class VirtualParadiseClient : IDisposable
vp_friends_get(NativeInstanceHandle);
}
/// <summary>
/// Sends a chat message to everyone in the current world.
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>The message which was sent.</returns>
/// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception>
/// <exception cref="InvalidOperationException">
/// An attempt was made to send a message while not connected to a world.
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="message" /> is empty, or consists of only whitespace.</para>
/// -or-
/// <para><paramref name="message" /> is too long to send.</para>
/// </exception>
public Task<VirtualParadiseMessage> SendMessageAsync(string message)
{
ArgumentNullException.ThrowIfNull(message);
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException("Message cannot be empty.");
}
lock (Lock)
{
var reason = (ReasonCode)vp_say(NativeInstanceHandle, message);
if (reason != ReasonCode.Success)
{
switch (reason)
{
case ReasonCode.NotInWorld:
throw new InvalidOperationException("A connection to the world server is required to send messages.");
case ReasonCode.StringTooLong:
throw new ArgumentException("The message is too long to send.");
}
}
}
var avatar = CurrentAvatar;
return Task.FromResult(new VirtualParadiseMessage(
MessageType.ChatMessage,
avatar!.Name,
message,
avatar,
FontStyle.Regular,
Color.Black
));
}
internal TaskCompletionSource<ReasonCode> AddJoinCompletionSource(int reference)
{
var taskCompletionSource = new TaskCompletionSource<ReasonCode>();