private void CreateMenuFile() { FileMenu = new ThemedToolStripMenuHeader() { Name = "FileMenuItem", // Add spaces to give some extra room to the menu. Size = CorrectedMeasureText(" File ", BaseUI.BaseFont), Text = "File", }; ThemedToolStripMenuItem OpenMenu = new ThemedToolStripMenuItem() { Text = "Open" }; OpenMenu.Click += (s, a) => { OpenFileDialog OpenFile = new OpenFileDialog(); // This will set OpenFile.FileName to the chosen path. OpenFile.ShowDialog(); // "" is returned when the user closes the DialogBox with alt f4 or the X icon. It was annoying when // the error message showed up after doing this. if (OpenFile.FileName != "") { // This should not really be handled here, rather in the main class file. FlashFromFile(OpenFile.FileName); } }; ThemedToolStripMenuItem OpenClipboardMenu = new ThemedToolStripMenuItem() { Text = "Open from clipboard" }; OpenClipboardMenu.Click += (s, a) => { // Parse the clip board as text. IO.TXT ParsedClipboard = IO.TXT.Parse(System.Text.Encoding.UTF8.GetBytes(Clipboard.GetText(TextDataFormat.UnicodeText))); // If the output is null, there was an error somewhere, but this is already handled in the TXT class. // Regardless, the VM will be unchanged in this case. if (ParsedClipboard != null) { // This should not really be handled here, rather in the main class file. FlashProcedure(ParsedClipboard.Instructions); } }; // It is important this is called after both of these are initialised. FileMenu.DropDownItems.AddRange(new[] { OpenMenu, OpenClipboardMenu }); }
public ParseResult Parse(ParseMode mode, out byte[] Instructions) { byte[] MagicBytes = new byte[4]; // Open the file as read only. using (FileStream Reader = TargetFile.Open(FileMode.Open)) { if (mode == ParseMode.AUTO) { // There must be no less than 4 bytes in the file in order to determine its magic bytes. This is not necessarily true for // every file type. If there are only 3 bytes in the read file, the user probably chose the wrong file anyway. // Reader.Read() returns the number of bytes read. if (Reader.Read(MagicBytes, 0, 4) != 4) { Logger.Log(LogCode.IO_INVALIDFILE, "File must be no less than 4 bytes in length"); Instructions = null; return(ParseResult.INVALID); } // Order the bytes in big endian(because was read from a file) to form the signature of magic bytes. // Most magic byte signatures are 4 bytes, especially for the purposes of this program. int Signature = MagicBytes[3] + (MagicBytes[2] << 8) + (MagicBytes[1] << 16) + (MagicBytes[0] << 24); // Check if the file type can be inferred from the magic byte signature from the signatures registered in the SignatureTable. MagicDelegate ResultDel; if (SignatureTable.TryGetValue(Signature, out ResultDel)) { IMyExecutable Result = ResultDel(Reader); // If $Result is null after parsing, there was an error in doing so. if (Result == null) { Instructions = null; return(ParseResult.INVALID); } else { Instructions = ResultDel(Reader).Instructions; return(ParseResult.SUCCESS); } } else { // If the file type cannot be inferred, Instructions = null; return(ParseResult.NOT_INFERRED); } } // Other parse modes for forcing files to be interpreted as a particular format. else if (mode == ParseMode.BIN) { Instructions = BIN.Parse(Reader).Instructions; return(ParseResult.SUCCESS); } else if (mode == ParseMode.TXT) { TXT Result = TXT.Parse(Reader); // If $Result is null after parsing, there was an error in doing so. if (Result == null) { Instructions = null; return(ParseResult.INVALID); } else { Instructions = Result.Instructions; return(ParseResult.SUCCESS); } } } // This should never be reached. throw new System.Exception(); }