Exemplo n.º 1
0
        public int ReassembleFile(string outputFile, string ilSuffix, CpuPlatform cpu)
        {
            string currentDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(TempDirectory);

            try
            {
                string directoryName = Path.GetDirectoryName(outputFile);
                if (directoryName != null && !Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }

                using (IlParser ilParser = new IlParser(ServiceProvider)) {
                    ReassembleFile(ilParser, ilSuffix, cpu);
                }

                return(Run(outputFile, ilSuffix, cpu));
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }
        }
Exemplo n.º 2
0
        private int RunCore(CpuPlatform cpu, string fileName, string ressourceParam, string ilSuffix)
        {
            string str = (string)null;

            if (!string.IsNullOrEmpty(this.InputValues.KeyFile))
            {
                str = Path.GetFullPath(this.InputValues.KeyFile);
            }
            if (!string.IsNullOrEmpty(str) && !File.Exists(str))
            {
                if (!string.IsNullOrEmpty(this.InputValues.RootDirectory) && Directory.Exists(this.InputValues.RootDirectory))
                {
                    str = Path.Combine(this.InputValues.RootDirectory, this.InputValues.KeyFile);
                }
                if (!File.Exists(str))
                {
                    throw new FileNotFoundException(string.Format(Resources.Provided_key_file_0_cannot_be_found, (object)str));
                }
            }

            int ret = IlParser.RunIlTool
                      (
                String.IsNullOrWhiteSpace(InputValues.OurILAsmPath) ? InputValues.FrameworkPath : InputValues.OurILAsmPath,
                "ilasm.exe",
                null,
                null,
                "ILAsmPath",
                GetCommandLineArguments(cpu, fileName, ressourceParam, ilSuffix, str),
                DllExportLogginCodes.IlAsmLogging,
                DllExportLogginCodes.VerboseToolLogging,
                Notifier,
                Timeout,
                line =>
            {
                int col = line.IndexOf(": ");
                if (col > 0)
                {
                    line = line.Substring(col + 1);
                }

                return(IlAsm
                       ._NormalizeIlErrorLineRegex
                       .Replace(line, "")
                       .ToLowerInvariant()
                       .StartsWith("warningnonvirtualnonabstractinstancemethodininterfacesettosuch"));
            }
                      );

            if (ret != 0)
            {
                return(ret);
            }

            ret = CheckPE(cpu, fileName);
            if (ret == 0)
            {
                RunLibTool(cpu, fileName, Path.GetFullPath(Path.GetDirectoryName(fileName)));
            }
            return(ret);
        }
Exemplo n.º 3
0
        public int ReassembleFile(string outputFile, string ilSuffix, CpuPlatform cpu)
        {
            string currentDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(this.TempDirectory);
            try
            {
                string directoryName = Path.GetDirectoryName(outputFile);
                if (directoryName != null && !Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                using (IlParser ilParser = new IlParser(this.ServiceProvider))
                {
                    ilParser.Exports       = this.Exports;
                    ilParser.InputValues   = this.InputValues;
                    ilParser.TempDirectory = this.TempDirectory;
                    List <string> stringList = new List <string>(ilParser.GetLines(cpu));
                    if (stringList.Count > 0)
                    {
                        string input = stringList[stringList.Count - 1];
                        if (!input.NullSafeCall <string, bool>((Func <string, bool>)(l => {
                            if (!l.EndsWith("\\r"))
                            {
                                return(l.EndsWith("\\n"));
                            }
                            return(true);
                        })))
                        {
                            stringList[stringList.Count - 1] = input + Environment.NewLine;
                        }
                    }

                    Stream stream = null;
                    try {
                        stream = new FileStream(Path.Combine(TempDirectory, InputValues.FileName + ilSuffix + ".il"), FileMode.Create);
                        using (StreamWriter swriter = new StreamWriter(stream, Encoding.Unicode))
                        {
                            stream = null; // avoid CA2202
                            swriter.WriteLine(String.Join(Environment.NewLine, stringList));
                        }
                    }
                    finally {
                        if (stream != null)
                        {
                            stream.Dispose();
                        }
                    }
                }
                return(this.Run(outputFile, ilSuffix, cpu));
            }
            finally
            {
                Directory.SetCurrentDirectory(currentDirectory);
            }
        }
        private int RunLibToolCore(CpuPlatform cpu, string directory, string defFileName)
        {
            string path = Path.Combine(directory, Path.GetFileNameWithoutExtension(this.InputValues.OutputFileName)) + ".lib";

            try
            {
                return(IlParser.RunIlTool(this.InputValues.LibToolPath, "Lib.exe", string.IsNullOrEmpty(this.InputValues.LibToolDllPath) || !Directory.Exists(this.InputValues.LibToolDllPath) ? (string)null : this.InputValues.LibToolDllPath, (string)null, "LibToolPath", string.Format("\"/def:{0}\" /machine:{1} \"/out:{2}\"", (object)defFileName, (object)cpu, (object)path), DllExportLogginCodes.LibToolLooging, DllExportLogginCodes.LibToolVerboseLooging, this.Notifier, this.Timeout, (Func <string, bool>)null));
            }
            catch (Exception ex)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                throw;
            }
        }
            public static Dictionary <ParserState, IParserStateAction> GetActionsByState(
                IlParser parser)
            {
                Dictionary <ParserState, IParserStateAction> dictionary = new Dictionary <ParserState, IParserStateAction>()
                {
                    {
                        ParserState.ClassDeclaration,
                        (IParserStateAction) new ClassDeclarationParserAction()
                    },
                    {
                        ParserState.Class,
                        (IParserStateAction) new ClassParserAction()
                    },
                    {
                        ParserState.DeleteExportAttribute,
                        (IParserStateAction) new DeleteExportAttributeParserAction()
                    },
                    {
                        ParserState.MethodDeclaration,
                        (IParserStateAction) new MethodDeclarationParserAction()
                    },
                    {
                        ParserState.Method,
                        (IParserStateAction) new MethodParserAction()
                    },
                    {
                        ParserState.MethodProperties,
                        (IParserStateAction) new MethodPropertiesParserAction()
                    },
                    {
                        ParserState.Normal,
                        (IParserStateAction) new NormalParserAction()
                    }
                };

                foreach (IParserStateAction parserStateAction in dictionary.Values)
                {
                    parserStateAction.Parser = parser;
                }
                return(dictionary);
            }
