Fixes and verbose logging

This commit is contained in:
Marc Hernandez 2024-05-05 20:18:46 -07:00
parent 380974d673
commit 08cf4d3aca
2 changed files with 305 additions and 218 deletions

View File

@ -7,41 +7,38 @@ using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Threading; using System.Threading;
using System.IO;
using Microsoft.CodeAnalysis;
namespace res namespace res;
using ImmDefLoad = ImmutableQueue<(string name, Ref)>;
public interface Res_old
{ {
}
using ImmDefLoad = ImmutableQueue<(string name, Ref)>; [DebuggerDisplay("Path = {path}")]
public class Ref : lib.I_Serialize
{
static public bool s_verboseLogging = true;
public interface Res_old
{
}
[Serializable]
public class Ref : lib.I_Serialize
{
public string Filename =>path; public string Filename =>path;
//For construction //For construction
public Ref() public Ref()
{ {
path = "{UNSET_CONS}"; path = "{set_from_ref_default_cons}";
if( s_verboseLogging ) log.info( $"Ref: {GetType().Name} {path}" );
} }
public Ref( string filename ) public Ref( string filename )
{ {
path = filename; path = filename;
} if( s_verboseLogging ) log.info( $"Ref: {GetType().Name} {path}" );
virtual public void OnSerialize()
{
}
virtual public void OnDeserialize( object enclosing )
{
} }
virtual public void OnChange() virtual public void OnChange()
@ -53,80 +50,114 @@ namespace res
} }
private string path = "{UNSET_INLINE}"; private string path = "{set_from_inline_cons}";
} }
[Serializable] [Serializable]
public class Ref<T> : Ref where T : class [DebuggerDisplay("Path = {path} / Res = {res}")]
public class Ref<T> : Ref where T : class
{
public T? res => m_res != null ? m_res : lookup();
public T? lookup()
{ {
public T? res => m_res != null ? m_res : ( m_res = Mgr.load<T>( Filename ) ); m_res = Mgr.load<T>( Filename );
if( s_verboseLogging ) log.info( $"Ref.lookup {GetType().Name} {GetType().GenericTypeArguments[0]} path {Filename}" );
return m_res;
}
//For serialization //For serialization
public Ref() public Ref()
: :
base( "{unknown}" ) base( "{set_from_ref<>_default_cons}" )
{ {
if( s_verboseLogging ) log.info( $"Ref {GetType().Name} {GetType().GenericTypeArguments[0]} path {Filename}" );
} }
public Ref( string filename ) public Ref( string filename )
: :
base( filename ) base( filename )
{ {
if( s_verboseLogging ) log.info( $"Ref {GetType().Name} {GetType().GenericTypeArguments[0]} path {Filename}" );
} }
/*
public Ref( string filename, T res ) : base( filename )
{
m_res = res;
}
*/
override internal void load() override internal void load()
{ {
m_res = Mgr.load<T>( Filename ); m_res = Mgr.load<T>( Filename );
if( s_verboseLogging ) log.info( $"Ref.load {GetType().Name} {GetType().GenericTypeArguments[0]} path {Filename}" );
}
public object OnDeserialize( object enclosing )
{
return enclosing;
}
static public Ref<T> createAsset( T v, string path )
{
if( File.Exists( path ) )
{
log.warn( $"For {typeof(T).Name}, saving asset to {path}, but it already exists" );
var newPath = $"{path}_{DateTime.Now.ToShortDateString()}_{DateTime.Now.ToShortTimeString()}";
System.IO.File.Move(path, newPath );
log.warn( $"For {typeof(T).Name}, renamed to {newPath}" );
}
var newRef = new Ref<T>( path );
return newRef;
} }
[NonSerialized] [NonSerialized]
private T m_res; protected T m_res;
}
public class RefMemory<T> : Ref<T> where T : class
{
//For serialization
public RefMemory( T res )
:
base( "{memory}" )
{
m_res = res;
} }
override internal void load()
public class Resource
{ {
}
}
public class Resource
{
static public Mgr mgr; static public Mgr mgr;
} }
/* public delegate T Load<out T>( string filename );
public class Loader<T>
{
static public T load( string filename )
{
Debug.Assert( false, "Specialize Loader for your type for file" );
return default(T);
}
}
*/
public delegate T Load<out T>( string filename );
class LoadHolder class LoadHolder
{ {
internal virtual object load() internal virtual object load()
{ {
return null; return null;
} }
} }
class LoadHolder<T> : LoadHolder class LoadHolder<T> : LoadHolder
{ {
public LoadHolder( Load<T> fnLoad ) public LoadHolder( Load<T> fnLoad )
{ {
_fnLoad = fnLoad; _fnLoad = fnLoad;
@ -138,18 +169,24 @@ namespace res
{ {
return load(); return load();
} }
} }
//generic classes make a new static per generic type public record class ResourceHolder<T>( WeakReference<T> weak, string Name, DateTime captured ) : imm.Recorded<ResourceHolder<T>>
class ResCache<T> where T : class where T : class
{ {
}
//generic classes make a new static per generic type
class ResCache<T> where T : class
{
public static T s_default = default; public static T s_default = default;
public static ImmutableDictionary<string, WeakReference<T>> s_cache = ImmutableDictionary<string, WeakReference<T>>.Empty; public static ImmutableDictionary<string, ResourceHolder<T>> s_cache = ImmutableDictionary<string, ResourceHolder<T>>.Empty;
} }
public class Mgr public class Mgr
{ {
static public void startup() static public void startup()
{ {
Resource.mgr = new Mgr(); Resource.mgr = new Mgr();
@ -225,12 +262,16 @@ namespace res
} }
//*/ //*/
// @@@ TODO Pass information through here
static public T? load<T>( string filename ) where T : class static public T? load<T>( string filename ) where T : class
{ {
if( ResCache<T>.s_cache.TryGetValue( filename, out var wr ) ) if( ResCache<T>.s_cache.TryGetValue( filename, out var holder ) )
{
if( holder.weak.TryGetTarget( out var v ) )
{ {
if( wr.TryGetTarget( out var v ) )
return v; return v;
}
log.info( $"{filename} was in cache, but its been dropped, reloading." ); log.info( $"{filename} was in cache, but its been dropped, reloading." );
} }
@ -244,22 +285,31 @@ namespace res
static public T actualLoad<T>( string filename ) where T : class static public T actualLoad<T>( string filename ) where T : class
{ {
lock(s_loading)
{
if( s_loading.TryGetValue( filename, out var evt ) ) if( s_loading.TryGetValue( filename, out var evt ) )
{ {
evt.WaitOne();
if( ResCache<T>.s_cache.TryGetValue( filename, out var wr ) ) //var waiting = evt.WaitOne();
if( ResCache<T>.s_cache.TryGetValue( filename, out var holder ) )
{ {
if( wr.TryGetTarget( out var v ) ) if( holder.weak.TryGetTarget( out var v ) )
{
log.trace( $"{typeof(T).Name} loading {filename}" );
return v; return v;
}
log.error( $"{filename} was in cache, but its been dropped, reloading." ); log.error( $"{filename} was in cache, but its been dropped, reloading." );
} }
} }
var evtNew = new AutoResetEvent( false ); //var evtNew = new AutoResetEvent( false );
if( ImmutableInterlocked.TryAdd( ref s_loading, filename, evtNew ) ) //if( ImmutableInterlocked.TryAdd( ref s_loading, filename, evtNew ) )
{ {
if( Resource.mgr.m_loaders.TryGetValue( typeof( T ), out var loaderGen ) ) if( Resource.mgr.m_loaders.TryGetValue( typeof( T ), out var loaderGen ) )
{ {
@ -269,14 +319,18 @@ namespace res
var weak = new WeakReference<T>( v ); var weak = new WeakReference<T>( v );
var alreadyAdded = !ImmutableInterlocked.TryAdd( ref ResCache<T>.s_cache, filename, weak ); var holder = new ResourceHolder<T>( weak, $"", DateTime.Now ).Record();
evtNew.Set(); log.info( $"To {typeof(T).Name} add {filename}" );
var alreadyAdded = !ImmutableInterlocked.TryAdd( ref ResCache<T>.s_cache, filename, holder );
//evtNew.Set();
//Done loading //Done loading
if( !ImmutableInterlocked.TryRemove( ref s_loading, filename, out var oldEvt ) ) //if( !ImmutableInterlocked.TryRemove( ref s_loading, filename, out var oldEvt ) )
{ {
log.error( $"Error removing loading event for {filename}" ); //log.error( $"Error removing loading event for {filename}" );
} }
if( alreadyAdded ) if( alreadyAdded )
@ -294,6 +348,8 @@ namespace res
} }
} }
}
return actualLoad<T>( filename ); return actualLoad<T>( filename );
} }
@ -333,10 +389,5 @@ namespace res
Thread m_deferredLoader; Thread m_deferredLoader;
}
} }

View File

@ -38,8 +38,8 @@ namespace lib
public interface I_Serialize public interface I_Serialize
{ {
void OnSerialize(); void OnSerialize() {}
void OnDeserialize( object enclosing ); object OnDeserialize( object enclosing ) => this;
} }
[Flags] [Flags]
@ -123,6 +123,8 @@ namespace lib
public class XmlFormatter2Cfg : Config public class XmlFormatter2Cfg : Config
{ {
public bool VerboseLogging = true;
public Datastructure datastructure = Datastructure.Tree; public Datastructure datastructure = Datastructure.Tree;
public int Version = 2; public int Version = 2;
@ -286,6 +288,8 @@ namespace lib
{ {
TypeCode typeCode = Type.GetTypeCode( type ); TypeCode typeCode = Type.GetTypeCode( type );
if( _cfg.VerboseLogging ) log.info( $"{type.Name}.{name} {existing} {mi?.Name}" );
if( typeCode != TypeCode.Object ) if( typeCode != TypeCode.Object )
{ {
return DeserializeConcrete( elem, mi, name, type ); return DeserializeConcrete( elem, mi, name, type );
@ -302,11 +306,10 @@ namespace lib
{ {
object obj = DeserializeObject( elem, mi, type, existing ); object obj = DeserializeObject( elem, mi, type, existing );
if( obj is I_Serialize ) if( obj is I_Serialize iser )
{ {
var iser = obj as I_Serialize; if( _cfg.VerboseLogging ) log.info( $"" );
obj = iser.OnDeserialize( null );
iser.OnDeserialize( null );
} }
return obj; return obj;
@ -320,7 +323,7 @@ namespace lib
} }
catch( Exception ex ) catch( Exception ex )
{ {
log.warn( $"Caught exception fn {mi.Name} type {type.Name} of {ex.Message}" ); log.warn( $"Caught exception fn {mi?.Name} type {type?.Name} of {ex.Message}" );
} }
return existing; return existing;
@ -343,6 +346,8 @@ namespace lib
private object DeserializeConcrete( XmlElement elem, MemberInfo mi, string name, Type type ) private object DeserializeConcrete( XmlElement elem, MemberInfo mi, string name, Type type )
{ {
if( _cfg.VerboseLogging ) log.info( $"" );
string val = ""; string val = "";
if( elem.HasAttribute( "v" ) ) if( elem.HasAttribute( "v" ) )
@ -425,6 +430,8 @@ namespace lib
private object HydrateObject( XmlElement elem, MemberInfo mi, Type finalType, object obj ) private object HydrateObject( XmlElement elem, MemberInfo mi, Type finalType, object obj )
{ {
if( _cfg.VerboseLogging ) log.info( $"" );
if( obj is IList ) if( obj is IList )
{ {
var list = obj as IList; var list = obj as IList;
@ -513,6 +520,8 @@ namespace lib
private object HydrateObjectOfNarrowType( XmlElement elem, MemberInfo mi, Type narrowType, object obj ) private object HydrateObjectOfNarrowType( XmlElement elem, MemberInfo mi, Type narrowType, object obj )
{ {
if( _cfg.VerboseLogging ) log.info( $"" );
var isImm = typeof(imm.Imm).IsAssignableFrom( narrowType ); var isImm = typeof(imm.Imm).IsAssignableFrom( narrowType );
XmlNodeList allChildren = elem.ChildNodes; XmlNodeList allChildren = elem.ChildNodes;
@ -671,6 +680,7 @@ namespace lib
private object GetObjectForDeser( XmlElement elem, Type type, out Type finalType, object obj ) private object GetObjectForDeser( XmlElement elem, Type type, out Type finalType, object obj )
{ {
finalType = type; finalType = type;
if( elem.HasAttribute( "_.t" ) ) if( elem.HasAttribute( "_.t" ) )
{ {
@ -685,6 +695,8 @@ namespace lib
int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1; int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1;
if( _cfg.VerboseLogging ) log.info( $"{finalType?.Name}({type?.Name}) refInt {refInt} exitingObj = {obj?.ToString()}" );
obj = createObject( elem, finalType, refInt, obj ); obj = createObject( elem, finalType, refInt, obj );
return obj; return obj;
@ -692,6 +704,8 @@ namespace lib
private object DeserializeList( XmlElement elem, MemberInfo mi, Type type, IList list ) private object DeserializeList( XmlElement elem, MemberInfo mi, Type type, IList list )
{ {
if( _cfg.VerboseLogging ) log.info( $"" );
XmlNodeList arrNodeList = elem.ChildNodes; XmlNodeList arrNodeList = elem.ChildNodes;
Type t = list.GetType(); Type t = list.GetType();
@ -726,6 +740,7 @@ namespace lib
typeElem = typeof( KeyValuePair<,> ).MakeGenericType( type.GenericTypeArguments ); typeElem = typeof( KeyValuePair<,> ).MakeGenericType( type.GenericTypeArguments );
} }
if( _cfg.VerboseLogging ) log.info( $"DserCol {type.GetType().Name} {typeElem.Name} into reflT {mi.ReflectedType.Name} declT {mi.DeclaringType.Name} {mi.Name}" );
string refString = elem.GetAttribute( "ref" ); string refString = elem.GetAttribute( "ref" );
int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1; int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1;
@ -752,7 +767,9 @@ namespace lib
finalType = typeElem; finalType = typeElem;
} }
arr.SetValue( Deserialize( arrElem, mi, finalType, null ), i ); var arrItem = Deserialize( arrElem, mi, finalType, null );
arr.SetValue( arrItem, i );
} }
} }
@ -768,6 +785,8 @@ namespace lib
var typeGen = Type.MakeGenericSignatureType( type ); var typeGen = Type.MakeGenericSignatureType( type );
if( _cfg.VerboseLogging ) log.info( $"TypeGen: {typeGen.Name}" );
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 );
@ -823,6 +842,8 @@ namespace lib
private object DeserializeArray( XmlElement elem, MemberInfo mi, Type type ) private object DeserializeArray( XmlElement elem, MemberInfo mi, Type type )
{ {
if( _cfg.VerboseLogging ) log.info( $"" );
Type typeElem = type.GetElementType(); Type typeElem = type.GetElementType();
string refString = elem.GetAttribute( "ref" ); string refString = elem.GetAttribute( "ref" );
@ -866,11 +887,12 @@ namespace lib
private object createObject( XmlElement elem, Type type, int refInt, object existingObj ) private object createObject( XmlElement elem, Type type, int refInt, object existingObj )
{ {
TypeCode tc = Type.GetTypeCode( type ); TypeCode tc = Type.GetTypeCode( type );
if( _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 ); if( _cfg.VerboseLogging ) log.info( $"Reuse object" );
return m_alreadySerialized[refInt]; return m_alreadySerialized[refInt];
} }
@ -880,6 +902,7 @@ namespace lib
if( isProxy ) if( isProxy )
{ {
if( _cfg.VerboseLogging ) log.info( $"use Proxy" );
object obj = null; object obj = null;
var tryType = type; var tryType = type;
@ -921,28 +944,41 @@ namespace lib
var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type ); var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type );
if( isSubclass ) if( isSubclass )
{
if( _cfg.VerboseLogging ) log.info( $"Using existing obj {existingObj?.ToString()}" );
return existingObj; return existingObj;
}
// old // old
//if( type == existingObjType ) return existingObj; //if( type == existingObjType ) return existingObj;
} }
if( typeof(res.Ref).IsAssignableFrom( type ) )
{
log.info( $"Ref time!" );
}
// THIRD create a new object // THIRD create a new object
{ {
object obj = null; object obj = null;
try try
{ {
if( _cfg.VerboseLogging ) log.info( $"Activator.CreateInstance" );
//Trying the nice way to creat objects first. //Trying the nice way to creat objects first.
obj = Activator.CreateInstance( type ); obj = Activator.CreateInstance( type );
if( _cfg.VerboseLogging ) log.info( $"Got obj {obj?.ToString()}" );
} }
catch( Exception ) catch( Exception ex )
{ {
try try
{ {
if( _cfg.VerboseLogging ) log.info( $"GetUninitializedObject" );
obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject( type ); obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject( type );
if( _cfg.VerboseLogging ) log.info( $"Got obj {obj?.ToString()}" );
} }
catch( Exception exInner ) catch( Exception exInner )
{ {