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.
- Loading branch information
Showing
8 changed files
with
1,090 additions
and
971 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
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()); | ||
} |
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 |
---|---|---|
@@ -1,104 +1,47 @@ | ||
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 | ||
}; | ||
}; | ||
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 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" | ||
var enumWritter = new EnumWritter(); | ||
var structWrittter = new StructWritter(enumWritter); | ||
|
||
"""); | ||
|
||
foreach (var enumType in assembly.GetTypes()) | ||
foreach (var type 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)) | ||
if (type.FullName!.Contains("MonoGame.Interop")) | ||
{ | ||
Console.WriteLine($""" | ||
WARNING: Duplicate enum name for {enumType.Name}: | ||
- {enumType.FullName} | ||
- {dupFullName} | ||
// Console.WriteLine(enumType.FullName!); | ||
|
||
"""); | ||
continue; | ||
} | ||
|
||
//Console.WriteLine(type.Name + ": " + StructWritter.IsValid(type)); | ||
|
||
var enumValues = Enum.GetValues(enumType); | ||
if (!type.IsClass) | ||
continue; | ||
|
||
// 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(""" | ||
}; | ||
foreach (var method in type.GetMethods()) | ||
{ | ||
if (!method.IsStatic) | ||
continue; | ||
|
||
"""); | ||
Console.WriteLine(method.Name); | ||
|
||
outputText.AppendLine($$""" | ||
class ECS{{enumType.Name}} | ||
{ | ||
public: | ||
static const char* ToString(CS{{enumType.Name}} enumValue) | ||
{ | ||
switch (enumValue) | ||
foreach (var parm in method.GetParameters()) | ||
{ | ||
"""); | ||
foreach (var enumValue in enumValues) | ||
{ | ||
outputText.AppendLine($" case {enumValue}: return \"{enumValue}\";"); | ||
} | ||
outputText.AppendLine(""" | ||
Console.WriteLine(parm.ParameterType.Name + " " + parm.Name); | ||
//Console.WriteLine(parm.ParameterType.Name + ": " + StructWritter.IsValid(parm.ParameterType)); | ||
} | ||
return "Unknown Value"; | ||
} | ||
}; | ||
"""); | ||
} | ||
|
||
duplicateChecker.Add(enumType.Name, enumType.FullName!); | ||
if (EnumWritter.IsValid(type)) | ||
{ | ||
enumWritter.Append(type); | ||
} | ||
} | ||
|
||
if (!Directory.Exists(monogamePlatformDir)) | ||
Directory.CreateDirectory(monogamePlatformDir); | ||
|
||
File.WriteAllText(Path.Combine(monogamePlatformDir, "csharp_enums.h"), outputText.ToString()); | ||
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 | ||
}; | ||
} |
Oops, something went wrong.