Beispiel #1
0
        static void Main(string[] args)
        {
            Builder builder = new Builder();

            builder.AddClassAnalysisPass(
                new AnalysisPasses.AddNodeClassPass());

            builder.AddInstructionAnalysisPass(
                new AnalysisPasses.InputFindingInstructionPass());

            // builder.AddInstructionAnalysisPass(
            //     new AnalysisPasses.PrintAllInstructionPass());
            // TODO: give real implementation!

            builder.AddInstructionAnalysisPass(
                new AnalysisPasses.PrintMethodInfoInstructionPass("System.Void FollowPlayer::OnCollisionEnter(UnityEngine.Collision)"));

            builder.AddInstructionAnalysisPass(
                new AnalysisPasses.FindNodeUsageInstructionPass());

            ClassGraph graph = builder.Build();

            Console.WriteLine(graph.ToString());

            // CecilTests("Assembly-CSharp.dll");
            // GraphTests();
        }
 public static void validate(ClassGraph scene)
 {
     foreach (Node node in scene.GetNodes())
     {
         foreach (IConstraint constraint in node.Constraints)
         {
             // TODO:: prove base case
             ICollection <string> constrainedFields = constraint.getConstrainedFieldIdentifiers();
             HashSet <IOperation> totalOperations   = new HashSet <IOperation>();
             foreach (Edge edge in node.GetIncoming())
             {
                 // Should this instead allow for duplicate operations from different sources?
                 totalOperations.UnionWith(edge.getFilteredData(constrainedFields));
             }
             foreach (HashSet <IOperation> operations in getCombinations(totalOperations))
             {
                 bool test = constraint.test(operations);
                 if (!test)
                 {
                     // TODO:: log constraint and failed set of operations
                 }
             }
         }
     }
 }
Beispiel #3
0
        public void TestGenerics()
        {
            // Create the open class<T> which contains a List<T>

            GenericType genericClassArg = new GenericType("T");

            ClassGraph openClass = new ClassGraph();

            openClass.Name      = "OpenClass";
            openClass.Namespace = "Namespace";
            openClass.DeclaredGenericArguments.Add(genericClassArg);

            TypeSpecifier listType = TypeSpecifier.FromType(typeof(List <>));

            Assert.AreEqual(listType.GenericArguments.Count, 1);

            listType.GenericArguments[0] = genericClassArg;

            MethodGraph openMethod = new MethodGraph("OpenMethod");

            // Add open list parameter
            TypeNode listTypeNode = new TypeNode(openMethod, listType);

            openMethod.MainReturnNode.AddReturnType();
            GraphUtil.ConnectTypePins(listTypeNode.OutputTypePins[0], openMethod.MainReturnNode.InputTypePins[0]);

            GraphUtil.ConnectExecPins(openMethod.EntryNode.InitialExecutionPin, openMethod.ReturnNodes.First().ReturnPin);

            openClass.Methods.Add(openMethod);

            // Create the closed class which contains a List<string>

            ClassGraph closedClass = new ClassGraph();

            closedClass.Name      = "ClosedClass";
            closedClass.Namespace = "Namespace";

            TypeSpecifier closedListType = TypeSpecifier.FromType <string>();

            MethodGraph closedMethod = new MethodGraph("ClosedMethod");

            // Add closed list parameter
            TypeNode closedListTypeNode = new TypeNode(closedMethod, closedListType);

            closedMethod.MainReturnNode.AddReturnType();
            GraphUtil.ConnectTypePins(closedListTypeNode.OutputTypePins[0], closedMethod.MainReturnNode.InputTypePins[0]);

            GraphUtil.ConnectExecPins(closedMethod.EntryNode.InitialExecutionPin, closedMethod.ReturnNodes.First().ReturnPin);

            closedClass.Methods.Add(closedMethod);

            // Translate the classes

            ClassTranslator translator = new ClassTranslator();

            string openClassTranslated = translator.TranslateClass(openClass);

            string closedClassTranslated = translator.TranslateClass(closedClass);
        }
Beispiel #4
0
        public static void CompileNetPrintsClass(ClassGraph classGraph, string outputPath)
        {
            ClassTranslator classTranslator = new ClassTranslator();

            string translated = classTranslator.TranslateClass(classGraph);

            File.WriteAllText(outputPath, translated);
        }
Beispiel #5
0
        public static void AddEdgeToScenes(Edge edge, IList <string> sceneIds)
        {
            string fromNode = edge.From;
            string toNode   = edge.To;

            foreach (string sceneId in sceneIds)
            {
                ClassGraph sceneGraph = sceneGraphs[sceneId];
                if (sceneGraph.ContainsNode(fromNode) && sceneGraph.ContainsNode(toNode))
                {
                    sceneGraph.AddEdge(edge);
                }
            }
        }
