Skip to content

Commit

Permalink
Merge pull request #6 from ArchipelagoMW/development
Browse files Browse the repository at this point in the history
Update for AP 0.1.5
  • Loading branch information
Ijwu authored Aug 4, 2021
2 parents ea27400 + 7ace788 commit c8f5917
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Archipelago.MultiClient.Net.Converters
{
public class ArchipelagoPacketConverter : JsonConverter
{
public static Dictionary<ArchipelagoPacketType, Func<JObject, ArchipelagoPacketBase>> PacketDeserializationMap = new Dictionary<ArchipelagoPacketType, Func<JObject, ArchipelagoPacketBase>>()
{
[ArchipelagoPacketType.RoomInfo] = (JObject obj) => obj.ToObject<RoomInfoPacket>(),
[ArchipelagoPacketType.ConnectionRefused] = (JObject obj) => obj.ToObject<ConnectionRefusedPacket>(),
[ArchipelagoPacketType.Connected] = (JObject obj) => obj.ToObject<ConnectedPacket>(),
[ArchipelagoPacketType.ReceivedItems] = (JObject obj) => obj.ToObject<ReceivedItemsPacket>(),
[ArchipelagoPacketType.LocationInfo] = (JObject obj) => obj.ToObject<LocationInfoPacket>(),
[ArchipelagoPacketType.RoomUpdate] = (JObject obj) => obj.ToObject<RoomUpdatePacket>(),
[ArchipelagoPacketType.Print] = (JObject obj) => obj.ToObject<PrintPacket>(),
[ArchipelagoPacketType.PrintJSON] = (JObject obj) => obj.ToObject<PrintJsonPacket>(),
[ArchipelagoPacketType.Connect] = (JObject obj) => obj.ToObject<ConnectPacket>(),
[ArchipelagoPacketType.LocationChecks] = (JObject obj) => obj.ToObject<LocationChecksPacket>(),
[ArchipelagoPacketType.LocationScouts] = (JObject obj) => obj.ToObject<LocationScoutsPacket>(),
[ArchipelagoPacketType.StatusUpdate] = (JObject obj) => obj.ToObject<StatusUpdatePacket>(),
[ArchipelagoPacketType.Say] = (JObject obj) => obj.ToObject<SayPacket>(),
[ArchipelagoPacketType.GetDataPackage] = (JObject obj) => obj.ToObject<GetDataPackagePacket>(),
[ArchipelagoPacketType.DataPackage] = (JObject obj) => obj.ToObject<DataPackagePacket>(),
[ArchipelagoPacketType.Bounce] = (JObject obj) => obj.ToObject<BouncePacket>(),
[ArchipelagoPacketType.Bounced] = (JObject obj) => obj.ToObject<BouncedPacket>(),
};

public override bool CanWrite => false;

public override bool CanConvert(Type objectType) => objectType.IsAssignableFrom(typeof(ArchipelagoPacketBase));
Expand All @@ -18,57 +40,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

var commandType = token["cmd"].ToString();
ArchipelagoPacketType packetType = (ArchipelagoPacketType)Enum.Parse(typeof(ArchipelagoPacketType), commandType);
ArchipelagoPacketBase ret;
switch (packetType)

ArchipelagoPacketBase ret = null;
if (PacketDeserializationMap.ContainsKey(packetType))
{
case ArchipelagoPacketType.RoomInfo:
ret = token.ToObject<RoomInfoPacket>();
break;
case ArchipelagoPacketType.ConnectionRefused:
ret = token.ToObject<ConnectionRefusedPacket>();
break;
case ArchipelagoPacketType.Connected:
ret = token.ToObject<ConnectedPacket>();
break;
case ArchipelagoPacketType.ReceivedItems:
ret = token.ToObject<ReceivedItemsPacket>();
break;
case ArchipelagoPacketType.LocationInfo:
ret = token.ToObject<LocationInfoPacket>();
break;
case ArchipelagoPacketType.RoomUpdate:
ret = token.ToObject<RoomUpdatePacket>();
break;
case ArchipelagoPacketType.Print:
ret = token.ToObject<PrintPacket>();
break;
case ArchipelagoPacketType.PrintJSON:
ret = token.ToObject<PrintJsonPacket>();
break;
case ArchipelagoPacketType.Connect:
ret = token.ToObject<ConnectPacket>();
break;
case ArchipelagoPacketType.LocationChecks:
ret = token.ToObject<LocationChecksPacket>();
break;
case ArchipelagoPacketType.LocationScouts:
ret = token.ToObject<LocationScoutsPacket>();
break;
case ArchipelagoPacketType.StatusUpdate:
ret = token.ToObject<StatusUpdatePacket>();
break;
case ArchipelagoPacketType.Say:
ret = token.ToObject<SayPacket>();
break;
case ArchipelagoPacketType.GetDataPackage:
ret = token.ToObject<GetDataPackagePacket>();
break;
case ArchipelagoPacketType.DataPackage:
ret = token.ToObject<DataPackagePacket>();
break;
default:
throw new NotImplementedException("Received an unknown packet.");
ret = PacketDeserializationMap[packetType](token);
}
else
{
throw new InvalidOperationException("Received an unknown packet.");
}

return ret;
}

Expand Down
4 changes: 3 additions & 1 deletion Archipelago.MultiClient.Net/Enums/ArchipelagoPacketType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum ArchipelagoPacketType
Say,
GetDataPackage,
DataPackage,
Sync
Sync,
Bounced,
Bounce
}
}
3 changes: 3 additions & 0 deletions Archipelago.MultiClient.Net/Models/DataPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public class DataPackage

[JsonProperty("version")]
public int Version { get; set; }

[JsonProperty("games")]
public Dictionary<string, GameData> Games { get; set; }
}
}
20 changes: 20 additions & 0 deletions Archipelago.MultiClient.Net/Models/GameData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Archipelago.MultiClient.Net.Models
{
public class GameData
{
[JsonProperty("item_name_to_id")]
public Dictionary<string, int> LocationLookup { get; set; }

[JsonProperty("location_name_to_id")]
public Dictionary<string, int> ItemLookup { get; set; }

[JsonProperty("version")]
public int Version { get; set; }
}
}
23 changes: 23 additions & 0 deletions Archipelago.MultiClient.Net/Packets/BouncePacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using Archipelago.MultiClient.Net.Enums;
using Newtonsoft.Json;

namespace Archipelago.MultiClient.Net.Packets
{
public class BouncePacket : ArchipelagoPacketBase
{
public override ArchipelagoPacketType PacketType => ArchipelagoPacketType.Bounce;

[JsonProperty("games")]
public List<string> Games { get; set; }

[JsonProperty("slots")]
public List<int> Slots { get; set; }

[JsonProperty("tags")]
public List<string> Tags { get; set; }

[JsonProperty("data")]
public Dictionary<string, object> Data { get; set; }
}
}
12 changes: 12 additions & 0 deletions Archipelago.MultiClient.Net/Packets/BouncedPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using Archipelago.MultiClient.Net.Enums;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Archipelago.MultiClient.Net.Packets
{
public class BouncedPacket : BouncePacket
{
public override ArchipelagoPacketType PacketType => ArchipelagoPacketType.Bounced;
}
}

0 comments on commit c8f5917

Please sign in to comment.