예제 #1
0
        static void Main(string[] args)
        {
            string dataWinFileName = args.ElementAtOrDefault(0);

            if (string.IsNullOrWhiteSpace(dataWinFileName))
            {
                Console.WriteLine("Missing data.win file");
                BadExit(1);
            }
            try
            {
                cr = new ChunkReader(dataWinFileName, false); // main pc
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not open data.win file '" + dataWinFileName + "'");
                Console.WriteLine("Exception: " + e.Message);
                BadExit(1);
            }

            List <string> stringList = cr.stringList.Select(x => x.str).ToList();

            InstanceList = cr.objList.Select(x => x.Name).ToList();
            scriptList   = cr.scriptIndex.Select(x => x.script_name).ToList();
            FunctionReplacement();
            GMContext context = new GMContext()
            {
                cr = cr, InstanceList = InstanceList, scriptList = scriptList, Debug = false
            };
            bool doAsm = false;
            bool all   = false;

            string toSearch = null;
            int    pos      = 1;

            while (pos < args.Length)
            {
                switch (args[pos])
                {
                case "-s":
                    pos++;
                    toSearch = args.ElementAtOrDefault(pos);
                    pos++;
                    break;

                case "-debug":
                    pos++;
                    context.Debug = true;
                    break;

                case "-all":
                    all = true;
                    pos++;
                    toSearch = args.ElementAtOrDefault(pos);
                    pos++;
                    break;

                case "-asm":
                    doAsm = true;
                    pos++;
                    break;

                default:
                    Console.WriteLine("Bad argument " + args[pos]);
                    BadExit(1);
                    break;
                }
                if (toSearch != null)
                {
                    break;
                }
            }
            if (toSearch == null)
            {
                Console.WriteLine("Missing search field");
                BadExit(1);
            }
            List <string> FilesFound = new List <string>();

            if (all)
            {
                switch (toSearch)
                {
                case "objects":
                {
                    var          errorinfo   = Directory.CreateDirectory("error");
                    StreamWriter errorWriter = null;
                    foreach (var a in cr.GetAllObjectCode())
                    {
                        var info = Directory.CreateDirectory(a.ObjectName);
                        foreach (var files in a.Streams)
                        {
                            var instructionsNew = betteribttest.Dissasembler.Instruction.Dissasemble(files.stream.BaseStream, stringList, InstanceList);
                            if (doAsm)
                            {
                                string asm_filename = Path.Combine(info.FullName, files.ScriptName + ".asm");
                                betteribttest.Dissasembler.InstructionHelper.DebugSaveList(instructionsNew.Values, asm_filename);
                            }
                            string code_name = Path.Combine(info.FullName, files.ScriptName + ".cpp");
                            try
                            {
                                ILBlock block = new betteribttest.Dissasembler.ILAstBuilder().Build(instructionsNew, false, context);
                                FunctionFix.FixCalls(block);
                                PushFix.FixCalls(block);

                                FilesFound.Add(code_name);
                                block.DebugSave(code_name, "// ScriptName: " + files.ScriptName);
                                Console.WriteLine("Written: " + files.ScriptName + ".cpp");
                            }
                            catch (Exception e)
                            {
                                if (errorWriter == null)
                                {
                                    errorWriter = new StreamWriter("error_objects.txt");
                                }
                                string message = string.Format("Object: {0}  Error: {1}", files.ScriptName, e.Message);
                                errorWriter.WriteLine(message);
                                Console.WriteLine("Error: " + message);
                            }
                        }
                    }
                }
                break;

                case "scripts":
                {
                    var          errorinfo   = Directory.CreateDirectory("error");
                    var          info        = Directory.CreateDirectory("scripts");
                    StreamWriter errorWriter = null;
                    foreach (var files in cr.GetAllScripts())
                    {
                        string code_name = Path.Combine(info.FullName, files.ScriptName + ".cpp");
                        context.CurrentScript = files.ScriptName;
                        var instructionsNew = betteribttest.Dissasembler.Instruction.Dissasemble(files.stream.BaseStream, stringList, InstanceList);
                        if (doAsm)
                        {
                            string asm_filename = Path.Combine(info.FullName, files.ScriptName + ".asm");
                            betteribttest.Dissasembler.InstructionHelper.DebugSaveList(instructionsNew.Values, asm_filename);
                        }
#if !DEBUG
                        try
                        {
#endif
                        Console.WriteLine("Writing: " + files.ScriptName + ".cpp");
                        ILBlock block = new betteribttest.Dissasembler.ILAstBuilder().Build(instructionsNew, false, context);
                        FunctionFix.FixCalls(block);
                        PushFix.FixCalls(block);

                        FilesFound.Add(code_name);
                        block.DebugSave(code_name, "// ScriptName: " + files.ScriptName);

#if !DEBUG
                    }
                    catch (Exception e)
                    {
                        if (errorWriter == null)
                        {
                            errorWriter = new StreamWriter("error_scripts.txt");
                        }
                        string message = string.Format("Script: {0}  Error: {1}", files.ScriptName, e.Message);
                        errorWriter.WriteLine(message);
                        Console.WriteLine("Error: " + message);
                    }
#endif
                    }
                }
                break;

                default:
                    Console.WriteLine("Unkonwn -all specifiyer");
                    BadExit(1);
                    break;
                }
            }
            else
            {
                foreach (var files in cr.GetCodeStreams(toSearch))
                {
                    //  Instruction.Instructions instructions = null;// Instruction.Create(files.stream, stringList, InstanceList);

                    var instructionsNew = betteribttest.Dissasembler.Instruction.Dissasemble(files.stream.BaseStream, stringList, InstanceList);
                    if (doAsm)
                    {
                        betteribttest.Dissasembler.InstructionHelper.DebugSaveList(instructionsNew.Values, files.ScriptName + ".asm");
                    }
                    ILBlock block = new betteribttest.Dissasembler.ILAstBuilder().Build(instructionsNew, false, context);
                    //   block.Body.WriteNodes
                    // block.DebugSave("bytecode_test.cpp", "// ScriptName: " + files.ScriptName);
                    FunctionFix.FixCalls(block);
                    PushFix.FixCalls(block);
                    FilesFound.Add(files.ScriptName);
                    // block.DebugSave("bytecode_test.cpp", "// ScriptName: " + files.ScriptName);
#if DEBUG
                    betteribttest.Dissasembler.InstructionHelper.DebugSaveList(instructionsNew.Values, "debug.asm");
                    block.DebugSave("debug.cpp", "// ScriptName: " + files.ScriptName);
                    block.DebugSave(files.ScriptName + ".cpp", "// ScriptName: " + files.ScriptName);
#endif
                    Console.WriteLine("Written: " + files.ScriptName + ".cpp");
                }
            }

            if (FilesFound.Count == 0)
            {
                Console.WriteLine("No scripts or objects found with '" + toSearch + "' in the name");
            }
            System.Diagnostics.Debug.WriteLine("Done");
        }