/// <summary> /// Initializes static default content properties of type <typeparamref name="T"/> using an init method /// that can create or retrieve a matching <see cref="Resource"/> instance for each property name. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resourceCreator"></param> public static void InitType <T>(Func <string, T> resourceCreator) where T : Resource { string contentPathBase = Resource.DefaultContentBasePath + typeof(T).Name + ":"; TypeInfo resourceType = typeof(T).GetTypeInfo(); PropertyInfo[] defaultResProps = resourceType .DeclaredPropertiesDeep() .Where(p => p.IsPublic() && p.IsStatic() && typeof(IContentRef).GetTypeInfo().IsAssignableFrom(p.PropertyType.GetTypeInfo())) .ToArray(); for (int i = 0; i < defaultResProps.Length; i++) { string name = defaultResProps[i].Name; string contentPath = contentPathBase + name.Replace('_', ':'); T resource = resourceCreator(name); if (resource != null) { // Register the newly created default content globally ContentProvider.AddContent(contentPath, resource); defaultResProps[i].SetValue(null, ContentProvider.RequestContent <T>(contentPath)); } } }
/// <summary> /// Saves the Resource to the specified path. /// </summary> /// <param name="saveAsPath">The path to which this Resource is saved to. If null, the Resources <see cref="Path"/> is used as destination.</param> /// <param name="makePermanent"> /// When true, the Resource will be made permanently available from now on. If it has been generated at runtime /// or was loaded explicitly outside the ContentProvider, this will set the Resources <see cref="Path"/> Property /// and register it in the ContentProvider. If the Resource already is a permanent, this parameter will be ignored. /// </param> public void Save(string saveAsPath = null, bool makePermanent = true) { if (this.Disposed) { throw new ApplicationException("Can't save a Ressource that has been disposed."); } if (string.IsNullOrWhiteSpace(saveAsPath)) { saveAsPath = this.path; if (string.IsNullOrWhiteSpace(saveAsPath)) { throw new ArgumentException("Can't save a Resource to an undefined path.", "saveAsPath"); } } this.CheckedOnSaving(saveAsPath); // We're saving a new Ressource for the first time: Register it in the library if (makePermanent && string.IsNullOrWhiteSpace(this.path)) { this.path = saveAsPath; ContentProvider.AddContent(this.path, this); } string streamName; string dirName = System.IO.Path.GetDirectoryName(saveAsPath); if (!string.IsNullOrEmpty(dirName) && !Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } using (FileStream str = File.Open(saveAsPath, FileMode.Create)) { if (CompressOnSave) { SaveCompressed(str); } else { this.WriteToStream(str, out streamName); } } this.CheckedOnSaved(saveAsPath); }
/// <summary> /// Saves the Resource to the specified path. /// </summary> /// <param name="saveAsPath">The path to which this Resource is saved to. If null, the Resources <see cref="Path"/> is used as destination.</param> /// <param name="makePermanent"> /// When true, the Resource will be made permanently available from now on. If it has been generated at runtime /// or was loaded explicitly outside the ContentProvider, this will set the Resources <see cref="Path"/> Property /// and register it in the <see cref="ContentProvider"/>. If the Resource already is a permanent, this parameter will be ignored. /// </param> public void Save(string saveAsPath = null, bool makePermanent = true) { if (this.Disposed) { throw new ObjectDisposedException("Can't save a Resource that has been disposed."); } if (string.IsNullOrWhiteSpace(saveAsPath)) { saveAsPath = this.path; if (string.IsNullOrWhiteSpace(saveAsPath)) { throw new ArgumentException("Can't save a Resource to an undefined path.", "saveAsPath"); } } // Prepare saving the Resource and abort if an error occurred in the process bool preparedSuccessfully = this.CheckedOnSaving(saveAsPath); if (!preparedSuccessfully) { return; } // We're saving a new Resource for the first time: Register it in the library bool isPermanent = !string.IsNullOrWhiteSpace(this.path); if (makePermanent && !isPermanent) { this.path = saveAsPath; ContentProvider.AddContent(this.path, this); } string dirName = PathOp.GetDirectoryName(saveAsPath); if (!string.IsNullOrEmpty(dirName) && !DirectoryOp.Exists(dirName)) { DirectoryOp.Create(dirName); } using (Stream str = FileOp.Create(saveAsPath)) { this.WriteToStream(str); } this.CheckedOnSaved(saveAsPath); }