Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ obj/
.vs/
__pycache__/
/CodeGen/steam/lib/
CodeGen/.vscode/settings.json
5 changes: 5 additions & 0 deletions CodeGen/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true
charset = utf-8
indent_size = 4
indent_style = space
tab_width = 4
15 changes: 15 additions & 0 deletions CodeGen/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Generator",
"type": "debugpy",
"request": "launch",
"program": "Steamworks.NET_CodeGen.py",
"console": "integratedTerminal"
}
]
}
107 changes: 101 additions & 6 deletions CodeGen/src/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,20 @@
HEADER = None

g_NativeMethods = []

# AnyCPU wrappers of steam native function
g_NativeAnyCpu = []
# Underlying dll import class of AnyCPU, used to hold platform specific DllImports
# parsed result are shared between 64bit and 32bit
g_NativeMethodsPlatform = []

g_Output = []
g_Typedefs = None

def main(parser):
try:
os.makedirs("../com.rlabrecque.steamworks.net/Runtime/autogen/")
os.makedirs("../Standalone/AnyCPU/autogen/")
except OSError:
pass

Expand All @@ -564,13 +572,51 @@ def main(parser):

with open("../com.rlabrecque.steamworks.net/Runtime/autogen/NativeMethods.cs", "wb") as out:
#out.write(bytes(HEADER, "utf-8"))
with open("templates/nativemethods.txt", "r") as f:
with open("templates/nativemethods.txt", "r", encoding="utf-8") as f:
out.write(bytes(f.read(), "utf-8"))
with open("templates/nativemethods_dllimport.txt", "r", encoding="utf-8") as f:
out.write(bytes(f.read(), "utf-8"))
for line in g_NativeMethods:
out.write(bytes(line + "\n", "utf-8"))
out.write(bytes("\t}\n", "utf-8"))
out.write(bytes("}\n\n", "utf-8"))
out.write(bytes("#endif // !DISABLESTEAMWORKS\n", "utf-8"))

with open("../Standalone/AnyCPU/autogen/NativeMethods.cs", "wb") as anycpu:
with open("templates/nativemethods_anycpu.txt", "r", encoding="utf-8") as f:
anycpu.write(bytes(f.read(), "utf-8"))

for line in g_NativeAnyCpu:
anycpu.write(bytes(line + "\n", "utf-8"))
anycpu.write(bytes("\t}\n", "utf-8"))
anycpu.write(bytes("}\n\n", "utf-8"))
anycpu.write(bytes("#endif // !DISABLESTEAMWORKS\n", "utf-8"))

with open("../Standalone/AnyCPU/autogen/NativeMethodsUnderlyingWin64.cs", "wb") as anycpu_win64:
with open("templates/nativemethods_win64.txt", "r", encoding="utf-8") as f:
anycpu_win64.write(bytes(f.read(), "utf-8"))
with open("templates/nativemethods_dllimport.txt", "r", encoding="utf-8") as f:
anycpu_win64.write(bytes(f.read(), "utf-8"))

for line in g_NativeMethodsPlatform:
anycpu_win64.write(bytes(line + "\n", "utf-8"))
anycpu_win64.write(bytes("\t}\n", "utf-8"))
anycpu_win64.write(bytes("}\n\n", "utf-8"))
anycpu_win64.write(bytes("#endif // !DISABLESTEAMWORKS\n", "utf-8"))

with open("../Standalone/AnyCPU/autogen/NativeMethodsUnderlying.cs", "wb") as anycpu_general:
with open("templates/nativemethods_general.txt", "r", encoding="utf-8") as f:
anycpu_general.write(bytes(f.read(), "utf-8"))
with open("templates/nativemethods_dllimport.txt", "r", encoding="utf-8") as f:
anycpu_general.write(bytes(f.read(), "utf-8"))

for line in g_NativeMethodsPlatform:
anycpu_general.write(bytes(line + "\n", "utf-8"))
anycpu_general.write(bytes("\t}\n", "utf-8"))
anycpu_general.write(bytes("}\n\n", "utf-8"))
anycpu_general.write(bytes("#endif // !DISABLESTEAMWORKS\n", "utf-8"))



