protected virtual void UnregisterInstance() { lock (_GlobalLock) { GlobalInstances.Remove(Serial); } var user = User; if (user == null) { lock (_InstanceLock) { user = Instances.FirstOrDefault(kv => kv.Value.Contains(this)).Key; } } if (user == null) { return; } List <SuperGump> list; lock (_InstanceLock) { list = Instances.GetValue(User); if (list == null || list.Count == 0) { Instances.Remove(user); return; } } var removed = false; lock (((ICollection)list).SyncRoot) { if (list.Remove(this)) { list.TrimExcess(); removed = true; } if (list.Count == 0) { lock (_InstanceLock) { Instances.Remove(user); } } } if (removed) { OnInstanceUnregistered(); } }
private static void OnEncode0xB0_0xDD(NetState state, PacketReader reader, ref byte[] buffer, ref int length) { if (state == null || reader == null || buffer == null || length < 0) { return; } int pos = reader.Seek(0, SeekOrigin.Current); reader.Seek(3, SeekOrigin.Begin); int serial = reader.ReadInt32(); reader.Seek(pos, SeekOrigin.Begin); if (serial < 0 || !GlobalInstances.ContainsKey(serial)) { return; } SuperGump gump = GlobalInstances[serial]; if (!gump.Compiled) { gump.Refresh(true); } }
protected virtual void RegisterInstance() { if (User == null || User.Deleted || IsDisposed) { return; } lock (_GlobalLock) { GlobalInstances.AddOrReplace(Serial, this); } lock (_InstanceLock) { Instances.AddOrReplace( User, l => { l = l ?? new List <SuperGump>(10); l.AddOrReplace(this); return(l); }); } OnInstanceRegistered(); }
private static void OnEncode0xB0_0xDD(NetState state, PacketReader reader, ref byte[] buffer, ref int length) { if (state == null || reader == null || buffer == null || length < 0) { return; } var pos = reader.Seek(0, SeekOrigin.Current); reader.Seek(3, SeekOrigin.Begin); var serial = reader.ReadInt32(); reader.Seek(pos, SeekOrigin.Begin); if (serial <= 0) { return; } SuperGump g; lock (_GlobalLock) { g = GlobalInstances.GetValue(serial); } if (g != null && !g.Compiled) { g.Refresh(true); } }
public static T CreateInstance <T>() { var t = typeof(T); if (GlobalInstances.ContainsKey(t)) { return((T)GlobalInstances[t]); } return((T)CreateObject(t, new Dictionary <Type, object>())); }
void Awake() { if (Instance == null) { Instance = this; } else { Debug.Log("Warning: multiple " + this + " in scene!"); } }
protected virtual void RegisterInstance() { if (User == null || User.Deleted || IsDisposed) { return; } lock (GlobalInstances) { SuperGump gump; if (!GlobalInstances.TryGetValue(Serial, out gump)) { GlobalInstances.Add(Serial, this); } else if (gump != this) { GlobalInstances[Serial] = this; } } lock (Instances) { List <SuperGump> list; if (!Instances.TryGetValue(User, out list)) { Instances.Add( User, new List <SuperGump> { this }); } else if (list == null) { Instances[User] = new List <SuperGump> { this }; } else if (!list.Contains(this)) { list.Add(this); } } OnInstanceRegistered(); }
protected virtual void UnregisterInstance() { lock (GlobalInstances) { GlobalInstances.Remove(Serial); } bool removed = false; lock (Instances) { PlayerMobile user = User ?? Instances.FirstOrDefault(kv => kv.Value.Contains(this)).Key; if (user == null) { return; } List <SuperGump> list; if (!Instances.TryGetValue(user, out list)) { return; } if (list == null) { Instances.Remove(user); return; } if (list.Remove(this)) { removed = true; } if (list.Count == 0) { Instances.Remove(user); } } if (removed) { OnInstanceUnregistered(); } }
private static void OnEncode0xB0_0xDD(NetState state, PacketReader reader, ref byte[] buffer, ref int length) { if (state == null || reader == null || buffer == null || length < 0) { return; } var pos = reader.Seek(0, SeekOrigin.Current); reader.Seek(3, SeekOrigin.Begin); var serial = reader.ReadInt32(); reader.Seek(pos, SeekOrigin.Begin); if (serial <= 0) { return; } var cg = state.Gumps.FirstOrDefault(o => o != null && o.Serial == serial); if (cg == null || cg is SuperGump) { var g = cg as SuperGump; if (g == null) { lock (_GlobalLock) { g = GlobalInstances.GetValue(serial); } } if (g != null && !g.Compiled) { g.Refresh(true); } return; } if (CoreGumpSend != null && state.Mobile != null) { Timer.DelayCall(m => CoreGumpSend(m, cg), state.Mobile); } }
public static void Reset() { GlobalInstances.Clear(); GlobalMembers.Clear(); GlobalNames.Clear(); GlobalFactories.Clear(); Register <byte>(1); Register <sbyte>(1); Register <short>(1); Register <ushort>(1); Register <int>(1); Register <uint>(1); Register <long>(1); Register <ulong>(1); Register <float>(1.0f); Register <double>(1.0); Register("Foo"); Register(Guid.Empty); Register(new DateTime(2020, 1, 1)); }
private static object CreateObject(Type t, IDictionary <Type, object> instances) { if (instances.TryGetValue(t, out var existing) || GlobalInstances.TryGetValue(t, out existing)) { return(existing); } if (t.IsInterface) { if (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(IList <>) || t.GetGenericTypeDefinition() == typeof(IEnumerable <>))) { var result = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(t.GenericTypeArguments), 1); instances[t] = result; FillList(result, t, instances); return(result); } Debug.Assert(false, $"Can't create instance of interface {t}"); } if (IsListType(t)) { var result = (IList)Activator.CreateInstance(t, 1); instances[t] = result; FillList(result, t, instances); return(result); } if (t.IsClass || (t.IsValueType && !t.IsEnum)) { var ctors = t.GetConstructors().OrderBy(a => a.GetParameters().Length).ToList(); object result = null; if (ctors.Any()) { var parameters = ctors[0].GetParameters().Select(a => CreateObject(a.ParameterType, instances)).ToArray(); result = Activator.CreateInstance(t, parameters); } else if (GlobalFactories.TryGetValue(t, out var factory)) { result = factory(); } else { result = Activator.CreateInstance(t); Debug.Assert(result != null, $"Unable to create instance of {t}"); } instances[t] = result; foreach (var property in t.GetProperties()) { if (GlobalMembers.TryGetValue(Tuple.Create(property.DeclaringType, property.PropertyType, property.Name), out var value)) { property.SetValue(result, value); } else if (property.SetMethod != null) { property.SetValue(result, GlobalNames.TryGetValue((property.Name, property.PropertyType), out value) ? value : CreateObject(property.PropertyType, instances)); } else if (IsListType(property.PropertyType)) { var lst = (IList)property.GetValue(result); FillList(lst, property.PropertyType, instances); } } return(result); } return(instances[t] = Activator.CreateInstance(t)); }