Skip to content

Commit

Permalink
feat(telemetry): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyupe committed Apr 5, 2022
1 parent 327c96b commit 740ae8a
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Cafe.Matcha/Cafe.Matcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Network\State.cs" />
<Compile Include="Network\Universalis\Structures\UniversalisQueryResponse.cs" />
<Compile Include="Telemetry\Opcode.cs" />
<Compile Include="Telemetry\NpcSpawn.cs" />
<Compile Include="Utils\BindingTarget.cs" />
<Compile Include="DTO\BaseDTO.cs" />
<Compile Include="DTO\CompanyVoyageStatusDTO.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Cafe.Matcha/Constant/MatchaOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal enum MatchaOpcode
MarketBoardItemListing,
MarketBoardItemListingCount,
MarketBoardItemListingHistory,
NpcSpawn,
PlayerSetup,
PlayerSpawn,
}
Expand All @@ -43,6 +44,7 @@ internal static class OpcodeStorage
{ 0x014b, MatchaOpcode.MarketBoardItemListing },
{ 0x0370, MatchaOpcode.MarketBoardItemListingCount },
{ 0x0103, MatchaOpcode.MarketBoardItemListingHistory },
{ 0x032f, MatchaOpcode.NpcSpawn },
{ 0x01db, MatchaOpcode.PlayerSetup },
{ 0x03dc, MatchaOpcode.PlayerSpawn },
};
Expand All @@ -62,6 +64,7 @@ internal static class OpcodeStorage
{ 0x0255, MatchaOpcode.MarketBoardItemListing },
{ 0x030f, MatchaOpcode.MarketBoardItemListingCount },
{ 0x006b, MatchaOpcode.MarketBoardItemListingHistory },
{ 0x0318, MatchaOpcode.NpcSpawn },
{ 0x03b2, MatchaOpcode.PlayerSetup },
{ 0x03b3, MatchaOpcode.PlayerSpawn },
};
Expand Down
3 changes: 3 additions & 0 deletions Cafe.Matcha/Constant/Secret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ internal static partial class Secret
{
public static string TelemetryRoot = "";
public static string UniversalisKey = "";

public static string TelemetryFate = "";
public static string TelemetryNpc = "";
}
}
43 changes: 17 additions & 26 deletions Cafe.Matcha/Models/ConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ public ConfigData() : this(null, null, null, null, null, null, null, null, null,
private ConfigData(
Region? region,
Language? language,
string uuid,
string hash,
ConfigOutput output,
ConfigFormatter formatter,
ConfigLogger logger,
ConfigWatch watch,
ConfigOverlay overlay,
ConfigTelemetry telemetry,
ObservableCollection<ConfigWebhook> webhook)
{
Region = region;
Language = language;
UUID = uuid ?? null;
Hash = hash ?? null;
Output = output ?? new ConfigOutput();
Formatter = formatter ?? new ConfigFormatter();
Logger = logger ?? new ConfigLogger();
Watch = watch ?? new ConfigWatch();
Overlay = overlay ?? new ConfigOverlay();
Telemetry = telemetry ?? new ConfigTelemetry();
Webhook = webhook ?? new ObservableCollection<ConfigWebhook>();
}

Expand All @@ -47,35 +47,14 @@ private ConfigData(
[JsonProperty("language")]
public Language? Language { get; set; }

public string UUID { get; set; }

public string Hash { get; set; }

[JsonIgnore]
public uint UUIDHash
{
get
{
if (string.IsNullOrEmpty(UUID) || UUID == "no")
{
return 0;
}

if (Guid.TryParse(UUID, out var guid))
{
var array = guid.ToByteArray();
return BitConverter.ToUInt32(array, 0);
}
else
{
return 0;
}
}
}

[JsonProperty("overlay")]
public ConfigOverlay Overlay { get; set; }

[JsonProperty("telemetry")]
public ConfigTelemetry Telemetry { get; set; }

[JsonProperty("watch")]
public ConfigWatch Watch { get; set; }

Expand Down Expand Up @@ -113,6 +92,18 @@ public class ConfigWebhook : BindingTarget
public string Header { get; set; }
}

public class ConfigTelemetry : BindingTarget
{
[JsonProperty("enable")]
public bool Enable { get; set; } = false;

[JsonProperty("uuid")]
public string UUID { get; set; } = null;

[JsonProperty("agreement")]
public string Agreement { get; set; } = null;
}

public class ConfigOverlay : BindingTarget
{
[JsonProperty("universalis")]
Expand Down
21 changes: 17 additions & 4 deletions Cafe.Matcha/Models/TelemetryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ internal class TelemetryData
{
public TelemetryData()
{
ClientId = Config.Instance.UUID;
ClientId = Config.Instance.Telemetry.UUID;
Version = Data.Version;
Date = DateTime.Now.ToString("yyyy-MM-dd");
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

Server = Network.State.Instance.WorldId;
World = Network.State.Instance.WorldId;
Server = Network.State.Instance.ServerId;
Zone = Network.State.Instance.ZoneId;
Instance = Network.State.Instance.InstanceId;
}

[JsonProperty("world")]
public ushort World = 0;

[JsonProperty("server")]
public uint Server = 0;
public ushort Server = 0;

[JsonProperty("zone")]
public uint Zone = 0;
public ushort Zone = 0;

[JsonProperty("instance")]
public ushort Instance = 0;

[JsonProperty("client_id")]
public string ClientId;
Expand Down Expand Up @@ -62,5 +70,10 @@ public override int GetHashCode()
{
return Server.GetHashCode() ^ Zone.GetHashCode() ^ Timestamp.GetHashCode();
}

public override string ToString()
{
return $"TData {{ World={World}, Server={Server}, Zone={Zone}, Instance={Instance} }} {base.ToString()}";
}
}
}
41 changes: 41 additions & 0 deletions Cafe.Matcha/Network/NetworkMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal interface INetworkMonitor
internal class NetworkMonitor : INetworkMonitor
{
private Fate fateTelemetry = new Fate();
private NpcSpawn npcTelemetry = new NpcSpawn();

private bool ToMatchaOpcode(ushort opcode, out MatchaOpcode matchaOpcode)
{
Expand Down Expand Up @@ -167,6 +168,32 @@ private bool HandleMessageByOpcode(byte[] message)
});
}
}
else if (opcode == MatchaOpcode.NpcSpawn)
{
if (message.Length != 672)
{
return false;
}

var bNpcName = BitConverter.ToUInt32(data, 68);
var hpMax = BitConverter.ToUInt32(data, 92);
var hpCur = BitConverter.ToUInt32(data, 96);
var fateId = BitConverter.ToUInt16(data, 104);
var level = data[127];

if (fateId != 0 || IsSpecialNpcName(bNpcName))
{
npcTelemetry.Send(
bNpcName,
fateId,
BitConverter.ToSingle(data, 504),
BitConverter.ToSingle(data, 508),
BitConverter.ToSingle(data, 512),
level,
hpMax
);
}
}
else if (opcode == MatchaOpcode.ActorControlSelf)
{
if (message.Length != 64)
Expand Down Expand Up @@ -330,7 +357,10 @@ private bool HandleMessageByOpcode(byte[] message)
return false;
}

