2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-06-30 19:03:42 -04:00

Tidying up project

- Added Release folder for important dlls that will be used in nuget package
- Moved Rayforms into Examples folder
- Added updated generator(WIP)
This commit is contained in:
2018-10-05 17:31:46 +01:00
parent 7008c67dfa
commit e2a32241de
16 changed files with 240 additions and 146 deletions

View File

@ -7,39 +7,8 @@ namespace Raylibcs
{
static class Generator
{
public static string template = @"
using System;
using System.Runtime.InteropServices;
namespace Raylib
{
public static partial class rl
{
#region Raylib-cs Variables
// Used by DllImport to load the native library.
private const string nativeLibName = 'raylib.dll';
#endregion
#region Raylib-cs Functions
{{ CONTENT }}
}
}
";
public static string exampleTemplate = @"
using Raylib;
using static Raylib.Raylib;
public partial class Examples
{
{{ CONTENT }}
}";
public static string RaylibDirectory = "C:\\raylib\\raylib\\";
public static string InstallDirectory = "C:\\raylib\\raylib\\src\\";
public static string RaylibDirectory2 = "C:\\Users\\Chris\\Documents\\projects\\raylib\\";
// string extensions
private static string CapitalizeFirstCharacter(string format)
@ -62,11 +31,12 @@ public partial class Examples
// testing regex
public static string ReplaceEx(this string input, string pattern, string replacement)
{
input = input.Replace("\r", "\r\n");
foreach (Match match in Regex.Matches(input, pattern))
var matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
//Console.WriteLine(match.Value);
}
// Console.WriteLine(matches.Count);
//return input;
//var match = Regex.IsMatch(input, pattern);
@ -76,9 +46,13 @@ public partial class Examples
// Create binding code
public static void Process(string filePath, string api)
{
var lines = File.ReadAllLines(InstallDirectory + filePath);
var lines = File.ReadAllLines(RaylibDirectory + "src//" + filePath);
var output = "";
output += "using Raylib;\n";
output += "using static Raylib.Raylib;\n\n";
output += "public partial class Examples\n{\n";
// convert functions to c#
foreach (string line in lines)
{
@ -94,8 +68,11 @@ public partial class Examples
}
output += "\t\t#endregion\n";
//output += text;
output += "\n}";
// convert syntax to c#
output = template.Replace("{{ CONTENT }}", output);
//output = template.Replace("{{ CONTENT }}", output);
output = output.Replace("(void)", "()");
output = output.Replace("const char *", "string ");
@ -120,38 +97,109 @@ public partial class Examples
// Convert c style to c#
// Design is close to raylib so only a few changes needed
public static void ProcessExample(string file, string dirName)
public static void ProcessExample(string file, string folder, string path)
{
// fix #defines
// fix structs
// fix enums
// remove return 0 for main
// fix {} initialization(not all cases covered)
// load and setup
var fileName = Path.GetFileNameWithoutExtension(file);
var text = File.ReadAllText(file);
var result = "";
var output = "";
output += "using Raylib;\n";
output += "using static Raylib.Raylib;\n\n";
output += "public partial class " + folder + "\n{\n";
// text = text.Replace("\r", "\r\n");
// indent since example will be in Examples namespace
text = text.Indent(4);
// add template and fill in content
var output = exampleTemplate;
output = output.Replace("{{ CONTENT }}", text);
//output = output.Replace("int main()", "public static int " + fileName + "()");
//output = output.Replace("#include \"raylib.h\"", "");
// test regex on one file for now
if (fileName == "core_2d_camera")
// rough file conversion
string[] lines = text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
// remove #include lines
string line = lines[i];
// #define x y -> private const int x = y;
//output = output.ReplaceEx(@"#define (\w+).*?(\d+)", "private const int $1 = $2;");
// ignore certain preprocess symbols
if (line.Contains("#include"))
continue;
else if (line.Contains("#if"))
continue;
else if (line.Contains("#else"))
continue;
else if (line.Contains("#endif"))
continue;
// (Type){...} -> new Type(...);
// output = output.ReplaceEx(@"(\((\w+)\).*?{.*})", @"");
// output = output.ReplaceEx(@"\((\w +)\).*{ (.*)}", @"new $1($2)");
else if (line.Contains("#define"))
{
var str = line.Replace("#define", "");
var arr = str.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length < 2)
continue;
bool isNumeric = int.TryParse(arr[1], out var n);
if (isNumeric)
result += "public const int " + arr[0] + " = " + arr[1] + ";\r\n";
else
result += "public const string " + arr[0] + " = " + arr[1] + ";\r\n";
}
// change main signature
else if (line.StartsWith("int main"))
result += "public static void Main()\r\n";
// remove typedef and mark members as public
else if (line.StartsWith("typedef struct"))
{
var members = "";
while (!line.StartsWith("}"))
{
i++;
line = lines[i];
members += "public " + line + "\n";
}
line = line.Replace(" ", "");
line = line.Replace("}", "");
line = line.Replace(";", "");
result += "struct " + line + "{\n\n";
result += members;
}
// copy line by default
else
result += line + "\n";
}
//output = output.ReplaceEx(@"#define (\w+) (\w+)", @"struct 1 public 2 public 3 public 4");
// regex
var path = "Examples\\" + dirName + "\\" + fileName + ".cs";
File.WriteAllText(path, output);
// (Type){...} -> new Type(...)
// e.g (Vector2){ 100, 100 } -> new Vector2( 100, 100 );
result = result.ReplaceEx(@"\((\w+)\){(.*)}", @"new $1($2);");
result = result.ReplaceEx(@"\((\w+)\) \w+ = {(.*)}", @"new $1($2);");
// Type name[size] -> Type[] name = new Type[size];
// e.g Rectangle buildings[MAX_BUILDINGS]; -> Rectangle[] buildings = new Rectangle[MAX_BUILDINGS];
result = result.ReplaceEx(@"(\w+) (\w+)\[(.*)\];", "$1[] $2 = new $1[$3];");
result = result.Replace("Music ", "IntPtr ");
result = result.Replace("(void)", "()");
// defines to enums(might use defines as variables aswell not sure)
result = result.ReplaceEx(@"KEY_(\w+)", @"(int)Key.$1");
result = result.ReplaceEx(@"MOUSE_(\w+)", @"(int)Mouse.$1");
result = result.ReplaceEx(@"FLAG_(\w+)", @"(int)Flag.$1");
// result = result.ReplaceEx(@"FLAG_(\w+)", @"(int)Flag.$1");
// add result
result = result.Indent(4);
output += result;
output += "\n}\n";
// saves relative to executable location
var loc = path + "//" + fileName + ".cs";
File.WriteAllText(loc, output);
Console.WriteLine("Generated " + fileName + ".cs");
}
}

