/// <summary> /// Retrieve a TimeSupplier by id or create a CustomTimeSupplier if it doesn't exist. /// </summary> /// <param name="id"></param> /// <returns></returns> public static T GetOrCreate <T>(string id) where T : class, ITimeSupplier { if (id == null) { return(null); } ITimeSupplier ts; if (_registeredTimeSuppliers.TryGetValue(id, out ts)) { return(ts as T); } else { if (typeof(T).IsAssignableFrom(typeof(CustomTimeSupplier))) { var ct = new CustomTimeSupplier(id); _registeredTimeSuppliers[id] = ct; if (_customTimeSuppliers.Count == 0) { GameLoop.RegisterInternalEarlyUpdate(SPTime.Update); } _customTimeSuppliers.Add(ct); return(ct as T); } else { throw new System.ArgumentException(string.Format("Supplied type '{0}' can not have a CustomTimeSupplier auto created for it.", typeof(T).FullName)); } } }
/// <summary> /// Retrieve a CustomTimeSupplier by name. /// </summary> /// <param name="id"></param> /// <param name="createIfNotExists"></param> /// <returns></returns> public static CustomTimeSupplier Custom(string id, bool createIfNotExists = false) { if (id == null) { return(null); } if (_customTimes == null) { if (createIfNotExists) { _customTimes = new Dictionary <string, CustomTimeSupplier>(); GameLoop.RegisterInternalEarlyUpdate(SPTime.Update); var ct = new CustomTimeSupplier(id); _customTimes[ct.Id] = ct; return(ct); } else { return(null); } } else { CustomTimeSupplier ct; if (_customTimes.TryGetValue(id, out ct)) { return(ct); } else if (createIfNotExists) { ct = new CustomTimeSupplier(id); _customTimes[ct.Id] = ct; return(ct); } else { return(null); } } }
public static void RegisterCustom(ITimeSupplier supplier) { if (supplier == null) { throw new System.ArgumentNullException("supplier"); } string id = supplier.Id; if (_registeredTimeSuppliers.ContainsKey(id)) { throw new System.ArgumentException(string.Format("A timesupplier with id '{0}' already exists.", id)); } _registeredTimeSuppliers[id] = supplier; if (supplier is CustomTimeSupplier) { if (_customTimeSuppliers.Count == 0) { GameLoop.RegisterInternalEarlyUpdate(SPTime.Update); } _customTimeSuppliers.Add(supplier as ICustomTimeSupplier); } }