2018-02-24 11:10:56 +08:00
using System.Collections.Generic ;
using System.IO ;
2018-12-30 18:00:05 +08:00
using CustomNPCFramework.Framework.Enums ;
2018-02-24 11:10:56 +08:00
namespace CustomNPCFramework.Framework.Graphics
{
2018-12-30 18:00:05 +08:00
/// <summary>Used to hold assets from specified directories.</summary>
2018-02-24 11:10:56 +08:00
public class AssetManager
{
2018-12-30 18:00:05 +08:00
/// <summary>A list of all of the assets held by this asset manager.</summary>
2019-01-06 13:31:40 +08:00
public List < AssetSheet > assets { get ; } = new List < AssetSheet > ( ) ;
2018-02-24 11:10:56 +08:00
2019-01-06 13:31:40 +08:00
/// <summary>A list of directories managed by this asset manager, relative to the mod folder.</summary>
public Dictionary < string , string > relativePaths { get ; } = new Dictionary < string , string > ( ) ;
2018-03-04 20:36:46 +08:00
2018-12-30 18:00:05 +08:00
/// <summary>Default loading function from hardcoded paths.</summary>
2018-02-24 11:10:56 +08:00
public void loadAssets ( )
{
2019-01-06 13:31:40 +08:00
foreach ( var relativePath in this . relativePaths )
this . ProcessDirectory ( relativePath . Value ) ;
2018-03-04 20:36:46 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>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.</summary>
2019-01-06 13:31:40 +08:00
/// <param name="relativeDirPath">The relative directory path to process.</param>
2018-12-30 18:00:05 +08:00
/// <remarks>Taken from Microsoft c# documented webpages.</remarks>
2019-01-06 13:31:40 +08:00
private void ProcessDirectory ( string relativeDirPath )
2018-03-04 20:36:46 +08:00
{
2019-01-06 13:31:40 +08:00
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 ) ;
2018-12-30 18:00:05 +08:00
2018-03-04 20:36:46 +08:00
// Recurse into subdirectories of this directory.
2019-01-06 13:31:40 +08:00
foreach ( DirectoryInfo subdir in root . GetDirectories ( ) )
this . ProcessDirectory ( Path . Combine ( relativeDirPath , subdir . Name ) ) ;
2018-03-04 20:36:46 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Actually load in the asset information.</summary>
2019-01-06 13:31:40 +08:00
/// <param name="relativeFilePath">The relative path to the file to process.</param>
/// <param name="relativeDirPath">The relative path containing the file.</param>
private void ProcessFile ( string relativeFilePath , string relativeDirPath )
2018-03-04 20:36:46 +08:00
{
2018-03-04 23:53:55 +08:00
try
{
2019-01-06 13:31:40 +08:00
ExtendedAssetInfo info = ExtendedAssetInfo . readFromJson ( relativeFilePath ) ;
AssetSheet sheet = new AssetSheet ( info , relativeDirPath ) ;
2018-12-30 18:00:05 +08:00
this . addAsset ( sheet ) ;
2018-03-17 19:04:04 +08:00
Class1 . ModMonitor . Log ( "Loaded in new modular asset: " + info . assetName + " asset type: " + info . type ) ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
catch
2018-03-04 23:53:55 +08:00
{
2019-01-06 13:31:40 +08:00
AssetInfo info = AssetInfo . readFromJson ( relativeFilePath ) ;
AssetSheet sheet = new AssetSheet ( info , relativeDirPath ) ;
2018-12-30 18:00:05 +08:00
this . addAsset ( sheet ) ;
2018-03-04 23:53:55 +08:00
}
2018-02-24 11:10:56 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Add an asset to be handled from the asset manager.</summary>
/// <param name="asset">The asset sheet.</param>
2018-02-24 11:10:56 +08:00
public void addAsset ( AssetSheet asset )
{
this . assets . Add ( asset ) ;
}
2018-12-30 18:00:05 +08:00
/// <summary>Get an individual asset by its name.</summary>
/// <param name="s">The asset name.</param>
2018-02-24 11:10:56 +08:00
public AssetSheet getAssetByName ( string s )
{
2018-12-30 18:00:05 +08:00
foreach ( var v in this . assets )
2018-02-24 11:10:56 +08:00
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo . assetName = = s )
return v ;
2018-02-24 11:10:56 +08:00
}
return null ;
}
2018-12-30 18:00:05 +08:00
/// <summary>Add a new path to the asset manager and create the directory for it.</summary>
/// <param name="path">The absolute path to add.</param>
public void addPathCreateDirectory ( KeyValuePair < string , string > path )
2018-02-24 11:10:56 +08:00
{
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 ) ) ;
}
2018-12-30 18:00:05 +08:00
/// <summary>Add a path to the dictionary.</summary>
2019-01-06 13:31:40 +08:00
/// <param name="path">The relative path to add.</param>
2018-12-30 18:00:05 +08:00
private void addPath ( KeyValuePair < string , string > path )
2018-02-24 11:10:56 +08:00
{
2019-01-06 13:31:40 +08:00
this . relativePaths . Add ( path . Key , path . Value ) ;
2018-02-24 11:10:56 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Create appropriate directories for the path.</summary>
2018-02-24 11:10:56 +08:00
private void createDirectoriesFromPaths ( )
{
2019-01-06 13:31:40 +08:00
foreach ( var v in this . relativePaths )
2018-12-30 18:00:05 +08:00
Directory . CreateDirectory ( Path . Combine ( Class1 . ModHelper . DirectoryPath , v . Value ) ) ;
2018-02-24 11:10:56 +08:00
}
2018-03-04 23:53:55 +08:00
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="gender">The gender to match.</param>
2018-03-04 23:53:55 +08:00
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( Genders gender )
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
foreach ( var v in this . assets )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( info . gender = = gender )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="type">The part type to match.</param>
2018-03-04 23:53:55 +08:00
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( PartType type )
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
2018-03-04 23:53:55 +08:00
foreach ( var v in this . assets )
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( info . type = = type )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="gender">The gender to match.</param>
/// <param name="type">The part type to match.</param>
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( Genders gender , PartType type )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
2018-03-04 23:53:55 +08:00
foreach ( var v in this . assets )
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( info . type = = type & & info . gender = = gender )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="season">The season to match.</param>
2018-03-04 23:53:55 +08:00
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( Seasons season )
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
2018-03-04 23:53:55 +08:00
foreach ( var v in this . assets )
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
foreach ( var sea in info . seasons )
{
if ( sea = = season )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
break ; //Only need to find first validation that this is a valid asset.
}
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="gender">The gender to match.</param>
/// <param name="season">The season to match.</param>
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( Genders gender , Seasons season )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
2018-03-04 23:53:55 +08:00
foreach ( var v in this . assets )
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
foreach ( var sea in info . seasons )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( sea = = season & & info . gender = = gender )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
break ; //Only need to find first validation that this is a valid asset.
}
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Get a list of assets which match the given critera.</summary>
/// <param name="gender">The gender to match.</param>
/// <param name="season">The season to match.</param>
/// <param name="type">The part type to match.</param>
2018-03-04 23:53:55 +08:00
public List < AssetSheet > getListOfAssetsThatMatchThisCriteria ( Genders gender , Seasons season , PartType type )
{
2018-12-30 18:00:05 +08:00
List < AssetSheet > sheets = new List < AssetSheet > ( ) ;
2018-03-04 23:53:55 +08:00
foreach ( var v in this . assets )
{
2018-12-30 18:00:05 +08:00
if ( v . assetInfo is ExtendedAssetInfo info )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
foreach ( var sea in info . seasons )
2018-03-04 23:53:55 +08:00
{
2018-12-30 18:00:05 +08:00
if ( sea = = season & & info . gender = = gender & & info . type = = type )
sheets . Add ( v ) ;
2018-03-04 23:53:55 +08:00
}
}
}
2018-12-30 18:00:05 +08:00
return sheets ;
2018-03-04 23:53:55 +08:00
}
2018-02-24 11:10:56 +08:00
}
}