(a00338777) v0.9.2.1
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Barotrauma.Items.Components;
|
||||
using Lidgren.Network;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
@@ -7,9 +6,9 @@ using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
enum ChatMessageType
|
||||
public enum ChatMessageType
|
||||
{
|
||||
Default, Error, Dead, Server, Radio, Private, Console, MessageBox, Order, ServerLog
|
||||
Default, Error, Dead, Server, Radio, Private, Console, MessageBox, Order, ServerLog, ServerMessageBox
|
||||
}
|
||||
|
||||
partial class ChatMessage
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using Lidgren.Network;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -12,7 +12,8 @@ namespace Barotrauma.Networking
|
||||
|
||||
public string Name;
|
||||
public byte ID;
|
||||
|
||||
public UInt64 SteamID;
|
||||
|
||||
public Character.TeamType TeamID;
|
||||
|
||||
private Character character;
|
||||
@@ -38,6 +39,12 @@ namespace Barotrauma.Networking
|
||||
HasSpawned = true;
|
||||
#if CLIENT
|
||||
GameMain.GameSession?.CrewManager?.SetPlayerVoiceIconState(this, muted, mutedLocally);
|
||||
|
||||
if (character == GameMain.Client.Character && GameMain.Client.SpawnAsTraitor)
|
||||
{
|
||||
character.IsTraitor = true;
|
||||
character.TraitorCurrentObjective = GameMain.Client.TraitorFirstObjective;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -179,7 +186,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public void WritePermissions(NetBuffer msg)
|
||||
public void WritePermissions(IWriteMessage msg)
|
||||
{
|
||||
msg.Write(ID);
|
||||
msg.Write((UInt16)Permissions);
|
||||
@@ -192,7 +199,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ReadPermissions(NetBuffer inc, out ClientPermissions permissions, out List<DebugConsole.Command> permittedCommands)
|
||||
public static void ReadPermissions(IReadMessage inc, out ClientPermissions permissions, out List<DebugConsole.Command> permittedCommands)
|
||||
{
|
||||
UInt16 permissionsInt = inc.ReadUInt16();
|
||||
|
||||
@@ -221,7 +228,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadPermissions(NetIncomingMessage inc)
|
||||
public void ReadPermissions(IReadMessage inc)
|
||||
{
|
||||
ClientPermissions permissions = ClientPermissions.None;
|
||||
List<DebugConsole.Command> permittedCommands = new List<DebugConsole.Command>();
|
||||
|
||||
@@ -7,7 +7,7 @@ using System.Xml.Linq;
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
[Flags]
|
||||
enum ClientPermissions
|
||||
public enum ClientPermissions
|
||||
{
|
||||
None = 0x0,
|
||||
ManageRound = 0x1,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
interface INetSerializable { }
|
||||
|
||||
@@ -10,10 +8,10 @@ namespace Barotrauma.Networking
|
||||
interface IClientSerializable : INetSerializable
|
||||
{
|
||||
#if CLIENT
|
||||
void ClientWrite(NetBuffer msg, object[] extraData = null);
|
||||
void ClientWrite(IWriteMessage msg, object[] extraData = null);
|
||||
#endif
|
||||
#if SERVER
|
||||
void ServerRead(ClientNetObject type, NetBuffer msg, Client c);
|
||||
void ServerRead(ClientNetObject type, IReadMessage msg, Client c);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -23,10 +21,10 @@ namespace Barotrauma.Networking
|
||||
interface IServerSerializable : INetSerializable
|
||||
{
|
||||
#if SERVER
|
||||
void ServerWrite(NetBuffer msg, Client c, object[] extraData = null);
|
||||
void ServerWrite(IWriteMessage msg, Client c, object[] extraData = null);
|
||||
#endif
|
||||
#if CLIENT
|
||||
void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime);
|
||||
void ClientRead(ServerNetObject type, IReadMessage msg, float sendingTime);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,12 +53,12 @@ namespace Barotrauma
|
||||
public float ExtinguishFireKarmaIncrease { get; set; }
|
||||
|
||||
|
||||
private float allowedWireDisconnectionsPerMinute;
|
||||
[Serialize(5.0f, true)]
|
||||
public float AllowedWireDisconnectionsPerMinute
|
||||
private int allowedWireDisconnectionsPerMinute;
|
||||
[Serialize(5, true)]
|
||||
public int AllowedWireDisconnectionsPerMinute
|
||||
{
|
||||
get { return allowedWireDisconnectionsPerMinute; }
|
||||
set { allowedWireDisconnectionsPerMinute = Math.Max(0.0f, value); }
|
||||
set { allowedWireDisconnectionsPerMinute = Math.Max(0, value); }
|
||||
}
|
||||
|
||||
[Serialize(6.0f, true)]
|
||||
@@ -76,6 +76,9 @@ namespace Barotrauma
|
||||
[Serialize(1.0f, true)]
|
||||
public float KickBanThreshold { get; set; }
|
||||
|
||||
[Serialize(0, true)]
|
||||
public int KicksBeforeBan { get; set; }
|
||||
|
||||
[Serialize(10.0f, true)]
|
||||
public float KarmaNotificationInterval { get; set; }
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
|
||||
+7
-8
@@ -1,5 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
@@ -11,10 +10,10 @@ namespace Barotrauma.Networking
|
||||
/// <summary>
|
||||
/// Write the events to the outgoing message. The recipient parameter is only needed for ServerEntityEventManager
|
||||
/// </summary>
|
||||
protected void Write(NetOutgoingMessage msg, List<NetEntityEvent> eventsToSync, out List<NetEntityEvent> sentEvents, Client recipient = null)
|
||||
protected void Write(IWriteMessage msg, List<NetEntityEvent> eventsToSync, out List<NetEntityEvent> sentEvents, Client recipient = null)
|
||||
{
|
||||
//write into a temporary buffer so we can write the number of events before the actual data
|
||||
NetBuffer tempBuffer = new NetBuffer();
|
||||
IWriteMessage tempBuffer = new WriteOnlyMessage();
|
||||
|
||||
sentEvents = new List<NetEntityEvent>();
|
||||
|
||||
@@ -22,7 +21,7 @@ namespace Barotrauma.Networking
|
||||
foreach (NetEntityEvent e in eventsToSync)
|
||||
{
|
||||
//write into a temporary buffer so we can write the length before the actual data
|
||||
NetBuffer tempEventBuffer = new NetBuffer();
|
||||
IWriteMessage tempEventBuffer = new WriteOnlyMessage();
|
||||
try
|
||||
{
|
||||
WriteEvent(tempEventBuffer, e, recipient);
|
||||
@@ -67,7 +66,7 @@ namespace Barotrauma.Networking
|
||||
|
||||
tempBuffer.Write(e.EntityID);
|
||||
tempBuffer.Write((byte)tempEventBuffer.LengthBytes);
|
||||
tempBuffer.Write(tempEventBuffer);
|
||||
tempBuffer.Write(tempEventBuffer.Buffer, 0, tempEventBuffer.LengthBytes);
|
||||
tempBuffer.WritePadBits();
|
||||
sentEvents.Add(e);
|
||||
|
||||
@@ -78,10 +77,10 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
msg.Write(eventsToSync[0].ID);
|
||||
msg.Write((byte)eventCount);
|
||||
msg.Write(tempBuffer);
|
||||
msg.Write(tempBuffer.Buffer, 0, tempBuffer.LengthBytes);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null);
|
||||
protected abstract void WriteEvent(IWriteMessage buffer, NetEntityEvent entityEvent, Client recipient = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Barotrauma.Items.Components;
|
||||
using Lidgren.Network;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -9,9 +8,6 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
enum ClientPacketHeader
|
||||
{
|
||||
REQUEST_AUTH, //ask the server if a password is needed, if so we'll get nonce for encryption
|
||||
REQUEST_STEAMAUTH, //the same as REQUEST_AUTH, but in addition we want to authenticate the player's Steam ID
|
||||
REQUEST_INIT, //ask the server to give you initialization
|
||||
UPDATE_LOBBY, //update state in lobby
|
||||
UPDATE_INGAME, //update state ingame
|
||||
|
||||
@@ -64,7 +60,9 @@ namespace Barotrauma.Networking
|
||||
|
||||
QUERY_STARTGAME, //ask the clients whether they're ready to start
|
||||
STARTGAME, //start a new round
|
||||
ENDGAME
|
||||
ENDGAME,
|
||||
|
||||
TRAITOR_MESSAGE
|
||||
}
|
||||
enum ServerNetObject
|
||||
{
|
||||
@@ -78,6 +76,14 @@ namespace Barotrauma.Networking
|
||||
ENTITY_EVENT_INITIAL,
|
||||
}
|
||||
|
||||
enum TraitorMessageType
|
||||
{
|
||||
Server,
|
||||
ServerMessageBox,
|
||||
Objective,
|
||||
Console
|
||||
}
|
||||
|
||||
enum VoteType
|
||||
{
|
||||
Unknown,
|
||||
@@ -94,6 +100,7 @@ namespace Barotrauma.Networking
|
||||
Banned,
|
||||
Kicked,
|
||||
ServerShutdown,
|
||||
ServerCrashed,
|
||||
ServerFull,
|
||||
AuthenticationRequired,
|
||||
SteamAuthenticationRequired,
|
||||
@@ -132,13 +139,7 @@ namespace Barotrauma.Networking
|
||||
#if DEBUG
|
||||
public Dictionary<string, long> messageCount = new Dictionary<string, long>();
|
||||
#endif
|
||||
|
||||
public NetPeer NetPeer
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
|
||||
protected string name;
|
||||
|
||||
protected ServerSettings serverSettings;
|
||||
@@ -154,12 +155,6 @@ namespace Barotrauma.Networking
|
||||
|
||||
public bool ShowNetStats;
|
||||
|
||||
public int Port
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public int TickRate
|
||||
{
|
||||
get { return serverSettings.TickRate; }
|
||||
@@ -205,13 +200,7 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
get { return serverSettings; }
|
||||
}
|
||||
|
||||
public NetPeerConfiguration NetPeerConfiguration
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
|
||||
public bool CanUseRadio(Character sender)
|
||||
{
|
||||
if (sender == null) return false;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public enum DeliveryMethod : byte
|
||||
{
|
||||
Unreliable = 0x0,
|
||||
Reliable = 0x1,
|
||||
ReliableOrdered = 0x2
|
||||
}
|
||||
|
||||
public enum ConnectionInitialization : byte
|
||||
{
|
||||
//used by all peer implementations
|
||||
SteamTicketAndVersion = 0x1,
|
||||
Password = 0x2,
|
||||
Success = 0x0,
|
||||
|
||||
//used only by SteamP2P implementations
|
||||
ConnectionStarted = 0x3
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum PacketHeader : byte
|
||||
{
|
||||
//used by all peer implementations
|
||||
None = 0x0,
|
||||
IsCompressed = 0x1,
|
||||
IsConnectionInitializationStep = 0x2,
|
||||
|
||||
//used only by SteamP2P implementations
|
||||
IsDisconnectMessage = 0x4,
|
||||
IsServerMessage = 0x8,
|
||||
IsHeartbeatMessage = 0x10
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public interface IReadMessage
|
||||
{
|
||||
bool ReadBoolean();
|
||||
void ReadPadBits();
|
||||
byte ReadByte();
|
||||
UInt16 ReadUInt16();
|
||||
Int16 ReadInt16();
|
||||
UInt32 ReadUInt32();
|
||||
Int32 ReadInt32();
|
||||
UInt64 ReadUInt64();
|
||||
Int64 ReadInt64();
|
||||
Single ReadSingle();
|
||||
Double ReadDouble();
|
||||
UInt32 ReadVariableUInt32();
|
||||
String ReadString();
|
||||
int ReadRangedInteger(int min, int max);
|
||||
Single ReadRangedSingle(Single min, Single max, int bitCount);
|
||||
byte[] ReadBytes(int numberOfBytes);
|
||||
|
||||
int BitPosition { get; set; }
|
||||
int BytePosition { get; }
|
||||
byte[] Buffer { get; }
|
||||
int LengthBits { get; set; }
|
||||
int LengthBytes { get; }
|
||||
|
||||
NetworkConnection Sender { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public interface IWriteMessage
|
||||
{
|
||||
void Write(bool val);
|
||||
void WritePadBits();
|
||||
void Write(byte val);
|
||||
void Write(Int16 val);
|
||||
void Write(UInt16 val);
|
||||
void Write(Int32 val);
|
||||
void Write(UInt32 val);
|
||||
void Write(Int64 val);
|
||||
void Write(UInt64 val);
|
||||
void Write(Single val);
|
||||
void Write(Double val);
|
||||
void WriteVariableUInt32(UInt32 val);
|
||||
void Write(string val);
|
||||
void WriteRangedIntegerDeprecated(int min, int max, int val); //TODO: remove this, val should be first parameter >:(
|
||||
void WriteRangedInteger(int val, int min, int max);
|
||||
void WriteRangedSingle(Single val, Single min, Single max, int bitCount);
|
||||
void Write(byte[] val, int startIndex, int length);
|
||||
|
||||
void PrepareForSending(ref byte[] outBuf, out bool isCompressed, out int outLength);
|
||||
|
||||
int BitPosition { get; set; }
|
||||
int BytePosition { get; }
|
||||
byte[] Buffer { get; }
|
||||
int LengthBits { get; set; }
|
||||
int LengthBytes { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,955 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public static class MsgConstants
|
||||
{
|
||||
public const int MTU = 1200;
|
||||
public const int CompressionThreshold = 1000;
|
||||
public const int InitialBufferSize = 256;
|
||||
public const int BufferOverAllocateAmount = 4;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Utility struct for writing Singles
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct SingleUIntUnion
|
||||
{
|
||||
/// <summary>
|
||||
/// Value as a 32 bit float
|
||||
/// </summary>
|
||||
[FieldOffset(0)]
|
||||
public float SingleValue;
|
||||
|
||||
/// <summary>
|
||||
/// Value as an unsigned 32 bit integer
|
||||
/// </summary>
|
||||
[FieldOffset(0)]
|
||||
public uint UIntValue;
|
||||
}
|
||||
|
||||
internal static class MsgWriter
|
||||
{
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, bool val)
|
||||
{
|
||||
#if DEBUG
|
||||
int resetPos = bitPos;
|
||||
#endif
|
||||
|
||||
EnsureBufferSize(ref buf, bitPos + 1);
|
||||
|
||||
int bytePos = bitPos / 8;
|
||||
int bitOffset = bitPos % 8;
|
||||
byte bitFlag = (byte)(1 << bitOffset);
|
||||
byte bitMask = (byte)((~bitFlag) & 0xff);
|
||||
buf[bytePos] &= bitMask;
|
||||
if (val) buf[bytePos] |= bitFlag;
|
||||
bitPos++;
|
||||
|
||||
#if DEBUG
|
||||
bool testVal = MsgReader.ReadBoolean(buf, ref resetPos);
|
||||
if (testVal != val || resetPos != bitPos)
|
||||
{
|
||||
DebugConsole.ThrowError("Boolean written incorrectly! " + testVal + ", " + val + "; " + resetPos + ", " + bitPos);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static void WritePadBits(ref byte[] buf, ref int bitPos)
|
||||
{
|
||||
int bitOffset = bitPos % 8;
|
||||
bitPos += ((8 - bitOffset) % 8);
|
||||
EnsureBufferSize(ref buf, bitPos);
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, byte val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 8);
|
||||
NetBitWriter.WriteByte(val, 8, buf, bitPos);
|
||||
bitPos += 8;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, UInt16 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 16);
|
||||
NetBitWriter.WriteUInt16(val, 16, buf, bitPos);
|
||||
bitPos += 16;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, Int16 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 16);
|
||||
NetBitWriter.WriteUInt16((UInt16)val, 16, buf, bitPos);
|
||||
bitPos += 16;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, UInt32 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 32);
|
||||
NetBitWriter.WriteUInt32(val, 32, buf, bitPos);
|
||||
bitPos += 32;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, Int32 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 32);
|
||||
NetBitWriter.WriteUInt32((UInt32)val, 32, buf, bitPos);
|
||||
bitPos += 32;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, UInt64 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 64);
|
||||
NetBitWriter.WriteUInt64(val, 64, buf, bitPos);
|
||||
bitPos += 64;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, Int64 val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 64);
|
||||
NetBitWriter.WriteUInt64((UInt64)val, 64, buf, bitPos);
|
||||
bitPos += 64;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, Single val)
|
||||
{
|
||||
// Use union to avoid BitConverter.GetBytes() which allocates memory on the heap
|
||||
SingleUIntUnion su;
|
||||
su.UIntValue = 0; // must initialize every member of the union to avoid warning
|
||||
su.SingleValue = val;
|
||||
|
||||
EnsureBufferSize(ref buf, bitPos + 32);
|
||||
|
||||
NetBitWriter.WriteUInt32(su.UIntValue, 32, buf, bitPos);
|
||||
bitPos += 32;
|
||||
}
|
||||
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, Double val)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + 64);
|
||||
|
||||
byte[] bytes = BitConverter.GetBytes(val);
|
||||
WriteBytes(ref buf, ref bitPos, bytes, 0, bytes.Length);
|
||||
bitPos += 64;
|
||||
}
|
||||
internal static void Write(ref byte[] buf, ref int bitPos, string val)
|
||||
{
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
WriteVariableUInt32(ref buf, ref bitPos, (uint)0);
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(val);
|
||||
WriteVariableUInt32(ref buf, ref bitPos, (uint)bytes.Length);
|
||||
WriteBytes(ref buf, ref bitPos, bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
internal static int WriteVariableUInt32(ref byte[] buf, ref int bitPos, uint value)
|
||||
{
|
||||
int retval = 1;
|
||||
uint num1 = (uint)value;
|
||||
while (num1 >= 0x80)
|
||||
{
|
||||
Write(ref buf, ref bitPos, (byte)(num1 | 0x80));
|
||||
num1 = num1 >> 7;
|
||||
retval++;
|
||||
}
|
||||
Write(ref buf, ref bitPos, (byte)num1);
|
||||
return retval;
|
||||
}
|
||||
|
||||
internal static void WriteRangedInteger(ref byte[] buf, ref int bitPos, int val, int min, int max)
|
||||
{
|
||||
uint range = (uint)(max - min);
|
||||
int numberOfBits = NetUtility.BitsToHoldUInt(range);
|
||||
|
||||
EnsureBufferSize(ref buf, bitPos + numberOfBits);
|
||||
|
||||
uint rvalue = (uint)(val - min);
|
||||
NetBitWriter.WriteUInt32(rvalue, numberOfBits, buf, bitPos);
|
||||
bitPos += numberOfBits;
|
||||
}
|
||||
|
||||
internal static void WriteRangedSingle(ref byte[] buf, ref int bitPos, Single val, Single min, Single max, int numberOfBits)
|
||||
{
|
||||
float range = max - min;
|
||||
float unit = ((val - min) / range);
|
||||
int maxVal = (1 << numberOfBits) - 1;
|
||||
|
||||
EnsureBufferSize(ref buf, bitPos + numberOfBits);
|
||||
|
||||
NetBitWriter.WriteUInt32((UInt32)((float)maxVal * unit), numberOfBits, buf, bitPos);
|
||||
bitPos += numberOfBits;
|
||||
}
|
||||
|
||||
internal static void WriteBytes(ref byte[] buf, ref int bitPos, byte[] val, int pos, int length)
|
||||
{
|
||||
EnsureBufferSize(ref buf, bitPos + length * 8);
|
||||
NetBitWriter.WriteBytes(val, pos, length, buf, bitPos);
|
||||
bitPos += length * 8;
|
||||
}
|
||||
|
||||
internal static void EnsureBufferSize(ref byte[] buf, int numberOfBits)
|
||||
{
|
||||
int byteLen = ((numberOfBits + 7) >> 3);
|
||||
if (buf == null)
|
||||
{
|
||||
buf = new byte[byteLen + MsgConstants.BufferOverAllocateAmount];
|
||||
return;
|
||||
}
|
||||
if (buf.Length < byteLen)
|
||||
{
|
||||
Array.Resize<byte>(ref buf, byteLen + MsgConstants.BufferOverAllocateAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class MsgReader
|
||||
{
|
||||
internal static bool ReadBoolean(byte[] buf, ref int bitPos)
|
||||
{
|
||||
byte retval = NetBitWriter.ReadByte(buf, 1, bitPos);
|
||||
bitPos++;
|
||||
return (retval > 0 ? true : false);
|
||||
}
|
||||
|
||||
internal static void ReadPadBits(byte[] buf, ref int bitPos)
|
||||
{
|
||||
int bitOffset = bitPos % 8;
|
||||
bitPos += (8 - bitOffset) % 8;
|
||||
}
|
||||
|
||||
internal static byte ReadByte(byte[] buf, ref int bitPos)
|
||||
{
|
||||
byte retval = NetBitWriter.ReadByte(buf, 8, bitPos);
|
||||
bitPos += 8;
|
||||
return retval;
|
||||
}
|
||||
|
||||
internal static UInt16 ReadUInt16(byte[] buf, ref int bitPos)
|
||||
{
|
||||
uint retval = NetBitWriter.ReadUInt16(buf, 16, bitPos);
|
||||
bitPos += 16;
|
||||
return (ushort)retval;
|
||||
}
|
||||
|
||||
internal static Int16 ReadInt16(byte[] buf, ref int bitPos)
|
||||
{
|
||||
return (Int16)ReadUInt16(buf, ref bitPos);
|
||||
}
|
||||
|
||||
internal static UInt32 ReadUInt32(byte[] buf, ref int bitPos)
|
||||
{
|
||||
uint retval = NetBitWriter.ReadUInt32(buf, 32, bitPos);
|
||||
bitPos += 32;
|
||||
return retval;
|
||||
}
|
||||
|
||||
internal static Int32 ReadInt32(byte[] buf, ref int bitPos)
|
||||
{
|
||||
return (Int32)ReadUInt32(buf, ref bitPos);
|
||||
}
|
||||
|
||||
internal static UInt64 ReadUInt64(byte[] buf, ref int bitPos)
|
||||
{
|
||||
ulong low = NetBitWriter.ReadUInt32(buf, 32, bitPos);
|
||||
bitPos += 32;
|
||||
ulong high = NetBitWriter.ReadUInt32(buf, 32, bitPos);
|
||||
ulong retval = low + (high << 32);
|
||||
bitPos += 32;
|
||||
return retval;
|
||||
}
|
||||
|
||||
internal static Int64 ReadInt64(byte[] buf, ref int bitPos)
|
||||
{
|
||||
return (Int64)ReadUInt64(buf, ref bitPos);
|
||||
}
|
||||
|
||||
internal static Single ReadSingle(byte[] buf, ref int bitPos)
|
||||
{
|
||||
if ((bitPos & 7) == 0) // read directly
|
||||
{
|
||||
float retval = BitConverter.ToSingle(buf, bitPos >> 3);
|
||||
bitPos += 32;
|
||||
return retval;
|
||||
}
|
||||
|
||||
byte[] bytes = ReadBytes(buf, ref bitPos, 4);
|
||||
return BitConverter.ToSingle(bytes, 0);
|
||||
}
|
||||
|
||||
internal static Double ReadDouble(byte[] buf, ref int bitPos)
|
||||
{
|
||||
if ((bitPos & 7) == 0) // read directly
|
||||
{
|
||||
// read directly
|
||||
double retval = BitConverter.ToDouble(buf, bitPos >> 3);
|
||||
bitPos += 64;
|
||||
return retval;
|
||||
}
|
||||
|
||||
byte[] bytes = ReadBytes(buf, ref bitPos, 8);
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
internal static UInt32 ReadVariableUInt32(byte[] buf, ref int bitPos)
|
||||
{
|
||||
int bitLength = buf.Length * 8;
|
||||
|
||||
int num1 = 0;
|
||||
int num2 = 0;
|
||||
while (bitLength - bitPos >= 8)
|
||||
{
|
||||
byte num3 = ReadByte(buf, ref bitPos);
|
||||
num1 |= (num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
if ((num3 & 0x80) == 0)
|
||||
return (uint)num1;
|
||||
}
|
||||
|
||||
// ouch; failed to find enough bytes; malformed variable length number?
|
||||
return (uint)num1;
|
||||
}
|
||||
|
||||
internal static String ReadString(byte[] buf, ref int bitPos)
|
||||
{
|
||||
int bitLength = buf.Length * 8;
|
||||
int byteLen = (int)ReadVariableUInt32(buf, ref bitPos);
|
||||
|
||||
if (byteLen <= 0) { return String.Empty; }
|
||||
|
||||
if ((ulong)(bitLength - bitPos) < ((ulong)byteLen * 8))
|
||||
{
|
||||
// not enough data
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((bitPos & 7) == 0)
|
||||
{
|
||||
// read directly
|
||||
string retval = System.Text.Encoding.UTF8.GetString(buf, bitPos >> 3, byteLen);
|
||||
bitPos += (8 * byteLen);
|
||||
return retval;
|
||||
}
|
||||
|
||||
byte[] bytes = ReadBytes(buf, ref bitPos, byteLen);
|
||||
return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
internal static int ReadRangedInteger(byte[] buf, ref int bitPos, int min, int max)
|
||||
{
|
||||
uint range = (uint)(max - min);
|
||||
int numBits = NetUtility.BitsToHoldUInt(range);
|
||||
|
||||
uint rvalue = NetBitWriter.ReadUInt32(buf, numBits, bitPos);
|
||||
bitPos += numBits;
|
||||
|
||||
return (int)(min + rvalue);
|
||||
}
|
||||
|
||||
internal static Single ReadRangedSingle(byte[] buf, ref int bitPos, Single min, Single max, int bitCount)
|
||||
{
|
||||
int maxInt = (1 << bitCount) - 1;
|
||||
int intVal = ReadRangedInteger(buf, ref bitPos, 0, maxInt);
|
||||
Single range = max - min;
|
||||
return min + (range * ((Single)intVal) / ((Single)maxInt));
|
||||
}
|
||||
|
||||
internal static byte[] ReadBytes(byte[] buf, ref int bitPos, int numberOfBytes)
|
||||
{
|
||||
byte[] retval = new byte[numberOfBytes];
|
||||
NetBitWriter.ReadBytes(buf, numberOfBytes, bitPos, retval, 0);
|
||||
bitPos += (8 * numberOfBytes);
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
public class WriteOnlyMessage : IWriteMessage
|
||||
{
|
||||
private byte[] buf = new byte[MsgConstants.InitialBufferSize];
|
||||
private int seekPos = 0;
|
||||
private int lengthBits = 0;
|
||||
|
||||
public int BitPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos;
|
||||
}
|
||||
set
|
||||
{
|
||||
seekPos = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int BytePosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Buffer
|
||||
{
|
||||
get
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBits
|
||||
{
|
||||
get
|
||||
{
|
||||
lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
|
||||
return lengthBits;
|
||||
}
|
||||
set
|
||||
{
|
||||
lengthBits = value;
|
||||
seekPos = seekPos > lengthBits ? lengthBits : seekPos;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
return (LengthBits + ((8 - (LengthBits % 8)) % 8)) / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(bool val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WritePadBits()
|
||||
{
|
||||
MsgWriter.WritePadBits(ref buf, ref seekPos);
|
||||
}
|
||||
|
||||
public void Write(byte val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt16 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int16 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt32 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int32 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt64 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int64 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Single val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Double val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WriteVariableUInt32(UInt32 val)
|
||||
{
|
||||
MsgWriter.WriteVariableUInt32(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(String val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WriteRangedIntegerDeprecated(int min, int max, int val)
|
||||
{
|
||||
MsgWriter.WriteRangedInteger(ref buf, ref seekPos, val, min, max);
|
||||
}
|
||||
|
||||
public void WriteRangedInteger(int val, int min, int max)
|
||||
{
|
||||
MsgWriter.WriteRangedInteger(ref buf, ref seekPos, val, min, max);
|
||||
}
|
||||
|
||||
public void WriteRangedSingle(Single val, Single min, Single max, int bitCount)
|
||||
{
|
||||
MsgWriter.WriteRangedSingle(ref buf, ref seekPos, val, min, max, bitCount);
|
||||
}
|
||||
|
||||
public void Write(byte[] val, int startPos, int length)
|
||||
{
|
||||
MsgWriter.WriteBytes(ref buf, ref seekPos, val, startPos, length);
|
||||
}
|
||||
|
||||
public void PrepareForSending(ref byte[] outBuf, out bool isCompressed, out int length)
|
||||
{
|
||||
if (LengthBytes <= MsgConstants.CompressionThreshold)
|
||||
{
|
||||
isCompressed = false;
|
||||
if (LengthBytes > outBuf.Length) { Array.Resize(ref outBuf, LengthBytes); }
|
||||
Array.Copy(buf, outBuf, LengthBytes);
|
||||
length = LengthBytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (MemoryStream output = new MemoryStream())
|
||||
{
|
||||
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Fastest))
|
||||
{
|
||||
dstream.Write(buf, 0, LengthBytes);
|
||||
}
|
||||
|
||||
byte[] compressedBuf = output.ToArray();
|
||||
//don't send the data as compressed if the data takes up more space after compression
|
||||
//(which may happen when sending a sub/save file that's already been compressed with a better compression ratio)
|
||||
if (compressedBuf.Length >= outBuf.Length)
|
||||
{
|
||||
isCompressed = false;
|
||||
if (LengthBytes > outBuf.Length) { Array.Resize(ref outBuf, LengthBytes); }
|
||||
Array.Copy(buf, outBuf, LengthBytes);
|
||||
length = LengthBytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
isCompressed = true;
|
||||
if (compressedBuf.Length > outBuf.Length) { Array.Resize(ref outBuf, compressedBuf.Length); }
|
||||
Array.Copy(compressedBuf, outBuf, compressedBuf.Length);
|
||||
length = compressedBuf.Length;
|
||||
DebugConsole.NewMessage("Compressed message: " + LengthBytes + " to " + length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadOnlyMessage : IReadMessage
|
||||
{
|
||||
private byte[] buf;
|
||||
private int seekPos = 0;
|
||||
private int lengthBits = 0;
|
||||
|
||||
public int BitPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos;
|
||||
}
|
||||
set
|
||||
{
|
||||
seekPos = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int BytePosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Buffer
|
||||
{
|
||||
get
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBits
|
||||
{
|
||||
get
|
||||
{
|
||||
lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
|
||||
return lengthBits;
|
||||
}
|
||||
set
|
||||
{
|
||||
lengthBits = value;
|
||||
seekPos = seekPos > lengthBits ? lengthBits : seekPos;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
return lengthBits / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public NetworkConnection Sender { get; private set; }
|
||||
|
||||
public ReadOnlyMessage(byte[] inBuf, bool isCompressed, int startPos, int inLength, NetworkConnection sender)
|
||||
{
|
||||
Sender = sender;
|
||||
if (isCompressed)
|
||||
{
|
||||
byte[] decompressedData;
|
||||
using (MemoryStream input = new MemoryStream(inBuf, startPos, inLength))
|
||||
{
|
||||
using (MemoryStream output = new MemoryStream())
|
||||
{
|
||||
using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
|
||||
{
|
||||
dstream.CopyTo(output);
|
||||
}
|
||||
decompressedData = output.ToArray();
|
||||
}
|
||||
}
|
||||
buf = new byte[decompressedData.Length];
|
||||
Array.Copy(decompressedData, 0, buf, 0, decompressedData.Length);
|
||||
lengthBits = decompressedData.Length * 8;
|
||||
DebugConsole.NewMessage("Decompressing message: " + inLength + " to " + LengthBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = new byte[inBuf.Length];
|
||||
Array.Copy(inBuf, startPos, buf, 0, inLength);
|
||||
lengthBits = inLength * 8;
|
||||
}
|
||||
seekPos = 0;
|
||||
}
|
||||
|
||||
public bool ReadBoolean()
|
||||
{
|
||||
return MsgReader.ReadBoolean(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public void ReadPadBits()
|
||||
{
|
||||
MsgReader.ReadPadBits(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public byte ReadByte()
|
||||
{
|
||||
return MsgReader.ReadByte(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt16 ReadUInt16()
|
||||
{
|
||||
return MsgReader.ReadUInt16(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int16 ReadInt16()
|
||||
{
|
||||
return MsgReader.ReadInt16(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt32 ReadUInt32()
|
||||
{
|
||||
return MsgReader.ReadUInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int32 ReadInt32()
|
||||
{
|
||||
return MsgReader.ReadInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt64 ReadUInt64()
|
||||
{
|
||||
return MsgReader.ReadUInt64(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int64 ReadInt64()
|
||||
{
|
||||
return MsgReader.ReadInt64(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Single ReadSingle()
|
||||
{
|
||||
return MsgReader.ReadSingle(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Double ReadDouble()
|
||||
{
|
||||
return MsgReader.ReadDouble(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt32 ReadVariableUInt32()
|
||||
{
|
||||
return MsgReader.ReadVariableUInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public String ReadString()
|
||||
{
|
||||
return MsgReader.ReadString(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public int ReadRangedInteger(int min, int max)
|
||||
{
|
||||
return MsgReader.ReadRangedInteger(buf, ref seekPos, min, max);
|
||||
}
|
||||
|
||||
public Single ReadRangedSingle(Single min, Single max, int bitCount)
|
||||
{
|
||||
return MsgReader.ReadRangedSingle(buf, ref seekPos, min, max, bitCount);
|
||||
}
|
||||
|
||||
public byte[] ReadBytes(int numberOfBytes)
|
||||
{
|
||||
return MsgReader.ReadBytes(buf, ref seekPos, numberOfBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadWriteMessage : IWriteMessage, IReadMessage
|
||||
{
|
||||
private byte[] buf = new byte[MsgConstants.InitialBufferSize];
|
||||
private int seekPos = 0;
|
||||
private int lengthBits = 0;
|
||||
|
||||
public int BitPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos;
|
||||
}
|
||||
set
|
||||
{
|
||||
seekPos = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int BytePosition
|
||||
{
|
||||
get
|
||||
{
|
||||
return seekPos / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Buffer
|
||||
{
|
||||
get
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBits
|
||||
{
|
||||
get
|
||||
{
|
||||
lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
|
||||
return lengthBits;
|
||||
}
|
||||
set
|
||||
{
|
||||
lengthBits = value;
|
||||
seekPos = seekPos > lengthBits ? lengthBits : seekPos;
|
||||
}
|
||||
}
|
||||
|
||||
public int LengthBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
return (LengthBits + ((8 - (LengthBits % 8)) % 8)) / 8;
|
||||
}
|
||||
}
|
||||
|
||||
public NetworkConnection Sender { get { return null; } }
|
||||
|
||||
public void Write(bool val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WritePadBits()
|
||||
{
|
||||
MsgWriter.WritePadBits(ref buf, ref seekPos);
|
||||
}
|
||||
|
||||
public void Write(byte val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt16 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int16 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt32 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int32 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(UInt64 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Int64 val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Single val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(Double val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WriteVariableUInt32(UInt32 val)
|
||||
{
|
||||
MsgWriter.WriteVariableUInt32(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void Write(String val)
|
||||
{
|
||||
MsgWriter.Write(ref buf, ref seekPos, val);
|
||||
}
|
||||
|
||||
public void WriteRangedIntegerDeprecated(int min, int max, int val)
|
||||
{
|
||||
MsgWriter.WriteRangedInteger(ref buf, ref seekPos, val, min, max);
|
||||
}
|
||||
|
||||
public void WriteRangedInteger(int val, int min, int max)
|
||||
{
|
||||
MsgWriter.WriteRangedInteger(ref buf, ref seekPos, val, min, max);
|
||||
}
|
||||
|
||||
public void WriteRangedSingle(Single val, Single min, Single max, int bitCount)
|
||||
{
|
||||
MsgWriter.WriteRangedSingle(ref buf, ref seekPos, val, min, max, bitCount);
|
||||
}
|
||||
|
||||
public void Write(byte[] val, int startPos, int length)
|
||||
{
|
||||
MsgWriter.WriteBytes(ref buf, ref seekPos, val, startPos, length);
|
||||
}
|
||||
|
||||
public bool ReadBoolean()
|
||||
{
|
||||
return MsgReader.ReadBoolean(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public void ReadPadBits()
|
||||
{
|
||||
MsgReader.ReadPadBits(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public byte ReadByte()
|
||||
{
|
||||
return MsgReader.ReadByte(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt16 ReadUInt16()
|
||||
{
|
||||
return MsgReader.ReadUInt16(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int16 ReadInt16()
|
||||
{
|
||||
return MsgReader.ReadInt16(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt32 ReadUInt32()
|
||||
{
|
||||
return MsgReader.ReadUInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int32 ReadInt32()
|
||||
{
|
||||
return MsgReader.ReadInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt64 ReadUInt64()
|
||||
{
|
||||
return MsgReader.ReadUInt64(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Int64 ReadInt64()
|
||||
{
|
||||
return MsgReader.ReadInt64(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Single ReadSingle()
|
||||
{
|
||||
return MsgReader.ReadSingle(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public Double ReadDouble()
|
||||
{
|
||||
return MsgReader.ReadDouble(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public UInt32 ReadVariableUInt32()
|
||||
{
|
||||
return MsgReader.ReadVariableUInt32(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public String ReadString()
|
||||
{
|
||||
return MsgReader.ReadString(buf, ref seekPos);
|
||||
}
|
||||
|
||||
public int ReadRangedInteger(int min, int max)
|
||||
{
|
||||
return MsgReader.ReadRangedInteger(buf, ref seekPos, min, max);
|
||||
}
|
||||
|
||||
public Single ReadRangedSingle(Single min, Single max, int bitCount)
|
||||
{
|
||||
return MsgReader.ReadRangedSingle(buf, ref seekPos, min, max, bitCount);
|
||||
}
|
||||
|
||||
public byte[] ReadBytes(int numberOfBytes)
|
||||
{
|
||||
return MsgReader.ReadBytes(buf, ref seekPos, numberOfBytes);
|
||||
}
|
||||
|
||||
public void PrepareForSending(ref byte[] outBuf, out bool isCompressed, out int outLength)
|
||||
{
|
||||
throw new InvalidOperationException("ReadWriteMessages are not to be sent");
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public class LidgrenConnection : NetworkConnection
|
||||
{
|
||||
public NetConnection NetConnection { get; private set; }
|
||||
|
||||
public IPEndPoint IPEndPoint => NetConnection.RemoteEndPoint;
|
||||
|
||||
public string IPString
|
||||
{
|
||||
get
|
||||
{
|
||||
return IPEndPoint.Address.IsIPv4MappedToIPv6 ? IPEndPoint.Address.MapToIPv4().ToString() : IPEndPoint.Address.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public UInt16 Port
|
||||
{
|
||||
get
|
||||
{
|
||||
return (UInt16)IPEndPoint.Port;
|
||||
}
|
||||
}
|
||||
|
||||
public LidgrenConnection(string name, NetConnection netConnection, UInt64 steamId)
|
||||
{
|
||||
Name = name;
|
||||
NetConnection = netConnection;
|
||||
SteamID = steamId;
|
||||
EndPointString = IPString;
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public enum NetworkConnectionStatus
|
||||
{
|
||||
Connected = 0x1,
|
||||
Disconnected = 0x2
|
||||
}
|
||||
|
||||
public abstract class NetworkConnection
|
||||
{
|
||||
public string Name;
|
||||
|
||||
public UInt64 SteamID
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
public string EndPointString
|
||||
{
|
||||
get;
|
||||
protected set;
|
||||
}
|
||||
|
||||
public NetworkConnectionStatus Status = NetworkConnectionStatus.Disconnected;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
public class SteamP2PConnection : NetworkConnection
|
||||
{
|
||||
public double Timeout = 0.0;
|
||||
|
||||
public SteamP2PConnection(string name, UInt64 steamId)
|
||||
{
|
||||
SteamID = steamId;
|
||||
EndPointString = SteamID.ToString();
|
||||
Name = name;
|
||||
Heartbeat();
|
||||
}
|
||||
|
||||
public void Decay(float deltaTime)
|
||||
{
|
||||
Timeout -= deltaTime;
|
||||
}
|
||||
|
||||
public void Heartbeat()
|
||||
{
|
||||
Timeout = 20.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -282,7 +282,7 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
RespawnCharactersProjSpecific();
|
||||
}
|
||||
|
||||
|
||||
public Vector2 FindSpawnPos()
|
||||
{
|
||||
if (Level.Loaded == null || Submarine.MainSub == null) { return Vector2.Zero; }
|
||||
@@ -310,12 +310,11 @@ namespace Barotrauma.Networking
|
||||
//make sure there aren't any walls too close
|
||||
var tooCloseCells = Level.Loaded.GetTooCloseCells(potentialSpawnPos.Position.ToVector2(), Math.Max(minWidth, minHeight));
|
||||
if (tooCloseCells.Any()) { continue; }
|
||||
|
||||
|
||||
//make sure the spawnpoint is far enough from other subs
|
||||
foreach (Submarine sub in Submarine.Loaded)
|
||||
{
|
||||
if (sub == RespawnShuttle || RespawnShuttle.DockedTo.Contains(sub)) { continue; }
|
||||
|
||||
float minDist = Math.Max(Math.Max(minWidth, minHeight) + Math.Max(sub.Borders.Width, sub.Borders.Height), 10000.0f);
|
||||
if (Vector2.DistanceSquared(sub.WorldPosition, potentialSpawnPos.Position.ToVector2()) < minDist * minDist)
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ using System.Linq;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
partial class ServerLog
|
||||
public partial class ServerLog
|
||||
{
|
||||
private struct LogMessage
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@@ -14,21 +13,21 @@ using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
enum SelectionMode
|
||||
public enum SelectionMode
|
||||
{
|
||||
Manual = 0, Random = 1, Vote = 2
|
||||
}
|
||||
|
||||
enum YesNoMaybe
|
||||
public enum YesNoMaybe
|
||||
{
|
||||
No = 0, Maybe = 1, Yes = 2
|
||||
}
|
||||
|
||||
enum BotSpawnMode
|
||||
public enum BotSpawnMode
|
||||
{
|
||||
Normal, Fill
|
||||
}
|
||||
|
||||
|
||||
partial class ServerSettings : ISerializableEntity
|
||||
{
|
||||
public const string SettingsFile = "serversettings.xml";
|
||||
@@ -57,25 +56,17 @@ namespace Barotrauma.Networking
|
||||
|
||||
public class SavedClientPermission
|
||||
{
|
||||
public readonly string IP;
|
||||
public readonly string EndPoint;
|
||||
public readonly ulong SteamID;
|
||||
public readonly string Name;
|
||||
public List<DebugConsole.Command> PermittedCommands;
|
||||
|
||||
public ClientPermissions Permissions;
|
||||
|
||||
public SavedClientPermission(string name, IPAddress ip, ClientPermissions permissions, List<DebugConsole.Command> permittedCommands)
|
||||
public SavedClientPermission(string name, string endpoint, ClientPermissions permissions, List<DebugConsole.Command> permittedCommands)
|
||||
{
|
||||
this.Name = name;
|
||||
this.IP = ip.IsIPv4MappedToIPv6 ? ip.MapToIPv4().ToString() : ip.ToString();
|
||||
this.Permissions = permissions;
|
||||
this.PermittedCommands = permittedCommands;
|
||||
}
|
||||
public SavedClientPermission(string name, string ip, ClientPermissions permissions, List<DebugConsole.Command> permittedCommands)
|
||||
{
|
||||
this.Name = name;
|
||||
this.IP = ip;
|
||||
|
||||
this.EndPoint = endpoint;
|
||||
this.Permissions = permissions;
|
||||
this.PermittedCommands = permittedCommands;
|
||||
}
|
||||
@@ -139,9 +130,9 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(NetBuffer msg)
|
||||
public void Read(IReadMessage msg)
|
||||
{
|
||||
long oldPos = msg.Position;
|
||||
int oldPos = msg.BitPosition;
|
||||
UInt32 size = msg.ReadVariableUInt32();
|
||||
|
||||
float x; float y; float z; float w;
|
||||
@@ -152,7 +143,7 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
case "float":
|
||||
if (size != 4) break;
|
||||
property.SetValue(parentObject, msg.ReadFloat());
|
||||
property.SetValue(parentObject, msg.ReadSingle());
|
||||
return;
|
||||
case "int":
|
||||
if (size != 4) break;
|
||||
@@ -160,23 +151,23 @@ namespace Barotrauma.Networking
|
||||
return;
|
||||
case "vector2":
|
||||
if (size != 8) break;
|
||||
x = msg.ReadFloat();
|
||||
y = msg.ReadFloat();
|
||||
x = msg.ReadSingle();
|
||||
y = msg.ReadSingle();
|
||||
property.SetValue(parentObject, new Vector2(x, y));
|
||||
return;
|
||||
case "vector3":
|
||||
if (size != 12) break;
|
||||
x = msg.ReadFloat();
|
||||
y = msg.ReadFloat();
|
||||
z = msg.ReadFloat();
|
||||
x = msg.ReadSingle();
|
||||
y = msg.ReadSingle();
|
||||
z = msg.ReadSingle();
|
||||
property.SetValue(parentObject, new Vector3(x, y, z));
|
||||
return;
|
||||
case "vector4":
|
||||
if (size != 16) break;
|
||||
x = msg.ReadFloat();
|
||||
y = msg.ReadFloat();
|
||||
z = msg.ReadFloat();
|
||||
w = msg.ReadFloat();
|
||||
x = msg.ReadSingle();
|
||||
y = msg.ReadSingle();
|
||||
z = msg.ReadSingle();
|
||||
w = msg.ReadSingle();
|
||||
property.SetValue(parentObject, new Vector4(x, y, z, w));
|
||||
return;
|
||||
case "color":
|
||||
@@ -196,17 +187,17 @@ namespace Barotrauma.Networking
|
||||
property.SetValue(parentObject, new Rectangle(ix, iy, width, height));
|
||||
return;
|
||||
default:
|
||||
msg.Position = oldPos; //reset position to properly read the string
|
||||
msg.BitPosition = oldPos; //reset position to properly read the string
|
||||
string incVal = msg.ReadString();
|
||||
property.TrySetValue(parentObject, incVal);
|
||||
return;
|
||||
}
|
||||
|
||||
//size didn't match: skip this
|
||||
msg.Position += 8 * size;
|
||||
msg.BitPosition += (int)(8 * size);
|
||||
}
|
||||
|
||||
public void Write(NetBuffer msg, object overrideValue = null)
|
||||
public void Write(IWriteMessage msg, object overrideValue = null)
|
||||
{
|
||||
if (overrideValue == null) overrideValue = property.GetValue(parentObject);
|
||||
switch (typeString)
|
||||
@@ -287,7 +278,7 @@ namespace Barotrauma.Networking
|
||||
ServerName = serverName;
|
||||
Port = port;
|
||||
QueryPort = queryPort;
|
||||
//EnableUPnP = enableUPnP;
|
||||
EnableUPnP = enableUPnP;
|
||||
this.maxPlayers = maxPlayers;
|
||||
this.isPublic = isPublic;
|
||||
|
||||
@@ -328,7 +319,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string ServerName;
|
||||
|
||||
private string serverMessageText;
|
||||
@@ -346,7 +337,9 @@ namespace Barotrauma.Networking
|
||||
public int Port;
|
||||
|
||||
public int QueryPort;
|
||||
|
||||
|
||||
public bool EnableUPnP;
|
||||
|
||||
public ServerLog ServerLog;
|
||||
|
||||
public Voting Voting;
|
||||
@@ -354,10 +347,10 @@ namespace Barotrauma.Networking
|
||||
public Dictionary<string, bool> MonsterEnabled { get; private set; }
|
||||
|
||||
public Dictionary<ItemPrefab, int> ExtraCargo { get; private set; }
|
||||
|
||||
|
||||
private TimeSpan sparseUpdateInterval = new TimeSpan(0, 0, 0, 3);
|
||||
private float selectedLevelDifficulty;
|
||||
private string password;
|
||||
private byte[] password;
|
||||
|
||||
public float AutoRestartTimer;
|
||||
|
||||
@@ -370,7 +363,7 @@ namespace Barotrauma.Networking
|
||||
public List<SavedClientPermission> ClientPermissions { get; private set; } = new List<SavedClientPermission>();
|
||||
|
||||
public WhiteList Whitelist { get; private set; }
|
||||
|
||||
|
||||
[Serialize(20, true)]
|
||||
public int TickRate
|
||||
{
|
||||
@@ -446,7 +439,7 @@ namespace Barotrauma.Networking
|
||||
ServerDetailsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Serialize(true, true)]
|
||||
public bool EndRoundAtLevelEnd
|
||||
{
|
||||
@@ -514,9 +507,15 @@ namespace Barotrauma.Networking
|
||||
|
||||
public bool HasPassword
|
||||
{
|
||||
get { return !string.IsNullOrEmpty(password); }
|
||||
get { return password != null; }
|
||||
#if CLIENT
|
||||
set
|
||||
{
|
||||
password = value ? (password ?? new byte[1]) : null;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
[Serialize(true, true)]
|
||||
public bool AllowVoteKick
|
||||
{
|
||||
@@ -555,7 +554,7 @@ namespace Barotrauma.Networking
|
||||
ServerDetailsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Serialize(0, true)]
|
||||
public int BotCount
|
||||
{
|
||||
@@ -569,7 +568,8 @@ namespace Barotrauma.Networking
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
[Serialize(BotSpawnMode.Normal, true)]
|
||||
public BotSpawnMode BotSpawnMode
|
||||
{
|
||||
get;
|
||||
@@ -588,7 +588,7 @@ namespace Barotrauma.Networking
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
[Serialize(true, true)]
|
||||
public bool AllowRewiring
|
||||
{
|
||||
@@ -604,6 +604,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
|
||||
private YesNoMaybe traitorsEnabled;
|
||||
[Serialize(YesNoMaybe.No, true)]
|
||||
public YesNoMaybe TraitorsEnabled
|
||||
{
|
||||
get { return traitorsEnabled; }
|
||||
@@ -615,6 +616,13 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
[Serialize(defaultValue: 1, isSaveable: true)]
|
||||
public int TraitorsMinPlayerCount
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private SelectionMode subSelectionMode;
|
||||
[Serialize(SelectionMode.Manual, true)]
|
||||
public SelectionMode SubSelectionMode
|
||||
@@ -642,7 +650,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
|
||||
public BanList BanList { get; private set; }
|
||||
|
||||
|
||||
[Serialize(0.6f, true)]
|
||||
public float EndVoteRequiredRatio
|
||||
{
|
||||
@@ -663,7 +671,7 @@ namespace Barotrauma.Networking
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
|
||||
[Serialize(120.0f, true)]
|
||||
public float KickAFKTime
|
||||
{
|
||||
@@ -671,20 +679,6 @@ namespace Barotrauma.Networking
|
||||
private set;
|
||||
}
|
||||
|
||||
[Serialize(true, true)]
|
||||
public bool TraitorUseRatio
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
[Serialize(0.2f, true)]
|
||||
public float TraitorRatio
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
private bool karmaEnabled;
|
||||
[Serialize(false, true)]
|
||||
public bool KarmaEnabled
|
||||
@@ -719,7 +713,7 @@ namespace Barotrauma.Networking
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
public int MaxPlayers
|
||||
{
|
||||
get { return maxPlayers; }
|
||||
@@ -745,26 +739,42 @@ namespace Barotrauma.Networking
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
|
||||
public void SetPassword(string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(password))
|
||||
{
|
||||
this.password = "";
|
||||
this.password = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.password = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(password)));
|
||||
this.password = Lidgren.Network.NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(password));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsPasswordCorrect(string input, int nonce)
|
||||
public static byte[] SaltPassword(byte[] password, int salt)
|
||||
{
|
||||
byte[] saltedPw = new byte[password.Length*2];
|
||||
for (int i = 0; i < password.Length; i++)
|
||||
{
|
||||
saltedPw[(i * 2)] = password[i];
|
||||
saltedPw[(i * 2) + 1] = (byte)((salt >> (8 * (i % 4))) & 0xff);
|
||||
}
|
||||
saltedPw = Lidgren.Network.NetUtility.ComputeSHAHash(saltedPw);
|
||||
return saltedPw;
|
||||
}
|
||||
|
||||
public bool IsPasswordCorrect(byte[] input, int salt)
|
||||
{
|
||||
if (!HasPassword) return true;
|
||||
string saltedPw = password;
|
||||
saltedPw = saltedPw + Convert.ToString(nonce);
|
||||
saltedPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(saltedPw)));
|
||||
return input == saltedPw;
|
||||
byte[] saltedPw = SaltPassword(password, salt);
|
||||
DebugConsole.NewMessage(ToolBox.ByteArrayToString(input)+" "+ToolBox.ByteArrayToString(saltedPw));
|
||||
if (input.Length != saltedPw.Length) return false;
|
||||
for (int i=0;i<input.Length;i++)
|
||||
{
|
||||
if (input[i] != saltedPw[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -795,7 +805,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadMonsterEnabled(NetBuffer inc)
|
||||
public void ReadMonsterEnabled(IReadMessage inc)
|
||||
{
|
||||
InitMonstersEnabled();
|
||||
List<string> monsterNames = MonsterEnabled.Keys.ToList();
|
||||
@@ -806,7 +816,7 @@ namespace Barotrauma.Networking
|
||||
inc.ReadPadBits();
|
||||
}
|
||||
|
||||
public void WriteMonsterEnabled(NetBuffer msg, Dictionary<string, bool> monsterEnabled = null)
|
||||
public void WriteMonsterEnabled(IWriteMessage msg, Dictionary<string, bool> monsterEnabled = null)
|
||||
{
|
||||
//monster spawn settings
|
||||
if (monsterEnabled == null) monsterEnabled = MonsterEnabled;
|
||||
@@ -819,7 +829,7 @@ namespace Barotrauma.Networking
|
||||
msg.WritePadBits();
|
||||
}
|
||||
|
||||
public bool ReadExtraCargo(NetBuffer msg)
|
||||
public bool ReadExtraCargo(IReadMessage msg)
|
||||
{
|
||||
bool changed = false;
|
||||
UInt32 count = msg.ReadUInt32();
|
||||
@@ -844,7 +854,7 @@ namespace Barotrauma.Networking
|
||||
return changed;
|
||||
}
|
||||
|
||||
public void WriteExtraCargo(NetBuffer msg)
|
||||
public void WriteExtraCargo(IWriteMessage msg)
|
||||
{
|
||||
if (ExtraCargo == null)
|
||||
{
|
||||
|
||||
@@ -9,10 +9,20 @@ namespace Barotrauma.Steam
|
||||
{
|
||||
public const bool USE_STEAM = true;
|
||||
|
||||
public const int STEAMP2P_OWNER_PORT = 30000;
|
||||
|
||||
public const uint AppID = 602960;
|
||||
|
||||
|
||||
private Facepunch.Steamworks.Client client;
|
||||
private Server server;
|
||||
private Facepunch.Steamworks.Server server;
|
||||
|
||||
private static List<string> initializationErrors = new List<string>();
|
||||
public static IEnumerable<string> InitializationErrors
|
||||
{
|
||||
get { return initializationErrors; }
|
||||
}
|
||||
|
||||
public const string MetadataFileName = "filelist.xml";
|
||||
|
||||
private Dictionary<string, int> tagCommonness = new Dictionary<string, int>()
|
||||
{
|
||||
@@ -63,6 +73,34 @@ namespace Barotrauma.Steam
|
||||
instance = new SteamManager();
|
||||
}
|
||||
|
||||
public static ulong GetSteamID()
|
||||
{
|
||||
if (instance == null || !instance.isInitialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (instance.client != null)
|
||||
{
|
||||
return instance.client.SteamId;
|
||||
}
|
||||
else if (instance.server != null)
|
||||
{
|
||||
return instance.server.SteamId;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static string GetUsername()
|
||||
{
|
||||
if (instance == null || !instance.isInitialized || instance.client == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return instance.client.Username;
|
||||
}
|
||||
|
||||
public static void OverlayCustomURL(string url)
|
||||
{
|
||||
if (instance == null || !instance.isInitialized || instance.client == null)
|
||||
@@ -146,5 +184,33 @@ namespace Barotrauma.Steam
|
||||
instance.server = null;
|
||||
instance = null;
|
||||
}
|
||||
|
||||
public static UInt64 SteamIDStringToUInt64(string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str)) { return 0; }
|
||||
UInt64 retVal;
|
||||
if (UInt64.TryParse(str, out retVal) && retVal >(1<<52)) { return retVal; }
|
||||
if (str.ToUpper().IndexOf("STEAM_") != 0) { return 0; }
|
||||
string[] split = str.Substring(6).Split(':');
|
||||
if (split.Length != 3) { return 0; }
|
||||
|
||||
UInt64 universe = 0; UInt64 y = 0; UInt64 accountNumber = 0;
|
||||
if (!UInt64.TryParse(split[0], out universe)) { return 0; }
|
||||
if (!UInt64.TryParse(split[1], out y)) { return 0; }
|
||||
if (!UInt64.TryParse(split[2], out accountNumber)) { return 0; }
|
||||
|
||||
UInt64 accountInstance = 1; UInt64 accountType = 1;
|
||||
|
||||
return (universe << 56) | (accountType << 52) | (accountInstance << 32) | (accountNumber << 1) | y;
|
||||
}
|
||||
|
||||
public static string SteamIDUInt64ToString(UInt64 uint64)
|
||||
{
|
||||
UInt64 y = uint64 & 0x1;
|
||||
UInt64 accountNumber = (uint64 >> 1) & 0x7fffffff;
|
||||
UInt64 universe = (uint64 >> 56) & 0xff;
|
||||
|
||||
return "STEAM_" + universe.ToString() + ":" + y.ToString() + ":" + accountNumber.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
@@ -113,7 +112,7 @@ namespace Barotrauma.Networking
|
||||
outBuf = null;
|
||||
}
|
||||
|
||||
public virtual void Write(NetBuffer msg)
|
||||
public virtual void Write(IWriteMessage msg)
|
||||
{
|
||||
if (!CanSend) throw new Exception("Called Write on a VoipQueue not set up for sending");
|
||||
|
||||
@@ -127,7 +126,7 @@ namespace Barotrauma.Networking
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool Read(NetBuffer msg)
|
||||
public virtual bool Read(IReadMessage msg)
|
||||
{
|
||||
if (!CanReceive) throw new Exception("Called Read on a VoipQueue not set up for receiving");
|
||||
|
||||
@@ -138,7 +137,7 @@ namespace Barotrauma.Networking
|
||||
for (int i = 0; i < BUFFER_COUNT; i++)
|
||||
{
|
||||
bufferLengths[i] = msg.ReadByte();
|
||||
msg.ReadBytes(buffers[i], 0, bufferLengths[i]);
|
||||
buffers[i] = msg.ReadBytes(bufferLengths[i]);
|
||||
}
|
||||
newestBufferInd = BUFFER_COUNT - 1;
|
||||
LatestBufferID = incLatestBufferID;
|
||||
@@ -150,7 +149,7 @@ namespace Barotrauma.Networking
|
||||
for (int i = 0; i < BUFFER_COUNT; i++)
|
||||
{
|
||||
byte len = msg.ReadByte();
|
||||
msg.Position += len * 8;
|
||||
msg.BitPosition += len * 8;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user