copy paste from existing library
parent
66d79dc366
commit
d154b31bbf
@ -0,0 +1,299 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using BitSet;
|
||||
using DemoParser;
|
||||
using TF2Net;
|
||||
using TF2Net.Data;
|
||||
using TF2Net.NetMessages;
|
||||
|
||||
namespace DemoLib.Commands
|
||||
{
|
||||
public enum DemoCommandType : byte {
|
||||
|
||||
dem_invalid = 0,
|
||||
|
||||
// it's a startup message, process as fast as possible
|
||||
dem_signon = 1,
|
||||
// it's a normal network packet that we stored off
|
||||
dem_packet,
|
||||
// sync client clock to demo tick
|
||||
dem_synctick,
|
||||
// console command
|
||||
dem_consolecmd,
|
||||
// user input command
|
||||
dem_usercmd,
|
||||
// network data tables
|
||||
dem_datatables,
|
||||
// end of time.
|
||||
dem_stop,
|
||||
|
||||
dem_stringtables,
|
||||
|
||||
// Last command -- not necessary in C#
|
||||
//dem_lastcmd = dem_stringtables
|
||||
}
|
||||
|
||||
public class DemoCommand
|
||||
{
|
||||
public DemoCommandType Type { get; protected set; } = DemoCommandType.dem_invalid;
|
||||
}
|
||||
|
||||
class TimestampedDemoCommand : DemoCommand
|
||||
{
|
||||
public int Tick { get; set; }
|
||||
|
||||
public TimestampedDemoCommand(Stream input)
|
||||
{
|
||||
using (BinaryReader reader = new BinaryReader(input, Encoding.Default, true))
|
||||
{
|
||||
Tick = reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DemoConsoleCommand : TimestampedDemoCommand
|
||||
{
|
||||
public string Command { get; set; }
|
||||
|
||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||
private string DebuggerDisplayAttributeValue
|
||||
{
|
||||
get { return Command.Replace('"', '\''); }
|
||||
}
|
||||
|
||||
public DemoConsoleCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_consolecmd;
|
||||
|
||||
using (BinaryReader reader = new BinaryReader(input, Encoding.ASCII, true))
|
||||
Command = new string(reader.ReadChars(reader.ReadInt32())).TrimEnd('\0');
|
||||
}
|
||||
}
|
||||
|
||||
class DemoDataTablesCommand : TimestampedDemoCommand {
|
||||
const int PROPINFOBITS_NUMPROPS = 10;
|
||||
const int PROPINFOBITS_TYPE = 5;
|
||||
const int PROPINFOBITS_FLAGS = SPROP_NUMFLAGBITS_NETWORKED;
|
||||
const int PROPINFOBITS_NUMELEMENTS = 10;
|
||||
const int PROPINFOBITS_NUMBITS = 7;
|
||||
|
||||
const int SPROP_NUMFLAGBITS_NETWORKED = 16;
|
||||
|
||||
public IList<SendTable> SendTables { get; set; } = new List<SendTable>();
|
||||
public IList<ServerClass> ServerClasses { get; set; } = new List<ServerClass>();
|
||||
|
||||
public DemoDataTablesCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_datatables;
|
||||
|
||||
BitStream stream;
|
||||
using (BinaryReader reader = new BinaryReader(input, Encoding.ASCII, true))
|
||||
{
|
||||
int length = reader.ReadInt32();
|
||||
stream = new BitStream(reader.ReadBytes(length));
|
||||
}
|
||||
|
||||
while (stream.ReadBool())
|
||||
SendTables.Add(ParseSendTable(stream));
|
||||
|
||||
// Link referenced datatables
|
||||
foreach (SendTable table in SendTables)
|
||||
{
|
||||
foreach (SendPropDefinition dtProp in table.Properties)
|
||||
{
|
||||
if (dtProp.Type == SendPropType.Datatable)
|
||||
{
|
||||
dtProp.Table = SendTables.Single(t => t.NetTableName == dtProp.ExcludeName);
|
||||
dtProp.ExcludeName = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
short serverClasses = stream.ReadShort();
|
||||
Debug.Assert(serverClasses > 0);
|
||||
|
||||
ServerClasses = new List<ServerClass>(serverClasses);
|
||||
|
||||
for (int i = 0; i < serverClasses; i++)
|
||||
{
|
||||
short classID = stream.ReadShort();
|
||||
if (classID >= serverClasses)
|
||||
throw new Exception("Demo parsing failed: Invalid server class ID");
|
||||
|
||||
ServerClass sc = new ServerClass();
|
||||
sc.Classname = stream.ReadCString();
|
||||
sc.DatatableName = stream.ReadCString();
|
||||
ServerClasses.Add(sc);
|
||||
}
|
||||
|
||||
Debug.Assert((stream.Length - stream.Cursor) < 8);
|
||||
}
|
||||
|
||||
static SendTable ParseSendTable(BitStream stream)
|
||||
{
|
||||
SendTable table = new SendTable();
|
||||
|
||||
table.Unknown1 = stream.ReadBool();
|
||||
|
||||
table.NetTableName = stream.ReadCString();
|
||||
|
||||
int propertyCount = (int)stream.ReadULong(PROPINFOBITS_NUMPROPS);
|
||||
|
||||
SendPropDefinition arrayElementProp = null;
|
||||
|
||||
for (int i = 0; i < propertyCount; i++)
|
||||
{
|
||||
SendPropDefinition prop = new SendPropDefinition(table);
|
||||
|
||||
prop.Type = (SendPropType)stream.ReadULong(PROPINFOBITS_TYPE);
|
||||
Debug.Assert(Enum.GetValues(typeof(SendPropType)).Cast<SendPropType>().Contains(prop.Type));
|
||||
|
||||
Debug.Assert(prop.Type == SendPropType.Datatable ? prop.Flags == 0 : true);
|
||||
|
||||
prop.Name = stream.ReadCString();
|
||||
|
||||
prop.Flags = (SendPropFlags)stream.ReadULong(PROPINFOBITS_FLAGS);
|
||||
|
||||
if (prop.Type == SendPropType.Datatable)
|
||||
{
|
||||
prop.ExcludeName = stream.ReadCString();
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((prop.Flags & SendPropFlags.Exclude) != 0)
|
||||
prop.ExcludeName = stream.ReadCString();
|
||||
else if (prop.Type == SendPropType.Array)
|
||||
prop.ArrayElements = (int)stream.ReadULong(PROPINFOBITS_NUMELEMENTS);
|
||||
else
|
||||
{
|
||||
prop.LowValue = stream.ReadSingle();
|
||||
prop.HighValue = stream.ReadSingle();
|
||||
|
||||
prop.BitCount = stream.ReadULong(PROPINFOBITS_NUMBITS);
|
||||
}
|
||||
}
|
||||
|
||||
if (prop.Flags.HasFlag(SendPropFlags.NoScale))
|
||||
{
|
||||
if (prop.Type == SendPropType.Float)
|
||||
prop.BitCount = 32;
|
||||
else if (prop.Type == SendPropType.Vector)
|
||||
{
|
||||
if (!prop.Flags.HasFlag(SendPropFlags.Normal))
|
||||
prop.BitCount = 32 * 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (arrayElementProp != null)
|
||||
{
|
||||
Debug.Assert(prop.Type == SendPropType.Array);
|
||||
prop.ArrayProperty = arrayElementProp;
|
||||
arrayElementProp = null;
|
||||
}
|
||||
|
||||
if (prop.Flags.HasFlag(SendPropFlags.InsideArray))
|
||||
{
|
||||
Debug.Assert(arrayElementProp == null);
|
||||
Debug.Assert(!prop.Flags.HasFlag(SendPropFlags.ChangesOften));
|
||||
arrayElementProp = prop;
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Properties.Add(prop);
|
||||
}
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DemoUserCommand : TimestampedDemoCommand
|
||||
{
|
||||
public int OutgoingSequence { get; set; }
|
||||
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
public DemoUserCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_usercmd;
|
||||
|
||||
using (BinaryReader reader = new BinaryReader(input, Encoding.ASCII, true))
|
||||
{
|
||||
OutgoingSequence = reader.ReadInt32();
|
||||
|
||||
Data = reader.ReadBytes(reader.ReadInt32());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DemoSyncTickCommand : TimestampedDemoCommand
|
||||
{
|
||||
public DemoSyncTickCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_synctick;
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DemoStringTablesCommand : TimestampedDemoCommand
|
||||
{
|
||||
public BitStream Data { get; set; }
|
||||
|
||||
public DemoStringTablesCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_stringtables;
|
||||
|
||||
using (BinaryReader reader = new BinaryReader(input, Encoding.ASCII, true))
|
||||
{
|
||||
int dataLength = reader.ReadInt32();
|
||||
Data = new BitStream(reader.ReadBytes(dataLength));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DemoSignonCommand : DemoPacketCommand
|
||||
{
|
||||
public DemoSignonCommand(Stream input, ulong signonLength) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_signon;
|
||||
}
|
||||
}
|
||||
|
||||
class DemoPacketCommand : TimestampedDemoCommand
|
||||
{
|
||||
public DemoViewpoint Viewpoint { get; set; }
|
||||
|
||||
public int SequenceIn { get; set; }
|
||||
public int SequenceOut { get; set; }
|
||||
|
||||
public IList<INetMessage> Messages { get; set; }
|
||||
|
||||
public DemoPacketCommand(Stream input) : base(input)
|
||||
{
|
||||
Type = DemoCommandType.dem_packet;
|
||||
|
||||
using (BinaryReader r = new BinaryReader(input, Encoding.ASCII, true))
|
||||
{
|
||||
Viewpoint = new DemoViewpoint();
|
||||
|
||||
Viewpoint.ViewpointFlags = (DemoViewpoint.Flags)r.ReadInt32();
|
||||
|
||||
Viewpoint.ViewOrigin1 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
Viewpoint.ViewAngles1 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
Viewpoint.LocalViewAngles1 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
|
||||
Viewpoint.ViewOrigin2 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
Viewpoint.ViewAngles2 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
Viewpoint.LocalViewAngles2 = new Vector(r.ReadSingle(), r.ReadSingle(), r.ReadSingle());
|
||||
|
||||
SequenceIn = r.ReadInt32();
|
||||
SequenceOut = r.ReadInt32();
|
||||
|
||||
BitStream data = new BitStream(r.ReadBytes((int)r.ReadUInt32()));
|
||||
Messages = NetMessageCoder.Decode(data).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
using BitSet;
|
||||
using TF2Net.Data;
|
||||
using TF2Net.NetMessages;
|
||||
|
||||
namespace TF2Net.Extensions
|
||||
{
|
||||
public static class BitStreamExtensions
|
||||
{
|
||||
public static Vector ReadVector(this BitStream stream)
|
||||
{
|
||||
bool flagX = stream.ReadBool();
|
||||
bool flagY = stream.ReadBool();
|
||||
bool flagZ = stream.ReadBool();
|
||||
|
||||
Vector vector = new Vector();
|
||||
if (flagX)
|
||||
vector.X = BitCoord.Read(stream);
|
||||
if (flagY)
|
||||
vector.Y = BitCoord.Read(stream);
|
||||
if (flagZ)
|
||||
vector.Z = BitCoord.Read(stream);
|
||||
return vector;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue