diff --git a/RageCoop.Client/Main.cs b/RageCoop.Client/Main.cs index 760e039..9941bd0 100644 --- a/RageCoop.Client/Main.cs +++ b/RageCoop.Client/Main.cs @@ -182,7 +182,7 @@ namespace RageCoop.Client Ticked++; } - + private void OnKeyDown(object sender, KeyEventArgs e) { if (MainChat.Focused) diff --git a/RageCoop.Client/Sync/Entities/SyncedPed.cs b/RageCoop.Client/Sync/Entities/SyncedPed.cs index da124f1..a665f47 100644 --- a/RageCoop.Client/Sync/Entities/SyncedPed.cs +++ b/RageCoop.Client/Sync/Entities/SyncedPed.cs @@ -84,7 +84,6 @@ namespace RageCoop.Client { if (IsPlayer) { - if (Username=="N/A") { var p = PlayerList.GetPlayer(ID); @@ -290,9 +289,9 @@ namespace RageCoop.Client private void SetClothes() { - for (byte i = 0; i < 11; i++) + for (byte i = 0; i < 12; i++) { - Function.Call(Hash.SET_PED_COMPONENT_VARIATION, MainPed.Handle, i, (int)Clothes[i], (int)Clothes[i+11], (int)Clothes[i+22]); + Function.Call(Hash.SET_PED_COMPONENT_VARIATION, MainPed.Handle, i, (int)Clothes[i], (int)Clothes[i+12], (int)Clothes[i+24]); } _lastClothes = Clothes; } diff --git a/RageCoop.Client/Util/PedExtensions.cs b/RageCoop.Client/Util/PedExtensions.cs index b35f664..d288494 100644 --- a/RageCoop.Client/Util/PedExtensions.cs +++ b/RageCoop.Client/Util/PedExtensions.cs @@ -68,12 +68,12 @@ namespace RageCoop.Client // Not sure whether component will always be lesser than 255, whatever... public static byte[] GetPedClothes(this Ped ped) { - var result = new byte[33]; - for (byte i = 0; i < 11; i++) + var result = new byte[36]; + for (byte i = 0; i < 12; i++) { result[i]=(byte)Function.Call(Hash.GET_PED_DRAWABLE_VARIATION, ped.Handle, i); - result[i+11]=(byte)Function.Call(Hash.GET_PED_TEXTURE_VARIATION, ped.Handle, i); - result[i+22]=(byte)Function.Call(Hash.GET_PED_PALETTE_VARIATION, ped.Handle, i); + result[i+12]=(byte)Function.Call(Hash.GET_PED_TEXTURE_VARIATION, ped.Handle, i); + result[i+24]=(byte)Function.Call(Hash.GET_PED_PALETTE_VARIATION, ped.Handle, i); } return result; } diff --git a/RageCoop.Core/BitReader.cs b/RageCoop.Core/BitReader.cs index 7b39653..5bbe17d 100644 --- a/RageCoop.Core/BitReader.cs +++ b/RageCoop.Core/BitReader.cs @@ -103,6 +103,13 @@ namespace RageCoop.Core CurrentIndex += index; return value; } + public string ReadString() + { + var len = ReadInt(); + string value = Encoding.UTF8.GetString(ResultArray.Skip(CurrentIndex).Take(len).ToArray()); + CurrentIndex += len; + return value; + } public Vector3 ReadVector3() { diff --git a/RageCoop.Core/CoreUtils.cs b/RageCoop.Core/CoreUtils.cs index 5d5bba0..c9bc69d 100644 --- a/RageCoop.Core/CoreUtils.cs +++ b/RageCoop.Core/CoreUtils.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using GTA.Math; +using System.Security.Cryptography; using System.Net; namespace RageCoop.Core { @@ -32,6 +33,8 @@ namespace RageCoop.Core return (0x08, BitConverter.GetBytes((float)obj)); case bool _: return (0x09, BitConverter.GetBytes((bool)obj)); + case string _: + return (0x10, (obj as string).GetBytesWithLength()); default: return (0x0, null); } @@ -73,11 +76,31 @@ namespace RageCoop.Core { bytes.AddRange(BitConverter.GetBytes(i)); } + public static void AddString(this List bytes, string s) + { + var sb = Encoding.UTF8.GetBytes(s); + bytes.AddInt(sb.Length); + bytes.AddRange(sb); + } + public static int GetHash(string s) + { + MD5 md5Hasher = MD5.Create(); + var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(s)); + return BitConverter.ToInt32(hashed, 0); + } public static byte[] GetBytes(this string s) { return Encoding.UTF8.GetBytes(s); } + public static byte[] GetBytesWithLength(this string s) + { + var data = new List(100); + var sb = Encoding.UTF8.GetBytes(s); + data.AddInt(sb.Length); + data.AddRange(sb); + return data.ToArray(); + } public static string GetString(this byte[] data) { return Encoding.UTF8.GetString(data); diff --git a/RageCoop.Core/Logging/Logger.cs b/RageCoop.Core/Logging/Logger.cs index 44df0d2..9320475 100644 --- a/RageCoop.Core/Logging/Logger.cs +++ b/RageCoop.Core/Logging/Logger.cs @@ -24,9 +24,16 @@ namespace RageCoop.Core.Logging public Logger(bool overwrite=true) { - if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); } LoggerThread=new Thread(() => { + if (!UseConsole) + { + while (LogPath==default) + { + Thread.Sleep(100); + } + if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); } + } while (!Stopping) { Flush(); diff --git a/RageCoop.Core/Packets/CustomEvent.cs b/RageCoop.Core/Packets/CustomEvent.cs new file mode 100644 index 0000000..a23a934 --- /dev/null +++ b/RageCoop.Core/Packets/CustomEvent.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Lidgren.Network; +namespace RageCoop.Core +{ + public partial class Packets + { + + public class CustomEvent : Packet + { + public int Hash { get; set; } + public List Args { get; set; } + + public override void Pack(NetOutgoingMessage message) + { + message.Write((byte)PacketTypes.CustomEvent); + + List result = new List(); + result.AddInt(Hash); + result.AddInt(Args.Count); + (byte, byte[]) tup; + foreach (var arg in Args) + { + tup=CoreUtils.GetBytesFromObject(arg); + if (tup.Item2==null) + { + throw new ArgumentException($"Object of type {arg.GetType()} is not supported"); + } + result.Add(tup.Item1); + result.AddRange(tup.Item2); + } + + message.Write(result.Count); + message.Write(result.ToArray()); + } + + public override void Unpack(byte[] array) + { + BitReader reader = new BitReader(array); + + Hash = reader.ReadInt(); + var len=reader.ReadInt(); + for (int i = 0; i < len; i++) + { + byte argType = reader.ReadByte(); + switch (argType) + { + case 0x01: + Args.Add(reader.ReadByte()); + break; + case 0x02: + Args.Add(reader.ReadShort()); + break; + case 0x03: + Args.Add(reader.ReadUShort()); + break; + case 0x04: + Args.Add(reader.ReadInt()); + break; + case 0x05: + Args.Add(reader.ReadUInt()); + break; + case 0x06: + Args.Add(reader.ReadLong()); + break; + case 0x07: + Args.Add(reader.ReadULong()); + break; + case 0x08: + Args.Add(reader.ReadFloat()); + break; + case 0x09: + Args.Add(reader.ReadBool()); + break; + case 0x10: + Args.Add(reader.ReadString()); + break; + } + } + } + } + } +} diff --git a/RageCoop.Core/Packets/Packets.cs b/RageCoop.Core/Packets/Packets.cs index 2f6d9e0..7478143 100644 --- a/RageCoop.Core/Packets/Packets.cs +++ b/RageCoop.Core/Packets/Packets.cs @@ -7,112 +7,6 @@ using GTA.Math; namespace RageCoop.Core { - /* - /// - /// - /// - public struct LVector3 - { - #region CLIENT-ONLY - /// - /// - /// - public Vector3 ToVector() - { - return new Vector3(X, Y, Z); - } - #endregion - #region SERVER-ONLY - public float Length() => (float)Math.Sqrt((X * X) + (Y * Y) + (Z * Z)); - public static LVector3 Subtract(LVector3 pos1, LVector3 pos2) { return new LVector3(pos1.X - pos2.X, pos1.Y - pos2.Y, pos1.Z - pos2.Z); } - public static bool Equals(LVector3 value1, LVector3 value2) => value1.X == value2.X && value1.Y == value2.Y && value1.Z == value2.Z; - public static LVector3 operator /(LVector3 value1, float value2) - { - float num = 1f / value2; - return new LVector3(value1.X * num, value1.Y * num, value1.Z * num); - } - public static LVector3 operator -(LVector3 left, LVector3 right) - { - return new LVector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); - } - public static LVector3 operator -(LVector3 value) - { - return default(LVector3) - value; - } - #endregion - /// - /// - /// - public LVector3(float X, float Y, float Z) - { - this.X = X; - this.Y = Y; - this.Z = Z; - } - - /// - /// - /// - public float X { get; set; } - - /// - /// - /// - public float Y { get; set; } - - /// - /// - /// - public float Z { get; set; } - } - - /// - /// - /// - public struct LQuaternion - { - #region CLIENT-ONLY - /// - /// - /// - public Quaternion ToQuaternion() - { - return new Quaternion(X, Y, Z, W); - } - #endregion - - /// - /// - /// - public LQuaternion(float X, float Y, float Z, float W) - { - this.X = X; - this.Y = Y; - this.Z = Z; - this.W = W; - } - - /// - /// - /// - public float X { get; set; } - - /// - /// - /// - public float Y { get; set; } - - /// - /// - /// - public float Z { get; set; } - - /// - /// - /// - public float W { get; set; } - } - */ public enum PacketTypes:byte { Handshake=0, @@ -131,7 +25,6 @@ namespace RageCoop.Core FileTransferComplete=17, CustomEvent = 18, - InvokeCustomEvent=19, #region Sync #region INTERVAL @@ -290,86 +183,6 @@ namespace RageCoop.Core } } - public class CustomEvent : Packet - { - public int Hash { get; set; } - public byte[] Data { get; set; } - - public override void Pack(NetOutgoingMessage message) - { - message.Write((byte)PacketTypes.CustomEvent); - - List byteArray = new List(); - - - // Write hash - byteArray.AddInt(Hash); - - // Write data - byteArray.AddRange(Data); - - byte[] result = byteArray.ToArray(); - - message.Write(result.Length); - message.Write(result); - } - - public override void Unpack(byte[] array) - { - BitReader reader = new BitReader(array); - - Hash = reader.ReadInt(); - - Data=reader.ReadByteArray(array.Length-4); - } - } - public class InvokeCustomEvent : Packet - { - public int Hash { get; set; } - public int[] Targets { get; set; } - public byte[] Data { get; set; } - - public override void Pack(NetOutgoingMessage message) - { - message.Write((byte)PacketTypes.InvokeCustomEvent); - - List byteArray = new List(); - - - // Write hash - byteArray.AddInt(Hash); - - // Write targets - byteArray.AddInt(Targets.Length); - foreach (var target in Targets) - { - byteArray.AddInt(target); - } - - // Write data - byteArray.AddRange(Data); - - byte[] result = byteArray.ToArray(); - - message.Write(result.Length); - message.Write(result); - } - - public override void Unpack(byte[] array) - { - BitReader reader = new BitReader(array); - - Hash = reader.ReadInt(); - - Targets = new int[reader.ReadInt()]; - for(int i = 0; i < Targets.Length; i++) - { - Targets[i]=reader.ReadInt(); - } - - Data=reader.ReadByteArray(array.Length-4); - } - } #region ===== NATIVECALL ===== public class NativeCall : Packet { diff --git a/RageCoop.Core/Packets/PedPackets.cs b/RageCoop.Core/Packets/PedPackets.cs index b5e1db0..d41d64d 100644 --- a/RageCoop.Core/Packets/PedPackets.cs +++ b/RageCoop.Core/Packets/PedPackets.cs @@ -81,7 +81,7 @@ namespace RageCoop.Core ModelHash = reader.ReadInt(); // Read player clothes - Clothes =reader.ReadByteArray(33); + Clothes =reader.ReadByteArray(36); // Read ped OwnerID OwnerID= reader.ReadInt(); diff --git a/RageCoop.Core/RageCoop.Core.csproj b/RageCoop.Core/RageCoop.Core.csproj index 6cd3d62..f485cc4 100644 --- a/RageCoop.Core/RageCoop.Core.csproj +++ b/RageCoop.Core/RageCoop.Core.csproj @@ -30,4 +30,8 @@ + + + + diff --git a/RageCoop.Core/Scripting/Events/BuiltInEvents.cs b/RageCoop.Core/Scripting/Events/BuiltInEvents.cs deleted file mode 100644 index 65bcd75..0000000 --- a/RageCoop.Core/Scripting/Events/BuiltInEvents.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RageCoop.Core.Scripting.Events -{ - public class OnVehicleSpawned:CustomEvent - { - public int VehicleID { get; set; } - public override int EventID { get; set; } = Hasher.Hash("RageCoop.OnVehicleSpawned"); - - public override byte[] Serialize() - { - return BitConverter.GetBytes(VehicleID); - } - public override void Deserialize(byte[] data) - { - VehicleID= BitConverter.ToInt32(data, 0); - } - } -} diff --git a/RageCoop.Core/Scripting/Events/CustomEvent.cs b/RageCoop.Core/Scripting/Events/CustomEvent.cs deleted file mode 100644 index ea5309f..0000000 --- a/RageCoop.Core/Scripting/Events/CustomEvent.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Security.Cryptography; - -namespace RageCoop.Core.Scripting.Events -{ - public interface ICustomEvent - { - int EventID { get; set; } - byte[] Serialize(); - void Deserialize(byte[] data); - } - public abstract class CustomEvent:ICustomEvent - { - public abstract int EventID { get; set; } - public abstract byte[] Serialize(); - public abstract void Deserialize(byte[] data); - } - public static class Hasher - { - public static int Hash(string s) - { - MD5 md5Hasher = MD5.Create(); - var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(s)); - return BitConverter.ToInt32(hashed, 0); - } - } -} diff --git a/RageCoop.Server/Client.cs b/RageCoop.Server/Client.cs index a1623aa..28a1edb 100644 --- a/RageCoop.Server/Client.cs +++ b/RageCoop.Server/Client.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using RageCoop.Core; using Lidgren.Network; using GTA.Math; -using RageCoop.Core.Scripting.Events; +using RageCoop.Core.Scripting; namespace RageCoop.Server { @@ -186,7 +186,7 @@ namespace RageCoop.Server Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default); } - public void SendCustomEvent(int id,byte[] data) + public void SendCustomEvent(int id,List args) { if (!IsReady) { @@ -201,7 +201,7 @@ namespace RageCoop.Server new Packets.CustomEvent() { Hash=id, - Data=data + Args=args }.Pack(outgoingMessage); Server.MainNetServer.SendMessage(outgoingMessage, Connection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Event); diff --git a/RageCoop.Server/Scripting/API.cs b/RageCoop.Server/Scripting/API.cs index ac0c39a..3757ce2 100644 --- a/RageCoop.Server/Scripting/API.cs +++ b/RageCoop.Server/Scripting/API.cs @@ -5,13 +5,18 @@ using System.Text; using System.Threading.Tasks; using Lidgren.Network; using RageCoop.Core; -using RageCoop.Core.Scripting.Events; using System.Net; namespace RageCoop.Server.Scripting { public static class API { + #region INTERNAL + internal static void InvokeCustomEvent(int hash,List args) + { + + } + #endregion public static class Events { #region DELEGATES @@ -194,26 +199,17 @@ namespace RageCoop.Server.Scripting } - public static void SendCustomEvent(int eventID, byte[] data, IEnumerable recepients) + public static void TriggerCustomEvent(int eventHash,List args,List targets=null) { - foreach(var c in recepients) + targets ??= new(Server.Clients.Values); + var p = new Packets.CustomEvent() { - c.SendCustomEvent(eventID,data); - } - } - public static void SendCustomEvent(CustomEvent e, Client[] recepients) - { - SendCustomEvent(e.EventID, e.Serialize(), recepients); - } - public static void SendCustomEvent(int eventID, byte[] data, int[] recepients) - { - if (recepients.Length==0) + Args=args, + Hash=eventHash + }; + foreach(var c in targets) { - SendCustomEvent(eventID,data,Server.Clients.Values); - } - else - { - SendCustomEvent(eventID,data, Server.Clients.Values.Where(x => recepients.Contains(x.Player.PedID)).ToArray()); + Server.Send(p,c,ConnectionChannel.Event,NetDeliveryMethod.ReliableOrdered); } } #endregion diff --git a/RageCoop.Server/Server.cs b/RageCoop.Server/Server.cs index 5f7b00d..f1f2848 100644 --- a/RageCoop.Server/Server.cs +++ b/RageCoop.Server/Server.cs @@ -328,13 +328,6 @@ namespace RageCoop.Server } } break; - case PacketTypes.InvokeCustomEvent: - { - var p = new Packets.InvokeCustomEvent(); - p.Unpack(data); - API.SendCustomEvent(p.Hash,p.Data,p.Targets); - } - break; case PacketTypes.FileTransferComplete: { Packets.FileTransferComplete packet = new Packets.FileTransferComplete();