private IEnumerable <RawContentPack> GetContentPacks(IMigration[] migrations) { this.Monitor.VerboseLog("Preloading content packs..."); foreach (IContentPack contentPack in this.Helper.ContentPacks.GetOwned()) { RawContentPack rawContentPack; try { // validate content.json has required fields ContentConfig content = contentPack.ReadJsonFile <ContentConfig>(this.PatchFileName); if (content == null) { this.Monitor.Log($"Ignored content pack '{contentPack.Manifest.Name}' because it has no {this.PatchFileName} file.", LogLevel.Error); continue; } if (content.Format == null || content.Changes == null) { this.Monitor.Log($"Ignored content pack '{contentPack.Manifest.Name}' because it doesn't specify the required {nameof(ContentConfig.Format)} or {nameof(ContentConfig.Changes)} fields.", LogLevel.Error); continue; } // apply migrations IMigration migrator = new AggregateMigration(content.Format, this.SupportedFormatVersions, migrations); if (!migrator.TryMigrate(content, out string error)) { this.Monitor.Log($"Loading content pack '{contentPack.Manifest.Name}' failed: {error}.", LogLevel.Error); continue; } // init rawContentPack = new RawContentPack(new ManagedContentPack(contentPack), content, migrator); } catch (Exception ex) { this.Monitor.Log($"Error preloading content pack '{contentPack.Manifest.Name}'. Technical details:\n{ex}", LogLevel.Error); continue; } yield return(rawContentPack); } }
/// <summary>Parse the underlying <c>content.json</c> file and set the <see cref="Content"/> and <see cref="Migrator"/> fields.</summary> /// <param name="error">The error indicating why the content could not be reloaded, if applicable.</param> public bool TryReloadContent(out string error) { const string filename = "content.json"; // load raw file ContentConfig content = this.ContentPack.ReadJsonFile <ContentConfig>(filename); if (content == null) { error = $"content pack has no {filename} file"; return(false); } // validate base fields if (content.Format == null) { error = $"content pack doesn't specify the required {nameof(ContentConfig.Format)} field."; return(false); } if (!content.Changes.Any() && !content.CustomLocations.Any()) { error = $"content pack must specify the {nameof(ContentConfig.Changes)} or {nameof(ContentConfig.CustomLocations)} fields."; return(false); } // apply migrations IMigration migrator = new AggregateMigration(content.Format, this.GetMigrations(content)); if (!migrator.TryMigrate(content, out error)) { return(false); } // load content this.ContentImpl = content; this.MigratorImpl = migrator; return(true); }