2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-05 11:19:39 -04:00

Organising generator code

- Split directory and file loop from code generation.
This commit is contained in:
ChrisDill 2018-09-11 22:26:31 +01:00
parent 8272439d26
commit f966abca38
2 changed files with 92 additions and 56 deletions

View File

@ -7,7 +7,7 @@ namespace Raylibcs
{ {
static class Generator static class Generator
{ {
static string template = @" public static string template = @"
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -29,17 +29,17 @@ namespace Raylib
} }
"; ";
static string exampleTemplate = @" public static string exampleTemplate = @"
using Raylib; using Raylib;
using static Raylib.rl; using static Raylib.Raylib;
public partial class Examples public partial class Examples
{ {
{{ CONTENT }} {{ CONTENT }}
}"; }";
public static string RaylibDirectory = "C:\\raylib\\raylib\\";
public static string InstallDirectory = "C:\\raylib\\raylib\\src\\"; public static string InstallDirectory = "C:\\raylib\\raylib\\src\\";
public static string ExamplesDirectory = "C:\\raylib\\raylib\\examples\\";
// string extensions // string extensions
private static string CapitalizeFirstCharacter(string format) private static string CapitalizeFirstCharacter(string format)
@ -59,6 +59,7 @@ public partial class Examples
return sb.ToString(); return sb.ToString();
} }
// testing regex
public static string ReplaceEx(this string input, string pattern, string replacement) public static string ReplaceEx(this string input, string pattern, string replacement)
{ {
input = input.Replace("\r", "\r\n"); input = input.Replace("\r", "\r\n");
@ -72,11 +73,7 @@ public partial class Examples
return Regex.Replace(input, pattern, replacement); return Regex.Replace(input, pattern, replacement);
} }
/// <summary> // Create binding code
/// Proocess raylib file
/// </summary>
/// <param name="filePath"></param>
/// <param name="api"></param>
public static void Process(string filePath, string api) public static void Process(string filePath, string api)
{ {
var lines = File.ReadAllLines(InstallDirectory + filePath); var lines = File.ReadAllLines(InstallDirectory + filePath);
@ -121,27 +118,10 @@ public partial class Examples
File.WriteAllText("Raylib-cs/ " + filePath + ".cs", output); File.WriteAllText("Raylib-cs/ " + filePath + ".cs", output);
} }
/// <summary> // Convert c style to c#
/// Process raylib examples // Design is close to raylib so only a few changes needed
/// </summary> public static void ProcessExample(string file, string dirName)
public static void ProcessExamples()
{ {
// create directory in output folder with same layout as raylib examples
Directory.CreateDirectory("Examples");
var dirs = Directory.GetDirectories(ExamplesDirectory);
foreach (var dir in dirs)
{
var dirName = new DirectoryInfo(dir).Name;
var files = Directory.GetFiles(dir);
Directory.CreateDirectory("Examples\\" + dirName);
foreach (var file in files)
{
if (Path.GetExtension(file) != ".c")
continue;
var fileName = Path.GetFileNameWithoutExtension(file); var fileName = Path.GetFileNameWithoutExtension(file);
var text = File.ReadAllText(file); var text = File.ReadAllText(file);
@ -151,16 +131,16 @@ public partial class Examples
// add template and fill in content // add template and fill in content
var output = exampleTemplate; var output = exampleTemplate;
output = output.Replace("{{ CONTENT }}", text); output = output.Replace("{{ CONTENT }}", text);
output = output.Replace("int main()", "public static int " + fileName + "()"); //output = output.Replace("int main()", "public static int " + fileName + "()");
output = output.Replace("#include \"raylib.h\"", ""); //output = output.Replace("#include \"raylib.h\"", "");
// REGEX WHYYYYYYY!!! // test regex on one file for now
//if (fileName == "core_2d_camera") if (fileName == "core_2d_camera")
{ {
// remove #include lines // remove #include lines
// #define x y -> private const int x = y; // #define x y -> private const int x = y;
output = output.ReplaceEx(@"#define (\w+).*?(\d+)", "private const int $1 = $2;"); //output = output.ReplaceEx(@"#define (\w+).*?(\d+)", "private const int $1 = $2;");
// (Type){...} -> new Type(...); // (Type){...} -> new Type(...);
// output = output.ReplaceEx(@"(\((\w+)\).*?{.*})", @""); // output = output.ReplaceEx(@"(\((\w+)\).*?{.*})", @"");
@ -171,8 +151,8 @@ public partial class Examples
var path = "Examples\\" + dirName + "\\" + fileName + ".cs"; var path = "Examples\\" + dirName + "\\" + fileName + ".cs";
File.WriteAllText(path, output); File.WriteAllText(path, output);
}
} Console.WriteLine("Generated " + fileName + ".cs");
} }
} }
} }

View File

@ -1,16 +1,72 @@
using System; using System;
using System.IO;
namespace Raylibcs namespace Raylibcs
{ {
/// <summary>
/// Rough generator for Raylib-cs to help automate binding porting raylib code
/// Output will still need to be modified as it is a work in progess
/// </summary>
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("Raylib-cs generator"); Console.WriteLine("Raylib-cs generator");
// Generator.Process("raylib.h", "RLAPI");
Generator.ProcessExamples(); //GenerateBindings();
GenerateExamples();
//GenerateTemplates();
//GenerateGames();
Console.WriteLine("Finished generating. Enjoy! :)");
Console.WriteLine("Press enter to exit"); Console.WriteLine("Press enter to exit");
Console.Read(); Console.Read();
} }
static void GenerateBindings()
{
Console.WriteLine("Generating bindings");
// Generator.Generate("raylib.h", "RLAPI");
}
static void GenerateExamples()
{
Console.WriteLine("Generating examples");
// output folder
Directory.CreateDirectory("Examples");
// load files
var path = Generator.RaylibDirectory + "Examples";
var dirs = Directory.GetDirectories(path);
// convert each file to rough c# version
foreach (var dir in dirs)
{
// create sub folder in output
var dirName = new DirectoryInfo(dir).Name;
Directory.CreateDirectory("Examples\\" + dirName);
var files = Directory.GetFiles(dir, "*.c");
foreach (var file in files)
{
Generator.ProcessExample(file, dirName);
}
}
}
static void GenerateTemplates()
{
Console.WriteLine("Generating templates");
// TODO
}
static void GenerateGames()
{
Console.WriteLine("Generating games");
// TODO
}
} }
} }