forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Native] Add generator for c# types used on c++ side
- Loading branch information
Showing
5 changed files
with
2,240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Tools/MonoGame.Generator.CTypes/MonoGame.Generator.CTypes.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
static string GetCEnumType(string cstype) | ||
{ | ||
return cstype switch | ||
{ | ||
"System.Byte" => "csbyte", | ||
"System.Int16" => "csshort", | ||
"System.UInt16" => "csushort", | ||
"System.Int32" => "csint", | ||
"System.UInt32" => "csuint", | ||
"System.Int64" => "cslong", | ||
"System.UInt64" => "csulong", | ||
_ => "CS" + cstype | ||
}; | ||
}; | ||
|
||
var repoDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../../"); | ||
var monogamePlatformDir = Path.Combine(repoDirectory, "src/monogame/include"); | ||
var monogameFrameworkPath = Path.Combine(repoDirectory, "Artifacts/MonoGame.Framework/Native/Debug/MonoGame.Framework.dll"); | ||
var assembly = Assembly.LoadFile(monogameFrameworkPath); | ||
var outputText = new StringBuilder(); | ||
var duplicateChecker = new Dictionary<string, string>(); | ||
|
||
outputText.AppendLine($""" | ||
// | ||
// This code is auto generated, don't modify it by hand. | ||
// To regenerate it run: Tools/MonoGame.Generator.CTypes | ||
// | ||
#pragma once | ||
#include "csharp_common.h" | ||
"""); | ||
|
||
foreach (var enumType in assembly.GetTypes()) | ||
{ | ||
if (!enumType.IsEnum) | ||
continue; | ||
|
||
if (enumType.IsNested) | ||
continue; | ||
|
||
if (!enumType.FullName!.StartsWith("MonoGame") && !enumType.FullName!.StartsWith("Microsoft.Xna.Framework")) | ||
continue; | ||
|
||
if (duplicateChecker.TryGetValue(enumType.Name, out string? dupFullName)) | ||
{ | ||
Console.WriteLine($""" | ||
WARNING: Duplicate enum name for {enumType.Name}: | ||
- {enumType.FullName} | ||
- {dupFullName} | ||
"""); | ||
continue; | ||
} | ||
|
||
var enumValues = Enum.GetValues(enumType); | ||
|
||
// Write all values to output | ||
outputText.AppendLine($$""" | ||
enum CS{{enumType.Name}} : {{GetCEnumType(Enum.GetUnderlyingType(enumType).ToString())}} | ||
{ | ||
"""); | ||
foreach (var enumValue in enumValues) | ||
{ | ||
outputText.AppendLine($" {enumValue} = {((Enum)enumValue).ToString("d")},"); | ||
} | ||
outputText.AppendLine(""" | ||
}; | ||
"""); | ||
|
||
outputText.AppendLine($$""" | ||
class ECS{{enumType.Name}} | ||
{ | ||
public: | ||
static const char* ToString(CS{{enumType.Name}} enumValue) | ||
{ | ||
switch (enumValue) | ||
{ | ||
"""); | ||
foreach (var enumValue in enumValues) | ||
{ | ||
outputText.AppendLine($" case {enumValue}: return \"{enumValue}\";"); | ||
} | ||
outputText.AppendLine(""" | ||
} | ||
return "Unknown Value"; | ||
} | ||
}; | ||
"""); | ||
|
||
duplicateChecker.Add(enumType.Name, enumType.FullName!); | ||
} | ||
|
||
if (!Directory.Exists(monogamePlatformDir)) | ||
Directory.CreateDirectory(monogamePlatformDir); | ||
|
||
File.WriteAllText(Path.Combine(monogamePlatformDir, "csharp_enums.h"), outputText.ToString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
typedef char cschar; | ||
typedef unsigned char csbyte; | ||
typedef short csshort; | ||
typedef unsigned short csushort; | ||
typedef int csint; | ||
typedef unsigned int csuint; | ||
typedef long long cslong; | ||
typedef unsigned long long csulong; | ||
typedef bool csbool; |
Oops, something went wrong.