diff --git a/6 Terrain/TestScene3D.cs b/6 Terrain/TestScene3D.cs
index 12e231f10..245297184 100644
--- a/6 Terrain/TestScene3D.cs
+++ b/6 Terrain/TestScene3D.cs
@@ -83,6 +83,8 @@ public override void Initialize()
#region Models
+ string resources = @"Resources\Resources.zip";
+
Stopwatch sw = Stopwatch.StartNew();
string loadingText = null;
@@ -92,7 +94,7 @@ public override void Initialize()
sw.Restart();
this.cursor3D = this.AddModel(new ModelDescription()
{
- ContentPath = @"Resources\Resources.zip",
+ ContentPath = resources,
ModelFileName = "cursor.dae",
});
sw.Stop();
@@ -105,7 +107,7 @@ public override void Initialize()
sw.Restart();
this.terrain = this.AddTerrain(new TerrainDescription()
{
- ContentPath = @"Resources\Resources.zip",
+ ContentPath = resources,
ModelFileName = "two_levels.dae",
UseQuadtree = true,
UsePathFinding = true,
@@ -149,7 +151,7 @@ public override void Initialize()
sw.Restart();
this.helicopter = this.AddModel(new ModelDescription()
{
- ContentPath = @"Resources\Resources.zip",
+ ContentPath = resources,
ModelFileName = "helicopter.dae",
DropShadow = true,
TextureIndex = 2,
@@ -186,7 +188,7 @@ public override void Initialize()
sw.Restart();
this.tank = this.AddModel(new ModelDescription()
{
- ContentPath = @"Resources\Resources.zip",
+ ContentPath = resources,
ModelFileName = "tank.dae",
DropShadow = true,
});
@@ -208,7 +210,7 @@ public override void Initialize()
sw.Restart();
this.obelisk = this.AddInstancingModel(new ModelInstancedDescription()
{
- ContentPath = @"Resources\Resources.zip",
+ ContentPath = resources,
ModelFileName = "obelisk.dae",
DropShadow = true,
Instances = 4,
diff --git a/Engine/Common/ModelBase.cs b/Engine/Common/ModelBase.cs
index 80e87e0b7..61b48ac2c 100644
--- a/Engine/Common/ModelBase.cs
+++ b/Engine/Common/ModelBase.cs
@@ -1,13 +1,12 @@
-using System;
+using SharpDX;
+using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
-using SharpDX;
using ShaderResourceView = SharpDX.Direct3D11.ShaderResourceView;
namespace Engine.Common
{
using Engine.Content;
- using Engine.Helpers;
///
/// Model basic implementation
@@ -278,53 +277,7 @@ protected virtual void InitializeTextures(ModelContent modelContent)
{
ImageContent info = modelContent.Images[images];
- ShaderResourceView view = null;
-
- if (info.Stream != null)
- {
- byte[] buffer = info.Stream.GetBuffer();
-
- view = this.Game.Graphics.Device.LoadTexture(buffer);
- }
- else
- {
- if (info.IsArray)
- {
- if (info.Paths != null && info.Paths.Length > 0)
- {
- view = this.Game.Graphics.Device.LoadTextureArray(info.Paths);
- }
- else if (info.Streams != null && info.Streams.Length > 0)
- {
- view = this.Game.Graphics.Device.LoadTextureArray(info.Streams);
- }
- }
- else if (info.IsCubic)
- {
- int faceSize = info.CubicFaceSize;
-
- if (info.Path != null)
- {
- view = this.Game.Graphics.Device.LoadTextureCube(info.Path, faceSize);
- }
- else if (info.Stream != null)
- {
- view = this.Game.Graphics.Device.LoadTextureCube(info.Stream, faceSize);
- }
- }
- else
- {
- if (info.Path != null)
- {
- view = this.Game.Graphics.Device.LoadTexture(info.Path);
- }
- else if (info.Stream != null)
- {
- view = this.Game.Graphics.Device.LoadTexture(info.Stream);
- }
- }
- }
-
+ ShaderResourceView view = info.CreateResource(this.Game.Graphics.Device);
if (view != null)
{
this.Textures.Add(images, view);
diff --git a/Engine/Content/Compression.cs b/Engine/Content/Compression.cs
deleted file mode 100644
index c0929ded3..000000000
--- a/Engine/Content/Compression.cs
+++ /dev/null
@@ -1,110 +0,0 @@
-using System.Collections.Generic;
-using System;
-using System.IO;
-using System.IO.Compression;
-using System.Text.RegularExpressions;
-
-namespace Engine.Content
-{
- public static class Compression
- {
- public static string[] ReadIndex(string file)
- {
- List files = new List();
-
- using (ZipArchive archive = ZipFile.OpenRead(file))
- {
- foreach (var compressedFile in archive.Entries)
- {
- files.Add(compressedFile.Name);
- }
- }
-
- return files.ToArray();
- }
-
- public static bool Contains(string file, string name)
- {
- string[] entries = ReadIndex(file);
-
- return Array.Exists(entries, e => e.Equals(name, StringComparison.OrdinalIgnoreCase));
- }
-
- public static MemoryStream GetFile(string file, string name)
- {
- using (ZipArchive archive = ZipFile.OpenRead(file))
- {
- ZipArchiveEntry entry = archive.GetEntry(name);
-
- using (var stream = entry.Open())
- {
- MemoryStream ms = new MemoryStream();
-
- stream.CopyTo(ms);
-
- ms.Position = 0;
-
- return ms;
- }
- }
- }
-
- public static MemoryStream[] GetFiles(string file, string mask)
- {
- List res = new List();
-
- string regexMask = Regex.Escape(mask).Replace(@"\*", ".*").Replace(@"\?", ".");
-
- using (ZipArchive archive = ZipFile.OpenRead(file))
- {
- for (int i = 0; i < archive.Entries.Count; i++)
- {
- ZipArchiveEntry entry = archive.Entries[i];
-
- Match match = Regex.Match(entry.Name, regexMask);
- if (match.Success)
- {
- using (var stream = entry.Open())
- {
- MemoryStream ms = new MemoryStream();
-
- stream.CopyTo(ms);
-
- ms.Position = 0;
-
- res.Add(ms);
- }
- }
- }
- }
-
- return res.ToArray();
- }
-
- public static MemoryStream[] GetFiles(string file, string[] names)
- {
- MemoryStream[] res = new MemoryStream[names.Length];
-
- using (ZipArchive archive = ZipFile.OpenRead(file))
- {
- for (int i = 0; i < names.Length; i++)
- {
- ZipArchiveEntry entry = archive.GetEntry(names[i]);
-
- using (var stream = entry.Open())
- {
- MemoryStream ms = new MemoryStream();
-
- stream.CopyTo(ms);
-
- ms.Position = 0;
-
- res[i] = ms;
- }
- }
- }
-
- return res;
- }
- }
-}
diff --git a/Engine/Content/ContentManager.cs b/Engine/Content/ContentManager.cs
index 5872fdb9c..5404872a1 100644
--- a/Engine/Content/ContentManager.cs
+++ b/Engine/Content/ContentManager.cs
@@ -1,5 +1,8 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.IO;
+using System.IO.Compression;
+using System.Text.RegularExpressions;
namespace Engine.Content
{
@@ -8,6 +11,130 @@ namespace Engine.Content
///
public static class ContentManager
{
+ ///
+ /// Zip files manager
+ ///
+ static class ZipManager
+ {
+ ///
+ /// Reads zip file entry names
+ ///
+ /// Zip file name
+ /// Returns zip file entry names array
+ public static string[] ReadEntryNames(string file)
+ {
+ List files = new List();
+
+ using (ZipArchive archive = ZipFile.OpenRead(file))
+ {
+ foreach (var compressedFile in archive.Entries)
+ {
+ files.Add(compressedFile.Name);
+ }
+ }
+
+ return files.ToArray();
+ }
+ ///
+ /// Gets an entry name, comparing names using ordinal ignore case
+ ///
+ /// Zip file name
+ /// Entry name
+ /// Returns entry name if exists
+ public static string GetEntryName(string file, string entryName)
+ {
+ string[] entries = ReadEntryNames(file);
+
+ return Array.Find(entries, e => e.Equals(entryName, StringComparison.OrdinalIgnoreCase));
+ }
+ ///
+ /// Gets if an entry name eixts into the zip file, comparing names using ordinal ignore case
+ ///
+ /// Zip file name
+ /// Entry name
+ /// Returns true if the entry exists
+ public static bool Contains(string file, string entryName)
+ {
+ string entry = GetEntryName(file, entryName);
+
+ return !string.IsNullOrEmpty(entry);
+ }
+ ///
+ /// Gets file stream of the entry
+ ///
+ /// Zip file name
+ /// Entry name
+ /// Returns file stream of the entry
+ public static MemoryStream GetFile(string file, string entryName)
+ {
+ using (ZipArchive archive = ZipFile.OpenRead(file))
+ {
+ ZipArchiveEntry entry = archive.GetEntry(GetEntryName(file, entryName));
+
+ using (var stream = entry.Open())
+ {
+ return stream.WriteToMemory();
+ }
+ }
+ }
+ ///
+ /// Gets file stream of the entry pattern
+ ///
+ /// Zip file name
+ /// Entry name
+ /// Returns file streams for the entry pattern
+ public static MemoryStream[] GetFiles(string file, string pattern)
+ {
+ List res = new List();
+
+ string regexMask = Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".");
+
+ using (ZipArchive archive = ZipFile.OpenRead(file))
+ {
+ for (int i = 0; i < archive.Entries.Count; i++)
+ {
+ ZipArchiveEntry entry = archive.Entries[i];
+
+ Match match = Regex.Match(entry.Name, regexMask);
+ if (match.Success)
+ {
+ using (var stream = entry.Open())
+ {
+ res.Add(stream.WriteToMemory());
+ }
+ }
+ }
+ }
+
+ return res.ToArray();
+ }
+ ///
+ /// Gets file streams of the entry list
+ ///
+ /// Zip file name
+ /// Entry names list
+ /// Returns file streams of the entry list
+ public static MemoryStream[] GetFiles(string file, string[] entryNames)
+ {
+ MemoryStream[] res = new MemoryStream[entryNames.Length];
+
+ using (ZipArchive archive = ZipFile.OpenRead(file))
+ {
+ for (int i = 0; i < entryNames.Length; i++)
+ {
+ ZipArchiveEntry entry = archive.GetEntry(GetEntryName(file, entryNames[i]));
+
+ using (var stream = entry.Open())
+ {
+ res[i] = stream.WriteToMemory();
+ }
+ }
+ }
+
+ return res;
+ }
+ }
+
///
/// Finds content
///
@@ -15,7 +142,7 @@ public static class ContentManager
/// Resource path
/// Returns resource paths found
///
- /// Content source could be a folder or a zip file
+ /// Content source can be a folder or a zip file
/// If not unique file found, searchs pattern "[filename]*[extension]" and returns result array
///
public static MemoryStream[] FindContent(string contentSource, string resourcePath)
@@ -26,7 +153,7 @@ public static MemoryStream[] FindContent(string contentSource, string resourcePa
}
else if (File.Exists(resourcePath))
{
- return new[] { ReadToMemory(resourcePath) };
+ return new[] { resourcePath.WriteToMemory() };
}
else
{
@@ -36,7 +163,7 @@ public static MemoryStream[] FindContent(string contentSource, string resourcePa
resourcePath = Path.Combine(contentSource, resourcePath);
if (File.Exists(resourcePath))
{
- return new[] { ReadToMemory(resourcePath) };
+ return new[] { resourcePath.WriteToMemory() };
}
else
{
@@ -45,7 +172,14 @@ public static MemoryStream[] FindContent(string contentSource, string resourcePa
Path.GetFileNameWithoutExtension(resourcePath) + "*" + Path.GetExtension(resourcePath));
if (files != null && files.Length > 0)
{
- return ReadToMemory(files);
+ MemoryStream[] msList = new MemoryStream[files.Length];
+
+ for (int i = 0; i < files.Length; i++)
+ {
+ msList[i] = files[i].WriteToMemory();
+ }
+
+ return msList;
}
else
{
@@ -56,13 +190,13 @@ public static MemoryStream[] FindContent(string contentSource, string resourcePa
else if (File.Exists(contentSource))
{
//Compressed file
- if (Compression.Contains(contentSource, resourcePath))
+ if (ZipManager.Contains(contentSource, resourcePath))
{
- return new[] { Compression.GetFile(contentSource, resourcePath) };
+ return new[] { ZipManager.GetFile(contentSource, resourcePath) };
}
else
{
- MemoryStream[] res = Compression.GetFiles(contentSource, Path.GetFileNameWithoutExtension(resourcePath) + "*" + Path.GetExtension(resourcePath));
+ MemoryStream[] res = ZipManager.GetFiles(contentSource, Path.GetFileNameWithoutExtension(resourcePath) + "*" + Path.GetExtension(resourcePath));
if (res != null && res.Length > 0)
{
return res;
@@ -86,7 +220,7 @@ public static MemoryStream[] FindContent(string contentSource, string resourcePa
/// Resource path list
/// Returns resource path list
///
- /// Content source could be a folder or a zip file
+ /// Content source can be a folder or a zip file
///
public static MemoryStream[] FindContent(string contentSource, string[] resourcePaths)
{
@@ -110,31 +244,5 @@ public static MemoryStream[] FindContent(string contentSource, string[] resource
return res.ToArray();
}
-
- private static MemoryStream ReadToMemory(string file)
- {
- using (var stream = File.OpenRead(file))
- {
- MemoryStream ms = new MemoryStream();
-
- stream.CopyTo(ms);
-
- ms.Position = 0;
-
- return ms;
- }
- }
-
- private static MemoryStream[] ReadToMemory(string[] files)
- {
- MemoryStream[] msList = new MemoryStream[files.Length];
-
- for (int i = 0; i < files.Length; i++)
- {
- msList[i] = ReadToMemory(files[i]);
- }
-
- return msList;
- }
}
}
diff --git a/Engine/Content/ImageContent.cs b/Engine/Content/ImageContent.cs
index 21dae1bd1..c9a4c1e6d 100644
--- a/Engine/Content/ImageContent.cs
+++ b/Engine/Content/ImageContent.cs
@@ -1,7 +1,11 @@
using System.IO;
+using Device = SharpDX.Direct3D11.Device;
+using ShaderResourceView = SharpDX.Direct3D11.ShaderResourceView;
namespace Engine.Content
{
+ using Engine.Helpers;
+
///
/// Image content
///
@@ -172,6 +176,63 @@ public static ImageContent Cubic(MemoryStream texture, int faceSize)
};
}
+ ///
+ /// Generate the resource view
+ ///
+ /// Graphics device
+ /// Returns the created resource view
+ public ShaderResourceView CreateResource(Device device)
+ {
+ ShaderResourceView view = null;
+
+ if (this.Stream != null)
+ {
+ byte[] buffer = this.Stream.GetBuffer();
+
+ view = device.LoadTexture(buffer);
+ }
+ else
+ {
+ if (this.IsArray)
+ {
+ if (this.Paths != null && this.Paths.Length > 0)
+ {
+ view = device.LoadTextureArray(this.Paths);
+ }
+ else if (this.Streams != null && this.Streams.Length > 0)
+ {
+ view = device.LoadTextureArray(this.Streams);
+ }
+ }
+ else if (this.IsCubic)
+ {
+ int faceSize = this.CubicFaceSize;
+
+ if (this.Path != null)
+ {
+ view = device.LoadTextureCube(this.Path, faceSize);
+ }
+ else if (this.Stream != null)
+ {
+ view = device.LoadTextureCube(this.Stream, faceSize);
+ }
+ }
+ else
+ {
+ if (this.Path != null)
+ {
+ view = device.LoadTexture(this.Path);
+ }
+ else if (this.Stream != null)
+ {
+ view = device.LoadTexture(this.Stream);
+ }
+ }
+ }
+
+ return view;
+ }
+
///
/// Gets text representation of instance
///
diff --git a/Engine/Engine.csproj b/Engine/Engine.csproj
index daf229881..21740fa9a 100644
--- a/Engine/Engine.csproj
+++ b/Engine/Engine.csproj
@@ -316,7 +316,6 @@
-
diff --git a/Engine/Helper.cs b/Engine/Helper.cs
index d4d5d3ffc..628cc6b5b 100644
--- a/Engine/Helper.cs
+++ b/Engine/Helper.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using SharpDX;
+using System.IO;
namespace Engine
{
@@ -307,5 +308,32 @@ public static string Join(this ICollection list, string separator = "")
return dictionary.Select(item => item.Key);
}
+ ///
+ /// Writes stream to memory
+ ///
+ /// Stream
+ /// Returns a memory stream
+ public static MemoryStream WriteToMemory(this Stream stream)
+ {
+ MemoryStream ms = new MemoryStream();
+
+ stream.CopyTo(ms);
+
+ ms.Position = 0;
+
+ return ms;
+ }
+ ///
+ /// Writes file to memory
+ ///
+ /// File name
+ /// Returns a memory stream
+ public static MemoryStream WriteToMemory(this string fileName)
+ {
+ using (var stream = File.OpenRead(fileName))
+ {
+ return stream.WriteToMemory();
+ }
+ }
}
}