UltimateZoneBuilder/Source/IO/Lump.cs

180 lines
4.3 KiB
C#
Raw Normal View History

2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
2007-06-13 19:39:38 +00:00
/*
* 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.
*
*/
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Namespaces
2007-06-13 19:39:38 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
2007-06-14 23:31:57 +00:00
#endregion
2007-06-13 19:39:38 +00:00
namespace CodeImp.DoomBuilder.IO
{
2008-01-02 21:49:43 +00:00
internal class Lump
2007-06-13 19:39:38 +00:00
{
#region ================== Variables
// Owner
private WAD owner;
// Data stream
private ClippedStream stream;
// Data info
private string name;
2007-10-07 22:21:47 +00:00
private long longname;
2007-06-13 19:39:38 +00:00
private byte[] fixedname;
private int offset;
private int length;
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
public WAD Owner { get { return owner; } }
public string Name { get { return name; } }
2007-10-07 22:21:47 +00:00
public long LongName { get { return longname; } }
2007-06-13 19:39:38 +00:00
public byte[] FixedName { get { return fixedname; } }
public int Offset { get { return offset; } }
public int Length { get { return length; } }
public ClippedStream Stream { get { return stream; } }
public bool IsDisposed { get { return isdisposed; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
public Lump(Stream data, WAD owner, byte[] fixedname, int offset, int length)
{
// Initialize
this.stream = new ClippedStream(data, offset, length);
this.owner = owner;
this.fixedname = fixedname;
this.offset = offset;
this.length = length;
2007-10-07 22:21:47 +00:00
// Make name
2007-06-24 18:56:43 +00:00
this.name = MakeNormalName(fixedname, WAD.ENCODING).ToUpperInvariant();
this.fixedname = MakeFixedName(name, WAD.ENCODING);
2007-10-07 22:21:47 +00:00
this.longname = MakeLongName(name);
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-06-13 19:39:38 +00:00
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
stream.Dispose();
owner = null;
// Done
isdisposed = true;
}
}
#endregion
#region ================== Methods
2007-10-07 22:21:47 +00:00
// This returns the long value for a 8 byte texture name
public static unsafe long MakeLongName(string name)
{
long value = 0;
2008-02-15 14:08:26 +00:00
byte[] namebytes = Encoding.ASCII.GetBytes(name.Trim().ToUpper());
2007-10-07 22:21:47 +00:00
uint bytes = (uint)namebytes.Length;
if(bytes > 8) bytes = 8;
fixed(void* bp = namebytes)
{
General.CopyMemory(&value, bp, new UIntPtr(bytes));
}
return value;
}
2007-06-13 19:39:38 +00:00
// This makes the normal name from fixed name
2007-06-24 18:56:43 +00:00
public static string MakeNormalName(byte[] fixedname, Encoding encoding)
2007-06-13 19:39:38 +00:00
{
2007-06-16 19:53:51 +00:00
int length = 0;
// Figure out the length of the lump name
while((length < fixedname.Length) && (fixedname[length] != 0)) length++;
// Make normal name
2008-02-15 14:08:26 +00:00
return encoding.GetString(fixedname, 0, length).Trim().ToUpper();
2007-06-13 19:39:38 +00:00
}
// This makes the fixed name from normal name
public static byte[] MakeFixedName(string name, Encoding encoding)
{
// Make 8 bytes, all zeros
byte[] fixedname = new byte[8];
// Write the name in bytes
2008-02-15 14:08:26 +00:00
encoding.GetBytes(name.Trim().ToUpper(), 0, name.Length, fixedname, 0);
2007-06-13 19:39:38 +00:00
// Return result
return fixedname;
}
2007-06-24 18:56:43 +00:00
// This copies lump data to another lump
public void CopyTo(Lump lump)
{
BinaryReader reader;
// Create a reader
reader = new BinaryReader(stream);
// Copy bytes over
stream.Seek(0, SeekOrigin.Begin);
2007-06-24 18:56:43 +00:00
lump.Stream.Write(reader.ReadBytes((int)stream.Length), 0, (int)stream.Length);
}
2007-06-13 19:39:38 +00:00
// String representation
public override string ToString()
{
return name;
}
2007-10-14 15:44:55 +00:00
// This renames the lump
public void Rename(string newname)
{
// Make name
this.fixedname = MakeFixedName(newname, WAD.ENCODING);
this.name = MakeNormalName(this.fixedname, WAD.ENCODING).ToUpperInvariant();
this.longname = MakeLongName(newname);
// Write changes
owner.WriteHeaders();
}
2007-06-13 19:39:38 +00:00
#endregion
}
}