/// <summary> /// Initializes the fixes and applies them. /// </summary> /// <param name="parent">The parent plugin.</param> /// <param name="configKeyword">The configuration file keyword.</param> /// <exception cref="System.ArgumentException">Warning: parent.Name fix t.Name does not have valid parameterless constructor!</exception> internal static void init(NetScriptFramework.Plugin parent, string configKeyword) { if (System.Threading.Interlocked.Exchange(ref is_init, 1) != 0) { throw new InvalidOperationException(); } var asm = System.Reflection.Assembly.GetExecutingAssembly(); var types = asm.GetTypes(); foreach (var t in types) { if (t.IsAbstract || !t.IsSubclassOf(typeof(Fix))) { continue; } var cis = t.GetConstructors().Where(q => q.GetParameters().Length == 0).ToList(); if (cis.Count != 1) { throw new ArgumentException("Warning: " + parent.Name + " fix " + t.Name + " does not have valid parameterless constructor!"); } Fix f = (Fix)cis[0].Invoke(new object[0]); fixes.Add(f); } var config = new NetScriptFramework.Tools.ConfigFile(configKeyword); foreach (var f in fixes) { config.AddSetting(f.Key + ".Enabled", new NetScriptFramework.Tools.Value(f.Enabled), null, f.Description); foreach (var pair in f.IntParameters) { config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value)); } foreach (var pair in f.FloatParameters) { config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value)); } foreach (var pair in f.StringParameters) { config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value)); } foreach (var pair in f.BoolParameters) { config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value)); } } if (!config.Load()) { config.Save(); } foreach (var f in fixes) { var v = config.GetValue(f.Key + ".Enabled"); bool ub; if (v != null && v.TryToBoolean(out ub)) { f.Enabled = ub; } { var map = f.IntParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); long uv; if (v != null && v.TryToInt64(out uv)) { map[pair.Key] = uv; } } } { var map = f.FloatParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); double uv; if (v != null && v.TryToDouble(out uv)) { map[pair.Key] = uv; } } } { var map = f.BoolParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); bool uv; if (v != null && v.TryToBoolean(out uv)) { map[pair.Key] = uv; } } } { var map = f.StringParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); if (v != null) { map[pair.Key] = v.ToString(); } } } } fixes.Sort((u, v) => u.SortValue.CompareTo(v.SortValue)); foreach (var f in fixes) { if (f.Enabled) { f.Apply(); } } }
/// <summary> /// Initializes the fixes and applies them. /// </summary> /// <param name="parent">The parent plugin.</param> /// <param name="configKeyword">The configuration file keyword.</param> /// <exception cref="System.ArgumentException">Warning: parent.Name fix t.Name does not have valid parameterless constructor!</exception> internal static void init(NetScriptFramework.Plugin parent, string configKeyword) { if (System.Threading.Interlocked.Exchange(ref is_init, 1) != 0) { throw new InvalidOperationException(); } var asm = System.Reflection.Assembly.GetExecutingAssembly(); var types = asm.GetTypes(); foreach (var t in types) { if (t.IsAbstract || !t.IsSubclassOf(typeof(Mod))) { continue; } var cis = t.GetConstructors().Where(q => q.GetParameters().Length == 0).ToList(); if (cis.Count != 1) { throw new ArgumentException("Warning: " + parent.Name + " mod " + t.Name + " does not have valid parameterless constructor!"); } Mod f = (Mod)cis[0].Invoke(new object[0]); mods.Add(f); } var config = new NetScriptFramework.Tools.ConfigFile(configKeyword); foreach (var f in mods) { config.AddSetting(f.Key + ".Enabled", new NetScriptFramework.Tools.Value(f.Enabled), null, f.Description); foreach (var okey in f.OrderOfParams) { long val_i; double val_f; string val_s; bool val_b; if (f.IntParameters.TryGetValue(okey, out val_i)) { string cmt = null; f.ParameterComments.TryGetValue(okey, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + okey, new NetScriptFramework.Tools.Value(val_i), null, cmt, fl); } if (f.FloatParameters.TryGetValue(okey, out val_f)) { string cmt = null; f.ParameterComments.TryGetValue(okey, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + okey, new NetScriptFramework.Tools.Value(val_f), null, cmt, fl); } if (f.StringParameters.TryGetValue(okey, out val_s)) { string cmt = null; f.ParameterComments.TryGetValue(okey, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + okey, new NetScriptFramework.Tools.Value(val_s), null, cmt, fl); } if (f.BoolParameters.TryGetValue(okey, out val_b)) { string cmt = null; f.ParameterComments.TryGetValue(okey, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + okey, new NetScriptFramework.Tools.Value(val_b), null, cmt, fl); } } foreach (var pair in f.IntParameters) { if (f.OrderOfParams.Contains(pair.Key)) { continue; } string cmt = null; f.ParameterComments.TryGetValue(pair.Key, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value), null, cmt, fl); } foreach (var pair in f.FloatParameters) { if (f.OrderOfParams.Contains(pair.Key)) { continue; } string cmt = null; f.ParameterComments.TryGetValue(pair.Key, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value), null, cmt, fl); } foreach (var pair in f.StringParameters) { if (f.OrderOfParams.Contains(pair.Key)) { continue; } string cmt = null; f.ParameterComments.TryGetValue(pair.Key, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value), null, cmt, fl); } foreach (var pair in f.BoolParameters) { if (f.OrderOfParams.Contains(pair.Key)) { continue; } string cmt = null; f.ParameterComments.TryGetValue(pair.Key, out cmt); NetScriptFramework.Tools.ConfigEntryFlags fl = !string.IsNullOrEmpty(cmt) ? NetScriptFramework.Tools.ConfigEntryFlags.VeryShortComment : NetScriptFramework.Tools.ConfigEntryFlags.NoNewLineBefore; config.AddSetting(f.Key + "." + pair.Key, new NetScriptFramework.Tools.Value(pair.Value), null, cmt, fl); } } if (!config.Load()) { config.Save(); } foreach (var f in mods) { var v = config.GetValue(f.Key + ".Enabled"); bool ub; if (v != null && v.TryToBoolean(out ub)) { f.Enabled = ub; } { var map = f.IntParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); long uv; if (v != null && v.TryToInt64(out uv)) { map[pair.Key] = uv; } } } { var map = f.FloatParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); double uv; if (v != null && v.TryToDouble(out uv)) { map[pair.Key] = uv; } } } { var map = f.BoolParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); bool uv; if (v != null && v.TryToBoolean(out uv)) { map[pair.Key] = uv; } } } { var map = f.StringParameters; var ls = map.ToList(); foreach (var pair in ls) { string k = f.Key + "." + pair.Key; v = config.GetValue(k); if (v != null) { map[pair.Key] = v.ToString(); } } } } mods.Sort((u, v) => u.SortValue.CompareTo(v.SortValue)); foreach (var f in mods) { if (f.Enabled) { f.Apply(); } } }