// Noddybox.DOOM - DOOM WAD Wrapper
// Copyright (C) 2004 Ian Cowburn
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// $Id$
//
using System;
using System.Collections;
namespace Noddybox.DOOM
{
///
/// Performs operations on a collection of WADs
///
public class WADCollection : IEnumerable, IEnumerator
{
// ====================================
// PUBLIC
// ====================================
///
/// Delegate for WAD added event
///
public delegate void WadAddedEvent(WADCollection collection, WAD wad);
///
/// Delegate for WAD removed event
///
public delegate void WadRemovedEvent(WADCollection collection);
///
/// Create an empty collection of WADs
///
public WADCollection()
{
m_wads=new ArrayList();
m_iter_index=-1;
}
///
/// Add a WAD to the collection
///
/// The WAD
public void AddWad(WAD w)
{
m_wads.Add(w);
if (WadAdded!=null)
{
WadAdded(this,w);
}
}
///
/// Remove a WAD from the collection
///
/// The WAD
public void RemoveWad(WAD w)
{
m_wads.Remove(w);
if (WadRemoved!=null)
{
WadRemoved(this);
}
}
///
/// Get the first palette (PLAYPAL) lump from the WAD files
///
/// The lump containing the palette, or null if not found
public WADEntry GetPalette()
{
return QueryDirectory("PLAYPAL");
}
///
/// Queries the WAD directories for the named lump. Note it searches
/// back from the last loaded WAD as DOOM does.
///
/// The lump name
/// The lump, or null if it doesn't exist
public WADEntry QueryDirectory(string name)
{
for(int f=m_wads.Count-1;f>=0;f--)
{
WAD w=(WAD)m_wads[f];
WADEntry e=w.FindEntry("PLAYPAL");
if (e!=null)
{
return e;
}
}
return null;
}
///
/// Called when a WAD is added to the collection
///
public event WadAddedEvent WadAdded;
///
/// Called when a WAD is removed from the collection
///
public event WadRemovedEvent WadRemoved;
///
/// Returns the number of WADs in the collection
///
public int Count
{
get {return m_wads.Count;}
}
///
/// Index into the WADs
///
public WAD this[int index]
{
get {return (WAD)m_wads[index];}
}
///
/// Gets an enumerator
///
/// The enumerator
public IEnumerator GetEnumerator()
{
Reset();
return this;
}
///
/// Get the current object from the enumerator
///
public object Current
{
get
{
if (m_iter_index==-1)
{
throw new InvalidOperationException();
}
return m_wads[m_iter_index];
}
}
///
/// Reset the enumerator to the start of the collection.
///
public void Reset()
{
m_iter_index=-1;
}
///
/// Move the enumerator to the next item.
///
/// True if there are more objects to enumerate over
public bool MoveNext()
{
m_iter_index++;
if (m_iter_index>=m_wads.Count)
{
m_iter_index=-1;
}
return m_iter_index!=-1;
}
// ====================================
// PRIVATE
// ====================================
private ArrayList m_wads;
private int m_iter_index;
}
}