/// <summary> /// Write the data in the given instance of AddonPacketData into the given raw packet instance. /// </summary> /// <param name="packet">The packet to write into.</param> /// <param name="addonPacketData">AddonPacketData instance from which to data should be written.</param> /// <returns>true if any of the data written was reliable; otherwise false.</returns> private bool WriteAddonPacketData( Packet packet, AddonPacketData addonPacketData ) => WritePacketData( packet, addonPacketData.PacketData, addonPacketData.PacketIdEnumerator, addonPacketData.PacketIdSize );
/// <summary> /// Read all raw addon data from the given packet into the given dictionary containing entries for all addons. /// </summary> /// <param name="packet">The raw packet instance to read from.</param> /// <param name="addonDataDict">The dictionary for all addon data to write the read data into.</param> /// <exception cref="Exception">Thrown if the any part of reading the data throws.</exception> private void ReadAddonDataDict( Packet packet, Dictionary <byte, AddonPacketData> addonDataDict ) { // Read the number of the addon packet data instances from the packet var numAddonData = packet.ReadByte(); while (numAddonData-- > 0) { var addonId = packet.ReadByte(); if (!AddonPacketInfoDict.TryGetValue(addonId, out var addonPacketInfo)) { // If the addon packet info for this addon could not be found, we need to throw an exception throw new Exception($"Addon with ID {addonId} has no defined addon packet info"); } // Read the length of the addon packet data for this addon var addonDataLength = packet.ReadUShort(); // Read exactly as many bytes as was indicated by the previously read value var addonDataBytes = packet.ReadBytes(addonDataLength); // Create a new packet object with the given bytes so we can sandbox the reading var addonPacket = new Packet(addonDataBytes); // Create a new instance of AddonPacketData to read packet data into and eventually // add to this packet instance's dictionary var addonPacketData = new AddonPacketData(addonPacketInfo.PacketIdSize); try { ReadAddonPacketData( addonPacket, addonPacketInfo.PacketIdSize, addonPacketInfo.PacketDataInstantiator, addonPacketData.PacketData ); } catch (Exception e) { // If the addon data reading throws an exception, we skip it entirely and since // we read it into a separate packet, it has no impact on the regular packet Logger.Get().Debug(this, $"Addon with ID {addonId} has thrown an exception while reading addon packet data, type: {e.GetType()}, message: {e.Message}"); continue; } addonDataDict[addonId] = addonPacketData; } }
/// <summary> /// Sets the given addonPacketData with the given addon ID for sending. /// </summary> /// <param name="addonId">The addon ID to set data for.</param> /// <param name="packetData">Instance of AddonPacketData to set.</param> public void SetSendingAddonPacketData(byte addonId, AddonPacketData packetData) { _addonPacketData[addonId] = packetData; }
/// <summary> /// Tries to get addon packet data for the addon with the given ID. /// </summary> /// <param name="addonId">The ID of the addon to get the data for.</param> /// <param name="addonPacketData">An instance of AddonPacketData corresponding to the given ID. /// Null if this method returns false.</param> /// <returns>true if the addon packet data exists and will be stored in the addonPacketData variable; /// otherwise false.</returns> public bool TryGetSendingAddonPacketData(byte addonId, out AddonPacketData addonPacketData) { return(_addonPacketData.TryGetValue(addonId, out addonPacketData)); }