void WriteProperty(PropertyDefinition prop, int level) { var getterNative = prop.Getter.Native; var setterNative = prop.Setter?.Native; EnsureWhiteSpace(); Write(level + 1, $"public "); if (getterNative.IsStatic) { Write("static "); } WriteLine($"{GetTypeNameCS(prop.Type)} {prop.Name}"); WriteLine(level + 1, "{"); if (prop.Parent.CachedProperties.ContainsKey(prop.Name)) { var cachedProperty = prop.Parent.CachedProperties[prop.Name]; WriteLine(level + 2, $"get {{ return {cachedProperty.CacheFieldName}; }}"); if (setterNative != null) { WriteLine(level + 2, "set"); WriteLine(level + 2, "{"); Write(level + 3, $"{GetFullNameC(prop.Parent.Native)}_{setterNative.Name}("); if (!setterNative.IsStatic) { Write("_native, "); } WriteLine($"{GetTypeSetterCSMarshal(prop.Type)});"); WriteLine(level + 3, $"{cachedProperty.CacheFieldName} = value;"); WriteLine(level + 2, "}"); } } else { var type = prop.Type; if ((type.Target != null && type.Target.MarshalAsStruct) || (type.Kind == TypeKind.LValueReference && type.Referenced.Target != null && type.Referenced.Target.MarshalAsStruct)) { WriteLine(level + 2, "get"); WriteLine(level + 2, "{"); WriteLine(level + 3, $"{GetTypeNameCS(type)} value;"); Write(level + 3, $"{GetFullNameC(prop.Parent.Native)}_{getterNative.Name}("); if (!getterNative.IsStatic) { Write("_native, "); } WriteLine("out value);"); WriteLine(level + 3, "return value;"); WriteLine(level + 2, "}"); } else { string objPtr = getterNative.IsStatic ? "" : "_native"; WriteLine(level + 2, string.Format("get {{ return {0}{1}_{2}({3}){4}; }}", BulletParser.GetTypeMarshalConstructorStartCS(getterNative), GetFullNameC(prop.Parent.Native), getterNative.Name, objPtr, BulletParser.GetTypeMarshalConstructorEndCS(getterNative))); } if (setterNative != null) { string marshal = GetTypeSetterCSMarshal(prop.Type); Write(level + 2, $"set {{ {GetFullNameC(prop.Parent.Native)}_{setterNative.Name}("); if (!setterNative.IsStatic) { Write("_native, "); } WriteLine($"{marshal}); }}"); } } WriteLine(level + 1, "}"); hasCSWhiteSpace = false; }
void WriteMethodDefinition(ManagedMethod method, int numParameters, int overloadIndex, int level, ManagedParameter outValueParameter) { var nativeMethod = method.Native; if (nativeMethod.IsConstructor) { if (method.Parent.BaseClass == null) { Write("_native = "); } Write($"{GetFullNameC(method.Parent.Native)}_new"); } else { if (!nativeMethod.IsVoid) { if (outValueParameter != null) { // Temporary variable WriteLine(string.Format("{0} {1};", DotNetParser.GetManaged(outValueParameter.Native.Type.Referenced.Target).Name, outValueParameter.Name)); WriteTabs(level + 2); } else { Write($"return {BulletParser.GetTypeMarshalConstructorStartCS(nativeMethod)}"); } } Write($"{GetFullNameC(method.Parent.Native)}_{method.Native.Name}"); } if (overloadIndex != 0) { Write((overloadIndex + 1).ToString()); } Write('('); var parameters = method.Parameters.Take(numParameters) .Select(p => GetParameterMarshal(p)); if (outValueParameter != null) { parameters = parameters.Concat(new[] { $"out {outValueParameter.Name }" }); } // The first parameter is the instance pointer (if not constructor or static method) if (!nativeMethod.IsConstructor && !nativeMethod.IsStatic) { parameters = new[] { "_native" }.Concat(parameters); } Write(ListToLines(parameters, WriteTo.CS, level + 2)); if (nativeMethod.IsConstructor && method.Parent.BaseClass != null) { Write(")"); if (method.Parent.BaseClass.Native.HasPreventDelete) { Write(", false"); } WriteLine(")"); WriteLine(level + 1, "{"); } else { if (!nativeMethod.IsConstructor && !nativeMethod.IsVoid) { Write(BulletParser.GetTypeMarshalConstructorEndCS(nativeMethod)); } WriteLine(");"); } // Cache property values if (nativeMethod.IsConstructor) { var methodParent = method.Parent; while (methodParent != null) { foreach (var cachedProperty in methodParent.CachedProperties.OrderBy(p => p.Key)) { foreach (var param in method.Parameters) { if (param.Name.ToLower().Equals(cachedProperty.Key.ToLower()) && GetName(param.Native.Type).Equals(GetName(cachedProperty.Value.Property.Type))) { WriteLine(level + 2, $"{cachedProperty.Value.CacheFieldName} = {param.Name};"); } } } methodParent = methodParent.BaseClass; } } // Return temporary variable if (outValueParameter != null) { WriteLine(level + 2, $"return {outValueParameter.Name};"); } }