using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace RageCoop.Core.Scripting
{
///
/// Describes how the event should be sent or processed
///
public enum CustomEventFlags : byte
{
None = 0,
///
/// Data will be encrypted and decrypted on target client
///
Encrypted = 1,
///
/// Event will be queued and fired in script thread, specify this flag if your handler will call native functions.
///
Queued = 2,
}
///
/// Struct to identify different event using hash
///
public struct CustomEventHash
{
private static readonly MD5 Hasher = MD5.Create();
private static readonly Dictionary Hashed = new Dictionary();
///
/// Hash value
///
public int Hash;
///
/// Create from hash
///
///
public static implicit operator CustomEventHash(int hash)
{
return new CustomEventHash() { Hash = hash };
}
///
/// Create from string
///
///
public static implicit operator CustomEventHash(string name)
{
return new CustomEventHash() { Hash = FromString(name) };
}
///
/// Get a Int32 hash of a string.
///
///
///
/// The exception is thrown when the name did not match a previously computed one and the hash was the same.
public static int FromString(string s)
{
var hash = BitConverter.ToInt32(Hasher.ComputeHash(Encoding.UTF8.GetBytes(s)), 0);
lock (Hashed)
{
if (Hashed.TryGetValue(hash, out string name))
{
if (name != s)
{
throw new ArgumentException($"Hashed value has collision with another name:{name}, hashed value:{hash}");
}
return hash;
}
Hashed.Add(hash, s);
return hash;
}
}
///
/// To int
///
///
public static implicit operator int(CustomEventHash h)
{
return h.Hash;
}
}
///
///
///
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";
///
/// Get event hash from string.
///
///
/// This method is obsoete, you should use implicit operator or
[Obsolete]
public static int Hash(string s)
{
return CustomEventHash.FromString(s);
}
}
}