UltimateZoneBuilder/Source/Map/MapOptions.cs

249 lines
6.6 KiB
C#
Raw Normal View History

2007-06-14 23:31:57 +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 CodeImp.DoomBuilder.IO;
using System.IO;
2007-10-01 20:53:10 +00:00
using CodeImp.DoomBuilder.Data;
2008-06-01 20:25:46 +00:00
using CodeImp.DoomBuilder.Config;
2007-06-14 23:31:57 +00:00
#endregion
namespace CodeImp.DoomBuilder.Map
{
2008-01-02 21:49:43 +00:00
internal sealed class MapOptions
2007-06-14 23:31:57 +00:00
{
#region ================== Constants
#endregion
#region ================== Variables
2008-06-01 20:25:46 +00:00
// Map configuration
private Configuration mapconfig;
2007-06-14 23:31:57 +00:00
// Game configuration
private string configfile;
// Map header name
private string currentname;
2007-10-14 15:44:55 +00:00
private string previousname; // When zero length string, map has not renamed
2007-06-14 23:31:57 +00:00
// Additional resources
2007-10-01 20:53:10 +00:00
private DataLocationList resources;
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Properties
public string ConfigFile { get { return configfile; } set { configfile = value; } }
2007-10-01 20:53:10 +00:00
public DataLocationList Resources { get { return resources; } }
2007-10-14 15:44:55 +00:00
public string PreviousName { get { return previousname; } set { previousname = value; } }
2007-06-14 23:31:57 +00:00
public string CurrentName
{
get { return currentname; }
set
{
// Change the name, but keep previous name
if(currentname != value)
{
if(previousname == "") previousname = currentname;
currentname = value;
}
}
}
#endregion
#region ================== Constructor / Disposer
// Constructor
internal MapOptions()
2007-06-14 23:31:57 +00:00
{
// Initialize
this.previousname = "";
this.currentname = "";
this.configfile = "";
2007-10-01 20:53:10 +00:00
this.resources = new DataLocationList();
2008-06-02 04:55:51 +00:00
this.mapconfig = new Configuration(true);
2007-06-14 23:31:57 +00:00
}
2007-06-16 19:53:51 +00:00
// Constructor to load from Doom Builder Map Settings Configuration
internal MapOptions(Configuration cfg, string mapname)
2007-06-16 19:53:51 +00:00
{
IDictionary mapinfo, resinfo;
2007-10-01 20:53:10 +00:00
DataLocation res;
2007-06-16 19:53:51 +00:00
// Initialize
this.previousname = "";
this.currentname = mapname;
this.configfile = cfg.ReadSetting("config", "");
2007-10-01 20:53:10 +00:00
this.resources = new DataLocationList();
2008-06-02 04:55:51 +00:00
this.mapconfig = new Configuration(true);
2008-06-01 20:25:46 +00:00
2007-06-16 19:53:51 +00:00
// Go for all items in the map info
2008-06-02 04:55:51 +00:00
this.mapconfig.Root = cfg.ReadSetting("maps." + mapname, new Hashtable());
IDictionary reslist = this.mapconfig.ReadSetting("resources", new Hashtable());
foreach(DictionaryEntry mp in reslist)
2007-06-16 19:53:51 +00:00
{
// Item is a structure?
if(mp.Value is IDictionary)
{
// Create resource
resinfo = (IDictionary)mp.Value;
2007-10-01 20:53:10 +00:00
res = new DataLocation();
2007-06-16 19:53:51 +00:00
// Copy information from Configuration to ResourceLocation
if(resinfo.Contains("type") && (resinfo["type"] is int)) res.type = (int)resinfo["type"];
if(resinfo.Contains("location") && (resinfo["location"] is string)) res.location = (string)resinfo["location"];
if(resinfo.Contains("textures") && (resinfo["textures"] is bool)) res.textures = (bool)resinfo["textures"];
if(resinfo.Contains("flats") && (resinfo["flats"] is bool)) res.flats = (bool)resinfo["flats"];
// Add resource
AddResource(res);
}
}
}
2007-06-14 23:31:57 +00:00
~MapOptions()
{
// Clean up
this.resources = null;
}
#endregion
#region ================== Methods
2008-06-01 20:25:46 +00:00
// This stores the map options in a configuration
public void WriteConfiguration(string settingsfile)
{
Configuration wadcfg;
// Write settings to config
resources.WriteToConfig(mapconfig, "resources");
// Load the file or make a new file
if(File.Exists(settingsfile))
wadcfg = new Configuration(settingsfile, true);
else
wadcfg = new Configuration(true);
// Write configuration type information
wadcfg.WriteSetting("type", "Doom Builder Map Settings Configuration");
// Update the settings file with this map configuration
wadcfg.WriteSetting("maps." + currentname, mapconfig.Root);
// Save file
wadcfg.SaveConfiguration(settingsfile);
}
2007-06-14 23:31:57 +00:00
// This adds a resource location and returns the index where the item was added
2007-10-01 20:53:10 +00:00
public int AddResource(DataLocation res)
2007-06-14 23:31:57 +00:00
{
// Get a fully qualified path
res.location = Path.GetFullPath(res.location);
// Go for all items in the list
for(int i = 0; i < resources.Count; i++)
{
// Check if location is already added
if(Path.GetFullPath(resources[i].location) == res.location)
{
// Update the item in the list
resources[i] = res;
return i;
}
}
// Add to list
resources.Add(res);
return resources.Count - 1;
}
// This clears all reasource
public void ClearResources()
{
// Clear list
resources.Clear();
}
// This removes a resource by index
public void RemoveResource(int index)
{
// Remove the item
resources.RemoveAt(index);
}
2007-09-27 12:34:38 +00:00
// This copies resources from a list
2007-10-01 20:53:10 +00:00
public void CopyResources(DataLocationList fromlist)
2007-09-27 12:34:38 +00:00
{
// Clear this list
resources.Clear();
resources.AddRange(fromlist);
}
2007-10-05 11:17:58 +00:00
2007-06-16 19:53:51 +00:00
// This displays the current map name
public override string ToString()
{
return currentname;
}
2008-06-01 20:25:46 +00:00
// This returns the UDMF field type
public int GetUniversalFieldType(string elementname, string fieldname, int defaulttype)
{
int type;
// Check if the field type is set in the game configuration
type = General.Map.Config.ReadSetting("universalfields." + elementname + "." + fieldname + ".type", -1);
if(type == -1)
{
// Read from map configuration
type = mapconfig.ReadSetting("fieldtypes." + elementname + "." + fieldname, defaulttype);
2008-06-01 20:25:46 +00:00
}
return type;
}
// This stores the UDMF field type
public void SetUniversalFieldType(string elementname, string fieldname, int type)
{
// Check if the type of this field is not set in the game configuration
if(General.Map.Config.ReadSetting("universalfields." + elementname + "." + fieldname + ".type", -1) == -1)
{
// Write type to map configuration
mapconfig.WriteSetting("fieldtypes." + elementname + "." + fieldname, type);
2008-06-01 20:25:46 +00:00
}
}
// This removes all UDMF field types
public void ForgetUniversalFieldTypes()
{
mapconfig.DeleteSetting("fieldtypes");
}
2007-06-14 23:31:57 +00:00
#endregion
}
}