XmlManager Update
parent
1746bc4e65
commit
72b0273644
|
@ -16,102 +16,15 @@ namespace Galactic_Colors_Control_Console
|
|||
public ConsoleColor[] logBackColor = new ConsoleColor[6] { ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Red };
|
||||
public int lang = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Load config from xml file
|
||||
/// App.config is too easy
|
||||
/// </summary>
|
||||
/// <returns>Loaded config</returns>
|
||||
public Config Load()
|
||||
public void PreSave()
|
||||
{
|
||||
Console.logger.Write("Loading config", Logger.logType.info);
|
||||
Config config = new Config();
|
||||
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
if (CorrectConfig())
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
using (StreamReader re = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
config = xs.Deserialize(re) as Config;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.logger.Write("Old config in Config.xml.old", Logger.logType.warm);
|
||||
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
File.Move(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
config.Save();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.logger.Write("Any config file", Logger.logType.error);
|
||||
config.Save();
|
||||
}
|
||||
if (Console._debug) { config.logLevel = Logger.logType.debug; }
|
||||
if (Console._dev) { config.logLevel = Logger.logType.dev; }
|
||||
return config;
|
||||
if (Console._debug || Console._dev) { logLevel = Logger.logType.info; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write actual config in xml file
|
||||
/// </summary>
|
||||
public void Save()
|
||||
public void PostSave()
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
if (Console._debug || Console._dev) { logLevel = Logger.logType.info; }
|
||||
using (StreamWriter st = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
xs.Serialize(st, this);
|
||||
};
|
||||
if (Console._debug) { logLevel = Logger.logType.debug; }
|
||||
if (Console._dev) { logLevel = Logger.logType.dev; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check config format using Schema
|
||||
/// </summary>
|
||||
public bool CorrectConfig()
|
||||
{
|
||||
bool isCorrect = false;
|
||||
|
||||
using (Stream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", FileMode.Open))
|
||||
{
|
||||
XmlReader re = new XmlTextReader(fs);
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
try
|
||||
{
|
||||
isCorrect = xs.CanDeserialize(re);
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Console.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
if (isCorrect)
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlDocument d = new XmlDocument();
|
||||
d.Load(AppDomain.CurrentDomain.BaseDirectory + "Config.xml");
|
||||
d.Schemas.Add("", XmlReader.Create("ConfigSchema.xsd"));
|
||||
|
||||
d.Validate((o, e) =>
|
||||
{
|
||||
Console.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
isCorrect = false;
|
||||
});
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Console.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
return isCorrect;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,8 +2,10 @@
|
|||
using Galactic_Colors_Control_Common;
|
||||
using Galactic_Colors_Control_Common.Protocol;
|
||||
using MyCommon;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
using Consol = MyCommon.ConsoleIO;
|
||||
|
||||
namespace Galactic_Colors_Control_Console
|
||||
|
@ -28,7 +30,8 @@ namespace Galactic_Colors_Control_Console
|
|||
System.Console.Write(">");
|
||||
logger.Write(System.Console.Title, Logger.logType.fatal);
|
||||
logger.Write("Console " + Assembly.GetEntryAssembly().GetName().Version.ToString(), Logger.logType.error);
|
||||
config = config.Load();
|
||||
config = XmlManager.Load<Config>(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", XmlManager.LoadMode.ReadCreateOrReplace, XmlReader.Create("ConfigSchema.xsd"), logger);
|
||||
config.PostSave();
|
||||
logger.Initialise(config.logPath, config.logBackColor, config.logForeColor, config.logLevel, _debug, _dev);
|
||||
multilang.Initialise(Common.dictionary);
|
||||
client.OnEvent += new System.EventHandler(OnEvent); //Set OnEvent function
|
||||
|
@ -172,6 +175,9 @@ namespace Galactic_Colors_Control_Console
|
|||
Consol.Write(new ColorStrings(new ColorString(multilang.GetWord("CantConnect", config.lang), System.ConsoleColor.Red)));
|
||||
}
|
||||
run = false;
|
||||
config.PreSave();
|
||||
XmlManager.Save(config, AppDomain.CurrentDomain.BaseDirectory + "Config.xml", logger);
|
||||
config.PostSave();
|
||||
logger.Join();
|
||||
System.Console.Read();
|
||||
}
|
||||
|
|
|
@ -17,102 +17,15 @@ namespace Galactic_Colors_Control_GUI
|
|||
public int lang = 1;
|
||||
public string skin = "default";
|
||||
|
||||
/// <summary>
|
||||
/// Load config from xml file
|
||||
/// App.config is too easy
|
||||
/// </summary>
|
||||
/// <returns>Loaded config</returns>
|
||||
public Config Load()
|
||||
public void PreSave()
|
||||
{
|
||||
Game.singleton.logger.Write("Loading config", Logger.logType.info);
|
||||
Config config = new Config();
|
||||
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
if (CorrectConfig())
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
using (StreamReader re = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
config = xs.Deserialize(re) as Config;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.singleton.logger.Write("Old config in Config.xml.old", Logger.logType.warm);
|
||||
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
File.Move(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
config.Save();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.singleton.logger.Write("Any config file", Logger.logType.error);
|
||||
config.Save();
|
||||
}
|
||||
if (Program._debug) { config.logLevel = Logger.logType.debug; }
|
||||
if (Program._dev) { config.logLevel = Logger.logType.dev; }
|
||||
return config;
|
||||
if (Program._debug || Program._dev) { logLevel = Logger.logType.info; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write actual config in xml file
|
||||
/// </summary>
|
||||
public void Save()
|
||||
public void PostSave()
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
if (Program._debug || Program._dev) { logLevel = Logger.logType.info; }
|
||||
using (StreamWriter st = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
xs.Serialize(st, this);
|
||||
};
|
||||
if (Program._debug) { logLevel = Logger.logType.debug; }
|
||||
if (Program._dev) { logLevel = Logger.logType.dev; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check config format using Schema
|
||||
/// </summary>
|
||||
public bool CorrectConfig()
|
||||
{
|
||||
bool isCorrect = false;
|
||||
|
||||
using (Stream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", FileMode.Open))
|
||||
{
|
||||
XmlReader re = new XmlTextReader(fs);
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
try
|
||||
{
|
||||
isCorrect = xs.CanDeserialize(re);
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Game.singleton.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
if (isCorrect)
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlDocument d = new XmlDocument();
|
||||
d.Load(AppDomain.CurrentDomain.BaseDirectory + "Config.xml");
|
||||
d.Schemas.Add("", XmlReader.Create("ConfigSchema.xsd"));
|
||||
|
||||
d.Validate((o, e) =>
|
||||
{
|
||||
Game.singleton.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
isCorrect = false;
|
||||
});
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Game.singleton.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
return isCorrect;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ using MyMonoGame.GUI;
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Xml;
|
||||
|
||||
namespace Galactic_Colors_Control_GUI
|
||||
{
|
||||
|
@ -66,7 +67,8 @@ namespace Galactic_Colors_Control_GUI
|
|||
/// </summary>
|
||||
protected override void Initialize()
|
||||
{
|
||||
config = config.Load();
|
||||
config = XmlManager.Load<Config>(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", XmlManager.LoadMode.ReadCreateOrReplace, XmlReader.Create("ConfigSchema.xsd"), logger);
|
||||
config.PostSave();
|
||||
logger.Write("Galactic Colors Control GUI " + Assembly.GetEntryAssembly().GetName().Version.ToString(), Logger.logType.fatal);
|
||||
logger.Initialise(config.logPath, config.logBackColor, config.logForeColor, config.logLevel, Program._debug, Program._dev, false);
|
||||
multilang.Initialise(Common.dictionary);
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MyMonoGame.GUI;
|
||||
using System.Threading;
|
||||
using MyCommon;
|
||||
using System;
|
||||
|
||||
namespace Galactic_Colors_Control_GUI.States
|
||||
{
|
||||
|
@ -24,7 +26,9 @@ namespace Galactic_Colors_Control_GUI.States
|
|||
{
|
||||
locked = true;
|
||||
Game.singleton.GUI.ResetFocus();
|
||||
Game.singleton.config.Save();
|
||||
Game.singleton.config.PreSave();
|
||||
XmlManager.Save(Game.singleton.config, AppDomain.CurrentDomain.BaseDirectory + "Config.xml", Game.singleton.logger);
|
||||
Game.singleton.config.PostSave();
|
||||
new Thread(() =>
|
||||
{
|
||||
while (!Utilities.DoubleTo(ref Game.singleton.background.speedX, 1, 0.1)) { Thread.Sleep(20); }
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
using MyCommon;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Galactic_Colors_Control_Server
|
||||
|
@ -18,102 +16,15 @@ namespace Galactic_Colors_Control_Server
|
|||
public int lang = 1;
|
||||
public int partysize = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Load config from xml file
|
||||
/// App.config is too easy
|
||||
/// </summary>
|
||||
/// <returns>Loaded config</returns>
|
||||
public Config Load()
|
||||
public void PreSave()
|
||||
{
|
||||
Server.logger.Write("Loading config", Logger.logType.info);
|
||||
Config config = new Config();
|
||||
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
if (CorrectConfig())
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
using (StreamReader re = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
config = xs.Deserialize(re) as Config;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
Server.logger.Write("Old config in Config.xml.old", Logger.logType.warm);
|
||||
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
File.Move(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old");
|
||||
config.Save();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Server.logger.Write("Any config file", Logger.logType.error);
|
||||
config.Save();
|
||||
}
|
||||
if (Server._debug) { config.logLevel = Logger.logType.debug; }
|
||||
if (Server._dev) { config.logLevel = Logger.logType.dev; }
|
||||
return config;
|
||||
if (Server._debug || Server._dev) { logLevel = Logger.logType.info; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write actual config in xml file
|
||||
/// </summary>
|
||||
public void Save()
|
||||
public void PostSave()
|
||||
{
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
if (Server._debug || Server._dev) { logLevel = Logger.logType.info; }
|
||||
using (StreamWriter st = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
|
||||
{
|
||||
xs.Serialize(st, this);
|
||||
};
|
||||
if (Server._debug) { logLevel = Logger.logType.debug; }
|
||||
if (Server._dev) { logLevel = Logger.logType.dev; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check config format using Schema
|
||||
/// </summary>
|
||||
public bool CorrectConfig()
|
||||
{
|
||||
bool isCorrect = false;
|
||||
|
||||
using (Stream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", FileMode.Open))
|
||||
{
|
||||
XmlReader re = new XmlTextReader(fs);
|
||||
XmlSerializer xs = new XmlSerializer(typeof(Config));
|
||||
try
|
||||
{
|
||||
isCorrect = xs.CanDeserialize(re);
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Server.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
if (isCorrect)
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlDocument d = new XmlDocument();
|
||||
d.Load(AppDomain.CurrentDomain.BaseDirectory + "Config.xml");
|
||||
d.Schemas.Add("", XmlReader.Create("ConfigSchema.xsd"));
|
||||
|
||||
d.Validate((o, e) =>
|
||||
{
|
||||
Server.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
isCorrect = false;
|
||||
});
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
isCorrect = false;
|
||||
Server.logger.Write("Error: " + e.Message, Logger.logType.error);
|
||||
}
|
||||
}
|
||||
|
||||
return isCorrect;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ using System.Net;
|
|||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
using Console = MyCommon.ConsoleIO;
|
||||
|
||||
//TODO gui parties pages
|
||||
|
@ -49,7 +50,8 @@ namespace Galactic_Colors_Control_Server
|
|||
{
|
||||
Console.Title = "Galactic Colors Control Server";
|
||||
logger.Write(Console.Title + " " + Assembly.GetEntryAssembly().GetName().Version.ToString(), Logger.logType.fatal);
|
||||
config = config.Load();
|
||||
config = XmlManager.Load<Config>(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", XmlManager.LoadMode.ReadCreateOrReplace, XmlReader.Create("ConfigSchema.xsd"), logger);
|
||||
config.PostSave();
|
||||
logger.Initialise(config.logPath, config.logBackColor, config.logForeColor, config.logLevel, _debug, _dev);
|
||||
multilang.Initialise(Common.dictionary);
|
||||
if (args.Length > 0)
|
||||
|
@ -114,7 +116,9 @@ namespace Galactic_Colors_Control_Server
|
|||
{
|
||||
logger.Write("Stoping server", Logger.logType.warm, Logger.logConsole.show);
|
||||
Utilities.Broadcast(new EventData(EventTypes.ServerKick, Strings.ArrayFromStrings("Close")));
|
||||
config.Save();
|
||||
config.PreSave();
|
||||
XmlManager.Save(config, AppDomain.CurrentDomain.BaseDirectory + "Config.xml", logger);
|
||||
config.PostSave();
|
||||
foreach (Socket socket in clients.Keys)
|
||||
{
|
||||
socket.Shutdown(SocketShutdown.Both);
|
||||
|
|
Reference in New Issue