예제 #1
0
 /// <summary>
 /// Write a new _dir.vpk file, based on a vpk directory structure.
 /// </summary>
 /// <param name="directoryinfo">A big directory information structure as a List of VPKExts</param>
 private void WriteDirectory(List <VPKExt> directoryinfo)
 {
     using (FileStream output = File.OpenWrite(fileprefix + "_dir.vpk"))
     {
         using (BinaryWriter bw = new BinaryWriter(output))
         {
             VPKHeader headerbin = new VPKHeader(null);
             bw.Write(StructTools.RawSerialize(headerbin));
             foreach (VPKExt ext in directoryinfo)
             {
                 bw.Write(Encoding.ASCII.GetBytes(ext.ext));
                 bw.Write((byte)0); //null terminator
                 foreach (VPKDirectory dir in ext.directories)
                 {
                     bw.Write(Encoding.ASCII.GetBytes(dir.path));
                     bw.Write((byte)0);
                     foreach (VPKFileEntry fe in dir.entries)
                     {
                         bw.Write(Encoding.ASCII.GetBytes(fe.name));
                         bw.Write(StructTools.RawSerialize(fe.body));
                     }
                     bw.Write((byte)0);
                 }
                 bw.Write((byte)0);
             }
             // Go back and fix up the length
             long len = bw.BaseStream.Position;
             bw.Seek(0, SeekOrigin.Begin);
             headerbin.tree_length = (uint)(len - 18);
             bw.Write(StructTools.RawSerialize(headerbin));
         }
     }
 }
예제 #2
0
        public static long filealignment = 1024; // 1^10 - align to kilobyte boundaries

        /// <summary>
        /// Construct a VPK object with a specific filename
        /// </summary>
        /// <param name="filename">The filename (relative) where the vpk is</param>
        public VPK(string filename)
        {
            vpkmutex.WaitOne();
            if (filename.EndsWith(".vpk"))
            {
                filename = filename.Substring(0, filename.Length - 4);
            }
            if (filename.EndsWith("_dir"))
            {
                filename = filename.Substring(0, filename.Length - 4);
            }
            fileprefix = filename;
            if (!File.Exists(fileprefix + "_dir.vpk"))
            {
                //we've gotta make a new vpk from scratch
                using (FileStream headfile = File.Create(fileprefix + "_dir.vpk"))
                {
                    using (BinaryWriter writer = new BinaryWriter(headfile))
                    {
                        VPKHeader head = new VPKHeader(null);
                        writer.Write(StructTools.RawSerialize(head));
                    }
                }
            }
            vpkmutex.ReleaseMutex();
        }
예제 #3
0
 public void Send <T>(T data, MessageType type, QosType qosType = QosType.Unreliable)
 {
     byte[] objData = StructTools.RawSerialize(data);
     byte[] objType = BitConverter.GetBytes((short)type);
     byte[] payload = objType.Concat(objData).ToArray();
     _reliableClient.SendMessage(payload, payload.Length, QosType.Unreliable);
 }
예제 #4
0
        public void Send <T>(RemoteClient client, T data, MessageType type, QosType qosType = QosType.Unreliable)
        {
            var objData = StructTools.RawSerialize(data);
            var objType = BitConverter.GetBytes((short)type);
            var payload = objType.Concat(objData).ToArray();

            _clients.TryGetValue(client, out var reliableEndpoint);
            reliableEndpoint?.SendMessage(payload, payload.Length, qosType);
        }
예제 #5
0
        public static async Task <NetPacket> ReadPacketAsync(NetworkStream stream, int maxSize)
        {
            var buffer    = new byte[maxSize];
            var bytesRead = await stream.ReadAsync(buffer, 0, maxSize);

            Array.Resize(ref buffer, bytesRead);

            var rawPacket = StructTools.RawDeserialize <RawNetPacket>(buffer, 0);

            return(NetPacket.FromRawPacket(rawPacket));
        }
예제 #6
0
        public void SendAll <T>(T data, MessageType type, QosType qosType = QosType.Unreliable)
        {
            var objData = StructTools.RawSerialize(data);
            var objType = BitConverter.GetBytes((short)type);
            var payload = objType.Concat(objData).ToArray();

            foreach (var(rc, re) in _clients)
            {
                re.TransmitCallback = (buffer, size) => { rc.SendPayload(buffer, size); };
                re.SendMessage(payload, payload.Length, qosType);
            }
        }
