Beispiel #1
0
        public EcsFile GenerateCodeFile(TypeDefinition type, Lazy <SyntaxTree> syntaxTree, OutputMode mode)
        {
            var outputName = type.Name + mode.extensions;
            //var sourceRes = type.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + mode.Name + "File");
            //if (sourceRes != null) {
            //	var sourceName = (string)sourceRes.ConstructorArguments[0].Value;

            //	if (sourceName == "") {
            //		// no filename provided, create empty output
            //		return new EcsFile(outputName, "");
            //	}

            //	var sourceContent = EmbeddedFileLoader.GetResourceFile(sourceName, type.Module);
            //	return new EcsFile(sourceName, sourceContent);
            //}

            var sourceRes = type.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + mode.Name);

            if (sourceRes != null)
            {
                var sourceContent = (string)sourceRes.ConstructorArguments[0].Value;
                return(new EcsFile(outputName, sourceContent));
            }


            var code = WriteCode(m_settings, syntaxTree.Value, m_decompiler.TypeSystem, mode);

            return(new EcsFile(outputName, code));
        }
Beispiel #2
0
        //private static EcsFile GenerateFile(TypeDefinition t, CSharpDecompiler codeDomBuilder, string attributeString, OutputMode mode, string ext)
        //{
        //	var sourceRes = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString + "File");
        //	var sourceAtt = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString);
        //	if (sourceRes != null) {
        //		var sourceName = t.Name + ext;
        //		sourceName = (string)sourceRes.ConstructorArguments[0].Value;
        //		var sourceContent = EmbeddedFileLoader.GetResourceFile(sourceName, t.Module);
        //		return new EcsFile(sourceName, sourceContent);
        //	}
        //	if (sourceAtt != null) {
        //		var sourceContent = (string)sourceAtt.ConstructorArguments[0].Value;
        //		return new EcsFile(t.Name + ext, sourceContent);
        //	}

        //	return new EcsFile(t.Name + ext, WriteType(codeDomBuilder, mode));
        //}

        string WriteCode(DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem, OutputMode mode)
        {
            //syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });

            var ms = new MemoryStream();

            using (StreamWriter w = new StreamWriter(ms)) {
                //codeDomBuilder.GenerateCode(new PlainTextOutput(w), mode);
                TokenWriter tokenWriter = new TextTokenWriter(new PlainTextOutput(w), settings, typeSystem)
                {
                    FoldBraces = settings.FoldBraces, ExpandMemberDefinitions = settings.ExpandMemberDefinitions
                };
                //syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));

                syntaxTree.AcceptVisitor(new COutputVisitor(tokenWriter, settings.CSharpFormattingOptions, mode));

                w.Flush();
                ms.Position = 0;
                var sr    = new StreamReader(ms);
                var myStr = sr.ReadToEnd();
                return(myStr);
            }
        }