public static void ToFile(string filename, WorkingFolder cfg) { XmlSerializer ser = new XmlSerializer(typeof(WorkingFolder)); WorkingFolder config = (WorkingFolder)cfg.Clone(); if (!string.IsNullOrEmpty(config.PreferedToCompile.Path)) { config.PreferedToCompile.Path = Formatter.MakeRelative(config.PreferedToCompile.Path, config.Path + PlatformSpecific.NameSeparator); } if (!string.IsNullOrEmpty(config.CompileConfigPath)) { config.CompileConfigPath = Formatter.MakeRelative(config.CompileConfigPath, config.Path + PlatformSpecific.NameSeparator); } if (!string.IsNullOrEmpty(config.UserConfigPath)) { config.UserConfigPath = Formatter.MakeRelative(config.UserConfigPath, config.Path + PlatformSpecific.NameSeparator); } FileStream fs = new FileStream(filename, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8) { Formatting = Formatting.Indented, Indentation = 4 }; ser.Serialize(writer, config); fs.Close(); }
public static WorkingFolder FromFile(string filename, string directory) { XmlSerializer ser = new XmlSerializer(typeof(WorkingFolder)); FileStream fs = new FileStream(filename, FileMode.Open); XmlReader reader = XmlReader.Create(fs); WorkingFolder cfg = (WorkingFolder)ser.Deserialize(reader); cfg.Path = new DirectoryInfo(directory).Parent.Parent.FullName; cfg.PreferedToCompile.Path = PlatformSpecific.CombinePath(cfg.Path, cfg.PreferedToCompile.Path); cfg.CompileConfigPath = PlatformSpecific.CombinePath(cfg.Path, cfg.CompileConfigPath); cfg.UserConfigPath = PlatformSpecific.CombinePath(cfg.Path, cfg.UserConfigPath); fs.Close(); return(cfg); }
private void Form1_Load(object sender, EventArgs e) { Self = this; tabControl1_SizeChanged(null, null); string configName = ""; if (File.Exists("directory.txt")) { configName = File.ReadAllText("directory.txt"); } if (!Directory.Exists(configName)) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { configName = PlatformSpecific.CombinePath(folderBrowserDialog1.SelectedPath, WorkingFolder.MainConfigPostfix); string compileConfigFileName = PlatformSpecific.CombinePath(folderBrowserDialog1.SelectedPath, WorkingFolder.CompileConfigPostfix); string userConfigFileName = PlatformSpecific.CombinePath(folderBrowserDialog1.SelectedPath, WorkingFolder.UserConfigPostfix); Directory.CreateDirectory(PlatformSpecific.CombinePath(folderBrowserDialog1.SelectedPath, WorkingFolder.IdeDirPostfix)); workingFolder = new WorkingFolder() { CompileConfigPath = compileConfigFileName, UserConfigPath = userConfigFileName, Path = folderBrowserDialog1.SelectedPath, PreferedToCompile = new SourceFile("", "") }; WorkingFolder.ToFile(configName, workingFolder); File.WriteAllText("directory.txt", folderBrowserDialog1.SelectedPath); } else { Close(); return; } } else { configName = PlatformSpecific.CombinePath(configName, WorkingFolder.MainConfigPostfix); if (File.Exists(configName)) { workingFolder = WorkingFolder.FromFile(configName, configName); } else { workingFolder = new WorkingFolder() { CompileConfigPath = PlatformSpecific.CombinePath(new FileInfo(configName).DirectoryName, WorkingFolder.CompileConfigPostfix), UserConfigPath = PlatformSpecific.CombinePath(new FileInfo(configName).DirectoryName, WorkingFolder.UserConfigPostfix), Path = new FileInfo(configName).Directory.Parent.FullName, }; WorkingFolder.ToFile(configName, workingFolder); } } if (File.Exists(workingFolder.UserConfigPath)) { workingFolder.UserConfig = UserConfig.FromFile(workingFolder.UserConfigPath, workingFolder.Path); Size = workingFolder.UserConfig.WindowSize; splitContainer_main.SplitterDistance = workingFolder.UserConfig.MainSplitterDistance; splitContainer_editor.SplitterDistance = workingFolder.UserConfig.EditorSplitterDistance; if (workingFolder.UserConfig.WindowPosition.X == -32000) { WindowState = FormWindowState.Minimized; } else { Left = workingFolder.UserConfig.WindowPosition.X; Top = workingFolder.UserConfig.WindowPosition.Y; } } else { workingFolder.UserConfig = new UserConfig() { EditorSplitterDistance = splitContainer_editor.SplitterDistance, MainSplitterDistance = splitContainer_main.SplitterDistance, OpenedTabs = new List <string>(), OutputType = OutputType.Hex, SelectedTab = -1, WindowPosition = new Point(Left, Top), WindowSize = Size }; UserConfig.ToFile(workingFolder.UserConfigPath, workingFolder.UserConfig); } if (File.Exists(workingFolder.CompileConfigPath)) { workingFolder.CompileConfig = CompileConfig.FromFile(workingFolder.CompileConfigPath); workingFolder.CompileConfig.FileName = workingFolder.CompileConfigPath; } else { workingFolder.CompileConfig = new CompileConfig() { FileName = workingFolder.CompileConfigPath }; CompileConfig.ToFile(workingFolder.CompileConfigPath, workingFolder.CompileConfig); } Machine = new HASMMachine( (uint)workingFolder.CompileConfig.RAM, (uint)workingFolder.CompileConfig.EEPROM, (uint)workingFolder.CompileConfig.Flash, workingFolder.CompileConfig.Base) { BannedFeatures = workingFolder.CompileConfig.BannedFeatures, UserDefinedDefines = workingFolder.CompileConfig.Defines .FindAll(p => !string.IsNullOrEmpty(p.Name)) .Select(p => new HASMLib.Parser.SyntaxTokens.Preprocessor.Define(p.Name, new HASMLib.Parser.SyntaxTokens.Preprocessor.StringGroup(p.Value))) .ToList() }; Machine.SetRegisters(workingFolder.CompileConfig.RegisterNameFormat, (uint)workingFolder.CompileConfig.RegisterCount); foreach (var path in workingFolder.CompileConfig.IncludePaths) { string value = path.Split(',')[0]; string pattern = path.Split(',')[1]; if (Directory.Exists(value)) { Machine.Cache.AddFileNames(value, pattern); } else { value = PlatformSpecific.CombinePath(workingFolder.Path, value); if (Directory.Exists(value)) { Machine.Cache.AddFileNames(value, pattern); } else { MessageBox.Show($"Unable to directory file {value}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } } } RebuildCache(); if (workingFolder.UserConfig.OpenedTabs != null) { foreach (string path in workingFolder.UserConfig.OpenedTabs) { AddTab(path); } } workingFolder.SetTreeView(treeView1); treeView1.ExpandAll(); toolStripComboBox1.Items.Clear(); toolStripComboBox1.Items.AddRange(workingFolder.SourceFiles.Select(p => (object)p).ToArray()); toolStripComboBox1.SelectedItem = workingFolder.PreferedToCompile; tabControl1.Enabled = true; tabControl1.SelectedIndex = workingFolder.UserConfig.SelectedTab; switch (workingFolder.UserConfig.OutputType) { case OutputType.Hex: hexOutputToolStripMenuItem.Checked = true; break; case OutputType.Dec: decOutputToolStripMenuItem.Checked = true; break; case OutputType.Char: charOutputToolStripMenuItem.Checked = true; break; default: break; } outputType = workingFolder.UserConfig.OutputType; Text = $"HASM Editor { (tabControl1.SelectedTab == null ? "" : " - " + (tabControl1.SelectedTab as TextEditor).Path.NormalizePath())}"; }