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>();
|
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 = "";
|
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);
|
|
|
|
|
}
|
|
|
|
|
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();
|
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;
|
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);
|
|
|
|
|
}
|
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();
|
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;
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
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
|
|
|
|
|
2017-06-16 18:27:53 +02:00
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|