Пример #1
0
 static void ApplyTemplate(ApiModel item, string outputDirectory, TemplateEngine templateEngine, string templateName)
 {
     var outputPath = Path.Combine(outputDirectory, TemplateCallbacks.GetApiClassName(item) + ".cs");
     if (File.Exists(outputPath))
         File.Delete(outputPath);
     File.WriteAllText(outputPath, templateEngine.ApplyByName(templateName, item));
 }
Пример #2
0
        public static string EnumItem(TemplateEngine engine, object source)
        {
            dynamic s = source;
            if (s.Name.EndsWith("FORCE_DWORD") || s.Name.EndsWith("FORCE_UINT"))
                return string.Empty;

            return s.Name + " = " + s.Value + ",";
        }
Пример #3
0
 static void ApplyTemplate(IEnumerable<TypeModel> items, string outputDirectory, TemplateEngine templateEngine, string templateName)
 {
     foreach (var item in items)
     {
         var outputPath = Path.Combine(outputDirectory, item.Name + ".cs");
         if (File.Exists(outputPath))
             File.Delete(outputPath);
         File.WriteAllText(outputPath, templateEngine.ApplyByName(templateName, item));
     }
 }
Пример #4
0
        public static string FunctionPrologue(TemplateEngine engine, object source)
        {
            var method  = (FunctionModel)source;
            var builder = new StringBuilder();

            foreach (var parameter in method.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendLine(formatter.GetLocalVariableSetupCode(marshaller, parameter));
            }

            return(builder.ToString());
        }
Пример #5
0
        public static string FunctionEpilogue(TemplateEngine engine, object source)
        {
            var method = (FunctionModel)source;
            var builder = new StringBuilder();

            foreach (var parameter in method.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendLine(formatter.GetLocalVariableCleanupCode(parameter));
            }

            if (method.Type != ApiModel.VoidModel)
                builder.Append("return _result;");

            return builder.ToString();
        }
Пример #6
0
        public static string FunctionEpilogue(TemplateEngine engine, object source)
        {
            var method  = (FunctionModel)source;
            var builder = new StringBuilder();

            foreach (var parameter in method.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendLine(formatter.GetLocalVariableCleanupCode(parameter));
            }

            if (method.Type != ApiModel.VoidModel)
            {
                builder.Append("return _result;");
            }

            return(builder.ToString());
        }
Пример #7
0
        public static string FunctionParameters(TemplateEngine engine, object source)
        {
            var function = (FunctionModel)source;
            var builder  = new StringBuilder();

            for (var parameterIndex = 0; parameterIndex < function.Parameters.Count; ++parameterIndex)
            {
                var parameter = function.Parameters[parameterIndex];
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.Append(formatter.GetFormalParameterCode(parameter));

                if (parameterIndex < function.Parameters.Count - 1)
                {
                    builder.Append(", ");
                }
            }

            return(builder.ToString());
        }
Пример #8
0
        public static string FunctionTrampoline(TemplateEngine engine, object source)
        {
            var function = (FunctionModel)source;
            var builder  = new StringBuilder();

            string trampolineSuffix = string.Empty;

            if (function.Type != ApiModel.VoidModel)
            {
                var methodTypeName   = function.Type.Name;
                var translationModel = function.Type as TranslationModel;
                if (translationModel != null)
                {
                    var type = Type.GetType(translationModel.TargetType);
                    methodTypeName   = type.FullName;
                    trampolineSuffix = type.Name;
                }

                builder.AppendFormat("{0} _result = ", methodTypeName);
            }

            var method = function as MethodModel;
            var api    = function.Api;

            if (method != null)
            {
                builder.AppendFormat("{0}.Trampoline.CallInstance{1}(System.IntPtr.Size * {2}, NativePointer", api.Name, trampolineSuffix, method.Index);
            }
            else
            {
                builder.AppendFormat("{0}.Trampoline.CallFree{1}(functions[{2}]", api.Name, trampolineSuffix, api.Functions.IndexOf(function));
            }

            foreach (var parameter in function.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendFormat(", {0}", formatter.GetTrampolineParameterCode(parameter));
            }

            builder.Append(");");
            return(builder.ToString());
        }
Пример #9
0
        public static string StructureMemberMarshallerReleaseStatement(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
            case MarshalBehavior.String:
                builder.AppendFormat("System.Runtime.InteropServices.Marshal.FreeHGlobal({0});", member.Name);
                break;

            case MarshalBehavior.Structure:
                builder.AppendFormat("{0}.Release();", member.Name);
                break;

            default:
                break;
            }

            return(builder.ToString());
        }
