2007-10-26 14:16:23 +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 CodeImp.DoomBuilder.IO;
|
2007-10-27 16:21:43 +00:00
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
2007-10-26 14:16:23 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Data
|
|
|
|
{
|
2009-01-03 19:45:59 +00:00
|
|
|
internal sealed class PK3Reader : PK3StructuredReader
|
2007-10-26 14:16:23 +00:00
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
private List<string> fileslist;
|
2007-10-26 14:16:23 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public PK3Reader(DataLocation dl) : base(dl)
|
|
|
|
{
|
|
|
|
General.WriteLogLine("Opening PK3 resource '" + location.location + "'");
|
2009-01-03 19:45:59 +00:00
|
|
|
|
|
|
|
// Open the zip file
|
|
|
|
ZipInputStream zipstream = OpenPK3File();
|
|
|
|
|
|
|
|
// Make list of all files
|
|
|
|
fileslist = new List<string>();
|
|
|
|
ZipEntry entry = zipstream.GetNextEntry();
|
|
|
|
while(entry != null)
|
2007-10-27 16:21:43 +00:00
|
|
|
{
|
2009-01-03 19:45:59 +00:00
|
|
|
if(entry.IsFile) fileslist.Add(entry.Name.ToLowerInvariant());
|
2007-10-27 16:21:43 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// Next
|
|
|
|
entry = zipstream.GetNextEntry();
|
2007-10-27 16:21:43 +00:00
|
|
|
}
|
2009-01-03 19:45:59 +00:00
|
|
|
|
|
|
|
// Done with the zip file
|
|
|
|
zipstream.Close();
|
|
|
|
zipstream.Dispose();
|
2007-10-27 16:21:43 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// Initialize without path (because we use paths relative to the PK3 file)
|
|
|
|
Initialize("");
|
|
|
|
|
2007-10-26 14:16:23 +00:00
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposer
|
|
|
|
public override void Dispose()
|
|
|
|
{
|
|
|
|
// Not already disposed?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
General.WriteLogLine("Closing PK3 resource '" + location.location + "'");
|
2009-01-03 19:45:59 +00:00
|
|
|
|
2007-10-26 14:16:23 +00:00
|
|
|
// Done
|
|
|
|
base.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
#region ================== Management
|
2007-10-26 14:16:23 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// This opens the zip file for reading
|
|
|
|
private ZipInputStream OpenPK3File()
|
2007-10-26 14:16:23 +00:00
|
|
|
{
|
2009-01-03 19:45:59 +00:00
|
|
|
FileStream filestream = File.Open(location.location, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
|
filestream.Seek(0, SeekOrigin.Begin);
|
|
|
|
return new ZipInputStream(filestream);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This creates an image
|
|
|
|
protected override ImageData CreateImage(string name, string filename, bool flat)
|
|
|
|
{
|
|
|
|
return new PK3FileImage(this, name, filename, flat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns true if the specified file exists
|
|
|
|
protected override bool FileExists(string filename)
|
|
|
|
{
|
|
|
|
string lowfile = filename.ToLowerInvariant();
|
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if((string.Compare(f, lowfile) == 0)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns all files in a given directory
|
2009-01-22 15:10:41 +00:00
|
|
|
protected override string[] GetAllFiles(string path, bool subfolders)
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
|
|
|
List<string> matches = new List<string>();
|
|
|
|
string lowpath = path.ToLowerInvariant();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(subfolders)
|
|
|
|
{
|
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if(Path.GetDirectoryName(f).StartsWith(lowpath, true, CultureInfo.InvariantCulture) &&
|
|
|
|
(Path.GetFileName(f).Length > 0))
|
|
|
|
matches.Add(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if((string.Compare(Path.GetDirectoryName(f), lowpath) == 0) &&
|
|
|
|
(Path.GetFileName(f).Length > 0))
|
|
|
|
matches.Add(f);
|
|
|
|
}
|
2009-01-03 19:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return matches.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
|
|
|
List<string> matches = new List<string>();
|
|
|
|
string lowpath = path.ToLowerInvariant();
|
|
|
|
string lowext = "." + extension.ToLowerInvariant();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(subfolders)
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if(Path.GetDirectoryName(f).StartsWith(lowpath, true, CultureInfo.InvariantCulture) && f.EndsWith(lowext))
|
|
|
|
matches.Add(f);
|
|
|
|
}
|
2009-01-03 19:45:59 +00:00
|
|
|
}
|
2009-01-22 15:10:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if((string.Compare(Path.GetDirectoryName(f), lowpath) == 0) && f.EndsWith(lowext))
|
|
|
|
matches.Add(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
return matches.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
|
|
|
string lowpath = path.ToLowerInvariant();
|
|
|
|
string lowbegin = beginswith.ToLowerInvariant();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(subfolders)
|
2009-01-03 19:45:59 +00:00
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if(Path.GetDirectoryName(f).StartsWith(lowpath, true, CultureInfo.InvariantCulture) &&
|
|
|
|
(string.Compare(Path.GetFileNameWithoutExtension(f), lowbegin) == 0))
|
|
|
|
return f;
|
|
|
|
}
|
2009-01-03 19:45:59 +00:00
|
|
|
}
|
2009-01-22 15:10:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if((string.Compare(Path.GetDirectoryName(f), lowpath) == 0) &&
|
|
|
|
(string.Compare(Path.GetFileNameWithoutExtension(f), lowbegin) == 0))
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-26 14:16:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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 lowpath = path.ToLowerInvariant();
|
|
|
|
string lowbegin = beginswith.ToLowerInvariant();
|
2009-01-22 15:10:41 +00:00
|
|
|
if(subfolders)
|
|
|
|
{
|
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if(Path.GetDirectoryName(f).StartsWith(lowpath, true, CultureInfo.InvariantCulture) &&
|
|
|
|
(string.Compare(Path.GetFileName(f), lowbegin) == 0))
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-01-21 23:09:25 +00:00
|
|
|
{
|
2009-01-22 15:10:41 +00:00
|
|
|
foreach(string f in fileslist)
|
|
|
|
{
|
|
|
|
if((string.Compare(Path.GetDirectoryName(f), lowpath) == 0) &&
|
|
|
|
(string.Compare(Path.GetFileName(f), lowbegin) == 0))
|
|
|
|
return f;
|
|
|
|
}
|
2009-01-21 23:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
MemoryStream filedata = null;
|
|
|
|
byte[] copybuffer = new byte[4096];
|
|
|
|
|
|
|
|
// Open the zip file
|
|
|
|
ZipInputStream zipstream = OpenPK3File();
|
|
|
|
|
|
|
|
ZipEntry entry = zipstream.GetNextEntry();
|
|
|
|
while(entry != null)
|
|
|
|
{
|
|
|
|
// Is this the entry we are looking for?
|
|
|
|
if(string.Compare(entry.Name, filename, true) == 0)
|
|
|
|
{
|
|
|
|
int expectedsize = (int)entry.Size;
|
|
|
|
if(expectedsize < 1) expectedsize = 1024;
|
|
|
|
filedata = new MemoryStream(expectedsize);
|
|
|
|
int readsize = zipstream.Read(copybuffer, 0, copybuffer.Length);
|
|
|
|
while(readsize > 0)
|
|
|
|
{
|
|
|
|
filedata.Write(copybuffer, 0, readsize);
|
|
|
|
readsize = zipstream.Read(copybuffer, 0, copybuffer.Length);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next
|
|
|
|
entry = zipstream.GetNextEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done with the zip file
|
|
|
|
zipstream.Close();
|
|
|
|
zipstream.Dispose();
|
|
|
|
|
|
|
|
// Nothing found?
|
|
|
|
if(filedata == null)
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException("Cannot find the file " + filename + " in PK3 file " + location.location + ".");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return filedata;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 14:16:23 +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)
|
|
|
|
{
|
|
|
|
// Just copy the file
|
|
|
|
string tempfile = General.MakeTempFilename(General.Map.TempPath, "wad");
|
|
|
|
MemoryStream filedata = LoadFile(filename);
|
|
|
|
File.WriteAllBytes(tempfile, filedata.ToArray());
|
|
|
|
filedata.Dispose();
|
|
|
|
return tempfile;
|
|
|
|
}
|
2007-10-26 14:16:23 +00:00
|
|
|
|
2009-01-03 19:45:59 +00:00
|
|
|
// Public version to load a file
|
|
|
|
internal MemoryStream ExtractFile(string filename)
|
|
|
|
{
|
|
|
|
return LoadFile(filename);
|
|
|
|
}
|
|
|
|
|
2007-10-26 14:16:23 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|