public void SetBlock(int x, int y, int z, ClassicBlock block) { var sec = y >> 4; var blocks = _blocks[sec]; if (blocks == null) { _blocks[sec] = blocks = new byte[4096]; } var index = GetBlockIndexByCoord(x, y, z); blocks[index] = unchecked ((byte)(block.Id & 0xff)); if (block.Id >= 256) { if (_add[sec] == null) { _add[sec] = new byte[2048]; } EndianHelper.SetHalfInt(_add[sec], index, block.Id >> 8); } if (block.Data != 0) { if (_data[sec] == null) { _data[sec] = new byte[2048]; } EndianHelper.SetHalfInt(_data[sec], index, block.Data); } }
/// <summary> /// Get block's Id and data at a given coordinate /// </summary> /// <param name="x">World X</param> /// <param name="y">World Y</param> /// <param name="z">World Z</param> /// <returns>Block</returns> public ClassicBlock GetBlock(int x, int y, int z) { var sec = y >> 4; var data = 0; var blocks = _blocks[sec]; if (blocks == null) { return(ClassicBlock.AirBlock); } var index = GetBlockIndexByCoord(x, y, z); var blockId = (int)blocks[index]; if (_add[sec] != null) { blockId += (EndianHelper.GetHalfInt(_add[sec], index) << 8); } if (_data[sec] != null) { data = EndianHelper.GetHalfInt(_data[sec], index); } return(new ClassicBlock { Id = blockId, Data = data }); }
internal override bool IsAirBlock(int x, int y, int z) { var sec = y >> 4; var blocks = _blocks[sec]; if (blocks == null) { return(true); } var index = GetBlockIndexByCoord(x, y, z); if (blocks[index] != 0) { return(false); } if (_add[sec] != null) { if (EndianHelper.GetHalfInt(_add[sec], index) != 0) { return(false); } } return(true); }
public void HalfIntMalformedDataTest() { var bytes = new byte[16]; for (var i = 0; i < 32; i++) { EndianHelper.SetHalfInt(bytes, i, BaseData[i]); } const int indexUnderflow = 5; const int indexOverflow = 9; const int underflow = -999; const int overflow = 999; EndianHelper.SetHalfInt(bytes, indexUnderflow, underflow); EndianHelper.SetHalfInt(bytes, indexOverflow, overflow); for (var i = 0; i < 32; i++) { if (i == indexUnderflow) { Assert.AreEqual(EndianHelper.GetHalfInt(bytes, i), underflow & 0xf); } else if (i == indexOverflow) { Assert.AreEqual(EndianHelper.GetHalfInt(bytes, i), overflow & 0xf); } else { Assert.AreEqual(EndianHelper.GetHalfInt(bytes, i), BaseData[i]); } } }
public void SwapBytes( ) { this.op = ( ushort )EndianHelper.LittleShort(( short )this.op); this.a = EndianHelper.LittleShort(this.a); this.b = EndianHelper.LittleShort(this.b); this.c = EndianHelper.LittleShort(this.c); }
/// <summary> /// Mod_LoadSpriteGroup /// </summary> private Int32 LoadSpriteGroup(ByteArraySegment pin, out Object ppframe, Int32 framenum, Func <String, ByteArraySegment, Int32, Int32, Int32> onLoadSpriteTexture) { var pingroup = Utilities.BytesToStructure <dspritegroup_t>(pin.Data, pin.StartIndex); var numframes = EndianHelper.LittleLong(pingroup.numframes); var pspritegroup = new mspritegroup_t( ); pspritegroup.numframes = numframes; pspritegroup.frames = new mspriteframe_t[numframes]; ppframe = pspritegroup; var poutintervals = new Single[numframes]; pspritegroup.intervals = poutintervals; var offset = pin.StartIndex + dspritegroup_t.SizeInBytes; for (var i = 0; i < numframes; i++, offset += dspriteinterval_t.SizeInBytes) { var interval = Utilities.BytesToStructure <dspriteinterval_t>(pin.Data, offset); poutintervals[i] = EndianHelper.LittleFloat(interval.interval); if (poutintervals[i] <= 0) { Utilities.Error("Mod_LoadSpriteGroup: interval<=0"); } } for (var i = 0; i < numframes; i++) { Object tmp; offset = LoadSpriteFrame(new ByteArraySegment(pin.Data, offset), out tmp, framenum * 100 + i, onLoadSpriteTexture); pspritegroup.frames[i] = ( mspriteframe_t )tmp; } return(offset); }
/// <summary> /// /// </summary> /// <returns>Offset of next data block</returns> private Int32 LoadSpriteFrame(ByteArraySegment pin, out Object ppframe, Int32 framenum, Func <String, ByteArraySegment, Int32, Int32, Int32> onLoadSpriteTexture) { var pinframe = Utilities.BytesToStructure <dspriteframe_t>(pin.Data, pin.StartIndex); var width = EndianHelper.LittleLong(pinframe.width); var height = EndianHelper.LittleLong(pinframe.height); var size = width * height; var pspriteframe = new mspriteframe_t( ); ppframe = pspriteframe; pspriteframe.width = width; pspriteframe.height = height; var orgx = EndianHelper.LittleLong(pinframe.origin[0]); var orgy = EndianHelper.LittleLong(pinframe.origin[1]); pspriteframe.up = orgy; // origin[1]; pspriteframe.down = orgy - height; pspriteframe.left = orgx; // origin[0]; pspriteframe.right = width + orgx; // origin[0]; var name = Name + "_" + framenum.ToString( ); var index = onLoadSpriteTexture(name, new ByteArraySegment(pin.Data, pin.StartIndex + dspriteframe_t.SizeInBytes), width, height); pspriteframe.gl_texturenum = index; return(pin.StartIndex + dspriteframe_t.SizeInBytes + size); }
private qsocket_t ProcessRequestServerInfo(Socket acceptsock, EndPoint clientaddr) { var tmp = Host.Network.Reader.ReadString(); if (tmp != "QUAKE") { return(null); } Host.Network.Message.Clear(); // save space for the header, filled in later Host.Network.Message.WriteLong(0); Host.Network.Message.WriteByte(CCRep.CCREP_SERVER_INFO); var newaddr = acceptsock.LocalEndPoint; //dfunc.GetSocketAddr(acceptsock, &newaddr); Host.Network.Message.WriteString(newaddr.ToString()); // dfunc.AddrToString(&newaddr)); Host.Network.Message.WriteString(Host.Network.HostName); Host.Network.Message.WriteString(Host.Server.sv.name); Host.Network.Message.WriteByte(Host.Network.ActiveConnections); Host.Network.Message.WriteByte(Host.Server.svs.maxclients); Host.Network.Message.WriteByte(NetworkDef.NET_PROTOCOL_VERSION); Utilities.WriteInt(Host.Network.Message.Data, 0, EndianHelper.BigLong(NetFlags.NETFLAG_CTL | (Host.Network.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); Host.Network.LanDriver.Write(acceptsock, Host.Network.Message.Data, Host.Network.Message.Length, clientaddr); Host.Network.Message.Clear(); return(null); }
/// <summary> /// _Datagram_CheckNewConnections /// </summary> public qsocket_t InternalCheckNewConnections( ) { var acceptsock = Host.Network.LanDriver.CheckNewConnections( ); if (acceptsock == null) { return(null); } EndPoint clientaddr = new IPEndPoint(IPAddress.Any, 0); Host.Network.Message.FillFrom(Host.Network, acceptsock, ref clientaddr); if (Host.Network.Message.Length < sizeof(Int32)) { return(null); } Host.Network.Reader.Reset( ); var control = EndianHelper.BigLong(Host.Network.Reader.ReadLong( )); var isControlInvalid = (control == -1 || ((control & (~NetFlags.NETFLAG_LENGTH_MASK)) != NetFlags.NETFLAG_CTL) || (control & NetFlags.NETFLAG_LENGTH_MASK) != Host.Network.Message.Length); if (isControlInvalid) { return(null); } var command = Host.Network.Reader.ReadByte( ); switch (command) { case CCReq.CCREQ_SERVER_INFO: return(ProcessRequestServerInfo(acceptsock, clientaddr)); case CCReq.CCREQ_PLAYER_INFO: return(ProcessRequestPlayerInfo(acceptsock, clientaddr)); case CCReq.CCREQ_RULE_INFO: return(ProcessRequestRuleInfo(acceptsock, clientaddr)); case CCReq.CCREQ_CONNECT: if (Host.Network.Reader.ReadString() != "QUAKE") { return(null); } if (Host.Network.Reader.ReadByte() != NetworkDef.NET_PROTOCOL_VERSION) { return(ProcessInvalidProtocol(acceptsock, clientaddr)); } #if BAN_TEST // check for a ban if (clientaddr.sa_family == AF_INET) { unsigned long testAddr; testAddr = ((struct sockaddr_in *)&clientaddr)->sin_addr.s_addr;
/// <summary> /// 写ushort /// </summary> /// <param name="value"></param> public void Write(ushort value) { if (!LittleEndian) { value = EndianHelper.SwapUInt16(value); } writer.Write(value); }
/// <summary> /// 写uint /// </summary> /// <param name="value"></param> public void Write(uint value) { if (!LittleEndian) { value = EndianHelper.SwapUInt32(value); } writer.Write(value); }
/// <summary> /// 写ulong /// </summary> /// <param name="value"></param> public void Write(ulong value) { if (!LittleEndian) { value = EndianHelper.SwapUInt64(value); } writer.Write(value); }
private void UpdateScreen() { Host.Screen.vid.maxwarpwidth = WARP_WIDTH; Host.Screen.vid.maxwarpheight = WARP_HEIGHT; Host.Screen.vid.colormap = Host.ColorMap; var v = BitConverter.ToInt32(Host.ColorMap, 2048); Host.Screen.vid.fullbright = 256 - EndianHelper.LittleLong(v); }
public void SwapBytes( ) { this.first_statement = EndianHelper.LittleLong(this.first_statement); this.parm_start = EndianHelper.LittleLong(this.parm_start); this.locals = EndianHelper.LittleLong(this.locals); this.s_name = EndianHelper.LittleLong(this.s_name); this.s_file = EndianHelper.LittleLong(this.s_file); this.numparms = EndianHelper.LittleLong(this.numparms); }
private qsocket_t ProcessRequestRuleInfo(Socket acceptsock, EndPoint clientaddr) { // find the search start location var prevCvarName = Host.Network.Reader.ReadString(); ClientVariable var; if (!String.IsNullOrEmpty(prevCvarName)) { var = Host.CVars.Get(prevCvarName); if (var == null) { return(null); } var index = Host.CVars.IndexOf(var.Name); var = Host.CVars.GetByIndex(index + 1); } else { var = Host.CVars.GetByIndex(0); } // search for the next server cvar while (var != null) { if (var.IsServer) { break; } var index = Host.CVars.IndexOf(var.Name); var = Host.CVars.GetByIndex(index + 1); } // send the response Host.Network.Message.Clear(); // save space for the header, filled in later Host.Network.Message.WriteLong(0); Host.Network.Message.WriteByte(CCRep.CCREP_RULE_INFO); if (var != null) { Host.Network.Message.WriteString(var.Name); Host.Network.Message.WriteString(var.Get().ToString()); } Utilities.WriteInt(Host.Network.Message.Data, 0, EndianHelper.BigLong(NetFlags.NETFLAG_CTL | (Host.Network.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); Host.Network.LanDriver.Write(acceptsock, Host.Network.Message.Data, Host.Network.Message.Length, clientaddr); Host.Network.Message.Clear(); return(null); }
/// <summary> /// 读int /// </summary> /// <returns></returns> public int ReadInt() { int value = reader.ReadInt32(); if (!LittleEndian) { value = EndianHelper.SwapInt32(value); } return(value); }
/// <summary> /// 读ushort /// </summary> /// <returns></returns> public ushort ReadUShort() { ushort value = reader.ReadUInt16(); if (!LittleEndian) { value = EndianHelper.SwapUInt16(value); } return(value); }
/// <summary> /// 读short /// </summary> /// <returns></returns> public short ReadShort() { short value = reader.ReadInt16(); if (!LittleEndian) { value = EndianHelper.SwapInt16(value); } return(value); }
/// <summary> /// 读ulong /// </summary> /// <returns></returns> public ulong ReadULong() { ulong value = reader.ReadUInt64(); if (!LittleEndian) { value = EndianHelper.SwapUInt64(value); } return(value); }
/// <summary> /// 读long /// </summary> /// <returns></returns> public long ReadLong() { long value = reader.ReadInt64(); if (!LittleEndian) { value = EndianHelper.SwapInt64(value); } return(value); }
public void BytesLongArrayConversionTest() { var longs = new[] { 2L, -1L, 2181234892347L, 10000L }; var bytes = EndianHelper.LongArrayToBytes(longs); Assert.AreEqual(EndianHelper.BytesToLongArray(bytes), longs); bytes = new byte[] { 47, 55, 60, 12, 0, 254, 80, 3, 7, 12, 0, 254, 80, 3, 254, 80, 3, 7, 12, 0, 254, 80, 3, 254 }; longs = EndianHelper.BytesToLongArray(bytes); Assert.AreEqual(EndianHelper.LongArrayToBytes(longs), bytes); }
/// <summary> /// CL_WriteDemoMessage /// Dumps the current net message, prefixed by the length and view angles /// </summary> private void WriteDemoMessage( ) { var len = EndianHelper.LittleLong(Host.Network.Message.Length); var writer = ((DisposableWrapper <BinaryWriter>)cls.demofile).Object; writer.Write(len); writer.Write(EndianHelper.LittleFloat(cl.viewangles.X)); writer.Write(EndianHelper.LittleFloat(cl.viewangles.Y)); writer.Write(EndianHelper.LittleFloat(cl.viewangles.Z)); writer.Write(Host.Network.Message.Data, 0, Host.Network.Message.Length); writer.Flush( ); }
/// <summary> /// Get all blocks' world coordinates, block Id and block data. /// The blocks may not be ordered to maximize performance. /// </summary> /// <returns>Blocks</returns> public IEnumerable <(int X, int Y, int Z, ClassicBlock Block)> AllBlocks() { for (var sy = 0; sy < 16; sy++) { var data = _data[sy]; var add = _add[sy]; var blocks = _blocks[sy]; if (blocks == null) { continue; } var dataAvailable = data != null; var addAvailable = add != null; var baseY = sy << 4; var index = 0; int blockId, blockData; for (var y = 0; y < 16; y++) { for (var z = 0; z < 16; z++) { for (var x = 0; x < 16; x += 2) { blockId = addAvailable ? ((EndianHelper.GetHalfIntEvenIndex(add, index) << 8) | blocks[index]) : blocks[index]; blockData = dataAvailable ? EndianHelper.GetHalfIntEvenIndex(data, index) : 0; yield return(x, y + baseY, z, new ClassicBlock { Data = blockData, Id = blockId }); index += 2; } index -= 15; for (var x = 1; x < 16; x += 2) { blockId = addAvailable ? ((EndianHelper.GetHalfIntOddIndex(add, index) << 8) | blocks[index]) : blocks[index]; blockData = dataAvailable ? EndianHelper.GetHalfIntOddIndex(data, index) : 0; yield return(x, y + baseY, z, new ClassicBlock { Data = blockData, Id = blockId }); index += 2; } index -= 1; } } } }
private qsocket_t ProcessInvalidProtocol(Socket acceptsock, EndPoint clientaddr) { Host.Network.Message.Clear(); // save space for the header, filled in later Host.Network.Message.WriteLong(0); Host.Network.Message.WriteByte(CCRep.CCREP_REJECT); Host.Network.Message.WriteString("Incompatible version.\n"); Utilities.WriteInt(Host.Network.Message.Data, 0, EndianHelper.BigLong(NetFlags.NETFLAG_CTL | (Host.Network.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); Host.Network.LanDriver.Write(acceptsock, Host.Network.Message.Data, Host.Network.Message.Length, clientaddr); Host.Network.Message.Clear(); return(null); }
private void SendHostDetails(Socket acceptsock, Socket newsock, EndPoint clientaddr) { Host.Network.Message.Clear(); // save space for the header, filled in later Host.Network.Message.WriteLong(0); Host.Network.Message.WriteByte(CCRep.CCREP_ACCEPT); var newaddr2 = newsock.LocalEndPoint;// dfunc.GetSocketAddr(newsock, &newaddr); Host.Network.Message.WriteLong(Host.Network.LanDriver.GetSocketPort(newaddr2)); Utilities.WriteInt(Host.Network.Message.Data, 0, EndianHelper.BigLong(NetFlags.NETFLAG_CTL | (Host.Network.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); Host.Network.LanDriver.Write(acceptsock, Host.Network.Message.Data, Host.Network.Message.Length, clientaddr); Host.Network.Message.Clear(); }
public void HalfIntTest() { var bytes = new byte[16]; for (var i = 0; i < 32; i++) { EndianHelper.SetHalfInt(bytes, i, BaseData[i]); } for (var i = 0; i < 32; i++) { Assert.AreEqual(EndianHelper.GetHalfInt(bytes, i), BaseData[i]); } }
private Boolean CheckForExistingConnection(Socket acceptsock, EndPoint clientaddr) { var exit = false; // see if this guy is already connected foreach (var s in Host.Network.ActiveSockets) { if (s.driver != Host.Network.DriverLevel) { continue; } var ret = Host.Network.LanDriver.AddrCompare(clientaddr, s.addr); if (ret >= 0) { // is this a duplicate connection reqeust? if (ret == 0 && Host.Network.Time - s.connecttime < 2.0) { // yes, so send a duplicate reply Host.Network.Message.Clear(); // save space for the header, filled in later Host.Network.Message.WriteLong(0); Host.Network.Message.WriteByte(CCRep.CCREP_ACCEPT); var newaddr = s.socket.LocalEndPoint; //dfunc.GetSocketAddr(s.socket, &newaddr); Host.Network.Message.WriteLong(Host.Network.LanDriver.GetSocketPort(newaddr)); Utilities.WriteInt(Host.Network.Message.Data, 0, EndianHelper.BigLong(NetFlags.NETFLAG_CTL | (Host.Network.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); Host.Network.LanDriver.Write(acceptsock, Host.Network.Message.Data, Host.Network.Message.Length, clientaddr); Host.Network.Message.Clear(); exit = true; } // it's somebody coming back in from a crash/disconnect // so close the old qsocket and let their retry get them back in if (!exit) { Host.Network.Close(s); exit = true; } } if (exit) { break; } } return(exit); }
public void LoadWadFile(string filename, byte[] buffer) { this._Data = buffer; if (this._Data == null) { Utilities.Error("Wad.LoadWadFile: couldn't load {0}", filename); } if (this._Handle.IsAllocated) { this._Handle.Free( ); } this._Handle = GCHandle.Alloc(this._Data, GCHandleType.Pinned); this._DataPtr = this._Handle.AddrOfPinnedObject( ); var header = Utilities.BytesToStructure <WadInfo>(this._Data, 0); this.Version = Encoding.ASCII.GetString(header.identification); if (this.Version != "WAD2" && this.Version != "WAD3") { Utilities.Error($"Wad file {filename} doesn't have WAD2 or WAD3 id, got {this.Version}\n", filename); } var numlumps = EndianHelper.LittleLong(header.numlumps); var infotableofs = EndianHelper.LittleLong(header.infotableofs); var lumpInfoSize = Marshal.SizeOf(typeof(WadLumpInfo)); this._Lumps = new(numlumps); for (var i = 0; i < numlumps; i++) { var ptr = new IntPtr(this._DataPtr.ToInt64( ) + infotableofs + i * lumpInfoSize); var lump = ( WadLumpInfo )Marshal.PtrToStructure(ptr, typeof(WadLumpInfo)); lump.filepos = EndianHelper.LittleLong(lump.filepos); lump.size = EndianHelper.LittleLong(lump.size); if (lump.type == Wad.TYP_QPIC) { ptr = new(this._DataPtr.ToInt64( ) + lump.filepos); var pic = ( WadPicHeader )Marshal.PtrToStructure(ptr, typeof(WadPicHeader)); EndianHelper.SwapPic(pic); Marshal.StructureToPtr(pic, ptr, true); } this._Lumps.Add(Encoding.ASCII.GetString(lump.name).TrimEnd('\0').ToLower( ), lump); } }
// MSG_ReadFloat (void) public float ReadFloat( ) { if (!this.HasRoom(4)) { return(0); } this._Val.b0 = this._Source.Data[this._Count + 0]; this._Val.b1 = this._Source.Data[this._Count + 1]; this._Val.b2 = this._Source.Data[this._Count + 2]; this._Val.b3 = this._Source.Data[this._Count + 3]; this._Count += 4; this._Val.i0 = EndianHelper.LittleLong(this._Val.i0); return(this._Val.f0); }
public void DynBitArrayEquivalentTest() { var bytes = new byte[16]; for (var i = 0; i < 32; i++) { EndianHelper.SetHalfInt(bytes, i, BaseData[i]); } var dynarray = DynBitArray.CreateFromByteArray(bytes, 4); Assert.AreEqual(32, dynarray.Length); for (var i = 0; i < 32; i++) { Assert.AreEqual(dynarray[i], EndianHelper.GetHalfInt(bytes, i)); } }