예제 #7
0
        private void OnReliableReceiveCallback(byte[] payload, int payloadSize)
        {
            //Extract Header Type
            MessageType type = (MessageType)BitConverter.ToInt16(payload, 0);

            if (type == MessageType.Chat)
            {
                var msg = StructTools.RawDeserialize <ChatMessage>(payload, HEADER_OFFSET);
                OnReceiveChatMessage?.Invoke(msg);
            }

            if (type == MessageType.Entity)
            {
                Debug.Log($"Entity: {payloadSize - 2}");
                var entity = StructTools.RawDeserialize <Entity>(payload, 2);
                OnEntityReceived?.Invoke(entity);
            }
        }
예제 #8
0
        private void ReliableClientMessageReceived(byte[] payload, int payloadSize)
        {
            Console.WriteLine($"Received Payload of {payloadSize} bytes.");
            MessageType type = (MessageType)BitConverter.ToInt16(payload, 0);

            if (type == MessageType.Chat)
            {
                //var data = StructTools.RawDeserialize<ChatMessage>(payload, HEADER_OFFSET);
                //OnChatMessage?.Invoke(data);
                var chatMessage = StructTools.RawDeserialize <ChatMessage>(payload, HEADER_OFFSET);
                //OnChatMessageReceived?.Invoke(chatMessage);
                SendAll(chatMessage, MessageType.Chat);
            }
            else
            {
                Console.WriteLine($"Type: {(MessageType) Enum.Parse(typeof(MessageType), type.ToString())}");

                var pos = StructTools.RawDeserialize <Position>(payload, HEADER_OFFSET); // 0 is offset in byte[]
                //Console.WriteLine($"messageReceivedHandler: {client} sent {payloadSize} bytes of data.");
                Console.WriteLine(pos.ToString());
                SendAll(payload, payloadSize);
            }
        }
예제 #9
0
        /// <summary>
        /// Read the directory from the file. This is mostly ported code from my [pw]
        /// vpk.h from vpktool.
        /// </summary>
        /// <returns>The VPKDirectory for the file's contents</returns>
        private List <VPKExt> GetDirectory()
        {
            byte[] body   = null;
            int    length = 0;

            using (FileStream vpkfile = File.OpenRead(fileprefix + "_dir.vpk"))
            {
                using (BinaryReader reader = new BinaryReader(vpkfile))
                {
                    byte[]    headerbytes = reader.ReadBytes(12);
                    VPKHeader head        = StructTools.RawDeserialize <VPKHeader>(headerbytes, 0);
                    if (head.version != 1)
                    {
                        return(null);
                    }
                    body   = reader.ReadBytes((int)head.tree_length);
                    length = (int)head.tree_length;
                }
            }
            if (body == null)
            {
                return(null);
            }
            List <VPKExt> ret     = new List <VPKExt>();
            int           index   = 0;
            int           level   = 0;
            VPKExt        curext  = new VPKExt();
            VPKDirectory  curpath = new VPKDirectory();

            while (index < length && level >= 0)
            {
                if (body[index] == '\0')
                {
                    switch (level)
                    {
                    case 0:
                        break;

                    case 1:
                        ret.Add(curext);
                        break;

                    case 2:
                        curext.directories.Add(curpath);
                        break;
                    }
                    index++;
                    level--;
                    continue;
                }
                switch (level)
                {
                case 0:     //ext level
                    int extstartindex = index;
                    level++;
                    while (body[index] != 0)
                    {
                        index++;
                    }
                    index++;
                    curext             = new VPKExt();
                    curext.ext         = Encoding.ASCII.GetString(body, extstartindex, index - extstartindex);
                    curext.directories = new List <VPKDirectory>();
                    break;

                case 1:
                    int pathstartindex = index;
                    level++;
                    while (body[index] != 0)
                    {
                        index++;
                    }
                    index++;
                    curpath         = new VPKDirectory();
                    curpath.path    = Encoding.ASCII.GetString(body, pathstartindex, index - pathstartindex);
                    curpath.ext     = curext.ext;
                    curpath.entries = new List <VPKFileEntry>();
                    break;

                case 2:
                    int filestartindex = index;
                    while (body[index] != 0)
                    {
                        index++;
                    }
                    index++;
                    VPKFileEntry vpkfile = new VPKFileEntry();
                    vpkfile.name = Encoding.ASCII.GetString(body, filestartindex, index - filestartindex);
                    vpkfile.path = curpath.path;
                    vpkfile.ext  = curext.ext;
                    vpkfile.body = StructTools.RawDeserialize <VPKFile>(body, index);
                    index       += 18;
                    index       += vpkfile.body.preload_bytes; // we ignore preload data
                    curpath.entries.Add(vpkfile);
                    break;
                }
            }
            return(ret);
        }
예제 #10
0
 public static async Task WritePacketAsync(NetworkStream stream, RawNetPacket packet)
 {
     var packetStruct = StructTools.RawSerialize(packet);
     await stream.WriteAsync(packetStruct, 0, packetStruct.Length);
 }