using System.Collections.Generic; using System.IO; using CustomNPCFramework.Framework.Enums; namespace CustomNPCFramework.Framework.Graphics { /// Used to hold assets from specified directories. public class AssetManager { /// A list of all of the assets held by this asset manager. public List assets { get; } = new List(); /// A list of directories managed by this asset manager, relative to the mod folder. public Dictionary relativePaths { get; } = new Dictionary(); /// Default loading function from hardcoded paths. public void loadAssets() { foreach (var relativePath in this.relativePaths) this.ProcessDirectory(relativePath.Value); } /// Process all .json files in the given directory. If there are more nested directories, keep digging to find more .json files. Also allows us to specify a broader directory like Content/Grahphics/ModularNPC/Hair to have multiple hair styles. /// The relative directory path to process. /// Taken from Microsoft c# documented webpages. private void ProcessDirectory(string relativeDirPath) { DirectoryInfo root = new DirectoryInfo(Path.Combine(Class1.ModHelper.DirectoryPath, relativeDirPath)); foreach (FileInfo file in root.GetFiles("*.json")) this.ProcessFile(Path.Combine(relativeDirPath, file.Name), relativeDirPath); // Recurse into subdirectories of this directory. foreach (DirectoryInfo subdir in root.GetDirectories()) this.ProcessDirectory(Path.Combine(relativeDirPath, subdir.Name)); } /// Actually load in the asset information. /// The relative path to the file to process. /// The relative path containing the file. private void ProcessFile(string relativeFilePath, string relativeDirPath) { try { ExtendedAssetInfo info = ExtendedAssetInfo.readFromJson(relativeFilePath); AssetSheet sheet = new AssetSheet(info, relativeDirPath); this.addAsset(sheet); Class1.ModMonitor.Log("Loaded in new modular asset: " + info.assetName + " asset type: " + info.type); } catch { AssetInfo info = AssetInfo.readFromJson(relativeFilePath); AssetSheet sheet = new AssetSheet(info, relativeDirPath); this.addAsset(sheet); } } /// Add an asset to be handled from the asset manager. /// The asset sheet. public void addAsset(AssetSheet asset) { this.assets.Add(asset); } /// Get an individual asset by its name. /// The asset name. public AssetSheet getAssetByName(string s) { foreach (var v in this.assets) { if (v.assetInfo.assetName == s) return v; } return null; } /// Add a new path to the asset manager and create the directory for it. /// The absolute path to add. public void addPathCreateDirectory(KeyValuePair path) { this.addPath(path); string dir = Path.Combine(Class1.ModHelper.DirectoryPath, path.Value); if (!Directory.Exists(dir)) Directory.CreateDirectory(Path.Combine(Class1.ModHelper.DirectoryPath, path.Value)); } /// Add a path to the dictionary. /// The relative path to add. private void addPath(KeyValuePair path) { this.relativePaths.Add(path.Key, path.Value); } /// Create appropriate directories for the path. private void createDirectoriesFromPaths() { foreach (var v in this.relativePaths) Directory.CreateDirectory(Path.Combine(Class1.ModHelper.DirectoryPath, v.Value)); } /// Get a list of assets which match the given critera. /// The gender to match. public List getListOfAssetsThatMatchThisCriteria(Genders gender) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { if (info.gender == gender) sheets.Add(v); } } return sheets; } /// Get a list of assets which match the given critera. /// The part type to match. public List getListOfAssetsThatMatchThisCriteria(PartType type) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { if (info.type == type) sheets.Add(v); } } return sheets; } /// Get a list of assets which match the given critera. /// The gender to match. /// The part type to match. public List getListOfAssetsThatMatchThisCriteria(Genders gender, PartType type) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { if (info.type == type && info.gender == gender) sheets.Add(v); } } return sheets; } /// Get a list of assets which match the given critera. /// The season to match. public List getListOfAssetsThatMatchThisCriteria(Seasons season) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { foreach (var sea in info.seasons) { if (sea == season) sheets.Add(v); break; //Only need to find first validation that this is a valid asset. } } } return sheets; } /// Get a list of assets which match the given critera. /// The gender to match. /// The season to match. public List getListOfAssetsThatMatchThisCriteria(Genders gender, Seasons season) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { foreach (var sea in info.seasons) { if (sea == season && info.gender == gender) sheets.Add(v); break; //Only need to find first validation that this is a valid asset. } } } return sheets; } /// Get a list of assets which match the given critera. /// The gender to match. /// The season to match. /// The part type to match. public List getListOfAssetsThatMatchThisCriteria(Genders gender, Seasons season, PartType type) { List sheets = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo info) { foreach (var sea in info.seasons) { if (sea == season && info.gender == gender && info.type == type) sheets.Add(v); } } } return sheets; } } }