fix enum keyword warnings

This commit is contained in:
Jakob Gillich 2023-03-26 16:01:33 +00:00
parent 0772c27973
commit b69e13c3ee
No known key found for this signature in database
GPG key ID: FD8BF52DB8452C91
11 changed files with 102 additions and 93 deletions

View file

@ -89,20 +89,20 @@ object PacketHelpers {
} }
/** Create a Codec for an enumeration type that can correctly represent its value /** Create a Codec for an enumeration type that can correctly represent its value
* @param enum the enumeration type to create a codec for * @param e the enumeration type to create a codec for
* @param storageCodec the Codec used for actually representing the value * @param storageCodec the Codec used for actually representing the value
* @tparam E The inferred type * @tparam E The inferred type
* @return Generated codec * @return Generated codec
*/ */
def createEnumerationCodec[E <: Enumeration](enum: E, storageCodec: Codec[Int]): Codec[E#Value] = { def createEnumerationCodec[E <: Enumeration](e: E, storageCodec: Codec[Int]): Codec[E#Value] = {
type Struct = Int :: HNil type Struct = Int :: HNil
val struct: Codec[Struct] = storageCodec.hlist val struct: Codec[Struct] = storageCodec.hlist
val primitiveLimit = Math.pow(2, storageCodec.sizeBound.exact.get.toDouble) val primitiveLimit = Math.pow(2, storageCodec.sizeBound.exact.get.toDouble)
// Assure that the enum will always be able to fit in a N-bit int // Assure that the enum will always be able to fit in a N-bit int
assert( assert(
enum.maxId <= primitiveLimit, e.maxId <= primitiveLimit,
enum.getClass.getCanonicalName + s": maxId exceeds primitive type (limit of $primitiveLimit, maxId ${enum.maxId})" e.getClass.getCanonicalName + s": maxId exceeds primitive type (limit of $primitiveLimit, maxId ${e.maxId})"
) )
def to(pkt: E#Value): Struct = { def to(pkt: E#Value): Struct = {
@ -113,13 +113,13 @@ object PacketHelpers {
struct match { struct match {
case enumVal :: HNil => case enumVal :: HNil =>
// verify that this int can match the enum // verify that this int can match the enum
val first = enum.values.firstKey.id val first = e.values.firstKey.id
val last = enum.maxId - 1 val last = e.maxId - 1
if (enumVal >= first && enumVal <= last) if (enumVal >= first && enumVal <= last)
Attempt.successful(enum(enumVal)) Attempt.successful(e(enumVal))
else else
Attempt.failure(Err(s"Expected ${enum} with ID between [${first}, ${last}], but got '${enumVal}'")) Attempt.failure(Err(s"Expected ${e} with ID between [${first}, ${last}], but got '${enumVal}'"))
} }
struct.narrow[E#Value](from, to) struct.narrow[E#Value](from, to)
@ -130,12 +130,12 @@ object PacketHelpers {
* NOTE: enumerations in scala can't be represented by more than an Int anyways, so this conversion shouldn't matter. * NOTE: enumerations in scala can't be represented by more than an Int anyways, so this conversion shouldn't matter.
* This is only to overload createEnumerationCodec to work with uint32[L] codecs (which are Long) * This is only to overload createEnumerationCodec to work with uint32[L] codecs (which are Long)
*/ */
def createLongEnumerationCodec[E <: Enumeration](enum: E, storageCodec: Codec[Long]): Codec[E#Value] = { def createLongEnumerationCodec[E <: Enumeration](e: E, storageCodec: Codec[Long]): Codec[E#Value] = {
createEnumerationCodec(enum, storageCodec.xmap[Int](_.toInt, _.toLong)) createEnumerationCodec(e, storageCodec.xmap[Int](_.toInt, _.toLong))
} }
/** Create a Codec for enumeratum's IntEnum type */ /** Create a Codec for enumeratum's IntEnum type */
def createIntEnumCodec[E <: IntEnumEntry](enum: IntEnum[E], storageCodec: Codec[Int]): Codec[E] = { def createIntEnumCodec[E <: IntEnumEntry](e: IntEnum[E], storageCodec: Codec[Int]): Codec[E] = {
type Struct = Int :: HNil type Struct = Int :: HNil
val struct: Codec[Struct] = storageCodec.hlist val struct: Codec[Struct] = storageCodec.hlist
@ -146,36 +146,36 @@ object PacketHelpers {
def from(struct: Struct): Attempt[E] = def from(struct: Struct): Attempt[E] =
struct match { struct match {
case enumVal :: HNil => case enumVal :: HNil =>
enum.withValueOpt(enumVal) match { e.withValueOpt(enumVal) match {
case Some(v) => Attempt.successful(v) case Some(v) => Attempt.successful(v)
case None => case None =>
Attempt.failure(Err(s"Enum value '${enumVal}' not found in values '${enum.values.toString()}'")) Attempt.failure(Err(s"Enum value '${enumVal}' not found in values '${e.values.toString()}'"))
} }
} }
struct.narrow[E](from, to) struct.narrow[E](from, to)
} }
def createLongIntEnumCodec[E <: IntEnumEntry](enum: IntEnum[E], storageCodec: Codec[Long]): Codec[E] = { def createLongIntEnumCodec[E <: IntEnumEntry](e: IntEnum[E], storageCodec: Codec[Long]): Codec[E] = {
createIntEnumCodec(enum, storageCodec.xmap[Int](_.toInt, _.toLong)) createIntEnumCodec(e, storageCodec.xmap[Int](_.toInt, _.toLong))
} }
/** Create a Codec for enumeratum's Enum type */ /** Create a Codec for enumeratum's Enum type */
def createEnumCodec[E <: EnumEntry](enum: Enum[E], storageCodec: Codec[Int]): Codec[E] = { def createEnumCodec[E <: EnumEntry](e: Enum[E], storageCodec: Codec[Int]): Codec[E] = {
type Struct = Int :: HNil type Struct = Int :: HNil
val struct: Codec[Struct] = storageCodec.hlist val struct: Codec[Struct] = storageCodec.hlist
def to(pkt: E): Struct = { def to(pkt: E): Struct = {
enum.indexOf(pkt) :: HNil e.indexOf(pkt) :: HNil
} }
def from(struct: Struct): Attempt[E] = def from(struct: Struct): Attempt[E] =
struct match { struct match {
case enumVal :: HNil => case enumVal :: HNil =>
enum.valuesToIndex.find(_._2 == enumVal) match { e.valuesToIndex.find(_._2 == enumVal) match {
case Some((v, _)) => Attempt.successful(v) case Some((v, _)) => Attempt.successful(v)
case None => case None =>
Attempt.failure(Err(s"Enum index '${enumVal}' not found in values '${enum.valuesToIndex.toString()}'")) Attempt.failure(Err(s"Enum index '${enumVal}' not found in values '${e.valuesToIndex.toString()}'"))
} }
} }

View file

@ -59,12 +59,15 @@ object GenericActionMessage extends Marshallable[GenericActionMessage] {
}) })
} }
private val genericActionCodec = uint(bits = 6).xmap[GenericAction]({ private val genericActionCodec = uint(bits = 6).xmap[GenericAction](
i => GenericAction.values.find { _.value == i } match { { i =>
GenericAction.values.find { _.value == i } match {
case Some(enum) => enum case Some(enum) => enum
case None => GenericAction.Unknown(i) case None => GenericAction.Unknown(i)
} }
}, enum => enum.value) },
e => e.value
)
implicit val codec: Codec[GenericActionMessage] = ("action" | genericActionCodec).as[GenericActionMessage] implicit val codec: Codec[GenericActionMessage] = ("action" | genericActionCodec).as[GenericActionMessage]
} }

