void OpenFile() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = pluginLoader.GetAllFormatsFilter(); DialogResult result = openFileDialog.ShowDialog(); if (result != DialogResult.OK) { return; } var fileName = openFileDialog.FileName; string extension = Path.GetExtension(fileName).ToLowerInvariant().Replace(".", ""); if (!File.Exists(fileName)) { MessageBox.Show("File does not exist: " + fileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte[] rawBinaryFile; Plugins.FileFormat fileFormatPlugin; if (pluginLoader.TryGetFileFormat(extension, out fileFormatPlugin)) { if (fileFormatPlugin.Open(fileName)) { if (fileFormatPlugin.TryReadBytes(out rawBinaryFile)) { if (rawBinaryFile.Length > 0) { SaveFile(rawBinaryFile); return; } } } } MessageBox.Show("Invalid file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
bool OpenFile() { _rawBinaryFile = new byte[0]; var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = _pluginLoader.GetAllFormatsFilter(); DialogResult result = openFileDialog.ShowDialog(); if (result != DialogResult.OK) { return(false); } var fileName = openFileDialog.FileName; string extension = Path.GetExtension(fileName).ToLowerInvariant().Replace(".", ""); if (!File.Exists(fileName)) { MessageBox.Show("File does not exist: " + fileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } Plugins.FileFormat fileFormatPlugin; if (_pluginLoader.TryGetFileFormat(extension, out fileFormatPlugin)) { if (fileFormatPlugin.Open(fileName)) { if (fileFormatPlugin.TryReadBytes(out _rawBinaryFile)) { if (_rawBinaryFile.Length == 1048576) { return(true); } else { MessageBox.Show($"Invalid file size, expected {1048576} bytes but the file was {_rawBinaryFile.Length } bytes", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } MessageBox.Show("Invalid file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Could not find a plugin to load this type of file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); }