示例#1
0
        /// <summary>
        /// Handles the Click event of the Open control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog()
            {
                Filter = FILE_FILTER
            };

            while (true)
            {
                if (dialog.ShowDialog() == true)
                {
                    try
                    {
                        StopAll();
                        InstructionsTextBox.Document.Blocks.Clear();
                        string script = File.ReadAllText(dialog.FileName);

                        GlobalData = new GlobalData();

                        if (script.Contains(GLOBAL_AND_INSTRUCTIONS_SEPARATOR))
                        {
                            string[] splitted = script.Split(GLOBAL_AND_INSTRUCTIONS_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            try
                            {
                                GlobalData = JsonConvert.DeserializeObject <GlobalData>(splitted[0]);
                            }
                            catch
                            {
                                MessageBox.Show("Error deserializing the global values. Using default values.");
                            }

                            InstructionsTextBox.Document.Blocks.Add(StringManager.ConvertStringToBlock(splitted[1].Trim()));
                        }
                        else
                        {
                            MessageBox.Show("Couldn't distinguish between global values and instructions.");
                            InstructionsTextBox.Document.Blocks.Add(StringManager.ConvertStringToBlock(script));
                        }

                        break;
                    }
                    catch
                    {
                        MessageBox.Show("File has wrong format or is corrupted.");
                    }
                }
                else
                {
                    break;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Handles the Click event of the Save control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog()
            {
                Filter = FILE_FILTER
            };

            if (dialog.ShowDialog() == true)
            {
                try
                {
                    StopAll();
                    string serialized = JsonConvert.SerializeObject(GlobalData);
                    serialized += GLOBAL_AND_INSTRUCTIONS_SEPARATOR;
                    serialized += StringManager.ConvertRichTextBoxToString(InstructionsTextBox);
                    File.WriteAllText(dialog.FileName, serialized);
                }
                catch
                {
                    MessageBox.Show("Couldn't write to file.");
                }
            }
        }