Skip to content

Commit

Permalink
Update protos and implement chat channel list request
Browse files Browse the repository at this point in the history
  • Loading branch information
paralin committed Sep 25, 2015
1 parent 5adc155 commit 929eaee
Show file tree
Hide file tree
Showing 18 changed files with 1,134 additions and 315 deletions.
124 changes: 65 additions & 59 deletions Dota2/Backports/KeyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace Dota2.Backports
{
class KVTextReader : StreamReader
internal class KVTextReader : StreamReader
{
static Dictionary<char, char> escapedMapping = new Dictionary<char, char>
private static Dictionary<char, char> escapedMapping = new Dictionary<char, char>
{
{ 'n', '\n' },
{ 'r', '\r' },
{ 't', '\t' },
{'n', '\n'},
{'r', '\r'},
{'t', '\t'},
// todo: any others?
};

Expand Down Expand Up @@ -70,15 +70,14 @@ public KVTextReader(KeyValue kv, Stream input)
}

currentKey = null;
}
while (!EndOfStream);
} while (!EndOfStream);
}

private void EatWhiteSpace()
{
while (!EndOfStream)
{
if (!Char.IsWhiteSpace((char)Peek()))
if (!Char.IsWhiteSpace((char) Peek()))
{
break;
}
Expand All @@ -91,7 +90,7 @@ private bool EatCPPComment()
{
if (!EndOfStream)
{
char next = (char)Peek();
char next = (char) Peek();
if (next == '/')
{
Read();
Expand Down Expand Up @@ -135,7 +134,7 @@ public string ReadToken(out bool wasQuoted, out bool wasConditional)
if (EndOfStream)
return null;

char next = (char)Peek();
char next = (char) Peek();
if (next == '"')
{
wasQuoted = true;
Expand All @@ -150,7 +149,7 @@ public string ReadToken(out bool wasQuoted, out bool wasConditional)
{
Read();

char escapedChar = (char)Read();
char escapedChar = (char) Read();
char replacedChar;

if (escapedMapping.TryGetValue(escapedChar, out replacedChar))
Expand All @@ -164,7 +163,7 @@ public string ReadToken(out bool wasQuoted, out bool wasConditional)
if (Peek() == '"')
break;

sb.Append((char)Read());
sb.Append((char) Read());
}

// "
Expand All @@ -184,7 +183,7 @@ public string ReadToken(out bool wasQuoted, out bool wasConditional)
var ret = new StringBuilder();
while (!EndOfStream)
{
next = (char)Peek();
next = (char) Peek();

if (next == '"' || next == '{' || next == '}')
break;
Expand Down Expand Up @@ -219,7 +218,7 @@ public string ReadToken(out bool wasQuoted, out bool wasConditional)
/// </summary>
public class KeyValue
{
enum Type : byte
private enum Type : byte
{
None = 0,
String = 1,
Expand Down Expand Up @@ -248,12 +247,13 @@ public KeyValue(string name = null, string value = null)
/// <summary>
/// Represents an invalid <see cref="KeyValue"/> given when a searched for child does not exist.
/// </summary>
public readonly static KeyValue Invalid = new KeyValue();
public static readonly KeyValue Invalid = new KeyValue();

/// <summary>
/// Gets or sets the name of this instance.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets the value of this instance.
/// </summary>
Expand Down Expand Up @@ -345,6 +345,7 @@ public string AsString()

return value;
}

/// <summary>
/// Attempts to convert and return the value of this instance as an unsigned long.
/// If the conversion is invalid, the default value is returned.
Expand Down Expand Up @@ -447,7 +448,9 @@ public static KeyValue LoadAsText(string path)
/// </summary>
/// <param name="path">The path to the file to load.</param>
/// <returns>a <see cref="KeyValue"/> instance if the load was successful, or <c>null</c> on failure.</returns>
[Obsolete("Use TryReadAsBinary instead. Note that TryLoadAsBinary returns the root object, not a dummy parent node containg the root object.")]
[Obsolete(
"Use TryReadAsBinary instead. Note that TryLoadAsBinary returns the root object, not a dummy parent node containg the root object."
)]
public static KeyValue LoadAsBinary(string path)
{
var kv = LoadFromFile(path, true);
Expand All @@ -474,7 +477,7 @@ public static bool TryLoadAsBinary(string path, out KeyValue keyValue)
}


static KeyValue LoadFromFile(string path, bool asBinary)
private static KeyValue LoadFromFile(string path, bool asBinary)
{
if (File.Exists(path) == false)
{
Expand Down Expand Up @@ -586,7 +589,7 @@ internal void RecursiveLoadFromBuffer(KVTextReader kvr)
throw new Exception("RecursiveLoadFromBuffer: got EOF or empty keyname");
}

if (name.StartsWith("}") && !wasQuoted) // top level closed, stop reading
if (name.StartsWith("}") && !wasQuoted) // top level closed, stop reading
break;

KeyValue dat = new KeyValue(name);
Expand Down Expand Up @@ -655,30 +658,30 @@ public void SaveToStream(Stream stream, bool asBinary)
}
}

void RecursiveSaveBinaryToStream(Stream f)
private void RecursiveSaveBinaryToStream(Stream f)
{
RecursiveSaveBinaryToStreamCore(f);
f.WriteByte((byte)Type.End);
f.WriteByte((byte) Type.End);
}

void RecursiveSaveBinaryToStreamCore(Stream f)
private void RecursiveSaveBinaryToStreamCore(Stream f)
{
// Only supported types ATM:
// 1. KeyValue with children (no value itself)
// 2. String KeyValue
if (Children.Any())
{
f.WriteByte((byte)Type.None);
f.WriteByte((byte) Type.None);
f.WriteNullTermString(Name, Encoding.UTF8);
foreach (var child in Children)
{
child.RecursiveSaveBinaryToStreamCore(f);
}
f.WriteByte((byte)Type.End);
f.WriteByte((byte) Type.End);
}
else
{
f.WriteByte((byte)Type.String);
f.WriteByte((byte) Type.String);
f.WriteNullTermString(Name, Encoding.UTF8);
f.WriteNullTermString(Value ?? string.Empty, Encoding.UTF8);
}
Expand Down Expand Up @@ -714,12 +717,12 @@ private void RecursiveSaveTextToFile(Stream stream, int indentLevel = 0)
WriteString(stream, "}\n");
}

void WriteIndents(Stream stream, int indentLevel)
private void WriteIndents(Stream stream, int indentLevel)
{
WriteString(stream, new string('\t', indentLevel));
}

static void WriteString(Stream stream, string str, bool quote = false)
private static void WriteString(Stream stream, string str, bool quote = false)
{
byte[] bytes = Encoding.UTF8.GetBytes((quote ? "\"" : "") + str.Replace("\"", "\\\"") + (quote ? "\"" : ""));
stream.Write(bytes, 0, bytes.Length);
Expand All @@ -730,7 +733,9 @@ static void WriteString(Stream stream, string str, bool quote = false)
/// </summary>
/// <param name="input">The input <see cref="Stream"/> to read from.</param>
/// <returns><c>true</c> if the read was successful; otherwise, <c>false</c>.</returns>
[Obsolete("Use TryReadAsBinary instead. Note that TryReadAsBinary returns the root object, not a dummy parent node containg the root object.")]
[Obsolete(
"Use TryReadAsBinary instead. Note that TryReadAsBinary returns the root object, not a dummy parent node containg the root object."
)]
public bool ReadAsBinary(Stream input)
{
var dummyChild = new KeyValue();
Expand All @@ -748,13 +753,13 @@ public bool TryReadAsBinary(Stream input)
return TryReadAsBinaryCore(input, this, null);
}

static bool TryReadAsBinaryCore(Stream input, KeyValue current, KeyValue parent)
private static bool TryReadAsBinaryCore(Stream input, KeyValue current, KeyValue parent)
{
current.Children = new List<KeyValue>();

while (true)
{
var type = (Type)input.ReadByte();
var type = (Type) input.ReadByte();

if (type == Type.End)
{
Expand All @@ -766,52 +771,53 @@ static bool TryReadAsBinaryCore(Stream input, KeyValue current, KeyValue parent)
switch (type)
{
case Type.None:
{
var child = new KeyValue();
var didReadChild = TryReadAsBinaryCore(input, child, current);
if (!didReadChild)
{
var child = new KeyValue();
var didReadChild = TryReadAsBinaryCore(input, child, current);
if (!didReadChild)
{
return false;
}
break;
return false;
}
break;
}

case Type.String:
{
current.Value = input.ReadNullTermString(Encoding.UTF8);
break;
}
{
current.Value = input.ReadNullTermString(Encoding.UTF8);
break;
}

case Type.WideString:
{
DebugLog.WriteLine("KeyValue", "Encountered WideString type when parsing binary KeyValue, which is unsupported. Returning false.");
return false;
}
{
DebugLog.WriteLine("KeyValue",
"Encountered WideString type when parsing binary KeyValue, which is unsupported. Returning false.");
return false;
}

case Type.Int32:
case Type.Color:
case Type.Pointer:
{
current.Value = Convert.ToString(input.ReadInt32());
break;
}
{
current.Value = Convert.ToString(input.ReadInt32());
break;
}

case Type.UInt64:
{
current.Value = Convert.ToString(input.ReadUInt64());
break;
}
{
current.Value = Convert.ToString(input.ReadUInt64());
break;
}

case Type.Float32:
{
current.Value = Convert.ToString(input.ReadFloat());
break;
}
{
current.Value = Convert.ToString(input.ReadFloat());
break;
}

default:
{
return false;
}
{
return false;
}
}

if (parent != null)
Expand Down
3 changes: 1 addition & 2 deletions Dota2/Base/Data/CacheTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ internal enum CSOTypes : int
PARTYINVITE = 2006,
LOBBYINVITE = 2011
}

}
}
2 changes: 1 addition & 1 deletion Dota2/Base/Data/Games.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public enum Games : uint
DOTA2 = 570,
DOTA2TEST = 205790
}
}
}
2 changes: 1 addition & 1 deletion Dota2/Base/Data/LobbyExtraData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public class LobbyExtraData
/// </summary>
public CMsgLeagueAdminList LeagueAdminList { get; protected internal set; }
}
}
}
Loading

0 comments on commit 929eaee

Please sign in to comment.