Skip to content

Commit

Permalink
Migrating codebase to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
iVerb1 committed Oct 15, 2016
0 parents commit b44c795
Show file tree
Hide file tree
Showing 85 changed files with 67,122 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Demo/DemoParser.cs
@@ -0,0 +1,84 @@
using System;
using System.IO;
using System.Text;

namespace SourceLiveTimer.Demo
{
public class DemoParser
{
public static DemoParseResult ParseDemo(string file)
{
byte num;
DemoParseResult result;
using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
if (Encoding.ASCII.GetString(binaryReader.ReadBytes(8)).TrimEnd(new char[1]) != "HL2DEMO")
{
throw new Exception("Not a demo");
}
DemoProtocolVersion demoProtocolVersion = (DemoProtocolVersion)binaryReader.ReadInt32();
if ((int)demoProtocolVersion == 2)
{
demoProtocolVersion = DemoProtocolVersion.HL2;
}
if (!Enum.IsDefined(typeof(DemoProtocolVersion), demoProtocolVersion))
{
throw new Exception(string.Concat("Unknown demo protocol version: 0x", demoProtocolVersion.ToString("x")));
}
int num1 = binaryReader.ReadInt32();
binaryReader.BaseStream.Seek((long)260, SeekOrigin.Current);
string str = Encoding.ASCII.GetString(binaryReader.ReadBytes(260)).TrimEnd(new char[1]);
string str1 = Encoding.ASCII.GetString(binaryReader.ReadBytes(260)).TrimEnd(new char[1]);
string str2 = Encoding.ASCII.GetString(binaryReader.ReadBytes(260)).TrimEnd(new char[1]);
binaryReader.BaseStream.Seek((long)4, SeekOrigin.Current);
binaryReader.BaseStream.Seek((long)4, SeekOrigin.Current);
binaryReader.BaseStream.Seek((long)4, SeekOrigin.Current);
int num2 = binaryReader.ReadInt32();
GameHandler hL2GameHandler = null;
try
{
hL2GameHandler = GameHandler.getGameHandler(str2, str1);
}
catch (Exception)
{
if (demoProtocolVersion == DemoProtocolVersion.HL2)
{
hL2GameHandler = new HL2GameHandler();
}
else if (demoProtocolVersion == DemoProtocolVersion.ORANGEBOX)
{
hL2GameHandler = new OrangeBoxGameHandler();
}
}

hL2GameHandler.FileName = file;
hL2GameHandler.Map = str1;
hL2GameHandler.GameDir = str2;
hL2GameHandler.PlayerName = str;
hL2GameHandler.SignOnLen = num2;
hL2GameHandler.NetworkProtocol = num1;

do
{
num = binaryReader.ReadByte();
if (hL2GameHandler.IsStop(num))
{
break;
}
int num3 = binaryReader.ReadInt32();
if (hL2GameHandler.DemoVersion >= DemoProtocolVersion.ORANGEBOX)
{
binaryReader.ReadByte();
}
hL2GameHandler.HandleCommand(num, num3, binaryReader);
}
while (!hL2GameHandler.IsStop(num));
result = hL2GameHandler.GetResult();
}
}
return result;
}
}
}
10 changes: 10 additions & 0 deletions Demo/DemoProtocolVersion.cs
@@ -0,0 +1,10 @@
using System;

namespace SourceLiveTimer.Demo
{
public enum DemoProtocolVersion
{
HL2 = 3,
ORANGEBOX = 4
}
}
134 changes: 134 additions & 0 deletions Demo/GameHandler/GameHandler.cs
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using SourceLiveTimer.Speedrun;

namespace SourceLiveTimer.Demo
{
public abstract class GameHandler
{
protected int CurrentTick
{
get;
set;
}

public abstract DemoProtocolVersion DemoVersion
{
get;
protected set;
}

public string FileName
{
get;
set;
}

public string GameDir
{
get;
set;
}

public string Map
{
get;
set;
}

protected string MapEndAdjustType
{
get;
private set;
}

protected List<string> Maps
{
get;
private set;
}

protected string MapStartAdjustType
{
get;
private set;
}

public int NetworkProtocol
{
get;
set;
}

public string PlayerName
{
get;
set;
}

public int SignOnLen
{
get;
set;
}

public GameHandler()
{
this.MapStartAdjustType = "Map Start";
this.MapEndAdjustType = "Map End";
this.Maps = new List<string>();
}

public static GameHandler getGameHandler(string gameDir, string map)
{
if (gameDir == "portal")
{
return new PortalGameHandler();
}
if (gameDir == "portal2") {

if (Category.PORTAL_2_SP.Maps.Contains(map))
{
return new Portal2SpGameHandler();
}
if (Category.PORTAL_2_COOP.Maps.Contains(map))
{
return new Portal2CoopGameHandler();
}
if (Category.PORTAL_2_COOP_COURSE_6.Maps.Contains(map))
{
return new Portal2CoopCourse6GameHandler();
}
}
throw new Exception("Unknown game");
}

public abstract DemoParseResult GetResult();

public abstract long HandleCommand(byte command, int tick, BinaryReader br);

public abstract bool IsStop(byte command);

protected abstract ConsoleCmdResult ProcessConsoleCmd(BinaryReader br);

protected abstract long ProcessCustomData(BinaryReader br);

protected abstract PacketResult ProcessPacket(BinaryReader br);

protected int ProcessSignOn(BinaryReader br)
{
br.BaseStream.Seek((long)this.SignOnLen, SeekOrigin.Current);
return this.SignOnLen;
}

protected abstract long ProcessStringTables(BinaryReader br);

protected abstract long ProcessUserCmd(BinaryReader br);
}

}
153 changes: 153 additions & 0 deletions Demo/GameHandler/HL2GameHandler.cs
@@ -0,0 +1,153 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using SourceLiveTimer.Util;

