Example #1
0
        public static void saveConfig(Expandable exa, Expandable.IConfig config, Hotkey[] hks)
        {
            if (!exa.isConfigChanged)
            {
                return;
            }
            if (!Directory.Exists(CONFIG_PATH))
            {
                Directory.CreateDirectory(CONFIG_PATH);
            }

            string module = exa.dllFileName;
            string file   = module == CORE_ID ? CORE_CONFIG_PATH : CONFIG_PATH + module + ".ini";

            List <Expandable.Property> property    = convertConfig(config);
            List <HotkeyInfo>          hotkeyInfos = getHotkeyInfo(hks);
            Config targetConfig = new Config(property, hotkeyInfos);

            using (Stream str = File.Create(file))
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(Config));
                    xs.Serialize(str, targetConfig);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            exa.isConfigChanged = false;
        }
Example #2
0
        private void saveSettings()
        {
            string lang = Core.setting.language;

            foreach (TabPage page in tabControl1.TabPages)
            {
                if (page.Tag == null)
                {
                    continue;
                }
                object[] tags = (object[])page.Tag;

                Expandable exa = (Expandable)tags[0];
                exa.isConfigChanged = true;

                Control[]          controls = (Control[])tags[1];
                Expandable.IConfig config   = (Expandable.IConfig)tags[2];

                saveSetting(controls, config);
            }

            if (Core.setting.language == lang)
            {
                return;
            }
            Core core = Core.getInstance();

            if (core == null)
            {
                return;
            }
            MessageBox.Show(core.getString("str_change_lang_msg"));
        }
Example #3
0
        private void InitPages()
        {
            foreach (Expandable exa in Core.loadedModules)
            {
                Expandable.IConfig config = exa.config;
                if (config == null)
                {
                    continue;
                }

                string module = exa.dllFileName;
                string text   = module == Core.CORE_ID ? "General" : exa.getTitle();
                Dictionary <string, string> strRes = exa.strRes;

                TabPage page = new TabPage
                {
                    Text       = text,
                    AutoScroll = true,
                    Location   = new Point(4, 22),
                    Padding    = new Padding(3),
                    Size       = new Size(290, 294),
                    TabIndex   = tabControl1.TabCount,
                    UseVisualStyleBackColor = true,
                    Name = module
                };

                Type           type     = config.GetType();
                FieldInfo[]    fields   = type.GetFields();
                List <Control> controls = new List <Control>();
                int            index    = 0;
                foreach (FieldInfo fi in fields)
                {
                    dynamic value   = Convert.ChangeType(fi.GetValue(config), fi.FieldType);
                    Control control = createControl(ref index, page, fi, exa, value);
                    if (control == null)
                    {
                        continue;
                    }
                    controls.Add(control);
                }

                page.Tag = new object[] { exa, controls.ToArray(), config };
                tabControl1.Controls.Add(page);
            }
        }
Example #4
0
        private static List <Expandable.Property> convertConfig(Expandable.IConfig config)
        {
            if (config == null)
            {
                return(null);
            }

            List <Expandable.Property> fieldList = new List <Expandable.Property>();
            Type type = config.GetType();

            FieldInfo[] fields = type.GetFields();
            foreach (FieldInfo fi in fields)
            {
                object value = fi.GetValue(config);
                //Console.WriteLine(fi.Name + ", as " + fi.FieldType);
                Expandable.Property property = new Expandable.Property(fi.Name, value);
                fieldList.Add(property);
            }
            return(fieldList);
        }
Example #5
0
        private void saveSetting(Control[] controls, Expandable.IConfig config)
        {
            foreach (Control control in controls)
            {
                FieldInfo field = (FieldInfo)control.Tag;
                Type      type  = field.FieldType;
                object    value = null;

                if (type.Equals(typeof(string)))
                {
                    value = control.Text;
                }
                else if (type.Equals(typeof(bool)))
                {
                    value = ((CheckBox)control).Checked;
                }
                else if (NumericTypes.Contains(type))
                {
                    value = ((NumericUpDown)control).Value;
                    value = Convert.ChangeType(value, type);

                    /*
                     * if (type.Equals(typeof(double)))
                     * {
                     *  value = Convert.ToDouble(value);
                     * }
                     * else if (type.Equals(typeof(float)))
                     * {
                     *  value = Convert.ToSingle(value);
                     * }
                     */
                }

                if (value == null)
                {
                    return;
                }
                field.SetValue(config, value);
            }
        }
Example #6
0
        public static bool loadConfig(string module, Expandable.IConfig config, Hotkey[] hks)
        {
            string file = module == CORE_ID ? CORE_CONFIG_PATH : CONFIG_PATH + module + ".ini";

            if (!File.Exists(file))
            {
                return(false);
            }

            Config tmp = new Config();

            using (Stream str = File.OpenRead(file))
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(Config));
                    tmp = (Config)xs.Deserialize(str);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

            if (tmp.Properties.Count == 0 && tmp.Hotkeys.Count == 0)
            {
                return(false);
            }

            if (tmp.Hotkeys.Count > 0)
            {
                for (int i = 0; i < hks.Length; i++)
                {
                    HotkeyInfo hki = tmp.Hotkeys.FirstOrDefault(_ => _.id == hks[i].id);
                    if (hki.id != null)
                    {
                        hks[i].keys = hki.keys;
                    }
                }
            }

            if (tmp.Properties.Count > 0)
            {
                Type        confType = config.GetType();
                FieldInfo[] fields   = confType.GetFields();
                foreach (FieldInfo fi in fields)
                {
                    object value = tmp.Properties.FirstOrDefault(_ => _.name == fi.Name).value;
                    if (value == null)
                    {
                        continue;
                    }

                    if (value.GetType().AssemblyQualifiedName == fi.FieldType.AssemblyQualifiedName)
                    {
                        fi.SetValue(config, value);
                    }
                }
            }

            return(true);
        }