Skip to content

Commit

Permalink
Merge pull request WowLegacyCore#32 from ratkosrb/cypher_proxy
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
ratkosrb authored May 27, 2022
2 parents 77edf93 + 69c259c commit 986dcf6
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 73 deletions.
102 changes: 100 additions & 2 deletions HermesProxy/VersionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,56 @@ static LegacyVersion()
UpdateFieldDictionary = new Dictionary<Type, SortedList<int, UpdateFieldInfo>>();
UpdateFieldNameDictionary = new Dictionary<Type, Dictionary<string, int>>();
if (!LoadUFDictionariesInto(UpdateFieldDictionary, UpdateFieldNameDictionary))
Log.Print(LogType.Error, "Could not load update fields for current server version.");
Log.Print(LogType.Error, "Could not load update fields for current legacy version.");
if (!LoadOpcodeDictionaries())
Log.Print(LogType.Error, "Could not load opcodes for current legacy version.");
}

private static readonly Dictionary<uint, Opcode> CurrentToUniversalOpcodeDictionary = new();
private static readonly Dictionary<Opcode, uint> UniversalToCurrentOpcodeDictionary = new();

private static bool LoadOpcodeDictionaries()
{
Type enumType = Opcodes.GetOpcodesEnumForVersion(Build);
if (enumType == null)
return false;

foreach (var item in Enum.GetValues(enumType))
{
string oldOpcodeName = Enum.GetName(enumType, item);
Opcode universalOpcode = Opcodes.GetUniversalOpcode(oldOpcodeName);
if (universalOpcode == Opcode.MSG_NULL_ACTION &&
oldOpcodeName != "MSG_NULL_ACTION")
{
Log.Print(LogType.Error, $"Opcode {oldOpcodeName} is missing from the universal opcode enum!");
continue;
}

CurrentToUniversalOpcodeDictionary.Add((uint)item, universalOpcode);
UniversalToCurrentOpcodeDictionary.Add(universalOpcode, (uint)item);
}

if (CurrentToUniversalOpcodeDictionary.Count < 1)
return false;

Log.Print(LogType.Server, $"Loaded {CurrentToUniversalOpcodeDictionary.Count} legacy opcodes.");
return true;
}

public static Opcode GetUniversalOpcode(uint opcode)
{
Opcode universalOpcode;
if (CurrentToUniversalOpcodeDictionary.TryGetValue(opcode, out universalOpcode))
return universalOpcode;
return Opcode.MSG_NULL_ACTION;
}

public static uint GetCurrentOpcode(Opcode universalOpcode)
{
uint opcode;
if (UniversalToCurrentOpcodeDictionary.TryGetValue(universalOpcode, out opcode))
return opcode;
return 0;
}

private static readonly Dictionary<Type, SortedList<int, UpdateFieldInfo>> UpdateFieldDictionary;
Expand Down Expand Up @@ -284,7 +333,56 @@ static ModernVersion()
UpdateFieldDictionary = new Dictionary<Type, SortedList<int, UpdateFieldInfo>>();
UpdateFieldNameDictionary = new Dictionary<Type, Dictionary<string, int>>();
if (!LoadUFDictionariesInto(UpdateFieldDictionary, UpdateFieldNameDictionary))
Log.Print(LogType.Error, "Could not load update fields for current server version.");
Log.Print(LogType.Error, "Could not load update fields for current modern version.");
if (!LoadOpcodeDictionaries())
Log.Print(LogType.Error, "Could not load opcodes for current modern version.");
}

private static readonly Dictionary<uint, Opcode> CurrentToUniversalOpcodeDictionary = new();
private static readonly Dictionary<Opcode, uint> UniversalToCurrentOpcodeDictionary = new();

