API Reference¶
The following section outlines the API of VentFramework
Custom RPC¶
Attributes¶
- public class ModRPCAttribute : Attribute¶
The ModRPC attribute is the basis for Custom RPCs. This attribute can be put over any method (static or non-static), and it’ll be automatically picked up by the framework for custom rpc use.
Important
Non-static methods are only supported in classes implementing IRpcInstance
The following parameters are supported by ModRPC:
Any native type (bool, int, uint64, string, double, etc)
Any type implementing the interface
IRpcSendableAny List<T> where T is any of the above type
Additionally, optional parameters are allowed BUT ModRPC does not support ANY
nullvalues passed into the method.
- public ModRPCAttribute(uint rpc, RpcActors senders, RpcActors receivers, MethodInvocation invocation)¶
- rpc:The custom and unique rpc-id to send.senders:**Default: RpcActors.Everyone** the allowed sender of this RPC. Note: calls to this method from non-allowed senders ONLY blocks the RPC from being sent, based on the :type:`MethodInvocation` parameter, this method still may end up running.receivers:**Default: RpcActors.Everyone** the allowed receiver of this RPC. This rule is handled by the receiving client and NOT the sending client.invocation:If and when the code for the method should be run.
- public enum RpcActors¶
Used in ModRPC attribute to specify allowed senders / receivers of RPC
Values:None, Host, NonHost, LastSender, EveryoneNone:Ignores sending / receiving of RPCHost:Only allows host to send / receive RPCNonHost:Allows any non-host to send / receive RPCLastSender:Receiver only! Special value which allows for senders to "respond" back to the last client that sent the specific RPCEveryone:Allows any caller to send / receive RPC
Usage
public enum MyRPCs {
PublicSendMsg,
HostSendMsg
}
// Sends / receives a message
[ModRPC((uint)MyRPCs.PublicSendMessage)]
public void AnyoneSendMessage(string message) {
log.Info($"Message Received: {message}");
}
// Allows only host to send a message, and allows for only non-hosts to receive the message
[ModRPC((uint)MyRPCs.HostSendMsg, senders: RpcActors.Host, receivers: RpcActors.NonHost]
public void HostMessage(string message) {
log.Info($"I am not the host and I received this: \"{message}\" message.);
}
Interfaces¶
Note
You must declare a default, no-parameters constructor in implementing classes. Additionally, when declaring this interface on an abstract class it is required to register that class via the AbstractConstructors class.
- public interface IRpcSendable<T> : IRpcReadable<T>, IRpcWritable¶
When implemented on a type, allows for that type to be transfered and receieved via
ModRPCAttributemethods.
- public T Read(MessageReader reader)¶
- Returns:Newly constructed instance of class.
This method is automatically called when receiving an RPC with T as a declared parameter. The
MessageReaderis automatically passed in and should be used to retrieve the necessary data in order to construct the objectreader:The current message reader to pull data from.
- public void Write(MessageWriter writer)¶
This method is automatically called when sending an RPC that declares the implementing type as a parameter. The
MessageWriteris automatically passed, and should be used to write the information needed byReadto re-construct this objectwriter:The message writer, used to write current data about this instance.
Usage
public class MyObject : IRpcSendable<MyObject> {
public int a;
public MyObject(int a) {
this.a = a;
}
public MyObject Read(MessageReader reader) {
return new MyObject(reader.ReadInt32()); // read integer value from reader and construct new object from it
}
public void Write(MessageWriter writer) {
write.Write(this.a); // write this object's value to the message writer
}
}
Utilities¶
- public class ModRPC¶
The object representation for a method marked with ModRPC. Allows for single-use and targeted invocation of the related ModRPCAttribute method.
- public void Send(int[] clientIds, params object[] args)¶
Sends a Custom RPC to the targeted client(s) with the passed in arguments.
clientIds:An array of clientIds to specifically target with an RPC or null to target all clientsargs:A variable number of arguments which matches the targeted method's signature.
- public void InvokeTrampoline(object[] args)¶
Invokes the original, underlying, method with the given parameters without sending any Custom RPC.
args:An array of objects representing the arguments of the original targeted method.
Usage
[ModRPC(0)]
public void MyMethod(int a) {
// Do something
}
public void ManualSendAndInvoke() {
ModRPC myMethodModRPC; // Acquired usually through Vents.Find()
// Below assumes myMethodModRPC is a valid object and not null.
myMethodModRPC.Send(new int[] { 1 }, 3); // Sends Custom RPC 0 (defined by MyMethod) to client with the ID of 1
myMethodModRPC.InvokeTrampoline(new object[] { 1 }); // Invokes "MyMethod(1)"
}
See also
Refer to Vents.FindRPC() for acquiring a ModRPC instance
Example text with reference on RpcActors <#enum-VentLib.RPC.Attributes.ModRPCAttribute.RpcActors> _.
Vents¶
The main class for VentFramework which contains a couple utility methods, initialization methods, and contains management instances for other modules.
- public static class Vents¶
Variables¶
- public static readonly uint[] BuiltinRPCs¶
An array of internal RPC ids used by VentFramework. These ids cannot be used with ModRPCAttribute and require the special VentRPCAttribute instead.
- public static VersionControl VersionControl¶
Provides an instance of the VersionControl class used for Client2Host handshakes and general versioning
- public static CommandRunner CommandRunner¶
Provides and instance of the CommandRunner class used for intercepting chat commands
Methods¶
Important
All assemblies that utilize VentFramework should call Vents.Initialize() at least once!
- public void Initialize()¶
Initializes VentFramework
- public void Register(Assembly assembly, bool localize = true)¶
This method should be automatically called on all assemblies, but is provided for edge cases where it isn’t.
assembly:The assembly to register with VentLiblocalize:If the assembly should be localized
- public ModRPC FindRPC(uint callerId, MethodInfo targetMethod = null)¶
- Returns:The first matching ModRPC or null if no such ModRPC exists
Finds the first ModRPC associated with a unique RPC id, or the speciifc ModRPC linked with the target method, if specified.
callerId:The first matching ModRPC or null if no such ModRPC existstargetMethod:The unique RPC id to search for
Usage:
ModRPC rpc = Vents.FindRPC((uint)RPCs.ExampleRPC);
- public PlayerControl GetLastSender(uint callerId)¶
- Returns:The last sender of the specified RPC or null if there hasn't been a sender
Gets the last sender of a specified RPC
callerId:The last sender of the specified RPC or null if there hasn't been a sender
Usage:
PlayerControl myPlayer = Vents.GetLastSender((uint)RPCs.ExampleRPC);
- public void BlockClient(Assembly assembly, int clientId)¶
Blocks reception of RPCs from a speciifc client for all ModRPCs in a specified assembly
assembly:The assembly to block RPC reception fromclientId:The specific client to block