UltimateZoneBuilder/Source/Data/DirectoryReader.cs

248 lines
7 KiB
C#
Raw Normal View History

2007-10-01 20:57:41 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
2007-10-01 20:57:41 +00:00
using CodeImp.DoomBuilder.IO;
#endregion
namespace CodeImp.DoomBuilder.Data
{
internal sealed class DirectoryReader : PK3StructuredReader
2007-10-01 20:57:41 +00:00
{
#region ================== Variables
private DirectoryFilesList files;
#endregion
2009-01-02 14:23:57 +00:00
#region ================== Constructor / Disposer
2007-10-01 20:57:41 +00:00
// Constructor
2007-10-07 22:21:47 +00:00
public DirectoryReader(DataLocation dl) : base(dl)
2007-10-01 20:57:41 +00:00
{
2007-10-07 22:21:47 +00:00
General.WriteLogLine("Opening directory resource '" + location.location + "'");
2009-01-02 14:23:57 +00:00
// Initialize
files = new DirectoryFilesList(dl.location, true);
Initialize();
2009-01-02 14:23:57 +00:00
2007-10-01 20:57:41 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2009-01-02 14:23:57 +00:00
// Disposer
2007-10-07 22:21:47 +00:00
public override void Dispose()
2007-10-01 20:57:41 +00:00
{
// Not already disposed?
if(!isdisposed)
{
2009-01-02 14:23:57 +00:00
General.WriteLogLine("Closing directory resource '" + location.location + "'");
2007-10-07 22:21:47 +00:00
2007-10-01 20:57:41 +00:00
// Done
2007-10-07 22:21:47 +00:00
base.Dispose();
2007-10-01 20:57:41 +00:00
}
}
2009-01-02 14:23:57 +00:00
#endregion
#region ================== Textures
// This finds and returns a patch stream
public override Stream GetPatchData(string pname)
{
// Error when suspended
if(issuspended) throw new Exception("Data reader is suspended");
// Find in any of the wad files
// Note the backward order, because the last wad's images have priority
for(int i = wads.Count - 1; i >= 0; i--)
{
Stream data = wads[i].GetPatchData(pname);
if(data != null) return data;
}
// Find in patches directory
string path = Path.Combine(PATCHES_DIR, Path.GetDirectoryName(pname));
string filename = FindFirstFile(path, Path.GetFileName(pname), true);
if((filename != null) && FileExists(filename))
{
return LoadFile(filename);
}
// Nothing found
return null;
}
// This finds and returns a textue stream
public override Stream GetTextureData(string pname)
{
// Error when suspended
if(issuspended) throw new Exception("Data reader is suspended");
// Find in any of the wad files
// Note the backward order, because the last wad's images have priority
for(int i = wads.Count - 1; i >= 0; i--)
{
Stream data = wads[i].GetTextureData(pname);
if(data != null) return data;
}
// Find in patches directory
string path = Path.Combine(TEXTURES_DIR, Path.GetDirectoryName(pname));
string filename = FindFirstFile(path, Path.GetFileName(pname), true);
if((filename != null) && FileExists(filename))
{
return LoadFile(filename);
}
// Nothing found
return null;
}
#endregion
#region ================== Sprites
// This finds and returns a sprite stream
public override Stream GetSpriteData(string pname)
{
// Error when suspended
if(issuspended) throw new Exception("Data reader is suspended");
// Find in any of the wad files
for(int i = wads.Count - 1; i >= 0; i--)
{
Stream sprite = wads[i].GetSpriteData(pname);
if(sprite != null) return sprite;
}
// Find in sprites directory
string path = Path.Combine(SPRITES_DIR, Path.GetDirectoryName(pname));
string filename = FindFirstFile(path, Path.GetFileName(pname), true);
if((filename != null) && FileExists(filename))
{
return LoadFile(filename);
}
// Nothing found
return null;
}
// This checks if the given sprite exists
public override bool GetSpriteExists(string pname)
{
// Error when suspended
if(issuspended) throw new Exception("Data reader is suspended");
// Find in any of the wad files
for(int i = wads.Count - 1; i >= 0; i--)
{
if(wads[i].GetSpriteExists(pname)) return true;
}
// Find in sprites directory
string path = Path.Combine(SPRITES_DIR, Path.GetDirectoryName(pname));
string filename = FindFirstFile(path, Path.GetFileName(pname), true);
if((filename != null) && FileExists(filename))
{
return true;
}
// Nothing found
return false;
}
2007-10-01 20:57:41 +00:00
#endregion
2009-01-02 14:23:57 +00:00
#region ================== Methods
2009-01-02 14:23:57 +00:00
// This creates an image
protected override ImageData CreateImage(string name, string filename, bool flat)
2009-01-02 14:23:57 +00:00
{
return new FileImage(name, Path.Combine(location.location, filename), flat);
2009-01-02 14:23:57 +00:00
}
2007-10-05 11:17:58 +00:00
// This returns true if the specified file exists
protected override bool FileExists(string filename)
2007-10-05 11:17:58 +00:00
{
return files.FileExists(filename);
2007-10-05 11:17:58 +00:00
}
2007-10-07 22:21:47 +00:00
// This returns all files in a given directory
2009-01-22 15:10:41 +00:00
protected override string[] GetAllFiles(string path, bool subfolders)
2007-10-27 13:59:24 +00:00
{
return files.GetAllFiles(path, subfolders).ToArray();
2009-01-02 14:23:57 +00:00
}
2007-10-27 13:59:24 +00:00
// This returns all files in a given directory that match the given extension
2009-01-22 15:10:41 +00:00
protected override string[] GetFilesWithExt(string path, string extension, bool subfolders)
2007-10-27 13:59:24 +00:00
{
return files.GetAllFiles(path, extension, subfolders).ToArray();
}
// This finds the first file that has the specific name, regardless of file extension
protected override string FindFirstFile(string beginswith, bool subfolders)
{
return files.GetFirstFile(beginswith, subfolders);
2007-10-27 13:59:24 +00:00
}
// This finds the first file that has the specific name, regardless of file extension
2009-01-22 15:10:41 +00:00
protected override string FindFirstFile(string path, string beginswith, bool subfolders)
2007-10-27 13:59:24 +00:00
{
return files.GetFirstFile(path, beginswith, subfolders);
2007-10-27 13:59:24 +00:00
}
2009-01-21 23:09:25 +00:00
// This finds the first file that has the specific name
2009-01-22 15:10:41 +00:00
protected override string FindFirstFileWithExt(string path, string beginswith, bool subfolders)
2009-01-21 23:09:25 +00:00
{
string title = Path.GetFileNameWithoutExtension(beginswith);
string ext = Path.GetExtension(beginswith);
if(ext.Length > 1) ext = ext.Substring(1); else ext = "";
return files.GetFirstFile(path, title, subfolders, ext);
2009-01-21 23:09:25 +00:00
}
2007-10-26 13:31:02 +00:00
// This loads an entire file in memory and returns the stream
// NOTE: Callers are responsible for disposing the stream!
protected override MemoryStream LoadFile(string filename)
2007-10-27 13:59:24 +00:00
{
return new MemoryStream(File.ReadAllBytes(Path.Combine(location.location, filename)));
2007-10-27 13:59:24 +00:00
}
// This creates a temp file for the speciied file and return the absolute path to the temp file
// NOTE: Callers are responsible for removing the temp file when done!
protected override string CreateTempFile(string filename)
2009-01-02 14:23:57 +00:00
{
// Just copy the file
string tempfile = General.MakeTempFilename(General.Map.TempPath, "wad");
File.Copy(Path.Combine(location.location, filename), tempfile);
return tempfile;
2009-01-02 14:23:57 +00:00
}
2007-10-01 20:57:41 +00:00
#endregion
}
}