///<summary> /// Returns serialized pluginExtraData from a device /// Low level function ///</summary> ///<param name="ped"></param> ///<param name="pedName"></param> ///<returns></returns> ///<remarks></remarks> public static object PedGet(clsPlugExtraData ped, string pedName) { if (ped == null) { return(null); } object value = ped.GetNamed(pedName); if (value == null || !(value is byte[])) { return(value); } byte[] byteObject = (byte[])value; if (!DeSerializeObject(ref byteObject, out object returnValue)) { if (DeSerializingWarnings) { string err = $"DeSerializing object: {pedName}"; Console.WriteLine(err); Log(err, LogType.Warning); } } return(returnValue); }
///<summary> /// Adds serialized PluginExtraData to a device, removes and adds if it already exists /// Low level function ///</summary> ///<param name="PED">The PED object to return data to (ByRef)</param> ///<param name="PEDName">The key to look for. Moskus: I only use the pl</param> ///<param name="PEDValue"></param> ///<remarks></remarks> public static void PedAdd <T>(ref clsPlugExtraData ped, string pedName, T pedValue) { if (ped == null) { ped = new clsPlugExtraData(); } SerializeObject(pedValue, out byte[] byteObject); bool ret1 = ped.RemoveNamed(pedName); bool ret2 = ped.AddNamed(pedName, byteObject); }
/// <summary> /// Convert GetAllPEDs to string /// </summary> /// <param name="ped"></param> /// <returns></returns> public static string GetAllPEDsStr(clsPlugExtraData ped) { string lst = ""; var peds = GetAllPEDs(ped); foreach (string name in peds.Keys) { string val = (peds[name] != null) ? peds[name].ToString() : ""; lst += $"{name}: {val}\n"; } return(lst); }
///<summary> /// Adds serialized PluginExtraData to a device, removes and adds if it already exists /// Low level function ///</summary> ///<param name="PED">The PED object to return data to (ByRef)</param> ///<param name="PEDName">The key to look for. Moskus: I only use the pl</param> ///<param name="PEDValue"></param> ///<remarks></remarks> public static clsPlugExtraData PedAdd(clsPlugExtraData ped, string pedName, object pedValue) { if (ped == null) { ped = new clsPlugExtraData(); } SerializeObject(pedValue, out byte[] byteObject); ped.RemoveNamed(pedName); ped.AddNamed(pedName, byteObject); return(ped); }
/// <summary> /// Get dictionary of all named peds, /// </summary> /// <param name="ped">Device PED</param> /// <param name="part">Part of the PED name to filter peds</param> /// <returns></returns> public static Dictionary <string, object> GetAllPEDs(clsPlugExtraData ped, string part = null) { var peds = new Dictionary <string, object>(); if (ped != null && ped.GetNamedKeys() != null) { foreach (string name in ped.GetNamedKeys()) { if (part == null || name.Contains(part.ToLower())) { peds[name] = PedGet(ped, name); } } } return(peds); }
/// <summary> /// Get named PED /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pedName"></param> /// <returns></returns> public static T GetPED <T>(string pedName, clsPlugExtraData ped, T deflt = default(T)) { object val = PedGet(ped, pedName); if (val == null) { val = deflt; } try { return((T)val); } catch { return(deflt); } }
/// <summary> /// Get named PED (nullable) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pedName"></param> /// <returns></returns> public static T GetPEDnull <T>(string pedName, clsPlugExtraData ped) where T : class { return(GetPED <T>(pedName, ped, deflt: (T)null)); }