Exemplo n.º 6
0
 public int Run()
 {
     return(IlParser.RunIlTool
            (
                String.IsNullOrWhiteSpace(InputValues.OurILAsmPath) ? InputValues.SdkPath : InputValues.OurILAsmPath,
                "ildasm.exe",
                null,
                null,
                "ILDasmPath",
                String.Format(
                    CultureInfo.InvariantCulture,
                    "/quoteallnames /unicode /nobar{2}\"/out:{0}.il\" \"{1}\"",
                    Path.Combine(TempDirectory, InputValues.FileName),
                    InputValues.InputFileName,
                    InputValues.EmitDebugSymbols ? " /linenum " : " "
                    ),
                DllExportLogginCodes.IlDasmLogging,
                DllExportLogginCodes.VerboseToolLogging,
                Notifier,
                Timeout,
                null
            ));
 }
Exemplo n.º 7
0
        private void ReassembleFile(IlParser ilParser, string ilSuffix, CpuPlatform cpu)
        {
            ilParser.Exports       = Exports;
            ilParser.InputValues   = InputValues;
            ilParser.TempDirectory = TempDirectory;

            var stringList = new List <string>(ilParser.GetLines(cpu));

            if (stringList.Count > 0)
            {
                string input = stringList[stringList.Count - 1];

                if (!input.NullSafeCall(l => !l.EndsWith("\\r") ? l.EndsWith("\\n") : true))
                {
                    stringList[stringList.Count - 1] = input + Environment.NewLine;
                }
            }

            var dest = Path.Combine(TempDirectory, InputValues.FileName + ilSuffix + ".il");

            using (var swriter = new StreamWriter(dest, false, Encoding.Unicode)) {
                swriter.WriteLine(String.Join(Environment.NewLine, stringList));
            }
        }
Exemplo n.º 8
0
 private int RunLibToolCore(string tool, string args, string path = "", string reqPath = null)
 {
     try
     {
         return(IlParser.RunIlTool
                (
                    path,
                    tool,
                    reqPath,
                    null,
                    "LibToolPath",
                    args,
                    DllExportLogginCodes.LibToolLooging,
                    DllExportLogginCodes.LibToolVerboseLooging,
                    Notifier,
                    Timeout,
                    null
                ));
     }
     catch (Exception ex) {
         Notifier.Notify(-1, DllExportLogginCodes.LibToolLooging, Resources.An_error_occurred_while_calling_0_1_, $" {tool} {args} ", ex.Message);
         return(-1);
     }
 }
Exemplo n.º 9
0
 public int Run()
 {
     return(IlParser.RunIlTool(this.InputValues.SdkPath, "ildasm.exe", (string)null, (string)null, "ILDasmPath", string.Format((IFormatProvider)CultureInfo.InvariantCulture, "/quoteallnames /unicode /nobar{2}\"/out:{0}.il\" \"{1}\"", (object)Path.Combine(this.TempDirectory, this.InputValues.FileName), (object)this.InputValues.InputFileName, this.InputValues.EmitDebugSymbols ? (object)" /linenum " : (object)" "), DllExportLogginCodes.IlDasmLogging, DllExportLogginCodes.VerboseToolLogging, this.Notifier, this.Timeout, (Func <string, bool>)null));
 }