View file

@ -15,7 +15,7 @@ object TerrainCondition extends Enumeration {
type Type = Value type Type = Value
val Safe, Unsafe = Value val Safe, Unsafe = Value
implicit val codec = PacketHelpers.createEnumerationCodec(enum = this, uint(bits = 1)) implicit val codec = PacketHelpers.createEnumerationCodec(e = this, uint(bits = 1))
} }
/** /**
@ -32,7 +32,7 @@ final case class InvalidTerrainMessage(
vehicle_guid: PlanetSideGUID, vehicle_guid: PlanetSideGUID,
proximity_alert: TerrainCondition.Value, proximity_alert: TerrainCondition.Value,
pos: Vector3 pos: Vector3
) extends PlanetSideGamePacket { ) extends PlanetSideGamePacket {
type Packet = InvalidTerrainMessage type Packet = InvalidTerrainMessage
def opcode = GamePacketOpcode.InvalidTerrainMessage def opcode = GamePacketOpcode.InvalidTerrainMessage
def encode = InvalidTerrainMessage.encode(this) def encode = InvalidTerrainMessage.encode(this)
@ -40,8 +40,7 @@ final case class InvalidTerrainMessage(
object InvalidTerrainMessage extends Marshallable[InvalidTerrainMessage] { object InvalidTerrainMessage extends Marshallable[InvalidTerrainMessage] {
implicit val codec: Codec[InvalidTerrainMessage] = ( implicit val codec: Codec[InvalidTerrainMessage] = (("player_guid" | PlanetSideGUID.codec) ::
("player_guid" | PlanetSideGUID.codec) ::
("vehicle_guid" | PlanetSideGUID.codec) :: ("vehicle_guid" | PlanetSideGUID.codec) ::
("proximity_alert" | TerrainCondition.codec) :: ("proximity_alert" | TerrainCondition.codec) ::
("pos" | floatL :: floatL :: floatL).narrow[Vector3]( ("pos" | floatL :: floatL :: floatL).narrow[Vector3](

View file

@ -22,7 +22,7 @@ object SquadAction {
val AnyPositions, AvailablePositions, SomeCertifications, AllCertifications = Value val AnyPositions, AvailablePositions, SomeCertifications, AllCertifications = Value
implicit val codec: Codec[SearchMode.Value] = PacketHelpers.createEnumerationCodec(enum = this, uint(bits = 3)) implicit val codec: Codec[SearchMode.Value] = PacketHelpers.createEnumerationCodec(e = this, uint(bits = 3))
} }
final case class DisplaySquad() extends SquadAction(code = 0) final case class DisplaySquad() extends SquadAction(code = 0)

View file

@ -11,7 +11,7 @@ object MemberEvent extends Enumeration {
val Add, Remove, Promote, UpdateZone, Outfit = Value val Add, Remove, Promote, UpdateZone, Outfit = Value
implicit val codec = PacketHelpers.createEnumerationCodec(enum = this, uint(bits = 3)) implicit val codec = PacketHelpers.createEnumerationCodec(e = this, uint(bits = 3))
} }
final case class SquadMemberEvent( final case class SquadMemberEvent(
@ -58,13 +58,18 @@ object SquadMemberEvent extends Marshallable[SquadMemberEvent] {
("unk2" | uint16L) :: ("unk2" | uint16L) ::
("char_id" | uint32L) :: ("char_id" | uint32L) ::
("position" | uint4) :: ("position" | uint4) ::
("player_name" | conditional(action == MemberEvent.Add, PacketHelpers.encodedWideStringAligned(adjustment = 1))) :: ("player_name" | conditional(
action == MemberEvent.Add,
PacketHelpers.encodedWideStringAligned(adjustment = 1)
)) ::
("zone_number" | conditional(action == MemberEvent.Add || action == MemberEvent.UpdateZone, uint16L)) :: ("zone_number" | conditional(action == MemberEvent.Add || action == MemberEvent.UpdateZone, uint16L)) ::
("outfit_id" | conditional(action == MemberEvent.Add || action == MemberEvent.Outfit, uint32L)) ("outfit_id" | conditional(action == MemberEvent.Add || action == MemberEvent.Outfit, uint32L))
}).exmap[SquadMemberEvent]( }).exmap[SquadMemberEvent](
{ {
case action :: unk2 :: char_id :: member_position :: player_name :: zone_number :: outfit_id :: HNil => case action :: unk2 :: char_id :: member_position :: player_name :: zone_number :: outfit_id :: HNil =>
Attempt.Successful(SquadMemberEvent(action, unk2, char_id, member_position, player_name, zone_number, outfit_id)) Attempt.Successful(
SquadMemberEvent(action, unk2, char_id, member_position, player_name, zone_number, outfit_id)
)
}, },
{ {
case SquadMemberEvent( case SquadMemberEvent(

View file

@ -16,7 +16,7 @@ object WaypointEventAction extends Enumeration {
val Add, Unknown1, Remove, Unknown3 //unconfirmed val Add, Unknown1, Remove, Unknown3 //unconfirmed
= Value = Value
implicit val codec: Codec[WaypointEventAction.Value] = PacketHelpers.createEnumerationCodec(enum = this, uint2) implicit val codec: Codec[WaypointEventAction.Value] = PacketHelpers.createEnumerationCodec(e = this, uint2)
} }
/** /**

View file

@ -15,7 +15,7 @@ sealed abstract class CharacterSex(
val pronounSubject: String, val pronounSubject: String,
val pronounObject: String, val pronounObject: String,
val possessive: String val possessive: String
) extends IntEnumEntry { ) extends IntEnumEntry {
def possessiveNoObject: String = possessive def possessiveNoObject: String = possessive
} }
@ -25,14 +25,16 @@ sealed abstract class CharacterSex(
object CharacterSex extends IntEnum[CharacterSex] { object CharacterSex extends IntEnum[CharacterSex] {
val values = findValues val values = findValues
case object Male extends CharacterSex( case object Male
extends CharacterSex(
value = 1, value = 1,
pronounSubject = "he", pronounSubject = "he",
pronounObject = "him", pronounObject = "him",
possessive = "his" possessive = "his"
) )
case object Female extends CharacterSex( case object Female
extends CharacterSex(
value = 2, value = 2,
pronounSubject = "she", pronounSubject = "she",
pronounObject = "her", pronounObject = "her",
@ -41,5 +43,5 @@ object CharacterSex extends IntEnum[CharacterSex] {
override def possessiveNoObject: String = "hers" override def possessiveNoObject: String = "hers"
} }
implicit val codec = PacketHelpers.createIntEnumCodec(enum = this, uint2L) implicit val codec = PacketHelpers.createIntEnumCodec(e = this, uint2L)
} }

View file

@ -15,5 +15,5 @@ object ExperienceType extends IntEnum[ExperienceType] {
case object Support extends ExperienceType(value = 2) case object Support extends ExperienceType(value = 2)
case object RabbitBall extends ExperienceType(value = 4) case object RabbitBall extends ExperienceType(value = 4)
implicit val codec: Codec[ExperienceType] = PacketHelpers.createIntEnumCodec(enum = this, uint(bits = 3)) implicit val codec: Codec[ExperienceType] = PacketHelpers.createIntEnumCodec(e = this, uint(bits = 3))
} }

View file

@ -165,8 +165,8 @@ object MeritCommendation extends Enumeration {
{ {
case MeritCommendation.None => case MeritCommendation.None =>
Attempt.successful(0xffffffffL) Attempt.successful(0xffffffffL)
case enum => case e =>
Attempt.successful(enum.id.toLong) Attempt.successful(e.id.toLong)
} }
) )
} }

View file

@ -23,5 +23,5 @@ object OxygenState extends Enum[OxygenState] {
case object Recovery extends OxygenState case object Recovery extends OxygenState
case object Suffocation extends OxygenState case object Suffocation extends OxygenState
implicit val codec: Codec[OxygenState] = PacketHelpers.createEnumCodec(enum = this, uint(bits = 1)) implicit val codec: Codec[OxygenState] = PacketHelpers.createEnumCodec(e = this, uint(bits = 1))
} }

View file

@ -25,12 +25,12 @@ object Config {
} }
implicit def enumeratumIntConfigConvert[A <: IntEnumEntry](implicit implicit def enumeratumIntConfigConvert[A <: IntEnumEntry](implicit
enum: IntEnum[A], e: IntEnum[A],
ct: ClassTag[A] ct: ClassTag[A]
): ConfigConvert[A] = ): ConfigConvert[A] =
viaNonEmptyStringOpt[A]( viaNonEmptyStringOpt[A](
v => v =>
enum.values.toList.collectFirst { e.values.toList.collectFirst {
case e: ServerType if e.name == v => e.asInstanceOf[A] case e: ServerType if e.name == v => e.asInstanceOf[A]
case e: BattleRank if e.value.toString == v => e.asInstanceOf[A] case e: BattleRank if e.value.toString == v => e.asInstanceOf[A]
case e: CommandRank if e.value.toString == v => e.asInstanceOf[A] case e: CommandRank if e.value.toString == v => e.asInstanceOf[A]
@ -40,12 +40,12 @@ object Config {
) )
implicit def enumeratumConfigConvert[A <: EnumEntry](implicit implicit def enumeratumConfigConvert[A <: EnumEntry](implicit
enum: Enum[A], e: Enum[A],
ct: ClassTag[A] ct: ClassTag[A]
): ConfigConvert[A] = ): ConfigConvert[A] =
viaNonEmptyStringOpt[A]( viaNonEmptyStringOpt[A](
v => v =>
enum.values.toList.collectFirst { e.values.toList.collectFirst {
case e if e.toString.toLowerCase == v.toLowerCase => e.asInstanceOf[A] case e if e.toString.toLowerCase == v.toLowerCase => e.asInstanceOf[A]
}, },
_.toString _.toString