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 { public class AssetManager { public List assets; public Dictionary paths; /// /// Basic constructor. /// public AssetManager() { this.assets = new List(); this.paths = new Dictionary(); } 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); } private void ProcessFile(string file,string path) { try { ExtendedAssetInfo info = ExtendedAssetInfo.readFromJson(file); AssetSheet sheet = new AssetSheet(info, path); addAsset(sheet); } 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; } 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; } 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; } 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) { if (sea == season && (v.assetInfo as ExtendedAssetInfo).gender == gender && (v.assetInfo as ExtendedAssetInfo).type == type) aSheet.Add(v); break; //Only need to find first validation that this is a valid asset. } } } return aSheet; } } }