コード例 #1
0
ファイル: Program.cs プロジェクト: LexouDuck/Event-Assembler
        // 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(" ", "", ""));
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: LexouDuck/Event-Assembler
        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> (" ", "", ""));
                    }
                }
            }
        }