2017-06-16 18:27:53 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2017-06-16 20:03:38 +02:00
using System.Xml ;
using System.Reflection ;
using System.Xml.Serialization ;
using System.IO ;
using System.Windows.Forms ;
2017-06-16 18:27:53 +02:00
namespace EduNetworkBuilder
{
/// <summary>
/// We have settings that need to be loaded and saved. Mono has issues with
/// properties.settings.default stuff, so we need to load/save from an xml file
/// when we are using mono. So this class does that
/// </summary>
public class NBSettings
{
2017-07-31 19:08:01 +02:00
protected bool BelongsToUser = false ;
public int FileHistoryCount = 5 ;
2017-06-16 18:27:53 +02:00
public string LastPath = "" ;
public List < string > ScoreList = new List < string > ( ) ;
2017-07-31 19:08:01 +02:00
public List < string > RecentFiles = new List < string > ( ) ;
2018-10-05 19:43:57 +02:00
public List < string > PuzzlesPassed = new List < string > ( ) ;
public List < string > PuzzlesFailed = new List < string > ( ) ;
2017-06-16 18:27:53 +02:00
public bool AutoStartPuzzles = true ;
public string ChosenLanguage = "en" ;
public bool LanguageHasBeenChosen = false ;
public int MainWindowX = - 1 ;
public int MainWindowY = - 1 ;
public int MainWindowWidth = - 1 ;
public int MainWindowHeight = - 1 ;
public string ProcessingLevel = "none" ;
2017-07-31 18:31:24 +02:00
public UInt32 NextID = 100 ;
2017-08-14 17:23:20 +02:00
public bool AutoDHCPAllMachinesAtNetworkLoad = false ; //Mainly for teachers
2017-10-03 22:19:18 +02:00
public string LastTrans1Lang = "" ;
public string LastTrans2Lang = "" ;
2017-10-30 22:47:06 +01:00
public string FormLastTransDir = "" ;
2019-01-23 15:24:30 +01:00
public int MillisecondsBetweenPacketMoves = NB . MillisecondsBetweenPacketMoves ; //How quick do packets move
2018-10-05 19:47:18 +02:00
[XmlIgnore] //it is protected so it should not be serialized anyway.
2018-05-03 19:58:54 +02:00
protected ActionCollection UserActions = new ActionCollection ( ) ;
2018-05-09 23:15:37 +02:00
[XmlIgnore]
2018-05-15 15:27:41 +02:00
public bool ReplayMode = false ; //Whether or not we are able to do replays
2017-07-31 18:31:24 +02:00
2017-06-16 18:27:53 +02:00
public NBSettings ( )
{
2017-06-16 20:03:38 +02:00
//We need this for reflection
2017-07-31 19:08:01 +02:00
BelongsToUser = true ; //Only true when using reflection (serialization)
2017-06-16 20:03:38 +02:00
}
public NBSettings ( NBSettings fromSettings )
{
if ( fromSettings = = null ) return ; //Break if we are going to error out.
Duplicate ( fromSettings , this ) ;
}
2018-05-03 19:58:54 +02:00
public void RegisterNetAsStarted ( Network newnet )
{
UserActions . RegisterNet ( newnet ) ;
}
public void RegisterActionDone ( ActionClass What )
{
UserActions . Add ( What ) ;
}
2018-10-05 20:00:04 +02:00
public void RegisterReplayPassed ( string networkname = "" )
2018-09-24 17:24:02 +02:00
{
if ( UserActions = = null ) return ;
2018-10-05 20:00:04 +02:00
NetworkAction NA ;
if ( networkname = = "" )
NA = UserActions . GetCurrentNetAction ( ) ;
else NA = UserActions . FindAction ( networkname ) ;
2018-10-05 19:53:38 +02:00
if ( NA ! = null )
{
NA . SetPassed ( ) ;
if ( ! PuzzlesPassed . Contains ( NA . NetworkName ) ) PuzzlesPassed . Add ( NA . NetworkName ) ;
if ( PuzzlesFailed . Contains ( NA . NetworkName ) ) PuzzlesFailed . Remove ( NA . NetworkName ) ;
}
2018-09-24 17:24:02 +02:00
}
2018-10-05 20:00:04 +02:00
public void RegisterReplayFailed ( string networkname = "" )
2018-09-24 17:24:02 +02:00
{
if ( UserActions = = null ) return ;
2018-10-05 20:00:04 +02:00
NetworkAction NA ;
if ( networkname = = "" )
NA = UserActions . GetCurrentNetAction ( ) ;
else NA = UserActions . FindAction ( networkname ) ;
2018-10-05 19:53:38 +02:00
if ( NA ! = null )
{
NA . SetFailed ( ) ;
if ( ! PuzzlesFailed . Contains ( NA . NetworkName ) ) PuzzlesFailed . Add ( NA . NetworkName ) ;
if ( PuzzlesPassed . Contains ( NA . NetworkName ) ) PuzzlesPassed . Remove ( NA . NetworkName ) ;
}
2018-09-24 17:24:02 +02:00
}
2018-10-05 20:06:53 +02:00
public void ClearReplayStatus ( )
{
if ( UserActions = = null ) return ;
foreach ( NetworkAction NA in UserActions . NetActions )
{
NA . HasFailed = false ;
NA . HasPassed = false ;
}
PuzzlesPassed . Clear ( ) ;
PuzzlesFailed . Clear ( ) ;
}
2018-05-03 19:58:54 +02:00
public ActionCollection GetUserActionCollection ( )
{
return UserActions ;
}
2017-06-16 20:03:38 +02:00
static void Duplicate ( NBSettings FromClass , NBSettings ToClass )
{
if ( FromClass = = null ) return ; //Break if we are going to error out.
if ( ToClass = = null ) return ; //Break if we are going to error out.
2017-07-29 01:06:41 +02:00
//
if ( Properties . Settings . Default . ScoreList = = null )
Properties . Settings . Default . ScoreList = new System . Collections . Specialized . StringCollection ( ) ;
2017-07-31 19:08:01 +02:00
if ( Properties . Settings . Default . RecentFiles = = null )
Properties . Settings . Default . RecentFiles = new System . Collections . Specialized . StringCollection ( ) ;
2018-10-05 19:43:57 +02:00
if ( Properties . Settings . Default . PuzzlesFailed = = null )
Properties . Settings . Default . PuzzlesFailed = new System . Collections . Specialized . StringCollection ( ) ;
if ( Properties . Settings . Default . PuzzlesPassed = = null )
Properties . Settings . Default . PuzzlesPassed = new System . Collections . Specialized . StringCollection ( ) ;
2017-07-29 01:06:41 +02:00
2017-06-16 20:03:38 +02:00
ToClass . LastPath = FromClass . LastPath ;
ToClass . AutoStartPuzzles = FromClass . AutoStartPuzzles ;
ToClass . ChosenLanguage = FromClass . ChosenLanguage ;
ToClass . LanguageHasBeenChosen = FromClass . LanguageHasBeenChosen ;
ToClass . MainWindowX = FromClass . MainWindowX ;
ToClass . MainWindowY = FromClass . MainWindowY ;
ToClass . MainWindowHeight = FromClass . MainWindowHeight ;
ToClass . MainWindowWidth = FromClass . MainWindowWidth ;
2017-07-31 19:08:01 +02:00
ToClass . BelongsToUser = FromClass . BelongsToUser ;
2017-08-14 17:23:20 +02:00
ToClass . AutoDHCPAllMachinesAtNetworkLoad = FromClass . AutoDHCPAllMachinesAtNetworkLoad ;
2017-10-30 22:47:06 +01:00
ToClass . FormLastTransDir = FromClass . FormLastTransDir ;
2019-01-23 15:24:30 +01:00
ToClass . MillisecondsBetweenPacketMoves = FromClass . MillisecondsBetweenPacketMoves ;
2017-06-16 20:03:38 +02:00
foreach ( string one in FromClass . ScoreList )
{
if ( ! ToClass . ScoreList . Contains ( one ) )
ToClass . ScoreList . Add ( one ) ;
}
2017-07-31 19:08:01 +02:00
foreach ( string one in FromClass . RecentFiles )
{
if ( ! ToClass . RecentFiles . Contains ( one ) )
ToClass . RecentFiles . Add ( one ) ;
}
2018-10-05 19:43:57 +02:00
foreach ( string one in FromClass . PuzzlesPassed )
{
if ( ! ToClass . PuzzlesPassed . Contains ( one ) )
ToClass . PuzzlesPassed . Add ( one ) ;
}
foreach ( string one in FromClass . PuzzlesFailed )
{
if ( ! ToClass . PuzzlesFailed . Contains ( one ) )
ToClass . PuzzlesFailed . Add ( one ) ;
}
2017-06-16 20:03:38 +02:00
}
public NBSettings ( bool UsingMono )
{
if ( UsingMono )
2017-06-16 18:27:53 +02:00
{
2017-06-16 20:03:38 +02:00
try
{
//Try loading from xml file
string filename = GetFilename ( ) ;
if ( File . Exists ( filename ) )
{
XmlSerializer sr = new XmlSerializer ( this . GetType ( ) ) ;
TextReader Reader = new StreamReader ( filename ) ;
NBSettings settings = ( NBSettings ) sr . Deserialize ( Reader ) ;
Duplicate ( settings , this ) ;
Reader . Close ( ) ;
}
}
catch ( Exception e )
{
MessageBox . Show ( "Error Loading Settings:\n" + e . ToString ( ) , "Error Loading Settings" ) ;
}
2017-06-16 18:27:53 +02:00
}
else
{
2017-07-29 01:06:41 +02:00
if ( Properties . Settings . Default . ScoreList = = null )
Properties . Settings . Default . ScoreList = new System . Collections . Specialized . StringCollection ( ) ;
2017-07-31 19:08:01 +02:00
if ( Properties . Settings . Default . RecentFiles = = null )
Properties . Settings . Default . RecentFiles = new System . Collections . Specialized . StringCollection ( ) ;
2018-10-05 19:43:57 +02:00
if ( Properties . Settings . Default . PuzzlesFailed = = null )
Properties . Settings . Default . PuzzlesFailed = new System . Collections . Specialized . StringCollection ( ) ;
if ( Properties . Settings . Default . PuzzlesPassed = = null )
Properties . Settings . Default . PuzzlesPassed = new System . Collections . Specialized . StringCollection ( ) ;
2017-07-29 01:06:41 +02:00
2017-06-16 18:27:53 +02:00
LastPath = Properties . Settings . Default . LastPath ;
AutoStartPuzzles = Properties . Settings . Default . AutoStartPuzzles ;
ChosenLanguage = Properties . Settings . Default . ChosenLanguage ;
LanguageHasBeenChosen = Properties . Settings . Default . LanguageHasBeenChosen ;
MainWindowX = Properties . Settings . Default . MainWindowX ;
MainWindowY = Properties . Settings . Default . MainWindowY ;
MainWindowHeight = Properties . Settings . Default . MainWindowHeight ;
MainWindowWidth = Properties . Settings . Default . MainWindowWidth ;
2017-08-14 17:23:20 +02:00
AutoDHCPAllMachinesAtNetworkLoad = Properties . Settings . Default . AutoDHCP ;
2017-10-03 22:19:18 +02:00
LastTrans1Lang = Properties . Settings . Default . LastTrans1Lang ;
LastTrans2Lang = Properties . Settings . Default . LastTrans2Lang ;
2017-10-30 22:47:06 +01:00
FormLastTransDir = Properties . Settings . Default . FormTransLastDir ;
2019-01-23 15:24:30 +01:00
//MillisecondsBetweenPacketMoves = Properties.Settings.Default.MSBetweenPacketMoves; //For the moment we do not have a place to edit this. Do not save it so we can change it easier through code
2017-06-16 18:27:53 +02:00
2017-10-03 22:19:18 +02:00
foreach ( string one in Properties . Settings . Default . ScoreList )
2017-06-16 18:27:53 +02:00
{
if ( ! ScoreList . Contains ( one ) )
ScoreList . Add ( one ) ;
}
2018-10-05 19:48:27 +02:00
foreach ( string one in Properties . Settings . Default . PuzzlesFailed )
{
if ( ! PuzzlesFailed . Contains ( one ) )
PuzzlesFailed . Add ( one ) ;
}
foreach ( string one in Properties . Settings . Default . PuzzlesPassed )
{
if ( ! PuzzlesPassed . Contains ( one ) )
PuzzlesPassed . Add ( one ) ;
}
2017-06-16 18:27:53 +02:00
}
}
2017-07-30 01:42:27 +02:00
public void MarkAsDone ( string PuzzleName )
2017-07-30 21:55:37 +02:00
{
2017-07-30 01:42:27 +02:00
//we just mark it in the settings class
if ( ! ScoreList . Contains ( PuzzleName ) )
{
ScoreList . Add ( PuzzleName ) ;
Save ( ) ;
}
}
2018-05-15 15:27:41 +02:00
public void LoadReplays ( )
{
ReplayMode = true ;
UserActions = NB . ReadFromXmlResource < ActionCollection > ( "EduNetworkReplay" ) ;
2018-05-16 21:18:07 +02:00
foreach ( NetworkAction NetAction in UserActions . NetActions )
2018-05-16 16:14:18 +02:00
{
NetAction . HasBeenStored = true ; //All of them start out stored
2018-09-21 20:09:07 +02:00
NetAction . HasFailed = false ;
NetAction . HasPassed = false ;
2018-05-16 16:14:18 +02:00
}
2018-05-16 21:18:07 +02:00
UserActions . HasUnsavedChanges = false ; //we have just loaded a clean thing. Nothing to save
2018-05-15 15:27:41 +02:00
}
2018-09-21 18:32:48 +02:00
public bool HasReplay ( string networkname )
{
if ( UserActions = = null ) return false ;
NetworkAction NA = UserActions . FindAction ( networkname ) ;
if ( NA = = null ) return false ;
return true ;
}
2018-09-21 20:11:05 +02:00
public bool HasPassed ( string networkname )
{
if ( UserActions = = null ) return false ;
2018-10-05 20:00:04 +02:00
if ( PuzzlesPassed . Contains ( networkname ) ) return true ;
2018-09-21 20:11:05 +02:00
NetworkAction NA = UserActions . FindAction ( networkname ) ;
if ( NA = = null ) return false ;
return NA . HasPassed ;
}
public bool HasFailed ( string networkname )
{
if ( UserActions = = null ) return false ;
2018-10-05 20:00:04 +02:00
if ( PuzzlesFailed . Contains ( networkname ) ) return true ;
2018-09-21 20:11:05 +02:00
NetworkAction NA = UserActions . FindAction ( networkname ) ;
if ( NA = = null ) return false ;
return NA . HasFailed ;
}
2018-09-21 20:09:07 +02:00
public void SetPassed ( string networkname )
{
2018-10-05 20:00:04 +02:00
RegisterReplayPassed ( networkname ) ;
2018-09-21 20:09:07 +02:00
}
public void SetFailed ( string networkname )
{
2018-10-05 20:00:04 +02:00
RegisterReplayFailed ( networkname ) ;
2018-09-21 20:09:07 +02:00
}
2017-07-30 01:52:23 +02:00
public bool CheckIfDone ( string PuzzleName )
2017-07-30 21:55:37 +02:00
{
2017-07-30 01:52:23 +02:00
//we just mark it in the settings class
if ( ScoreList . Contains ( PuzzleName ) )
return true ;
else
return false ;
}
2017-06-16 20:03:38 +02:00
public static string GetFilename ( )
2017-06-16 18:27:53 +02:00
{
2017-06-16 20:03:38 +02:00
string BaseDir = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
string FileName = "EduNetworkBuilder_config.xml" ;
return Path . Combine ( BaseDir , FileName ) ;
}
2017-07-31 19:08:01 +02:00
public void RegisterFileAsLoaded ( string FilenameWithPath )
{
//Remove it from these settings
if ( RecentFiles . Contains ( FilenameWithPath ) ) RecentFiles . Remove ( FilenameWithPath ) ;
RecentFiles . Insert ( 0 , FilenameWithPath ) ;
if ( RecentFiles . Count > FileHistoryCount )
for ( int index = RecentFiles . Count - 1 ; index > = FileHistoryCount ; index - - )
RecentFiles . RemoveAt ( index ) ;
if ( ! NB . IsRunningOnMono ( ) )
{
if ( Properties . Settings . Default . RecentFiles . Contains ( FilenameWithPath ) )
Properties . Settings . Default . RecentFiles . Remove ( FilenameWithPath ) ;
Properties . Settings . Default . RecentFiles . Insert ( 0 , FilenameWithPath ) ;
if ( Properties . Settings . Default . RecentFiles . Count > FileHistoryCount )
{
for ( int index = Properties . Settings . Default . RecentFiles . Count - 1 ; index > = FileHistoryCount ; index - - )
Properties . Settings . Default . RecentFiles . RemoveAt ( index ) ;
}
Properties . Settings . Default . Save ( ) ;
}
}
2017-06-19 18:17:09 +02:00
2017-07-31 20:39:46 +02:00
public List < string > GetRecentFiles ( )
{
//If we do not have a user and are not using mono, grab the settings from properties.settings
//Otherwise, we grab the settings from this settings object
List < string > TheFiles = new List < string > ( ) ;
if ( NB . GetUser ( ) = = null & & ! NB . IsRunningOnMono ( ) )
{
foreach ( string one in Properties . Settings . Default . RecentFiles )
TheFiles . Add ( one ) ;
}
else
{
//This is the repository of recent files. Use it
TheFiles . AddRange ( RecentFiles ) ;
}
return TheFiles ;
}
2017-06-19 18:17:09 +02:00
public void Save ( )
{
Save ( NB . IsRunningOnMono ( ) ) ;
}
2017-07-31 18:27:25 +02:00
public UInt64 IssueID ( )
{
//return the id and increment it.
return NextID + + ;
}
2018-05-14 15:05:56 +02:00
/// <summary>
/// We are saving the ReplayActions. Right now, we are doing this for automatic testing
/// of the puzzles. Regression Testing
/// </summary>
public void SaveActions ( )
{
NB . WriteToXmlFile ( @"c:\Users\TimYo\Desktop\EduNetworkReplay.xml" , UserActions ) ;
}
2017-06-16 20:03:38 +02:00
public void Save ( bool UsingMono )
{
if ( UsingMono )
2017-06-16 18:27:53 +02:00
{
//Save it to an XML file.
2017-06-16 20:03:38 +02:00
try
{
string filename = GetFilename ( ) ;
if ( ! Directory . Exists ( Path . GetDirectoryName ( filename ) ) )
{
Directory . CreateDirectory ( Path . GetDirectoryName ( filename ) ) ;
}
XmlSerializer sr = new XmlSerializer ( this . GetType ( ) ) ;
TextWriter writer = new StreamWriter ( filename ) ;
sr . Serialize ( writer , this ) ;
writer . Close ( ) ;
}
catch ( Exception e )
{
MessageBox . Show ( "ERROR Saving Settings:\n" + e . ToString ( ) , "Error saving settings." ) ;
}
2017-06-16 18:27:53 +02:00
}
else
{
if ( Properties . Settings . Default . ScoreList = = null )
Properties . Settings . Default . ScoreList = new System . Collections . Specialized . StringCollection ( ) ;
2017-07-31 23:36:07 +02:00
if ( NB . GetUser ( ) = = null )
{
Properties . Settings . Default . AutoStartPuzzles = AutoStartPuzzles ;
Properties . Settings . Default . LanguageHasBeenChosen = LanguageHasBeenChosen ;
}
else
{
Properties . Settings . Default . AutoStartPuzzles = AutoStartPuzzles ;
Properties . Settings . Default . LanguageHasBeenChosen = LanguageHasBeenChosen ;
}
2017-06-16 18:27:53 +02:00
Properties . Settings . Default . LastPath = LastPath ;
Properties . Settings . Default . ChosenLanguage = ChosenLanguage ;
Properties . Settings . Default . MainWindowX = MainWindowX ;
Properties . Settings . Default . MainWindowY = MainWindowY ;
Properties . Settings . Default . MainWindowHeight = MainWindowHeight ;
Properties . Settings . Default . MainWindowWidth = MainWindowWidth ;
2017-08-14 17:23:20 +02:00
Properties . Settings . Default . AutoDHCP = AutoDHCPAllMachinesAtNetworkLoad ;
2017-10-03 22:19:18 +02:00
Properties . Settings . Default . LastTrans1Lang = LastTrans1Lang ;
Properties . Settings . Default . LastTrans2Lang = LastTrans2Lang ;
2017-10-30 22:47:06 +01:00
Properties . Settings . Default . FormTransLastDir = FormLastTransDir ;
2019-01-23 15:24:30 +01:00
Properties . Settings . Default . MSBetweenPacketMoves = MillisecondsBetweenPacketMoves ;
2017-06-16 18:27:53 +02:00
Properties . Settings . Default . ScoreList . Clear ( ) ;
foreach ( string One in ScoreList )
{
Properties . Settings . Default . ScoreList . Add ( One ) ;
}
2017-07-31 19:08:01 +02:00
2018-10-05 19:47:18 +02:00
Properties . Settings . Default . PuzzlesFailed . Clear ( ) ;
foreach ( string One in PuzzlesFailed )
{
Properties . Settings . Default . PuzzlesFailed . Add ( One ) ;
}
Properties . Settings . Default . PuzzlesPassed . Clear ( ) ;
foreach ( string One in PuzzlesPassed )
{
Properties . Settings . Default . PuzzlesPassed . Add ( One ) ;
}
2017-06-16 18:27:53 +02:00
Properties . Settings . Default . Save ( ) ;
}
}
}
}