0
\$\begingroup\$

it's a tool I developed to make byte arrays meaningful in multiplayer games.

A tool that prepares the encode and decode codes for go and c#.

example input(example.customExtension)

type UnitNetworkDto{
 int16 ViewId;
 float X;
 float Z;
 int Health;
 int8 TeamType;
 int16 TargetViewId;
 Vector3Qoo Position;
}

type Vector3Qoo
 { float X;float Y;float Z;} 

go output

package typed

var (
  Vector3Qoo_BYTE_COUNT     = 12
  UnitNetworkDto_BYTE_COUNT = 29
)

type Vector3Qoo struct {
  X float32
  Y float32
  Z float32
}

func (v *Vector3Qoo) Encode() []byte {
  res := Float32ToBytes(v.X)
  res = append(res, Float32ToBytes(v.Y)...)
  res = append(res, Float32ToBytes(v.Z)...)
  return res
}

func Vector3QooDecode(input []byte) Vector3Qoo {
  res := Vector3Qoo{}
  res.X = Float32FromBytes(input[0:4])
  res.Y = Float32FromBytes(input[4:8])
  res.Z = Float32FromBytes(input[8:12])
  return res
}

type UnitNetworkDto struct {
  ViewId       int16
  X            float32
  Z            float32
  Health       int32
  TeamType     int8
  TargetViewId int16
  Position     Vector3Qoo
}

func (u *UnitNetworkDto) Encode() []byte {
  res := Int16ToBytes(u.ViewId)
  res = append(res, Float32ToBytes(u.X)...)
  res = append(res, Float32ToBytes(u.Z)...)
  res = append(res, Int32ToBytes(u.Health)...)
  res = append(res, Int8ToBytes(u.TeamType)...)
  res = append(res, Int16ToBytes(u.TargetViewId)...)
  res = append(res, u.Position.Encode()...)
  return res
}

func UnitNetworkDtoDecode(input []byte) UnitNetworkDto {
  res := UnitNetworkDto{}
  res.ViewId = Int16FromBytes(input[0:2])
  res.X = Float32FromBytes(input[2:6])
  res.Z = Float32FromBytes(input[6:10])
  res.Health = Int32FromBytes(input[10:14])
  res.TeamType = Int8FromBytes(input[14:15])
  res.TargetViewId = Int16FromBytes(input[15:17])
  res.Position = Vector3QooDecode(input[17:29])
  return res
}

c# output

namespace Typed
{
  public static class CSharpTypedFileHelper{
    public static int Vector3Qoo_BYTE_COUNT = 12;
    public static int UnitNetworkDto_BYTE_COUNT = 29;
  }

  public class Vector3Qoo{
    public float X { get; set; }
    public float Y { get; set; }
    public float Z { get; set; }
    public Vector3Qoo(byte[] input){
      this.X = QooUtils.FloatFromBytes(input.SubArray(0,4));
      this.Y = QooUtils.FloatFromBytes(input.SubArray(4,4));
      this.Z = QooUtils.FloatFromBytes(input.SubArray(8,4));
    }

    public Vector3Qoo(float q_X,float q_Y,float q_Z){
      X = q_X;
      Y = q_Y;
      Z = q_Z;
    }

    public byte[] Encode(){
      byte[] res = QooUtils.FloatToBytes(X);
      res = QooUtils.Combine(res,QooUtils.FloatToBytes(Y));
      res = QooUtils.Combine(res,QooUtils.FloatToBytes(Z));
      return res;
    }
 }

 public class UnitNetworkDto{
   public int ViewId { get; set; }
   public float X { get; set; }
   public float Z { get; set; }
   public int Health { get; set; }
   public int TeamType { get; set; }
   public int TargetViewId { get; set; }
   public Vector3Qoo Position { get; set; }
   
   public UnitNetworkDto(byte[] input){
     this.ViewId = QooUtils.Int16FromBytes(input.SubArray(0,2));
     this.X = QooUtils.FloatFromBytes(input.SubArray(2,4));
     this.Z = QooUtils.FloatFromBytes(input.SubArray(6,4));
     this.Health = QooUtils.Int32FromBytes(input.SubArray(10,4));
     this.TeamType = QooUtils.Int8FromBytes(input.SubArray(14,1));
     this.TargetViewId = QooUtils.Int16FromBytes(input.SubArray(15,2));
     this.Position = new Vector3Qoo(input.SubArray(17,12));
   }

   public UnitNetworkDto(int q_ViewId,float q_X,float q_Z,int q_Health,int 
     q_TeamType,int q_TargetViewId,Vector3Qoo q_Position){
     ViewId = q_ViewId;
     X = q_X;
     Z = q_Z;
     Health = q_Health;
     TeamType = q_TeamType;
     TargetViewId = q_TargetViewId;
     Position = q_Position;
   }

   public byte[] Encode(){
     byte[] res = QooUtils.Int16ToBytes(ViewId);
     res = QooUtils.Combine(res,QooUtils.FloatToBytes(X));
     res = QooUtils.Combine(res,QooUtils.FloatToBytes(Z));
     res = QooUtils.Combine(res,QooUtils.Int32ToBytes(Health));
     res = QooUtils.Combine(res,QooUtils.Int8ToBytes(TeamType));
     res = QooUtils.Combine(res,QooUtils.Int16ToBytes(TargetViewId));
     res = QooUtils.Combine(res,Position.Encode());
     return res;
   }
 }

}

I wrote this system with go. It converts the type file I gave (example.customExtension) to c# and go.

I'm new to multiplayer development, so I'm not sure if there's any cost on the server side to transfer and translate in this way. I'm wondering if creating and using a tool in this way is a method used among multiplayer game developers, your comments and suggestions. Evaluate the outputs I want from you

also the Int8FromBytes function might sound a bit silly :)

\$\endgroup\$
3
  • \$\begingroup\$ What is QooUtils? What is SubArray? Why do you need Vector3Qoo instead of System.Numerics.Vector3 which is hardware-accelerated? \$\endgroup\$
    – aepot
    Jan 31, 2022 at 10:27
  • \$\begingroup\$ QooUtils is simply a byte array converter. BitConverter.GetBytes(3.55); as. SubArray is an extension method. Returns a certain range of the array. Vector3Qoo is just an example it doesn't make sense. \$\endgroup\$
    – snn
    Jan 31, 2022 at 10:46
  • \$\begingroup\$ Please include all used here custom methods. They required for the review. What is .NET and Unity version? Do you care about performance? For Unity Vector3Qoo has even less sense than for other .NET apps. Please show the real code. Synthetic example isn't suitable for review. \$\endgroup\$
    – aepot
    Jan 31, 2022 at 10:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.