State.Instance.ServerId = BitConverter.ToUInt16(data, 0);
State.Instance.ZoneId = BitConverter.ToUInt16(data, 2);
State.Instance.InstanceId = BitConverter.ToUInt16(data, 4);

FireEvent(new InitZoneDTO()
{
Zone = State.Instance.ZoneId,
Expand Down Expand Up @@ -555,6 +585,17 @@ private bool HandleMessageByOpcode(byte[] message)
return true;
}

private bool IsSpecialNpcName(uint bNpcName)
{
return (bNpcName >= 2919 && bNpcName <= 2969) ||
(bNpcName >= 4350 && bNpcName <= 4378) ||
bNpcName == 4380 ||
(bNpcName >= 5984 && bNpcName <= 6013) ||
(bNpcName >= 8653 && bNpcName <= 8657) ||
(bNpcName >= 8890 && bNpcName <= 8916) ||
(bNpcName >= 10615 && bNpcName <= 10616);
}

public delegate void ExceptionHandler(Exception e);
public event ExceptionHandler OnException;
private void FireException(Exception e)
Expand Down
5 changes: 5 additions & 0 deletions Cafe.Matcha/Network/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ internal class State : StaticBindingTarget<State>
{
private ushort worldId = 0;
private ushort zoneId = 0;

public ushort InstanceId { get; set; } = 0;

public ushort ServerId { get; set; } = 0;

public ushort WorldId
{
get
Expand Down
24 changes: 22 additions & 2 deletions Cafe.Matcha/Network/Universalis/PacketProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ private MarketBoardItemRequest GetRequestData(MatchaOpcode opcode, byte[] messag
{
if (opcode == MatchaOpcode.PlayerSetup)
{
LocalContentId = BitConverter.ToUInt64(message, 0x20);
LocalContentId = LocalContentId & 0xffffffff00000000 | (uint)Config.Instance.UUIDHash; // mask the lower 32 bit for privacy concern
// Mask lower-32bit for privacy concern
LocalContentId = BitConverter.ToUInt64(message, 0x20) & 0xffffffff00000000;
LocalContentId = LocalContentId | GetClientIdentifier();
Log?.Invoke(this, $"New CID: {LocalContentId.ToString("X")}");
LocalContentIdUpdated?.Invoke(this, LocalContentId);
return null;
Expand Down Expand Up @@ -235,5 +236,24 @@ private MarketBoardItemRequest GetRequestData(MatchaOpcode opcode, byte[] messag

return null;
}

private uint GetClientIdentifier()
{
var uuid = Config.Instance.Telemetry.UUID;
if (string.IsNullOrEmpty(uuid))
{
return 0;
}

if (Guid.TryParse(uuid, out var guid))
{
var array = guid.ToByteArray();
return BitConverter.ToUInt32(array, 0);
}
else
{
return 0;
}
}
}
}
4 changes: 2 additions & 2 deletions Cafe.Matcha/Telemetry/Fate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public override int GetHashCode()

public override string ToString()
{
return $"Fate {{ Id={FateId}, Zone={Zone} }}";
return $"Fate {{ Id={FateId}, Zone={Zone} }} {base.ToString()}";
}
}

internal class Fate : TelemetryWorker<FateInitDTO>
{
public Fate() : base("cc5a99c0-6296-11ea-8409-b51101308fa3") { }
public Fate() : base(Constant.Secret.TelemetryFate) { }

public void Send(uint fateId)
{
Expand Down
Loading

0 comments on commit 740ae8a

Please sign in to comment.