Modding:Network API: Difference between revisions

From Vintage Story Wiki
m
Updated navbox to new code navbox.
(Marked this version for translation)
m (Updated navbox to new code navbox.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
{{GameVersion|1.15}}
{{GameVersion|1.19.3}}
<languages/><translate>
<languages/><translate>
<!--T:1-->
<!--T:1-->
Line 15: Line 15:
<!--T:5-->
<!--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.
If something goes wrong, Wireshark with the [https://github.com/bluelightning32/vs-protocol vs-protocol] dissector is a helpful debug tool.


== Preparation == <!--T:6-->
== Preparation == <!--T:6-->
Line 38: Line 40:
== ProtoContracts ==
== ProtoContracts ==


VintageStory uses the [https://github.com/protobuf-net/protobuf-net Protobuf-net] library to serialize classes declared with the <code>ProtoContract</code> attribute.  
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 116: Line 118:
             ;
             ;


             api.RegisterCommand("nwtest", "Send a test network message", "", OnNwTestCmd, Privilege.controlserver);
             api.ChatCommands.Create("nwtest")
                .WithDescription("Send a test network message")
                .RequiresPrivilege(Privilege.controlserver)
                .HandleWith(new OnCommandDelegate(OnNwTestCmd));
         }
         }
#endregion
#endregion
Line 136: Line 141:
         public override void StartServerSide(ICoreServerAPI api)...
         public override void StartServerSide(ICoreServerAPI api)...


         private void OnNwTestCmd(IServerPlayer player, int groupId, CmdArgs args)
         private TextCommandResult OnNwTestCmd(TextCommandCallingArgs args)
         {
         {
             serverChannel.BroadcastPacket(new NetworkApiTestMessage()
             serverChannel.BroadcastPacket(new NetworkApiTestMessage()
Line 142: Line 147:
                 message = "Hello World!",
                 message = "Hello World!",
             });
             });
            return TextCommandResult.Success();
         }
         }
#endregion
#endregion
Line 260: Line 266:
     public class NetworkApiTest : ModSystem
     public class NetworkApiTest : ModSystem
     {
     {
        #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
         #region Server
         #region Server
         IServerNetworkChannel serverChannel;
         IServerNetworkChannel serverChannel;
Line 303: Line 281:
             ;
             ;


             api.RegisterCommand("nwtest", "Send a test network message", "", OnNwTestCmd, Privilege.controlserver);
             api.ChatCommands.Create("nwtest")
                .WithDescription("Send a test network message")
                .RequiresPrivilege(Privilege.controlserver)
                .HandleWith(new OnCommandDelegate(OnNwTestCmd));
         }
         }


         private void OnNwTestCmd(IServerPlayer player, int groupId, CmdArgs args)
         private TextCommandResult OnNwTestCmd(TextCommandCallingArgs args)
         {
         {
             serverChannel.BroadcastPacket(new NetworkApiTestMessage()
             serverChannel.BroadcastPacket(new NetworkApiTestMessage()
Line 312: Line 293:
                 message = "Hello World!",
                 message = "Hello World!",
             });
             });
            return TextCommandResult.Success();
         }
         }


Line 323: 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 333: 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}}
 
== Distribution ==
 
To distribute this mod, you may run the following command in the modtools cli <code>pack <your mod id></code>, then copy the .zip file into your VintageStory mods folder.
 
Here are the official versions:
* for VS v1.10-rc.4: [https://wiki.vintagestory.at/images/7/73/NetworkApiTest_vs1.10-rc.4_v1.0.0.zip NetworkApiTest_vs1.10-rc.4_v1.0.0.zip]
* for VS v1.6: [https://wiki.vintagestory.at/images/d/d1/NetworkApiTest_vs1.6_v1.0.0.zip NetworkApiTest_vs1.6_v1.0.0.zip]
 
{{Navbox/modding|Vintage Story}}
Confirmedusers
637

edits