TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

466 lines
14 KiB
C#

using UnityEngine;
using System.Collections;
using System;
public class PacketManager
{
private static byte[] outStreamBuffer;
//packet id 1
//sends player update
public static void sendPlayerUpdatePacket(float x, float y, float z, float rotationY, float lookAngle, float animX, float animZ, float magnitude, byte flag)
{
//Debug.LogWarning("Send Player Packet " + playerId + ", x " + x + " y " + y + " z " + z);
byte packetSize = 34;
outStreamBuffer = new byte[packetSize];
int bufferPosition = 0;
WriteByte(43, ref bufferPosition);
WriteFloat(x, ref bufferPosition);
WriteFloat(y, ref bufferPosition);
WriteFloat(z, ref bufferPosition);
WriteFloat(rotationY, ref bufferPosition);
WriteFloat(lookAngle, ref bufferPosition);
WriteFloat(animX, ref bufferPosition);
WriteFloat(animZ, ref bufferPosition);
WriteFloat(magnitude, ref bufferPosition);
WriteByte(flag, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendLogoutPacket()
{
byte packetSize = 1;
const byte packetId = 2;
int bufferPosition = 0;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet id 3 send chat message
public static void sendChatMessage(string message)
{
int bufferPosition = 0;
int packetSize = 2 + message.Length;
outStreamBuffer = new byte[packetSize];
const byte packetId = 3;
WriteByte(packetId, ref bufferPosition);
WriteString(message, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet id 4 send player shot
public static void sendPlayerShot(Vector3 origin, Vector3 direction, int[] hitFlag, Vector3[] impact, string[] gameObjectHit, int[] entityId, int [] hitSheild)
{
int bufferPosition = 0;
//int packetSize = 33;
//packetSize += 25;
int packetSize = 26;
for (int i = 0; i < hitFlag.Length; i++)
{
//packetSize += 4 + 1 + 12 + gameObjectHit[i].Length + 1 + 4;
packetSize += 25 + gameObjectHit[i].Length;
}
outStreamBuffer = new byte[packetSize];
const byte packetId = 54;
WriteByte(packetId, ref bufferPosition);
WriteFloat(origin.x, ref bufferPosition);
WriteFloat(origin.y, ref bufferPosition);
WriteFloat(origin.z, ref bufferPosition);
WriteFloat(direction.x, ref bufferPosition);
WriteFloat(direction.y, ref bufferPosition);
WriteFloat(direction.z, ref bufferPosition);
WriteByte((byte)hitFlag.Length, ref bufferPosition);
for (int i = 0; i < hitFlag.Length; i++)
{
WriteInt(hitFlag[i], ref bufferPosition);
WriteFloat(impact[i].x, ref bufferPosition);
WriteFloat(impact[i].y, ref bufferPosition);
WriteFloat(impact[i].z, ref bufferPosition);
WriteString(gameObjectHit[i], ref bufferPosition);
WriteInt(entityId[i], ref bufferPosition);
WriteInt(hitSheild[i], ref bufferPosition);
}
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet id 5 - login request
public static void sendLoginRequest(string username, string password)
{
String macAddress = Client.Instance.getMacAddress();
GISocket.Instance.clearOutStream();
int bufferPosition = 0;
const byte packetId = 55;
int packetSize = 9 + Config.CLIENT_VERSION.Length + username.Length + password.Length + macAddress.Length;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(1, ref bufferPosition);
WriteString(Config.CLIENT_VERSION, ref bufferPosition);
WriteString(username, ref bufferPosition);
WriteString(password, ref bufferPosition);
WriteString(macAddress, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
//clear the outstream
GISocket.Instance.pushOutStream();
//print message
// Debug.Log("Sending login request " + username + " " + password);
}
//packet id 6 send chat message
public static void sendButtonClick(string buttonName)
{
int bufferPosition = 0;
int packetSize = buttonName.Length + 4 + 1;
outStreamBuffer = new byte[packetSize];
const byte packetId = 6;
WriteByte(packetId, ref bufferPosition);
WriteLongString(buttonName, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet 8 click object
public static void sendObjectClick(string objectName, int x, int y)
{
int bufferPosition = 0;
int packetSize = objectName.Length + 10;
const byte packetId = 41;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteString(objectName, ref bufferPosition);
WriteInt(x, ref bufferPosition);
WriteInt(y, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet 9 drop item
public static void sendDropItem(int itemId, int slot)
{
int bufferPosition = 0;
int packetSize = 9;
const byte packetId = 42;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(itemId, ref bufferPosition);
WriteInt(slot, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//packet 10 move item in inventory
public static void sendMoveItem(int from, int to, int inventoryOrBank)
{
int bufferPosition = 0;
int packetSize = 13;
const byte packetId = 44;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(from, ref bufferPosition);
WriteInt(to, ref bufferPosition);
WriteInt(inventoryOrBank, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendClickItem(int itemId, int slot)
{
int bufferPosition = 0;
int packetSize = 9;
const byte packetId = 45;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(itemId, ref bufferPosition);
WriteInt(slot, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendPickupItem(int itemId, int x, int y)
{
int bufferPosition = 0;
int packetSize = 13;
const byte packetId = 46;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(itemId, ref bufferPosition);
WriteInt(x, ref bufferPosition);
WriteInt(y, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendUnequipItem(int slot)
{
int bufferPosition = 0;
int packetSize = 2;
const byte packetId = 47;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteByte((byte)slot, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendPing(float x, float y, float z, String parent)
{
int bufferPosition = 0;
int packetSize = 14 + parent.Length;
const byte packetId = 48;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteFloat(x, ref bufferPosition);
WriteFloat(y, ref bufferPosition);
WriteFloat(z, ref bufferPosition);
WriteString(parent, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendMeleeAttack(int hitFlag, int npcId)
{
int bufferPosition = 0;
int packetSize = 9;
const byte packetId = 49;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(hitFlag, ref bufferPosition);
WriteInt(npcId, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendThrowItem(float dX, float dY, float dZ)
{
int bufferPosition = 0;
int packetSize = 13;
const byte packetId = 50;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteFloat(dX, ref bufferPosition);
WriteFloat(dY, ref bufferPosition);
WriteFloat(dZ, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendThrowableExplosion(float x, float y, float z)
{
int bufferPosition = 0;
int packetSize = 13;
const byte packetId = 51;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteFloat(x, ref bufferPosition);
WriteFloat(y, ref bufferPosition);
WriteFloat(z, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendWithdrawBankOrForgeItem(int slotID, int bankOrForge)
{
int bufferPosition = 0;
int packetSize = 9;
const byte packetId = 18;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(slotID, ref bufferPosition);
WriteInt(bankOrForge, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendVoiceChatID(string voiceID)
{
int bufferPosition = 0;
int packetsize = 2 + voiceID.Length;
const byte packetId = 19;
outStreamBuffer = new byte[packetsize];
WriteByte(packetId, ref bufferPosition);
WriteString(voiceID, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendFractionalCryptoCollection(int collectedPlayerID, int dropType)
{
int bufferPosition = 0;
int packetSize = 9;
const byte packetId = 20;
outStreamBuffer = new byte[packetSize];
WriteByte(packetId, ref bufferPosition);
WriteInt(collectedPlayerID, ref bufferPosition);
WriteInt(dropType, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendForgeWeaponTrackers(int itemID)
{
int bufferPosition = 0;
int packetSize = 5;
const byte packetID = 21;
outStreamBuffer = new byte[packetSize];
WriteByte(packetID, ref bufferPosition);
WriteInt(itemID, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendNPCAttack(int type)
{
int bufferPosition = 0;
int packetSize = 5;
const byte packetID = 23;
outStreamBuffer = new byte[packetSize];
WriteByte(packetID, ref bufferPosition);
WriteInt(type, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendProjecctileHit(string projectile)
{
int bufferPosition = 0;
int packetSize = 2 + projectile.Length;
const byte packetID = 22;
outStreamBuffer = new byte[packetSize];
WriteByte(packetID, ref bufferPosition);
WriteString(projectile, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendPlaceItem(int itemID, float x, float y, float z, float rotY)
{
int bufferPosition = 0;
int packetSize = 21;
const byte packetID = 52;
outStreamBuffer = new byte[packetSize];
WriteByte(packetID, ref bufferPosition);
WriteInt(itemID, ref bufferPosition);
WriteFloat(x, ref bufferPosition);
WriteFloat(y, ref bufferPosition);
WriteFloat(z, ref bufferPosition);
WriteFloat(rotY, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
public static void sendMineRock(int rockId) {
int bufferPosition = 0;
int packetSize = 5;
const byte packetID = 53;
outStreamBuffer = new byte[packetSize];
WriteByte(packetID, ref bufferPosition);
WriteInt(rockId, ref bufferPosition);
GISocket.Instance.addToOutStream(outStreamBuffer);
}
//write a byte to the oustream buffer
private static void WriteByte(byte data, ref int position)
{
if (position < outStreamBuffer.Length)
outStreamBuffer[position] = data;
position += 1;
}
//write a short to the oustream buffer
private static void WriteShort(short data, ref int position)
{
outStreamBuffer[position] = (byte)(data >> 8);
outStreamBuffer[position + 1] = (byte)data;
position += 2;
}
//write an int to the outstream buffer
private static void WriteInt(int data, ref int position)
{
outStreamBuffer[position] = (byte)(data >> 24);
outStreamBuffer[position + 1] = (byte)(data >> 16);
outStreamBuffer[position + 2] = (byte)(data >> 8);
outStreamBuffer[position + 3] = (byte)(data);
position += 4;
}
//write a string to the outstream buffer
//must be less than 254 characters
private static void WriteString(string data, ref int position)
{
if (data.Length > 254)
{
Debug.Log("Error: String length more than 255");
return;
}
//write lenngth
outStreamBuffer[position] = (byte)data.Length;
position += 1;
//write string to buffer
for (int i = 0; i < data.Length; i++)
{
outStreamBuffer[position + i] = (byte)data[i];
}
position += data.Length;
}
//write a string to the outstream buffer
//must be less than 254 characters
private static void WriteLongString(string data, ref int position)
{
//write lenngth
WriteInt(data.Length, ref position);
//write string to buffer
for (int i = 0; i < data.Length; i++)
{
outStreamBuffer[position + i] = (byte)data[i];
}
position += data.Length;
}
private static void WriteFloat(float f, ref int position)
{
//string data = f.ToString("#.##");
//WriteString(data, ref position);
float data = f * 100;
WriteInt((int)data, ref position);
}
}