From dcd15a2663d04ff93686c3cea7e0278a8ed4697d Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 28 Apr 2024 17:09:29 -0700 Subject: [PATCH] x) Fix EOL on file --- .gitattributes | 1 + cfg/Config.cs | 375 +++++++++++++++++++++---------------------- ser/XmlFormatter2.cs | 369 +++++++++++++++++++++--------------------- 3 files changed, 372 insertions(+), 373 deletions(-) diff --git a/.gitattributes b/.gitattributes index c82a0ac..d612aee 100644 --- a/.gitattributes +++ b/.gitattributes @@ -19,6 +19,7 @@ # Source code # Source code # Source code +*.cs text eol=lf *.rs text *.bash text eol=lf *.bat text eol=crlf diff --git a/cfg/Config.cs b/cfg/Config.cs index 05f2e26..01392b2 100644 --- a/cfg/Config.cs +++ b/cfg/Config.cs @@ -1,188 +1,187 @@ -using System; -using System.IO; -using System.Xml; -using System.Reflection; - -/* - -TODO: -x) - -*/ - -namespace lib -{ - - public class DescAttribute: Attribute - { - public string Desc { get; private set; } - - public DescAttribute( string desc ) - { - Desc = desc; - } - } - - [Serializable] - public class ConfigCfg: Config - { - public readonly bool writeOutTemplateFiles = true; - } - - - - [Serializable] - public class Config - { - /* - static public Config Load( string filename ) - { - return null; - } - */ - - static ConfigCfg s_cfg = new ConfigCfg(); - - static public void startup( string filename ) - { - res.Mgr.register( load ); - res.Mgr.registerSub( typeof( Config ) ); - - s_cfg = Config.load( filename ); - - } - - - #region SaveLoad - /* - static public res.Ref res_load( string filename ) - { - return new res.Ref( filename, load( filename ) ); - } - */ - - static public T res_load( string filename ) where T : Config - { - return load( filename ); - } - - /* - static public ResRefConfig res_load( string filename, Type t ) - { - return new ResRefConfig( filename, load( filename, t ) ); - } - */ - - - static public Config load( string filename ) - { - return load( filename, null ); - } - - static public T load( string filename ) where T : Config - { - return (T)load( filename, typeof( T ) ); - } - - static public Config load( string filename, Type t ) - { - Config cfg = null; - - try - { - if( File.Exists( filename ) ) - { - FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); - - XmlFormatter2 formatter = new XmlFormatter2(); - - cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) ); - - cfg.SetFilename( filename ); - } - else - { - cfg = CreateTemplate( filename, t ); - } - } - catch( IOException ) - { - cfg = CreateTemplate( filename, t ); - } - - return cfg; - } - - private static Config CreateTemplate( string filename, Type t ) - { - Type[] types = new Type[0]; - object[] parms = new object[0]; - - //types[ 0 ] = typeof( string ); - //parms[ 0 ] = filename; - Config cfg = null; - - ConstructorInfo cons = t?.GetConstructor(types); - - try - { - cfg = (Config)cons?.Invoke( parms ); - } - catch( Exception e ) - { - log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" ); - } - - //cfg.SetFilename( filename ); - - if( s_cfg.writeOutTemplateFiles ) - { - var templateFile = $"templates/{filename}"; - - var dirName = Path.GetDirectoryName(templateFile); - - lib.Util.checkAndAddDirectory( dirName ); - - log.info( $"Writing out template config of type {t?.Name} in {templateFile}" ); - - Config.save( cfg, templateFile ); - } - - return cfg; - } - - static public void save( Config cfg ) - { - Config.save( cfg, cfg.m_filename ); - } - - static public void save( Config cfg, String filename ) - { - FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write); - - XmlFormatter2 formatter = new XmlFormatter2(); - - formatter.Serialize( fs, cfg ); - - fs.Close(); - } - #endregion - - private string m_filename = "{unknown}"; - - public Config() - { - } - - public Config( string filename ) - { - m_filename = filename; - } - - public String Filename { get { return m_filename; } } - - protected void SetFilename( String filename ) { m_filename = filename; } - - } -} - +using System; +using System.IO; +using System.Xml; +using System.Reflection; + +/* + +TODO: +x) + +*/ + +namespace lib +{ + + public class DescAttribute : Attribute + { + public string Desc { get; private set; } + + public DescAttribute( string desc ) + { + Desc = desc; + } + } + + [Serializable] + public class ConfigCfg : Config + { + public readonly bool writeOutTemplateFiles = true; + } + + public interface ConfigBase + { + + } + + [Serializable] + public class Config : ConfigBase + { + + //private int _test = 0; + + private static ConfigCfg s_cfg = new(); + + static public void startup( string filename ) + { + res.Mgr.register( load ); + res.Mgr.registerSub( typeof( Config ) ); + + s_cfg = load( filename ); + + } + + + #region SaveLoad + /* + static public res.Ref res_load( string filename ) + { + return new res.Ref( filename, load( filename ) ); + } + */ + + static public T res_load( string filename ) where T : Config + { + return load( filename ); + } + + /* + static public ResRefConfig res_load( string filename, Type t ) + { + return new ResRefConfig( filename, load( filename, t ) ); + } + */ + + + static public Config load( string filename ) + { + return load( filename, null ); + } + + static public T load( string filename ) where T : Config + { + return (T)load( filename, typeof( T ) ); + } + + static public Config load( string filename, Type t ) + { + Config cfg = null; + + try + { + if( File.Exists( filename ) ) + { + FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read ); + + XmlFormatter2 formatter = new XmlFormatter2(); + + cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) ); + + cfg.SetFilename( filename ); + } + else + { + cfg = CreateTemplate( filename, t ); + } + } + catch( IOException ) + { + cfg = CreateTemplate( filename, t ); + } + + return cfg; + } + + private static Config CreateTemplate( string filename, Type t ) + { + Type[] types = new Type[0]; + object[] parms = new object[0]; + + //types[ 0 ] = typeof( string ); + //parms[ 0 ] = filename; + Config cfg = null; + + ConstructorInfo cons = t?.GetConstructor( types ); + + try + { + cfg = (Config)cons?.Invoke( parms ); + } + catch( Exception e ) + { + log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" ); + } + + //cfg.SetFilename( filename ); + + if( s_cfg.writeOutTemplateFiles ) + { + var templateFile = $"templates/{filename}"; + + var dirName = Path.GetDirectoryName( templateFile ); + + Util.checkAndAddDirectory( dirName ); + + log.info( $"Writing out template config of type {t?.Name} in {templateFile}" ); + + save( cfg, templateFile ); + } + + return cfg; + } + + static public void save( Config cfg ) + { + save( cfg, cfg._filename ); + } + + static public void save( Config cfg, String filename ) + { + FileStream fs = new( filename, FileMode.Create, FileAccess.Write ); + + XmlFormatter2 formatter = new(); + + formatter.Serialize( fs, cfg ); + + fs.Close(); + } + #endregion + + private string _filename = "{unknown}"; + + public Config() + { + } + + public Config( string filename ) + { + _filename = filename; + } + + public String Filename { get { return _filename; } } + + protected void SetFilename( String filename ) { _filename = filename; } + + } +} + diff --git a/ser/XmlFormatter2.cs b/ser/XmlFormatter2.cs index 3bbea50..d46f998 100644 --- a/ser/XmlFormatter2.cs +++ b/ser/XmlFormatter2.cs @@ -10,8 +10,6 @@ using System.Collections.Generic; using System.Reflection; using System.Diagnostics; -using System.Runtime.InteropServices; -using static System.Net.WebRequestMethods; using System.Linq; using System.Collections.Immutable; @@ -19,15 +17,15 @@ using System.Collections.Immutable; /* element and attribute names - - Element names are case-sensitive + - Element names are case-sensitive - Element names must start with a letter or underscore - - Element names cannot start with the letters xml(or XML, or Xml, etc) - - Element names can contain letters, digits, hyphens, underscores, and periods + - Element names cannot start with the letters xml(or XML, or Xml, etc) + - Element names can contain letters, digits, hyphens, underscores, and periods - Element names cannot contain spaces * TODO * x) Add the ability for correctly named attributes to be able to fill in classes - * x) + * x) */ @@ -46,15 +44,15 @@ namespace lib [Flags] public enum Types { - Fields = 0b_0001, - Props = 0b_0010, + Fields = 0b_0001, + Props = 0b_0010, Implied = 0b_0100, - Explicit= 0b_1000, - + Explicit = 0b_1000, - None = 0b_0000, + + None = 0b_0000, Default = Fields, - All = Fields | Props, + All = Fields | Props, } public class Ser : Attribute @@ -70,22 +68,22 @@ namespace lib { } - public class ChildAttributes : Attribute + public class ChildAttribute : Attribute { public string[] Values { get; private set; } - public ChildAttributes( params string[] values ) + public ChildAttribute( params string[] values ) { this.Values = values; } } - public class ChildFieldsAttribute : ChildAttributes + public class ChildFieldsAttribute : ChildAttribute { public ChildFieldsAttribute( params string[] values ) : base( values ) { } } - public class ChildPropsAttribute : ChildAttributes + public class ChildPropsAttribute : ChildAttribute { public ChildPropsAttribute( params string[] values ) : base( values ) { } } @@ -103,7 +101,7 @@ namespace lib } public record struct TypeProxy( Func ser, Func des ); - + //public record struct CollectionCreator( Func FnCreate ); //These 2 enums are for serialization @@ -122,14 +120,14 @@ namespace lib } - public class XmlFormatter2Cfg: Config + public class XmlFormatter2Cfg : Config { public Datastructure datastructure = Datastructure.Tree; public int Version = 2; public Dictionary> WLProps = new(); - public Dictionary> WLFields= new(); + public Dictionary> WLFields = new(); public Dictionary TypeProxy = new(); @@ -141,14 +139,11 @@ namespace lib public Types TypesDefault = Types.Fields; } - public class XmlFormatter2: IFormatter + public class XmlFormatter2 : IFormatter { public StreamingContext Context { get; set; } - static Random s_rnd = new Random(); - int m_rndVal = s_rnd.Next(); - - XmlFormatter2Cfg m_cfg = new XmlFormatter2Cfg(); + private XmlFormatter2Cfg _cfg = new(); #region Unimplimented public ISurrogateSelector SurrogateSelector @@ -174,7 +169,7 @@ namespace lib { //Context = new StreamingContext( StreamingContextStates.All ); - m_cfg = cfg; + _cfg = cfg; log.warn( $"XML serialization is NOT fast" ); } @@ -189,16 +184,13 @@ namespace lib } - public T Deserialize(Stream stream) - { - return (T)DeserializeKnownType(stream, typeof(T)); - } + public T Deserialize( Stream stream ) => (T)DeserializeKnownType( stream, typeof( T ) ); public object DeserializeKnownType( Stream stream, Type t ) { - XmlTextReader reader = new XmlTextReader(stream); + XmlTextReader reader = new XmlTextReader( stream ); - object obj = Deserialize(reader, t); + object obj = Deserialize( reader, t ); return obj; } @@ -220,12 +212,12 @@ namespace lib return Deserialize( doc.DocumentElement, null, t, null ); } - public void DeserializeInto(Stream stream, T obj) + public void DeserializeInto( Stream stream, T obj ) { - XmlTextReader reader = new XmlTextReader( stream ); + XmlTextReader reader = new( stream ); reader.Read(); - XmlDocument doc = new XmlDocument(); + XmlDocument doc = new(); doc.Load( reader ); @@ -236,7 +228,7 @@ namespace lib { //lib.log.info( "object Deserialize( XmlElement elem ) {0} {1}", m_rndVal, m_alreadySerialized.Count ); - string typename = elem.HasAttribute("_.t") ? elem.GetAttribute("_.t") : elem.Name; + string typename = elem.HasAttribute( "_.t" ) ? elem.GetAttribute( "_.t" ) : elem.Name; return Deserialize( elem, null, typename ); } @@ -248,8 +240,8 @@ namespace lib Type type = null; - // @@@@: This should go backwards, we tend to lookup our own stuff, then builtins. - // Also, cache a typename into its assembly. + // @@@@: This should go backwards, we tend to lookup our own stuff, then builtins. + // Also, cache a typename into its assembly. foreach( Assembly a in assems ) { type = a.GetType( typename ); @@ -266,10 +258,7 @@ namespace lib return Deserialize( elem, null, type, null ); } - private bool IsEnumerable( Type type ) - { - return type.IsAssignableTo( typeof(IEnumerable) ); - } + static private bool IsEnumerable( Type type ) => type.IsAssignableTo( typeof( IEnumerable ) ); private object Deserialize( XmlElement elem, MemberInfo mi, Type type, object existing /*, object enclosing = null*/ ) { @@ -281,7 +270,7 @@ namespace lib { try { - TypeCode typeCode = Type.GetTypeCode(type); + TypeCode typeCode = Type.GetTypeCode( type ); if( typeCode != TypeCode.Object ) { @@ -291,13 +280,13 @@ namespace lib { if( !type.IsArray ) { - if( IsEnumerable(type) ) + if( IsEnumerable( type ) ) { return DeserializeCollection( elem, mi, type ); } else { - object obj = DeserializeObject(elem, mi, type, existing); + object obj = DeserializeObject( elem, mi, type, existing ); if( obj is I_Serialize ) { @@ -328,7 +317,7 @@ namespace lib { mm_types[0] = t; - var fn = GetType().GetMethod("GetDefaultGeneric").MakeGenericMethod(mm_types); + var fn = GetType().GetMethod( "GetDefaultGeneric" ).MakeGenericMethod( mm_types ); return fn.Invoke( this, null ); } @@ -342,11 +331,11 @@ namespace lib { string val = ""; - if( elem.HasAttribute("v") ) + if( elem.HasAttribute( "v" ) ) { - val = elem.GetAttribute("v"); + val = elem.GetAttribute( "v" ); } - else if( elem.HasAttribute(name) ) + else if( elem.HasAttribute( name ) ) { val = elem.GetAttribute( name ); } @@ -355,7 +344,7 @@ namespace lib val = elem.InnerText; } - if ( !type.IsEnum ) + if( !type.IsEnum ) { try { @@ -392,7 +381,7 @@ namespace lib foreach( Assembly a in ass ) { - Type t = a.GetType(shortname); + Type t = a.GetType( shortname ); if( t != null ) { @@ -502,25 +491,25 @@ namespace lib } else { - HydrateObjectOfNarrowType(elem, mi, finalType, obj); + HydrateObjectOfNarrowType( elem, mi, finalType, obj ); } return obj; } - private void HydrateObjectOfNarrowType(XmlElement elem, MemberInfo mi, Type narrowType, object obj) + private void HydrateObjectOfNarrowType( XmlElement elem, MemberInfo mi, Type narrowType, object obj ) { XmlNodeList allChildren = elem.ChildNodes; bool filterFields, filterProps, doImpls, doFields, doProps; HashSet whitelistFields, whitelistProps; - GetFilters(m_cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps); + GetFilters( _cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps ); - if (doFields || doImpls) + if( doFields || doImpls ) { - var fields = refl.GetAllFields(narrowType); + var fields = refl.GetAllFields( narrowType ); - foreach (FieldInfo childFi in fields) + foreach( FieldInfo childFi in fields ) { String name = childFi.Name; @@ -529,7 +518,7 @@ namespace lib string propName = ""; - if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) + if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) { var gtIndex = name.IndexOf( '>' ); @@ -548,34 +537,37 @@ namespace lib //if( name.EndsWith( ) ) //This is to convert c# names that would be bad as XML tags - name = refl.TypeToIdentifier(name); + name = refl.TypeToIdentifier( name ); // @@@ TODO This doesnt yet handle propNames! - if (FilterField(filterFields, doImpls, whitelistFields, childFi as MemberInfo, name)) continue; + if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) + continue; string attValue = elem.GetAttribute( name ); - if( !string.IsNullOrWhiteSpace( propName ) && string.IsNullOrWhiteSpace( attValue ) ) attValue = elem.GetAttribute( propName ); + if( !string.IsNullOrWhiteSpace( propName ) && string.IsNullOrWhiteSpace( attValue ) ) + attValue = elem.GetAttribute( propName ); if( !string.IsNullOrWhiteSpace( attValue ) ) { - object existingObj = childFi.GetValue(obj); + object existingObj = childFi.GetValue( obj ); - object childObj = DeserializeConcrete(elem, childFi, attValue, childFi.FieldType); + object childObj = DeserializeConcrete( elem, childFi, attValue, childFi.FieldType ); - childFi.SetValue(obj, childObj); + childFi.SetValue( obj, childObj ); } else { - XmlElement childElem = getNamedChild(allChildren, name); - if( childElem == null && !string.IsNullOrWhiteSpace( propName ) ) childElem = getNamedChild( allChildren, propName ); + XmlElement childElem = getNamedChild( allChildren, name ); + if( childElem == null && !string.IsNullOrWhiteSpace( propName ) ) + childElem = getNamedChild( allChildren, propName ); - if (childElem != null) + if( childElem != null ) { - object existingObj = childFi.GetValue(obj); + object existingObj = childFi.GetValue( obj ); - object childObj = Deserialize(childElem, childFi, childFi.FieldType, existingObj); + object childObj = Deserialize( childElem, childFi, childFi.FieldType, existingObj ); - childFi.SetValue(obj, childObj); + childFi.SetValue( obj, childObj ); } } @@ -583,11 +575,11 @@ namespace lib } - if (doProps || doImpls) + if( doProps || doImpls ) { - var props = refl.GetAllProperties(narrowType); + var props = refl.GetAllProperties( narrowType ); - foreach (var childPi in props) + foreach( var childPi in props ) { String name = childPi.Name; var dontAtt = childPi.GetCustomAttributes(); @@ -596,30 +588,31 @@ namespace lib continue; } - name = refl.TypeToIdentifier(name); + name = refl.TypeToIdentifier( name ); - if (FilterField(filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name)) continue; + if( FilterField( filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name ) ) + continue; - XmlElement childElem = getNamedChild(allChildren, name); + XmlElement childElem = getNamedChild( allChildren, name ); - if (childElem != null) + if( childElem != null ) { - object existingObj = childPi.GetValue(obj); + object existingObj = childPi.GetValue( obj ); - object childObj = Deserialize(childElem, childPi, childPi.PropertyType, existingObj); + object childObj = Deserialize( childElem, childPi, childPi.PropertyType, existingObj ); var setMethod = childPi.GetSetMethod(); - if (setMethod != null) + if( setMethod != null ) { //Object o = Activator.CreateInstance( setMethod.ReflectedType ); - setMethod.Invoke(obj, new object[] { childObj }); + setMethod.Invoke( obj, new object[] { childObj } ); //setMethod.CreateDelegate() } else { - childPi.SetValue(obj, childObj); + childPi.SetValue( obj, childObj ); } } @@ -631,10 +624,12 @@ namespace lib { if( doImpls ) { - if( mi.GetCustomAttribute( true ) == null ) return true; + if( mi.GetCustomAttribute( true ) == null ) + return true; } - if( filterFields && !whitelistFields.Contains( name ) ) return true; + if( filterFields && !whitelistFields.Contains( name ) ) + return true; return false; } @@ -674,7 +669,7 @@ namespace lib { if( arrNodeList.Item( i ) is XmlElement ) { - XmlElement arrElem = (XmlElement)arrNodeList.Item(i); + XmlElement arrElem = (XmlElement)arrNodeList.Item( i ); list.Add( Deserialize( arrElem, mi, genT[0], null ) ); } @@ -685,7 +680,7 @@ namespace lib private object DeserializeCollection( XmlElement elem, MemberInfo mi, Type type ) { - Type typeElem = typeof(object); + Type typeElem = typeof( object ); if( type.GenericTypeArguments.Length == 1 ) { @@ -693,40 +688,40 @@ namespace lib } else if( type.GenericTypeArguments.Length == 2 ) { - typeElem = typeof(KeyValuePair<,>).MakeGenericType(type.GenericTypeArguments ); + typeElem = typeof( KeyValuePair<,> ).MakeGenericType( type.GenericTypeArguments ); } - string refString = elem.GetAttribute("ref"); - int refInt = refString.Length > 0 ? Convert.ToInt32(refString) : -1; + string refString = elem.GetAttribute( "ref" ); + int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1; XmlNodeList arrNodeList = elem.ChildNodes; int length = arrNodeList.Count; - Array arr = createArray(typeElem, refInt, length); + Array arr = createArray( typeElem, refInt, length ); for( int i = 0; i < arr.Length; ++i ) { if( arrNodeList.Item( i ) is XmlElement ) { - XmlElement arrElem = (XmlElement)arrNodeList.Item(i); + XmlElement arrElem = (XmlElement)arrNodeList.Item( i ); var finalType = typeElem; - if (arrElem.HasAttribute("_.t")) + if( arrElem.HasAttribute( "_.t" ) ) { - var typename = arrElem.GetAttribute("_.t"); - finalType = FindType(typename); + var typename = arrElem.GetAttribute( "_.t" ); + finalType = FindType( typename ); - if (finalType == null) + if( finalType == null ) finalType = typeElem; } - arr.SetValue( Deserialize( arrElem, mi, finalType, null), i ); + arr.SetValue( Deserialize( arrElem, mi, finalType, null ), i ); } } - var listType = (typeof(List<>)).MakeGenericType( typeElem ); + var listType = ( typeof( List<> ) ).MakeGenericType( typeElem ); IList list = Activator.CreateInstance( listType ) as IList; foreach( var a in arr ) @@ -734,28 +729,28 @@ namespace lib list.Add( a ); } - MethodInfo ?toMeth = null; + MethodInfo? toMeth = null; var typeGen = Type.MakeGenericSignatureType( type ); - if( type == typeof(ImmutableArray<>).MakeGenericType( typeElem ) ) + if( type == typeof( ImmutableArray<> ).MakeGenericType( typeElem ) ) { - var genMeth = GetType().GetMethod("MakeImmutableArray", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); + var genMeth = GetType().GetMethod( "MakeImmutableArray", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); toMeth = genMeth.MakeGenericMethod( typeElem ); } - else if( type == typeof(ImmutableDictionary<,>).MakeGenericType( typeElem.GenericTypeArguments ) ) + else if( type == typeof( ImmutableDictionary<,> ).MakeGenericType( typeElem.GenericTypeArguments ) ) { - var genMeth = GetType().GetMethod("MakeImmutableDictionary", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); + var genMeth = GetType().GetMethod( "MakeImmutableDictionary", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); toMeth = genMeth.MakeGenericMethod( typeElem.GenericTypeArguments ); } - else if( type == typeof(List<>).MakeGenericType( typeElem ) ) + else if( type == typeof( List<> ).MakeGenericType( typeElem ) ) { - var genMeth = GetType().GetMethod("MakeList", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); + var genMeth = GetType().GetMethod( "MakeList", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); toMeth = genMeth.MakeGenericMethod( typeElem ); } - else if( type == typeof(Dictionary<,>).MakeGenericType( typeElem.GenericTypeArguments ) ) + else if( type == typeof( Dictionary<,> ).MakeGenericType( typeElem.GenericTypeArguments ) ) { - var genMeth = GetType().GetMethod("MakeDictionary", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); + var genMeth = GetType().GetMethod( "MakeDictionary", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); toMeth = genMeth.MakeGenericMethod( typeElem.GenericTypeArguments ); } @@ -777,14 +772,14 @@ namespace lib return arr; } - private object MakeImmutableDictionary( List> list ) + private object MakeImmutableDictionary( List> list ) { var dict = list.ToImmutableDictionary(); return dict; } - private object MakeDictionary( List> list ) + private object MakeDictionary( List> list ) { var dict = list.ToDictionary(); return dict; @@ -795,32 +790,32 @@ namespace lib { Type typeElem = type.GetElementType(); - string refString = elem.GetAttribute("ref"); - int refInt = refString.Length > 0 ? Convert.ToInt32(refString) : -1; + string refString = elem.GetAttribute( "ref" ); + int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1; XmlNodeList arrNodeList = elem.ChildNodes; int length = arrNodeList.Count; - Array arr = createArray(typeElem, refInt, length); + Array arr = createArray( typeElem, refInt, length ); for( int i = 0; i < arr.Length; ++i ) { if( arrNodeList.Item( i ) is XmlElement ) { - XmlElement arrElem = (XmlElement)arrNodeList.Item(i); + XmlElement arrElem = (XmlElement)arrNodeList.Item( i ); var finalType = typeElem; - if (arrElem.HasAttribute("_.t")) + if( arrElem.HasAttribute( "_.t" ) ) { - var typename = arrElem.GetAttribute("_.t"); - finalType = FindType(typename); + var typename = arrElem.GetAttribute( "_.t" ); + finalType = FindType( typename ); - if (finalType == null) + if( finalType == null ) finalType = typeElem; } - arr.SetValue( Deserialize( arrElem, mi, finalType, null), i ); + arr.SetValue( Deserialize( arrElem, mi, finalType, null ), i ); } } @@ -829,16 +824,16 @@ namespace lib private object createObject( XmlElement elem, string typename, int refInt, object obj ) { - Type type = Type.GetType(typename); + Type type = Type.GetType( typename ); return createObject( elem, type, refInt, obj ); } private object createObject( XmlElement elem, Type type, int refInt, object existingObj ) { - TypeCode tc = Type.GetTypeCode(type); + TypeCode tc = Type.GetTypeCode( type ); - if( m_cfg.datastructure == Datastructure.Graph && refInt > 0 && m_alreadySerialized.ContainsKey( refInt ) ) + if( _cfg.datastructure == Datastructure.Graph && refInt > 0 && m_alreadySerialized.ContainsKey( refInt ) ) { //lib.log.info( "Reusing object for {0}", refInt ); return m_alreadySerialized[refInt]; @@ -855,10 +850,10 @@ namespace lib var tryType = type; TypeProxy? proxy = null; - while( tryType != typeof(object) && obj == null ) + while( tryType != typeof( object ) && obj == null ) { //m_cfg.TypeProxy.TryGetValue( ) - if( m_cfg.TypeProxy.TryGetValue( tryType, out var newProxy ) ) + if( _cfg.TypeProxy.TryGetValue( tryType, out var newProxy ) ) { proxy = newProxy; break; @@ -890,7 +885,8 @@ namespace lib // @@@ GROSS Fix the types so theyre known good. var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type ); - if( isSubclass ) return existingObj; + if( isSubclass ) + return existingObj; // old //if( type == existingObjType ) return existingObj; @@ -926,7 +922,7 @@ namespace lib return obj; } - if( m_cfg.datastructure == Datastructure.Graph && refInt > 0 ) + if( _cfg.datastructure == Datastructure.Graph && refInt > 0 ) { m_alreadySerialized[refInt] = obj; } @@ -937,24 +933,24 @@ namespace lib private Array createArray( string elemTypename, int refInt, int length ) { - Type elemType = Type.GetType(elemTypename); + Type elemType = Type.GetType( elemTypename ); return createArray( elemType, refInt, length ); } private Array createArray( Type elemType, int refInt, int length ) { - TypeCode elemTC = Type.GetTypeCode(elemType); + TypeCode elemTC = Type.GetTypeCode( elemType ); - if( m_cfg.datastructure == Datastructure.Graph && refInt > 0 && m_alreadySerialized.ContainsKey( refInt ) ) + if( _cfg.datastructure == Datastructure.Graph && refInt > 0 && m_alreadySerialized.ContainsKey( refInt ) ) { return (Array)m_alreadySerialized[refInt]; } else { - Array arr = Array.CreateInstance(elemType, length); + Array arr = Array.CreateInstance( elemType, length ); - if( m_cfg.datastructure == Datastructure.Graph ) + if( _cfg.datastructure == Datastructure.Graph ) { m_alreadySerialized[refInt] = arr; @@ -992,13 +988,13 @@ namespace lib m_alreadySerialized.Clear(); m_objectID = new ObjectIDGenerator(); - XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.ASCII); + XmlTextWriter writer = new XmlTextWriter( stream, System.Text.Encoding.ASCII ); writer.Formatting = Formatting.Indented; Serialize( writer, mi, root ); - //Rely on the parent closing the stream. + //Rely on the parent closing the stream. //writer.Close(); writer.Flush(); @@ -1018,15 +1014,17 @@ namespace lib { Type type = root.GetType(); - TypeCode typeCode = Type.GetTypeCode(type); + TypeCode typeCode = Type.GetTypeCode( type ); if( typeCode != TypeCode.Object ) { - if( m_cfg.POD == POD.Elements || forceType ) writer.WriteStartElement( name ); + if( _cfg.POD == POD.Elements || forceType ) + writer.WriteStartElement( name ); SerializeConcrete( writer, mi, root, name, forceType ); - if( m_cfg.POD == POD.Elements || forceType ) writer.WriteEndElement(); + if( _cfg.POD == POD.Elements || forceType ) + writer.WriteEndElement(); return; } @@ -1035,7 +1033,7 @@ namespace lib writer.WriteStartElement( name ); if( !type.IsArray ) { - if( IsEnumerable( type )) + if( IsEnumerable( type ) ) { SerializeCollection( writer, mi, root, depth ); } @@ -1063,9 +1061,10 @@ namespace lib private void SerializeConcrete( XmlWriter writer, MemberInfo mi, object root, string name, bool forceType ) { //TODO: Only write this out if debugging. - if( forceType || m_cfg.POD == POD.Elements ) + if( forceType || _cfg.POD == POD.Elements ) { - if( forceType ) writer.WriteAttributeString( "_.t", getTypeName( root.GetType() ) ); + if( forceType ) + writer.WriteAttributeString( "_.t", getTypeName( root.GetType() ) ); writer.WriteAttributeString( "v", root.ToString() ); } else @@ -1088,16 +1087,16 @@ namespace lib bool first; - long refInt = m_objectID.GetId(root, out first); + long refInt = m_objectID.GetId( root, out first ); - if( m_cfg.datastructure == Datastructure.Graph ) + if( _cfg.datastructure == Datastructure.Graph ) { writer.WriteAttributeString( "ref", refInt.ToString() ); } if( first ) { - if( m_cfg.datastructure == Datastructure.Graph ) + if( _cfg.datastructure == Datastructure.Graph ) { m_alreadySerialized[refInt] = root; } @@ -1116,24 +1115,22 @@ namespace lib { writer.WriteAttributeString( "_.t", getTypeName( root.GetType() ) ); - if(depth == 1) + if( depth == 1 ) { - writer.WriteAttributeString( "_.version.", $"{m_cfg.Version}" ); + writer.WriteAttributeString( "_.version.", $"{_cfg.Version}" ); } - bool first; + long refInt = m_objectID.GetId( root, out var first ); - long refInt = m_objectID.GetId(root, out first); - - // @@@@ FIX for proxies. - if( m_cfg.datastructure == Datastructure.Graph ) + // @@@@ FIX for proxies. + if( _cfg.datastructure == Datastructure.Graph ) { writer.WriteAttributeString( "ref", refInt.ToString() ); } if( first ) { - if (m_cfg.datastructure == Datastructure.Graph) + if( _cfg.datastructure == Datastructure.Graph ) { m_alreadySerialized[refInt] = root; } @@ -1141,28 +1138,28 @@ namespace lib Type type = root.GetType(); //* - Type typeISerializable = typeof(ISerializable); + Type typeISerializable = typeof( ISerializable ); - if (root is ISerializable ser) + if( root is ISerializable ser ) { - if ((root is Delegate)) + if( root is Delegate ) { return; } - var serInfo = new SerializationInfo(type, new FormatterConverter()); + var serInfo = new SerializationInfo( type, new FormatterConverter() ); - var context = new StreamingContext(StreamingContextStates.File, root); + var context = new StreamingContext( StreamingContextStates.File, root ); - ser.GetObjectData(serInfo, context); + ser.GetObjectData( serInfo, context ); - foreach (var serMember in serInfo) + foreach( var serMember in serInfo ) { String name = serMember.Name; - name = refl.TypeToIdentifier(name); + name = refl.TypeToIdentifier( name ); - Serialize(writer, mi, serMember.Value, name, depth, true); + Serialize( writer, mi, serMember.Value, name, depth, true ); } return; @@ -1172,9 +1169,9 @@ namespace lib var tryType = type; TypeProxy? proxy = null; - while (tryType != typeof(object)) + while( tryType != typeof( object ) ) { - if (m_cfg.TypeProxy.TryGetValue(tryType, out var newProxy)) + if( _cfg.TypeProxy.TryGetValue( tryType, out var newProxy ) ) { proxy = newProxy; break; @@ -1183,37 +1180,37 @@ namespace lib tryType = tryType.BaseType; } - if (proxy.HasValue) + if( proxy.HasValue ) { - var proxyStr = proxy.Value.ser(root); - writer.WriteAttributeString("proxy", proxyStr); + var proxyStr = proxy.Value.ser( root ); + writer.WriteAttributeString( "proxy", proxyStr ); return; } } //*/ - SerializeObjectOfNarrowType(writer, mi, root, depth, type); + SerializeObjectOfNarrowType( writer, mi, root, depth, type ); } } - private void SerializeObjectOfNarrowType(XmlWriter writer, MemberInfo mi, object root, int depth, Type narrowType) + private void SerializeObjectOfNarrowType( XmlWriter writer, MemberInfo mi, object root, int depth, Type narrowType ) { bool filterFields, filterProps, doImpls, doFields, doProps; HashSet whitelistFields, whitelistProps; - GetFilters(m_cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps); + GetFilters( _cfg.TypesDefault, mi, narrowType, out filterFields, out filterProps, out doImpls, out doFields, out doProps, out whitelistFields, out whitelistProps ); - if (doFields || doImpls) + if( doFields || doImpls ) { - var fields = refl.GetAllFields(narrowType); + var fields = refl.GetAllFields( narrowType ); - foreach (var childFi in fields) + foreach( var childFi in fields ) { String name = childFi.Name; var dontAtt = childFi.GetCustomAttributes(); string propName = ""; - if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) + if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) { var gtIndex = name.IndexOf( '>' ); @@ -1230,32 +1227,33 @@ namespace lib continue; } - if (FilterField(filterFields, doImpls, whitelistFields, childFi as MemberInfo, name)) continue; + if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) + continue; - object[] objs = childFi.GetCustomAttributes(typeof(NonSerializedAttribute), true); + object[] objs = childFi.GetCustomAttributes( typeof( NonSerializedAttribute ), true ); - if (objs.Length > 0) + if( objs.Length > 0 ) { continue; } //if( childFi.GetCustomAttribute() ) - name = refl.TypeToIdentifier(name); + name = refl.TypeToIdentifier( name ); - var finalName = (m_cfg.Naming == BackingFieldNaming.Short && !string.IsNullOrEmpty( propName )) ? + var finalName = ( _cfg.Naming == BackingFieldNaming.Short && !string.IsNullOrEmpty( propName ) ) ? propName : name; - Serialize(writer, childFi, childFi.GetValue(root), finalName, depth + 1, false); + Serialize( writer, childFi, childFi.GetValue( root ), finalName, depth + 1, false ); } } - if (doProps || doImpls) + if( doProps || doImpls ) { - var props = refl.GetAllProperties(narrowType); + var props = refl.GetAllProperties( narrowType ); - foreach (var childPi in props) + foreach( var childPi in props ) { String name = childPi.Name; @@ -1265,18 +1263,19 @@ namespace lib continue; } - if (FilterField(filterProps, doImpls, whitelistProps, childPi as MemberInfo, name)) continue; + if( FilterField( filterProps, doImpls, whitelistProps, childPi as MemberInfo, name ) ) + continue; - object[] objs = childPi.GetCustomAttributes(typeof(NonSerializedAttribute), true); + object[] objs = childPi.GetCustomAttributes( typeof( NonSerializedAttribute ), true ); - if (objs.Length > 0) + if( objs.Length > 0 ) { continue; } - name = refl.TypeToIdentifier(name); + name = refl.TypeToIdentifier( name ); - Serialize(writer, childPi, childPi.GetValue(root), name, depth + 1, false); + Serialize( writer, childPi, childPi.GetValue( root ), name, depth + 1, false ); } } @@ -1311,16 +1310,16 @@ namespace lib bool first; - long refInt = m_objectID.GetId(root, out first); + long refInt = m_objectID.GetId( root, out first ); - if( m_cfg.datastructure == Datastructure.Graph ) + if( _cfg.datastructure == Datastructure.Graph ) { writer.WriteAttributeString( "ref", refInt.ToString() ); } if( first ) { - if( m_cfg.datastructure == Datastructure.Graph ) + if( _cfg.datastructure == Datastructure.Graph ) { m_alreadySerialized[refInt] = root; }