/// <summary> /// Creates a compiler based on a specified AinFile, using its initial variables and other symbols. Does not modify the original. /// </summary> /// <param name="ainFileIn">The ain file to read symbols from</param> public Compiler(AinFile ainFileIn) : this(ainFileIn.Clone(), -1) { //foreach (var function in ainFile.Functions) //{ // this.Symbols.Add(function.Name, function); //} //foreach (var global in ainFile.Globals) //{ // this.Symbols.Add(global.Name, global); //} }
private void ExportAndMerge() { string newDisassembledCode = null; if ((this.CodePatches2 != null && this.CodePatches2.Length > 0) || numberedStrings.Count > 0) { this.ainFile = ainFile.Clone(); } if (this.CodePatches2 != null && this.CodePatches2.Length > 0) { //this.ainFile = ainFile.Clone(); var compiler = new Compiler.Compiler(ainFile); byte[] codeBytes; compiler.CompileCode(this.CodePatches2.ToString(), out codeBytes, out newDisassembledCode, true, this.CodePatches); if (newDisassembledCode == null || compiler.Errors.Count > 0) { var errorsListForm = new ErrorsListForm(); errorsListForm.SetErrorList(compiler.Errors); errorsListForm.Show(); return; } //foreach (var func in functionsToOutput) //{ // this.CodePatches[func.Name] = ""; //} } currentFunctionName = ""; stringDictionary = null; messageDictionary = null; stringNumber = 0; messageNumber = 0; //var ainFile = this.ainFile; if (numberedStrings.Count > 0) { //this.ainFile = ainFile.Clone(); var stringExportImport = new StringExportImport(ainFile); int firstMessage = stringExportImport.GetFirstMessageIdNumber(); int lastMessage = firstMessage + ainFile.Messages.Count; int firstString = stringExportImport.GetFirstStringIdNumber(); int lastString = firstString + ainFile.Strings.Count; foreach (var pair in numberedStrings) { int number = pair.Key; string message = pair.Value; if (number >= firstMessage && number < lastMessage) { ainFile.Messages[number - firstMessage] = message; } else if (number >= firstString && number < lastString) { ainFile.Strings[number - firstString] = message; } } } using (TemporaryFile tempFile = new TemporaryFile("jam", true)) { var saver = new ExplorerForm.SaveProjectInBackground(); var options = new CodeDisplayOptions(); options.AnnotateWithDecompiledCode = false; options.MergeDuplicateStrings = true; options.MergeDuplicateMessages = true; var writer = new AssemblerProjectWriter(ainFile, options); writer.BeforeWritingInstruction += new EventHandler <InstructionInfoEventArgs>(writer_BeforeWritingInstruction); writer.BeforeWritingString += new EventHandler <InstructionInfoEventArgs>(writer_BeforeWritingString); saver.SaveAsProject(writer, tempFile.FileName, true); if (newDisassembledCode != null) { using (var fs = new FileStream(tempFile.FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { using (var sw = new StreamWriter(fs, Extensions.TextEncoding)) { sw.WriteLine(); sw.WriteLine(newDisassembledCode); sw.Flush(); fs.Flush(); sw.Close(); fs.Close(); } } } //WaitForFileLock(tempFile.FileName, 2000); //tempFile.FileName var builder = new ExplorerForm.BuildProjectInBackground(); builder.ForceUniqueMessages = true; builder.Run(tempFile.FileName, this.outputFileName, true); } }
public bool ImportTextFile(string fileName, string outputFileName) { var encoding = EncodingDetector.DetectEncoding(fileName); string[] lines = File.ReadAllLines(fileName, encoding); ParseTextFile(lines); int firstMessageIdNumber = GetFirstMessageIdNumber(); int lastMessageIdNumber = firstMessageIdNumber + ainFile.Messages.Count - 1; int firstStringIdNumber = GetFirstStringIdNumber(); int lastStringIdNumber = firstStringIdNumber + ainFile.Strings.Count - 1; //old system also exported filenames and global group names, we never want to change these, but don't throw error messages if these are seen. int firstExtraNumber = firstStringIdNumber + ainFile.Strings.Count; int lastExtraNumber = firstExtraNumber + ainFile.Filenames.Count + ainFile.GlobalGroupNames.Count - 1; foreach (var key in lineNumbersAndStrings.Keys) { if (!((key >= firstMessageIdNumber && key <= lastMessageIdNumber) || (key >= firstStringIdNumber && key <= lastStringIdNumber) || (key >= firstExtraNumber && key <= lastExtraNumber))) { string prompt = "Invalid line number " + key + "." + Environment.NewLine + "Line numbers must be between " + firstMessageIdNumber + " and " + lastMessageIdNumber + " (messages) " + "or between " + firstStringIdNumber + " and " + lastStringIdNumber + " (strings)." + Environment.NewLine + "Import aborted."; if (Program.ConsoleMode) { Console.WriteLine(prompt); } else { MessageBox.Show(prompt, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop); } return(false); } } var ainCopy = ainFile.Clone(); GetExclusionList(); foreach (var pair in lineNumbersAndStrings) { string text = pair.Value; int idNumber = pair.Key; int messageIndex = idNumber - firstMessageIdNumber; int stringIndex = idNumber - firstStringIdNumber; if (messageIndex >= 0 && messageIndex < ainCopy.Messages.Count) { ainCopy.Messages[messageIndex] = text; } if (stringIndex >= 0 && stringIndex < ainCopy.Strings.Count) { if (ApplyFiltersOnImport && stringsToExclude.ContainsKey(stringIndex)) { //it's filtered, do nothing } else { ainCopy.Strings[stringIndex] = text; } } } ainCopy.WriteAndEncryptAinFile(outputFileName); return(true); }