Beispiel #6
0
        public IEnumerable <string> GetGeneratedCode()
        {
            ClassTranslator classTranslator = new ClassTranslator();

            foreach (var project in dte.Solution.Projects.OfType <EnvDTE.Project>())
            {
                foreach (var projectItem in project.ProjectItems.OfType <EnvDTE.ProjectItem>())
                {
                    string fullPath = projectItem.Properties?.Item("FullPath")?.Value as string;
                    if (fullPath != null && fullPath.EndsWith(".netpc"))
                    {
                        ClassGraph classGraph = NetPrints.Serialization.SerializationHelper.LoadClass(fullPath);
                        yield return(classTranslator.TranslateClass(classGraph));
                    }
                }
            }
        }
        public void Setup()
        {
            classTranslator = new ClassTranslator();

            cls = new ClassGraph()
            {
                Name      = "TestClass",
                Namespace = "TestNamespace",
            };

            CreateStringLengthMethod();
            CreateMainMethod();

            cls.Variables.Add(new Variable(cls, "testVariable", TypeSpecifier.FromType <string>(), null, null, VariableModifiers.None));
            cls.Methods.Add(stringLengthMethod);
            cls.Methods.Add(mainMethod);
        }
Beispiel #8
0
        /// <summary>
        /// Translates a class into C#.
        /// </summary>
        /// <param name="c">Class to translate.</param>
        /// <returns>C# code for the class.</returns>
        public string TranslateClass(ClassGraph c)
        {
            StringBuilder content = new StringBuilder();

            foreach (Variable v in c.Variables)
            {
                content.AppendLine(TranslateVariable(v));
            }

            foreach (ConstructorGraph constructor in c.Constructors)
            {
                content.AppendLine(TranslateConstructor(constructor));
            }

            foreach (MethodGraph m in c.Methods)
            {
                content.AppendLine(TranslateMethod(m));
            }

            StringBuilder modifiers = new StringBuilder();

            modifiers.Append($"{TranslatorUtil.VisibilityTokens[c.Visibility]} ");

            if (c.Modifiers.HasFlag(ClassModifiers.Static))
            {
                modifiers.Append("static ");
            }

            if (c.Modifiers.HasFlag(ClassModifiers.Abstract))
            {
                modifiers.Append("abstract ");
            }

            if (c.Modifiers.HasFlag(ClassModifiers.Sealed))
            {
                modifiers.Append("sealed ");
            }

            if (c.Modifiers.HasFlag(ClassModifiers.Partial))
            {
                modifiers.Append("partial ");
            }

            string genericArguments = "";

            if (c.DeclaredGenericArguments.Count > 0)
            {
                genericArguments = "<" + string.Join(", ", c.DeclaredGenericArguments) + ">";
            }

            string baseTypes  = string.Join(", ", c.AllBaseTypes);
            var    attributes = TranslatorUtil.TranslateAttributes(c.DefinedAttributes);

            string generatedCode = (string.IsNullOrWhiteSpace(c.Namespace) ? CLASS_TEMPLATE_NO_NAMESPACE : CLASS_TEMPLATE)
                                   .Replace("%Namespace%", c.Namespace)
                                   .Replace("%ClassModifiers%", modifiers.ToString())
                                   .Replace("%ClassName%", c.Name)
                                   .Replace("%GenericArguments%", genericArguments)
                                   .Replace("%BaseTypes%", baseTypes)
                                   .Replace("%ClassAttributes%", attributes)
                                   .Replace("%Content%", content.ToString());

            return(TranslatorUtil.FormatCode(generatedCode));
        }
 /// <summary>
 /// Saves a class to a path. The class can be loaded again using LoadClass.
 /// </summary>
 /// <param name="cls">Class to save.</param>
 /// <param name="outputPath">Path to save the class at.</param>
 public static void SaveClass(ClassGraph cls, string outputPath)
 {
     using FileStream fileStream = File.Open(outputPath, FileMode.Create);
     classSerializer.WriteObject(fileStream, cls);
 }
Beispiel #10
0
 /// <summary>
 /// Saves a class to a path. The class can be loaded again using LoadClass.
 /// </summary>
 /// <param name="cls">Class to save.</param>
 /// <param name="outputPath">Path to save the class at.</param>
 public static void SaveClass(ClassGraph cls, string outputPath)
 {
     using var fileStream = File.Open(outputPath, FileMode.Create);
     using var writer     = XmlWriter.Create(fileStream, new XmlWriterSettings { Indent = true });
     classSerializer.WriteObject(writer, cls);
 }