// files is an array as oppose to just 1 file because things like shaders consist of multiple files private T Load <T>(string[] files, ContentCreateOptions createOptions) where T : LoadableContent { object[] ctorParams; if (createOptions != null) { // Files + 1 for create options ctorParams = new object[files.Length + 1]; // Assign last element for create options ctorParams[ctorParams.Length - 1] = createOptions; } else { // No create options just allocate space for files ctorParams = new object[files.Length]; } // Assign files to constructor parameters for (int i = 0; i < files.Length; i++) { ctorParams[i] = files[i]; } // Check if we have this content cached before loading a new one LoadableContent cached = GetCachedContent(files, createOptions); if (cached != null) { return((T)cached); } // Create content instance from constructor parameters const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance; LoadableContent content = (LoadableContent)Activator.CreateInstance(typeof(T), bindingFlags, null, ctorParams, null); // Cache the new content _contentCache.Add(new ContentInfo(files, createOptions), content); string logString = $"Loaded {typeof(T).Name}"; logString = files.Aggregate(logString, (current, file) => current + $" '{file}'"); if (createOptions != null) { logString += $" {createOptions}"; } Engine.Get.Debug.Info(logString); return((T)content); }
private LoadableContent GetCachedContent(string[] files, ContentCreateOptions createOptions) { LoadableContent cached = null; ContentInfo? contentInfo = null; foreach (KeyValuePair <ContentInfo, LoadableContent> loadableContent in _contentCache) { if (loadableContent.Key.Files.SequenceEqual(files)) { if (createOptions != null) { if (loadableContent.Key.CreateOptions.IsEqualContentInternal(createOptions)) { cached = loadableContent.Value; contentInfo = loadableContent.Key; break; } } else { contentInfo = loadableContent.Key; cached = loadableContent.Value; break; } } } if (cached == null) { return(null); } if (cached.IsDisposed) { _contentCache.Remove(contentInfo.Value); return(null); } return(cached); }