View File

@ -31,6 +31,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>raylib-cs.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -49,5 +52,8 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="raylib-cs.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -4,8 +4,8 @@ using System.IO;
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
/// Rough generator for Raylib-cs to help automate binding + porting raylib code
/// Output will still need to be modified
/// </summary>
class Program
{
@ -13,10 +13,10 @@ namespace Raylibcs
{
Console.WriteLine("Raylib-cs generator");
//GenerateBindings();
GenerateBindings();
GenerateExamples();
//GenerateTemplates();
//GenerateGames();
GenerateTemplates();
GenerateGames();
Console.WriteLine("Finished generating. Enjoy! :)");
Console.WriteLine("Press enter to exit");
@ -26,7 +26,8 @@ namespace Raylibcs
static void GenerateBindings()
{
Console.WriteLine("Generating bindings");
// Generator.Generate("raylib.h", "RLAPI");
Generator.Process("raylib.h", "RLAPI");
Generator.Process("rlgl.h", "RLGL");
}
static void GenerateExamples()
@ -34,39 +35,91 @@ namespace Raylibcs
Console.WriteLine("Generating examples");
// output folder
Directory.CreateDirectory("Examples");
// load files
var path = Generator.RaylibDirectory + "Examples";
var folder = "Examples";
Directory.CreateDirectory(folder);
var path = Generator.RaylibDirectory + folder.ToLower();
var dirs = Directory.GetDirectories(path);
// convert each file to rough c# version
foreach (var dir in dirs)
var files = Directory.GetFiles(path, "*.c", SearchOption.AllDirectories);
foreach (var file in files)
{
// 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);
}
var dirName = Path.GetDirectoryName(file);
var name = new DirectoryInfo(dirName).Name;
if (!Directory.Exists(folder + name))
Directory.CreateDirectory(folder + "//" + name);
Generator.ProcessExample(file, folder, folder + "//" + name);
}
}
static void GenerateTemplates()
{
Console.WriteLine("Generating templates");
// TODO
// output folder
var folder = "Templates";
Directory.CreateDirectory(folder);
var path = Generator.RaylibDirectory2 + folder.ToLower();
var dirs = Directory.GetDirectories(path);
// copy folder structure
foreach (string dirPath in Directory.GetDirectories(path, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(path, folder));
// process all c files in directory and output result
var files = Directory.GetFiles(path, "*.c", SearchOption.AllDirectories);
foreach (var file in files)
{
var dirName = Path.GetDirectoryName(file);
var name = new DirectoryInfo(dirName).Name;
if (name == folder.ToLower())
{
Generator.ProcessExample(file, folder, folder);
}
else
{
var t = file;
t = folder + t.Replace(path, "");
t = new FileInfo(t).Directory.FullName;
Generator.ProcessExample(file, folder, t);
}
}
}
static void GenerateGames()
{
Console.WriteLine("Generating games");
// TODO
// output folder
var folder = "Games";
Directory.CreateDirectory(folder);
var path = Generator.RaylibDirectory2 + folder.ToLower();
var dirs = Directory.GetDirectories(path);
// copy folder structure
foreach (string dirPath in Directory.GetDirectories(path, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(path, folder));
// process all c files in directory and output result
var files = Directory.GetFiles(path, "*.c", SearchOption.AllDirectories);
foreach (var file in files)
{
var dirName = Path.GetDirectoryName(file);
var name = new DirectoryInfo(dirName).Name;
if (name == folder.ToLower())
{
Generator.ProcessExample(file, folder, folder);
}
else
{
var t = file;
t = folder + t.Replace(path, "");
t = new FileInfo(t).Directory.FullName;
Generator.ProcessExample(file, folder, t);
}
}
}
}
}

BIN
Generator/raylib-cs.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB