Exemplo n.º 1
0
        public void Start()
        {
            Console.WriteLine("Client running under steam user: {0}", SteamFriends.GetPersonaName());


            // Go into chatting mode if selected.
            if (m_isChatClientOnly)
            {
                DoNonAuthenticatedChat();
                return;
            }

            // Join the game.
            var joinPacket = new CWTNet.Packets.CS.ClientJoinPacket();

            PacketUtil.SendP2PPacket(m_hostSteamID, joinPacket);


            Stopwatch posUpdateWatch = new Stopwatch();

            posUpdateWatch.Start();

            while (true)
            {
                SteamAPI.RunCallbacks();

                // Send our client updates to the server.
                if (posUpdateWatch.ElapsedMilliseconds >= 1000)
                {
                    posUpdateWatch.Restart();

                    Console.WriteLine("Sending creature update...");
                    var creatureUpdatePacket = new CWTNet.Packets.CS.ClientCreatureUpdate();
                    creatureUpdatePacket.data.GUID     = 2;
                    creatureUpdatePacket.data.Position = Position;
                    PacketUtil.SendP2PPacket(m_hostSteamID, creatureUpdatePacket);

                    // Zone discovery
                    Console.WriteLine("Sending Zone discovery update...");
                    var loadZone = new CWTNet.Packets.CS.DiscoverZone((int)((Position.X / 65536) / 64), (int)((Position.Y / 65536) / 64));
                    PacketUtil.SendP2PPacket(m_hostSteamID, loadZone);
                }

                // Read the servers updates for the client.
                if (SteamNetworking.IsP2PPacketAvailable(out uint packetSize, (int)CWTNet.Channel.SC))
                {
                    byte[] data = new byte[packetSize];
                    bool   ok   = SteamNetworking.ReadP2PPacket(data, packetSize, out uint newPacketSize, out CSteamID remoteSteamID, (int)CWTNet.Channel.SC);
                    using (var readMemoryStream = new MemoryStream(data))
                    {
                        var          rdr    = new BinaryReader(readMemoryStream);
                        SCPacketType opcode = (SCPacketType)rdr.ReadUInt32();



                        switch (opcode)
                        {
                        case SCPacketType.SC_CREATURE_UPDATE:
                            Console.WriteLine("Got packet:{0}", opcode);
                            var serverCreatureUpdatePacket = new CWTNet.Packets.SC.ServerCreatureUpdate();
                            serverCreatureUpdatePacket.Deserialize(ref rdr);
                            var update = serverCreatureUpdatePacket.data;

                            /*
                             * if (update.ClientSteamID.HasValue)
                             * {
                             *  Console.WriteLine("\tClientSteamID: {0}", update.ClientSteamID);
                             * }
                             */
                            Console.WriteLine("\tGUID: {0}", update.GUID);
                            Console.WriteLine(update.ChangesToString(indent: 1));


                            break;

                        case SCPacketType.SC_SET_PLAYER_GUID:
                            Console.WriteLine("Got packet:{0}", opcode);
                            var setPlayerGUIDPacket = new CWTNet.Packets.SC.SetPlayerGUID(0);
                            setPlayerGUIDPacket.Deserialize(ref rdr);

                            Console.WriteLine("\tGUID: {0}", setPlayerGUIDPacket.m_GUID);

                            GUID = setPlayerGUIDPacket.m_GUID;
                            break;

                        case SCPacketType.SC_SERVER_UPDATE:
                            Console.WriteLine("Got packet:{0}", opcode);
                            var serverUpdateData = rdr.ReadBytes((int)packetSize - 4);
                            Console.WriteLine("\t{0}", BitConverter.ToString(serverUpdateData));
                            break;

                        case SCPacketType.SC_INVITATION:
                            Console.WriteLine("Got packet:{0}", opcode);
                            var invitationPacket = new CWTNet.Packets.SC.Invitation(0);
                            invitationPacket.Deserialize(ref rdr);

                            Console.WriteLine("\tVersion: {0}", invitationPacket.m_gameVersion);

                            // If force-joining gets patched, then we can just accept invitations like normal:
                            //var joinPacket = new CWTNet.Packets.CS.ClientJoinPacket();
                            //PacketUtil.SendP2PPacket(remoteSteamID, joinPacket);

                            break;

                        case SCPacketType.SC_SET_TIME:
                            /*
                             * Console.WriteLine("Got packet:{0}", opcode);
                             * var setTimePacket = new CWTNet.Packets.SC.SetTime(0, 0);
                             * setTimePacket.Deserialize(ref rdr);
                             * Console.WriteLine("\tDay: {0}", setTimePacket.m_day);
                             * Console.WriteLine("\tTime: {0}", setTimePacket.m_time);
                             */
                            break;

                        default:
                            Console.WriteLine("Got unparsed packet from server: {0}, Size:0x{1:X}", opcode, packetSize);
                            break;
                        }
                    }
                }

                Thread.Sleep(100);
            }
        }
