#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; using CodeImp.DoomBuilder.IO; #endregion namespace CodeImp.DoomBuilder.Data { internal class DataManager : IDisposable { #region ================== Constants #endregion #region ================== Variables // Data containers private List containers; // Palette private Playpal palette; // Textures private Dictionary textures; // Flats private Dictionary flats; // Sprites private Dictionary sprites; // Disposing private bool isdisposed = false; #endregion #region ================== Properties // Disposing public bool IsDisposed { get { return isdisposed; } } #endregion #region ================== Constructor / Disposer // Constructor public DataManager() { // We have no destructor GC.SuppressFinalize(this); } // Disposer public void Dispose() { // Not already disposed? if(!isdisposed) { // Clean up Unload(); // Done isdisposed = true; } } #endregion #region ================== Loading / Unloading // This loads all data resources public void Load(DataLocationList configlist, DataLocationList maplist, DataLocation maplocation) { DataLocationList all = DataLocationList.Combined(configlist, maplist); all.Add(maplocation); Load(all); } // This loads all data resources public void Load(DataLocationList configlist, DataLocationList maplist) { DataLocationList all = DataLocationList.Combined(configlist, maplist); Load(all); } // This loads all data resources public void Load(DataLocationList locations) { DataReader c; // Create collections containers = new List(); textures = new Dictionary(); flats = new Dictionary(); sprites = new Dictionary(); // Go for all locations foreach(DataLocation dl in locations) { // Nothing chosen yet c = null; // TODO: Make this work more elegant using reflection. // Make DataLocation.type of type Type and assign the // types of the desired reader classes. // 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; } // Add container if(c != null) containers.Add(c); } // Load stuff General.WriteLogLine("Loading PLAYPAL palette..."); LoadPalette(); General.WriteLogLine("Loading textures..."); LoadTextures(); } // This unloads all data public void Unload() { // Dispose resources foreach(KeyValuePair i in textures) i.Value.Dispose(); foreach(KeyValuePair i in flats) i.Value.Dispose(); foreach(KeyValuePair i in sprites) i.Value.Dispose(); palette = null; // Dispose containers foreach(DataReader c in containers) c.Dispose(); containers.Clear(); } #endregion #region ================== Suspend / Resume // This suspends a data resource location public void SuspendLocation(string location) { // Go for all containers foreach(DataReader d in containers) { // Check if this is the location to suspend if(string.Compare(d.Location.location, location, true) == 0) { // Suspend General.WriteLogLine("Suspended data resource '" + location + "'"); d.Suspend(); return; } } General.WriteLogLine("WARNING: Cannot suspended data resource '" + location + "', no such location opened!"); } // This resume a data resource location public void ResumeLocation(string location) { // Go for all containers foreach(DataReader d in containers) { // Check if this is the location to resume if(string.Compare(d.Location.location, location, true) == 0) { // Resume General.WriteLogLine("Resumed data resource '" + location + "'"); d.Resume(); return; } } General.WriteLogLine("WARNING: Cannot resume data resource '" + location + "', no such location opened!"); } #endregion #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 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 } }