Пример #10
0
        public static string StructureMemberMarshallerDeclaration(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
            case MarshalBehavior.String:
                builder.AppendFormat("public System.IntPtr {0};", member.Name);
                break;

            case MarshalBehavior.Structure:
                builder.AppendFormat("public {0}Marshaller {1};", GetQualifiedName(engine, member.Type), member.Name);
                break;

            default:
                builder.AppendFormat("public {0} {1};", GetQualifiedName(engine, member.Type), member.Name);
                break;
            }

            return(builder.ToString());
        }
Пример #11
0
        public static string MemberFromMarshallerAssignment(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
            case MarshalBehavior.String:
                builder.AppendFormat("result.{0} = new string((sbyte*)source.{0});", member.Name);
                break;

            case MarshalBehavior.Structure:
                builder.AppendFormat("result.{0} = {1}.FromMarshaller(source.{0});", member.Name, GetQualifiedName(engine, member.Type));
                break;

            default:
                builder.AppendFormat("result.{0} = source.{0};", member.Name);
                break;
            }

            return(builder.ToString());
        }
Пример #12
0
        public static string MemberToMarshallerAssignment(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
            case MarshalBehavior.String:
                builder.AppendFormat("result.{0} = source.{0} != null ? System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(source.{0}) : System.IntPtr.Zero;", member.Name);
                break;

            case MarshalBehavior.Structure:
                builder.AppendFormat("result.{0} = {1}.ToMarshaller(source.{0});", member.Name, GetQualifiedName(engine, member.Type));
                break;

            default:
                builder.AppendFormat("result.{0} = source.{0};", member.Name);
                break;
            }

            return(builder.ToString());
        }
Пример #13
0
        static void RunGenerator(ApiModel api, Configuration configuration)
        {
            var templateDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Templates");
            var templateEngine    = new TemplateEngine(new[] { templateDirectory });

            templateEngine.RegisterCallback("ApiNamespace", (e, s) => api.Name);
            templateEngine.RegisterCallback("ApiName", (e, s) => TemplateCallbacks.GetApiClassName(api));
            templateEngine.RegisterCallback("ApiDll", (e, s) => TemplateCallbacks.GetApiDllName(api));
            templateEngine.RegisterCallbacks(typeof(TemplateCallbacks));

            var outputDirectory = configuration.GeneratorOutputPath;

            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            ApplyTemplate(api, outputDirectory, templateEngine, "Api");
            ApplyTemplate(api.Enumerations, outputDirectory, templateEngine, "Enumeration");
            ApplyTemplate(api.Structures, outputDirectory, templateEngine, "Structure");
            ApplyTemplate(api.Interfaces, outputDirectory, templateEngine, "Interface");

            BuildTrampolineAssembly(api, outputDirectory, string.Format("{0}.Trampoline", api.Name));
        }
Пример #14
0
        public static string FunctionParameters(TemplateEngine engine, object source)
        {
            var function = (FunctionModel)source;
            var builder = new StringBuilder();

            for (var parameterIndex = 0; parameterIndex < function.Parameters.Count; ++parameterIndex)
            {
                var parameter = function.Parameters[parameterIndex];
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.Append(formatter.GetFormalParameterCode(parameter));

                if (parameterIndex < function.Parameters.Count - 1)
                    builder.Append(", ");
            }

            return builder.ToString();
        }
Пример #15
0
        public static string StructureMemberMarshallerReleaseStatement(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
                case MarshalBehavior.String:
                    builder.AppendFormat("System.Runtime.InteropServices.Marshal.FreeHGlobal({0});", member.Name);
                    break;
                case MarshalBehavior.Structure:
                    builder.AppendFormat("{0}.Release();", member.Name);
                    break;
                default:
                    break;
            }

            return builder.ToString();
        }
Пример #16
0
        public static string StructureMemberMarshallerDeclaration(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
                case MarshalBehavior.String:
                    builder.AppendFormat("public System.IntPtr {0};", member.Name);
                    break;
                case MarshalBehavior.Structure:
                    builder.AppendFormat("public {0}Marshaller {1};", GetQualifiedName(engine, member.Type), member.Name);
                    break;
                default:
                    builder.AppendFormat("public {0} {1};", GetQualifiedName(engine, member.Type), member.Name);
                    break;
            }

            return builder.ToString();
        }
Пример #17
0
        public static string MemberToMarshallerAssignment(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
                case MarshalBehavior.String:
                    builder.AppendFormat("result.{0} = source.{0} != null ? System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(source.{0}) : System.IntPtr.Zero;", member.Name);
                    break;
                case MarshalBehavior.Structure:
                    builder.AppendFormat("result.{0} = {1}.ToMarshaller(source.{0});", member.Name, GetQualifiedName(engine, member.Type));
                    break;
                default:
                    builder.AppendFormat("result.{0} = source.{0};", member.Name);
                    break;
            }

            return builder.ToString();
        }
