private static void AddContentManager(ContentManager contentManager) { lock (ContentManagerLock) { // Check if the list contains this content manager already. // Also take the opportunity to prune the list of any finalized content managers. bool contains = false; for (int i = ContentManagers.Count; i-- > 0;) { if (!ContentManagers[i].TryGetTarget(out var target)) { ContentManagers.RemoveAt(i); } if (ReferenceEquals(target, contentManager)) { contains = true; } } if (!contains) { ContentManagers.Add(new WeakReference <ContentManager>(contentManager)); } } }
} // SortedContentManagers #endregion #endregion #region Constructor /// <summary> /// The ContentManager manages the loaded assets. /// To use it you have to create an instance of this class and set the CurrentContentManager static /// property of this class so that references the newly created ContentManager instance. /// All the assets that you load latter will be automatically managed by this content manager. /// You can unload or dispose it. In any case the loaded assets will be disposed. /// </summary> public GameObjectContentManager() { // Create a unique ID Id = uniqueIdCounter; uniqueIdCounter++; Name = "Content Manager"; ContentManagers.Add(this); GameObjects = new List <GameObject>(); areContentManagersSorted = false; } // GameObjectContentManager
} // SortedContentManagers #endregion #endregion #region Constructor /// <summary> /// The ContentManager manages the loaded assets. /// To use it you have to create an instance of this class and set the CurrentContentManager static /// property of this class so that references the newly created ContentManager instance. /// All the assets that you load latter will be automatically managed by this content manager. /// You can unload or dispose it. In any case the loaded assets will be disposed. /// </summary> public AssetContentManager() { // Create a unique ID Id = uniqueIdCounter; uniqueIdCounter++; XnaContentManager = new Microsoft.Xna.Framework.Content.ContentManager(EngineManager.GameServices); Name = "Content Manager"; ContentManagers.Add(this); Assets = new List <Asset>(); areContentManagersSorted = false; } // AssetContentManager
public void UpdateToEngine() { ContentManagers.Clear(); foreach (var manager in FlatRedBall.FlatRedBallServices.ContentManagers) { ContentManagerViewModel managerViewModel = new ContentManagerViewModel(); managerViewModel.UpdateTo(manager); ContentManagers.Add(managerViewModel); } }
private static void RemoveContentManager(ContentManager contentManager) { lock (ContentManagerLock) { // Check if the list contains this content manager and remove it. // Also take the opportunity to prune the list of any finalized content managers. for (int i = ContentManagers.Count; i-- > 0;) { if (!ContentManagers[i].TryGetTarget(out var target) || ReferenceEquals(target, contentManager)) { ContentManagers.RemoveAt(i); } } } }
} // GameObjectContentManager #endregion #region Dispose /// <summary> /// Dispose managed resources. /// </summary> protected override void DisposeManagedResources() { ContentManagers.Remove(this); areContentManagersSorted = false; // Dispose assets List <GameObject> gameObjectsTemporalList = new List <GameObject>(); // An auxiliary list is needed because the original will be modified for each asset. gameObjectsTemporalList.AddRange(GameObjects); foreach (GameObject gameObject in gameObjectsTemporalList) { gameObject.Dispose(); } gameObjectsTemporalList.Clear(); // A collection of all generations could be a good idea at this point. // Besides the used managed memory indicates rational values when this is executed here. GarbageCollector.CollectGarbage(); } // DisposeManagedResources
internal static void ReloadGraphicsContent() { lock (ContentManagerLock) { // Reload the graphic assets of each content manager. // Also take the opportunity to prune the list of any finalized content managers. for (int i = ContentManagers.Count; i-- > 0;) { if (ContentManagers[i].TryGetTarget(out var target)) { target.ReloadGraphicsAssets(); } else { ContentManagers.RemoveAt(i); } } } }
} // CompareContentManagers #endregion #region Set Unique Name /// <summary> /// Set a unique texture name. /// </summary> public void SetUniqueName(string newName) { // Is the name unique? bool isUnique = ContentManagers.All(contentManagerFromList => contentManagerFromList == this || contentManagerFromList.Name != newName); if (isUnique) { if (name != newName) { name = newName; areContentManagersSorted = false; } } // If not then we add one to its name and search again to see if is unique. else { SetUniqueName(newName.PlusOne()); } } // SetUniqueName
} // AssetContentManager #endregion #region Dispose /// <summary> /// Dispose managed resources. /// </summary> protected override void DisposeManagedResources() { if (systemContentManager == this) { throw new InvalidOperationException("Content Manager: System Content Manager can not be disposed."); } XnaContentManager.Dispose(); ContentManagers.Remove(this); areContentManagersSorted = false; // Dispose assets List <Asset> assetsTemporalList = new List <Asset>(); // An auxiliary list is needed because the original will be modified for each asset. assetsTemporalList.AddRange(Assets); foreach (Asset asset in assetsTemporalList) { asset.ContentManager = null; asset.Dispose(); } assetsTemporalList.Clear(); // A collection of all generations could be a good idea at this point. // Besides the used managed memory indicates rational values when this is executed here. GarbageCollector.CollectGarbage(); } // DisposeManagedResources