Refactor API_INJECT_CPP_CODE into API_INJECT_CODE to support code injection in other languages
This commit is contained in:
@@ -52,6 +52,6 @@
|
||||
#define API_EVENT(...)
|
||||
#define API_PARAM(...)
|
||||
#define API_TYPEDEF(...)
|
||||
#define API_INJECT_CPP_CODE(...)
|
||||
#define API_INJECT_CODE(...)
|
||||
#define API_AUTO_SERIALIZATION(...) public: void Serialize(SerializeStream& stream, const void* otherObj) override; void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;
|
||||
#define DECLARE_SCRIPTING_TYPE_MINIMAL(type) public: friend class type##Internal; static struct ScriptingTypeInitializer TypeInitializer;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "Engine/Core/Types/StringView.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Clipboard.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/Clipboard.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Native platform clipboard service.
|
||||
|
||||
@@ -35,7 +35,7 @@ enum class SpecialFolder
|
||||
Temporary,
|
||||
};
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/FileSystem.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/FileSystem.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Platform implementation of filesystem service.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "Engine/Core/Types/BaseTypes.h"
|
||||
#include "Engine/Core/Types/String.h"
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Network.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/Network.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Network connection protocol type.
|
||||
|
||||
@@ -124,7 +124,7 @@ enum class ThreadPriority
|
||||
|
||||
extern FLAXENGINE_API const Char* ToString(ThreadPriority value);
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Platform.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/Platform.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Runtime platform service.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "Engine/Core/Types/String.h"
|
||||
#include "Engine/Scripting/ScriptingObject.h"
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/User.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/User.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Native platform user object.
|
||||
|
||||
@@ -263,7 +263,7 @@ API_ENUM() enum class WindowHitCodes
|
||||
Help = 21,
|
||||
};
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Window.h\"");
|
||||
API_INJECT_CODE(cpp, "#include \"Engine/Platform/Window.h\"");
|
||||
|
||||
/// <summary>
|
||||
/// Native platform window object.
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Flax.Build.Bindings
|
||||
public const string Event = "API_EVENT";
|
||||
public const string Param = "API_PARAM";
|
||||
public const string Typedef = "API_TYPEDEF";
|
||||
public const string InjectCppCode = "API_INJECT_CPP_CODE";
|
||||
public const string InjectCode = "API_INJECT_CODE";
|
||||
public const string AutoSerialization = "API_AUTO_SERIALIZATION";
|
||||
|
||||
public static readonly string[] SearchTags =
|
||||
|
||||
@@ -1383,6 +1383,17 @@ namespace Flax.Build.Bindings
|
||||
GenerateCSharpEnum(buildData, contents, indent, enumInfo);
|
||||
else if (type is InterfaceInfo interfaceInfo)
|
||||
GenerateCSharpInterface(buildData, contents, indent, interfaceInfo);
|
||||
else if (type is InjectCodeInfo injectCodeInfo && string.Equals(injectCodeInfo.Lang, "csharp", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// `using` directives needs to go above the generated code
|
||||
foreach(var code in injectCodeInfo.Code.Split(';'))
|
||||
{
|
||||
if (code.StartsWith("using"))
|
||||
CSharpUsedNamespaces.Add(code.Substring(6));
|
||||
else if (code.Length > 0)
|
||||
contents.Append(injectCodeInfo.Code).AppendLine(";");
|
||||
}
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Flax.Build.Bindings
|
||||
partial class BindingsGenerator
|
||||
{
|
||||
private static readonly Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
|
||||
private const int CacheVersion = 13;
|
||||
private const int CacheVersion = 14;
|
||||
|
||||
internal static void Write(BinaryWriter writer, string e)
|
||||
{
|
||||
|
||||
@@ -2178,8 +2178,8 @@ namespace Flax.Build.Bindings
|
||||
GenerateCppEnum(buildData, contents, moduleInfo, enumInfo);
|
||||
else if (type is InterfaceInfo interfaceInfo)
|
||||
GenerateCppInterface(buildData, contents, moduleInfo, interfaceInfo);
|
||||
else if (type is InjectCppCodeInfo injectCppCodeInfo)
|
||||
contents.AppendLine(injectCppCodeInfo.Code);
|
||||
else if (type is InjectCodeInfo injectCodeInfo && string.Equals(injectCodeInfo.Lang, "cpp", StringComparison.OrdinalIgnoreCase))
|
||||
contents.AppendLine(injectCodeInfo.Code);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -1310,13 +1310,12 @@ namespace Flax.Build.Bindings
|
||||
return desc;
|
||||
}
|
||||
|
||||
private static InjectCppCodeInfo ParseInjectCppCode(ref ParsingContext context)
|
||||
private static InjectCodeInfo ParseInjectCode(ref ParsingContext context)
|
||||
{
|
||||
context.Tokenizer.ExpectToken(TokenType.LeftParent);
|
||||
var desc = new InjectCppCodeInfo
|
||||
{
|
||||
Code = context.Tokenizer.ExpectToken(TokenType.String).Value.Replace("\\\"", "\""),
|
||||
};
|
||||
var desc = new InjectCodeInfo();
|
||||
context.Tokenizer.SkipUntil(TokenType.Comma, out desc.Lang);
|
||||
desc.Code = context.Tokenizer.ExpectToken(TokenType.String).Value.Replace("\\\"", "\"");
|
||||
desc.Code = desc.Code.Substring(1, desc.Code.Length - 2);
|
||||
context.Tokenizer.ExpectToken(TokenType.RightParent);
|
||||
return desc;
|
||||
|
||||
@@ -302,10 +302,10 @@ namespace Flax.Build.Bindings
|
||||
var typeInfo = ParseTypedef(ref context);
|
||||
fileInfo.AddChild(typeInfo);
|
||||
}
|
||||
else if (string.Equals(token.Value, ApiTokens.InjectCppCode, StringComparison.Ordinal))
|
||||
else if (string.Equals(token.Value, ApiTokens.InjectCode, StringComparison.Ordinal))
|
||||
{
|
||||
var injectCppCodeInfo = ParseInjectCppCode(ref context);
|
||||
fileInfo.AddChild(injectCppCodeInfo);
|
||||
var injectCodeInfo = ParseInjectCode(ref context);
|
||||
fileInfo.AddChild(injectCodeInfo);
|
||||
}
|
||||
else if (string.Equals(token.Value, ApiTokens.Interface, StringComparison.Ordinal))
|
||||
{
|
||||
@@ -560,7 +560,7 @@ namespace Flax.Build.Bindings
|
||||
return type is ClassInfo ||
|
||||
type is StructureInfo ||
|
||||
type is InterfaceInfo ||
|
||||
type is InjectCppCodeInfo;
|
||||
type is InjectCodeInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -5,14 +5,16 @@ using System.IO;
|
||||
namespace Flax.Build.Bindings
|
||||
{
|
||||
/// <summary>
|
||||
/// The custom C++ code injection information for bindings generator.
|
||||
/// The custom C++/C# code injection information for bindings generator.
|
||||
/// </summary>
|
||||
public class InjectCppCodeInfo : ApiTypeInfo
|
||||
public class InjectCodeInfo : ApiTypeInfo
|
||||
{
|
||||
public string Lang;
|
||||
public string Code;
|
||||
|
||||
public override void Write(BinaryWriter writer)
|
||||
{
|
||||
writer.Write(Lang);
|
||||
writer.Write(Code);
|
||||
|
||||
base.Write(writer);
|
||||
@@ -20,6 +22,7 @@ namespace Flax.Build.Bindings
|
||||
|
||||
public override void Read(BinaryReader reader)
|
||||
{
|
||||
Lang = reader.ReadString();
|
||||
Code = reader.ReadString();
|
||||
|
||||
base.Read(reader);
|
||||
@@ -76,7 +76,7 @@
|
||||
<Compile Include="Bindings\FieldInfo.cs" />
|
||||
<Compile Include="Bindings\FileInfo.cs" />
|
||||
<Compile Include="Bindings\InheritanceInfo.cs" />
|
||||
<Compile Include="Bindings\InjectCppCodeInfo.cs" />
|
||||
<Compile Include="Bindings\InjectCodeInfo.cs" />
|
||||
<Compile Include="Bindings\InterfaceInfo.cs" />
|
||||
<Compile Include="Bindings\LangType.cs" />
|
||||
<Compile Include="Bindings\MemberInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user