211 lines
3.7 KiB
C#
211 lines
3.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
/*
|
|
|
|
TODO:
|
|
x)
|
|
|
|
*/
|
|
|
|
namespace lib
|
|
{
|
|
|
|
public class DescAttribute : Attribute
|
|
{
|
|
public string Desc { get; private set; }
|
|
|
|
public DescAttribute( string desc )
|
|
{
|
|
Desc = desc;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ConfigCfg : Config
|
|
{
|
|
public readonly bool writeOutTemplateFiles = true;
|
|
}
|
|
|
|
public interface ConfigBase
|
|
{
|
|
|
|
#region SaveLoad
|
|
|
|
static public T res_load<T>( string filename ) where T : Config
|
|
{
|
|
return load<T>( filename );
|
|
}
|
|
|
|
static public Config load( string filename )
|
|
{
|
|
return load( filename, null );
|
|
}
|
|
|
|
static public T load<T>( string filename ) where T : Config
|
|
{
|
|
return (T)load( filename, typeof( T ) );
|
|
}
|
|
|
|
static public Config load( string filename, Type t )
|
|
{
|
|
Config cfg = null;
|
|
|
|
try
|
|
{
|
|
if( File.Exists( filename ) )
|
|
{
|
|
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
|
|
|
XmlFormatter2 formatter = new XmlFormatter2();
|
|
|
|
cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) );
|
|
|
|
cfg.SetFilename( filename );
|
|
}
|
|
else
|
|
{
|
|
cfg = CreateTemplate( filename, t );
|
|
}
|
|
}
|
|
catch( IOException )
|
|
{
|
|
cfg = CreateTemplate( filename, t );
|
|
}
|
|
|
|
return cfg;
|
|
}
|
|
|
|
private static Config CreateTemplate( string filename, Type t )
|
|
{
|
|
log.debug( $"Config file {filename} not found, creating template." );
|
|
|
|
Type[] types = new Type[0];
|
|
object[] parms = new object[0];
|
|
|
|
//types[ 0 ] = typeof( string );
|
|
//parms[ 0 ] = filename;
|
|
Config cfg = null;
|
|
|
|
ConstructorInfo cons = t?.GetConstructor( types );
|
|
|
|
try
|
|
{
|
|
cfg = (Config)cons?.Invoke( parms );
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
|
|
}
|
|
|
|
//cfg.SetFilename( filename );
|
|
|
|
if( ConfigCfg.s_cfg.writeOutTemplateFiles )
|
|
{
|
|
var templateFile = $"templates/{filename}";
|
|
|
|
var dirName = Path.GetDirectoryName( templateFile );
|
|
|
|
Util.checkAndAddDirectory( dirName );
|
|
|
|
log.info( $"Writing out template config of type {t?.Name} in {templateFile}" );
|
|
|
|
saveDebug( cfg, templateFile );
|
|
}
|
|
|
|
return cfg;
|
|
}
|
|
|
|
static public void saveDebug( Config cfg )
|
|
{
|
|
saveDebug( cfg, cfg.Filename );
|
|
}
|
|
|
|
static ser.XmlCfg s_templateCfg = new()
|
|
{
|
|
POD = ser.POD.Elements,
|
|
Structure = ser.Datastructure.Tree,
|
|
Naming = ser.BackingFieldNaming.Regular,
|
|
};
|
|
|
|
static public void saveDebug( Config cfg, String filename )
|
|
{
|
|
FileStream fs = new( filename, FileMode.Create, FileAccess.Write );
|
|
|
|
UTF8Encoding enc = new UTF8Encoding(true);
|
|
|
|
byte[] info = enc.GetBytes($"<!--\nCaused when trying to load [{filename}]-->");
|
|
fs.Write(info, 0, info.Length);
|
|
|
|
ser.XmlSer formatter = new( s_templateCfg );
|
|
|
|
formatter.Serialize( fs, cfg );
|
|
|
|
fs.Close();
|
|
}
|
|
#endregion
|
|
|
|
|
|
public String Filename { get; }
|
|
|
|
//internal void SetFilename( String filename );
|
|
|
|
|
|
}
|
|
|
|
/* UNUSED
|
|
public record class ConfigRec : ConfigBase
|
|
{
|
|
|
|
}
|
|
*/
|
|
|
|
public class Config : ConfigBase
|
|
{
|
|
|
|
//private int _test = 0;
|
|
|
|
private static ser.XmlCfg s_templateCfg = new()
|
|
{
|
|
Structure = ser.Datastructure.Tree,
|
|
POD = ser.POD.Elements,
|
|
};
|
|
|
|
|
|
internal static ConfigCfg s_cfg = new();
|
|
|
|
static public void startup( string filename )
|
|
{
|
|
res.Mgr.Register( ConfigBase.load );
|
|
res.Mgr.RegisterSub( typeof( ConfigBase ) );
|
|
|
|
s_cfg = ConfigBase.load<ConfigCfg>( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
public Config()
|
|
{
|
|
}
|
|
|
|
public Config( string filename )
|
|
{
|
|
_filename = filename;
|
|
}
|
|
|
|
private string _filename = "{unknown}";
|
|
|
|
|
|
public String Filename { get { return _filename; } }
|
|
|
|
internal void SetFilename( String filename ) { _filename = filename; }
|
|
|
|
|
|
}
|
|
}
|
|
|