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.
Merge branch 'feature-native-enumexporter' of https://github.com/harr…
…y-cpp/MonoGame into native
- Loading branch information
Showing
11 changed files
with
2,359 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
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
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
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,15 @@ | ||
|
||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.Marshalling; | ||
|
||
namespace MonoGame.Interop; | ||
|
||
internal readonly struct GamePtr { } | ||
|
||
internal readonly struct GameWindowPtr { } | ||
|
||
internal static unsafe partial class GameWrapper | ||
{ | ||
[LibraryImport("monogame", StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void MG_GW_SetAllowUserResizing(GamePtr* game, GameWindowPtr* gameWindow, [MarshalAs(UnmanagedType.U1)] bool allowuserresizing); | ||
} |
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,95 @@ | ||
using System.Text; | ||
|
||
namespace MonoGame.Generator.CTypes; | ||
|
||
class EnumWritter | ||
{ | ||
private readonly StringBuilder _outputText; | ||
private readonly Dictionary<string, string> _duplicateChecker; | ||
|
||
public EnumWritter() | ||
{ | ||
_outputText = new StringBuilder($""" | ||
// | ||
// 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" | ||
"""); | ||
_duplicateChecker = []; | ||
} | ||
|
||
public static bool IsValid(Type type) | ||
{ | ||
return type.IsEnum && !type.IsNested; | ||
} | ||
|
||
public bool Append(Type type) | ||
{ | ||
if (!IsValid(type)) | ||
return false; | ||
|
||
if (_duplicateChecker.TryGetValue(type.Name, out string? dupFullName)) | ||
{ | ||
if (type.FullName != type.FullName) | ||
{ | ||
Console.WriteLine($""" | ||
WARNING: Duplicate enum name for {type.Name}: | ||
- {type.FullName} | ||
- {dupFullName} | ||
"""); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
var enumValues = Enum.GetValues(type); | ||
|
||
// Write all values to output | ||
_outputText.AppendLine($$""" | ||
enum CS{{type.Name}} : {{Util.GetCEnumType(Enum.GetUnderlyingType(type).ToString())}} | ||
{ | ||
"""); | ||
foreach (var enumValue in enumValues) | ||
{ | ||
_outputText.AppendLine($" {enumValue} = {((Enum)enumValue).ToString("d")},"); | ||
} | ||
_outputText.AppendLine(""" | ||
}; | ||
"""); | ||
|
||
_outputText.AppendLine($$""" | ||
class ECS{{type.Name}} | ||
{ | ||
public: | ||
static const char* ToString(CS{{type.Name}} enumValue) | ||
{ | ||
switch (enumValue) | ||
{ | ||
"""); | ||
foreach (var enumValue in enumValues) | ||
{ | ||
_outputText.AppendLine($" case {enumValue}: return \"{enumValue}\";"); | ||
} | ||
_outputText.AppendLine(""" | ||
} | ||
return "Unknown Value"; | ||
} | ||
}; | ||
"""); | ||
|
||
_duplicateChecker.Add(type.Name, type.FullName!); | ||
return true; | ||
} | ||
|
||
public void Flush(string dirPath) => File.WriteAllText(Path.Combine(dirPath, "csharp_enums.h"), _outputText.ToString()); | ||
} |
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,47 @@ | ||
using System.Reflection; | ||
using MonoGame.Generator.CTypes; | ||
|
||
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 enumWritter = new EnumWritter(); | ||
var structWrittter = new StructWritter(enumWritter); | ||
|
||
foreach (var type in assembly.GetTypes()) | ||
{ | ||
if (type.FullName!.Contains("MonoGame.Interop")) | ||
{ | ||
// Console.WriteLine(enumType.FullName!); | ||
|
||
|
||
//Console.WriteLine(type.Name + ": " + StructWritter.IsValid(type)); | ||
|
||
if (!type.IsClass) | ||
continue; | ||
|
||
foreach (var method in type.GetMethods()) | ||
{ | ||
if (!method.IsStatic) | ||
continue; | ||
|
||
Console.WriteLine(method.Name); | ||
|
||
foreach (var parm in method.GetParameters()) | ||
{ | ||
Console.WriteLine(parm.ParameterType.Name + " " + parm.Name); | ||
//Console.WriteLine(parm.ParameterType.Name + ": " + StructWritter.IsValid(parm.ParameterType)); | ||
} | ||
} | ||
} | ||
|
||
if (EnumWritter.IsValid(type)) | ||
{ | ||
enumWritter.Append(type); | ||
} | ||
} | ||
|
||
if (!Directory.Exists(monogamePlatformDir)) | ||
Directory.CreateDirectory(monogamePlatformDir); | ||
|
||
enumWritter.Flush(Path.Combine(monogamePlatformDir)); |
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,26 @@ | ||
using System.Text; | ||
|
||
namespace MonoGame.Generator.CTypes; | ||
|
||
class StructWritter | ||
{ | ||
private EnumWritter _enumWritter; | ||
|
||
public StructWritter(EnumWritter enumWritter) | ||
{ | ||
_enumWritter = enumWritter; | ||
} | ||
|
||
public static bool IsValid(Type type) | ||
{ | ||
return type.IsValueType && !type.IsPrimitive && !type.IsNested; | ||
} | ||
|
||
public bool Append(Type type) | ||
{ | ||
if (!IsValid(type)) | ||
return false; | ||
|
||
return true; | ||
} | ||
} |
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,17 @@ | ||
|
||
namespace MonoGame.Generator.CTypes; | ||
|
||
class Util | ||
{ | ||
public static string GetCEnumType(string cstype) => 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 | ||
}; | ||
} |
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.