private static bool LoadOpcodeDictionaries()
{
Type enumType = Opcodes.GetOpcodesEnumForVersion(Build);
if (enumType == null)
return false;

foreach (var item in Enum.GetValues(enumType))
{
string oldOpcodeName = Enum.GetName(enumType, item);
Opcode universalOpcode = Opcodes.GetUniversalOpcode(oldOpcodeName);
if (universalOpcode == Opcode.MSG_NULL_ACTION &&
oldOpcodeName != "MSG_NULL_ACTION")
{
Log.Print(LogType.Error, $"Opcode {oldOpcodeName} is missing from the universal opcode enum!");
continue;
}

CurrentToUniversalOpcodeDictionary.Add((uint)item, universalOpcode);
UniversalToCurrentOpcodeDictionary.Add(universalOpcode, (uint)item);
}

if (CurrentToUniversalOpcodeDictionary.Count < 1)
return false;

Log.Print(LogType.Server, $"Loaded {CurrentToUniversalOpcodeDictionary.Count} modern opcodes.");
return true;
}

public static Opcode GetUniversalOpcode(uint opcode)
{
Opcode universalOpcode;
if (CurrentToUniversalOpcodeDictionary.TryGetValue(opcode, out universalOpcode))
return universalOpcode;
return Opcode.MSG_NULL_ACTION;
}

public static uint GetCurrentOpcode(Opcode universalOpcode)
{
uint opcode;
if (UniversalToCurrentOpcodeDictionary.TryGetValue(universalOpcode, out opcode))
return opcode;
return 0;
}

private static readonly Dictionary<Type, SortedList<int, UpdateFieldInfo>> UpdateFieldDictionary;
Expand Down
2 changes: 1 addition & 1 deletion HermesProxy/World/Client/PacketHandlers/MovementHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void HandleMonsterMove(WorldPacket packet)

bool isTaxiFlight = (hasTaxiFlightFlags &&
(GetSession().GameState.IsWaitingForTaxiStart ||
GetSession().GameState.CurrentPlayerCreateTime == packet.GetReceivedTime()) &&
Math.Abs(packet.GetReceivedTime() - GetSession().GameState.CurrentPlayerCreateTime) <= 1000) &&
GetSession().GameState.CurrentPlayerGuid == guid);

if (isTaxiFlight)
Expand Down
15 changes: 9 additions & 6 deletions HermesProxy/World/Client/PacketHandlers/UpdateHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Framework.GameMath;
//#define DEBUG_UPDATES

using Framework.GameMath;
using Framework.Logging;
using Framework.Util;
using HermesProxy.Enums;
Expand Down Expand Up @@ -344,17 +346,18 @@ private string GetIndexString(params object[] values)
});
}

private const bool DebugUpdates = false;
private void PrintString(string txt, params object[] indexes)
{
if (DebugUpdates)
Console.WriteLine("{0}{1}", GetIndexString(indexes), txt);
#if DEBUG_UPDATES
Console.WriteLine("{0}{1}", GetIndexString(indexes), txt);
#endif
}

private T PrintValue<T>(string name, T obj, params object[] indexes)
{
if (DebugUpdates)
Console.WriteLine("{0}{1}: {2}", GetIndexString(indexes), name, obj);
#if DEBUG_UPDATES
Console.WriteLine("{0}{1}: {2}", GetIndexString(indexes), name, obj);
#endif
return obj;
}

Expand Down
4 changes: 2 additions & 2 deletions HermesProxy/World/Client/WorldClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private void ReceiveCallback(IAsyncResult AR)
}

WorldPacket packet = new WorldPacket(buffer);
packet.SetReceiveTime(Time.UnixTime);
packet.SetReceiveTime(Environment.TickCount);
HandlePacket(packet);
}
else
Expand Down Expand Up @@ -241,7 +241,7 @@ private void SendPacket(WorldPacket packet)
header.Opcode = packet.GetOpcode();
header.Write(buffer);

Log.Print(LogType.Debug, $"Sending opcode {Opcodes.GetOpcodeNameForVersion(header.Opcode, Settings.ServerBuild)} ({header.Opcode}) with size {header.Size}.");
Log.Print(LogType.Debug, $"Sending opcode {LegacyVersion.GetUniversalOpcode(header.Opcode)} ({header.Opcode}) with size {header.Size}.");

