-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSID_STARTADVEX2.cs
More file actions
136 lines (115 loc) · 5.8 KB
/
SID_STARTADVEX2.cs
File metadata and controls
136 lines (115 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Atlasd.Battlenet.Exceptions;
using Atlasd.Daemon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Atlasd.Battlenet.Protocols.Game.Messages
{
class SID_STARTADVEX2 : Message
{
public enum Statuses : UInt32
{
Failed = 0,
Success = 1,
};
public SID_STARTADVEX2()
{
Id = (byte)MessageIds.SID_STARTADVEX2;
Buffer = new byte[0];
}
public SID_STARTADVEX2(byte[] buffer)
{
Id = (byte)MessageIds.SID_STARTADVEX2;
Buffer = buffer;
}
public override bool Invoke(MessageContext context)
{
switch (context.Direction)
{
case MessageDirection.ClientToServer:
{
Logging.WriteLine(Logging.LogLevel.Debug, Logging.LogType.Client_Game, context.Client.RemoteEndPoint, $"[{Common.DirectionToString(context.Direction)}] {MessageName(Id)} ({4 + Buffer.Length} bytes)");
/**
* (UINT32) Status
* (UINT32) Unknown [editor's note: probably elapsed time]
* (UINT16) Game Type
* (UINT16) Unknown [editor's note: probably sub game type]
* (UINT32) Unknown [editor's note: probably viewing filter]
* (UINT32) Port
* (STRING) Game name
* (STRING) Game password
* (STRING) Game stats - Flags, Creator, Statstring
*/
if (Buffer.Length < 23)
throw new GameProtocolViolationException(context.Client, $"{MessageName(Id)} buffer must be at least 23 bytes");
if (context.Client.GameState.ActiveAccount == null)
throw new GameProtocolViolationException(context.Client, $"{MessageName(Id)} was received before logon");
using var m = new MemoryStream(Buffer);
using var r = new BinaryReader(m);
var gameState = r.ReadUInt32();
var gameElapsedTime = r.ReadUInt32();
var gameType = r.ReadUInt16();
var subGameType = r.ReadUInt16();
var viewingFilter = r.ReadUInt32();
var portNumber = r.ReadUInt32();
var gameName = r.ReadByteString();
var gamePassword = r.ReadByteString();
var gameStatstring = r.ReadByteString();
Statuses status = Statuses.Failed;
GameAd gameAd = null;
lock (Battlenet.Common.ActiveGameAdsLock)
{
foreach (GameAd _ad in Battlenet.Common.ActiveGameAds)
{
if (_ad.Name.SequenceEqual(gameName))
{
gameAd = _ad;
break;
}
}
if (gameAd == null)
{
gameAd = new GameAd(context.Client.GameState, gameName, gamePassword, gameStatstring, 6112, (GameAd.GameTypes)gameType, subGameType, context.Client.GameState.Version.VersionByte);
gameAd.AddClient(context.Client.GameState);
Battlenet.Common.ActiveGameAds.Add(gameAd);
}
}
if (gameAd.HasClient(context.Client.GameState)) context.Client.GameState.GameAd = gameAd;
bool gameAdOwner = gameAd != null && gameAd.Owner == context.Client.GameState;
if (!gameAdOwner)
{
status = Statuses.Failed;
}
else
{
status = Statuses.Success;
gameAd.SetActiveStateFlags((GameAd.StateFlags)gameState);
gameAd.SetElapsedTime(gameElapsedTime);
gameAd.SetGameType((GameAd.GameTypes)gameType);
gameAd.SetName(gameName);
gameAd.SetPassword(gamePassword);
gameAd.SetPort(6112);
gameAd.SetStatstring(gameStatstring);
}
return new SID_STARTADVEX2().Invoke(new MessageContext(context.Client, MessageDirection.ServerToClient, new Dictionary<string, dynamic>(){{ "status", status }}));
}
case MessageDirection.ServerToClient:
{
var status = context.Arguments.ContainsKey("status") ? (Statuses)context.Arguments["status"] : Statuses.Failed;
/**
* (UINT32) Status
*/
Buffer = new byte[4];
using var m = new MemoryStream(Buffer);
using var w = new BinaryWriter(m);
w.Write((UInt32)status);
Logging.WriteLine(Logging.LogLevel.Debug, Logging.LogType.Client_Game, context.Client.RemoteEndPoint, $"[{Common.DirectionToString(context.Direction)}] {MessageName(Id)} ({4 + Buffer.Length} bytes)");
context.Client.Send(ToByteArray(context.Client.ProtocolType));
return true;
}
}
return false;
}
}
}