Moving some files around
This commit is contained in:
parent
afd97b1522
commit
6e2e40dee3
1694
Utilities.cs
1694
Utilities.cs
File diff suppressed because it is too large
Load Diff
@ -1,172 +1,172 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
|
|
||||||
public class DescAttribute : Attribute
|
public class DescAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Desc { get; private set; }
|
public string Desc { get; private set; }
|
||||||
|
|
||||||
public DescAttribute( string desc )
|
public DescAttribute( string desc )
|
||||||
{
|
{
|
||||||
Desc = desc;
|
Desc = desc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class ConfigCfg : Config
|
public class ConfigCfg : Config
|
||||||
{
|
{
|
||||||
public readonly bool writeOutTemplateFiles = true;
|
public readonly bool writeOutTemplateFiles = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
static public Config Load( string filename )
|
static public Config Load( string filename )
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static ConfigCfg s_cfg = new ConfigCfg();
|
static ConfigCfg s_cfg = new ConfigCfg();
|
||||||
|
|
||||||
static public void startup( string filename )
|
static public void startup( string filename )
|
||||||
{
|
{
|
||||||
res.Mgr.register<Config>( load );
|
res.Mgr.register<Config>( load );
|
||||||
res.Mgr.registerSub( typeof( Config ) );
|
res.Mgr.registerSub( typeof( Config ) );
|
||||||
|
|
||||||
s_cfg = Config.load<ConfigCfg>( filename );
|
s_cfg = Config.load<ConfigCfg>( filename );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#region SaveLoad
|
#region SaveLoad
|
||||||
/*
|
/*
|
||||||
static public res.Ref<Config> res_load( string filename )
|
static public res.Ref<Config> res_load( string filename )
|
||||||
{
|
{
|
||||||
return new res.Ref<Config>( filename, load( filename ) );
|
return new res.Ref<Config>( filename, load( filename ) );
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static public T res_load<T>( string filename ) where T : Config
|
static public T res_load<T>( string filename ) where T : Config
|
||||||
{
|
{
|
||||||
return load<T>( filename );
|
return load<T>( filename );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
static public ResRefConfig res_load( string filename, Type t )
|
static public ResRefConfig res_load( string filename, Type t )
|
||||||
{
|
{
|
||||||
return new ResRefConfig( filename, load( filename, t ) );
|
return new ResRefConfig( filename, load( filename, t ) );
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
static public Config load( string filename )
|
static public Config load( string filename )
|
||||||
{
|
{
|
||||||
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
||||||
|
|
||||||
XmlFormatter2 formatter = new XmlFormatter2();
|
XmlFormatter2 formatter = new XmlFormatter2();
|
||||||
|
|
||||||
Config cfg = (Config)formatter.Deserialize( fs );
|
Config cfg = (Config)formatter.Deserialize( fs );
|
||||||
|
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public T load<T>( string filename ) where T : Config
|
static public T load<T>( string filename ) where T : Config
|
||||||
{
|
{
|
||||||
return (T)load( filename, typeof( T ) );
|
return (T)load( filename, typeof( T ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public Config load( string filename, Type t )
|
static public Config load( string filename, Type t )
|
||||||
{
|
{
|
||||||
Config cfg = null;
|
Config cfg = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
||||||
|
|
||||||
XmlFormatter2 formatter = new XmlFormatter2();
|
XmlFormatter2 formatter = new XmlFormatter2();
|
||||||
|
|
||||||
cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) );
|
cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) );
|
||||||
|
|
||||||
cfg.SetFilename( filename );
|
cfg.SetFilename( filename );
|
||||||
}
|
}
|
||||||
catch( FileNotFoundException )
|
catch( FileNotFoundException )
|
||||||
{
|
{
|
||||||
Type[] types = new Type[ 0 ];
|
Type[] types = new Type[ 0 ];
|
||||||
object[] parms = new object[ 0 ];
|
object[] parms = new object[ 0 ];
|
||||||
|
|
||||||
//types[ 0 ] = typeof( string );
|
//types[ 0 ] = typeof( string );
|
||||||
//parms[ 0 ] = filename;
|
//parms[ 0 ] = filename;
|
||||||
|
|
||||||
ConstructorInfo cons = t.GetConstructor( types );
|
ConstructorInfo cons = t.GetConstructor( types );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cfg = (Config)cons.Invoke( parms );
|
cfg = (Config)cons.Invoke( parms );
|
||||||
}
|
}
|
||||||
catch( Exception e )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
Log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
|
Log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
//cfg.SetFilename( filename );
|
//cfg.SetFilename( filename );
|
||||||
|
|
||||||
if( s_cfg.writeOutTemplateFiles )
|
if( s_cfg.writeOutTemplateFiles )
|
||||||
{
|
{
|
||||||
var templateFile = $"templates/{filename}";
|
var templateFile = $"templates/{filename}";
|
||||||
|
|
||||||
var dirName = Path.GetDirectoryName( templateFile );
|
var dirName = Path.GetDirectoryName( templateFile );
|
||||||
|
|
||||||
lib.Util.checkAndAddDirectory( dirName );
|
lib.Util.checkAndAddDirectory( dirName );
|
||||||
|
|
||||||
lib.Log.info( $"Writing out template config of type {t.Name} in {templateFile}" );
|
lib.Log.info( $"Writing out template config of type {t.Name} in {templateFile}" );
|
||||||
|
|
||||||
Config.save( cfg, templateFile );
|
Config.save( cfg, templateFile );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void save( Config cfg )
|
static public void save( Config cfg )
|
||||||
{
|
{
|
||||||
Config.save( cfg, cfg.m_filename );
|
Config.save( cfg, cfg.m_filename );
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void save( Config cfg, String filename )
|
static public void save( Config cfg, String filename )
|
||||||
{
|
{
|
||||||
FileStream fs = new FileStream( filename, FileMode.Create, FileAccess.Write );
|
FileStream fs = new FileStream( filename, FileMode.Create, FileAccess.Write );
|
||||||
|
|
||||||
XmlFormatter2 formatter = new XmlFormatter2();
|
XmlFormatter2 formatter = new XmlFormatter2();
|
||||||
|
|
||||||
formatter.Serialize( fs, cfg );
|
formatter.Serialize( fs, cfg );
|
||||||
|
|
||||||
fs.Close();
|
fs.Close();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private string m_filename = "";
|
private string m_filename = "";
|
||||||
|
|
||||||
public Config()
|
public Config()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public Config( string filename )
|
public Config( string filename )
|
||||||
{
|
{
|
||||||
m_filename = filename;
|
m_filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String Filename { get { return m_filename; } }
|
public String Filename { get { return m_filename; } }
|
||||||
|
|
||||||
protected void SetFilename( String filename ) { m_filename = filename; }
|
protected void SetFilename( String filename ) { m_filename = filename; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,15 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
// A spot for immutable helpers
|
// A spot for immutable helpers
|
||||||
|
|
||||||
public static class imm
|
public static class imm
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,52 +1,52 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public struct Pos
|
public struct Pos
|
||||||
{
|
{
|
||||||
public float x { get; private set; }
|
public float x { get; private set; }
|
||||||
public float y { get; private set; }
|
public float y { get; private set; }
|
||||||
public float z { get; private set; }
|
public float z { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
public Pos( float _x, float _y, float _z ) : this()
|
public Pos( float _x, float _y, float _z ) : this()
|
||||||
{
|
{
|
||||||
x = _x;
|
x = _x;
|
||||||
y = _y;
|
y = _y;
|
||||||
z = _z;
|
z = _z;
|
||||||
}
|
}
|
||||||
|
|
||||||
// overload operator +
|
// overload operator +
|
||||||
public static Pos operator +( Pos a, Pos b )
|
public static Pos operator +( Pos a, Pos b )
|
||||||
{
|
{
|
||||||
return new Pos( a.x + b.x, a.y + b.y, a.z + b.z );
|
return new Pos( a.x + b.x, a.y + b.y, a.z + b.z );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Pos operator -( Pos a, Pos b )
|
public static Pos operator -( Pos a, Pos b )
|
||||||
{
|
{
|
||||||
return new Pos( a.x - b.x, a.y - b.y, a.z - b.z );
|
return new Pos( a.x - b.x, a.y - b.y, a.z - b.z );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Pos operator /( Pos a, float val )
|
public static Pos operator /( Pos a, float val )
|
||||||
{
|
{
|
||||||
return new Pos( a.x / val, a.y / val, a.z / val );
|
return new Pos( a.x / val, a.y / val, a.z / val );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Pos operator *( Pos a, float val )
|
public static Pos operator *( Pos a, float val )
|
||||||
{
|
{
|
||||||
return new Pos( a.x * val, a.y * val, a.z * val );
|
return new Pos( a.x * val, a.y * val, a.z * val );
|
||||||
}
|
}
|
||||||
|
|
||||||
public float distSqr( Pos other )
|
public float distSqr( Pos other )
|
||||||
{
|
{
|
||||||
float dx = x - other.x;
|
float dx = x - other.x;
|
||||||
float dy = y - other.y;
|
float dy = y - other.y;
|
||||||
float dz = z - other.z;
|
float dz = z - other.z;
|
||||||
|
|
||||||
return dx * dx + dy * dy + dz * dz;
|
return dx * dx + dy * dy + dz * dz;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,110 +1,110 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
//using Util;
|
//using Util;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public interface IProcess
|
public interface IProcess
|
||||||
{
|
{
|
||||||
void process( object obj );
|
void process( object obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Conn
|
public class Conn
|
||||||
{
|
{
|
||||||
public Socket Sock { get { return m_socket; } }
|
public Socket Sock { get { return m_socket; } }
|
||||||
public Stream Stream { get { return m_streamNet; } }
|
public Stream Stream { get { return m_streamNet; } }
|
||||||
|
|
||||||
|
|
||||||
public Conn( Socket sock, IProcess proc )
|
public Conn( Socket sock, IProcess proc )
|
||||||
{
|
{
|
||||||
m_socket = sock;
|
m_socket = sock;
|
||||||
|
|
||||||
sock.NoDelay = true;
|
sock.NoDelay = true;
|
||||||
|
|
||||||
m_streamNet = new NetworkStream( m_socket );
|
m_streamNet = new NetworkStream( m_socket );
|
||||||
|
|
||||||
m_proc = proc;
|
m_proc = proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public object recieveObject()
|
public object recieveObject()
|
||||||
{
|
{
|
||||||
return recieveObject( Stream );
|
return recieveObject( Stream );
|
||||||
}
|
}
|
||||||
|
|
||||||
public object recieveObject( Stream stream )
|
public object recieveObject( Stream stream )
|
||||||
{
|
{
|
||||||
object obj = null;
|
object obj = null;
|
||||||
|
|
||||||
var formatter = new XmlFormatter2();
|
var formatter = new XmlFormatter2();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
obj = formatter.Deserialize( stream );
|
obj = formatter.Deserialize( stream );
|
||||||
}
|
}
|
||||||
catch( System.Xml.XmlException ex )
|
catch( System.Xml.XmlException ex )
|
||||||
{
|
{
|
||||||
lib.Log.error( $"Outer Exception {ex.Message}" );
|
lib.Log.error( $"Outer Exception {ex.Message}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send( object obj )
|
public void send( object obj )
|
||||||
{
|
{
|
||||||
|
|
||||||
var formatter = new XmlFormatter2();
|
var formatter = new XmlFormatter2();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ms = new MemoryStream( 1024 );
|
var ms = new MemoryStream( 1024 );
|
||||||
formatter.Serialize( ms, obj );
|
formatter.Serialize( ms, obj );
|
||||||
|
|
||||||
//var str = System.Text.Encoding.Default.GetString( mm_buffer, 0, (int)ms.Position );
|
//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 data {str} of length {ms.Position}" );
|
||||||
//lib.Log.info( $"Sent {obj}" );
|
//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( byteSize, 0, 4 );
|
||||||
m_streamNet.Write( ms.GetBuffer(), 0, (int)ms.Position );
|
m_streamNet.Write( ms.GetBuffer(), 0, (int)ms.Position );
|
||||||
|
|
||||||
m_streamNet.Flush();
|
m_streamNet.Flush();
|
||||||
}
|
}
|
||||||
catch( Exception e )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void recieve( object obj )
|
public virtual void recieve( object obj )
|
||||||
{
|
{
|
||||||
if( m_proc != null )
|
if( m_proc != null )
|
||||||
m_proc.process( obj );
|
m_proc.process( obj );
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket m_socket;
|
Socket m_socket;
|
||||||
|
|
||||||
NetworkStream m_streamNet;
|
NetworkStream m_streamNet;
|
||||||
|
|
||||||
IProcess m_proc;
|
IProcess m_proc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//private BufferedStream m_streamBufIn;
|
//private BufferedStream m_streamBufIn;
|
||||||
//private BufferedStream m_streamBufOut;
|
//private BufferedStream m_streamBufOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -17,8 +17,8 @@ namespace lib.Net
|
|||||||
{
|
{
|
||||||
m_username = name;
|
m_username = name;
|
||||||
m_password = pass;
|
m_password = pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly String m_username;
|
public readonly String m_username;
|
||||||
public readonly String m_password;
|
public readonly String m_password;
|
||||||
}
|
}
|
||||||
@ -29,23 +29,23 @@ namespace lib.Net
|
|||||||
public LoginResp( bool resp )
|
public LoginResp( bool resp )
|
||||||
{
|
{
|
||||||
m_resp = resp;
|
m_resp = resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly bool m_resp;
|
public readonly bool m_resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Admin Messages
|
#region Admin Messages
|
||||||
//Subclasses of this need to be on an admin client.
|
//Subclasses of this need to be on an admin client.
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Admin
|
public class Admin
|
||||||
{
|
{
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class CreateEntity : Admin
|
public class CreateEntity : Admin
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -53,17 +53,17 @@ namespace lib.Net
|
|||||||
public class MoveEntity : Admin
|
public class MoveEntity : Admin
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class EntityBase
|
public class EntityBase
|
||||||
{
|
{
|
||||||
public EntityBase( int id )
|
public EntityBase( int id )
|
||||||
{
|
{
|
||||||
m_id = id;
|
m_id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly int m_id;
|
public readonly int m_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -77,8 +77,8 @@ namespace lib.Net
|
|||||||
m_x = x;
|
m_x = x;
|
||||||
m_y = y;
|
m_y = y;
|
||||||
m_z = z;
|
m_z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly float m_x;
|
public readonly float m_x;
|
||||||
public readonly float m_y;
|
public readonly float m_y;
|
||||||
public readonly float m_z;
|
public readonly float m_z;
|
||||||
@ -90,14 +90,14 @@ namespace lib.Net
|
|||||||
public EntityDesc( int id ) :
|
public EntityDesc( int id ) :
|
||||||
base( id )
|
base( id )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
//Should an entity have a mesh? Be made up of multiple meshes?
|
//Should an entity have a mesh? Be made up of multiple meshes?
|
||||||
public readonly String m_mesh;
|
public readonly String m_mesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,220 +1,220 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static public class scr
|
static public class scr
|
||||||
{
|
{
|
||||||
|
|
||||||
public class PredEnumerator
|
public class PredEnumerator
|
||||||
{
|
{
|
||||||
public static PredEnumerator<T> Create<T>( IEnumerator<T> en, Predicate<T> pred )
|
public static PredEnumerator<T> Create<T>( IEnumerator<T> en, Predicate<T> pred )
|
||||||
{
|
{
|
||||||
return new PredEnumerator<T>( en, pred );
|
return new PredEnumerator<T>( en, pred );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PredEnumerator<T> Create<T>( IEnumerable<T> en, Predicate<T> pred )
|
public static PredEnumerator<T> Create<T>( IEnumerable<T> en, Predicate<T> pred )
|
||||||
{
|
{
|
||||||
return new PredEnumerator<T>( en.GetEnumerator(), pred );
|
return new PredEnumerator<T>( en.GetEnumerator(), pred );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PredEnumerator<T> : PredEnumerator, IEnumerator<T>
|
public class PredEnumerator<T> : PredEnumerator, IEnumerator<T>
|
||||||
{
|
{
|
||||||
|
|
||||||
public T Current => m_en.Current;
|
public T Current => m_en.Current;
|
||||||
|
|
||||||
object IEnumerator.Current => m_en.Current;
|
object IEnumerator.Current => m_en.Current;
|
||||||
|
|
||||||
public PredEnumerator( IEnumerator<T> en, Predicate<T> pred )
|
public PredEnumerator( IEnumerator<T> en, Predicate<T> pred )
|
||||||
{
|
{
|
||||||
m_en = en;
|
m_en = en;
|
||||||
m_pred = pred;
|
m_pred = pred;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
var success = m_en.MoveNext();
|
var success = m_en.MoveNext();
|
||||||
|
|
||||||
if( !success )
|
if( !success )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
while( !m_pred( m_en.Current ) && ( success = m_en.MoveNext() ) )
|
while( !m_pred( m_en.Current ) && ( success = m_en.MoveNext() ) )
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
m_en.Reset();
|
m_en.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IDisposable Support
|
#region IDisposable Support
|
||||||
private bool disposedValue = false; // To detect redundant calls
|
private bool disposedValue = false; // To detect redundant calls
|
||||||
|
|
||||||
protected virtual void Dispose( bool disposing )
|
protected virtual void Dispose( bool disposing )
|
||||||
{
|
{
|
||||||
if( !disposedValue )
|
if( !disposedValue )
|
||||||
{
|
{
|
||||||
if( disposing )
|
if( disposing )
|
||||||
{
|
{
|
||||||
// TODO: dispose managed state (managed objects).
|
// TODO: dispose managed state (managed objects).
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
|
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
|
||||||
// TODO: set large fields to null.
|
// TODO: set large fields to null.
|
||||||
|
|
||||||
disposedValue = true;
|
disposedValue = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
|
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
|
||||||
// ~MyEnumerator()
|
// ~MyEnumerator()
|
||||||
// {
|
// {
|
||||||
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
||||||
// Dispose(false);
|
// Dispose(false);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// This code added to correctly implement the disposable pattern.
|
// This code added to correctly implement the disposable pattern.
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
||||||
Dispose( true );
|
Dispose( true );
|
||||||
// TODO: uncomment the following line if the finalizer is overridden above.
|
// TODO: uncomment the following line if the finalizer is overridden above.
|
||||||
// GC.SuppressFinalize(this);
|
// GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
IEnumerator<T> m_en;
|
IEnumerator<T> m_en;
|
||||||
Predicate<T> m_pred;
|
Predicate<T> m_pred;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class PredEnumerable<T> : IEnumerable<T>
|
public class PredEnumerable<T> : IEnumerable<T>
|
||||||
{
|
{
|
||||||
public PredEnumerable( PredEnumerator<T> en )
|
public PredEnumerable( PredEnumerator<T> en )
|
||||||
{
|
{
|
||||||
m_en = en;
|
m_en = en;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator()
|
public IEnumerator<T> GetEnumerator()
|
||||||
{
|
{
|
||||||
return m_en;
|
return m_en;
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
{
|
{
|
||||||
return m_en;
|
return m_en;
|
||||||
}
|
}
|
||||||
|
|
||||||
PredEnumerator<T> m_en;
|
PredEnumerator<T> m_en;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void GetAllFields( Type t, List<FieldInfo> list )
|
public static void GetAllFields( Type t, List<FieldInfo> list )
|
||||||
{
|
{
|
||||||
var fieldArr = t.GetFields(
|
var fieldArr = t.GetFields(
|
||||||
BindingFlags.DeclaredOnly |
|
BindingFlags.DeclaredOnly |
|
||||||
BindingFlags.NonPublic |
|
BindingFlags.NonPublic |
|
||||||
BindingFlags.Public |
|
BindingFlags.Public |
|
||||||
BindingFlags.Instance);
|
BindingFlags.Instance);
|
||||||
|
|
||||||
var en = PredEnumerator.Create<FieldInfo>( fieldArr.AsEnumerable<FieldInfo>(), fa => fa.GetCustomAttribute( typeof( NonSerializedAttribute ) ) == null );
|
var en = PredEnumerator.Create<FieldInfo>( fieldArr.AsEnumerable<FieldInfo>(), fa => fa.GetCustomAttribute( typeof( NonSerializedAttribute ) ) == null );
|
||||||
|
|
||||||
list.AddRange( new PredEnumerable<FieldInfo>( en ) );
|
list.AddRange( new PredEnumerable<FieldInfo>( en ) );
|
||||||
|
|
||||||
if( t.BaseType != null && t.BaseType != typeof( object ) )
|
if( t.BaseType != null && t.BaseType != typeof( object ) )
|
||||||
{
|
{
|
||||||
GetAllFields( t.BaseType, list );
|
GetAllFields( t.BaseType, list );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static ImmutableList<FieldInfo> GetAllFields( Type t )
|
public static ImmutableList<FieldInfo> GetAllFields( Type t )
|
||||||
{
|
{
|
||||||
if( s_fieldCache.TryGetValue( t, out var info ) )
|
if( s_fieldCache.TryGetValue( t, out var info ) )
|
||||||
return info;
|
return info;
|
||||||
|
|
||||||
var list = new List<FieldInfo>();
|
var list = new List<FieldInfo>();
|
||||||
|
|
||||||
GetAllFields( t, list );
|
GetAllFields( t, list );
|
||||||
|
|
||||||
var immList = list.ToImmutableList();
|
var immList = list.ToImmutableList();
|
||||||
|
|
||||||
s_fieldCache = s_fieldCache.Add( t, immList );
|
s_fieldCache = s_fieldCache.Add( t, immList );
|
||||||
|
|
||||||
return immList;
|
return immList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void GetAllProperties( Type t, List<PropertyInfo> list )
|
public static void GetAllProperties( Type t, List<PropertyInfo> list )
|
||||||
{
|
{
|
||||||
var propArr = t.GetProperties(
|
var propArr = t.GetProperties(
|
||||||
BindingFlags.DeclaredOnly |
|
BindingFlags.DeclaredOnly |
|
||||||
BindingFlags.NonPublic |
|
BindingFlags.NonPublic |
|
||||||
BindingFlags.Public |
|
BindingFlags.Public |
|
||||||
BindingFlags.Instance
|
BindingFlags.Instance
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var en = PredEnumerator.Create<PropertyInfo>( propArr.AsEnumerable<PropertyInfo>(),
|
var en = PredEnumerator.Create<PropertyInfo>( propArr.AsEnumerable<PropertyInfo>(),
|
||||||
fa => fa.GetCustomAttribute( typeof( NonSerializedAttribute ) ) == null && !list.Exists( f => f.Name == fa.Name ) );
|
fa => fa.GetCustomAttribute( typeof( NonSerializedAttribute ) ) == null && !list.Exists( f => f.Name == fa.Name ) );
|
||||||
|
|
||||||
list.AddRange( new PredEnumerable<PropertyInfo>( en ) );
|
list.AddRange( new PredEnumerable<PropertyInfo>( en ) );
|
||||||
|
|
||||||
if( t.BaseType != null && t.BaseType != typeof( object ) )
|
if( t.BaseType != null && t.BaseType != typeof( object ) )
|
||||||
{
|
{
|
||||||
GetAllProperties( t.BaseType, list );
|
GetAllProperties( t.BaseType, list );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ImmutableList<PropertyInfo> GetAllProperties( Type t )
|
public static ImmutableList<PropertyInfo> GetAllProperties( Type t )
|
||||||
{
|
{
|
||||||
if( s_propCache.TryGetValue( t, out var info ) )
|
if( s_propCache.TryGetValue( t, out var info ) )
|
||||||
return info;
|
return info;
|
||||||
|
|
||||||
var list = new List<PropertyInfo>();
|
var list = new List<PropertyInfo>();
|
||||||
|
|
||||||
GetAllProperties( t, list );
|
GetAllProperties( t, list );
|
||||||
|
|
||||||
var immList = list.ToImmutableList();
|
var immList = list.ToImmutableList();
|
||||||
|
|
||||||
s_propCache = s_propCache.Add( t, immList );
|
s_propCache = s_propCache.Add( t, immList );
|
||||||
|
|
||||||
return immList;
|
return immList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ImmutableDictionary<Type, ImmutableList<FieldInfo>> s_fieldCache = ImmutableDictionary<Type, ImmutableList<FieldInfo>>.Empty;
|
static ImmutableDictionary<Type, ImmutableList<FieldInfo>> s_fieldCache = ImmutableDictionary<Type, ImmutableList<FieldInfo>>.Empty;
|
||||||
static ImmutableDictionary<Type, ImmutableList<PropertyInfo>> s_propCache = ImmutableDictionary<Type, ImmutableList<PropertyInfo>>.Empty;
|
static ImmutableDictionary<Type, ImmutableList<PropertyInfo>> s_propCache = ImmutableDictionary<Type, ImmutableList<PropertyInfo>>.Empty;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//SLOW
|
//SLOW
|
||||||
static public string TypeToIdentifier( string typename )
|
static public string TypeToIdentifier( string typename )
|
||||||
{
|
{
|
||||||
return typename.Replace( '<', '_' ).Replace( '>', '_' ).Replace( ',', '_' ).Replace( ' ', '_' ).Replace( '.', '_' ).Replace( '+', '_' ).Replace( '[', '_' ).Replace( ']', '_' ).Replace( '$', '_' ).Replace( ':', '_' );
|
return typename.Replace( '<', '_' ).Replace( '>', '_' ).Replace( ',', '_' ).Replace( ' ', '_' ).Replace( '.', '_' ).Replace( '+', '_' ).Replace( '[', '_' ).Replace( ']', '_' ).Replace( '$', '_' ).Replace( ':', '_' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -9,157 +9,157 @@ using System.IO;
|
|||||||
using System.Security.Permissions;
|
using System.Security.Permissions;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable
|
public class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable
|
||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
private const string DictionaryNodeName = "Dictionary";
|
private const string DictionaryNodeName = "Dictionary";
|
||||||
private const string ItemNodeName = "Item";
|
private const string ItemNodeName = "Item";
|
||||||
private const string KeyNodeName = "Key";
|
private const string KeyNodeName = "Key";
|
||||||
private const string ValueNodeName = "Value";
|
private const string ValueNodeName = "Value";
|
||||||
#endregion
|
#endregion
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public SerializableDictionary()
|
public SerializableDictionary()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializableDictionary( IDictionary<TKey, TVal> dictionary )
|
public SerializableDictionary( IDictionary<TKey, TVal> dictionary )
|
||||||
: base( dictionary )
|
: base( dictionary )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializableDictionary( IEqualityComparer<TKey> comparer )
|
public SerializableDictionary( IEqualityComparer<TKey> comparer )
|
||||||
: base( comparer )
|
: base( comparer )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializableDictionary( int capacity )
|
public SerializableDictionary( int capacity )
|
||||||
: base( capacity )
|
: base( capacity )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializableDictionary( IDictionary<TKey, TVal> dictionary, IEqualityComparer<TKey> comparer )
|
public SerializableDictionary( IDictionary<TKey, TVal> dictionary, IEqualityComparer<TKey> comparer )
|
||||||
: base( dictionary, comparer )
|
: base( dictionary, comparer )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializableDictionary( int capacity, IEqualityComparer<TKey> comparer )
|
public SerializableDictionary( int capacity, IEqualityComparer<TKey> comparer )
|
||||||
: base( capacity, comparer )
|
: base( capacity, comparer )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
#region ISerializable Members
|
#region ISerializable Members
|
||||||
|
|
||||||
protected SerializableDictionary( SerializationInfo info, StreamingContext context )
|
protected SerializableDictionary( SerializationInfo info, StreamingContext context )
|
||||||
{
|
{
|
||||||
int itemCount = info.GetInt32("ItemCount");
|
int itemCount = info.GetInt32("ItemCount");
|
||||||
for( int i = 0; i < itemCount; i++ )
|
for( int i = 0; i < itemCount; i++ )
|
||||||
{
|
{
|
||||||
KeyValuePair<TKey, TVal> kvp = (KeyValuePair<TKey, TVal>)info.GetValue(String.Format( $"Item{i}" ), typeof(KeyValuePair<TKey, TVal>));
|
KeyValuePair<TKey, TVal> kvp = (KeyValuePair<TKey, TVal>)info.GetValue(String.Format( $"Item{i}" ), typeof(KeyValuePair<TKey, TVal>));
|
||||||
this.Add( kvp.Key, kvp.Value );
|
this.Add( kvp.Key, kvp.Value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )]
|
[SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )]
|
||||||
void ISerializable.GetObjectData( SerializationInfo info, StreamingContext context )
|
void ISerializable.GetObjectData( SerializationInfo info, StreamingContext context )
|
||||||
{
|
{
|
||||||
info.AddValue( "ItemCount", this.Count );
|
info.AddValue( "ItemCount", this.Count );
|
||||||
int itemIdx = 0;
|
int itemIdx = 0;
|
||||||
foreach( KeyValuePair<TKey, TVal> kvp in this )
|
foreach( KeyValuePair<TKey, TVal> kvp in this )
|
||||||
{
|
{
|
||||||
info.AddValue( String.Format( $"Item{itemIdx}" ), kvp, typeof( KeyValuePair<TKey, TVal> ) );
|
info.AddValue( String.Format( $"Item{itemIdx}" ), kvp, typeof( KeyValuePair<TKey, TVal> ) );
|
||||||
itemIdx++;
|
itemIdx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
#region IXmlSerializable Members
|
#region IXmlSerializable Members
|
||||||
|
|
||||||
void IXmlSerializable.WriteXml( System.Xml.XmlWriter writer )
|
void IXmlSerializable.WriteXml( System.Xml.XmlWriter writer )
|
||||||
{
|
{
|
||||||
//writer.WriteStartElement(DictionaryNodeName);
|
//writer.WriteStartElement(DictionaryNodeName);
|
||||||
foreach( KeyValuePair<TKey, TVal> kvp in this )
|
foreach( KeyValuePair<TKey, TVal> kvp in this )
|
||||||
{
|
{
|
||||||
writer.WriteStartElement( ItemNodeName );
|
writer.WriteStartElement( ItemNodeName );
|
||||||
writer.WriteStartElement( KeyNodeName );
|
writer.WriteStartElement( KeyNodeName );
|
||||||
KeySerializer.Serialize( writer, kvp.Key );
|
KeySerializer.Serialize( writer, kvp.Key );
|
||||||
writer.WriteEndElement();
|
writer.WriteEndElement();
|
||||||
writer.WriteStartElement( ValueNodeName );
|
writer.WriteStartElement( ValueNodeName );
|
||||||
ValueSerializer.Serialize( writer, kvp.Value );
|
ValueSerializer.Serialize( writer, kvp.Value );
|
||||||
writer.WriteEndElement();
|
writer.WriteEndElement();
|
||||||
writer.WriteEndElement();
|
writer.WriteEndElement();
|
||||||
}
|
}
|
||||||
//writer.WriteEndElement();
|
//writer.WriteEndElement();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IXmlSerializable.ReadXml( System.Xml.XmlReader reader )
|
void IXmlSerializable.ReadXml( System.Xml.XmlReader reader )
|
||||||
{
|
{
|
||||||
if( reader.IsEmptyElement )
|
if( reader.IsEmptyElement )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move past container
|
// Move past container
|
||||||
if( !reader.Read() )
|
if( !reader.Read() )
|
||||||
{
|
{
|
||||||
throw new XmlException( "Error in Deserialization of Dictionary" );
|
throw new XmlException( "Error in Deserialization of Dictionary" );
|
||||||
}
|
}
|
||||||
|
|
||||||
//reader.ReadStartElement(DictionaryNodeName);
|
//reader.ReadStartElement(DictionaryNodeName);
|
||||||
while( reader.NodeType != XmlNodeType.EndElement )
|
while( reader.NodeType != XmlNodeType.EndElement )
|
||||||
{
|
{
|
||||||
reader.ReadStartElement( ItemNodeName );
|
reader.ReadStartElement( ItemNodeName );
|
||||||
reader.ReadStartElement( KeyNodeName );
|
reader.ReadStartElement( KeyNodeName );
|
||||||
TKey key = (TKey)KeySerializer.Deserialize(reader);
|
TKey key = (TKey)KeySerializer.Deserialize(reader);
|
||||||
reader.ReadEndElement();
|
reader.ReadEndElement();
|
||||||
reader.ReadStartElement( ValueNodeName );
|
reader.ReadStartElement( ValueNodeName );
|
||||||
TVal value = (TVal)ValueSerializer.Deserialize(reader);
|
TVal value = (TVal)ValueSerializer.Deserialize(reader);
|
||||||
reader.ReadEndElement();
|
reader.ReadEndElement();
|
||||||
reader.ReadEndElement();
|
reader.ReadEndElement();
|
||||||
this.Add( key, value );
|
this.Add( key, value );
|
||||||
reader.MoveToContent();
|
reader.MoveToContent();
|
||||||
}
|
}
|
||||||
//reader.ReadEndElement();
|
//reader.ReadEndElement();
|
||||||
|
|
||||||
reader.ReadEndElement(); // Read End Element to close Read of containing node
|
reader.ReadEndElement(); // Read End Element to close Read of containing node
|
||||||
}
|
}
|
||||||
|
|
||||||
System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
|
System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
#region Private Properties
|
#region Private Properties
|
||||||
protected XmlSerializer ValueSerializer
|
protected XmlSerializer ValueSerializer
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if( valueSerializer == null )
|
if( valueSerializer == null )
|
||||||
{
|
{
|
||||||
valueSerializer = new XmlSerializer( typeof( TVal ) );
|
valueSerializer = new XmlSerializer( typeof( TVal ) );
|
||||||
}
|
}
|
||||||
return valueSerializer;
|
return valueSerializer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private XmlSerializer KeySerializer
|
private XmlSerializer KeySerializer
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if( keySerializer == null )
|
if( keySerializer == null )
|
||||||
{
|
{
|
||||||
keySerializer = new XmlSerializer( typeof( TKey ) );
|
keySerializer = new XmlSerializer( typeof( TKey ) );
|
||||||
}
|
}
|
||||||
return keySerializer;
|
return keySerializer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
#region Private Members
|
#region Private Members
|
||||||
private XmlSerializer keySerializer = null;
|
private XmlSerializer keySerializer = null;
|
||||||
private XmlSerializer valueSerializer = null;
|
private XmlSerializer valueSerializer = null;
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,52 +1,52 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
public class Clock
|
public class Clock
|
||||||
{
|
{
|
||||||
public Clock( long timeOffset )
|
public Clock( long timeOffset )
|
||||||
{
|
{
|
||||||
m_timer = new Timer();
|
m_timer = new Timer();
|
||||||
|
|
||||||
m_lastTime = m_timer.Current;
|
m_lastTime = m_timer.Current;
|
||||||
|
|
||||||
m_totalMillis = timeOffset;
|
m_totalMillis = timeOffset;
|
||||||
m_totalSeconds = (double)m_totalMillis / 1000.0;
|
m_totalSeconds = (double)m_totalMillis / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tick()
|
public void tick()
|
||||||
{
|
{
|
||||||
long current = m_timer.Current;
|
long current = m_timer.Current;
|
||||||
|
|
||||||
m_dtMillis = (int)( current - m_lastTime );
|
m_dtMillis = (int)( current - m_lastTime );
|
||||||
|
|
||||||
m_dtSeconds = (double)m_dtMillis / 1000.0;
|
m_dtSeconds = (double)m_dtMillis / 1000.0;
|
||||||
|
|
||||||
m_totalMillis += m_dtMillis;
|
m_totalMillis += m_dtMillis;
|
||||||
m_totalSeconds = (double)m_totalMillis / 1000.0;
|
m_totalSeconds = (double)m_totalMillis / 1000.0;
|
||||||
|
|
||||||
m_lastTime = current;
|
m_lastTime = current;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int dtMs { get { return m_dtMillis; } }
|
public int dtMs { get { return m_dtMillis; } }
|
||||||
public double dtSec { get { return m_dtSeconds; } }
|
public double dtSec { get { return m_dtSeconds; } }
|
||||||
|
|
||||||
public long ms { get { return m_totalMillis; } }
|
public long ms { get { return m_totalMillis; } }
|
||||||
public double sec { get { return m_totalSeconds; } }
|
public double sec { get { return m_totalSeconds; } }
|
||||||
|
|
||||||
|
|
||||||
Timer m_timer;
|
Timer m_timer;
|
||||||
|
|
||||||
long m_lastTime = 0;
|
long m_lastTime = 0;
|
||||||
|
|
||||||
int m_dtMillis = 0;
|
int m_dtMillis = 0;
|
||||||
double m_dtSeconds = 0;
|
double m_dtSeconds = 0;
|
||||||
|
|
||||||
long m_totalMillis = 0;
|
long m_totalMillis = 0;
|
||||||
double m_totalSeconds = 0;
|
double m_totalSeconds = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,377 +1,377 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace lib
|
namespace lib
|
||||||
{
|
{
|
||||||
|
|
||||||
public class MicroStopwatch : System.Diagnostics.Stopwatch
|
public class MicroStopwatch : System.Diagnostics.Stopwatch
|
||||||
{
|
{
|
||||||
readonly double _microSecPerTick
|
readonly double _microSecPerTick
|
||||||
= 1000000D / System.Diagnostics.Stopwatch.Frequency;
|
= 1000000D / System.Diagnostics.Stopwatch.Frequency;
|
||||||
|
|
||||||
public MicroStopwatch()
|
public MicroStopwatch()
|
||||||
{
|
{
|
||||||
if( !System.Diagnostics.Stopwatch.IsHighResolution )
|
if( !System.Diagnostics.Stopwatch.IsHighResolution )
|
||||||
{
|
{
|
||||||
throw new Exception( "On this system the high-resolution " +
|
throw new Exception( "On this system the high-resolution " +
|
||||||
"performance counter is not available" );
|
"performance counter is not available" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long ElapsedMicroseconds
|
public long ElapsedMicroseconds
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return (long)( ElapsedTicks * _microSecPerTick );
|
return (long)( ElapsedTicks * _microSecPerTick );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MicroTimer class
|
/// MicroTimer class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MicroTimer
|
public class MicroTimer
|
||||||
{
|
{
|
||||||
public delegate void MicroTimerElapsedEventHandler(
|
public delegate void MicroTimerElapsedEventHandler(
|
||||||
object sender,
|
object sender,
|
||||||
MicroTimerEventArgs timerEventArgs );
|
MicroTimerEventArgs timerEventArgs );
|
||||||
public event MicroTimerElapsedEventHandler MicroTimerElapsed;
|
public event MicroTimerElapsedEventHandler MicroTimerElapsed;
|
||||||
|
|
||||||
System.Threading.Thread _threadTimer = null;
|
System.Threading.Thread _threadTimer = null;
|
||||||
long _ignoreEventIfLateBy = long.MaxValue;
|
long _ignoreEventIfLateBy = long.MaxValue;
|
||||||
long _timerIntervalInMicroSec = 0;
|
long _timerIntervalInMicroSec = 0;
|
||||||
bool _stopTimer = true;
|
bool _stopTimer = true;
|
||||||
|
|
||||||
public MicroTimer()
|
public MicroTimer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public MicroTimer( long timerIntervalInMicroseconds )
|
public MicroTimer( long timerIntervalInMicroseconds )
|
||||||
{
|
{
|
||||||
Interval = timerIntervalInMicroseconds;
|
Interval = timerIntervalInMicroseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Interval
|
public long Interval
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return System.Threading.Interlocked.Read(
|
return System.Threading.Interlocked.Read(
|
||||||
ref _timerIntervalInMicroSec );
|
ref _timerIntervalInMicroSec );
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
System.Threading.Interlocked.Exchange(
|
System.Threading.Interlocked.Exchange(
|
||||||
ref _timerIntervalInMicroSec, value );
|
ref _timerIntervalInMicroSec, value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long IgnoreEventIfLateBy
|
public long IgnoreEventIfLateBy
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return System.Threading.Interlocked.Read(
|
return System.Threading.Interlocked.Read(
|
||||||
ref _ignoreEventIfLateBy );
|
ref _ignoreEventIfLateBy );
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
System.Threading.Interlocked.Exchange(
|
System.Threading.Interlocked.Exchange(
|
||||||
ref _ignoreEventIfLateBy, value <= 0 ? long.MaxValue : value );
|
ref _ignoreEventIfLateBy, value <= 0 ? long.MaxValue : value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Enabled
|
public bool Enabled
|
||||||
{
|
{
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if( value )
|
if( value )
|
||||||
{
|
{
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Stop();
|
Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return ( _threadTimer != null && _threadTimer.IsAlive );
|
return ( _threadTimer != null && _threadTimer.IsAlive );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
if( Enabled || Interval <= 0 )
|
if( Enabled || Interval <= 0 )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_stopTimer = false;
|
_stopTimer = false;
|
||||||
|
|
||||||
System.Threading.ThreadStart threadStart = delegate()
|
System.Threading.ThreadStart threadStart = delegate()
|
||||||
{
|
{
|
||||||
NotificationTimer(ref _timerIntervalInMicroSec,
|
NotificationTimer(ref _timerIntervalInMicroSec,
|
||||||
ref _ignoreEventIfLateBy,
|
ref _ignoreEventIfLateBy,
|
||||||
ref _stopTimer);
|
ref _stopTimer);
|
||||||
};
|
};
|
||||||
|
|
||||||
_threadTimer = new System.Threading.Thread( threadStart );
|
_threadTimer = new System.Threading.Thread( threadStart );
|
||||||
_threadTimer.Priority = System.Threading.ThreadPriority.Highest;
|
_threadTimer.Priority = System.Threading.ThreadPriority.Highest;
|
||||||
_threadTimer.Start();
|
_threadTimer.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
_stopTimer = true;
|
_stopTimer = true;
|
||||||
|
|
||||||
if( _threadTimer != null && _threadTimer.ManagedThreadId ==
|
if( _threadTimer != null && _threadTimer.ManagedThreadId ==
|
||||||
System.Threading.Thread.CurrentThread.ManagedThreadId )
|
System.Threading.Thread.CurrentThread.ManagedThreadId )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while( Enabled )
|
while( Enabled )
|
||||||
{
|
{
|
||||||
System.Threading.Thread.SpinWait( 10 );
|
System.Threading.Thread.SpinWait( 10 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NotificationTimer( ref long timerIntervalInMicroSec,
|
void NotificationTimer( ref long timerIntervalInMicroSec,
|
||||||
ref long ignoreEventIfLateBy,
|
ref long ignoreEventIfLateBy,
|
||||||
ref bool stopTimer )
|
ref bool stopTimer )
|
||||||
{
|
{
|
||||||
int timerCount = 0;
|
int timerCount = 0;
|
||||||
long nextNotification = 0;
|
long nextNotification = 0;
|
||||||
|
|
||||||
MicroStopwatch microStopwatch = new MicroStopwatch();
|
MicroStopwatch microStopwatch = new MicroStopwatch();
|
||||||
microStopwatch.Start();
|
microStopwatch.Start();
|
||||||
|
|
||||||
while( !stopTimer )
|
while( !stopTimer )
|
||||||
{
|
{
|
||||||
long callbackFunctionExecutionTime =
|
long callbackFunctionExecutionTime =
|
||||||
microStopwatch.ElapsedMicroseconds - nextNotification;
|
microStopwatch.ElapsedMicroseconds - nextNotification;
|
||||||
|
|
||||||
long timerIntervalInMicroSecCurrent =
|
long timerIntervalInMicroSecCurrent =
|
||||||
System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
|
System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
|
||||||
long ignoreEventIfLateByCurrent =
|
long ignoreEventIfLateByCurrent =
|
||||||
System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);
|
System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);
|
||||||
|
|
||||||
nextNotification += timerIntervalInMicroSecCurrent;
|
nextNotification += timerIntervalInMicroSecCurrent;
|
||||||
timerCount++;
|
timerCount++;
|
||||||
long elapsedMicroseconds = 0;
|
long elapsedMicroseconds = 0;
|
||||||
|
|
||||||
while( ( elapsedMicroseconds = microStopwatch.ElapsedMicroseconds )
|
while( ( elapsedMicroseconds = microStopwatch.ElapsedMicroseconds )
|
||||||
< nextNotification )
|
< nextNotification )
|
||||||
{
|
{
|
||||||
System.Threading.Thread.SpinWait( 10 );
|
System.Threading.Thread.SpinWait( 10 );
|
||||||
}
|
}
|
||||||
|
|
||||||
long timerLateBy = elapsedMicroseconds - nextNotification;
|
long timerLateBy = elapsedMicroseconds - nextNotification;
|
||||||
|
|
||||||
if( timerLateBy >= ignoreEventIfLateByCurrent )
|
if( timerLateBy >= ignoreEventIfLateByCurrent )
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
MicroTimerEventArgs microTimerEventArgs =
|
MicroTimerEventArgs microTimerEventArgs =
|
||||||
new MicroTimerEventArgs(timerCount,
|
new MicroTimerEventArgs(timerCount,
|
||||||
elapsedMicroseconds,
|
elapsedMicroseconds,
|
||||||
timerLateBy,
|
timerLateBy,
|
||||||
callbackFunctionExecutionTime);
|
callbackFunctionExecutionTime);
|
||||||
MicroTimerElapsed( this, microTimerEventArgs );
|
MicroTimerElapsed( this, microTimerEventArgs );
|
||||||
}
|
}
|
||||||
|
|
||||||
microStopwatch.Stop();
|
microStopwatch.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MicroTimer Event Argument class
|
/// MicroTimer Event Argument class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MicroTimerEventArgs : EventArgs
|
public class MicroTimerEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
// Simple counter, number times timed event (callback function) executed
|
// Simple counter, number times timed event (callback function) executed
|
||||||
public int TimerCount { get; private set; }
|
public int TimerCount { get; private set; }
|
||||||
|
|
||||||
// Time when timed event was called since timer started
|
// Time when timed event was called since timer started
|
||||||
public long ElapsedMicroseconds { get; private set; }
|
public long ElapsedMicroseconds { get; private set; }
|
||||||
|
|
||||||
// How late the timer was compared to when it should have been called
|
// How late the timer was compared to when it should have been called
|
||||||
public long TimerLateBy { get; private set; }
|
public long TimerLateBy { get; private set; }
|
||||||
|
|
||||||
// Time it took to execute previous call to callback function (OnTimedEvent)
|
// Time it took to execute previous call to callback function (OnTimedEvent)
|
||||||
public long CallbackFunctionExecutionTime { get; private set; }
|
public long CallbackFunctionExecutionTime { get; private set; }
|
||||||
|
|
||||||
public MicroTimerEventArgs( int timerCount,
|
public MicroTimerEventArgs( int timerCount,
|
||||||
long elapsedMicroseconds,
|
long elapsedMicroseconds,
|
||||||
long timerLateBy,
|
long timerLateBy,
|
||||||
long callbackFunctionExecutionTime )
|
long callbackFunctionExecutionTime )
|
||||||
{
|
{
|
||||||
TimerCount = timerCount;
|
TimerCount = timerCount;
|
||||||
ElapsedMicroseconds = elapsedMicroseconds;
|
ElapsedMicroseconds = elapsedMicroseconds;
|
||||||
TimerLateBy = timerLateBy;
|
TimerLateBy = timerLateBy;
|
||||||
CallbackFunctionExecutionTime = callbackFunctionExecutionTime;
|
CallbackFunctionExecutionTime = callbackFunctionExecutionTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class Timer
|
public class Timer
|
||||||
{
|
{
|
||||||
MicroStopwatch m_watch;
|
MicroStopwatch m_watch;
|
||||||
private long startTime;
|
private long startTime;
|
||||||
private long stopTime;
|
private long stopTime;
|
||||||
private long freq;
|
private long freq;
|
||||||
private long freq_millis;
|
private long freq_millis;
|
||||||
|
|
||||||
public Timer()
|
public Timer()
|
||||||
{
|
{
|
||||||
m_watch = new MicroStopwatch();
|
m_watch = new MicroStopwatch();
|
||||||
//startTime = m_watch.ElapsedMicroseconds;
|
//startTime = m_watch.ElapsedMicroseconds;
|
||||||
//stopTime = m_watch.ElapsedMicroseconds;
|
//stopTime = m_watch.ElapsedMicroseconds;
|
||||||
freq = 1000 * 1000;
|
freq = 1000 * 1000;
|
||||||
freq_millis = freq / 1000;
|
freq_millis = freq / 1000;
|
||||||
|
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the timer
|
// Start the timer
|
||||||
|
|
||||||
public Timer Start()
|
public Timer Start()
|
||||||
{
|
{
|
||||||
m_watch.Start();
|
m_watch.Start();
|
||||||
startTime = m_watch.ElapsedMicroseconds;
|
startTime = m_watch.ElapsedMicroseconds;
|
||||||
stopTime = m_watch.ElapsedMicroseconds;
|
stopTime = m_watch.ElapsedMicroseconds;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the timer
|
// Stop the timer
|
||||||
|
|
||||||
public Timer Stop()
|
public Timer Stop()
|
||||||
{
|
{
|
||||||
m_watch.Stop();
|
m_watch.Stop();
|
||||||
stopTime = m_watch.ElapsedMicroseconds;
|
stopTime = m_watch.ElapsedMicroseconds;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Seconds
|
public double Seconds
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
long current = m_watch.ElapsedMicroseconds;
|
long current = m_watch.ElapsedMicroseconds;
|
||||||
return (double)( current - startTime ) / freq;
|
return (double)( current - startTime ) / freq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Current
|
public long Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
long current = m_watch.ElapsedMicroseconds;
|
long current = m_watch.ElapsedMicroseconds;
|
||||||
return ( current - startTime ) / freq_millis;
|
return ( current - startTime ) / freq_millis;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Duration
|
public double Duration
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return (double)( stopTime - startTime ) / (double)freq;
|
return (double)( stopTime - startTime ) / (double)freq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long DurationMS
|
public long DurationMS
|
||||||
{
|
{
|
||||||
get { return ( stopTime - startTime ) / freq_millis; }
|
get { return ( stopTime - startTime ) / freq_millis; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class TimerWin
|
public class TimerWin
|
||||||
{
|
{
|
||||||
[DllImport( "Kernel32.dll" )]
|
[DllImport( "Kernel32.dll" )]
|
||||||
private static extern bool QueryPerformanceCounter(
|
private static extern bool QueryPerformanceCounter(
|
||||||
out long lpPerformanceCount );
|
out long lpPerformanceCount );
|
||||||
|
|
||||||
[DllImport( "Kernel32.dll" )]
|
[DllImport( "Kernel32.dll" )]
|
||||||
private static extern bool QueryPerformanceFrequency(
|
private static extern bool QueryPerformanceFrequency(
|
||||||
out long lpFrequency );
|
out long lpFrequency );
|
||||||
|
|
||||||
private long startTime;
|
private long startTime;
|
||||||
private long stopTime;
|
private long stopTime;
|
||||||
private long freq;
|
private long freq;
|
||||||
private long freq_millis;
|
private long freq_millis;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
|
|
||||||
public TimerWin()
|
public TimerWin()
|
||||||
{
|
{
|
||||||
startTime = 0;
|
startTime = 0;
|
||||||
stopTime = 0;
|
stopTime = 0;
|
||||||
|
|
||||||
if( QueryPerformanceFrequency( out freq ) == false )
|
if( QueryPerformanceFrequency( out freq ) == false )
|
||||||
{
|
{
|
||||||
// high-performance counter not supported
|
// high-performance counter not supported
|
||||||
throw new Win32Exception();
|
throw new Win32Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
freq_millis = freq / 1000;
|
freq_millis = freq / 1000;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the timer
|
// Start the timer
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
// lets do the waiting threads there work
|
// lets do the waiting threads there work
|
||||||
|
|
||||||
//Thread.Sleep(0);
|
//Thread.Sleep(0);
|
||||||
|
|
||||||
QueryPerformanceCounter( out startTime );
|
QueryPerformanceCounter( out startTime );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the timer
|
// Stop the timer
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
QueryPerformanceCounter( out stopTime );
|
QueryPerformanceCounter( out stopTime );
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Seconds
|
public double Seconds
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
long current;
|
long current;
|
||||||
|
|
||||||
QueryPerformanceCounter( out current );
|
QueryPerformanceCounter( out current );
|
||||||
|
|
||||||
return (double)( current - startTime ) / freq;
|
return (double)( current - startTime ) / freq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Current
|
public long Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
long current;
|
long current;
|
||||||
|
|
||||||
QueryPerformanceCounter( out current );
|
QueryPerformanceCounter( out current );
|
||||||
|
|
||||||
return ( current - startTime ) / freq_millis;
|
return ( current - startTime ) / freq_millis;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double Duration
|
public double Duration
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return (double)( stopTime - startTime ) / (double)freq;
|
return (double)( stopTime - startTime ) / (double)freq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long DurationMS
|
public long DurationMS
|
||||||
{
|
{
|
||||||
get { return ( stopTime - startTime ) / freq_millis; }
|
get { return ( stopTime - startTime ) / freq_millis; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user