byte[] headerArray = buffer.GetData();
if (_worldCrypt != null)
Expand Down
61 changes: 25 additions & 36 deletions HermesProxy/World/Enums/Opcodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,41 @@ public static ClientVersionBuild GetOpcodesDefiningBuild(ClientVersionBuild vers
}
return ClientVersionBuild.Zero;
}
public static uint GetOpcodeValueForVersion(Opcode opcode, ClientVersionBuild version)

public static Type GetOpcodesEnumForVersion(ClientVersionBuild version)
{
switch (GetOpcodesDefiningBuild(version))
{
case ClientVersionBuild.V1_12_1_5875:
return FindOpcodeValueInEnum<V1_12_1_5875.Opcode>(opcode.ToString());
return typeof(V1_12_1_5875.Opcode);
case ClientVersionBuild.V2_4_3_8606:
return FindOpcodeValueInEnum<V2_4_3_8606.Opcode>(opcode.ToString());
return typeof(V2_4_3_8606.Opcode);
case ClientVersionBuild.V3_3_5a_12340:
return FindOpcodeValueInEnum<V3_3_5_12340.Opcode>(opcode.ToString());
return typeof(V3_3_5_12340.Opcode);
case ClientVersionBuild.V2_5_2_39570:
return FindOpcodeValueInEnum<V2_5_2_39570.Opcode>(opcode.ToString());
return typeof(V2_5_2_39570.Opcode);
}
return 0;
return null;
}

public static uint GetOpcodeValueForVersion(string opcode, ClientVersionBuild version)
public static uint GetOpcodeValueForVersion(Opcode opcode, ClientVersionBuild version)
{
switch (GetOpcodesDefiningBuild(version))
{
case ClientVersionBuild.V1_12_1_5875:
return FindOpcodeValueInEnum<V1_12_1_5875.Opcode>(opcode);
case ClientVersionBuild.V2_4_3_8606:
return FindOpcodeValueInEnum<V2_4_3_8606.Opcode>(opcode);
case ClientVersionBuild.V3_3_5a_12340:
return FindOpcodeValueInEnum<V3_3_5_12340.Opcode>(opcode);
case ClientVersionBuild.V2_5_2_39570:
return FindOpcodeValueInEnum<V2_5_2_39570.Opcode>(opcode);
}
return GetOpcodeValueForVersion(opcode.ToString(), version);
}

public static uint GetOpcodeValueForVersion(string opcodeName, ClientVersionBuild version)
{
object opcode;
if (Enum.TryParse(GetOpcodesEnumForVersion(version), opcodeName, out opcode))
return (uint)opcode;

return 0;
}

public static string GetOpcodeNameForVersion(uint opcode, ClientVersionBuild version)
{
switch (GetOpcodesDefiningBuild(version))
{
case ClientVersionBuild.V1_12_1_5875:
return FindOpcodeNameInEnum<V1_12_1_5875.Opcode>(opcode);
case ClientVersionBuild.V2_4_3_8606:
return FindOpcodeNameInEnum<V2_4_3_8606.Opcode>(opcode);
case ClientVersionBuild.V3_3_5a_12340:
return FindOpcodeNameInEnum<V3_3_5_12340.Opcode>(opcode);
case ClientVersionBuild.V2_5_2_39570:
return FindOpcodeNameInEnum<V2_5_2_39570.Opcode>(opcode);
}
return "UNKNOWN";
Type enumType = GetOpcodesEnumForVersion(version);
return Enum.ToObject(enumType, opcode).ToString();
}

public static Opcode GetUniversalOpcode(uint opcode, ClientVersionBuild version)
Expand All @@ -100,11 +89,10 @@ public static Opcode GetUniversalOpcode(uint opcode, ClientVersionBuild version)

