예제 #1
0
        public static string Marshal(EngineFunction.Argument arg)
        {
            StringBuilder result = new StringBuilder();

            string name = SanitizeNameContainer.SanitizeName(arg.Name);

            switch (arg.Type)
            {
            case EngineClass @class:
                result.Append($"{name}.ObjectPtr");
                break;

            case EngineEnum @enum:
                result.Append($"(int){name}");
                break;

            case EngineStruct @struct:
                //result.Write($"GenericMarshal.ToPtr({arg.Name}");
                result.Append($"{name}.internalStructPtr");
                //result.Write($"StructMarshal.StructToIntPtr(({arg.Name}");
                //result.Write($"ref ({arg.Name}");
                //if (arg.DefaultValue != null) {
                //    result.Write($" ?? new {@struct.Name}(\"{arg.DefaultValue}\")");
                //}
                //result.Write(").internalStruct)");
                break;

            default:
                result.Append($"{name}");
                break;
            }

            return(result.ToString().Trim());
        }
        public static string GetManagedParameters(EngineFunction function)
        {
            StringBuilder result = new StringBuilder();

            int firstArgIndex = function.IsStatic ? 0 : 1;

            for (var index = firstArgIndex; index < function.Arguments.Count; index++)
            {
                EngineFunction.Argument functionArgument = function.Arguments[index];
                result.Append(functionArgument.Type.ManagedType);
                if (functionArgument.Type is EngineStruct @struct &&
                    functionArgument.DefaultValue != null)
                {
                    //result.Write("?");
                }

                result.Append($" {SanitizeNameContainer.SanitizeName(functionArgument.Name, false)}");
                if (functionArgument.DefaultValue != null)
                {
                    result.Append(DefaultParamValueContainer.DefaultParamValue(functionArgument));
                }

                if (index != function.Arguments.Count - 1)
                {
                    result.Append(", ");
                }
            }

            return(result.ToString().Trim());
        }
예제 #3
0
        public static string StructFieldAlloc(TemplateContext context, EngineStruct.Field field)
        {
            string name_cC = SanitizeNameContainer.SanitizeName(field.Name, false);
            string name_CC = SanitizeNameContainer.SanitizeName(field.Name, true);

            StringBuilder result = new StringBuilder();

            switch (field.Type)
            {
            case EngineStruct @struct:
                result.AppendLine($"{name_CC}.Alloc();");
                result.AppendLine($"internalStruct.{name_cC} = {name_CC}.internalStruct;");
                break;

            case EngineClass @class:
                result.AppendLine($"internalStruct.{name_cC} = {name_CC}.ObjectPtr;");
                break;

            case EnginePrimitive @primitive when @primitive.NativeReturnType.Equals("IntPtr"):
                result.AppendLine($"internalStruct.{name_cC} = GenericMarshal.ToPtr({name_CC});");

                break;

            case EnginePrimitive @primitive:
                result.AppendLine($"internalStruct.{name_cC} = {name_CC};");
                break;
            }

            int indentation = Regex.Split(context.Output.ToString(), @"\r?\n|\r")
                              .ToList()
                              .Last()
                              .Length;

            return(IndentContainer.PrefixLines(result.ToString().Trim(), " ", indentation));
        }
예제 #4
0
        public static string Free(EngineFunction.Argument arg)
        {
            StringBuilder result = new StringBuilder();

            if (arg.Type is EngineStruct @struct)
            {
                result.Append($"{SanitizeNameContainer.SanitizeName(arg.Name, false)}.Free();");
            }

            return(result.ToString().Trim());
        }
예제 #5
0
        public static string StructFieldFree(TemplateContext context, EngineStruct.Field field)
        {
            string name_cC  = SanitizeNameContainer.SanitizeName(field.Name, false);
            string name_CC  = SanitizeNameContainer.SanitizeName(field.Name, true);
            string baseType = field.Type.ManagedType;

            if (baseType.StartsWith("ptr_"))
            {
                baseType = baseType.Substring(4);
            }

            StringBuilder result = new StringBuilder();

            switch (field.Type)
            {
            case EngineStruct @struct:
                result.AppendLine($"{name_CC}?.Free();");
                result.AppendLine($"{name_CC} = new {@struct.ManagedType}(internalStruct.{name_cC});");
                break;

            case EngineClass @class:
                result.AppendLine($"{name_CC}.ObjectPtr = internalStruct.{name_cC};");
                break;

            case EnginePrimitive @primitive when @primitive.NativeReturnType.Equals("IntPtr"):
                baseType = baseType.Substring(0, baseType.Length - 2);

                if (field.IndexedSize > 1)
                {
                    result.AppendLine($"{name_CC} = GenericMarshal.FromPtr<{baseType}>(internalStruct.{name_cC}, {field.IndexedSize}, true);");
                }
                else
                {
                    result.AppendLine($"{name_CC} = GenericMarshal.FromPtr<{baseType}>(internalStruct.{name_cC}, ElementCount, true);");
                }
                break;

            case EnginePrimitive @primitive:
                result.AppendLine($"{name_CC} = internalStruct.{name_cC};");
                break;
            }

            int indentation = Regex.Split(context.Output.ToString(), @"\r?\n|\r")
                              .ToList()
                              .Last()
                              .Length;

            return(IndentContainer.PrefixLines(result.ToString().Trim(), " ", indentation));
        }
예제 #6
0
        public static string Alloc(EngineFunction.Argument arg)
        {
            StringBuilder result = new StringBuilder();

            if (arg.Type is EngineStruct @struct)
            {
                string argName = SanitizeNameContainer.SanitizeName(arg.Name, false);
                if (arg.DefaultValue != null)
                {
                    result.AppendLine($"{argName} = {argName} ?? new {@struct.Name}(\"{arg.DefaultValue}\");");
                }

                result.Append($"{argName}.Alloc();");
            }

            return(result.ToString().Trim());
        }
        public static string DefaultParamValue(EngineFunction.Argument arg)
        {
            if (arg.DefaultValue == null)
            {
                return("");
            }


            if (arg.Type.ManagedType.Equals("string"))
            {
                return($" = \"{arg.DefaultValue}\"");
            }

            if (arg.Type is EnginePrimitive @primitive)
            {
                string result = "";
                result += $" = {arg.DefaultValue}";
                if (@primitive.ManagedType == "float" &&
                    !arg.DefaultValue.EndsWith("f"))
                {
                    result += "f";
                }

                return(result);
            }

            if (arg.Type is EngineStruct @struct)
            {
                return($" = null");
            }

            if (arg.Type is EngineClass @class)
            {
                if (arg.DefaultValue.Equals("null"))
                {
                    return($" = null");
                }
            }

            if (arg.Type is EngineEnum @enum)
            {
                return($" = {@enum.Name}.{SanitizeNameContainer.SanitizeName(arg.DefaultValue, true)}");
            }

            return("<<Unhandled defaultValue type.>>");
        }