def get_arg_attribute(strEntryPoint, arg):
return g_FixedAttributeValues.get(strEntryPoint, dict()).get(arg.name, arg.attribute)
Expand Down Expand Up @@ -610,6 +656,7 @@ def parse_interface(f, interface):

if not bGameServerVersion:
g_NativeMethods.append("#region " + interface.name[1:])
g_NativeMethodsPlatform.append("#region " + interface.name[1:])

lastIfStatement = None
for func in interface.functions:
Expand Down Expand Up @@ -652,6 +699,7 @@ def parse_interface(f, interface):

if not bGameServerVersion:
g_NativeMethods.append("#endregion")
g_NativeMethodsPlatform.append("#endregion")

g_Output.append("\t}")

Expand Down Expand Up @@ -686,23 +734,69 @@ def parse_func(f, interface, func):
wrapperreturntype = returntype

args = parse_args(strEntryPoint, func.args)
pinvokeargs = args[0] # TODO: NamedTuple
pinvokeargs: str = args[0] # TODO: NamedTuple
wrapperargs = args[1]
argnames = args[2]
argnames: str = args[2]
stringargs = args[3]
outstringargs = args[4][0]
outstringsize = args[4][1]
args_with_explicit_count = args[5]

if not bGameServerVersion:
# generate steam api import
g_NativeMethods.append("\t\t[DllImport(NativeLibraryName, EntryPoint = \"SteamAPI_{0}\", CallingConvention = CallingConvention.Cdecl)]".format(strEntryPoint))

g_NativeMethodsPlatform.append("\t\t[DllImport(NativeLibraryName, EntryPoint = \"SteamAPI_{0}\", CallingConvention = CallingConvention.Cdecl)]".format(strEntryPoint))

if returntype == "bool":
g_NativeMethods.append("\t\t[return: MarshalAs(UnmanagedType.I1)]")
g_NativeMethodsPlatform.append("\t\t[return: MarshalAs(UnmanagedType.I1)]")

g_NativeMethods.append("\t\tpublic static extern {0} {1}({2});".format(returntype, strEntryPoint, pinvokeargs))
g_NativeMethods.append("")


g_NativeMethodsPlatform.append("")
g_NativeMethodsPlatform.append("\t\tpublic static extern {0} {1}({2});".format(returntype, strEntryPoint, pinvokeargs))
g_NativeMethodsPlatform.append("")

# generate AnyCPU specific code
# build parameter list of wrapped steam function
# sanitize PInvoke attributes first
strAnyCpuCallParameterList = ""
for arg in pinvokeargs.replace("[In, Out]", "").replace("[In]", "").replace("[Out]", "").split(", "):
arg = arg
# cut argument name out
argnameBegin = arg.rfind(" ") + 1

wrapperArgName = arg[argnameBegin:]
# handle `ref` or `out` arguments
keywordRefOrOut = ""
if arg.startswith("ref"):
keywordRefOrOut = "ref "
elif arg.startswith("out"):
keywordRefOrOut = "out "
strAnyCpuCallParameterList += keywordRefOrOut + wrapperArgName + ", "
strAnyCpuCallParameterList = strAnyCpuCallParameterList.rstrip(", ")

callSteamApiExpression = "{0}({1})".format(strEntryPoint, strAnyCpuCallParameterList)

# for functions that have return value, generate return statement in wrapper
optionalReturn = ""
if returntype != "void":
optionalReturn = "return "

# build AnyCPU wrapper method body(without braces)
anyCpuWarpperBody = []
anyCpuWarpperBody.append("\t\t\tif (Environment.Is64BitProcess && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {")
anyCpuWarpperBody.append("\t\t\t\t" + optionalReturn + "NativeMethodsUnderlyingWin64." + callSteamApiExpression + ";")
anyCpuWarpperBody.append("\t\t\t} else {")
anyCpuWarpperBody.append("\t\t\t\t" + optionalReturn + "NativeMethodsUnderlying." + callSteamApiExpression + ";")
anyCpuWarpperBody.append("\t\t\t}")

