From 87d1cc6306ee2cfcb214c3216a9fc589158ccaba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Mon, 29 Nov 2010 15:57:25 +0000 Subject: [PATCH] Get d3d9 in buildable state again. --- CMakeLists.txt | 20 ++++----- d3d9.py | 20 ++++++--- stdapi.py | 17 ++++++-- trace.py | 127 +++++++++++++++++++++++++++++++++++++-------------------- 4 files changed, 120 insertions(+), 64 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 78a6f2d..1c04b50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,16 +102,16 @@ if (WIN32) #endif (DirectX_D3D8_FOUND) # d3d9.dll - #if (DirectX_D3DX9_FOUND) - # include_directories (${DirectX_D3DX9_INCLUDE_DIR}) - # add_custom_command ( - # OUTPUT d3d9.cpp - # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9.cpp - # DEPENDS d3d9.py d3d9types.py d3d9caps.py d3dshader.py winapi.py stdapi.py - # ) - # add_library (d3d9 SHARED d3d9.def d3d9.cpp trace_write.cpp os_win32.cpp) - # set_target_properties (d3d9 PROPERTIES PREFIX "") - #endif (DirectX_D3DX9_FOUND) + if (DirectX_D3DX9_FOUND) + include_directories (${DirectX_D3DX9_INCLUDE_DIR}) + add_custom_command ( + OUTPUT d3d9.cpp + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9.cpp + DEPENDS d3d9.py trace.py d3d9types.py d3d9caps.py d3dshader.py winapi.py stdapi.py + ) + add_library (d3d9 SHARED d3d9.def d3d9.cpp trace_write.cpp os_win32.cpp) + set_target_properties (d3d9 PROPERTIES PREFIX "") + endif (DirectX_D3DX9_FOUND) # d3d10.dll #if (DirectX_D3D10_FOUND) diff --git a/d3d9.py b/d3d9.py index 572d328..1fae2d6 100644 --- a/d3d9.py +++ b/d3d9.py @@ -26,11 +26,11 @@ """d3d9.h""" from winapi import * -from d3dshader import * from d3d9types import * from d3d9caps import * +from trace import DllTracer -D3DSHADER9 = D3DShader(9) +D3DSHADER9 = Opaque("const DWORD *") HRESULT = Enum("HRESULT", [ "D3D_OK", @@ -390,8 +390,8 @@ IDirect3DSwapChain9Ex.methods += [ Method(HRESULT, "GetDisplayModeEx", [Out(Pointer(D3DDISPLAYMODEEX), "pMode"), Out(Pointer(D3DDISPLAYROTATION), "pRotation")]), ] -d3d9 = Dll("d3d9") -d3d9.functions += [ +d3d9 = API("d3d9") +d3d9.add_functions([ StdFunction(PDIRECT3D9, "Direct3DCreate9", [(UINT, "SDKVersion")], fail='NULL'), StdFunction(HRESULT, "Direct3DCreate9Ex", [(UINT, "SDKVersion"), Out(Pointer(PDIRECT3D9EX), "ppD3D")], fail='D3DERR_NOTAVAILABLE'), StdFunction(Int, "D3DPERF_BeginEvent", [(D3DCOLOR, "col"), (LPCWSTR, "wszName")], fail='-1'), @@ -401,7 +401,14 @@ d3d9.functions += [ StdFunction(BOOL, "D3DPERF_QueryRepeatFrame", [], fail='FALSE'), StdFunction(Void, "D3DPERF_SetOptions", [(DWORD, "dwOptions")], fail=''), StdFunction(DWORD, "D3DPERF_GetStatus", [], fail='0'), -] +]) + + +class D3D9Tracer(DllTracer): + + pass + + if __name__ == '__main__': print '#include ' @@ -413,5 +420,6 @@ if __name__ == '__main__': print print '#include "trace_write.hpp"' print - wrap() + tracer = D3D9Tracer('d3d9.dll') + tracer.trace_api(d3d9) diff --git a/stdapi.py b/stdapi.py index 8dca958..e0d1f54 100644 --- a/stdapi.py +++ b/stdapi.py @@ -407,9 +407,12 @@ class Interface(Type): self.base = base self.methods = [] + def visit(self, visitor, *args, **kwargs): + return visitor.visit_interface(self, *args, **kwargs) + def itermethods(self): if self.base is not None: - for method in self.stdapi.itermethods(): + for method in self.base.itermethods(): yield method for method in self.methods: yield method @@ -420,7 +423,8 @@ class Method(Function): def __init__(self, type, name, args): Function.__init__(self, type, name, args, call = '__stdcall') - + for index in range(len(self.args)): + self.args[index].index = index + 1 towrap = [] @@ -516,7 +520,12 @@ class Collector(Visitor): pass def visit_interface(self, interface): - pass + if interface.base is not None: + self.visit(interface.base) + for method in interface.itermethods(): + for arg in method.args: + self.visit(arg.type) + self.visit(method.type) class API: @@ -535,7 +544,7 @@ class API: collector.visit(function.type) for interface in self.interfaces: collector.visit(interface) - for method in interface.methods: + for method in interface.itermethods(): for arg in method.args: collector.visit(arg.type) collector.visit(method.type) diff --git a/trace.py b/trace.py index 735d70e..361fd16 100644 --- a/trace.py +++ b/trace.py @@ -32,6 +32,9 @@ import stdapi all_types = {} +def interface_wrap_name(interface): + return "Wrap" + interface.expr + class DumpDeclarator(stdapi.OnceVisitor): '''Declare helper functions to dump complex types.''' @@ -116,7 +119,19 @@ class DumpDeclarator(stdapi.OnceVisitor): pass def visit_interface(self, interface): - pass + print "class %s : public %s " % (interface_wrap_name(interface), interface.name) + print "{" + print "public:" + print " %s(%s * pInstance);" % (interface_wrap_name(interface), interface.name) + print " virtual ~%s();" % interface_wrap_name(interface) + print + for method in interface.itermethods(): + print " " + method.prototype() + ";" + print + #print "private:" + print " %s * m_pInstance;" % (interface.name,) + print "};" + print class DumpImplementer(stdapi.Visitor): @@ -181,7 +196,7 @@ class DumpImplementer(stdapi.Visitor): print ' Trace::LiteralOpaque((const void *)%s);' % instance def visit_interface(self, interface, instance): - print ' Trace::LiteralOpaque((const void *)%s);' % instance + print ' Trace::LiteralOpaque((const void *)&%s);' % instance dump_instance = DumpImplementer().visit @@ -233,15 +248,20 @@ class Wrapper(stdapi.Visitor): pass def visit_interface(self, interface, instance): + assert instance.startswith('*') + instance = instance[1:] print " if(%s)" % instance - print " %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance) + print " %s = new %s(%s);" % (instance, interface_wrap_name(interface), instance) class Unwrapper(Wrapper): def visit_interface(self, interface, instance): + assert instance.startswith('*') + instance = instance[1:] print " if(%s)" % instance - print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance) + print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface_wrap_name(interface), instance) + wrap_instance = Wrapper().visit unwrap_instance = Unwrapper().visit @@ -264,10 +284,8 @@ class Tracer: print # Interfaces wrapers - map(self.interface_wrap_name, api.interfaces) - map(self.interface_pre_decl, api.interfaces) - map(self.interface_decl, api.interfaces) - map(self.interface_wrap_impl, api.interfaces) + interfaces = [type for type in types if isinstance(type, stdapi.Interface)] + map(self.interface_wrap_impl, interfaces) print # Function wrappers @@ -321,7 +339,7 @@ class Tracer: def trace_function_impl(self, function): pvalue = self.function_pointer_value(function) - print function.prototype() + ' {' + print 'extern "C" ' + function.prototype() + ' {' if function.args: print ' static const char * __args[%u] = {%s};' % (len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args])) else: @@ -376,33 +394,12 @@ class Tracer: def unwrap_ret(self, function, instance): unwrap_instance(function.type, instance) - def interface_wrap_name(self, interface): - return "Wrap" + interface.expr - - def interface_pre_decl(self, interface): - print "class %s;" % interface.wrap_name() - - def interface_decl(self, interface): - print "class %s : public %s " % (interface.wrap_name(), interface.name) - print "{" - print "public:" - print " %s(%s * pInstance);" % (interface.wrap_name(), interface.name) - print " virtual ~%s();" % interface.wrap_name() - print - for method in interface.itermethods(): - print " " + method.prototype() + ";" - print - #print "private:" - print " %s * m_pInstance;" % (interface.name,) - print "};" - print - def interface_wrap_impl(self, interface): - print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name) + print '%s::%s(%s * pInstance) {' % (interface_wrap_name(interface), interface_wrap_name(interface), interface.name) print ' m_pInstance = pInstance;' print '}' print - print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name()) + print '%s::~%s() {' % (interface_wrap_name(interface), interface_wrap_name(interface)) print '}' print for method in interface.itermethods(): @@ -410,13 +407,10 @@ class Tracer: print def trace_method(self, interface, method): - print method.prototype(interface.wrap_name() + '::' + method.name) + ' {' - if method.type is Void: - result = '' - else: - print ' %s __result;' % method.type - result = '__result = ' - print ' Trace::BeginCall("%s");' % (interface.name + '::' + method.name) + print method.prototype(interface_wrap_name(interface) + '::' + method.name) + ' {' + print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args])) + print ' static const Trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (int(method.id), interface.name + '::' + method.name, len(method.args) + 1) + print ' unsigned __call = Trace::BeginEnter(__sig);' print ' Trace::BeginArg(0);' print ' Trace::LiteralOpaque((const void *)m_pInstance);' print ' Trace::EndArg();' @@ -424,27 +418,72 @@ class Tracer: if not arg.output: self.unwrap_arg(method, arg) self.dump_arg(method, arg) + if method.type is stdapi.Void: + result = '' + else: + print ' %s __result;' % method.type + result = '__result = ' + print ' Trace::EndEnter();' print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args])) + print ' Trace::BeginLeave(__call);' for arg in method.args: if arg.output: self.dump_arg(method, arg) self.wrap_arg(method, arg) - if method.type is not Void: - print ' Trace::BeginReturn("%s");' % method.type + if method.type is not stdapi.Void: + print ' Trace::BeginReturn();' dump_instance(method.type, "__result") print ' Trace::EndReturn();' wrap_instance(method.type, '__result') - print ' Trace::EndCall();' + print ' Trace::EndLeave();' if method.name == 'QueryInterface': print ' if (*ppvObj == m_pInstance)' print ' *ppvObj = this;' if method.name == 'Release': - assert method.type is not Void + assert method.type is not stdapi.Void print ' if (!__result)' print ' delete this;' - if method.type is not Void: + if method.type is not stdapi.Void: print ' return __result;' print '}' print +class DllTracer(Tracer): + + def __init__(self, dllname): + self.dllname = dllname + + def get_function_address(self, function): + return '__GetProcAddress("%s")' % (function.name,) + + + def header(self, api): + Tracer.header(self, api) + + print ''' +static HINSTANCE g_hDll = NULL; + +static PROC +__GetProcAddress(LPCSTR lpProcName) +{ + if (!g_hDll) { + char szDll[MAX_PATH] = {0}; + + if (!GetSystemDirectoryA(szDll, MAX_PATH)) { + return NULL; + } + + strcat(szDll, "\\\\%s"); + + g_hDll = LoadLibraryA(szDll); + if (!g_hDll) { + return NULL; + } + } + + return GetProcAddress(g_hDll, lpProcName); +} + + ''' % self.dllname + -- 2.7.4