[C++ and C#] Need help converting code from C++ to C#

Experienced Elementalist
Joined
Apr 2, 2008
Messages
271
Reaction score
24
Hey, I'm currently trying to port a C++ source into C#, but it confuses me;

the C++ code:
Code:
void EncryptPacket( CTitanClient* baseclient, CTitanPacket* pak ){
        if(pak->Size() > 0xFF){
            pak->Set<byte>(0, 0, 0);
            memcpy(pak->Buffer() + 3, pak->Buffer() + 1, pak->Size() - 1);
            pak->Size(pak->Size() + 2);
            pak->Set<word>(pak->Size() -  3, 1, 0);
        }else{
            pak->Set<byte>(pak->Size() - 1,0,0);
        }
    }
the baseclient is declared but has no use?

Code:
    template <typename T> void Set( T val, word pos, word offset = PACKET_HEADER_SIZE )
    {
        *((T*)&_Buffer[pos + offset]) = val;
    }
The pak is a buffer wich stores all the written packet data, and after that, the buffer has to been Encrypted;

Current code:
the memcpy doesn't work with BlockCopy, how do I fix it?
packet.memoryStream = newpacket; doesn't work either
Code:
public void EncryptPacket(Packet packet, Player player)
        {
            if (packet.GetLenght() > 0xFF)
            {
                byte[] newpacket;
                packet.WriteAtOffset((byte)0, 0, 0);
              Buffer.BlockCopy(packet.GetWrittenBuffer() + 3, newpacket + 1, packet.GetLenght() - 1);
                packet.memoryStream = newpacket;
                packet.PacketSize += 2;
                packet.WriteAtOffset((short)packet.PacketSize - 3, 1, 0);
            }
            else
            {
                packet.WriteAtOffset((byte)packet.PacketSize - 1, 0, 0);
            }
        }

        public void WriteAtOffset(object value,int pos, int offset)
        {
            binaryWriter.Write(value, pos + offset, LENGHT);
        }

How do I get the bytes count from an object? Like if it's a short it would return 2?

Please help me
thx in advance :wink:
 
Last edited:
I see you're trying to convert those Fiesta files ;)
Also, shouldn't you initiate the newpacket var? (ie. byte[] newpacket = new byte[];)
Yeah you're right:
Code:
byte[] newpacket = new byte[packet.GetLenght()];
, but what about the memcpy?

I can't just make the packet.memorystream equal to a byte[] array?
I don't get the memcpy part :huh:

Thx in advance,
Kerelmans
 
Back