From 0e98d66d065abc0110663daa9ba2b2bb514f3abc Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 7 Jul 2019 18:02:45 -0700 Subject: [PATCH] Various fixes --- Conn.cs | 12 ++++---- Helpers.cs | 10 +++--- Log.cs | 6 ++-- Scr.cs | 11 +++++++ SerializableDictionary.cs | 4 +-- Utilities.cs | 2 +- XmlFormatter2.cs | 65 +++++++++++++++++++-------------------- math/AngleSingle.cs | 2 +- 8 files changed, 60 insertions(+), 52 deletions(-) diff --git a/Conn.cs b/Conn.cs index fafdc85..d45e4d4 100644 --- a/Conn.cs +++ b/Conn.cs @@ -52,9 +52,9 @@ public class Conn { obj = formatter.Deserialize( stream ); } - catch( System.Xml.XmlException e ) + catch( System.Xml.XmlException ex ) { - lib.Log.error( "Outer Exception {0}", e.ToString() ); + lib.Log.error( $"Outer Exception {ex.Message}" ); } return obj; @@ -70,11 +70,11 @@ public class Conn var ms = new MemoryStream( 1024 ); formatter.Serialize( ms, obj ); - //var str = System.Text.Encoding.Default.GetString( mm_buffer, 0, (int)ms.Position ); - //lib.Log.info( "Sent data {0} of length {1}", str, ms.Position ); - //lib.Log.info( "Sent {0}", obj ); + //var str = System.Text.Encoding.Default.GetString( mm_buffer, 0, (int)ms.Position ); + //lib.Log.info( $"Sent data {str} of length {ms.Position}" ); + //lib.Log.info( $"Sent {obj}" ); - byte[] byteSize = BitConverter.GetBytes( (uint)ms.Position ); + byte[] byteSize = BitConverter.GetBytes( (uint)ms.Position ); m_streamNet.Write( byteSize, 0, 4 ); m_streamNet.Write( ms.GetBuffer(), 0, (int)ms.Position ); diff --git a/Helpers.cs b/Helpers.cs index 56e08f1..37f2009 100644 --- a/Helpers.cs +++ b/Helpers.cs @@ -147,28 +147,28 @@ namespace lib } else { - Log.error( String.Format( "No attributes in node while loading file {0}", filename ) ); + Log.error( String.Format( $"No attributes in node while loading file {filename}" ) ); } } else { - Log.error( String.Format( "Incorrect key {0} found while loading file {1}", node.Name, filename ) ); + Log.error( String.Format( $"Incorrect key {node.Name} found while loading file {filename}" ) ); } } } else { if( keyMI == null ) - Log.error( String.Format( "Key type conversion not found for type {0}", keyType ) ); + Log.error( String.Format( $"Key type conversion not found for type {keyType}" ) ); if( valMI == null ) - Log.error( String.Format( "Val type conversion not found for type {0}", valType ) ); + Log.error( String.Format( $"Val type conversion not found for type {valType}" ) ); } } else { - Log.error( String.Format( "No dictionary element found while loading file {0}", filename ) ); + Log.error( String.Format( $"No dictionary element found while loading file {filename}" ) ); } } diff --git a/Log.cs b/Log.cs index 873292b..2af8cef 100644 --- a/Log.cs +++ b/Log.cs @@ -235,7 +235,7 @@ namespace lib { m_delegates.Add( cb ); } - + /* private void writeFileAndLine( StackTrace st ) { @@ -244,7 +244,7 @@ namespace lib string srcFile = frame.GetFileName(); string srcLine = frame.GetFileLineNumber().Tostring(); - Console.WriteLine( "{0} ({1}):", srcFile, srcLine ); + Console.WriteLine( $"{srcFile} ({srcLine}):" ); } private void writeStack( StackTrace st ) @@ -258,7 +258,7 @@ namespace lib if( srcFile != null ) { - Console.WriteLine( "{0} ({1})", srcFile, srcLine ); + Console.WriteLine( $"{srcFile} ({srcLine})" ); } } } diff --git a/Scr.cs b/Scr.cs index 87a63d5..3d477ed 100644 --- a/Scr.cs +++ b/Scr.cs @@ -200,4 +200,15 @@ static public class scr + + //SLOW + static public string TypeToIdentifier( string typename ) + { + return typename.Replace( '<', '_' ).Replace( '>', '_' ).Replace( ',', '_' ).Replace( ' ', '_' ).Replace( '.', '_' ).Replace( '+', '_' ).Replace( '[', '_' ).Replace( ']', '_' ).Replace( '$', '_' ).Replace( ':', '_' ); + } + + + + + } diff --git a/SerializableDictionary.cs b/SerializableDictionary.cs index 1724390..c467c7d 100644 --- a/SerializableDictionary.cs +++ b/SerializableDictionary.cs @@ -56,7 +56,7 @@ namespace lib { int itemCount = info.GetInt32("ItemCount"); for (int i = 0; i < itemCount; i++) { - KeyValuePair kvp = (KeyValuePair)info.GetValue(String.Format("Item{0}", i), typeof(KeyValuePair)); + KeyValuePair kvp = (KeyValuePair)info.GetValue(String.Format( $"Item{i}" ), typeof(KeyValuePair)); this.Add(kvp.Key, kvp.Value); } } @@ -67,7 +67,7 @@ namespace lib info.AddValue("ItemCount", this.Count); int itemIdx = 0; foreach (KeyValuePair kvp in this) { - info.AddValue(String.Format("Item{0}", itemIdx), kvp, typeof(KeyValuePair)); + info.AddValue(String.Format( $"Item{itemIdx}" ), kvp, typeof(KeyValuePair)); itemIdx++; } } diff --git a/Utilities.cs b/Utilities.cs index 09d0248..4f19eaa 100644 --- a/Utilities.cs +++ b/Utilities.cs @@ -88,7 +88,7 @@ namespace lib { if( !Directory.Exists( path ) ) { - lib.Log.info( "Creating directory {0}", path ); + lib.Log.info( $"Creating directory {path}" ); Directory.CreateDirectory( path ); } diff --git a/XmlFormatter2.cs b/XmlFormatter2.cs index f05a741..3380e2c 100644 --- a/XmlFormatter2.cs +++ b/XmlFormatter2.cs @@ -271,8 +271,7 @@ public class XmlFormatter2 : IFormatter String childType = node.GetAttribute( "t" ); - name = name.Replace( '+', '-' ); - name = name.Replace( '`', '_' ); + name = scr.TypeToIdentifier( name ); XmlElement childElem = getNamedChild( allChildren, name ); @@ -302,40 +301,39 @@ public class XmlFormatter2 : IFormatter objUnOnDeser.OnDeserialization( objUnOnDeser ); } - /* - ser.GetObjectData( serInfo, Context ); + /* + ser.GetObjectData( serInfo, Context ); - //var serEnum = ; + //var serEnum = ; - foreach( var serMember in serInfo ) - { - String name = serMember.Name; + foreach( var serMember in serInfo ) + { + String name = serMember.Name; - name = name.Replace( '+', '-' ); - name = name.Replace( '`', '_' ); + name = scr.TypeToIdentifier( name ); - XmlElement childElem = getNamedChild( allChildren, name ); + XmlElement childElem = getNamedChild( allChildren, name ); - var des = Deserialize( childElem, name ); + var des = Deserialize( childElem, name ); + } + */ } - */ - } - else + else { - MemberInfo[] miArr = FormatterServices.GetSerializableMembers( finalType ); - XmlNodeList allChildren = elem.ChildNodes; - for( int i = 0; i < miArr.Length; ++i ) - { - FieldInfo childFi = (FieldInfo)miArr[ i ]; + var fields = scr.GetAllFields( type ); - String name = childFi.Name; + //MemberInfo[] miArr = FormatterServices.GetSerializableMembers( type, Context ); - name = name.Replace( '+', '-' ); - name = name.Replace( '`', '_' ); + foreach( var childFi in fields ) + { - XmlElement childElem = getNamedChild( allChildren, name ); + String name = childFi.Name; + + name = scr.TypeToIdentifier( name ); + + XmlElement childElem = getNamedChild( allChildren, name ); if( childElem != null ) @@ -344,7 +342,7 @@ public class XmlFormatter2 : IFormatter childFi.SetValue( obj, childObj ); } - else if( miArr.Length == 1 ) + else if( fields.Count == 1 ) { object childObj = Deserialize( elem, childFi.FieldType, obj ); @@ -593,10 +591,9 @@ public class XmlFormatter2 : IFormatter { String name = serMember.Name; - name = name.Replace( '+', '-' ); - name = name.Replace( '`', '_' ); + name = scr.TypeToIdentifier( name ); - Serialize( writer, serMember.Value, name, true ); + Serialize( writer, serMember.Value, name, true ); } //var sc = new SerializationContext( @@ -606,11 +603,12 @@ public class XmlFormatter2 : IFormatter else //*/ { - MemberInfo[] miArr = FormatterServices.GetSerializableMembers( type, Context ); + var fields = scr.GetAllFields( type ); - for( int i = 0; i < miArr.Length; ++i ) + //MemberInfo[] miArr = FormatterServices.GetSerializableMembers( type, Context ); + + foreach( var childFi in fields ) { - FieldInfo childFi = (FieldInfo)miArr[ i ]; object[] objs = childFi.GetCustomAttributes( typeof( NonSerializedAttribute ), true ); @@ -621,10 +619,9 @@ public class XmlFormatter2 : IFormatter String name = childFi.Name; - name = name.Replace( '+', '-' ); - name = name.Replace( '`', '_' ); + name = scr.TypeToIdentifier( name ); - Serialize( writer, childFi.GetValue( root ), name, false ); + Serialize( writer, childFi.GetValue( root ), name, false ); } } } diff --git a/math/AngleSingle.cs b/math/AngleSingle.cs index 213fa3b..225d9d9 100644 --- a/math/AngleSingle.cs +++ b/math/AngleSingle.cs @@ -720,7 +720,7 @@ namespace math if (format == null) return ToString(); - return string.Format(CultureInfo.CurrentCulture, "{0}°", MathUtil.RadiansToDegrees(radians).ToString(format, CultureInfo.CurrentCulture)); + return string.Format(CultureInfo.CurrentCulture, $"{MathUtil.RadiansToDegrees(radians).ToString(format, CultureInfo.CurrentCulture)}°" ); } ///