namespace SourceLiveTimer.Demo
{
internal class HL2GameHandler : GameHandler
{
public override DemoProtocolVersion DemoVersion
{
get;
protected set;
}

public HL2GameHandler()
{
this.DemoVersion = DemoProtocolVersion.HL2;
}

public override DemoParseResult GetResult()
{
DemoParseResult demoParseResult = new DemoParseResult()
{
FileName = base.FileName,
MapName = base.Map,
PlayerName = base.PlayerName,
GameDir = base.GameDir,
TotalTicks = base.CurrentTick,
StartAdjustmentType = base.MapStartAdjustType,
EndAdjustmentType = base.MapEndAdjustType
};
return demoParseResult;
}

public override long HandleCommand(byte command, int tick, BinaryReader br)
{
if (tick < 0 || base.CurrentTick == 0 && tick > 66)
{
tick = 0;
}
if (tick > base.CurrentTick)
{
base.CurrentTick = tick;
}
Enum.IsDefined(typeof(HL2GameHandler.HL2DemoCommands), (HL2GameHandler.HL2DemoCommands)command);
if (command == 1)
{
return (long)base.ProcessSignOn(br);
}
if (command == 2)
{
return this.ProcessPacket(br).Read;
}
if (command == 3)
{
return (long)0;
}
if (command == 4)
{
return this.ProcessConsoleCmd(br).Read;
}
if (command == 5)
{
return this.ProcessUserCmd(br);
}
if (command == 6)
{
throw new NotImplementedException();
}
if (command != 8)
{
throw new Exception(string.Concat("Unknown command: 0x", command.ToString("x")));
}
return this.ProcessStringTables(br);
}

public override bool IsStop(byte command)
{
if (command == 7)
{
return true;
}
return false;
}

protected override ConsoleCmdResult ProcessConsoleCmd(BinaryReader br)
{
long position = br.BaseStream.Position;
int num = br.ReadInt32();
string str = Encoding.ASCII.GetString(br.ReadBytes(num)).TrimEnd(new char[1]);
ConsoleCmdResult consoleCmdResult = new ConsoleCmdResult()
{
Read = br.BaseStream.Position - position,
Command = str
};
return consoleCmdResult;
}

protected override long ProcessCustomData(BinaryReader br)
{
throw new NotImplementedException();
}

protected override PacketResult ProcessPacket(BinaryReader br)
{
long position = br.BaseStream.Position;
br.BaseStream.Seek((long)4, SeekOrigin.Current);
float single = br.ReadSingle();
float single1 = br.ReadSingle();
float single2 = br.ReadSingle();
br.BaseStream.Seek((long)68, SeekOrigin.Current);
int num = br.ReadInt32();
br.BaseStream.Seek((long)num, SeekOrigin.Current);
PacketResult packetResult = new PacketResult()
{
Read = br.BaseStream.Position - position,
CurrentPosition = new Vector(single, single1, single2)
};
return packetResult;
}

protected override long ProcessStringTables(BinaryReader br)
{
long position = br.BaseStream.Position;
int num = br.ReadInt32();
br.BaseStream.Seek((long)num, SeekOrigin.Current);
return br.BaseStream.Position - position;
}

protected override long ProcessUserCmd(BinaryReader br)
{
long position = br.BaseStream.Position;
br.BaseStream.Seek((long)4, SeekOrigin.Current);
int num = br.ReadInt32();
br.BaseStream.Seek((long)num, SeekOrigin.Current);
return br.BaseStream.Position - position;
}

protected enum HL2DemoCommands
{
SignOn = 1,
Packet = 2,
SyncTick = 3,
ConsoleCmd = 4,
UserCmd = 5,
DataTables = 6,
Stop = 7,
StringTables = 8
}
}
}

0 comments on commit b44c795

Please sign in to comment.