using System ;

using System.Collections.Generic ;

using System.Linq ;

using System.Text ;

using System.Threading.Tasks ;

using System.Xml.Serialization ;

using System.IO ;

using System.IO.Compression ;

namespace StoryEngine

{

/// <summary>

/// XML Serialization Helper class.

/// Statis helper class that provides methods to conveniently

/// save and load objects of arbitrary types using .Net's

/// XML serialization facilities.

/// </summary>

public static class XMLSerializationHelper

{

public static bool SerializeObjectToFile ( string Filename, object ToSerialize )

{

try

{

if ( File . Exists ( Filename ) )

{

Stream stream = File . Open ( Filename, FileMode . Truncate ) ;

return SerializeObjectToStream ( ref stream, ToSerialize ) ;

}

else

{

Stream stream = File . Open ( Filename, FileMode . OpenOrCreate ) ;

return SerializeObjectToStream ( ref stream, ToSerialize ) ;

}

}

catch ( Exception e )

{

Logger . WriteException ( e ) ;

return false ;

}

}

public static T DeserializeObjectFromFile < T > ( string Filename )

{

try

{

if ( File . Exists ( Filename ) == false )

{

return default ( T ) ;

}

else

{

Stream stream = File . Open ( Filename, FileMode . Open ) ;

return DeserializeObjectFromStream < T > ( ref stream ) ;

}

}

catch ( Exception e )

{

Logger . WriteException ( e ) ;

return default ( T ) ;

}

}

public static T DeserializeObjectFromZipEntry < T > ( ZipArchiveEntry Entry )

{

try

{

Stream stream = Entry . Open ( ) ;

return DeserializeObjectFromStream < T > ( ref stream ) ;

}

catch ( Exception e )

{

Logger . WriteException ( e ) ;

return default ( T ) ;

}

}

private static T DeserializeObjectFromStream < T > ( ref Stream SrcStream )

{

try

{

XmlSerializer Deserializer = new XmlSerializer ( typeof ( T ) ) ;

return ( T ) Deserializer . Deserialize ( SrcStream ) ;

}

catch ( Exception e )

{

Logger . WriteException ( e ) ;

return default ( T ) ;

}

}

private static bool SerializeObjectToStream ( ref Stream TargetStream, object ToSerialize )

{

try

{

XmlSerializer Serializer = new XmlSerializer ( ToSerialize . GetType ( ) ) ;

Serializer . Serialize ( TargetStream, ToSerialize ) ;

TargetStream . Close ( ) ;

return true ;

}

catch ( Exception e )

{

Logger . WriteException ( e ) ;

return false ;

}

}

}

/// <summary>

/// Logger class.

/// Static class that contains methods aimed towards

/// helping to make application logging as simple as

/// possible.

/// </summary>

public static class Logger

{

private static StreamWriter _LogWriterInstance ;

public static StreamWriter LogWriter

{

get

{

if ( _LogWriterInstance != null )

{

if ( Directory . Exists ( Path . GetFullPath ( "logs \\ " ) ) == false )

{

Directory . CreateDirectory ( Path . GetFullPath ( "logs \\ " ) ) ;

}

string DateString = DateTime . Now . ToString ( ) . Replace ( '/' , '-' ) . Replace ( ':' , '.' ) ;

_LogWriterInstance = new StreamWriter ( ( "logs \\ log-" + DateString + ".log" ) . Replace ( ':' , '.' ) ) ;

return _LogWriterInstance ;

}

else

{

return _LogWriterInstance ;

}

}

set

{

_LogWriterInstance = value ;

}

}

private static void WriteToLog ( string Content )

{

try

{

if ( _LogWriterInstance != null )

{

_LogWriterInstance . WriteLine ( Content + "

" ) ;

_LogWriterInstance . Flush ( ) ;

}

else

{

if ( Directory . Exists ( Path . GetFullPath ( "logs \\ " ) ) == false )

{

Directory . CreateDirectory ( Path . GetFullPath ( "logs \\ " ) ) ;

}

string DateString = DateTime . Now . ToString ( ) . Replace ( '/' , '-' ) . Replace ( ':' , '.' ) ;

_LogWriterInstance = new StreamWriter ( ( "logs \\ log-" + DateString + ".log" ) ) ;

_LogWriterInstance . WriteLine ( Content + "

" ) ;

_LogWriterInstance . Flush ( ) ;

}

}

catch ( Exception e )

{

System. Diagnostics . Debug . WriteLine ( e . Message ) ;

System. Diagnostics . Debug . WriteLine ( e . StackTrace ) ;

}

}

public static void WriteException ( Exception e )

{

WriteToLog ( "[DATE][EXCEPTION]: " . Replace ( "DATE" , DateTime . Now . ToString ( ) ) + e . Message ) ;

WriteToLog ( " \t STACK TRACE: " + e . StackTrace ) ;

}

public static void WriteInfo ( string Info )

{

WriteToLog ( "[DATE][INFO]: " . Replace ( "DATE" , DateTime . Now . ToString ( ) ) + Info ) ;

}

}