static void LoadConfig(XmlConfig config, out ECKeyPair privateKey) { bool saveFlag = false; config.Define<int> (CONFIG_BIND_PORT, IntParser.Instance, new IntRangeValidator (1, ushort.MaxValue), 8080); config.Define<byte[]> (CONFIG_PRIVATE_KEY, BinaryParser.Instance, null, null); config.Define<string> (CONFIG_HOST, StringParser.Instance, null, string.Empty); try { if (File.Exists (CONFIG_PATH)) config.Load (CONFIG_PATH); } catch { saveFlag = true; } byte[] raw = config.GetValue<byte[]> (CONFIG_PRIVATE_KEY); while (true) { if (raw == null || raw.Length == 0) { privateKey = ECKeyPair.Create (DefaultAlgorithm.ECDomainName); config.SetValue<byte[]> (CONFIG_PRIVATE_KEY, privateKey.PrivateKey, false); saveFlag = true; } else { privateKey = ECKeyPairExtensions.CreatePrivate (raw); if (privateKey.DomainName != DefaultAlgorithm.ECDomainName) { raw = null; continue; } } break; } if (config.GetValue<string> (CONFIG_HOST).Length == 0) { config.SetValue<string> (CONFIG_HOST, "localhost", false); saveFlag = true; } if (saveFlag) config.Save (CONFIG_PATH); }
static void Main(string[] args) { XmlConfig config = new XmlConfig(); #if FIRST_LOAD if (File.Exists("test.xml")) { config.Load("test.xml"); } #endif config.Define <int> ("type/int", IntParser.Instance, new IntRangeValidator(-10, 10), 0); config.Define <string> ("type/string", StringParser.Instance, null, "default string"); config.Define <byte[]> ("type/binary", BinaryParser.Instance, null, new byte[] { 0, 1, 2, 3, 4, 5 }); config.Define <DateTime> ("type/datetime", DateTimeParser.Instance, null, DateTime.Now); config.Define <TimeSpan> ("type/timespan", TimeSpanParser.Instance, null, new TimeSpan(DateTime.Now.Ticks)); config.Define <Guid> ("type/guid", GuidParser.Instance, null, Guid.NewGuid()); config.Define <TestEnum> ("type/enum", new EnumParser <TestEnum> (), null, TestEnum.hoge); config.Define <Rectangle> ("type/rectangle", RectangleParser.Instance, null, Rectangle.Empty); config.Define <bool> ("type/bool", BooleanParser.Instance, null, false); config.Define <string[]> ("type/array/string", new ArrayParser <string> (StringParser.Instance), null, new string[] { "default0", "default1" }); config.Define <TestEnum[]> ("type/array/enum", new ArrayParser <TestEnum> (new EnumParser <TestEnum> (), "i"), null, new TestEnum[] { TestEnum.foo, TestEnum.bar }); #if !FIRST_LOAD if (File.Exists("test.xml")) { config.Load("test.xml"); } #endif #if false config.SetValue <int> ("type/int", 1, false); config.SetValue <string> ("type/string", "hoge", false); byte[] temp = new byte[32]; new Random().NextBytes(temp); config.SetValue <byte[]> ("type/binary", temp, false); config.SetValue <DateTime> ("type/datetime", DateTime.Now, false); config.SetValue <TimeSpan> ("type/timespan", new TimeSpan(DateTime.Now.Ticks), false); config.SetValue <Guid> ("type/guid", Guid.NewGuid(), false); config.SetValue <TestEnum> ("type/enum", TestEnum.foo, false); config.SetValue <Rectangle> ("type/rectangle", new Rectangle(10, 20, 30, 40), false); config.SetValue <bool> ("type/bool", true, false); config.SetValue <string[]> ("type/array/string", new string[] { "item0", "item1", "item2", "item3" }, false); config.SetValue <TestEnum[]> ("type/array/enum", new TestEnum[] { TestEnum.hoge, TestEnum.piyo }, false); #endif config.Save("test2.xml"); }
public static bool LoadConfig(XmlConfig config) { bool exists = false; config.Define<int> (ConfigFields.NetBindUdp, IntParser.Instance, new IntRangeValidator (1, ushort.MaxValue), new Random ().Next (49152, ushort.MaxValue)); config.Define<int> (ConfigFields.NetBindTcp, IntParser.Instance, new IntRangeValidator (1, ushort.MaxValue), config.GetValue<int> (ConfigFields.NetBindUdp)); config.Define<int> (ConfigFields.GwBindTcp, IntParser.Instance, new IntRangeValidator (1, ushort.MaxValue), 8080); config.Define<bool> (ConfigFields.GwBindAny, BooleanParser.Instance, null, false); try { exists = File.Exists (CONFIG_PATH); if (exists) config.Load (CONFIG_PATH); } catch {} config.Save (CONFIG_PATH); return exists; }