summaryrefslogtreecommitdiff
path: root/WADCollection.cs
blob: ba6eb5be56e905c47ed164137d20726cea99da1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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.
//
using System;
using System.Collections;

namespace Noddybox.DOOM
{
	/// <summary>
	/// Performs operations on a collection of WADs
	/// </summary>
	public class WADCollection : IEnumerable, IEnumerator
	{
		// ====================================
		// PUBLIC
		// ====================================
		
		/// <summary>
		/// Delegate for WAD added event
		/// </summary>
		public delegate void WadAddedEvent(WADCollection collection, WAD wad);
		
		/// <summary>
		/// Delegate for WAD removed event
		/// </summary>
		public delegate void WadRemovedEvent(WADCollection collection);
		
		/// <summary>
		/// Create an empty collection of WADs
		/// </summary>
		public WADCollection()
		{
			m_wads=new ArrayList();
			m_iter_index=-1;
		}
		
		/// <summary>
		/// Add a WAD to the collection
		/// </summary>
		/// <param name="w">The WAD</param>
		public void AddWad(WAD w)
		{
			m_wads.Add(w);
			
			if (WadAdded!=null)
			{
				WadAdded(this,w);
			}
		}
		
		/// <summary>
		/// Remove a WAD from the collection
		/// </summary>
		/// <param name="w">The WAD</param>
		public void RemoveWad(WAD w)
		{
			m_wads.Remove(w);
			
			if (WadRemoved!=null)
			{
				WadRemoved(this);
			}
		}
		
		
		/// <summary>
		/// Get the first palette (PLAYPAL) lump from the WAD files
		/// </summary>
		/// <returns>The lump containing the palette, or null if not found</returns>
		public WADEntry GetPalette()
		{
			return QueryDirectory("PLAYPAL");
		}
		
		
		/// <summary>
		/// Queries the WAD directories for the named lump.  Note it searches
		/// back from the last loaded WAD as DOOM does.
		/// </summary>
		/// <param name="name">The lump name</param>
		/// <returns>The lump, or null if it doesn't exist</returns>
		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;
		}

		/// <summary>
		/// Called when a WAD is added to the collection
		/// </summary>
		public event WadAddedEvent 		WadAdded;
		
		/// <summary>
		/// Called when a WAD is removed from the collection
		/// </summary>
		public event WadRemovedEvent	WadRemoved;

		/// <summary>
		/// Returns the number of WADs in the collection
		/// </summary>
		public int Count
		{
			get {return m_wads.Count;}
		}
		
		/// <summary>
		/// Index into the WADs 
		/// </summary>
		public WAD this[int index]
		{
			get {return (WAD)m_wads[index];}
		}
		
		/// <summary>
		/// Gets an enumerator
		/// </summary>
		/// <returns>The enumerator</returns>
		public IEnumerator GetEnumerator()
		{
			Reset();
			return this;
		}
		
		/// <summary>
		/// Get the current object from the enumerator
		/// </summary>
		public object Current
		{
			get
			{
				if (m_iter_index==-1)
				{
					throw new InvalidOperationException();
				}
				
				return m_wads[m_iter_index];
			}
		}

		/// <summary>
		/// Reset the enumerator to the start of the collection.
		/// </summary>
		public void Reset()
		{
			m_iter_index=-1;
		}

		/// <summary>
		/// Move the enumerator to the next item.
		/// </summary>
		/// <returns>True if there are more objects to enumerate over</returns>
		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;
	}
}