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 ;
2007-10-07 22:21:47 +00:00
using CodeImp.DoomBuilder.IO ;
2007-10-14 21:31:45 +00:00
using System.Windows.Forms ;
2007-10-01 20:57:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2007-10-08 14:29:31 +00:00
internal sealed class DataManager : IDisposable
2007-10-01 20:57:41 +00:00
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
2007-10-04 18:16:05 +00:00
// Data containers
2007-10-07 22:21:47 +00:00
private List < DataReader > containers ;
// Palette
private Playpal palette ;
// Textures
private Dictionary < long , ImageData > textures ;
2007-10-04 18:16:05 +00:00
2007-10-07 22:21:47 +00:00
// Flats
private Dictionary < long , ImageData > flats ;
// Sprites
private Dictionary < long , ImageData > sprites ;
2007-10-01 20:57:41 +00:00
// Disposing
private bool isdisposed = false ;
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
2007-10-08 14:29:31 +00:00
public Playpal Palette { get { return palette ; } }
2007-10-01 20:57:41 +00:00
public bool IsDisposed { get { return isdisposed ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public DataManager ( )
{
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
2007-10-04 18:16:05 +00:00
// Disposer
2007-10-01 20:57:41 +00:00
public void Dispose ( )
{
// Not already disposed?
if ( ! isdisposed )
{
// Clean up
2007-10-05 11:17:58 +00:00
Unload ( ) ;
2007-10-01 20:57:41 +00:00
// Done
isdisposed = true ;
}
}
#endregion
2007-10-04 18:16:05 +00:00
#region = = = = = = = = = = = = = = = = = = Loading / Unloading
2007-10-05 11:17:58 +00:00
// This loads all data resources
public void Load ( DataLocationList configlist , DataLocationList maplist , DataLocation maplocation )
{
2007-10-07 22:21:47 +00:00
DataLocationList all = DataLocationList . Combined ( configlist , maplist ) ;
2007-10-05 11:17:58 +00:00
all . Add ( maplocation ) ;
Load ( all ) ;
}
// This loads all data resources
public void Load ( DataLocationList configlist , DataLocationList maplist )
{
2007-10-07 22:21:47 +00:00
DataLocationList all = DataLocationList . Combined ( configlist , maplist ) ;
2007-10-05 11:17:58 +00:00
Load ( all ) ;
}
2007-10-04 18:16:05 +00:00
// This loads all data resources
public void Load ( DataLocationList locations )
{
2007-10-07 22:21:47 +00:00
DataReader c ;
// Create collections
containers = new List < DataReader > ( ) ;
textures = new Dictionary < long , ImageData > ( ) ;
flats = new Dictionary < long , ImageData > ( ) ;
sprites = new Dictionary < long , ImageData > ( ) ;
2007-10-04 18:16:05 +00:00
// Go for all locations
foreach ( DataLocation dl in locations )
{
// Nothing chosen yet
c = null ;
2007-10-05 11:17:58 +00:00
2007-10-07 22:21:47 +00:00
// TODO: Make this work more elegant using reflection.
// Make DataLocation.type of type Type and assign the
// types of the desired reader classes.
2007-10-14 21:31:45 +00:00
try
2007-10-04 18:16:05 +00:00
{
2007-10-14 21:31:45 +00:00
// Choose container type
switch ( dl . type )
{
// WAD file container
case DataLocation . RESOURCE_WAD :
c = new WADReader ( dl ) ;
break ;
// Directory container
case DataLocation . RESOURCE_DIRECTORY :
c = new DirectoryReader ( dl ) ;
break ;
}
2007-10-04 18:16:05 +00:00
}
2007-10-14 21:31:45 +00:00
catch ( Exception )
{
// Unable to load resource
General . ShowErrorMessage ( "Unable to load resources from location \"" + dl . location + "\". Please make sure the location is accessible and not in use by another program." , MessageBoxButtons . OK ) ;
continue ;
}
2007-10-07 22:21:47 +00:00
// Add container
if ( c ! = null ) containers . Add ( c ) ;
2007-10-04 18:16:05 +00:00
}
2007-10-07 22:21:47 +00:00
// Load stuff
General . WriteLogLine ( "Loading PLAYPAL palette..." ) ;
LoadPalette ( ) ;
General . WriteLogLine ( "Loading textures..." ) ;
LoadTextures ( ) ;
2007-10-04 18:16:05 +00:00
}
2007-10-01 20:57:41 +00:00
2007-10-04 18:16:05 +00:00
// This unloads all data
public void Unload ( )
{
2007-10-07 22:21:47 +00:00
// Dispose resources
foreach ( KeyValuePair < long , ImageData > i in textures ) i . Value . Dispose ( ) ;
foreach ( KeyValuePair < long , ImageData > i in flats ) i . Value . Dispose ( ) ;
foreach ( KeyValuePair < long , ImageData > i in sprites ) i . Value . Dispose ( ) ;
palette = null ;
2007-10-04 18:16:05 +00:00
// Dispose containers
2007-10-07 22:21:47 +00:00
foreach ( DataReader c in containers ) c . Dispose ( ) ;
2007-10-04 18:16:05 +00:00
containers . Clear ( ) ;
}
2007-10-07 22:21:47 +00:00
#endregion
2007-10-04 18:16:05 +00:00
2007-10-07 22:21:47 +00:00
#region = = = = = = = = = = = = = = = = = = Suspend / Resume
2007-10-13 14:05:45 +00:00
// This suspends data resources
public void Suspend ( )
2007-10-05 11:17:58 +00:00
{
// Go for all containers
2007-10-07 22:21:47 +00:00
foreach ( DataReader d in containers )
2007-10-05 11:17:58 +00:00
{
2007-10-13 14:05:45 +00:00
// Suspend
General . WriteLogLine ( "Suspended data resource '" + d . Location . location + "'" ) ;
d . Suspend ( ) ;
2007-10-05 11:17:58 +00:00
}
}
2007-10-13 14:05:45 +00:00
// This resumes data resources
public void Resume ( )
2007-10-05 11:17:58 +00:00
{
// Go for all containers
2007-10-07 22:21:47 +00:00
foreach ( DataReader d in containers )
2007-10-05 11:17:58 +00:00
{
2007-10-14 21:31:45 +00:00
try
{
// Resume
General . WriteLogLine ( "Resumed data resource '" + d . Location . location + "'" ) ;
d . Resume ( ) ;
}
catch ( Exception )
{
// Unable to load resource
General . ShowErrorMessage ( "Unable to load resources from location \"" + d . Location . location + "\". Please make sure the location is accessible and not in use by another program." , MessageBoxButtons . OK ) ;
}
2007-10-05 11:17:58 +00:00
}
}
2007-10-01 20:57:41 +00:00
#endregion
2007-10-07 22:21:47 +00:00
#region = = = = = = = = = = = = = = = = = = Palette
// This loads the PLAYPAL palette
private void LoadPalette ( )
{
// Go for all opened containers
for ( int i = containers . Count - 1 ; i > = 0 ; i - - )
{
// Load palette
palette = containers [ i ] . LoadPalette ( ) ;
if ( palette ! = null ) break ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Textures
// This loads the textures
private void LoadTextures ( )
{
PatchNames pnames ;
ICollection < ImageData > images ;
// Go for all opened containers
foreach ( DataReader dr in containers )
{
// Load PNAMES info
// Note that pnames is NOT set to null in the loop
// because if a container has no pnames, the pnames
// of the previous (higher) container should be used.
pnames = dr . LoadPatchNames ( ) ;
if ( pnames ! = null )
{
// Load textures
images = dr . LoadTextures ( pnames ) ;
if ( images ! = null )
{
// Go for all textures
foreach ( ImageData img in images )
{
// Add or replace in textures list
textures . Remove ( img . LongName ) ;
textures . Add ( img . LongName , img ) ;
}
}
}
}
}
// This returns a specific patch stream
public Stream GetPatchData ( string pname )
{
Stream patch ;
// Go for all opened containers
for ( int i = containers . Count - 1 ; i > = 0 ; i - - )
{
// This contain provides this patch?
patch = containers [ i ] . GetPatchData ( pname ) ;
if ( patch ! = null ) return patch ;
}
// No such patch found
return null ;
}
#endregion
2007-10-01 20:57:41 +00:00
}
}