예제 #1
0
        public override void SetProperties(UserControl[] _controls)
        {
            MidiDeviceName = (string)(_controls.Where(x => (string)x.Tag == "DeviceSelector").First().Content as ComboBox).SelectedValue;
            var ctrl = _controls.OfType <MidiMappingControl>().First();

            Midis.Clear();
            foreach (var item in ctrl.Midis)
            {
                Midis.Add(item);
            }
            base.SetProperties(_controls);
        }
예제 #2
0
        /**<summary>Loads the settings from the config file.</summary>*/
        public static bool Load()
        {
            try {
                XmlNode      node;
                XmlElement   element;
                XmlAttribute attribute;
                XmlDocument  doc             = new XmlDocument();
                bool         deleteOldConfig = false;
                if (File.Exists(ConfigPath))
                {
                    doc.Load(ConfigPath);
                }
                else
                {
                    doc.Load(OldConfigPath);
                    if (doc.SelectSingleNode("TerrariaMidiPlayer") != null)
                    {
                        deleteOldConfig = true;
                    }
                }

                int     intValue;
                ushort  ushortValue;
                bool    boolValue;
                double  doubleValue;
                Keybind keybindValue;

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Version");
                if (node == null || !int.TryParse(node.InnerText, out intValue) || intValue > ConfigVersion || intValue <= 0)
                {
                    return(false);
                }

                #region Settings

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/ExecutableName");
                if (node != null)
                {
                    ExecutableNames = node.InnerText;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/UseTime");
                if (node != null && int.TryParse(node.InnerText, out intValue))
                {
                    UseTime = intValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/ClickTime");
                if (node != null && int.TryParse(node.InnerText, out intValue))
                {
                    ClickTime = intValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/ChecksEnabled");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    ChecksEnabled = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/CheckFrequency");
                if (node != null && int.TryParse(node.InnerText, out intValue))
                {
                    CheckFrequency = intValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Mount");
                if (node != null)
                {
                    for (int i = 0; i < Mount.Mounts.Length; i++)
                    {
                        if (string.Compare(node.InnerText, Mount.Mounts[i].Name, true) == 0)
                        {
                            MountIndex = i;
                            break;
                        }
                    }
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/ProjectileAngle");
                if (node != null && double.TryParse(node.InnerText, out doubleValue))
                {
                    ProjectileAngle = doubleValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/ProjectileRange");
                if (node != null && double.TryParse(node.InnerText, out doubleValue))
                {
                    ProjectileRange = doubleValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/CloseNoFocus");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    CloseNoFocus = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/PlaybackNoFocus");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    PlaybackNoFocus = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/DisableMountWhenTalking");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    DisableMountWhenTalking = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/UseTrackNames");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    UseTrackNames = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/WrapPianoMode");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    WrapPianoMode = boolValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/SkipPianoMode");
                if (node != null && bool.TryParse(node.InnerText, out boolValue))
                {
                    SkipPianoMode = boolValue;
                }

                #endregion
                //--------------------------------
                #region Keybinds

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Keybinds/Play");
                if (node != null && Keybind.TryParse(node.InnerText, out keybindValue))
                {
                    Keybinds.Play = keybindValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Keybinds/Pause");
                if (node != null && Keybind.TryParse(node.InnerText, out keybindValue))
                {
                    Keybinds.Pause = keybindValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Keybinds/Stop");
                if (node != null && Keybind.TryParse(node.InnerText, out keybindValue))
                {
                    Keybinds.Stop = keybindValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Keybinds/Close");
                if (node != null && Keybind.TryParse(node.InnerText, out keybindValue))
                {
                    Keybinds.Close = keybindValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Keybinds/Mount");
                if (node != null && Keybind.TryParse(node.InnerText, out keybindValue))
                {
                    Keybinds.Mount = keybindValue;
                }

                #endregion
                //--------------------------------
                #region Syncing

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/SyncType");
                if (node != null)
                {
                    Syncing.SyncType = ((string.Compare(node.InnerText, "Host", true) == 0) ? SyncTypes.Host : SyncTypes.Client);
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/ClientIPAddress");
                if (node != null)
                {
                    Syncing.ClientIPAddress = node.InnerText;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/ClientPort");
                if (node != null && ushort.TryParse(node.InnerText, out ushortValue))
                {
                    Syncing.ClientPort = ushortValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/ClientUsername");
                if (node != null)
                {
                    Syncing.ClientUsername = node.InnerText;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/ClientPassword");
                if (node != null)
                {
                    Syncing.ClientPassword = node.InnerText;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/ClientTimeOffset");
                if (node != null && int.TryParse(node.InnerText, out intValue))
                {
                    Syncing.ClientTimeOffset = intValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/HostPort");
                if (node != null && ushort.TryParse(node.InnerText, out ushortValue))
                {
                    Syncing.HostPort = ushortValue;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/HostPassword");
                if (node != null)
                {
                    Syncing.HostPassword = node.InnerText;
                }

                node = doc.SelectSingleNode("TerrariaMidiPlayer/Settings/Syncing/HostWait");
                if (node != null && int.TryParse(node.InnerText, out intValue))
                {
                    Syncing.HostWait = intValue;
                }

                #endregion
                //--------------------------------
                #region Midis

                XmlNodeList midiList = doc.SelectNodes("TerrariaMidiPlayer/Midis/Midi");
                for (int i = 0; i < midiList.Count; i++)
                {
                    node = midiList[i];
                    Midi midi = new Midi();

                    element = node["FilePath"];
                    if (element != null)
                    {
                        if (midi.Load(element.InnerText))
                        {
                            element = node["Name"];
                            if (element != null)
                            {
                                midi.Name = element.InnerText;
                            }

                            element = node["NoteOffset"];
                            if (element != null && int.TryParse(element.InnerText, out intValue))
                            {
                                midi.NoteOffset = Math.Max(-11, Math.Min(11, intValue));
                            }

                            element = node["Speed"];
                            if (element != null && int.TryParse(element.InnerText, out intValue))
                            {
                                midi.Speed = intValue;
                            }

                            element = node["Keybind"];
                            if (element != null && Keybind.TryParse(element.InnerText, out keybindValue))
                            {
                                midi.Keybind = keybindValue;
                            }

                            if (node.Attributes["Selected"] != null &&
                                bool.TryParse(midiList[i].Attributes["Selected"].Value, out boolValue) && boolValue)
                            {
                                MidiIndex = i;
                            }

                            element = node["Tracks"];
                            if (element != null)
                            {
                                XmlNodeList trackList = element.SelectNodes("Track");
                                for (int j = 0; j < trackList.Count && trackList.Count == midi.TrackCount; j++)
                                {
                                    node = trackList[j];

                                    attribute = node.Attributes["Name"];
                                    if (attribute != null)
                                    {
                                        midi.GetTrackSettingsAt(j).Name = attribute.Value;
                                    }

                                    attribute = node.Attributes["Enabled"];
                                    if (attribute != null && bool.TryParse(attribute.Value, out boolValue))
                                    {
                                        midi.GetTrackSettingsAt(j).Enabled = boolValue;
                                    }

                                    attribute = node.Attributes["OctaveOffset"];
                                    if (attribute != null && int.TryParse(attribute.Value, out intValue))
                                    {
                                        midi.GetTrackSettingsAt(j).OctaveOffset = Math.Max(-1, Math.Min(8, intValue));
                                    }
                                }
                            }

                            Midis.Add(midi);
                        }
                        else
                        {
                            // Error
                        }
                    }
                }
                if (Midis.Count > 0 && MidiIndex == -1)
                {
                    MidiIndex = 0;
                }

                #endregion

                if (deleteOldConfig && Save())
                {
                    try {
                        File.Delete(OldConfigPath);
                    }
                    catch { }
                }
            }
            catch (Exception ex) {
                LastException = ex;
                return(false);
            }
            return(true);
        }