strAnyCpuWrapperBody = ""
for line in anyCpuWarpperBody:
strAnyCpuWrapperBody += line + "\n"

g_NativeAnyCpu.append("\t\tpublic static {0} {1}({2}) {{\n{3}\t\t}}\n".format(returntype, strEntryPoint, pinvokeargs, strAnyCpuWrapperBody))
functionBody = []

if 'GameServer' in interface.name:
Expand Down Expand Up @@ -926,7 +1020,8 @@ def parse_args(strEntryPoint, args):

if __name__ == "__main__":
if len(sys.argv) != 2:
print("TODO: Usage Instructions")
print("Usage: indirectly invoked by Steamworks.NET_CodeGen.py" +
"or python3 src/interfaces.py <path/to/steam_headers/>\n")
exit()

steamworksparser.Settings.fake_gameserver_interfaces = True
Expand Down
16 changes: 14 additions & 2 deletions CodeGen/src/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,25 @@ def parse(struct):

packsize = g_CustomPackSize.get(structname, "Packsize.value")
if g_ExplicitStructs.get(structname, False):
lines.append("#if !STEAMWORKS_STANDALONE_ANYCPU")
lines.append("\t[StructLayout(LayoutKind.Explicit, Pack = " + packsize + ")]")
lines.append("#else")
lines.append("\t[StructLayout(LayoutKind.Explicit)]")
lines.append("#endif")
elif struct.packsize:
customsize = ""
if len(struct.fields) == 0:
customsize = ", Size = 1"
lines.append("\t[StructLayout(LayoutKind.Sequential, Pack = " + packsize + customsize + ")]")


if packsize == "Packsize.value":
lines.append("#if STEAMWORKS_STANDALONE_ANYCPU")
lines.append("\t[StructLayout(LayoutKind.Sequential" + customsize + ")]")
lines.append("#else")
lines.append("\t[StructLayout(LayoutKind.Sequential, Pack = " + packsize + customsize + ")]")
lines.append("#endif")
else:
lines.append("\t[StructLayout(LayoutKind.Sequential, Pack = " + packsize + customsize + ")]")

if struct.callbackid:
lines.append("\t[CallbackIdentity(Constants." + struct.callbackid + ")]")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace Steamworks
/// need to know what's inside. (Indeed, we don't really want them to
/// know, as it could reveal information useful to an attacker.)
[System.Serializable]
#if !STEAMWORKS_STANDALONE_ANYCPU
[StructLayout(LayoutKind.Sequential, Pack = Packsize.value)]
#else
[StructLayout(LayoutKind.Sequential)]
#endif
public struct SteamDatagramHostedAddress
{
// Size of data blob.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace Steamworks
/// need to know what's inside. (Indeed, we don't really want them to
/// know, as it could reveal information useful to an attacker.)
[System.Serializable]
#if !STEAMWORKS_STANDALONE_ANYCPU
[StructLayout(LayoutKind.Sequential, Pack = Packsize.value)]
#else
[StructLayout(LayoutKind.Sequential)]
#endif
public struct SteamDatagramRelayAuthTicket
{
/// Identity of the gameserver we want to talk to. This is required.
Expand Down Expand Up @@ -81,7 +85,11 @@ public struct SteamDatagramRelayAuthTicket
// we collect to partners, but we hope to in the future so that you can
// get visibility into network conditions.)
//
#if !STEAMWORKS_STANDALONE_ANYCPU
[StructLayout(LayoutKind.Sequential, Pack = Packsize.value)]
#else
[StructLayout(LayoutKind.Sequential)]
#endif
struct ExtraField
{
enum EType
Expand Down
2 changes: 1 addition & 1 deletion CodeGen/templates/header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// This file is automatically generated.
// Changes to this file will be reverted when you update Steamworks.NET

#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX)
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX || STEAMWORKS_STANDALONE_ANYCPU)
#define DISABLESTEAMWORKS
#endif

Expand Down
Loading