private void WriteFunction(CodeParser.Class clss, CodeParser.Class.Function func) { var returnType = BaseType.Parse(func.ReturnType); returnType.Func = func.Name; var args = func.Arguments.Select(x => { var bt = BaseType.Parse(x.Value, x.Key); bt.Func = func.Name; return(bt); }).ToArray(); for (int i = 0; i < args.Length; i++) { if (args[i] is StringType) { if (args[i + 1] is IntType || args[i + 1] is UIntType) { if (args[i + 1].Ref == string.Empty) { args[i + 1] = new ConstValueType(args[i + 1], "(1024 * 32)"); } } else { throw new System.Exception($"String Builder Next Type Is {args[i+1].GetType()}"); } } } var argstr = string.Join(", ", args.Where(x => !x.ShouldSkipAsArgument).Select(x => x.AsArgument()));; var delegateargstr = string.Join(", ", args.Select(x => x.AsNativeArgument())); var windowsSpecific = NeedsWindowsSpecificFunction(func, returnType, args); if (returnType is SteamApiCallType sap) { sap.CallResult = func.CallResult; argstr = string.Join(", ", args.Select(x => x.AsArgument().Replace("ref ", " /* ref */ "))); } WriteLine($"#region FunctionMeta"); WriteLine($"[UnmanagedFunctionPointer( Platform.MemberConvention )]"); if (returnType.ReturnAttribute != null) { WriteLine(returnType.ReturnAttribute); } if (returnType.IsReturnedWeird) { WriteLine("#if PLATFORM_WIN"); WriteLine($"private delegate void F{func.Name}( IntPtr self, ref {returnType.TypeName} retVal, {delegateargstr} );".Replace(" retVal, )", " retVal )")); WriteLine("#else"); } WriteLine($"private delegate {returnType.TypeNameFrom} F{func.Name}( IntPtr self, {delegateargstr} );".Replace("( IntPtr self, )", "( IntPtr self )")); if (returnType.IsReturnedWeird) { WriteLine("#endif"); } WriteLine($"private F{func.Name} _{func.Name};"); WriteLine(); WriteLine($"#endregion"); StartBlock($"internal {returnType.ReturnType} {func.Name}( {argstr} )".Replace("( )", "()")); { var callargs = string.Join(", ", args.Select(x => x.AsCallArgument())); // // Code before any calls // foreach (var arg in args) { if (arg is StringType sb) { WriteLine($"IntPtr mem{sb.VarName} = Helpers.TakeMemory();"); } } // // The actual call // if (returnType.IsReturnedWeird) { WriteLine("#if PLATFORM_WIN"); { WriteLine($"var retVal = default( {returnType.TypeName} );"); WriteLine($"_{func.Name}( Self, ref retVal, {callargs} );".Replace(", );", " );")); WriteLine($"{returnType.Return( "retVal" )}"); } WriteLine("#else"); } if (returnType.IsVoid) { WriteLine($"_{func.Name}( Self, {callargs} );".Replace("( Self, )", "( Self )")); } else { WriteLine($"var returnValue = _{func.Name}( Self, {callargs} );".Replace("( Self, )", "( Self )")); } // // Code after the call // foreach (var arg in args) { if (arg is StringType sb) { WriteLine($"{sb.VarName} = Helpers.MemoryToString( mem{sb.VarName} );"); } } // // Return // if (!returnType.IsVoid) { WriteLine(returnType.Return("returnValue")); } if (returnType.IsReturnedWeird) { WriteLine("#endif"); } } EndBlock(); }
public ConstValue(uint code, IntPtr loc, uint locSize) { switch ((CorElementType)code) { case CorElementType.BOOLEAN: this.valueType = ConstValueType.BOOL; this.boolVal = Decoder.ReadBoolean(loc); break; case CorElementType.I1: this.valueType = ConstValueType.I1; this.sbyteVal = Decoder.ReadSByte(loc); break; case CorElementType.U1: this.valueType = ConstValueType.U1; this.byteVal = Decoder.ReadByte(loc); break; case CorElementType.CHAR: this.valueType = ConstValueType.CHAR; this.charVal = Decoder.ReadChar(loc); break; case CorElementType.I2: this.valueType = ConstValueType.I2; this.int16Val = Decoder.ReadInt16(loc); break; case CorElementType.U2: this.valueType = ConstValueType.U2; this.uint16Val = Decoder.ReadUInt16(loc); break; case CorElementType.I4: this.valueType = ConstValueType.I4; this.int32Val = Decoder.ReadInt32(loc); break; case CorElementType.U4: this.valueType = ConstValueType.U4; this.uint32Val = Decoder.ReadUInt32(loc); break; case CorElementType.I8: this.valueType = ConstValueType.I8; this.int64Val = Decoder.ReadInt64(loc); break; case CorElementType.U8: this.valueType = ConstValueType.U8; this.uint64Val = Decoder.ReadUInt64(loc); break; case CorElementType.R4: this.valueType = ConstValueType.R4; this.singleVal = Decoder.ReadFloat32(loc); break; case CorElementType.R8: this.valueType = ConstValueType.R8; this.doubleVal = Decoder.ReadFloat64(loc); break; case CorElementType.STRING: this.valueType = ConstValueType.STRING; this.stringVal = Decoder.ReadString(loc, (int)locSize); break; case CorElementType.CLASS: this.valueType = ConstValueType.CLASS; break; default: throw new System.ArgumentException("Incorrect element type 0x" + code.ToString("x") + " in default value"); } }