public static Opcode GetUniversalOpcode(string name)
{
foreach (var item in Enum.GetValues(typeof(Opcode)))
{
if (Enum.GetName(typeof(Opcode), item) == name)
return (Opcode)item;
}
object opcode;
if (Enum.TryParse(typeof(Opcode), name, out opcode))
return (Opcode)opcode;

return Opcode.MSG_NULL_ACTION;
}

Expand All @@ -129,7 +117,7 @@ private static string FindOpcodeNameInEnum<T>(uint value) where T : System.Enum
}
}

public enum Opcode
public enum Opcode : uint
{
/* Generic opcode enumeration
* Every opcode _name_ should be here (any version)
Expand Down Expand Up @@ -3235,6 +3223,7 @@ public enum Opcode
SMSG_UNKNOWN_8,
SMSG_UNLEARNED_SPELLS,
SMSG_UNLOAD_CHILD_MAP,
SMSG_UPDATE_AADC_STATUS_RESPONSE,
SMSG_UPDATE_ACCOUNT_DATA,
SMSG_UPDATE_ACCOUNT_DATA_COMPLETE,
SMSG_UPDATE_ACTION_BUTTONS,
Expand Down
15 changes: 9 additions & 6 deletions HermesProxy/World/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Dispose()
public uint GetOpcode() { return _worldPacket.GetOpcode(); }
public Opcode GetUniversalOpcode()
{
return Opcodes.GetUniversalOpcode(GetOpcode(), Framework.Settings.ClientBuild);
return ModernVersion.GetUniversalOpcode(GetOpcode());
}

public void LogPacket(ref SniffFile sniffFile)
Expand All @@ -67,7 +67,7 @@ protected ServerPacket(Opcode universalOpcode)
{
connectionType = ConnectionType.Realm;

uint opcode = Opcodes.GetOpcodeValueForVersion(universalOpcode, Framework.Settings.ClientBuild);
uint opcode = ModernVersion.GetCurrentOpcode(universalOpcode);
System.Diagnostics.Trace.Assert(opcode != 0);
_worldPacket = new WorldPacket(opcode);
}
Expand All @@ -76,7 +76,7 @@ protected ServerPacket(Opcode universalOpcode, ConnectionType type = ConnectionT
{
connectionType = type;

uint opcode = Opcodes.GetOpcodeValueForVersion(universalOpcode, Framework.Settings.ClientBuild);
uint opcode = ModernVersion.GetCurrentOpcode(universalOpcode);
System.Diagnostics.Trace.Assert(opcode != 0);
_worldPacket = new WorldPacket(opcode);
}
Expand All @@ -93,7 +93,7 @@ public uint GetOpcode()
}
public Opcode GetUniversalOpcode()
{
return Opcodes.GetUniversalOpcode(GetOpcode(), Framework.Settings.ClientBuild);
return ModernVersion.GetUniversalOpcode(GetOpcode());
}

public byte[] GetData()
Expand Down Expand Up @@ -143,7 +143,7 @@ public WorldPacket(uint opcode = 0)

public WorldPacket(Opcode opcode)
{
this.opcode = Opcodes.GetOpcodeValueForVersion(opcode, Framework.Settings.ServerBuild);
this.opcode = LegacyVersion.GetCurrentOpcode(opcode);
System.Diagnostics.Trace.Assert(this.opcode != 0);
}

Expand Down Expand Up @@ -305,7 +305,10 @@ public void WriteBytes(WorldPacket data)
public uint GetOpcode() { return opcode; }
public Opcode GetUniversalOpcode(bool isModern)
{
return Opcodes.GetUniversalOpcode(GetOpcode(), isModern ? Framework.Settings.ClientBuild : Framework.Settings.ServerBuild);
if (isModern)
return ModernVersion.GetUniversalOpcode(GetOpcode());
else
return LegacyVersion.GetUniversalOpcode(GetOpcode());
}

public long GetReceivedTime() { return m_receivedTime; }
Expand Down
Loading

0 comments on commit 986dcf6

Please sign in to comment.