unlock
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
FileVersion = 1
|
||||
|
||||
[Project]
|
||||
Name = "ENet.Setup"
|
||||
StartupObject = "ENet.Setup.Program"
|
||||
|
||||
[Dependencies]
|
||||
corlib = "*"
|
||||
"Cpp2Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Cpp2Beef.git"}
|
||||
@@ -0,0 +1,8 @@
|
||||
FileVersion = 1
|
||||
|
||||
[Workspace]
|
||||
StartupProject = "ENet.Setup"
|
||||
|
||||
[Projects]
|
||||
"ENet.Setup" = {Path = "."}
|
||||
"Cpp2Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Cpp2Beef.git"}
|
||||
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Cpp2Beef;
|
||||
using LibClang;
|
||||
|
||||
namespace ENet.Setup;
|
||||
|
||||
class ENetGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
|
||||
{
|
||||
protected override Span<char8*> Args => args;
|
||||
protected override Flags Flags => .None;
|
||||
|
||||
StreamWriter writer = new .()..Create("../src/ENet.bf")..Write("""
|
||||
// This file was generated by Cpp2Beef
|
||||
|
||||
using System;
|
||||
using System.Interop;
|
||||
|
||||
namespace ENet;
|
||||
|
||||
|
||||
""") ~ delete _;
|
||||
|
||||
protected override StreamWriter GetWriterForHeader(StringView header)
|
||||
{
|
||||
if (header.EndsWith("enet.h"))
|
||||
return writer;
|
||||
return null;
|
||||
}
|
||||
|
||||
const String[?] handles = .("socket_", "host_", "address_", "peer_");
|
||||
const String[?] handleBlocks = .("ENetSocket", "ENetHost", "ENetAddress", "ENetPeer");
|
||||
|
||||
protected override Block GetCursorBlock(CXCursor cursor)
|
||||
{
|
||||
if (cursor.kind == .FunctionDecl) do
|
||||
{
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (!spelling.StartsWith("enet_")) break;
|
||||
spelling.RemoveFromStart(5);
|
||||
for (let i < handles.Count)
|
||||
if (spelling.StartsWith(handles[i]))
|
||||
return .CustomBlock(handleBlocks[i]);
|
||||
return .CustomBlock("ENet");
|
||||
}
|
||||
if (cursor.kind == .EnumDecl && Clang.Cursor_IsAnonymous(cursor) != 0)
|
||||
return .StaticBlock;
|
||||
if (cursor.kind == .EnumConstantDecl)
|
||||
return .StaticBlock;
|
||||
return base.GetCursorBlock(cursor);
|
||||
}
|
||||
|
||||
protected override void HandleCursor(CXCursor cursor)
|
||||
{
|
||||
switch (cursor.kind)
|
||||
{
|
||||
case .MacroDefinition:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling == "CLOCK_MONOTONIC" || spelling == "ENET_CALLBACK" || spelling == "ENET_API" || spelling.StartsWith("enet_address_"))
|
||||
return;
|
||||
case .TypedefDecl:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling == "ENetSocketSet" || spelling == "ENetSocket")
|
||||
return;
|
||||
case .StructDecl:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling == "_ENetHost")
|
||||
return;
|
||||
case .EnumDecl:
|
||||
if (Clang.Cursor_IsAnonymous(cursor) != 0)
|
||||
{
|
||||
BeginCursor(cursor);
|
||||
Clang.VisitChildren(cursor, (cursor, parent, client_data) =>
|
||||
{
|
||||
Self self = (.)Internal.UnsafeCastToObject(client_data);
|
||||
Runtime.Assert(cursor.kind == .EnumConstantDecl);
|
||||
self.BeginCursor(cursor);
|
||||
self.str.Append("public const let ");
|
||||
self.WriteCustomAttributes(cursor);
|
||||
self.str.Append(GetCursorSpelling!(cursor));
|
||||
self.WriteTokensAfterEquals(cursor);
|
||||
self.str.Append(';');
|
||||
return .Continue;
|
||||
}, Internal.UnsafeCastToPtr(this));
|
||||
return;
|
||||
}
|
||||
default:
|
||||
}
|
||||
base.HandleCursor(cursor);
|
||||
if (cursor.kind == .VarDecl)
|
||||
{
|
||||
str.Replace("{{{", ".(");
|
||||
str.Replace("}}}", ")");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void GetNameInBindings(LibClang.CXCursor cursor, String outString)
|
||||
{
|
||||
switch (cursor.kind)
|
||||
{
|
||||
case .FunctionDecl:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling.StartsWith("enet_")) spelling.RemoveFromStart(5);
|
||||
for (let handle in handles)
|
||||
if (spelling.StartsWith(handle))
|
||||
{
|
||||
spelling.RemoveFromStart(handle.Length);
|
||||
break;
|
||||
}
|
||||
UpperSnakeCase2PascalCase(spelling, outString);
|
||||
return;
|
||||
case .StructDecl, .UnionDecl, .EnumDecl:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling.StartsWith('_')) spelling.RemoveFromStart(1);
|
||||
outString.Append(spelling);
|
||||
return;
|
||||
case .MacroDefinition:
|
||||
StringView spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling == "INVALID_SOCKET")
|
||||
{
|
||||
outString.Append('0');
|
||||
return;
|
||||
}
|
||||
case .EnumConstantDecl:
|
||||
StringView spelling = UpperSnakeCase2PascalCase(GetCursorSpelling!(cursor), ..scope .());
|
||||
StringView parent = GetCursorSpelling!(Clang.GetCursorSemanticParent(cursor));
|
||||
if (parent.StartsWith('_')) parent.RemoveFromStart(1);
|
||||
if (parent == "ENetProtocolFlag") parent.RemoveFromEnd(4);
|
||||
if (parent == "ENetSocketOption") parent = "EnetSockopt";
|
||||
if (spelling.StartsWith(parent, .OrdinalIgnoreCase))
|
||||
spelling.RemoveFromStart(parent.Length);
|
||||
outString.Append(spelling);
|
||||
return;
|
||||
default:
|
||||
}
|
||||
base.GetNameInBindings(cursor, outString);
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
[CLink] static extern int32 system(char8*);
|
||||
|
||||
public static int Main(String[] args)
|
||||
{
|
||||
if (system("wget -O ../src/enet.h https://raw.githubusercontent.com/zpl-c/enet/refs/heads/master/include/enet.h") != 0)
|
||||
Runtime.FatalError("Failed to download enet.h");
|
||||
|
||||
scope ENetGenerator(char8*[?]("--language=c")).Generate("../src/enet.h");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user