Code to save to an XML file and fixing localization issues.

This commit is contained in:
Tim Young 2017-06-16 13:03:38 -05:00
parent 16f5c54ee9
commit b62155c76e
3 changed files with 145 additions and 61 deletions

View File

@ -322,11 +322,15 @@ namespace EduNetworkBuilder
/// Find the global random number generator. /// Find the global random number generator.
/// </summary> /// </summary>
/// <returns>A valid random number generator</returns> /// <returns>A valid random number generator</returns>
public static CultureInfo GetCulture() public static CultureInfo GetCulture(NBSettings theSettings = null)
{ {
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
CultureInfo CI=null; CultureInfo CI=null;
NBSettings oursettings = NB.GetSettings(); NBSettings oursettings;
if (theSettings == null) oursettings = NB.GetSettings();
else oursettings = theSettings;
if (myWin != null) if (myWin != null)
{ {
CI = myWin.GetCulture(); CI = myWin.GetCulture();
@ -419,13 +423,14 @@ namespace EduNetworkBuilder
/// </summary> /// </summary>
/// <param name="key">The key for the item we are translating</param> /// <param name="key">The key for the item we are translating</param>
/// <returns>A string of the translated information</returns> /// <returns>A string of the translated information</returns>
public static string Translate(string key) public static string Translate(string key, NBSettings theSettings=null)
{ {
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null) if (myWin == null)
{ {
ResourceManager RM = GetResource(); ResourceManager RM = GetResource();
CultureInfo CI = GetCulture(); CultureInfo CI = GetCulture(theSettings);
string answer=""; string answer="";
answer = RM.GetString(key, CI); answer = RM.GetString(key, CI);
if (answer == null) return ""; if (answer == null) return "";
@ -467,11 +472,15 @@ namespace EduNetworkBuilder
return mystring; return mystring;
} }
public static void ChangeLanguage() public static void ChangeLanguage(NBSettings theSettings=null)
{ {
//Find the window. If it exists, use /set the language setting there. If not, use / set the default. //Find the window. If it exists, use /set the language setting there. If not, use / set the default.
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
NBSettings oursettings = NB.GetSettings(); NBSettings oursettings;
if (theSettings == null) oursettings = NB.GetSettings();
else oursettings = theSettings;
string lang = oursettings.ChosenLanguage; string lang = oursettings.ChosenLanguage;
if (lang == "") lang = "en"; if (lang == "") lang = "en";

View File

@ -3,6 +3,11 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace EduNetworkBuilder namespace EduNetworkBuilder
{ {
@ -26,9 +31,56 @@ namespace EduNetworkBuilder
public NBSettings() public NBSettings()
{ {
if(NB.IsRunningOnMono()) //We need this for reflection
}
public NBSettings(NBSettings fromSettings)
{
if (fromSettings == null) return; //Break if we are going to error out.
Duplicate(fromSettings, this);
}
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.
//
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;
foreach (string one in FromClass.ScoreList)
{ {
//Try loading from xml file if (!ToClass.ScoreList.Contains(one))
ToClass.ScoreList.Add(one);
}
}
public NBSettings(bool UsingMono)
{
if(UsingMono)
{
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");
}
} }
else else
{ {
@ -49,11 +101,34 @@ namespace EduNetworkBuilder
} }
} }
public void Save() public static string GetFilename()
{ {
if(NB.IsRunningOnMono()) string BaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string FileName = "EduNetworkBuilder_config.xml";
return Path.Combine(BaseDir, FileName);
}
public void Save(bool UsingMono)
{
if(UsingMono)
{ {
//Save it to an XML file. //Save it to an XML file.
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.");
}
} }
else else
{ {

View File

@ -19,7 +19,7 @@ namespace EduNetworkBuilder
public partial class BuilderWindow : Form public partial class BuilderWindow : Form
{ {
public Random GameRandomGen = new Random(); public Random GameRandomGen = new Random();
public NBSettings OurSettings = new NBSettings(); //This will auto-load the settings public NBSettings OurSettings = new NBSettings(NB.IsRunningOnMono()); //This will auto-load the settings
private int LastPacketID=1; private int LastPacketID=1;
public DebugPausePoint DebugSetting = DebugPausePoint.none; public DebugPausePoint DebugSetting = DebugPausePoint.none;
@ -58,7 +58,7 @@ namespace EduNetworkBuilder
LastPath = OurSettings.LastPath; LastPath = OurSettings.LastPath;
if(!OurSettings.LanguageHasBeenChosen) if(!OurSettings.LanguageHasBeenChosen)
NB.ChangeLanguage(); NB.ChangeLanguage(OurSettings);
LanguagifyComponents(); LanguagifyComponents();
//I never implimented cut/copy/paste/undo. So we will remove them since they do nothing anyway //I never implimented cut/copy/paste/undo. So we will remove them since they do nothing anyway
cutToolStripMenuItem.Visible = false; cutToolStripMenuItem.Visible = false;
@ -72,12 +72,12 @@ namespace EduNetworkBuilder
// Force the ToolTip text to be displayed whether or not the form is active. // Force the ToolTip text to be displayed whether or not the form is active.
myTooltip.ShowAlways = true; myTooltip.ShowAlways = true;
myTooltip.SetToolTip(rbHelp1, NB.Translate("NB_BuildWindNone")); myTooltip.SetToolTip(rbHelp1, NB.Translate("NB_BuildWindNone", OurSettings));
myTooltip.SetToolTip(rbHelp2, NB.Translate("NB_BuildWindColor")); myTooltip.SetToolTip(rbHelp2, NB.Translate("NB_BuildWindColor", OurSettings));
myTooltip.SetToolTip(rbHelp3, NB.Translate("NB_BuildWindMinor")); myTooltip.SetToolTip(rbHelp3, NB.Translate("NB_BuildWindMinor", OurSettings));
myTooltip.SetToolTip(rbHelp4, NB.Translate("NB_BuildWindDecent")); myTooltip.SetToolTip(rbHelp4, NB.Translate("NB_BuildWindDecent", OurSettings));
myTooltip.SetToolTip(btnHelp, NB.Translate("NB_BuildWindMsg")); myTooltip.SetToolTip(btnHelp, NB.Translate("NB_BuildWindMsg", OurSettings));
myTooltip.SetToolTip(cbViewTitles, NB.Translate("NB_cbViewTitles")); myTooltip.SetToolTip(cbViewTitles, NB.Translate("NB_cbViewTitles", OurSettings));
myTooltip.Popup += myTooltip_Popup; myTooltip.Popup += myTooltip_Popup;
lblStatus.Text = ""; lblStatus.Text = "";
@ -111,47 +111,47 @@ namespace EduNetworkBuilder
private void LanguagifyComponents() private void LanguagifyComponents()
{ {
msMainMenuStrip.Text = NB.Translate("NB_msMainMenuStrip"); msMainMenuStrip.Text = NB.Translate("NB_msMainMenuStrip", OurSettings);
fileToolStripMenuItem.Text = NB.Translate("NB_fileToolStripMenuItem"); fileToolStripMenuItem.Text = NB.Translate("NB_fileToolStripMenuItem", OurSettings);
newToolStripMenuItem.Text = NB.Translate("LBW_btnAdd"); newToolStripMenuItem.Text = NB.Translate("LBW_btnAdd", OurSettings);
loadToolStripMenuItem.Text = NB.Translate("_Load"); loadToolStripMenuItem.Text = NB.Translate("_Load", OurSettings);
reloadToolStripMenuItem.Text = NB.Translate("NB_reloadToolStripMenuItem"); reloadToolStripMenuItem.Text = NB.Translate("NB_reloadToolStripMenuItem", OurSettings);
saveToolStripMenuItem.Text = NB.Translate("NB_saveToolStripMenuItem"); saveToolStripMenuItem.Text = NB.Translate("NB_saveToolStripMenuItem", OurSettings);
exitToolStripMenuItem.Text = NB.Translate("NB_exitToolStripMenuItem"); exitToolStripMenuItem.Text = NB.Translate("NB_exitToolStripMenuItem", OurSettings);
editToolStripMenuItem.Text = NB.Translate("_Edit"); editToolStripMenuItem.Text = NB.Translate("_Edit", OurSettings);
cutToolStripMenuItem.Text = NB.Translate("NB_cutToolStripMenuItem"); cutToolStripMenuItem.Text = NB.Translate("NB_cutToolStripMenuItem", OurSettings);
copyToolStripMenuItem.Text = NB.Translate("NB_copyToolStripMenuItem"); copyToolStripMenuItem.Text = NB.Translate("NB_copyToolStripMenuItem", OurSettings);
pasteToolStripMenuItem.Text = NB.Translate("NB_pasteToolStripMenuItem"); pasteToolStripMenuItem.Text = NB.Translate("NB_pasteToolStripMenuItem", OurSettings);
undoToolStripMenuItem.Text = NB.Translate("NB_undoToolStripMenuItem"); undoToolStripMenuItem.Text = NB.Translate("NB_undoToolStripMenuItem", OurSettings);
optionsToolStripMenuItem.Text = NB.Translate("NB_optionsToolStripMenuItem"); optionsToolStripMenuItem.Text = NB.Translate("NB_optionsToolStripMenuItem", OurSettings);
allToolStripMenuItem.Text = NB.Translate("_All"); allToolStripMenuItem.Text = NB.Translate("_All", OurSettings);
dHCPRequestToolStripMenuItem.Text = NB.Translate("NB_NetViewDHCP"); dHCPRequestToolStripMenuItem.Text = NB.Translate("NB_NetViewDHCP", OurSettings);
clearArpTableToolStripMenuItem.Text = NB.Translate("NB_NetViewClr"); clearArpTableToolStripMenuItem.Text = NB.Translate("NB_NetViewClr", OurSettings);
clearIPsToolStripMenuItem.Text = NB.Translate("NB_clearIPsToolStripMenuItem"); clearIPsToolStripMenuItem.Text = NB.Translate("NB_clearIPsToolStripMenuItem", OurSettings);
pingToolStripMenuItem.Text = NB.Translate("_Ping"); pingToolStripMenuItem.Text = NB.Translate("_Ping", OurSettings);
helpToolStripMenuItem.Text = NB.Translate("_Help"); helpToolStripMenuItem.Text = NB.Translate("_Help", OurSettings);
helpToolStripMenuItem1.Text = NB.Translate("_Help"); helpToolStripMenuItem1.Text = NB.Translate("_Help", OurSettings);
aboutToolStripMenuItem.Text = NB.Translate("NB_aboutToolStripMenuItem"); aboutToolStripMenuItem.Text = NB.Translate("NB_aboutToolStripMenuItem", OurSettings);
releaseNotesToolStripMenuItem.Text = NB.Translate("NB_releaseNotesToolStripMenuItem"); releaseNotesToolStripMenuItem.Text = NB.Translate("NB_releaseNotesToolStripMenuItem", OurSettings);
checkForUpdatesToolStripMenuItem.Text = NB.Translate("NB_checkForUpdatesToolStripMenuItem"); checkForUpdatesToolStripMenuItem.Text = NB.Translate("NB_checkForUpdatesToolStripMenuItem", OurSettings);
samplesToolStripMenuItem.Text = NB.Translate("NB_samplesToolStripMenuItem"); samplesToolStripMenuItem.Text = NB.Translate("NB_samplesToolStripMenuItem", OurSettings);
puzzlesToolStripMenuItem.Text = NB.Translate("NB_puzzlesToolStripMenuItem"); puzzlesToolStripMenuItem.Text = NB.Translate("NB_puzzlesToolStripMenuItem", OurSettings);
solvedToolStripMenuItem.Text = NB.Translate("_Solved"); solvedToolStripMenuItem.Text = NB.Translate("_Solved", OurSettings);
dHCPToolStripMenuItem.Text = NB.Translate("_DHCP"); dHCPToolStripMenuItem.Text = NB.Translate("_DHCP", OurSettings);
oneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork"); oneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork", OurSettings);
twoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks"); twoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks", OurSettings);
threeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks"); threeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks", OurSettings);
firewallsToolStripMenuItem.Text = NB.Translate("NB_Firewalls"); firewallsToolStripMenuItem.Text = NB.Translate("NB_Firewalls", OurSettings);
toSolveToolStripMenuItem.Text = NB.Translate("NB_toSolveToolStripMenuItem"); toSolveToolStripMenuItem.Text = NB.Translate("NB_toSolveToolStripMenuItem", OurSettings);
solvedDHCPToolStripMenuItem.Text = NB.Translate("_DHCP"); solvedDHCPToolStripMenuItem.Text = NB.Translate("_DHCP", OurSettings);
solvedOneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork"); solvedOneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork", OurSettings);
solvedTwoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks"); solvedTwoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks", OurSettings);
SolvedThreeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks"); SolvedThreeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks", OurSettings);
firewallsToolStripMenuItem1.Text = NB.Translate("NB_Firewalls"); firewallsToolStripMenuItem1.Text = NB.Translate("NB_Firewalls", OurSettings);
lblStatus.Text = NB.Translate("NB_lblStatus"); lblStatus.Text = NB.Translate("NB_lblStatus", OurSettings);
btnHelp.Text = NB.Translate("NB_btnHelp"); btnHelp.Text = NB.Translate("NB_btnHelp", OurSettings);
changeLanguageToolStripMenuItem.Text = NB.Translate("NB_changeLanguageToolStripMenuItem"); changeLanguageToolStripMenuItem.Text = NB.Translate("NB_changeLanguageToolStripMenuItem", OurSettings);
Text = NB.Translate("NB_Form"); Text = NB.Translate("NB_Form", OurSettings);
} }
public ResourceManager GetResource() public ResourceManager GetResource()
@ -401,7 +401,7 @@ namespace EduNetworkBuilder
UpdateVisuals(); UpdateVisuals();
processing = true; processing = true;
cbViewTitles.Checked = myNetwork.ShowLabelsHere; cbViewTitles.Checked = myNetwork.ShowLabelsHere;
Text = NB.Translate("NB_UpdteFrmName"); Text = NB.Translate("NB_UpdteFrmName", OurSettings);
if (myNetwork.NetTitle.GetText() != "") if (myNetwork.NetTitle.GetText() != "")
Text += ": " + myNetwork.NetTitle.GetText(); Text += ": " + myNetwork.NetTitle.GetText();
if (myNetwork.NetMessage.GetText() != "") if (myNetwork.NetMessage.GetText() != "")
@ -1434,7 +1434,7 @@ namespace EduNetworkBuilder
private void BuilderWindow_FormClosing(object sender, FormClosingEventArgs e) private void BuilderWindow_FormClosing(object sender, FormClosingEventArgs e)
{ {
//The values in the settings are all up-to-date. Just tell them to save. //The values in the settings are all up-to-date. Just tell them to save.
OurSettings.Save(); OurSettings.Save(NB.IsRunningOnMono());
} }
private void dHCPRequestToolStripMenuItem_Click(object sender, EventArgs e) private void dHCPRequestToolStripMenuItem_Click(object sender, EventArgs e)