Exemplo n.º 10
0
 internal static int RunIlTool(string installPath, string toolFileName, string requiredPaths, string workingDirectory, string settingsName, string arguments, string toolLoggingCode, string verboseLoggingCode, IDllExportNotifier notifier, int timeout, Func <string, bool> suppressErrorOutputLine = null)
 {
     using (notifier.CreateContextName((object)null, toolFileName))
     {
         if (suppressErrorOutputLine != null)
         {
             Func <string, bool> suppressErrorOutputLineCore = suppressErrorOutputLine;
             suppressErrorOutputLine = (Func <string, bool>)(line => {
                 if (line != null)
                 {
                     return(suppressErrorOutputLineCore(line));
                 }
                 return(false);
             });
         }
         else
         {
             suppressErrorOutputLine = (Func <string, bool>)(l => false);
         }
         string fileName         = Path.GetFileName(toolFileName);
         string exePath          = IlParser.GetExePath(fileName, installPath, settingsName);
         string withoutExtension = Path.GetFileNameWithoutExtension(fileName);
         int    num;
         using (Process process = new Process())
         {
             notifier.Notify(-2, toolLoggingCode, Resources.calling_0_with_1_, (object)exePath, (object)arguments);
             ProcessStartInfo processStartInfo = new ProcessStartInfo(exePath, arguments)
             {
                 UseShellExecute        = false,
                 CreateNoWindow         = true,
                 RedirectStandardOutput = true,
                 RedirectStandardError  = true
             };
             if (!string.IsNullOrEmpty(workingDirectory))
             {
                 processStartInfo.WorkingDirectory = Path.GetFullPath(workingDirectory);
             }
             if (!string.IsNullOrEmpty(requiredPaths))
             {
                 processStartInfo.EnvironmentVariables["PATH"] = requiredPaths.Trim(';') + ";" + (processStartInfo.EnvironmentVariables.ContainsKey("PATH") ? processStartInfo.EnvironmentVariables["PATH"] : (string)null);
             }
             process.StartInfo = processStartInfo;
             process.Start();
             Stopwatch     stopwatch      = Stopwatch.StartNew();
             StringBuilder stringBuilder1 = new StringBuilder();
             StringBuilder stringBuilder2 = new StringBuilder();
             Action <IEnumerable <string>, StringBuilder> appendLines = (Action <IEnumerable <string>, StringBuilder>)((lines, sb) => lines.Aggregate <string, StringBuilder>(sb, (Func <StringBuilder, string, StringBuilder>)((r, line) => r.AppendLine(line))));
             Action <StreamReader, StringBuilder>         action1     = (Action <StreamReader, StringBuilder>)((sr, sb) => appendLines(IlParser.EnumerateStreamLines(sr), sb));
             Action <StreamReader, StringBuilder>         action2     = (Action <StreamReader, StringBuilder>)((sr, sb) => appendLines(IlParser.EnumerateStreamLines(sr).Where <string>((Func <string, bool>)(line => !suppressErrorOutputLine(line))), sb));
             while (stopwatch.ElapsedMilliseconds < (long)timeout && !process.HasExited)
             {
                 action1(process.StandardOutput, stringBuilder1);
                 action2(process.StandardError, stringBuilder2);
             }
             bool hasExited = process.HasExited;
             action1(process.StandardOutput, stringBuilder1);
             action2(process.StandardError, stringBuilder2);
             if (hasExited)
             {
                 notifier.Notify(-2, toolLoggingCode, Resources.R_0_1_returned_gracefully, (object)withoutExtension, (object)exePath);
                 int exitCode = process.ExitCode;
                 if (exitCode != 0 || stringBuilder2.Length > 0)
                 {
                     throw new InvalidOperationException((stringBuilder2.Length > 0 ? (object)stringBuilder2 : (object)stringBuilder1).ToString());
                 }
                 if (stringBuilder2.Length > 0)
                 {
                     notifier.Notify(-3, verboseLoggingCode, stringBuilder2.ToString());
                 }
                 if (stringBuilder1.Length > 0)
                 {
                     notifier.Notify(-3, verboseLoggingCode, stringBuilder1.ToString());
                 }
                 num = exitCode;
             }
             else
             {
                 bool      flag      = false;
                 Exception exception = (Exception)null;
                 try
                 {
                     process.Kill();
                     flag = true;
                 }
                 catch (Exception ex)
                 {
                     exception = ex;
                 }
                 if (flag)
                 {
                     throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, Resources.R_0_did_not_return_after_1_ms, new object[2]
                     {
                         (object)withoutExtension,
                         (object)timeout
                     }));
                 }
                 throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, Resources.R_0_did_not_return_after_1_ms_and_it_could_not_be_stopped, (object)withoutExtension, (object)timeout, (object)exception.Message));
             }
         }
         return(num);
     }
 }