2009-04-19 18:07:22 +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 CodeImp.DoomBuilder.IO ;
2016-11-14 10:22:24 +00:00
using CodeImp.DoomBuilder.Types ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Config
{
2021-03-14 14:58:40 +00:00
public enum UDMFFieldAssociationModifier
{
None ,
Absolute
}
public struct UDMFFieldAssociation
{
public string Property ;
public UDMFFieldAssociationModifier Modify ;
public bool NeverShowEventLines ;
2021-12-17 15:33:42 +00:00
public bool ConsolidateEventLines ;
2021-03-14 14:58:40 +00:00
2021-12-17 15:33:42 +00:00
public UDMFFieldAssociation ( string property , UDMFFieldAssociationModifier modify , bool nevershoweventlines , bool consolidateeventlines )
2021-03-14 14:58:40 +00:00
{
Property = property ;
Modify = modify ;
NeverShowEventLines = nevershoweventlines ;
2021-12-17 15:33:42 +00:00
ConsolidateEventLines = consolidateeventlines ;
2021-03-14 14:58:40 +00:00
}
}
2009-04-19 18:07:22 +00:00
public class UniversalFieldInfo : IComparable < UniversalFieldInfo >
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
// Properties
private string name ;
private int type ;
private object defaultvalue ;
2022-01-27 18:34:28 +00:00
private bool thingtypespecific ;
2023-10-24 13:51:49 +00:00
private bool managed ;
2009-04-19 18:07:22 +00:00
private EnumList enumlist ;
2021-03-14 14:58:40 +00:00
private Dictionary < string , UDMFFieldAssociation > associations ;
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public string Name { get { return name ; } }
public int Type { get { return type ; } }
public object Default { get { return defaultvalue ; } }
2022-01-27 18:34:28 +00:00
public bool ThingTypeSpecific { get { return thingtypespecific ; } }
2023-10-24 13:51:49 +00:00
public bool Managed { get { return managed ; } }
2009-04-19 18:07:22 +00:00
public EnumList Enum { get { return enumlist ; } }
2021-03-14 14:58:40 +00:00
public Dictionary < string , UDMFFieldAssociation > Associations { get { return associations ; } }
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
2016-11-14 10:22:24 +00:00
internal UniversalFieldInfo ( string path , string name , string configname , Configuration cfg , IDictionary < string , EnumList > enums )
2009-04-19 18:07:22 +00:00
{
string setting = "universalfields." + path + "." + name ;
// Initialize
this . name = name . ToLowerInvariant ( ) ;
2021-03-14 14:58:40 +00:00
associations = new Dictionary < string , UDMFFieldAssociation > ( ) ;
2009-04-19 18:07:22 +00:00
// Read type
2022-01-27 18:34:28 +00:00
type = cfg . ReadSetting ( setting + ".type" , int . MinValue ) ;
defaultvalue = cfg . ReadSettingObject ( setting + ".default" , null ) ;
thingtypespecific = cfg . ReadSetting ( setting + ".thingtypespecific" , false ) ;
2023-10-24 13:51:49 +00:00
managed = cfg . ReadSetting ( setting + ".managed" , true ) ;
2016-11-14 10:22:24 +00:00
2022-01-23 18:02:45 +00:00
// Read enum
object enumsetting = cfg . ReadSettingObject ( setting + ".enum" , null ) ;
if ( enumsetting ! = null )
{
// Reference to existing enums list?
if ( enumsetting is string )
{
// Link to it
enumlist = enums [ enumsetting . ToString ( ) ] ;
}
else if ( enumsetting is IDictionary )
{
// Make list
enumlist = new EnumList ( enumsetting as IDictionary ) ;
}
}
2016-11-14 10:22:24 +00:00
//mxd. Check type
2022-01-23 18:02:45 +00:00
if ( this . type = = int . MinValue )
2016-11-14 10:22:24 +00:00
{
General . ErrorLogger . Add ( ErrorType . Warning , "No type is defined for universal field \"" + name + "\" defined in \"" + configname + "\". Integer type will be used." ) ;
this . type = ( int ) UniversalType . Integer ;
}
2022-01-23 18:02:45 +00:00
if ( type = = ( int ) UniversalType . EnumOption & & enumsetting = = null )
{
General . ErrorLogger . Add ( ErrorType . Warning , "Universal field \"" + name + "\" defined in \"" + configname + "\" is of type enum (" + this . type + "), but has no enum values set. Falling back to integer type" ) ;
type = ( int ) UniversalType . Integer ;
}
2016-11-14 10:22:24 +00:00
TypeHandler th = General . Types . GetFieldHandler ( this ) ;
if ( th is NullHandler )
{
General . ErrorLogger . Add ( ErrorType . Warning , "Universal field \"" + name + "\" defined in \"" + configname + "\" has unknown type " + this . type + ". String type will be used instead." ) ;
this . type = ( int ) UniversalType . String ;
if ( this . defaultvalue = = null ) this . defaultvalue = "" ;
}
//mxd. Default value is missing? Get it from typehandler
if ( this . defaultvalue = = null ) this . defaultvalue = th . GetDefaultValue ( ) ;
2021-03-14 14:58:40 +00:00
// Read associations
IDictionary assocdict = cfg . ReadSetting ( setting + ".associations" , new Hashtable ( ) ) ;
foreach ( DictionaryEntry section in assocdict )
{
string property = cfg . ReadSetting ( setting + ".associations." + section . Key + ".property" , string . Empty ) ;
string modifystr = cfg . ReadSetting ( setting + ".associations." + section . Key + ".modify" , string . Empty ) ;
bool nevershoweventlines = cfg . ReadSetting ( setting + ".associations." + section . Key + ".nevershoweventlines" , false ) ;
2021-12-17 15:33:42 +00:00
bool consolidateeventlines = cfg . ReadSetting ( setting + ".associations." + section . Key + ".consolidateeventlines" , false ) ;
2021-03-14 14:58:40 +00:00
UDMFFieldAssociationModifier ufam = UDMFFieldAssociationModifier . None ;
if ( ! string . IsNullOrWhiteSpace ( property ) )
{
switch ( modifystr )
{
case "abs" :
ufam = UDMFFieldAssociationModifier . Absolute ;
break ;
}
2021-12-17 15:33:42 +00:00
associations [ property ] = new UDMFFieldAssociation ( property , ufam , nevershoweventlines , consolidateeventlines ) ;
2021-03-14 14:58:40 +00:00
}
}
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
2022-06-12 18:07:41 +00:00
internal UniversalFieldInfo ( string name , int type , object defaultvalue )
{
this . name = name . ToLowerInvariant ( ) ;
this . type = type ;
this . defaultvalue = defaultvalue ;
}
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This presents the item as string
public override string ToString ( )
{
return name ;
}
// This compares against another field
public int CompareTo ( UniversalFieldInfo other )
{
return string . Compare ( this . name , other . name ) ;
}
#endregion
}
}