public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObject objectCreate) { if (objectCheck == null) { throw new ArgumentNullException("objectCheck"); } this.objectCheck = objectCheck; this.NumberOfInvalidObjects = allocation; this.objects = new T[allocation]; ConstructorInfo constructor = typeof(T).GetConstructor(new Type[0]); for (int i = 0; i < allocation; i++) { if (objectCreate != null) { T t = objectCreate(); if (t == null) { throw new Exception("CreateNewObject delegate cannot return null."); } this.objects[i] = t; } else { if (constructor == null) { throw new Exception(string.Format("CreateNewObject delegate must be specified or T ({0}) must implement a parameterless constructor.", typeof(T))); } this.objects[i] = (T)constructor.Invoke((object[])null); } } }
public override void LoadContent() { font = FontManager.Instance.GetElementByString("font"); interactivObject = new InteractiveObject(); //interactivObject = InteractiveObjectDataManager.Instance.GetElementByString("Hansel"); //interactivObject.Position = new Vector2((EngineSettings.VirtualResWidth / 2 - interactivObject.Texture.Width / 2), (EngineSettings.VirtualResHeight / 2 - interactivObject.Texture.Height / 2)); createForm = new CreateNewObject(); }
private void NewObject() { LastCommand?.Deactivate(Application.Current.MainWindow); var o = CurrentScene.CreateNew(); LastCommand = new CreateNewObject(Application.Current.MainWindow, o); SelectedObject = o; CurrentScene.Redraw(); }
/// <summary> /// Get or create object data, search the free list and match by matchMethod. If match return the exist object. /// </summary> /// <param name="createNewObjectMethod">The method to generate new data.</param> /// <param name="paramObj">Parameter for create.</param> /// <param name="key">Key to realse T, put data to free list.</param> /// <param name="matchMethod">The method to match key. If null get the first object in the free list.</param> /// <param name="param">Parameter for matchMethod.</param> /// <returns></returns> public T GetObjectData(CreateNewObject createNewObjectMethod, object paramObj, out string key, MatchBoxKey matchMethod, object param) { T result = default(T); key = string.Empty; lock (syncObj) { // check the free list first. Use free PoolBox first. if (freeList.Count > 0) { result = CheckAndGetObjectData(param, matchMethod); } // then try to new a PoolBox if (result == null) { T data; if (BoxCount < Capacity) { if (createNewObjectMethod(paramObj, out data, out key)) { // matchMethod != null and datas has the match key value. if (!datas.ContainsKey(key)) { datas.Add(key, data); result = data; } else { // datas has the match key value. Cannot add to datas wait this object release to free list. WaitToGetObjectFromFreeList(createNewObjectMethod, paramObj, out key, matchMethod, param); } } else { var msg = string.Format("Fail to create object."); throw new Exception(msg); } } else { var msg = string.Format("The buffer is full, the object with key \"{0}\" cannot be added.", key); throw new Exception(msg); } } if (result == null && matchMethod == null) { // wait for one obj free. // No object in free list and datas is full. Just wait some object relase to free list. WaitToGetObjectFromFreeList(createNewObjectMethod, paramObj, out key, matchMethod, param); } } return(result); }
private void CreateFastConstructor() { if (mFastConstructor != null) return; DynamicMethod dm = new DynamicMethod("_CreateRecordFast_FH_RT_", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, typeof(object), new Type[] { typeof(object[]) }, mRecordType, true); ILGenerator generator = dm.GetILGenerator(); // generator.DeclareLocal(mRecordType); generator.Emit(OpCodes.Newobj, mRecordConstructor); generator.Emit(OpCodes.Ret); mFastConstructor = (CreateNewObject)dm.CreateDelegate(typeof(CreateNewObject)); }
//============================================================================= public void AddCallback(CreateNewObject icallback) { callback = icallback; }
private void AddObject(libFlags.TeamSign.Teams team) { if (GameData.UserObjectCount <= 0) { if (GameData.arrSelectedUserObjects.Count <= 0) { //提示未选中 return; } } TeamSign.Teams theOtherTeam; if (team == TeamSign.Teams.User) { theOtherTeam = TeamSign.Teams.Enemy; } else { theOtherTeam = TeamSign.Teams.User; } Grid gdOtherSide; Grid gdAddSide; if (team == TeamSign.Teams.User) { gdOtherSide = gdEnemyObjects; gdAddSide = gdUserObjects; } else { gdOtherSide = gdUserObjects; gdAddSide = gdEnemyObjects; } List <DSObject> arrAddSide; if (team == TeamSign.Teams.User) { arrAddSide = GameData.arrUserObjects; } else { arrAddSide = GameData.arrEnemyObjects; } bool isAdded = false; int remainObjectCount; if (team == TeamSign.Teams.User) { remainObjectCount = GameData.UserObjectCount; } else { remainObjectCount = GameData.EnemyObjectCount; } if (remainObjectCount != 0) { foreach (UIElement ui in gdAddSide.Children) { Border bdr = ui as Border; if (bdr != null) { if (bdr.Child == null) { DSObject objNew = new DSObject(TeamSign.Teams.User); bdr.Child = objNew.ObjectProperties.Bdr; arrAddSide.Add(objNew); if (team == TeamSign.Teams.User) { GameData.UserObjectCount = GameData.UserObjectCount - 1; } else { GameData.EnemyObjectCount = GameData.EnemyObjectCount - 1; } isAdded = true; break; } } } } else { foreach (UIElement ui in gdAddSide.Children) { Border bdr = ui as Border; if (bdr != null) { if (bdr.Child == null) { DSObject objNew = CreateNewObject.Create(GameData.arrSelectedUserObjects, TeamSign.Teams.User); bdr.Child = objNew.ObjectProperties.Bdr; arrAddSide.Add(objNew); isAdded = true; break; } } } } if (!isAdded) { //添加失败:己方已经过载 if (remainObjectCount != 0) { foreach (UIElement ui in gdOtherSide.Children) { Border bdr = ui as Border; if (bdr != null) { if (bdr.Child == null) { DSObject objNew = new DSObject(TeamSign.Teams.User); bdr.Child = objNew.ObjectProperties.Bdr; arrAddSide.Add(objNew); if (team == TeamSign.Teams.User) { GameData.UserObjectCount = GameData.UserObjectCount - 1; } else { GameData.EnemyObjectCount = GameData.EnemyObjectCount - 1; } isAdded = true; break; } } } } else { foreach (UIElement ui in gdOtherSide.Children) { Border bdr = ui as Border; if (bdr != null) { if (bdr.Child == null) { DSObject objNew = CreateNewObject.Create(GameData.arrSelectedUserObjects, TeamSign.Teams.User); bdr.Child = objNew.ObjectProperties.Bdr; arrAddSide.Add(objNew); isAdded = true; break; } } } } } if (!isAdded) { bdrObjectCount.Background = new SolidColorBrush(Colors.Gray); } //检查是否越界 foreach (Border bdr in gdOtherSide.Children) { if (bdr.Child != null) { Border bdrThis = (Border)bdr.Child; DSObject ds = (DSObject)bdrThis.Tag; if (ds.Team == team) { bool isOverride; if (team == TeamSign.Teams.User) { isOverride = GameData.IsUserOverride; } else { isOverride = GameData.IsEnemyOverride; } if (isOverride) { return; } else { OverrideSet.SetOn(team); if (team == TeamSign.Teams.User) { GameData.IsUserOverride = true; } else { GameData.IsEnemyOverride = true; } } } } } }
/// <summary> /// Get or create object data, if these has free data, take the first one. /// </summary> /// <param name="createNewObjectMethod">The method to generate new data.</param> /// <param name="param">Parameter for create.</param> /// <param name="key">Key to realse T, put data to free list.</param> /// <returns></returns> public T GetObjectData(CreateNewObject createNewObjectMethod, object param, out string key) { return(GetObjectData(createNewObjectMethod, param, out key, null, string.Empty)); }
private T WaitToGetObjectFromFreeList(CreateNewObject createNewObjectMethod, object paramObj, out string key, MatchBoxKey matchMethod, object param) { Monitor.Wait(syncObj); return(GetObjectData(createNewObjectMethod, paramObj, out key, matchMethod, param)); }