Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-cpp committed Mar 12, 2024
1 parent 0cf7078 commit 802cd49
Show file tree
Hide file tree
Showing 8 changed files with 1,090 additions and 971 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"
}
]
}
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());
}
105 changes: 24 additions & 81 deletions Tools/MonoGame.Generator.CTypes/Program.cs
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));
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
};
}
Loading

0 comments on commit 802cd49

Please sign in to comment.