StrawberryBF/src/Static/Assets.bf

71 lines
1.8 KiB
Brainfuck
Raw Normal View History

2020-06-15 11:48:01 +08:00
using System;
using System.Collections;
using System.IO;
namespace Strawberry
{
static public class Assets
{
static public Dictionary<String, Sprite> Sprites { get; private set; }
static public Dictionary<String, Font> Fonts { get; private set; }
2020-06-15 15:56:01 +08:00
static public String ContentRoot { get; private set; }
2020-06-15 11:48:01 +08:00
static public void LoadAll()
{
2020-06-15 15:56:01 +08:00
#if DEBUG
ContentRoot = "../../../src/Content/";
#else
ContentRoot = "Content";
#endif
2020-06-15 11:48:01 +08:00
Sprites = new Dictionary<String, Sprite>();
Load<Sprite>("Sprites", "*.ase*", Sprites);
Fonts = new Dictionary<String, Font>();
Load<Font>("Fonts", "*.ttf", Fonts);
}
static public void DisposeAll()
{
DeleteDictionaryAndKeysAndItems!(Sprites);
DeleteDictionaryAndKeysAndItems!(Fonts);
Sprite.[Friend]Dispose();
}
static private void Load<T>(String directory, String wildcard, Dictionary<String, T> putInto) where T : Asset
{
2020-06-15 15:56:01 +08:00
let root = scope String(ContentRoot);
2020-06-15 11:48:01 +08:00
root.Append(Path.DirectorySeparatorChar);
root.Append(directory);
if (Directory.Exists(root))
2020-06-15 15:56:01 +08:00
LoadDir<T>(root, root, wildcard, putInto);
2020-06-15 11:48:01 +08:00
else
2020-06-15 15:56:01 +08:00
Calc.Log("Create a Content/{0} folder to load {0}", directory);
2020-06-15 11:48:01 +08:00
}
2020-06-15 15:56:01 +08:00
static private void LoadDir<T>(String rootDir, String directory, String wildcard, Dictionary<String, T> putInto) where T : Asset
2020-06-15 11:48:01 +08:00
{
//Recursive folder search
for (let dir in Directory.EnumerateDirectories(directory))
{
let path = scope String();
dir.GetFilePath(path);
2020-06-15 15:56:01 +08:00
LoadDir<T>(rootDir, path, wildcard, putInto);
2020-06-15 11:48:01 +08:00
}
//Load files
for (let file in Directory.EnumerateFiles(directory, wildcard))
{
let path = scope String();
file.GetFilePath(path);
2020-06-15 15:56:01 +08:00
let asset = new [Friend]T(path);
2020-06-15 11:48:01 +08:00
2020-06-15 15:56:01 +08:00
path.Remove(0, rootDir.Length + 1);
2020-06-15 11:48:01 +08:00
path.RemoveFromEnd(path.Length - path.IndexOf('.'));
2020-06-15 15:56:01 +08:00
putInto.Add(new String(path), asset);
2020-06-15 11:48:01 +08:00
}
}
}
}