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;
|
2009-01-22 22:22:02 +00:00
|
|
|
using System.Windows.Forms;
|
2007-10-01 20:57:41 +00:00
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Data
|
|
|
|
{
|
2009-01-03 19:45:59 +00:00
|
|
|
internal sealed class DirectoryReader : PK3StructuredReader
|
2007-10-01 20:57:41 +00:00
|
|
|
{
|
2009-01-22 22:22:02 +00:00
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
private DirectoryFilesList files;
|
|
|
|
|
|
|
|
#endregion
|
2009-01-02 14:23:57 +00:00
|
|
|
|
2009-01-22 22:22:02 +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
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// Initialize
|
2009-01-22 22:22:02 +00:00
|
|
|
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
|
|
|
|
2007-10-04 18:16:05 +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
|
|
|
|
2009-02-09 23:34:20 +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;
|
|
|
|
}
|
2009-02-10 14:45:09 +00:00
|
|
|
|
|
|
|
try
|
2009-02-09 23:34:20 +00:00
|
|
|
{
|
2009-02-10 14:45:09 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
2009-02-26 23:27:46 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, e.GetType().Name + " while loading patch '" + pname + "' from directory: " + e.Message);
|
2009-02-09 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-02-10 14:45:09 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
2009-02-09 23:34:20 +00:00
|
|
|
{
|
2009-02-26 23:27:46 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, e.GetType().Name + " while loading texture '" + pname + "' from directory: " + e.Message);
|
2009-02-09 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-02-10 14:45:09 +00:00
|
|
|
|
|
|
|
try
|
2009-02-09 23:34:20 +00:00
|
|
|
{
|
2009-02-10 14:45:09 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2009-02-09 23:34:20 +00:00
|
|
|
}
|
2009-02-10 14:45:09 +00:00
|
|
|
catch(Exception e)
|
|
|
|
{
|
2009-02-26 23:27:46 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, e.GetType().Name + " while loading sprite '" + pname + "' from directory: " + e.Message);
|
2009-02-10 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 23:34:20 +00:00
|
|
|
// 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
|
2009-02-10 14:45:09 +00:00
|
|
|
try
|
2009-02-09 23:34:20 +00:00
|
|
|
{
|
2009-02-10 14:45:09 +00:00
|
|
|
string path = Path.Combine(SPRITES_DIR, Path.GetDirectoryName(pname));
|
|
|
|
string filename = FindFirstFile(path, Path.GetFileName(pname), true);
|
|
|
|
if((filename != null) && FileExists(filename))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-09 23:34:20 +00:00
|
|
|
}
|
2009-02-10 14:45:09 +00:00
|
|
|
catch(Exception e)
|
|
|
|
{
|
2009-02-26 23:27:46 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, e.GetType().Name + " while checking sprite '" + pname + "' existance in directory: " + e.Message);
|
2009-02-10 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 23:34:20 +00:00
|
|
|
// Nothing found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-01 20:57:41 +00:00
|
|
|
#endregion
|
2009-01-02 14:23:57 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
#region ================== Methods
|
2009-01-02 14:23:57 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// This creates an image
|
|
|
|
protected override ImageData CreateImage(string name, string filename, bool flat)
|
2009-01-02 14:23:57 +00:00
|
|
|
{
|
2009-01-22 22:22:02 +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
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// This returns true if the specified file exists
|
|
|
|
protected override bool FileExists(string filename)
|
2007-10-05 11:17:58 +00:00
|
|
|
{
|
2009-01-22 22:22:02 +00:00
|
|
|
return files.FileExists(filename);
|
2007-10-05 11:17:58 +00:00
|
|
|
}
|
2007-10-07 22:21:47 +00:00
|
|
|
|
2009-01-03 19:45:59 +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
|
|
|
{
|
2009-01-22 22:22:02 +00:00
|
|
|
return files.GetAllFiles(path, subfolders).ToArray();
|
2009-01-02 14:23:57 +00:00
|
|
|
}
|
2007-10-27 13:59:24 +00:00
|
|
|
|
2009-01-03 19:45:59 +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
|
|
|
{
|
2009-01-22 22:22:02 +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
|
|
|
}
|
|
|
|
|
2009-01-03 19:45:59 +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
|
|
|
{
|
2009-01-22 22:22:02 +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
|
|
|
{
|
2009-01-22 22:22:02 +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
|
|
|
|
2009-01-03 19:45:59 +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
|
|
|
{
|
2009-01-22 22:22:02 +00:00
|
|
|
return new MemoryStream(File.ReadAllBytes(Path.Combine(location.location, filename)));
|
2007-10-27 13:59:24 +00:00
|
|
|
}
|
2009-01-03 19:45:59 +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
|
|
|
{
|
2009-01-03 19:45:59 +00:00
|
|
|
// Just copy the file
|
|
|
|
string tempfile = General.MakeTempFilename(General.Map.TempPath, "wad");
|
2009-01-22 22:22:02 +00:00
|
|
|
File.Copy(Path.Combine(location.location, filename), tempfile);
|
2009-01-03 19:45:59 +00:00
|
|
|
return tempfile;
|
2009-01-02 14:23:57 +00:00
|
|
|
}
|
2009-01-03 19:45:59 +00:00
|
|
|
|
2007-10-01 20:57:41 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|