From be5e5d5bc441516453ae006901007c96eaafe0af Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 4 Sep 2022 20:17:40 -0700 Subject: [PATCH] Small fixers --- SharpLib.csproj | 9 +- db/DB.cs | 2 - logging/Log.cs | 47 +++++-- math/Double2.cs | 24 ++-- math/Double3.cs | 24 ++-- math/Double4.cs | 16 +-- math/Vector2.cs | 24 ++-- math/Vector3.cs | 24 ++-- math/Vector4.cs | 16 +-- scr/Script.cs | 305 +++++++++++++++++++++++++++++++++++++++++++ ser/XmlFormatter2.cs | 8 ++ 11 files changed, 419 insertions(+), 80 deletions(-) create mode 100644 scr/Script.cs diff --git a/SharpLib.csproj b/SharpLib.csproj index 905a906..79ffaf3 100644 --- a/SharpLib.csproj +++ b/SharpLib.csproj @@ -1,11 +1,7 @@  - - netcoreapp2.0; - net462; - netstandard2.0; - + net6.0 lib 0.0.1.0 0.0.1.0 @@ -17,9 +13,10 @@ + - + diff --git a/db/DB.cs b/db/DB.cs index 55074a2..32ba515 100644 --- a/db/DB.cs +++ b/db/DB.cs @@ -133,9 +133,7 @@ namespace db } return CommitResults.Perfect; - } - } diff --git a/logging/Log.cs b/logging/Log.cs index b3598e1..188639d 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -28,6 +28,15 @@ static public class log Fatal = 7, } + [Flags] + public enum Endpoints + { + Invalid = 0, + File = 1 << 0, + Console = 1 << 1, + } + + public struct LogEvent { public DateTime Time; @@ -83,9 +92,10 @@ static public class log - static public void create( string filename ) + + static public void create( string filename, Endpoints endpoints ) { - startup( filename ); + startup( filename, endpoints ); } @@ -141,16 +151,16 @@ static public class log logBase( msg, LogType.Warn, path, line, member, cat, obj ); } + static public void high(string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "") + { + logBase(msg, LogType.High, path, line, member, cat, obj); + } + static public void info( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Info, path, line, member, cat, obj ); } - static public void high( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) - { - logBase( msg, LogType.High, path, line, member, cat, obj ); - } - static public void debug( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Debug, path, line, member, cat, obj ); @@ -232,7 +242,7 @@ static public class log } - static void startup( string filename ) + static void startup( string filename, Endpoints endpoints ) { var start = new ThreadStart( run ); @@ -261,6 +271,13 @@ static public class log //s_events.Enqueue( evt ); + if( (endpoints & Endpoints.Console) == Endpoints.Console ) + { + addDelegate(WriteToConsole); + } + + + info( $"startup" ); } @@ -406,6 +423,20 @@ static public class log } } + public static void WriteToConsole(LogEvent evt) + { + char sym = getSymbol(evt.LogType); + + var truncatedCat = evt.Cat.Substring(0, Math.Min(8, evt.Cat.Length)); + + string finalLine = string.Format("{0,-8}{1}| {2}", truncatedCat, sym, evt.Msg); + + Console.WriteLine(finalLine); + } + + + + private static Stream s_stream; private static StreamWriter s_writer; diff --git a/math/Double2.cs b/math/Double2.cs index 7d55231..f437776 100644 --- a/math/Double2.cs +++ b/math/Double2.cs @@ -806,11 +806,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -853,11 +853,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -918,11 +918,11 @@ namespace math public static void Transform(Double2[] source, ref Quaternion rotation, Double2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); double x = rotation.X + rotation.X; double y = rotation.Y + rotation.Y; @@ -985,11 +985,11 @@ namespace math public static void Transform(Double2[] source, ref Matrix transform, Double4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1060,11 +1060,11 @@ namespace math public static void TransformCoordinate(Double2[] source, ref Matrix transform, Double2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1131,11 +1131,11 @@ namespace math public static void TransformNormal(Double2[] source, ref Matrix transform, Double2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/math/Double3.cs b/math/Double3.cs index 0ef5661..6f267e8 100644 --- a/math/Double3.cs +++ b/math/Double3.cs @@ -977,11 +977,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1024,11 +1024,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1096,11 +1096,11 @@ namespace math public static void Transform(Double3[] source, ref Quaternion rotation, Double3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); double x = rotation.X + rotation.X; double y = rotation.Y + rotation.Y; @@ -1187,11 +1187,11 @@ namespace math public static void Transform(Double3[] source, ref Matrix transform, Double4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1260,11 +1260,11 @@ namespace math public static void TransformCoordinate(Double3[] source, ref Matrix transform, Double3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1332,11 +1332,11 @@ namespace math public static void TransformNormal(Double3[] source, ref Matrix transform, Double3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/math/Double4.cs b/math/Double4.cs index 77359f2..36b5adb 100644 --- a/math/Double4.cs +++ b/math/Double4.cs @@ -878,11 +878,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -925,11 +925,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -998,11 +998,11 @@ namespace math public static void Transform(Double4[] source, ref Quaternion rotation, Double4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); double x = rotation.X + rotation.X; double y = rotation.Y + rotation.Y; @@ -1077,11 +1077,11 @@ namespace math public static void Transform(Double4[] source, ref Matrix transform, Double4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/math/Vector2.cs b/math/Vector2.cs index 2376bca..b2dc55a 100644 --- a/math/Vector2.cs +++ b/math/Vector2.cs @@ -821,11 +821,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -868,11 +868,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -933,11 +933,11 @@ namespace math public static void Transform(Vec2[] source, ref Quaternion rotation, Vec2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); float x = rotation.X + rotation.X; float y = rotation.Y + rotation.Y; @@ -1000,11 +1000,11 @@ namespace math public static void Transform(Vec2[] source, ref Matrix transform, Vec4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1075,11 +1075,11 @@ namespace math public static void TransformCoordinate(Vec2[] source, ref Matrix transform, Vec2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1146,11 +1146,11 @@ namespace math public static void TransformNormal(Vec2[] source, ref Matrix transform, Vec2[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/math/Vector3.cs b/math/Vector3.cs index 3085aee..3f11f06 100644 --- a/math/Vector3.cs +++ b/math/Vector3.cs @@ -992,11 +992,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1039,11 +1039,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1111,11 +1111,11 @@ namespace math public static void Transform(Vec3[] source, ref Quaternion rotation, Vec3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); float x = rotation.X + rotation.X; float y = rotation.Y + rotation.Y; @@ -1202,11 +1202,11 @@ namespace math public static void Transform(Vec3[] source, ref Matrix transform, Vec4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1275,11 +1275,11 @@ namespace math public static void TransformCoordinate(Vec3[] source, ref Matrix transform, Vec3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1347,11 +1347,11 @@ namespace math public static void TransformNormal(Vec3[] source, ref Matrix transform, Vec3[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/math/Vector4.cs b/math/Vector4.cs index 48e1da0..2a77438 100644 --- a/math/Vector4.cs +++ b/math/Vector4.cs @@ -892,11 +892,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -939,11 +939,11 @@ namespace math //q5 = ... if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { @@ -1012,11 +1012,11 @@ namespace math public static void Transform(Vec4[] source, ref Quaternion rotation, Vec4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); float x = rotation.X + rotation.X; float y = rotation.Y + rotation.Y; @@ -1091,11 +1091,11 @@ namespace math public static void Transform(Vec4[] source, ref Matrix transform, Vec4[] destination) { if (source == null) - throw new ArgumentNullException("source"); + throw new ArgumentNullException("_source"); if (destination == null) throw new ArgumentNullException("destination"); if (destination.Length < source.Length) - throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array."); + throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the _source array."); for (int i = 0; i < source.Length; ++i) { diff --git a/scr/Script.cs b/scr/Script.cs new file mode 100644 index 0000000..ac89f62 --- /dev/null +++ b/scr/Script.cs @@ -0,0 +1,305 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Emit; +using Microsoft.CodeAnalysis.Text; + +public class MemoryRefResolver : SourceReferenceResolver +{ + public override bool Equals(object other) + { + return false; + } + + public override int GetHashCode() + { + return 0; + } + + public override string NormalizePath(string path, string baseFilePath) + { + return path; + } + + public override Stream OpenRead(string resolvedPath) + { + return null; + } + + public override SourceText ReadText(string resolvedPath) + { + return null; + } + + public override string ResolveReference(string path, string baseFilePath) + { + return null; + } + +} + + + + +public static class scr +{ + + + public static bool firstErrorSourceLogged = false; + + private static class ReferencesCache + { + // create the list of references on first use, but if two threads both *start* making the list thats fine since we'll just use whichever wins. + private static readonly Lazy> lazyReferences = new Lazy>(GetReferences, LazyThreadSafetyMode.PublicationOnly); + + public static IReadOnlyList References => lazyReferences.Value; + + private static ImmutableArray GetReferences() + { + var builder = ImmutableArray.CreateBuilder(); + + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + foreach (var ass in assemblies) + { + if (ass != null && !ass.IsDynamic && ass.Location != null) + { + try + { + builder.Add(MetadataReference.CreateFromFile(ass.Location)); + } + catch (Exception) + { + } + } + } + + return builder.ToImmutable(); + } + } + + public static string CleanString(string dirty) + { + // TODO @@ Get a clean string implementation in + return dirty; //return Newtonsoft.Json.JsonConvert.ToString(dirty); + } + + public static string PrettyName(Type type) + { + if (type.GetGenericArguments().Length == 0) + { + return type.FullName.Replace('+', '.'); + } + var genericArguments = type.GetGenericArguments(); + var typeDef = type.FullName; + + var indexOfTick = typeDef.IndexOf("`"); + + var unmangledOuterName = typeDef.Substring(0, typeDef.IndexOf('`')).Replace('+', '.'); + + var innerName = ""; + + //Check for inner class + if (typeDef.ElementAt(indexOfTick + 2) != '[') + { + var indexOfOpenBracket = typeDef.IndexOf('[', indexOfTick); + + innerName = typeDef.Substring(indexOfTick + 2, indexOfOpenBracket - (indexOfTick + 2)).Replace('+', '.'); + } + + return unmangledOuterName + "<" + String.Join(",", genericArguments.Select(PrettyName)) + ">" + innerName; + } + + public static FieldInfo GetFieldInfo(Type t, string name) + { + if (t == null) + return null; + + var fi = t.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); + + if (fi != null) + return fi; + + if (t.BaseType != null) + return GetFieldInfo(t.BaseType, name); + + return null; + } + + // From stack overflow + static Lazy> typeSetLazy = + new Lazy>(() => { + var types = AppDomain + .CurrentDomain + .GetAssemblies() + .SelectMany(a => a.GetTypes() + .Where(t => t.IsClass)); + var typesAndBaseTypes = types + .Select(t => new { Type = t, t.BaseType }) + .ToList(); + var typesWithSubclasses = typesAndBaseTypes + .Join( + typesAndBaseTypes, + t => t.Type, + t => t.BaseType, + (t1, t2) => t2.BaseType); + var typesHs = new HashSet(types); + typesHs.ExceptWith(typesWithSubclasses); + return typesHs; + }); + + static bool IsLeafType(this Type type) + { + return typeSetLazy.Value.Contains(type); + } + + static public string TypeToIdentifier(string typename) + { + return typename.Replace('<', '_').Replace('>', '_').Replace(',', '_').Replace(' ', '_').Replace('.', '_').Replace('+', '_').Replace('[', '_').Replace(']', '_').Replace('$', '_').Replace(':', '_'); + } + + public static void LogDiagnosticErrorsAndWarnings(ImmutableArray diagnostics, string sourceFile) + { + foreach (Diagnostic diagnostic in diagnostics) + { + //if (diagnostic.Severity != DiagnosticSeverity.Hidden) + { + switch (diagnostic.Severity) + { + case DiagnosticSeverity.Error: + { + var msg = $"{sourceFile}({diagnostic.Location.GetLineSpan().StartLinePosition.Line + 1}): {diagnostic.GetMessage()}"; + System.Diagnostics.Debug.WriteLine(msg); + + } + break; + + case DiagnosticSeverity.Warning: + case DiagnosticSeverity.Info: + { + var msg = $"{sourceFile}({diagnostic.Location.GetLineSpan().StartLinePosition.Line + 1}): {diagnostic.GetMessage()}"; + System.Diagnostics.Debug.WriteLine(msg); + } + break; + } + } + } + } + + public static void Compile(string dynamicScript, string uniquePath, Action onSuccess, Action> onFailure, Platform platform = Platform.X86) + { + string assemblyName = Path.GetRandomFileName(); + + var parseOptions = new CSharpParseOptions(documentationMode: DocumentationMode.Diagnose, kind: SourceCodeKind.Regular); + + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(dynamicScript, parseOptions, uniquePath, encoding: System.Text.Encoding.UTF8); + + var memRef = new MemoryRefResolver(); + + /* + CSharpCompilation compilation = CSharpCompilation.Create( + assemblyName, + syntaxTrees: new[] { syntaxTree }, + references: ReferencesCache.References, + options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, + sourceReferenceResolver: memRef, + optimizationLevel: OptimizationLevel.Debug, + platform: platform)); + */ + + using (var ms = new MemoryStream()) + using (var pdb = new MemoryStream()) + { + var result = CompileAndEmit(assemblyName, new[] { syntaxTree }, ms, pdb, platform); + + if (!result.Success) + { + if (onFailure == null) + { + LogDiagnosticErrorsAndWarnings(result.Diagnostics, "unknown"); + } + else + { + onFailure(result.Diagnostics); + } + } + else + { + /* + foreach (Diagnostic diagnostic in result.Diagnostics) + { + //if (diagnostic.Severity != DiagnosticSeverity.Hidden) + { + if (diagnostic.Id == "CS1591") continue; + + switch (diagnostic.Severity) + { + case DiagnosticSeverity.Error: + { + var msg = $"{source.file}({diagnostic.Location.GetLineSpan().StartLinePosition.Line + 1}): {diagnostic.GetMessage()}"; + log.Error(msg); + System.Diagnostics.Debug.WriteLine(msg); + + } + break; + + case DiagnosticSeverity.Warning: + case DiagnosticSeverity.Info: + { + var msg = $"{source.file}({diagnostic.Location.GetLineSpan().StartLinePosition.Line + 1}): {diagnostic.GetMessage()}"; + log.Warn(msg); + System.Diagnostics.Debug.WriteLine(msg); + } + break; + } + } + } + */ + + ms.Seek(0, SeekOrigin.Begin); + var assembly = Assembly.Load(ms.ToArray(), pdb.ToArray()); + + onSuccess(assembly); + + + } + } + } + + private static EmitResult CompileAndEmit(string assemblyName, SyntaxTree[] syntaxTrees, MemoryStream ms, MemoryStream pdb, Platform platform) + { + var memRef = new MemoryRefResolver(); + + CSharpCompilation compilation = CSharpCompilation.Create( + assemblyName, + syntaxTrees: syntaxTrees, + references: ReferencesCache.References, + options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, + sourceReferenceResolver: memRef, + optimizationLevel: OptimizationLevel.Debug, + platform: platform, + specificDiagnosticOptions: new Dictionary + { + { "CS1701", ReportDiagnostic.Suppress } + })); + + return compilation.Emit(ms, pdb); + } + + private static void AddIfFirst(IDictionary dict, TKey key, TValue value) + { + if (!dict.ContainsKey(key)) + { + dict.Add(key, value); + } + } +} + diff --git a/ser/XmlFormatter2.cs b/ser/XmlFormatter2.cs index 3e4774d..90bb393 100644 --- a/ser/XmlFormatter2.cs +++ b/ser/XmlFormatter2.cs @@ -91,6 +91,14 @@ namespace lib //lib.log.info( "Deserialize END ( Stream stream ) {0} {1}", m_rndVal, m_alreadySerialized.Count ); } + + public T Deserialize(Stream stream) + { + //lib.log.info( "Deserialize( Stream stream ) {0} {1}", m_rndVal, m_alreadySerialized.Count ); + return (T)DeserializeKnownType(stream, typeof(T)); + //lib.log.info( "Deserialize END ( Stream stream ) {0} {1}", m_rndVal, m_alreadySerialized.Count ); + } + public object DeserializeKnownType( Stream stream, Type t ) { //lib.log.info( "DeserializeKnownType( Stream stream, Type t ) {0} {1}", m_rndVal, m_alreadySerialized.Count );