mirror of
https://github.com/Starpelly/raylib-beef.git
synced 2025-06-10 07:42:19 +02:00
Added Raymath and Rlgl support
This commit is contained in:
parent
1186789e37
commit
9ef6d53382
51 changed files with 2441 additions and 647 deletions
|
@ -15,7 +15,24 @@ namespace RaylibBeefGenerator
|
|||
|
||||
private static string OutputDir = @"C:\Dev\raylib-beef\raylib-beef\src\";
|
||||
|
||||
private static Root API;
|
||||
private static Dictionary<string, FileDefinition> jsonFiles = new()
|
||||
{
|
||||
{ "raylib.json", new ("Raylib", "") },
|
||||
{ "rlgl.json", new("Rlgl", "Rlgl") },
|
||||
{ "raymath.json", new("Raymath", "Raymath") }
|
||||
};
|
||||
|
||||
public struct FileDefinition
|
||||
{
|
||||
public string FileName;
|
||||
public string ClassName;
|
||||
|
||||
public FileDefinition(string fileName, string className = "")
|
||||
{
|
||||
this.FileName = fileName;
|
||||
this.ClassName = className;
|
||||
}
|
||||
}
|
||||
|
||||
#region Output Defines
|
||||
private static string ImportLib = "raylib.dll";
|
||||
|
@ -31,32 +48,102 @@ namespace RaylibBeefGenerator
|
|||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string raylibjson = @"C:\Dev\raylib-beef\raylib-api\raylib.json";
|
||||
|
||||
API = JsonConvert.DeserializeObject<Root>(File.ReadAllText(raylibjson));
|
||||
|
||||
Console.WriteLine($"Generating files at {OutputDir}");
|
||||
Console.WriteLine($"...");
|
||||
|
||||
RaylibBf();
|
||||
|
||||
for (int i = 0; i < API.Structs.Count; i++)
|
||||
|
||||
for (var i = 0; i < jsonFiles.Count; i++)
|
||||
{
|
||||
StructBf(API.Structs[i]);
|
||||
}
|
||||
|
||||
Callbacks();
|
||||
|
||||
for (int i = 0; i < API.Enums.Count; i++)
|
||||
{
|
||||
Enum(API.Enums[i]);
|
||||
ConvertFile(jsonFiles.ElementAt(i).Value, @$"C:\Dev\raylib-beef\raylib-api\{jsonFiles.ElementAt(i).Key}");
|
||||
}
|
||||
|
||||
Console.WriteLine("Successfully Generated Bindings!");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
public static void Enum(Enum @enum)
|
||||
public static void ConvertFile(FileDefinition def, string location)
|
||||
{
|
||||
var api = JsonConvert.DeserializeObject<Root>(File.ReadAllText(location));
|
||||
|
||||
OutputString.Clear();
|
||||
OutputString = new();
|
||||
|
||||
UniversalHeader();
|
||||
|
||||
AppendLine((string.IsNullOrEmpty(def.ClassName)) ? "static" : $"public static class {def.ClassName}");
|
||||
AppendLine("{");
|
||||
IncreaseTab();
|
||||
|
||||
for (var i = 0; i < api.Defines.Count; i++)
|
||||
{
|
||||
var define = api.Defines[i];
|
||||
if (define.Type == "UNKNOWN" || define.Type == "MACRO" || define.Type == "GUARD") continue;
|
||||
|
||||
if (!string.IsNullOrEmpty(define.Description)) AppendLine($"/// {define.Description}");
|
||||
var defineType = define.Type.ConvertTypes();
|
||||
AppendLine($"public const {defineType} {define.Name.ConvertName()} = {define.Value.ToString().ParseValue(defineType)};");
|
||||
AppendLine("");
|
||||
}
|
||||
|
||||
for (var i = 0; i < api.Functions.Count; i++)
|
||||
{
|
||||
var func = api.Functions[i];
|
||||
|
||||
AppendLine($"/// {func.Description}");
|
||||
AppendLine($"[Import(\"{ImportLib}\"), CallingConvention(.Cdecl), LinkName(\"{func.Name}\")]");
|
||||
AppendLine($"public static extern {func.ReturnType.ConvertTypes()} {func.Name.ConvertName()}({Parameters2String(func.Params)});");
|
||||
AppendLine("");
|
||||
|
||||
/*
|
||||
var char8Params = new List<Param>();
|
||||
var funcHasChar8 = false;
|
||||
for (var p = 0; p < func.Params.Count; p++)
|
||||
{
|
||||
var param = func.Params[p];
|
||||
if (param.Type.ConvertTypes() == "char8 *")
|
||||
{
|
||||
param.Type = "String";
|
||||
funcHasChar8 = true;
|
||||
}
|
||||
char8Params.Add(param);
|
||||
}
|
||||
|
||||
if (funcHasChar8)
|
||||
{
|
||||
AppendLine($"/// {func.Description}");
|
||||
AppendLine($"[Import(\"{ImportLib}\"), CallingConvention(.Cdecl), LinkName(\"{func.Name}\")]");
|
||||
AppendLine($"public static extern void {func.Name.ConvertName()}({Parameters2String(char8Params)});");
|
||||
AppendLine("");
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
AppendLine("");
|
||||
|
||||
for (int i = 0; i < api.Callbacks.Count; i++)
|
||||
{
|
||||
var callback = api.Callbacks[i];
|
||||
|
||||
if (!string.IsNullOrEmpty(callback.Description)) AppendLine($"/// {callback.Description}");
|
||||
AppendLine($"public function {callback.ReturnType.ConvertTypes()} {callback.Name.ConvertName()}({callback.Params.Parameters2String()});");
|
||||
AppendLine("");
|
||||
}
|
||||
|
||||
DecreaseTab();
|
||||
AppendLine("}");
|
||||
|
||||
WriteToFile(def.FileName);
|
||||
|
||||
for (var i = 0; i < api.Structs.Count; i++)
|
||||
{
|
||||
StructBf(api, api.Structs[i]);
|
||||
}
|
||||
for (var i = 0; i < api.Enums.Count; i++)
|
||||
{
|
||||
Enum(api, api.Enums[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Enum(Root api, Enum @enum)
|
||||
{
|
||||
OutputString.Clear();
|
||||
OutputString = new();
|
||||
|
@ -77,41 +164,17 @@ namespace RaylibBeefGenerator
|
|||
DecreaseTab();
|
||||
AppendLine("}");
|
||||
|
||||
WriteToFile($@"Enums\{@enum.Name}");
|
||||
WriteToFile($@"{@enum.Name}");
|
||||
}
|
||||
|
||||
public static void Callbacks()
|
||||
public static void StructBf(Root api, Struct structu)
|
||||
{
|
||||
OutputString.Clear();
|
||||
OutputString = new();
|
||||
|
||||
UniversalHeader();
|
||||
|
||||
AppendLine($"static");
|
||||
AppendLine("{");
|
||||
IncreaseTab();
|
||||
for (int i = 0; i < API.Callbacks.Count; i++)
|
||||
{
|
||||
var callback = API.Callbacks[i];
|
||||
|
||||
if (!string.IsNullOrEmpty(callback.Description)) AppendLine($"/// {callback.Description}");
|
||||
AppendLine($"public function {callback.ReturnType.ConvertTypes()} {callback.Name.ConvertName()}({callback.Params.Parameters2String()});");
|
||||
AppendLine("");
|
||||
}
|
||||
DecreaseTab();
|
||||
AppendLine("}");
|
||||
|
||||
WriteToFile("Callbacks");
|
||||
}
|
||||
|
||||
public static void StructBf(Struct structu)
|
||||
{
|
||||
OutputString.Clear();
|
||||
OutputString = new();
|
||||
|
||||
UniversalHeader();
|
||||
|
||||
var alias = API.Aliases.FindAll(c => c.Type == structu.Name);
|
||||
var alias = api.Aliases.FindAll(c => c.Type == structu.Name);
|
||||
if (alias != null)
|
||||
{
|
||||
for (int i = 0; i < alias.Count; i++)
|
||||
|
@ -128,17 +191,26 @@ namespace RaylibBeefGenerator
|
|||
|
||||
var constructorLine = "public this(";
|
||||
|
||||
for (int i = 0; i < structu.Fields.Count; i++)
|
||||
var createdFields = new List<Field>();
|
||||
|
||||
for (var i = 0; i < structu.Fields.Count; i++)
|
||||
{
|
||||
var field = structu.Fields[i];
|
||||
|
||||
// This is like the only thing that is hardcoded, and that saddens me.
|
||||
if (field.Type == "rAudioProcessor *" || field.Type == "rAudioBuffer *") continue;
|
||||
if (field.Type == "rAudioProcessor *" || field.Type == "rAudioBuffer *" || field.Type.StartsWith("#if") || field.Type == "#endif") continue;
|
||||
|
||||
// Avoid duplicates
|
||||
if (createdFields.Find(c => c.Name == field.Name) == null)
|
||||
createdFields.Add(field);
|
||||
else
|
||||
continue;
|
||||
|
||||
AppendLine($"/// {field.Description}");
|
||||
AppendLine($"public {field.Type.ConvertTypes()} {field.Name.ConvertName()};");
|
||||
AppendLine("");
|
||||
|
||||
|
||||
constructorLine += $"{field.Type.ConvertTypes()} {field.Name.ConvertName()}";
|
||||
if (i < structu.Fields.Count - 1) constructorLine += ", ";
|
||||
}
|
||||
|
@ -148,10 +220,10 @@ namespace RaylibBeefGenerator
|
|||
AppendLine("{");
|
||||
IncreaseTab();
|
||||
|
||||
for (int i = 0; i < structu.Fields.Count; i++)
|
||||
for (var i = 0; i < createdFields.Count; i++)
|
||||
{
|
||||
var field = structu.Fields[i];
|
||||
if (field.Type == "rAudioProcessor *" || field.Type == "rAudioBuffer *") continue;
|
||||
var field = createdFields[i];
|
||||
if (field.Type == "rAudioProcessor *" || field.Type == "rAudioBuffer *" || field.Type.StartsWith("#if") || field.Type == "#endif") continue;
|
||||
|
||||
AppendLine($"this.{field.Name.ConvertName()} = {field.Name.ConvertName()};");
|
||||
}
|
||||
|
@ -164,49 +236,6 @@ namespace RaylibBeefGenerator
|
|||
WriteToFile($"{structu.Name}");
|
||||
}
|
||||
|
||||
public static void RaylibBf()
|
||||
{
|
||||
OutputString.Clear();
|
||||
OutputString = new();
|
||||
|
||||
UniversalHeader();
|
||||
|
||||
AppendLine("static");
|
||||
AppendLine("{");
|
||||
|
||||
IncreaseTab();
|
||||
|
||||
AppendLine($"/// Used internally for bindings.");
|
||||
AppendLine($"public const String RAYLIB_LIB = \"{ImportLib}\";");
|
||||
AppendLine("");
|
||||
|
||||
// Skip first one, no value.
|
||||
for (int i = 1; i < API.Defines.Count; i++)
|
||||
{
|
||||
var define = API.Defines[i];
|
||||
if (define.Type == "UNKNOWN" || define.Type == "MACRO" || define.Type == "GUARD") continue;
|
||||
|
||||
if (!string.IsNullOrEmpty(define.Description)) AppendLine($"/// {define.Description}");
|
||||
var defineType = define.Type.ConvertTypes();
|
||||
AppendLine($"public const {defineType} {define.Name.ConvertName()} = {define.Value.ToString().ParseValue(defineType)};");
|
||||
AppendLine("");
|
||||
}
|
||||
|
||||
for (int i = 0; i < API.Functions.Count; i++)
|
||||
{
|
||||
var func = API.Functions[i];
|
||||
|
||||
AppendLine($"/// {func.Description}");
|
||||
AppendLine($"[Import(RAYLIB_LIB), CallingConvention(.Cdecl), LinkName(\"{func.Name}\")]");
|
||||
AppendLine($"public static extern {func.ReturnType.ConvertTypes()} {func.Name.ConvertName()}({Parameters2String(func.Params)});");
|
||||
AppendLine("");
|
||||
}
|
||||
DecreaseTab();
|
||||
AppendLine("}");
|
||||
|
||||
WriteToFile("Raylib");
|
||||
}
|
||||
|
||||
public static string Parameters2String(this List<Param> @params)
|
||||
{
|
||||
var paramStr = string.Empty;
|
||||
|
@ -279,6 +308,7 @@ namespace RaylibBeefGenerator
|
|||
input = ReplaceWholeWord(input, "STRING", "char8*");
|
||||
input = ReplaceWholeWord(input, "FLOAT", "float");
|
||||
input = ReplaceWholeWord(input, "FLOAT_MATH", "float");
|
||||
input = ReplaceWholeWord(input, "DOUBLE", "double");
|
||||
input = ReplaceWholeWord(input, "COLOR", "Color");
|
||||
|
||||
if (input.StartsWith("const"))
|
||||
|
@ -300,6 +330,7 @@ namespace RaylibBeefGenerator
|
|||
input = ReplaceWholeWord(input, "append", "@append");
|
||||
input = ReplaceWholeWord(input, "box", "@box");
|
||||
input = ReplaceWholeWord(input, "params", "@params");
|
||||
input = ReplaceWholeWord(input, "readonly", "@readonly");
|
||||
|
||||
return input;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue