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:
29
OpenRa.Core/FileSystem.cs
Normal file
29
OpenRa.Core/FileSystem.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
public static class FileSystem
|
||||
{
|
||||
static List<IMountable> packages = new List<IMountable>();
|
||||
|
||||
public static void Mount(IMountable package)
|
||||
{
|
||||
packages.Add(package);
|
||||
}
|
||||
|
||||
internal static Stream GetItem(string filename)
|
||||
{
|
||||
foreach (IMountable package in packages)
|
||||
{
|
||||
Stream s = package.GetItem(filename);
|
||||
if (s != null)
|
||||
return s;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
OpenRa.Core/IMountable.cs
Normal file
12
OpenRa.Core/IMountable.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
public interface IMountable
|
||||
{
|
||||
Stream GetItem(string filename);
|
||||
}
|
||||
}
|
||||
53
OpenRa.Core/OpenRa.Core.csproj
Normal file
53
OpenRa.Core/OpenRa.Core.csproj
Normal file
@@ -0,0 +1,53 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>8.0.50727</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{1B60782F-B2DD-43F1-B51D-B798485F317C}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>OpenRa.Core</RootNamespace>
|
||||
<AssemblyName>OpenRa.Core</AssemblyName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="FileSystem.cs" />
|
||||
<Compile Include="IMountable.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Reflect.cs" />
|
||||
<Compile Include="Resource.cs" />
|
||||
<Compile Include="ResourceBindingAttribute.cs" />
|
||||
<Compile Include="ResourceCache.cs" />
|
||||
<Compile Include="ResourceLoader.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
17
OpenRa.Core/Properties/AssemblyInfo.cs
Normal file
17
OpenRa.Core/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("OpenRa.Core")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("OpenRa.Core")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2007")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
18
OpenRa.Core/Reflect.cs
Normal file
18
OpenRa.Core/Reflect.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
static class Reflect
|
||||
{
|
||||
public static T GetAttribute<T>(Type t)
|
||||
where T : Attribute
|
||||
{
|
||||
T[] attribs = (T[])t.GetCustomAttributes(typeof(T), false);
|
||||
if (attribs == null || attribs.Length == 0)
|
||||
return null;
|
||||
return attribs[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
17
OpenRa.Core/Resource.cs
Normal file
17
OpenRa.Core/Resource.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
public interface IResource { }
|
||||
|
||||
public abstract class Resource<T> : IResource
|
||||
where T : Resource<T>
|
||||
{
|
||||
public static T Get(string filename)
|
||||
{
|
||||
return (T)ResourceCache.Get(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
OpenRa.Core/ResourceBindingAttribute.cs
Normal file
16
OpenRa.Core/ResourceBindingAttribute.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
public class ResourceBindingAttribute : Attribute
|
||||
{
|
||||
internal readonly string[] Extensions;
|
||||
|
||||
public ResourceBindingAttribute(params string[] extensions)
|
||||
{
|
||||
Extensions = extensions;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
55
OpenRa.Core/ResourceLoader.cs
Normal file
55
OpenRa.Core/ResourceLoader.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace OpenRa.Core
|
||||
{
|
||||
static class ResourceLoader
|
||||
{
|
||||
static Dictionary<string, Converter<Stream, IResource>> loaders =
|
||||
new Dictionary<string,Converter<Stream,IResource>>();
|
||||
|
||||
static ResourceLoader()
|
||||
{
|
||||
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
|
||||
BindTypes(a);
|
||||
|
||||
AppDomain.CurrentDomain.AssemblyLoad +=
|
||||
delegate(object unused, AssemblyLoadEventArgs e) { BindTypes(e.LoadedAssembly); };
|
||||
}
|
||||
|
||||
static void BindTypes(Assembly a)
|
||||
{
|
||||
foreach (Type t in a.GetTypes())
|
||||
BindType(t);
|
||||
}
|
||||
|
||||
static void BindType(Type t)
|
||||
{
|
||||
ResourceBindingAttribute a = Reflect.GetAttribute<ResourceBindingAttribute>(t);
|
||||
if (a == null)
|
||||
return;
|
||||
|
||||
ConstructorInfo ctor = t.GetConstructor(new Type[] { typeof(Stream) });
|
||||
if (ctor == null)
|
||||
return;
|
||||
|
||||
Converter<Stream, IResource> loader = delegate(Stream s)
|
||||
{
|
||||
return (IResource)ctor.Invoke(new object[] { s });
|
||||
};
|
||||
|
||||
foreach (string extension in a.Extensions)
|
||||
loaders.Add(extension, loader);
|
||||
}
|
||||
|
||||
public static Converter<Stream, IResource> GetLoader(string extension)
|
||||
{
|
||||
Converter<Stream, IResource> result;
|
||||
loaders.TryGetValue(extension.ToLowerInvariant(), out result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
OpenRa.sln
12
OpenRa.sln
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShpViewer", "ShpViewer\ShpV
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.FileFormats", "OpenRa.FileFormats\OpenRa.FileFormats.csproj", "{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.Core", "OpenRa.Core\OpenRa.Core.csproj", "{1B60782F-B2DD-43F1-B51D-B798485F317C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -59,6 +61,16 @@ Global
|
||||
{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}.Release|Win32.ActiveCfg = Release|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Debug|Win32.ActiveCfg = Debug|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{1B60782F-B2DD-43F1-B51D-B798485F317C}.Release|Win32.ActiveCfg = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user