Confirmedusers
711
edits
No edit summary |
m (Updated navbox to new code navbox.) |
||
(9 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
{{GameVersion|1.19.3}} | |||
{{ | <languages/><translate> | ||
<!--T:1--> | |||
Before starting, you should have a development environment set up. If you don't have one already you should read the tutorial [[Setting up your Development Environment]]. Furthermore, we assume that you have a basic understanding of the C# language and Object Oriented Programming. Let's get started! | Before starting, you should have a development environment set up. If you don't have one already you should read the tutorial [[Setting up your Development Environment]]. Furthermore, we assume that you have a basic understanding of the C# language and Object Oriented Programming. Let's get started! | ||
<!--T:2--> | |||
'''Hint''': If you need to send data for Entities or BlockEntities, it may be more appropriate to use the inbuilt network methods for these. Check out the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerNetworkAPI.html IServerNetworkAPI] and [http://apidocs.vintagestory.at/api/Vintagestory.API.Client.IClientNetworkAPI.html IClientNetworkAPI] interfaces for more information. | '''Hint''': If you need to send data for Entities or BlockEntities, it may be more appropriate to use the inbuilt network methods for these. Check out the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerNetworkAPI.html IServerNetworkAPI] and [http://apidocs.vintagestory.at/api/Vintagestory.API.Client.IClientNetworkAPI.html IClientNetworkAPI] interfaces for more information. | ||
== Introduction == | == Introduction == <!--T:3--> | ||
<!--T:4--> | |||
In this example mod we will show you how to send custom data back and forth between the Server and the Client. To do this, we will set up a network channel through which string messages will be sent in a network channel. | In this example mod we will show you how to send custom data back and forth between the Server and the Client. To do this, we will set up a network channel through which string messages will be sent in a network channel. | ||
<!--T:5--> | |||
We will create a server command that will send out a message to all clients, and if a client receives this, it will send a response back to the server; upon receiving this response, the server will display it along with the responding client's player name. | We will create a server command that will send out a message to all clients, and if a client receives this, it will send a response back to the server; upon receiving this response, the server will display it along with the responding client's player name. | ||
== Preparation == | If something goes wrong, Wireshark with the [https://github.com/bluelightning32/vs-protocol vs-protocol] dissector is a helpful debug tool. | ||
== Preparation == <!--T:6--> | |||
<!--T:7--> | |||
Let's start by creating a new .cs file for this mod, and adding our imports and the namespace in which we'll wrap all of our classes: | Let's start by creating a new .cs file for this mod, and adding our imports and the namespace in which we'll wrap all of our classes: | ||
</translate> | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
using System; | using System; | ||
Line 34: | Line 40: | ||
== ProtoContracts == | == ProtoContracts == | ||
VintageStory uses the [https://github.com/protobuf-net/protobuf-net Protobuf-net] library | VintageStory uses multiple [[Modding:Serialization_Formats|serialization formats]]. This tutorial uses the protobuf format. The [https://github.com/protobuf-net/protobuf-net Protobuf-net] library can serialize classes declared with the <code>ProtoContract</code> attribute. | ||
Inside our namespace block, let's create our <code>ProtoContract</code> classes: | Inside our namespace block, let's create our <code>ProtoContract</code> classes: | ||
Line 112: | Line 118: | ||
; | ; | ||
api. | api.ChatCommands.Create("nwtest") | ||
.WithDescription("Send a test network message") | |||
.RequiresPrivilege(Privilege.controlserver) | |||
.HandleWith(new OnCommandDelegate(OnNwTestCmd)); | |||
} | } | ||
#endregion | #endregion | ||
Line 132: | Line 141: | ||
public override void StartServerSide(ICoreServerAPI api)... | public override void StartServerSide(ICoreServerAPI api)... | ||
private | private TextCommandResult OnNwTestCmd(TextCommandCallingArgs args) | ||
{ | { | ||
serverChannel.BroadcastPacket(new NetworkApiTestMessage() | serverChannel.BroadcastPacket(new NetworkApiTestMessage() | ||
Line 138: | Line 147: | ||
message = "Hello World!", | message = "Hello World!", | ||
}); | }); | ||
return TextCommandResult.Success(); | |||
} | } | ||
#endregion | #endregion | ||
Line 256: | Line 266: | ||
public class NetworkApiTest : ModSystem | public class NetworkApiTest : ModSystem | ||
{ | { | ||
#region Server | #region Server | ||
IServerNetworkChannel serverChannel; | IServerNetworkChannel serverChannel; | ||
Line 299: | Line 281: | ||
; | ; | ||
api. | api.ChatCommands.Create("nwtest") | ||
.WithDescription("Send a test network message") | |||
.RequiresPrivilege(Privilege.controlserver) | |||
.HandleWith(new OnCommandDelegate(OnNwTestCmd)); | |||
} | } | ||
private | private TextCommandResult OnNwTestCmd(TextCommandCallingArgs args) | ||
{ | { | ||
serverChannel.BroadcastPacket(new NetworkApiTestMessage() | serverChannel.BroadcastPacket(new NetworkApiTestMessage() | ||
Line 308: | Line 293: | ||
message = "Hello World!", | message = "Hello World!", | ||
}); | }); | ||
return TextCommandResult.Success(); | |||
} | } | ||
Line 319: | Line 305: | ||
} | } | ||
#endregion | |||
#region Client | |||
IClientNetworkChannel clientChannel; | |||
ICoreClientAPI clientApi; | |||
public override void StartClientSide(ICoreClientAPI api) | |||
{ | |||
clientApi = api; | |||
clientChannel = | |||
api.Network.RegisterChannel("networkapitest") | |||
.RegisterMessageType(typeof(NetworkApiTestMessage)) | |||
.RegisterMessageType(typeof(NetworkApiTestResponse)) | |||
.SetMessageHandler<NetworkApiTestMessage>(OnServerMessage) | |||
; | |||
} | |||
private void OnServerMessage(NetworkApiTestMessage networkMessage) | |||
{ | |||
clientApi.ShowChatMessage("Received following message from server: " + networkMessage.message); | |||
clientApi.ShowChatMessage("Sending response."); | |||
clientChannel.SendPacket(new NetworkApiTestResponse() | |||
{ | |||
response = "RE: Hello World!" | |||
}); | |||
} | |||
#endregion | #endregion | ||
} | } | ||
Line 329: | Line 341: | ||
Let's run the mod now! Once you're ingame, try entering <code>/nwtest</code>. You should be met with an initial "Hello World!" message as well as a confirmation message that the server received the response from the Client! | Let's run the mod now! Once you're ingame, try entering <code>/nwtest</code>. You should be met with an initial "Hello World!" message as well as a confirmation message that the server received the response from the Client! | ||
{{Navbox/codemodding}} | |||
{{Navbox/ |