UltimateZoneBuilder/Source/Data/DataManager.cs

840 lines
20 KiB
C#
Raw Normal View History

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;
using SlimDX.Direct3D9;
using CodeImp.DoomBuilder.Config;
using System.Threading;
2007-10-01 20:57:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2008-01-02 21:49:43 +00:00
public sealed class DataManager
2007-10-01 20:57:41 +00:00
{
#region ================== Constants
#endregion
#region ================== Variables
// 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-12-27 01:24:11 +00:00
private List<string> texturenames;
2007-10-07 22:21:47 +00:00
// Flats
private Dictionary<long, ImageData> flats;
2007-12-27 01:24:11 +00:00
private List<string> flatnames;
2007-10-07 22:21:47 +00:00
// Sprites
private Dictionary<long, ImageData> sprites;
// Background loading
private LinkedList<ImageData> loadlist;
private Thread backgroundloader;
2008-01-13 21:23:59 +00:00
// Special images
private ImageData missingtexture3d;
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; } }
public ICollection<ImageData> Textures { get { return textures.Values; } }
public ICollection<ImageData> Flats { get { return flats.Values; } }
2007-12-27 01:24:11 +00:00
public List<string> TextureNames { get { return texturenames; } }
public List<string> FlatNames { get { return flatnames; } }
2007-10-01 20:57:41 +00:00
public bool IsDisposed { get { return isdisposed; } }
2008-01-13 21:23:59 +00:00
public ImageData MissingTexture3D { get { return missingtexture3d; } }
2007-10-26 19:06:59 +00:00
public bool IsLoading
{
get
{
if(loadlist != null)
{
return (backgroundloader != null) && backgroundloader.IsAlive && (loadlist.Count > 0);
}
else
{
return false;
}
}
}
2007-10-01 20:57:41 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
2008-01-02 21:49:43 +00:00
internal DataManager()
2007-10-01 20:57:41 +00:00
{
// We have no destructor
GC.SuppressFinalize(this);
2008-01-13 21:23:59 +00:00
// Load special images
missingtexture3d = new ResourceImage("MissingTexture3D.png");
missingtexture3d.LoadImage();
2007-10-01 20:57:41 +00:00
}
// Disposer
2008-01-02 21:49:43 +00:00
internal void Dispose()
2007-10-01 20:57:41 +00:00
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
2007-10-05 11:17:58 +00:00
Unload();
2008-01-13 21:23:59 +00:00
missingtexture3d.Dispose();
missingtexture3d = null;
2007-10-05 11:17:58 +00:00
2007-10-01 20:57:41 +00:00
// Done
isdisposed = true;
}
}
#endregion
#region ================== Loading / Unloading
2007-10-05 11:17:58 +00:00
// This loads all data resources
2008-01-02 21:49:43 +00:00
internal void Load(DataLocationList configlist, DataLocationList maplist, DataLocation maplocation)
2007-10-05 11:17:58 +00:00
{
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
2008-01-02 21:49:43 +00:00
internal void Load(DataLocationList configlist, DataLocationList maplist)
2007-10-05 11:17:58 +00:00
{
2007-10-07 22:21:47 +00:00
DataLocationList all = DataLocationList.Combined(configlist, maplist);
2007-10-05 11:17:58 +00:00
Load(all);
}
// This loads all data resources
2008-01-02 21:49:43 +00:00
internal void Load(DataLocationList locations)
{
int texcount, flatcount, spritecount;
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-12-27 01:24:11 +00:00
texturenames = new List<string>();
flatnames = new List<string>();
loadlist = new LinkedList<ImageData>();
// 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-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-26 14:16:23 +00:00
// PK3 file container
case DataLocation.RESOURCE_PK3:
c = new PK3Reader(dl);
break;
2007-10-14 21:31:45 +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-07 22:21:47 +00:00
// Load stuff
LoadPalette();
texcount = LoadTextures();
flatcount = LoadFlats();
spritecount = LoadSprites();
// Sort names
texturenames.Sort();
flatnames.Sort();
// Start background loading
StartBackgroundLoader();
// Output info
General.WriteLogLine("Loaded " + texcount + " textures, " + flatcount + " flats, " + spritecount + " sprites");
}
2007-10-01 20:57:41 +00:00
// This unloads all data
2008-01-02 21:49:43 +00:00
internal void Unload()
{
// Stop background loader
StopBackgroundLoader();
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-12-27 01:24:11 +00:00
// Dispose containers
2007-10-07 22:21:47 +00:00
foreach(DataReader c in containers) c.Dispose();
containers.Clear();
// Trash collections
containers = null;
textures = null;
flats = null;
sprites = null;
texturenames = null;
flatnames = null;
loadlist = null;
}
2007-10-07 22:21:47 +00:00
#endregion
2007-10-07 22:21:47 +00:00
#region ================== Suspend / Resume
// This suspends data resources
2008-01-02 21:49:43 +00:00
internal void Suspend()
2007-10-05 11:17:58 +00:00
{
// Stop background loader
StopBackgroundLoader();
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
{
// Suspend
General.WriteLogLine("Suspended data resource '" + d.Location.location + "'");
d.Suspend();
2007-10-05 11:17:58 +00:00
}
}
// This resumes data resources
2008-01-02 21:49:43 +00:00
internal 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
}
// Start background loading
StartBackgroundLoader();
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 ================== Background Loading
// This starts background loading
private void StartBackgroundLoader()
{
// If a loader is already running, stop it first
if(backgroundloader != null) StopBackgroundLoader();
// Start a low priority thread to load images in background
General.WriteLogLine("Starting background resource loading...");
backgroundloader = new Thread(new ThreadStart(BackgroundLoad));
backgroundloader.Name = "BackgroundLoader";
backgroundloader.Priority = ThreadPriority.Lowest;
backgroundloader.Start();
}
// This stops background loading
private void StopBackgroundLoader()
{
LinkedListNode<ImageData> n;
General.WriteLogLine("Stopping background resource loading...");
2007-10-26 18:04:54 +00:00
if(backgroundloader != null)
{
// Stop the thread and wait for it to end
backgroundloader.Interrupt();
backgroundloader.Join();
// Reset load states on all images in the list
n = loadlist.First;
while(n != null)
{
n.Value.LoadState = ImageData.LOADSTATE_NONE;
n.Value.LoadingTicket = null;
n = n.Next;
}
loadlist.Clear();
2007-10-26 18:04:54 +00:00
// Done
backgroundloader = null;
General.MainWindow.UpdateStatusIcon();
2007-10-26 18:04:54 +00:00
}
}
// The background loader
private void BackgroundLoad()
{
try
{
do
{
// Get next item
ImageData image = null;
lock(loadlist)
{
// Anything to do?
if(loadlist.Count > 0)
{
// Fetch image
image = loadlist.First.Value;
image.LoadingTicket = null;
loadlist.RemoveFirst();
// Load or unload this image?
switch(image.LoadState)
{
// Load image
case ImageData.LOADSTATE_LOAD:
image.LoadImage();
//image.CreateTexture(); // Impossible from different thread
break;
// Unload image
case ImageData.LOADSTATE_TRASH:
image.UnloadImage();
break;
}
}
}
// Did we do something?
if(image != null)
{
// Wait a bit and update icon
General.MainWindow.UpdateStatusIcon();
Thread.Sleep(1);
}
else
{
// Wait longer to release CPU resources
Thread.Sleep(50);
}
}
while(true);
}
catch(ThreadInterruptedException)
{
return;
}
}
// This adds an image for background loading or unloading
public void BackgroundLoadImage(ImageData img, bool load)
{
int loadstate = load ? ImageData.LOADSTATE_LOAD : ImageData.LOADSTATE_TRASH;
lock(loadlist)
{
// Already in the list?
if(img.LoadingTicket != null)
{
// Just change the state
img.LoadState = loadstate;
}
else
{
// Set load state and add to list
img.LoadState = loadstate;
img.LoadingTicket = loadlist.AddLast(img);
}
}
// Update icon
General.MainWindow.UpdateStatusIcon();
}
// This removes an image from background loading
// This does not work for images that are being unloaded!
public void BackgroundCancelImage(ImageData img)
{
// Queued?
if(img.LoadingTicket != null)
{
// Not being trashed?
if(img.LoadState != ImageData.LOADSTATE_TRASH)
{
lock(loadlist)
{
// Remove it from queue
LinkedListNode<ImageData> ticket = img.LoadingTicket;
img.LoadingTicket = null;
loadlist.Remove(ticket);
}
// Update icon
General.MainWindow.UpdateStatusIcon();
}
}
}
#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;
}
// Make empty palette when still no palette found
if(palette == null)
{
General.WriteLogLine("WARNING: None of the loaded resources define a color palette!");
palette = new Playpal();
}
2007-10-07 22:21:47 +00:00
}
#endregion
#region ================== Textures
// This loads the textures
private int LoadTextures()
2007-10-07 22:21:47 +00:00
{
ICollection<ImageData> images;
2007-10-27 13:59:24 +00:00
PatchNames pnames = new PatchNames();
PatchNames newpnames;
int counter = 0;
2007-10-07 22:21:47 +00:00
// 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.
2007-10-27 13:59:24 +00:00
newpnames = dr.LoadPatchNames();
if(newpnames != null) pnames = newpnames;
// Load textures
images = dr.LoadTextures(pnames);
if(images != null)
2007-10-07 22:21:47 +00:00
{
2007-10-27 13:59:24 +00:00
// Go for all textures
foreach(ImageData img in images)
2007-10-07 22:21:47 +00:00
{
2007-10-27 13:59:24 +00:00
// Add or replace in textures list
2007-12-27 01:24:11 +00:00
if(!textures.ContainsKey(img.LongName)) texturenames.Add(img.Name);
2007-10-27 13:59:24 +00:00
textures.Remove(img.LongName);
textures.Add(img.LongName, img);
counter++;
2007-12-27 01:24:11 +00:00
// Also add as flat when using mixed resources
if(General.Map.Config.MixTexturesFlats)
{
2007-12-27 01:24:11 +00:00
if(!flats.ContainsKey(img.LongName)) flatnames.Add(img.Name);
flats.Remove(img.LongName);
flats.Add(img.LongName, img);
}
2007-10-07 22:21:47 +00:00
}
}
}
// Output info
return counter;
2007-10-07 22:21:47 +00:00
}
// This returns a specific patch stream
2008-01-02 21:49:43 +00:00
internal Stream GetPatchData(string pname)
2007-10-07 22:21:47 +00:00
{
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;
}
// This returns an image by string
public ImageData GetTextureImage(string name)
2007-10-24 17:25:03 +00:00
{
// Get the long name
long longname = Lump.MakeLongName(name);
return GetTextureImage(longname);
2007-10-24 17:25:03 +00:00
}
// This returns an image by long
public ImageData GetTextureImage(long longname)
2007-10-24 17:25:03 +00:00
{
// Does this texture exist?
if(textures.ContainsKey(longname))
2007-10-24 17:25:03 +00:00
{
// Return texture
return textures[longname];
}
else
{
// Return null image
return new NullImage();
2007-10-24 17:25:03 +00:00
}
}
// This returns a bitmap by string
public Bitmap GetTextureBitmap(string name)
{
ImageData img = GetTextureImage(name);
img.LoadImage();
return img.Bitmap;
}
// This returns a bitmap by string
public Bitmap GetTextureBitmap(long longname)
{
ImageData img = GetTextureImage(longname);
img.LoadImage();
return img.Bitmap;
}
// This returns a texture by string
public Texture GetTextureTexture(string name)
{
ImageData img = GetTextureImage(name);
img.LoadImage();
img.CreateTexture();
return img.Texture;
}
// This returns a texture by string
public Texture GetTextureTexture(long longname)
{
ImageData img = GetTextureImage(longname);
img.LoadImage();
img.CreateTexture();
return img.Texture;
}
2007-10-24 17:25:03 +00:00
#endregion
#region ================== Flats
// This loads the flats
private int LoadFlats()
{
ICollection<ImageData> images;
int counter = 0;
// Go for all opened containers
foreach(DataReader dr in containers)
{
// Load flats
images = dr.LoadFlats();
if(images != null)
{
// Go for all flats
foreach(ImageData img in images)
{
// Add or replace in flats list
2007-12-27 01:24:11 +00:00
if(!flats.ContainsKey(img.LongName)) flatnames.Add(img.Name);
flats.Remove(img.LongName);
flats.Add(img.LongName, img);
counter++;
// Also add as texture when using mixed resources
if(General.Map.Config.MixTexturesFlats)
{
2007-12-27 01:24:11 +00:00
if(!textures.ContainsKey(img.LongName)) texturenames.Add(img.Name);
textures.Remove(img.LongName);
textures.Add(img.LongName, img);
}
}
}
}
// Output info
return counter;
}
// This returns a specific flat stream
2008-01-02 21:49:43 +00:00
internal Stream GetFlatData(string pname)
{
Stream flat;
// Go for all opened containers
for(int i = containers.Count - 1; i >= 0; i--)
{
// This contain provides this flat?
flat = containers[i].GetFlatData(pname);
if(flat != null) return flat;
}
// No such patch found
return null;
}
// This returns an image by string
public ImageData GetFlatImage(string name)
2007-10-24 17:25:03 +00:00
{
// Get the long name
long longname = Lump.MakeLongName(name);
return GetFlatImage(longname);
2007-10-24 17:25:03 +00:00
}
// This returns an image by long
public ImageData GetFlatImage(long longname)
2007-10-24 17:25:03 +00:00
{
// Does this flat exist?
if(flats.ContainsKey(longname))
2007-10-24 17:25:03 +00:00
{
// Return flat
return flats[longname];
}
else
{
// Return null image
return new NullImage();
2007-10-24 17:25:03 +00:00
}
}
// This returns a bitmap by string
public Bitmap GetFlatBitmap(string name)
{
ImageData img = GetFlatImage(name);
img.LoadImage();
return img.Bitmap;
}
// This returns a bitmap by string
public Bitmap GetFlatBitmap(long longname)
{
ImageData img = GetFlatImage(longname);
img.LoadImage();
return img.Bitmap;
}
// This returns a texture by string
public Texture GetFlatTexture(string name)
{
ImageData img = GetFlatImage(name);
img.LoadImage();
img.CreateTexture();
return img.Texture;
}
// This returns a texture by string
public Texture GetFlatTexture(long longname)
{
ImageData img = GetFlatImage(longname);
img.LoadImage();
img.CreateTexture();
return img.Texture;
}
2007-10-24 17:25:03 +00:00
#endregion
#region ================== Sprites
// This loads the sprites
private int LoadSprites()
{
Stream spritedata = null;
SpriteImage image;
// Go for all things
foreach(ThingTypeInfo ti in General.Map.Config.Things)
{
// Sprite not added to collection yet?
if(!sprites.ContainsKey(ti.SpriteLongName))
{
// Go for all opened containers
for(int i = containers.Count - 1; i >= 0; i--)
{
// This contain provides this sprite?
spritedata = containers[i].GetSpriteData(ti.Sprite);
if(spritedata != null) break;
}
// Found anything?
if(spritedata != null)
{
// Make new sprite image
image = new SpriteImage(ti.Sprite);
// Add to collection
sprites.Add(ti.SpriteLongName, image);
}
}
}
// Output info
return sprites.Count;
}
2007-10-26 13:31:02 +00:00
// This returns an image by long
public ImageData GetSpriteImage(string name)
2007-10-24 17:25:03 +00:00
{
2007-10-26 13:31:02 +00:00
Stream spritedata = null;
2007-10-24 17:25:03 +00:00
long longname = Lump.MakeLongName(name);
2007-10-26 13:31:02 +00:00
SpriteImage image;
2007-10-24 17:25:03 +00:00
// Sprite already loaded?
if(sprites.ContainsKey(longname))
2007-10-24 17:25:03 +00:00
{
// Return exiting sprite
return sprites[longname];
}
else
{
// Go for all opened containers
for(int i = containers.Count - 1; i >= 0; i--)
2007-10-26 13:31:02 +00:00
{
// This contain provides this sprite?
spritedata = containers[i].GetSpriteData(name);
if(spritedata != null) break;
2007-10-26 13:31:02 +00:00
}
// Found anything?
if(spritedata != null)
{
// Make new sprite image
image = new SpriteImage(name);
2007-10-26 13:31:02 +00:00
// Add to collection
sprites.Add(longname, image);
// Return result
return image;
}
else
{
// Return null image
return new NullImage();
2007-10-26 13:31:02 +00:00
}
2007-10-24 17:25:03 +00:00
}
}
// This returns a bitmap by string
public Bitmap GetSpriteBitmap(string name)
{
ImageData img = GetSpriteImage(name);
img.LoadImage();
return img.Bitmap;
}
// This returns a texture by string
public Texture GetSpriteTexture(string name)
{
ImageData img = GetSpriteImage(name);
img.LoadImage();
img.CreateTexture();
return img.Texture;
}
2007-10-07 22:21:47 +00:00
#endregion
#region ================== Tools
// This finds the first IWAD resource
// Returns false when not found
internal bool FindFirstIWAD(out DataLocation result)
{
// Go for all data containers
foreach(DataReader dr in containers)
{
// Container is a WAD file?
if(dr is WADReader)
{
// Check if it is an IWAD
WADReader wr = dr as WADReader;
if(wr.IsIWAD)
{
// Return location!
result = wr.Location;
return true;
}
}
}
// No IWAD found
result = new DataLocation();
return false;
}
#endregion
2007-10-01 20:57:41 +00:00
}
}