using CustomNPCFramework.Framework.Enums; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; 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; /// /// A list of all of the directories managed by this asset manager. /// public Dictionary paths; /// /// Basic constructor. /// public AssetManager() { this.assets = new List(); this.paths = new Dictionary(); } /// /// Constructor. /// /// A list of all directories to be managed by the asset manager. Name, path is the key pair value. public AssetManager(Dictionary assetsPathsToLoadFrom) { this.assets = new List(); this.paths = assetsPathsToLoadFrom; } /// /// Default loading function from hard coded paths. /// public void loadAssets() { foreach(var path in this.paths) { ProcessDirectory(path.Value); } } /// /// Taken from Microsoft c# documented webpages. /// 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. /// /// private void ProcessDirectory(string targetDirectory) { // Process the list of files found in the directory. string[] files = Directory.GetFiles(targetDirectory, "*.json"); foreach (var file in files) { ProcessFile(file,targetDirectory); } // Recurse into subdirectories of this directory. string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach (string subdirectory in subdirectoryEntries) ProcessDirectory(subdirectory); } /// /// Actually load in the asset information. /// /// /// private void ProcessFile(string file,string path) { try { ExtendedAssetInfo info = ExtendedAssetInfo.readFromJson(file); AssetSheet sheet = new AssetSheet(info, path); addAsset(sheet); Class1.ModMonitor.Log("Loaded in new modular asset: " + info.assetName + " asset type: " + info.type); } catch (Exception err) { AssetInfo info = AssetInfo.readFromJson(file); AssetSheet sheet = new AssetSheet(info, path); addAsset(sheet); } } /// /// Add an asset to be handled from the asset manager. /// /// public void addAsset(AssetSheet asset) { this.assets.Add(asset); } /// /// Get an individual asset by its name. /// /// /// public AssetSheet getAssetByName(string s) { foreach(var v in assets) { if (v.assetInfo.assetName == s) return v; } return null; } /// /// Add a new path to the asset manager and create the directory for it. /// /// 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. /// /// private void addPath(KeyValuePair path) { this.paths.Add(path.Key, path.Value); } /// /// Create appropriate directories for the path. /// private void createDirectoriesFromPaths() { foreach(var v in paths) { Directory.CreateDirectory(Path.Combine(Class1.ModHelper.DirectoryPath,v.Value)); } } /// /// Returns a list of assets from this manager that match the given critera. /// /// The criteria we are searching for this time is gender. /// public List getListOfAssetsThatMatchThisCriteria(Genders gender) { List aSheet = new List(); foreach(var v in this.assets) { if(v.assetInfo is ExtendedAssetInfo) { if ((v.assetInfo as ExtendedAssetInfo).gender == gender) aSheet.Add(v); } } return aSheet; } /// /// Get a list of all the assets of this kind of part. /// /// The type of part to return a list of from this asset manager. /// public List getListOfAssetsThatMatchThisCriteria(PartType type) { List aSheet = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo) { if ((v.assetInfo as ExtendedAssetInfo).type == type) aSheet.Add(v); } } return aSheet; } /// /// Get a list of assets that match the critera. /// /// The gender criteria for this asset, such as if this part belongs to either a female or male character. /// The type of asset to return. Hair eyes, shoes, etc. /// public List getListOfAssetsThatMatchThisCriteria(Genders gender,PartType type) { List aSheet = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo) { if ((v.assetInfo as ExtendedAssetInfo).type == type && (v.assetInfo as ExtendedAssetInfo).gender == gender) aSheet.Add(v); } } return aSheet; } /// /// Returns a list of assets from this manager that match the given critera. /// /// The criteria we are searching for this time is gender. /// public List getListOfAssetsThatMatchThisCriteria(Seasons season) { List aSheet = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo) { foreach (var sea in (v.assetInfo as ExtendedAssetInfo).seasons) { if (sea == season) aSheet.Add(v); break; //Only need to find first validation that this is a valid asset. } } } return aSheet; } /// /// Get a list of assets that match this criteria of gender and seasons. /// /// /// /// public List getListOfAssetsThatMatchThisCriteria(Genders gender,Seasons season) { List aSheet = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo) { foreach (var sea in (v.assetInfo as ExtendedAssetInfo).seasons) { if (sea == season && (v.assetInfo as ExtendedAssetInfo).gender==gender) aSheet.Add(v); break; //Only need to find first validation that this is a valid asset. } } } return aSheet; } /// /// Get a list of asssets that match certain critera. /// /// The gengder certain assets belong to, such as male or female. /// The season that an asset can be displayed in. Good for seasonal assets. /// The type of part to return a list of such as hair, eyes, or pants. /// public List getListOfAssetsThatMatchThisCriteria(Genders gender, Seasons season, PartType type) { List aSheet = new List(); foreach (var v in this.assets) { if (v.assetInfo is ExtendedAssetInfo) { foreach (var sea in (v.assetInfo as ExtendedAssetInfo).seasons) { //Class1.ModMonitor.Log("Searching: seasons"); if (sea == season && (v.assetInfo as ExtendedAssetInfo).gender == gender && (v.assetInfo as ExtendedAssetInfo).type == type) { aSheet.Add(v); } else { //Class1.ModMonitor.Log("Not what I was looking for."); } } } } //Class1.ModMonitor.Log("ok it's over: "+aSheet.Count.ToString()); return aSheet; } } }