Exemplo n.º 2
0
        public void Start()
        {
            Console.WriteLine("Server running under steam user: {0}", SteamFriends.GetPersonaName());
            Console.WriteLine("CWD: {0}", Directory.GetCurrentDirectory());
            Console.WriteLine("AppID: {0}", SteamUtils.GetAppID());

            Stopwatch inviteStopWatch = new Stopwatch();

            inviteStopWatch.Start();

            while (true)
            {
                SteamAPI.RunCallbacks();

                // Send the friends an invite every 5 seconds.
                var friendsCount = SteamFriends.GetFriendCount(EFriendFlags.k_EFriendFlagImmediate);
                if (friendsCount > 0 && inviteStopWatch.ElapsedMilliseconds > 5000)
                {
                    // Restart the stopwatch.
                    inviteStopWatch.Restart();

                    for (int i = 0; i < friendsCount; i++)
                    {
                        CSteamID friend = SteamFriends.GetFriendByIndex(i, EFriendFlags.k_EFriendFlagImmediate);
                        Console.WriteLine("[Server] Inviting friend[{0}], steamID: {1}", i, friend.m_SteamID);


                        var invitationPacket = new CWTNet.Packets.SC.Invitation(SERVER_GAME_VERSION);
                        PacketUtil.SendP2PPacket(friend, invitationPacket, CWTNet.Channel.SC);
                    }
                }

                // Read packets
                if (SteamNetworking.IsP2PPacketAvailable(out uint packetSize, (int)CWTNet.Channel.CS))
                {
                    byte[] data = new byte[packetSize];
                    bool   ok   = SteamNetworking.ReadP2PPacket(data, packetSize, out uint newPacketSize, out CSteamID remoteSteamID, 1);
                    using (var readMemoryStream = new MemoryStream(data))
                    {
                        var          rdr    = new BinaryReader(readMemoryStream);
                        CSPacketType opcode = (CSPacketType)rdr.ReadUInt32();
                        switch (opcode)
                        {
                        case CSPacketType.CS_CLIENT_JOIN:
                            var clientJoinPacket = new CWTNet.Packets.CS.ClientJoinPacket();
                            clientJoinPacket.Deserialize(ref rdr);

                            Console.WriteLine(opcode);
                            Console.WriteLine("\tSteamID: {0}", remoteSteamID);

                            Thread.Sleep(1 * 1000);

                            // Send them a welcome message :).
                            Console.WriteLine("Sending chat packet");
                            var serverChatMessage = new CWTNet.Packets.SC.ServerChatMessage(0, "Welcome to the CWT dev server!");
                            PacketUtil.SendP2PPacket(remoteSteamID, serverChatMessage);


                            Thread.Sleep(1 * 1000);

                            // Send them a GUID.
                            var setPlayerGUIDPacket = new CWTNet.Packets.SC.SetPlayerGUID(2);
                            PacketUtil.SendP2PPacket(remoteSteamID, setPlayerGUIDPacket);

                            Thread.Sleep(1 * 1000);

                            // Send them the UNK6 packet. Don't know what this does tbh.
                            var unk6Packet = new CWTNet.Packets.SC.Unk6();
                            PacketUtil.SendP2PPacket(remoteSteamID, unk6Packet);

                            // Set the time.
                            var setTimePacket = new CWTNet.Packets.SC.SetTime(0, 0);
                            PacketUtil.SendP2PPacket(remoteSteamID, setTimePacket);



                            break;

                        case CSPacketType.CS_DISCOVER_ZONE:
                            var discoverZonePacket = new CWTNet.Packets.CS.DiscoverZone(0, 0);
                            discoverZonePacket.Deserialize(ref rdr);

                            /*
                             * Console.WriteLine(opcode);
                             * Console.WriteLine("\tZoneX:{0}, ZoneY:{1}", discoverZonePacket.m_zoneX, discoverZonePacket.m_zoneY);
                             */
                            break;

                        case CSPacketType.CS_CREATURE_UPDATE:
                            var creatureUpdatePacket = new CWTNet.Packets.CS.ClientCreatureUpdate();
                            creatureUpdatePacket.Deserialize(ref rdr);
                            var update = creatureUpdatePacket.data;

                            Console.WriteLine("\tGUID: {0}", update.GUID);
                            Console.WriteLine(update.ChangesToString(indent: 1));
                            break;


                        default:
                            Console.WriteLine("Got unparsed packet: {0}, Size:{1}", opcode, packetSize);
                            break;
                        }
                    }
                }

                Thread.Sleep(10);
            }
        }