JsonData skeleton
This commit is contained in:
parent
ce168985a8
commit
81ba16a0d1
196
data/JsonData.cs
Normal file
196
data/JsonData.cs
Normal file
@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
|
||||
|
||||
|
||||
static public class json
|
||||
{
|
||||
|
||||
|
||||
static public T load<T>( string filename ) where T : lib.JsonData
|
||||
{
|
||||
return (T)lib.JsonData.load<T>( filename );
|
||||
}
|
||||
|
||||
static public lib.JsonData load( string filename )
|
||||
{
|
||||
return lib.ConfigBase.load( filename, null );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace lib
|
||||
{
|
||||
|
||||
public interface JsonData
|
||||
{
|
||||
|
||||
#region SaveLoad
|
||||
|
||||
static public T res_load<T>( string filename ) where T : JsonData
|
||||
where T : class
|
||||
{
|
||||
return JsonData.load<T>( filename );
|
||||
}
|
||||
|
||||
static public JsonData load( string filename )
|
||||
{
|
||||
return actual_load( filename, null );
|
||||
}
|
||||
|
||||
static public T load<T>( string filename ) where T : JsonData
|
||||
{
|
||||
return (T)actual_load( filename, typeof( T ) );
|
||||
}
|
||||
|
||||
static public JsonData actual_load( string filename, Type t )
|
||||
{
|
||||
JsonData cfg = null;
|
||||
|
||||
try
|
||||
{
|
||||
if( !File.Exists( filename ) )
|
||||
{
|
||||
if( filename.StartsWith( "res://" ) )
|
||||
filename.Replace( "res://", "" );
|
||||
}
|
||||
|
||||
|
||||
if( File.Exists( filename ) )
|
||||
{
|
||||
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
|
||||
|
||||
XmlFormatter2 formatter = new XmlFormatter2();
|
||||
|
||||
cfg = (JsonData)( 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 JsonData CreateTemplate( string filename, Type t )
|
||||
{
|
||||
log.debug( $"JsonData file {filename} not found, creating template." );
|
||||
|
||||
Type[] types = new Type[0];
|
||||
object[] parms = new object[0];
|
||||
|
||||
//types[ 0 ] = typeof( string );
|
||||
//parms[ 0 ] = filename;
|
||||
JsonData cfg = null;
|
||||
|
||||
ConstructorInfo cons = t?.GetConstructor( types );
|
||||
|
||||
try
|
||||
{
|
||||
cfg = (JsonData)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( JsonData cfg )
|
||||
{
|
||||
JsonData.actual_save( cfg, cfg.Filename );
|
||||
}
|
||||
|
||||
static public void actual_save( JsonData cfg, String filename )
|
||||
{
|
||||
FileStream fs = new( filename, FileMode.Create, FileAccess.Write );
|
||||
fs.Close();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
public String Filename { get; }
|
||||
}
|
||||
|
||||
/* UNUSED
|
||||
public record class ConfigRec : ConfigBase
|
||||
{
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public class JsonData : 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();
|
||||
|
||||
public static ConfigCfg Cfg => s_cfg;
|
||||
|
||||
static public void startup( string filename )
|
||||
{
|
||||
res.Mgr.Register( ConfigBase.load );
|
||||
res.Mgr.RegisterSub( typeof( ConfigBase ) );
|
||||
|
||||
s_cfg = ConfigBase.load<ConfigCfg>( filename );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public JsonData()
|
||||
{
|
||||
}
|
||||
|
||||
public JsonData( string filename )
|
||||
{
|
||||
_filename = filename;
|
||||
}
|
||||
|
||||
private string _filename = "{unknown}";
|
||||
|
||||
|
||||
public String Filename { get { return _filename; } }
|
||||
|
||||
internal void SetFilename( String filename ) { _filename = filename; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user