Пример #18
0
        public static string MemberFromMarshallerAssignment(TemplateEngine engine, object source)
        {
            StructureMemberModel member = (StructureMemberModel)source;
            var builder = new StringBuilder();

            switch (marshaller.ResolveBehavior(member))
            {
                case MarshalBehavior.String:
                    builder.AppendFormat("result.{0} = new string((sbyte*)source.{0});", member.Name);
                    break;
                case MarshalBehavior.Structure:
                    builder.AppendFormat("result.{0} = {1}.FromMarshaller(source.{0});", member.Name, GetQualifiedName(engine, member.Type));
                    break;
                default:
                    builder.AppendFormat("result.{0} = source.{0};", member.Name);
                    break;
            }

            return builder.ToString();
        }
Пример #19
0
 public static string GetQualifiedName(TemplateEngine engine, object source)
 {
     var type = (TypeModel)source;
     if (type.Api != null)
         return string.Format("{0}.{1}", type.Api.Name, type.Name);
     return type.Name;
 }
Пример #20
0
        public static string FunctionTrampoline(TemplateEngine engine, object source)
        {
            var function = (FunctionModel)source;
            var builder = new StringBuilder();

            string trampolineSuffix = string.Empty;
            if (function.Type != ApiModel.VoidModel)
            {
                var methodTypeName = function.Type.Name;
                var translationModel = function.Type as TranslationModel;
                if (translationModel != null)
                {
                    var type = Type.GetType(translationModel.TargetType);
                    methodTypeName = type.FullName;
                    trampolineSuffix = type.Name;
                }

                builder.AppendFormat("{0} _result = ", methodTypeName);
            }

            var method = function as MethodModel;
            var api = function.Api;
            if (method != null)
                builder.AppendFormat("{0}.Trampoline.CallInstance{1}(System.IntPtr.Size * {2}, NativePointer", api.Name, trampolineSuffix, method.Index);
            else
                builder.AppendFormat("{0}.Trampoline.CallFree{1}(functions[{2}]", api.Name, trampolineSuffix, api.Functions.IndexOf(function));

            foreach (var parameter in function.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendFormat(", {0}", formatter.GetTrampolineParameterCode(parameter));
            }

            builder.Append(");");
            return builder.ToString();
        }
Пример #21
0
        static void RunGenerator(ApiModel api, Configuration configuration)
        {
            var templateDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Templates");
            var templateEngine = new TemplateEngine(new[] { templateDirectory });

            templateEngine.RegisterCallback("ApiNamespace", (e, s) => api.Name);
            templateEngine.RegisterCallback("ApiName", (e, s) => TemplateCallbacks.GetApiClassName(api));
            templateEngine.RegisterCallback("ApiDll", (e, s) => TemplateCallbacks.GetApiDllName(api));
            templateEngine.RegisterCallbacks(typeof(TemplateCallbacks));

            var outputDirectory = configuration.GeneratorOutputPath;
            if (!Directory.Exists(outputDirectory))
                Directory.CreateDirectory(outputDirectory);

            ApplyTemplate(api, outputDirectory, templateEngine, "Api");
            ApplyTemplate(api.Enumerations, outputDirectory, templateEngine, "Enumeration");
            ApplyTemplate(api.Structures, outputDirectory, templateEngine, "Structure");
            ApplyTemplate(api.Interfaces, outputDirectory, templateEngine, "Interface");

            BuildTrampolineAssembly(api, outputDirectory, string.Format("{0}.Trampoline", api.Name));
        }
Пример #22
0
 static void ApplyTemplate(IEnumerable <TypeModel> items, string outputDirectory, TemplateEngine templateEngine, string templateName)
 {
     foreach (var item in items)
     {
         var outputPath = Path.Combine(outputDirectory, item.Name + ".cs");
         if (File.Exists(outputPath))
         {
             File.Delete(outputPath);
         }
         File.WriteAllText(outputPath, templateEngine.ApplyByName(templateName, item));
     }
 }
Пример #23
0
        public static string FunctionPrologue(TemplateEngine engine, object source)
        {
            var method = (FunctionModel)source;
            var builder = new StringBuilder();

            foreach (var parameter in method.Parameters)
            {
                var formatter = formatters[marshaller.ResolveBehavior(parameter)];
                builder.AppendLine(formatter.GetLocalVariableSetupCode(marshaller, parameter));
            }

            return builder.ToString();
        }