public static void Preprocess(string originalFile, string outputFile, string game, ILog messageLog) { EACodeLanguage eaCodeLanguage = Program.Languages [game]; using (IPreprocessor preprocessor = (IPreprocessor) new Preprocessor(messageLog)) { preprocessor.AddReserved(eaCodeLanguage.GetCodeNames()); preprocessor.AddDefined(new string[] { "_" + game + "_", "_EA_" }); using (StreamReader streamReader = File.OpenText(originalFile)) { using (IInputStream inputStream = (IInputStream) new PreprocessingInputStream((TextReader)streamReader, preprocessor)) { StringWriter stringWriter = new StringWriter(); while (true) { string str = inputStream.ReadLine(); if (str != null) { stringWriter.WriteLine(str); } else { break; } } messageLog.AddMessage("Processed code:\n" + stringWriter.ToString() + "\nEnd processed code"); } } } }
// Used by Emblem Magic (and could potentially be used by other external software) public static void Disassemble( EACodeLanguage language, byte[] rom, string filename, TextWriter output, bool addEndGuards, DisassemblyMode mode, int offset, Priority priority, int size, ILog log) { if (offset > rom.Length) { log.AddError("Offset is larger than size of ROM."); } if (size <= 0 || size + offset > rom.Length) { size = rom.Length - offset; } IEnumerable <string[]> strArrays; string[] lines; switch (mode) { case DisassemblyMode.Block: strArrays = language.Disassemble(rom, offset, size, priority, addEndGuards, log); lines = CoreInfo.DefaultLines(language.Name, Path.GetFileName(filename), offset, new int?(size)); break; case DisassemblyMode.ToEnd: strArrays = language.DisassembleToEnd(rom, offset, priority, addEndGuards, log); lines = CoreInfo.DefaultLines(language.Name, Path.GetFileName(filename), offset, new int?()); break; case DisassemblyMode.Structure: strArrays = language.DisassembleChapter(rom, offset, addEndGuards, log); lines = CoreInfo.DefaultLines(language.Name, Path.GetFileName(filename), offset, new int?()); break; default: throw new ArgumentException(); } if (log.ErrorCount == 0) { if (filename.Length > 0) { output.WriteLine(Program.Frame(lines, "//", 1)); output.WriteLine(); } foreach (string[] strArray in strArrays) { output.WriteLine(((IEnumerable <string>)strArray).ToElementWiseString(" ", "", "")); } } }
// Used by Emblem Magic (and could potentially be used by other external software) public static void Assemble(EACodeLanguage language, TextReader input, BinaryWriter output, ILog log) { List <string> stringList = new List <string>(); stringList.Add("_" + language.Name + "_"); stringList.Add("_EA_"); using (IPreprocessor preprocessor = new Preprocessor(log)) { preprocessor.AddReserved(language.GetCodeNames()); preprocessor.AddDefined(stringList.ToArray()); using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor)) { new EAExpressionAssembler(language.CodeStorage, new TokenParser <int>(new Func <string, int>(StringExtensions.GetValue))).Assemble(inputStream, output, log); } } }
private static void Assemble(EACodeLanguage language, TextReader input, BinaryWriter output, ILog log) { var predefined = new List<string> { "_" + language.Name + "_", "_EA_" }; using (IPreprocessor preprocessor = new Preprocessor(log)) { preprocessor.AddReserved(language.GetCodeNames()); preprocessor.AddDefined(predefined.ToArray()); IInputStream stream = new PreprocessingInputStream(input, preprocessor); language.Assemble(stream, output, log); } }
public static void LoadCodes(string rawsFolder, string extension, bool isDirectory, bool collectDocCodes) { languages = new Dictionary<string, EACodeLanguage>(); LanguageProcessor pro = new LanguageProcessor(collectDocCodes, new TemplateComparer(), stringComparer); IPointerMaker<int> ptrMaker = new GBAPointerMaker(); if (isDirectory) { pro.ProcessCode(rawsFolder, extension); } else { pro.ProcessCode(rawsFolder); } foreach (KeyValuePair<string, ICodeTemplateStorer> item in pro.Languages) { Tuple<string, List<Priority>>[][] pointerList; switch (item.Key) { case "FE6": pointerList = FE6CodeLanguage.PointerList; break; case "FE7": pointerList = FE7CodeLanguage.PointerList; break; case "FE8": pointerList = FE8CodeLanguage.PointerList; break; default: throw new NotSupportedException("Language " + item.Key + " not supported."); } ICodeTemplateStorer storer = item.Value; if (item.Key == "FE8") { storer.AddCode(new GenericFE8Template(), Priority.none); } EACodeLanguage language = new EACodeLanguage( item.Key, ptrMaker, pointerList, storer, stringComparer ); languages[item.Key] = language; } }
private static void Disassemble(ILog log) { if (!File.Exists(Program.RunConfig.inputFile)) { log.AddError("File `{0}` doesn't exist.", Program.RunConfig.inputFile); return; } if (File.Exists(Program.RunConfig.outputFile) && File.GetAttributes(Program.RunConfig.outputFile).HasFlag(FileAttributes.ReadOnly)) { log.AddError("Output cannot be written to. It is read-only."); return; } if (!Program.CodesLoaded) { LoadCodes(false); } EACodeLanguage eaCodeLanguage = Program.Languages [Program.RunConfig.language]; byte[] code = File.ReadAllBytes(Program.RunConfig.inputFile); if (Program.RunConfig.disassemblyOffset > code.Length) { log.AddError("Offset is larger than size of file."); return; } int size = Program.RunConfig.disassemblySize; int offset = Program.RunConfig.disassemblyOffset; if (size <= 0 || size + offset > code.Length) { size = code.Length - offset; } IEnumerable <string[]> strArrays; string[] lines; switch (Program.RunConfig.disassemblyMode) { case DisassemblyMode.Block: strArrays = eaCodeLanguage.Disassemble( code, offset, size, Program.RunConfig.disassemblyPriority, Program.RunConfig.addEndGuards, log ); lines = CoreInfo.DefaultLines( eaCodeLanguage.Name, Path.GetFileName(Program.RunConfig.inputFile), offset, new int?(size) ); break; case DisassemblyMode.ToEnd: strArrays = eaCodeLanguage.DisassembleToEnd( code, offset, Program.RunConfig.disassemblyPriority, Program.RunConfig.addEndGuards, log ); lines = CoreInfo.DefaultLines( eaCodeLanguage.Name, Path.GetFileName(Program.RunConfig.inputFile), offset, new int?() ); break; case DisassemblyMode.Structure: strArrays = eaCodeLanguage.DisassembleChapter( code, offset, Program.RunConfig.addEndGuards, log ); lines = CoreInfo.DefaultLines( eaCodeLanguage.Name, Path.GetFileName(Program.RunConfig.inputFile), offset, new int?() ); break; default: throw new ArgumentException(); } if (log.ErrorCount == 0) { using (StreamWriter streamWriter = new StreamWriter(Program.RunConfig.outputFile)) { streamWriter.WriteLine(); streamWriter.WriteLine(Program.Frame(lines, "//", 1)); streamWriter.WriteLine(); foreach (string[] strArray in strArrays) { streamWriter.WriteLine(((IEnumerable <string>)strArray).ToElementWiseString <string> (" ", "", "")); } } } }
private static void Assemble(ILog log) { TextReader input; bool inputIsFile; if (Program.RunConfig.inputFile != null) { input = File.OpenText(Program.RunConfig.inputFile); inputIsFile = false; } else { input = Console.In; inputIsFile = true; } using (IDirectivePreprocessor preprocessor = new Preprocessor(log)) { // preprocessor.AddReserved (eaCodeLanguage.GetCodeNames ()); preprocessor.AddDefined(new string[] { "_" + Program.RunConfig.language + "_", "_EA_" }); DependencyMakingIncludeListener depMaker = null; if (Program.RunConfig.ppDepEnable) { depMaker = new DependencyMakingIncludeListener(); preprocessor.IncludeListener = depMaker; } using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor)) { if (Program.RunConfig.ppSimulation) { // preprocess to null output while (inputStream.ReadLine() != null) { ; } } else { if (Program.RunConfig.outputFile == null) { log.AddError("No output file specified for assembly."); return; } string outFile = Program.RunConfig.outputFile; if (File.Exists(outFile) && File.GetAttributes(outFile).HasFlag((Enum)FileAttributes.ReadOnly)) { log.AddError("File `{0}` exists and cannot be written to.", outFile); return; } ChangeStream changeStream = new ChangeStream(); using (BinaryWriter output = new BinaryWriter((Stream)changeStream)) { if (!Program.CodesLoaded) { LoadCodes(false); } EACodeLanguage language = Program.Languages [Program.RunConfig.language]; EAExpressionAssembler assembler = new EAExpressionAssembler(language.CodeStorage, new TokenParser <int> (new Func <string, int> (StringExtensions.GetValue))); assembler.Assemble(inputStream, output, log); if (Program.RunConfig.symbolOutputFile != null) { // Outputting global symbols to another file try { if (File.Exists(Program.RunConfig.symbolOutputFile)) { File.Delete(Program.RunConfig.symbolOutputFile); } using (FileStream fileStream = File.OpenWrite(Program.RunConfig.symbolOutputFile)) using (StreamWriter symOut = new StreamWriter(fileStream)) foreach (KeyValuePair <string, int> symbol in assembler.GetGlobalSymbols()) { symOut.WriteLine("{0}={1}", symbol.Key, symbol.Value.ToHexString("$")); } } catch (Exception e) { log.AddError(e.ToString()); } } } if (log.ErrorCount == 0) { using (Stream stream = (Stream)File.OpenWrite(outFile)) changeStream.WriteToFile(stream); } } } if (depMaker != null) { try { depMaker.GenerateMakeDependencies(log); } catch (Exception e) { log.AddError(e.ToString()); } } } if (inputIsFile) { input.Close(); } }
private static void Compile(ILog log) { TextReader input; bool inputIsFile; if (Program.RunConfig.inputFile != null) { input = File.OpenText(Program.RunConfig.inputFile); inputIsFile = false; } else { input = Console.In; inputIsFile = true; } using (IDirectivePreprocessor preprocessor = new Preprocessor(log)) { // preprocessor.AddReserved (eaCodeLanguage.GetCodeNames ()); preprocessor.AddDefined(new string[] { "_" + Program.RunConfig.language + "_", "_EA_" }); DependencyMakingIncludeListener depMaker = null; if (Program.RunConfig.ppDepEnable) { depMaker = new DependencyMakingIncludeListener(); preprocessor.IncludeListener = depMaker; } using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor)) { if (Program.RunConfig.ppSimulation) { // preprocess to null output while (inputStream.ReadLine() != null) { ; } } else { if (Program.RunConfig.outputFile == null) { log.AddError("No output file specified for assembly."); return; } string outFile = Program.RunConfig.outputFile; if (File.Exists(outFile) && File.GetAttributes(outFile).HasFlag((Enum)FileAttributes.ReadOnly)) { log.AddError("File `{0}` exists and cannot be written to.", outFile); return; } using (StreamWriter output = new StreamWriter(outFile, false, Encoding.Default)) { // Make entry point label global to call in C source file //TODO support ARM? output.WriteLine("\t.thumb"); output.WriteLine("\t.global " + Path.GetFileNameWithoutExtension(outFile).Replace(".", "_")); output.WriteLine("\t.include \"event_func_Thumb.inc\""); output.WriteLine("\t.include \"event_func_C.inc\""); output.WriteLine(Path.GetFileNameWithoutExtension(outFile).Replace(".", "_") + ":"); if (!Program.CodesLoaded) { LoadCodes(false); } // Console.WriteLine("language: {0}", Program.RunConfig.language); EACodeLanguage language = Program.languages [Program.RunConfig.language]; EAExpressionAssembler assembler = new EAExpressionAssembler(language.CodeStorage, new TokenParser <int> (new Func <string, int> (StringExtensions.GetValue))); assembler.Compile(inputStream, output, log); if (Program.RunConfig.symbolOutputFile != null) { // Outputting global symbols to another file try { if (File.Exists(Program.RunConfig.symbolOutputFile)) { File.Delete(Program.RunConfig.symbolOutputFile); } using (FileStream fileStream = File.OpenWrite(Program.RunConfig.symbolOutputFile)) using (StreamWriter symOut = new StreamWriter(fileStream)) foreach (KeyValuePair <string, int> symbol in assembler.GetGlobalSymbols()) { symOut.WriteLine("{0}={1}", symbol.Key, symbol.Value.ToHexString("$")); } } catch (Exception e) { log.AddError(e.ToString()); } } output.Close(); } } } if (depMaker != null) { try { depMaker.GenerateMakeDependencies(log); } catch (Exception e) { log.AddError(e.ToString()); } } } if (inputIsFile) { input.Close(); } }
public static void GetNotepadPlusPluslanguageDoc(EACodeLanguage language, string outputFile) { using (XmlWriter writer = XmlWriter.Create(outputFile, new XmlWriterSettings() { Encoding = Encoding.ASCII, Indent = true, OmitXmlDeclaration = true, IndentChars = " " })) { writer.WriteStartElement("NotepadPlus"); writer.WriteStartElement("UserLang"); writer.WriteAttributeString("name", language.Name + " Event Assembly"); writer.WriteAttributeString("ext", "event"); writer.WriteStartElement("Settings"); writer.WriteStartElement("Global"); writer.WriteAttributeString("caseIgnored", "yes"); writer.WriteEndElement(); writer.WriteStartElement("TreatAsSymbol"); writer.WriteAttributeString("comment", "yes"); writer.WriteAttributeString("commentLine", "yes"); writer.WriteEndElement(); writer.WriteStartElement("Prefix"); writer.WriteAttributeString("words1", "no"); writer.WriteAttributeString("words2", "no"); writer.WriteAttributeString("words3", "no"); writer.WriteAttributeString("words4", "no"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement("KeywordLists"); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Delimiters", new string[1] { "<>" }); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Folder+", new string[1] { "{" }); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Folder-", new string[1] { "}" }); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Operators", "(", ")", "[", "]", "+", "-", "*", "/", "%", ">>", "<<", "&", "|", "^", ",", ";"); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Comment", "1/*", "2*/", "0//"); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Words1", language.GetCodeNames()); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Words2"); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Words3", "#ifdef", "#define", "#pool", "#else", "#endif", "#ifndef", "#include", "#incbin", "#undef"); HighlightingHelper.NotepadPlusPlus.WriteKeywords(writer, "Words4"); writer.WriteEndElement(); writer.WriteStartElement("Styles"); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "DEFAULT", 11, Color.Black, Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "FOLDEROPEN", 12, Color.Black, Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "FOLDERCLOSE", 13, Color.Black, Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "KEYWORD1", 5, Color.FromArgb(0, 0, (int)byte.MaxValue), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "KEYWORD2", 6, Color.FromArgb(0, 128, (int)byte.MaxValue), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "KEYWORD3", 7, Color.FromArgb(0, 64, 128), Color.White, "", 1, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "KEYWORD4", 8, Color.FromArgb(64, 128, 128), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "COMMENT", 1, Color.FromArgb(0, 159, 0), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "COMMENT LINE", 2, Color.FromArgb(0, 128, 0), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "NUMBER", 4, Color.FromArgb(128, 0, 128), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "OPERATOR", 10, Color.FromArgb((int)byte.MaxValue, 0, 0), Color.White, "", 1, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "DELIMINER1", 14, Color.FromArgb((int)byte.MaxValue, 0, (int)byte.MaxValue), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "DELIMINER2", 15, Color.FromArgb((int)byte.MaxValue, 0, 128), Color.White, "", 0, new int?()); HighlightingHelper.NotepadPlusPlus.WriteStyle(writer, "DELIMINER3", 16, Color.Black, Color.White, "", 0, new int?()); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); } }