//TODO might just be a string... protected override void Start() { base.Start(); //on start go grab the correct information from the appmodel's loadedFunctions dict // use the name of this type as the key into the dictionary funcdef = GameObject.FindObjectOfType <AppModel>().LoadedFunctions[this.GetType().FullName]; //on start add a correctly named input port for each //parameter in methodinfo that we've loaded foreach (var param in funcdef.Parameters) { AddInputPort(param.Name); } //add 1 output, 1 start, and end trigger AddExecutionInputPort("start"); //TODO support multiout attribute AddOutPutPort("OUTPUT"); AddExecutionOutPutPort("done"); CodePointer = CompiledNodeEval; Evaluator = this.gameObject.AddComponent <CsharpEvaluator>(); }
private void AppendMethod(StringBuilder strBuilder, FunctionDescription functionDescription) { string methodStr = "public "; methodStr += functionDescription.ReturnType + " "; methodStr += functionDescription.Name + "("; if (functionDescription.Arguments.ToList().Count != 0) { int argNum = 0; foreach (var argType in functionDescription.Arguments) { methodStr += argType + $" arg{argNum}, "; argNum++; } int lastCommaIdx = methodStr.LastIndexOf(","); if (lastCommaIdx >= 0) { methodStr = methodStr.Substring(0, lastCommaIdx); } } methodStr += ")"; AppendLine(strBuilder, methodStr); OpenBracket(strBuilder); AppendLine(strBuilder, "throw new NotImplementedException();"); CloseBracket(strBuilder); }
/// <summary> /// Public translation interface. /// Translates the given method to GLSL /// </summary> /// <param name="s">Shader type definition.</param> /// <param name="m">A method representing a shader to translate.</param> /// <param name="attr">The shader type as attribute (either FragmentShaderAttribute or VertexShaderAttribute</param> /// <param name="type">The shader type as ShaderType</param> /// <returns>The translated GLSL shader source</returns> public FunctionDescription Transform(TypeDefinition s, MethodDefinition m, CustomAttribute attr, ShaderType type) { if (s == null) { throw new ArgumentNullException("s"); } if (m == null) { throw new ArgumentNullException("m"); } if (attr == null) { throw new ArgumentNullException("attr"); } var ctx = new DecompilerContext(s.Module) { CurrentType = s, CurrentMethod = m, CancellationToken = CancellationToken.None }; var d = AstMethodBodyBuilder.CreateMethodBody(m, ctx); var glsl = new GlslVisitor(d, attr, ctx); _functions.UnionWith(glsl.Functions); var sig = GlslVisitor.GetSignature(m); var entry = (bool)attr.ConstructorArguments.FirstOrDefault().Value; var code = glsl.Result; var desc = new FunctionDescription(Shader.GetMethodName(m), sig + code, entry, type); _dependencies.UnionWith(glsl.Dependencies); return(desc); }
/// <summary> /// Enumerate the static methods of all types in an assembly, create nodemodels pointing to these methods /// and add them to the appmodels's /// dictionaries(this will populate the searchview). Internally catches exceptions and sends the error /// to the console. /// </summary> /// <Returns>The list of node types which are constructed from the methods loaded from this assembly</Returns> /// //TODO possibly change this back to using typeloadData or simplified version to embed search tags, name, etc in search view and appmodel easily public override List <Type> LoadNodesFromAssembly( Assembly assembly) { Debug.Log("inside load nodes from specific assembly: " + assembly.FullName); var nodeModelTypes = new List <Type>(); if (assembly == null) { throw new ArgumentNullException("assembly"); } Type[] loadedTypes = null; var loadedMethodDict = new Dictionary <Type, List <MethodInfo> >(); try { loadedTypes = assembly.GetTypes(); } catch (ReflectionTypeLoadException e) { Debug.Log("Could not load types."); Debug.LogException(e); foreach (var ex in e.LoaderExceptions) { Debug.Log("Dll Load Exception:"); Debug.Log(ex.ToString()); } } catch (Exception e) { Debug.Log("Could not load types."); Debug.LogException(e); } foreach (var t in (loadedTypes ?? Enumerable.Empty <Type>())) { try { //load all declared,static,public methods on the type loadedMethodDict[t] = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static).ToList(); //now we need to build a nodemodel type that represents each method foreach (var method in loadedMethodDict[t]) { //http://stackoverflow.com/questions/9053440/create-type-at-runtime-that-inherits-an-abstract-class-and-implements-an-interfa AssemblyName asmName = new AssemblyName("ZeroTouchWrappers"); string typename = t.FullName; AssemblyBuilder asmbuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); ModuleBuilder modulebuilder = asmbuilder.DefineDynamicModule("loadedlib"); TypeBuilder typebuilder = modulebuilder.DefineType(typename + method.Name + "Node"); typebuilder.SetParent(typeof(ZTwrapperNode)); Type ztnode = typebuilder.CreateType(); nodeModelTypes.Add(ztnode); //define a function descriptor that wraps the needed parameters, type, and method for a specific node var currentFunc = new FunctionDescription(method.GetParameters().ToList(), method, t, ztnode); //push this into the functions dictionary for this loader functions.Add(ztnode.FullName, currentFunc); } //TODO need to inject the methodinfo,typeinfo,and parameter info somehow or the type is really nothing.... } catch (Exception e) { Debug.Log("Failed to load type from " + assembly.FullName); Debug.Log("The type was " + t.FullName); Debug.LogException(e); } } return(nodeModelTypes); }
/// <summary> /// Public translation interface. /// Translates the given method to HLSL /// </summary> /// <param name="s">Shader type definition.</param> /// <param name="m">A method representing a shader to translate.</param> /// <param name="attr">The shader type as attribute (either FragmentShaderAttribute or VertexShaderAttribute</param> /// <param name="type">The shader type as ShaderType</param> /// <returns>The translated GLSL shader source</returns> public FunctionDescription Transform(TypeDefinition s, MethodDefinition m, CustomAttribute attr, ShaderType type) { if (s == null) { throw new ArgumentNullException("s"); } if (m == null) { throw new ArgumentNullException("m"); } if (attr == null) { throw new ArgumentNullException("attr"); } var sbase = s.BaseType.Resolve(); while (sbase.MetadataToken.ToInt32() != typeof(Shader).MetadataToken) { sbase = sbase.BaseType.Resolve(); } var dctx = new DecompilerContext(s.Module) { CurrentType = s, CurrentMethod = m, CancellationToken = CancellationToken.None }; var d = AstMethodBodyBuilder.CreateMethodBody(m, dctx); //var ctx = new CecilTypeResolveContext(sbase.Module); var loader = new CecilLoader(); var mscorlib = loader.LoadAssemblyFile(typeof(object).Assembly.Location); var slsharp = loader.LoadAssembly(sbase.Module.Assembly); var project = loader.LoadAssembly(s.Module.Assembly); var ctx = new CompositeTypeResolveContext(new[] { project, slsharp, mscorlib }); var resolver = new CSharpResolver(ctx, CancellationToken.None) { UsingScope = new UsingScope(project) }; /* * foreach (var v in m.Body.Variables) * { * resolver.AddVariable(v.VariableType, null, v.Name) * }*/ //resolver.AddVariable() //resolver.LocalVariables = m.Body.Variables; // TODO: need a more sane way to get the correct class + member var ss = ctx.GetAllTypes().First(c => c.FullName == s.FullName); resolver.CurrentTypeDefinition = ss; resolver.CurrentMember = ss.Methods.First(n => SameMethod(m, n, ctx)); var rv = new ResolveVisitor(resolver, new ParsedFile("memory", resolver.UsingScope), null); var glsl = new HlslVisitor(d, attr, rv, dctx); _functions.UnionWith(glsl.Functions); var entry = (bool)attr.ConstructorArguments.FirstOrDefault().Value; var sig = HlslVisitor.GetSignature(m); var code = glsl.Result; var desc = new FunctionDescription(Shader.GetMethodName(m), sig + code, entry, type); _dependencies.UnionWith(glsl.Dependencies); return(desc); }