Fixes and verbose logging
This commit is contained in:
parent
380974d673
commit
08cf4d3aca
211
res/Resource.cs
211
res/Resource.cs
@ -7,41 +7,38 @@ using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Collections.Immutable;
|
||||
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;
|
||||
|
||||
//For construction
|
||||
public Ref()
|
||||
{
|
||||
path = "{UNSET_CONS}";
|
||||
path = "{set_from_ref_default_cons}";
|
||||
if( s_verboseLogging ) log.info( $"Ref: {GetType().Name} {path}" );
|
||||
}
|
||||
|
||||
public Ref( string filename )
|
||||
{
|
||||
path = filename;
|
||||
}
|
||||
|
||||
virtual public void OnSerialize()
|
||||
{
|
||||
}
|
||||
|
||||
virtual public void OnDeserialize( object enclosing )
|
||||
{
|
||||
if( s_verboseLogging ) log.info( $"Ref: {GetType().Name} {path}" );
|
||||
}
|
||||
|
||||
virtual public void OnChange()
|
||||
@ -53,80 +50,114 @@ namespace res
|
||||
|
||||
}
|
||||
|
||||
private string path = "{UNSET_INLINE}";
|
||||
}
|
||||
private string path = "{set_from_inline_cons}";
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Ref<T> : Ref where T : class
|
||||
[Serializable]
|
||||
[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
|
||||
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 )
|
||||
:
|
||||
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()
|
||||
{
|
||||
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]
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class Resource
|
||||
override internal void load()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class Resource
|
||||
{
|
||||
static public Mgr mgr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
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 );
|
||||
public delegate T Load<out T>( string filename );
|
||||
|
||||
|
||||
class LoadHolder
|
||||
{
|
||||
class LoadHolder
|
||||
{
|
||||
internal virtual object load()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class LoadHolder<T> : LoadHolder
|
||||
{
|
||||
class LoadHolder<T> : LoadHolder
|
||||
{
|
||||
public LoadHolder( Load<T> fnLoad )
|
||||
{
|
||||
_fnLoad = fnLoad;
|
||||
@ -138,18 +169,24 @@ namespace res
|
||||
{
|
||||
return load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//generic classes make a new static per generic type
|
||||
class ResCache<T> where T : class
|
||||
{
|
||||
public record class ResourceHolder<T>( WeakReference<T> weak, string Name, DateTime captured ) : imm.Recorded<ResourceHolder<T>>
|
||||
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 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()
|
||||
{
|
||||
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
lock(s_loading)
|
||||
{
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 ) )
|
||||
{
|
||||
@ -269,14 +319,18 @@ namespace res
|
||||
|
||||
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
|
||||
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 )
|
||||
@ -294,6 +348,8 @@ namespace res
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return actualLoad<T>( filename );
|
||||
}
|
||||
|
||||
@ -333,10 +389,5 @@ namespace res
|
||||
|
||||
Thread m_deferredLoader;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -38,8 +38,8 @@ namespace lib
|
||||
|
||||
public interface I_Serialize
|
||||
{
|
||||
void OnSerialize();
|
||||
void OnDeserialize( object enclosing );
|
||||
void OnSerialize() {}
|
||||
object OnDeserialize( object enclosing ) => this;
|
||||
}
|
||||
|
||||
[Flags]
|
||||
@ -123,6 +123,8 @@ namespace lib
|
||||
|
||||
public class XmlFormatter2Cfg : Config
|
||||
{
|
||||
public bool VerboseLogging = true;
|
||||
|
||||
public Datastructure datastructure = Datastructure.Tree;
|
||||
|
||||
public int Version = 2;
|
||||
@ -286,6 +288,8 @@ namespace lib
|
||||
{
|
||||
TypeCode typeCode = Type.GetTypeCode( type );
|
||||
|
||||
if( _cfg.VerboseLogging ) log.info( $"{type.Name}.{name} {existing} {mi?.Name}" );
|
||||
|
||||
if( typeCode != TypeCode.Object )
|
||||
{
|
||||
return DeserializeConcrete( elem, mi, name, type );
|
||||
@ -302,11 +306,10 @@ namespace lib
|
||||
{
|
||||
object obj = DeserializeObject( elem, mi, type, existing );
|
||||
|
||||
if( obj is I_Serialize )
|
||||
if( obj is I_Serialize iser )
|
||||
{
|
||||
var iser = obj as I_Serialize;
|
||||
|
||||
iser.OnDeserialize( null );
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
obj = iser.OnDeserialize( null );
|
||||
}
|
||||
|
||||
return obj;
|
||||
@ -320,7 +323,7 @@ namespace lib
|
||||
}
|
||||
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;
|
||||
@ -343,6 +346,8 @@ namespace lib
|
||||
|
||||
private object DeserializeConcrete( XmlElement elem, MemberInfo mi, string name, Type type )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
|
||||
string val = "";
|
||||
|
||||
if( elem.HasAttribute( "v" ) )
|
||||
@ -425,6 +430,8 @@ namespace lib
|
||||
|
||||
private object HydrateObject( XmlElement elem, MemberInfo mi, Type finalType, object obj )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
|
||||
if( obj is IList )
|
||||
{
|
||||
var list = obj as IList;
|
||||
@ -513,6 +520,8 @@ namespace lib
|
||||
|
||||
private object HydrateObjectOfNarrowType( XmlElement elem, MemberInfo mi, Type narrowType, object obj )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
|
||||
var isImm = typeof(imm.Imm).IsAssignableFrom( narrowType );
|
||||
|
||||
XmlNodeList allChildren = elem.ChildNodes;
|
||||
@ -671,6 +680,7 @@ namespace lib
|
||||
|
||||
private object GetObjectForDeser( XmlElement elem, Type type, out Type finalType, object obj )
|
||||
{
|
||||
|
||||
finalType = type;
|
||||
if( elem.HasAttribute( "_.t" ) )
|
||||
{
|
||||
@ -685,6 +695,8 @@ namespace lib
|
||||
|
||||
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 );
|
||||
|
||||
return obj;
|
||||
@ -692,6 +704,8 @@ namespace lib
|
||||
|
||||
private object DeserializeList( XmlElement elem, MemberInfo mi, Type type, IList list )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
|
||||
XmlNodeList arrNodeList = elem.ChildNodes;
|
||||
|
||||
Type t = list.GetType();
|
||||
@ -726,6 +740,7 @@ namespace lib
|
||||
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" );
|
||||
int refInt = refString.Length > 0 ? Convert.ToInt32( refString ) : -1;
|
||||
@ -752,7 +767,9 @@ namespace lib
|
||||
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 );
|
||||
|
||||
if( _cfg.VerboseLogging ) log.info( $"TypeGen: {typeGen.Name}" );
|
||||
|
||||
if( type == typeof( ImmutableArray<> ).MakeGenericType( typeElem ) )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"" );
|
||||
|
||||
Type typeElem = type.GetElementType();
|
||||
|
||||
string refString = elem.GetAttribute( "ref" );
|
||||
@ -866,11 +887,12 @@ namespace lib
|
||||
|
||||
private object createObject( XmlElement elem, Type type, int refInt, object existingObj )
|
||||
{
|
||||
|
||||
TypeCode tc = Type.GetTypeCode( type );
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
@ -880,6 +902,7 @@ namespace lib
|
||||
|
||||
if( isProxy )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"use Proxy" );
|
||||
object obj = null;
|
||||
|
||||
var tryType = type;
|
||||
@ -921,28 +944,41 @@ namespace lib
|
||||
var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type );
|
||||
|
||||
if( isSubclass )
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"Using existing obj {existingObj?.ToString()}" );
|
||||
return existingObj;
|
||||
}
|
||||
|
||||
// old
|
||||
//if( type == existingObjType ) return existingObj;
|
||||
}
|
||||
|
||||
|
||||
if( typeof(res.Ref).IsAssignableFrom( type ) )
|
||||
{
|
||||
log.info( $"Ref time!" );
|
||||
}
|
||||
|
||||
// THIRD create a new object
|
||||
{
|
||||
object obj = null;
|
||||
|
||||
try
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"Activator.CreateInstance" );
|
||||
|
||||
//Trying the nice way to creat objects first.
|
||||
obj = Activator.CreateInstance( type );
|
||||
|
||||
if( _cfg.VerboseLogging ) log.info( $"Got obj {obj?.ToString()}" );
|
||||
}
|
||||
catch( Exception )
|
||||
catch( Exception ex )
|
||||
{
|
||||
try
|
||||
{
|
||||
if( _cfg.VerboseLogging ) log.info( $"GetUninitializedObject" );
|
||||
obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject( type );
|
||||
if( _cfg.VerboseLogging ) log.info( $"Got obj {obj?.ToString()}" );
|
||||
}
|
||||
catch( Exception exInner )
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user