2018-12-30 18:00:05 +08:00
|
|
|
using System;
|
2018-03-06 12:46:45 +08:00
|
|
|
using System.Collections.Generic;
|
2019-07-16 17:09:42 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using StardewModdingAPI;
|
2018-03-06 12:46:45 +08:00
|
|
|
|
|
|
|
namespace StardustCore.UIUtilities
|
|
|
|
{
|
|
|
|
public class TextureManager
|
|
|
|
{
|
2019-07-16 17:09:42 +08:00
|
|
|
public static Dictionary<string, Dictionary<string, TextureManager>> TextureManagers = new Dictionary<string, Dictionary<string, TextureManager>>();
|
|
|
|
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public Dictionary<string, Texture2DExtended> textures;
|
2018-03-06 12:46:45 +08:00
|
|
|
|
2019-07-16 17:09:42 +08:00
|
|
|
public string name;
|
|
|
|
|
|
|
|
public TextureManager(string Name)
|
|
|
|
{
|
|
|
|
this.name = Name;
|
|
|
|
this.textures = new Dictionary<string, Texture2DExtended>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TextureManager(string Name, IContentPack ContentPack)
|
2018-03-06 12:46:45 +08:00
|
|
|
{
|
2019-07-16 17:09:42 +08:00
|
|
|
this.name = Name;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.textures = new Dictionary<string, Texture2DExtended>();
|
2018-03-06 12:46:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
public void addTexture(string name, Texture2DExtended texture)
|
2018-03-06 12:46:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.textures.Add(name, texture);
|
2018-03-06 12:46:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Returns a Texture2DExtended held by the manager.</summary>
|
2018-03-06 12:46:45 +08:00
|
|
|
public Texture2DExtended getTexture(string name)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var v in this.textures)
|
2018-03-06 12:46:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (v.Key == name)
|
|
|
|
return v.Value.Copy();
|
2018-03-06 12:46:45 +08:00
|
|
|
}
|
2019-07-16 17:09:42 +08:00
|
|
|
throw new Exception("Error, texture name not found!!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Content pack search.
|
|
|
|
/// </summary>
|
|
|
|
public void searchForTextures(IContentPack content, string RelativePath)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(RelativePath))
|
|
|
|
{
|
|
|
|
string path = content.DirectoryPath;
|
|
|
|
this.searchDirectories(path, "", content);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string path = Path.Combine(content.DirectoryPath, RelativePath);
|
|
|
|
this.searchDirectories(path, RelativePath, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Non-Content pack search.
|
|
|
|
/// </summary>
|
|
|
|
public void searchForTextures(string RelativePath = "")
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(RelativePath))
|
|
|
|
{
|
|
|
|
string path = ModCore.ModHelper.DirectoryPath;
|
|
|
|
this.searchDirectories(path, "", ModCore.Manifest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string path = Path.Combine(ModCore.ModHelper.DirectoryPath, RelativePath);
|
|
|
|
this.searchDirectories(path, RelativePath, ModCore.Manifest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Searches for textures in the given path. Used to search for textures in extended mods.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ABSPath">The absolute starting path to start looking</param>
|
|
|
|
/// <param name="RelativePath"></param>
|
|
|
|
public void searchForTextures(string ABSPath,IModHelper helper,IManifest manifest,string RelativePath = "")
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(RelativePath))
|
|
|
|
{
|
|
|
|
string path = ABSPath;
|
|
|
|
this.searchDirectories(path, "", manifest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string path = Path.Combine(ABSPath, RelativePath);
|
|
|
|
this.searchDirectories(path, RelativePath, manifest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Used to search through a mod's directories for textures.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="helper">The mod's SMAPI helper object.</param>
|
|
|
|
/// <param name="manifest">The mod's SMAPI manifest object.</param>
|
|
|
|
/// <param name="RelativePath">The relative starting path.</param>
|
|
|
|
public void searchForTextures(IModHelper helper, IManifest manifest, string RelativePath = "")
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(RelativePath))
|
|
|
|
{
|
|
|
|
string path = helper.DirectoryPath;
|
|
|
|
this.searchDirectories(path, "",helper,manifest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string path = Path.Combine(helper.DirectoryPath, RelativePath);
|
|
|
|
this.searchDirectories(path, RelativePath,helper ,manifest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Used to search the directories of a mod that has been extended from Stardust Core.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path to the manifest's directory path</param>
|
|
|
|
/// <param name="relativePath"></param>
|
|
|
|
/// <param name="helper"></param>
|
|
|
|
/// <param name="manifest"></param>
|
|
|
|
private void searchDirectories(string path, string relativePath, IModHelper helper,IManifest manifest)
|
|
|
|
{
|
|
|
|
string[] dirs = Directory.GetDirectories(path);
|
|
|
|
string[] files = Directory.GetFiles(path);
|
|
|
|
|
|
|
|
string[] extensions = new string[2]
|
|
|
|
{
|
|
|
|
".png",
|
|
|
|
".jpg"
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (string directory in dirs)
|
|
|
|
{
|
|
|
|
string combo = string.IsNullOrEmpty(relativePath) ? Path.GetFileName(directory) : Path.Combine(relativePath, Path.GetFileName(directory));
|
|
|
|
this.searchDirectories(directory, combo, helper,manifest);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (string file in files)
|
|
|
|
{
|
|
|
|
if (extensions.Contains(Path.GetExtension(file)))
|
|
|
|
{
|
|
|
|
this.processFoundTexture(file, relativePath,helper,manifest);
|
|
|
|
//StardustCore.ModCore.ModMonitor.Log("Found a texture!: "+Path.GetFileNameWithoutExtension(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void searchDirectories(string path, string relativePath, IManifest manifest)
|
|
|
|
{
|
|
|
|
string[] dirs = Directory.GetDirectories(path);
|
|
|
|
string[] files = Directory.GetFiles(path);
|
|
|
|
|
|
|
|
string[] extensions = new string[2]
|
|
|
|
{
|
|
|
|
".png",
|
|
|
|
".jpg"
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (string directory in dirs)
|
|
|
|
{
|
|
|
|
string combo = string.IsNullOrEmpty(relativePath) ? Path.GetFileName(directory) : Path.Combine(relativePath, Path.GetFileName(directory));
|
|
|
|
this.searchDirectories(directory, combo, manifest);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (string file in files)
|
|
|
|
{
|
|
|
|
if (extensions.Contains(Path.GetExtension(file)))
|
|
|
|
{
|
|
|
|
this.processFoundTexture(file, relativePath);
|
|
|
|
//StardustCore.ModCore.ModMonitor.Log("Found a texture!: "+Path.GetFileNameWithoutExtension(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
private void searchDirectories(string path, string relativePath, IContentPack ContentPack)
|
|
|
|
{
|
|
|
|
string[] dirs = Directory.GetDirectories(path);
|
|
|
|
string[] files = Directory.GetFiles(path);
|
|
|
|
|
|
|
|
string[] extensions = new string[2]
|
|
|
|
{
|
|
|
|
".png",
|
|
|
|
".jpg"
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (string directory in dirs)
|
|
|
|
{
|
|
|
|
string combo = string.IsNullOrEmpty(relativePath) ? Path.GetFileName(directory) : Path.Combine(relativePath, Path.GetFileName(directory));
|
|
|
|
this.searchDirectories(directory, combo, ContentPack);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (string file in files)
|
|
|
|
{
|
|
|
|
if (extensions.Contains(Path.GetExtension(file)))
|
|
|
|
{
|
|
|
|
this.processFoundTexture(file, relativePath, ContentPack);
|
|
|
|
//StardustCore.ModCore.ModMonitor.Log("Found a texture!: " + Path.GetFileNameWithoutExtension(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processFoundTexture(string file, string relativePath)
|
|
|
|
{
|
|
|
|
Texture2DExtended textureExtended = new Texture2DExtended(ModCore.ModHelper, ModCore.Manifest, Path.Combine(relativePath, Path.GetFileName(file)));
|
|
|
|
|
|
|
|
ModCore.log("Found texture: " + textureExtended.Name);
|
|
|
|
|
|
|
|
textureExtended.texture.Name = ModCore.Manifest.UniqueID + "_" + this.name + "_" + textureExtended.Name;
|
|
|
|
|
|
|
|
this.addTexture(textureExtended.Name, textureExtended);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processFoundTexture(string file,string relativePath ,IModHelper Helper, IManifest Manifest)
|
|
|
|
{
|
|
|
|
Texture2DExtended textureExtended = new Texture2DExtended(Helper, Manifest, Path.Combine(relativePath, Path.GetFileName(file)));
|
|
|
|
|
|
|
|
ModCore.log("Found texture: " + textureExtended.Name);
|
|
|
|
|
|
|
|
textureExtended.texture.Name = Manifest.UniqueID + "_" + this.name + "_" + textureExtended.Name;
|
|
|
|
|
|
|
|
this.addTexture(textureExtended.Name, textureExtended);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processFoundTexture(string file, string relativePath, IContentPack ContentPack)
|
|
|
|
{
|
|
|
|
Texture2DExtended textureExtended = new Texture2DExtended(ContentPack, Path.Combine(relativePath, Path.GetFileName(file)));
|
|
|
|
|
|
|
|
textureExtended.texture.Name = ContentPack.Manifest.UniqueID + "_" + this.name + "_" + textureExtended.Name;
|
|
|
|
ModCore.log("Found texture: " + textureExtended.Name);
|
|
|
|
|
|
|
|
//this.addTexture(textureExtended.Name, textureExtended);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
|
|
|
// Static Functions //
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
|
|
|
|
|
|
|
public static void addTexture(IManifest ModManifest, string managerName, string textureName, Texture2DExtended Texture)
|
|
|
|
{
|
|
|
|
Texture.texture.Name = managerName + "." + textureName;
|
|
|
|
TextureManagers[ModManifest.UniqueID][managerName].addTexture(textureName, Texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void AddTextureManager(IManifest ModManifest, string Name)
|
|
|
|
{
|
|
|
|
if (TextureManager.TextureManagers.ContainsKey(ModManifest.UniqueID))
|
|
|
|
{
|
|
|
|
TextureManagers[ModManifest.UniqueID].Add(Name, new TextureManager(Name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TextureManager.TextureManagers.Add(ModManifest.UniqueID, new Dictionary<string, TextureManager>());
|
|
|
|
TextureManagers[ModManifest.UniqueID].Add(Name, new TextureManager(Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextureManager GetTextureManager(IManifest Manifest, string Name)
|
|
|
|
{
|
|
|
|
return TextureManagers[Manifest.UniqueID][Name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Texture2D GetTexture(IManifest Manifest, string ManagerName, string TextureName)
|
|
|
|
{
|
|
|
|
return GetTextureManager(Manifest, ManagerName).getTexture(TextureName).texture;
|
|
|
|
}
|
|
|
|
public static Texture2DExtended GetExtendedTexture(IManifest Manifest, string ManagerName, string TextureName)
|
|
|
|
{
|
|
|
|
return GetTextureManager(Manifest, ManagerName).getTexture(TextureName);
|
2018-03-06 12:46:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|