wow, its like resource management!
git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1063 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
41
OpenRa.Core/ResourceCache.cs
Normal file
41
OpenRa.Core/ResourceCache.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
static class ResourceCache
|
||||
{
|
||||
static Dictionary<string, IResource> items = new Dictionary<string, IResource>();
|
||||
|
||||
public static void Flush()
|
||||
{
|
||||
items.Clear();
|
||||
}
|
||||
|
||||
public static IResource Get(string filename)
|
||||
{
|
||||
IResource r;
|
||||
if (!items.TryGetValue(filename, out r))
|
||||
items.Add(filename, r = Load(filename));
|
||||
return r;
|
||||
}
|
||||
|
||||
static IResource Load(string filename)
|
||||
{
|
||||
Converter<Stream, IResource> loader =
|
||||
ResourceLoader.GetLoader(Path.GetExtension(filename));
|
||||
|
||||
if (loader == null)
|
||||
return null;
|
||||
|
||||
Stream s = FileSystem.GetItem(filename);
|
||||
|
||||
if (s == null)
|
||||
return null;
|
||||
|
||||
return loader(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user