Skip to content

Commit

Permalink
Merge branch 'feature-native-enumexporter' of https://github.com/harr…
Browse files Browse the repository at this point in the history
…y-cpp/MonoGame into native
  • Loading branch information
tomspilman committed May 19, 2024
2 parents 94c4ada + 802cd49 commit 4305687
Show file tree
Hide file tree
Showing 11 changed files with 2,359 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Generator: CTypes",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "generator-ctypes",
"program": "${workspaceFolder}/Tools/MonoGame.Generator.CTypes/bin/Debug/net8.0/MonoGame.Generator.CTypes",
"args": [],
"cwd": "${workspaceFolder}/Tools/MonoGame.Generator.CTypes/bin/Debug/net8.0",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "MGCB Editor (Mac)",
"type": "coreclr",
Expand Down
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "generator-ctypes",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Tools/MonoGame.Generator.CTypes/MonoGame.Generator.CTypes.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
11 changes: 11 additions & 0 deletions MonoGame.Framework.Native.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Framework.Native", "MonoGame.Framework\MonoGame.Framework.Native.csproj", "{56BA741D-6AF1-489B-AB00-338DE11B1D32}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{65B3DC17-24BA-4C39-810F-E371AC48199A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Generator.CTypes", "Tools\MonoGame.Generator.CTypes\MonoGame.Generator.CTypes.csproj", "{74F12E34-D96B-4EC1-A218-BAFC83DC6220}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +22,12 @@ Global
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|Any CPU.Build.0 = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{74F12E34-D96B-4EC1-A218-BAFC83DC6220} = {65B3DC17-24BA-4C39-810F-E371AC48199A}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions MonoGame.Framework/Platform/Native/Interop.cs
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);
}
95 changes: 95 additions & 0 deletions Tools/MonoGame.Generator.CTypes/EnumWritter.cs
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 Tools/MonoGame.Generator.CTypes/MonoGame.Generator.CTypes.csproj
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>
47 changes: 47 additions & 0 deletions Tools/MonoGame.Generator.CTypes/Program.cs
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));
26 changes: 26 additions & 0 deletions Tools/MonoGame.Generator.CTypes/StructWritter.cs
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;
}
}
17 changes: 17 additions & 0 deletions Tools/MonoGame.Generator.CTypes/Util.cs
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
};
}
11 changes: 11 additions & 0 deletions src/monogame/include/csharp_common.h
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;
Loading

0 comments on commit 4305687

Please sign in to comment.