2022-10-05 15:53:57 +08:00
using GTA.Native ;
using System ;
2022-06-23 09:46:38 +08:00
using System.Collections.Generic ;
using System.Security.Cryptography ;
2022-09-08 12:41:56 -07:00
using System.Text ;
2022-06-23 09:46:38 +08:00
namespace RageCoop.Core.Scripting
{
/// <summary>
2022-10-05 15:53:57 +08:00
/// Describes how the event should be sent or processed
2022-06-23 09:46:38 +08:00
/// </summary>
2022-10-05 15:53:57 +08:00
public enum CustomEventFlags : byte
{
None = 0 ,
/// <summary>
/// Data will be encrypted and decrypted on target client
/// </summary>
Encrypted = 1 ,
/// <summary>
/// Event will be queued and fired in script thread, specify this flag if your handler will call native functions.
/// </summary>
Queued = 2 ,
}
/// <summary>
/// Struct to identify different event using hash
/// </summary>
public struct CustomEventHash
2022-06-23 09:46:38 +08:00
{
2022-09-08 12:41:56 -07:00
private static readonly MD5 Hasher = MD5 . Create ( ) ;
private static readonly Dictionary < int , string > Hashed = new Dictionary < int , string > ( ) ;
2022-10-05 15:53:57 +08:00
/// <summary>
/// Hash value
/// </summary>
public int Hash ;
/// <summary>
/// Create from hash
/// </summary>
/// <param name="hash"></param>
public static implicit operator CustomEventHash ( int hash )
{
return new CustomEventHash ( ) { Hash = hash } ;
}
/// <summary>
/// Create from string
/// </summary>
/// <param name="name"></param>
public static implicit operator CustomEventHash ( string name )
{
return new CustomEventHash ( ) { Hash = FromString ( name ) } ;
}
2022-06-23 09:46:38 +08:00
/// <summary>
/// Get a Int32 hash of a string.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">The exception is thrown when the name did not match a previously computed one and the hash was the same.</exception>
2022-10-05 15:53:57 +08:00
public static int FromString ( string s )
2022-06-23 09:46:38 +08:00
{
var hash = BitConverter . ToInt32 ( Hasher . ComputeHash ( Encoding . UTF8 . GetBytes ( s ) ) , 0 ) ;
lock ( Hashed )
{
2022-09-08 12:41:56 -07:00
if ( Hashed . TryGetValue ( hash , out string name ) )
2022-06-23 09:46:38 +08:00
{
2022-09-08 12:41:56 -07:00
if ( name ! = s )
2022-06-23 09:46:38 +08:00
{
throw new ArgumentException ( $"Hashed value has collision with another name:{name}, hashed value:{hash}" ) ;
}
2022-09-05 13:02:09 +02:00
2022-06-23 09:46:38 +08:00
return hash ;
}
2022-09-05 13:02:09 +02:00
Hashed . Add ( hash , s ) ;
return hash ;
2022-06-23 09:46:38 +08:00
}
}
2022-10-05 15:53:57 +08:00
/// <summary>
/// To int
/// </summary>
/// <param name="h"></param>
public static implicit operator int ( CustomEventHash h )
{
return h . Hash ;
}
}
/// <summary>
///
/// </summary>
public static class CustomEvents
{
internal static readonly CustomEventHash OnPlayerDied = "RageCoop.OnPlayerDied" ;
internal static readonly CustomEventHash SetWeather = "RageCoop.SetWeather" ;
internal static readonly CustomEventHash OnPedDeleted = "RageCoop.OnPedDeleted" ;
internal static readonly CustomEventHash OnVehicleDeleted = "RageCoop.OnVehicleDeleted" ;
internal static readonly CustomEventHash SetAutoRespawn = "RageCoop.SetAutoRespawn" ;
internal static readonly CustomEventHash SetDisplayNameTag = "RageCoop.SetDisplayNameTag" ;
internal static readonly CustomEventHash NativeCall = "RageCoop.NativeCall" ;
internal static readonly CustomEventHash NativeResponse = "RageCoop.NativeResponse" ;
internal static readonly CustomEventHash AllResourcesSent = "RageCoop.AllResourcesSent" ;
internal static readonly CustomEventHash ServerPropSync = "RageCoop.ServerPropSync" ;
internal static readonly CustomEventHash ServerBlipSync = "RageCoop.ServerBlipSync" ;
internal static readonly CustomEventHash SetEntity = "RageCoop.SetEntity" ;
internal static readonly CustomEventHash DeleteServerProp = "RageCoop.DeleteServerProp" ;
internal static readonly CustomEventHash UpdatePedBlip = "RageCoop.UpdatePedBlip" ;
internal static readonly CustomEventHash DeleteEntity = "RageCoop.DeleteEntity" ;
internal static readonly CustomEventHash DeleteServerBlip = "RageCoop.DeleteServerBlip" ;
internal static readonly CustomEventHash CreateVehicle = "RageCoop.CreateVehicle" ;
internal static readonly CustomEventHash WeatherTimeSync = "RageCoop.WeatherTimeSync" ;
internal static readonly CustomEventHash IsHost = "RageCoop.IsHost" ;
2022-06-23 09:46:38 +08:00
}
}