Skip to content

Instantly share code, notes, and snippets.

@tunabrain
Last active February 2, 2024 12:13
Show Gist options
  • Select an option

  • Save tunabrain/1fc7a4964914d61b5ae751d0c84f2382 to your computer and use it in GitHub Desktop.

Select an option

Save tunabrain/1fc7a4964914d61b5ae751d0c84f2382 to your computer and use it in GitHub Desktop.

Revisions

  1. tunabrain revised this gist Aug 4, 2020. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions wrap_openvr.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,25 @@
    #! python3

    # Copyright 2020 Benedikt Bitterli
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in all
    # copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    # SOFTWARE.

    import shlex
    import sys
    import re
  2. tunabrain revised this gist Dec 20, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wrap_openvr.py
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@

    annoyingMacroPattern = re.compile(r'#define\s+(\w+).*VR_CLANG_ATTR.*')
    fullClassPattern = re.compile(r'^([^\S\n]*)class\s+(\w+).*?\{.*?\};', re.MULTILINE | re.DOTALL)
    virtualPattern = re.compile(r'([^\S\n]*)virtual\s(.*?)(\w+)(\((.+?,\s*)*.*?\s*\))\s*=\s*0\s*;[^\S\n]*', re.MULTILINE)
    optionalParamPattern = re.compile(r'\s*=\s*[^,)]+')
    virtualPattern = re.compile(r'([^\S\n]*)virtual\s(.*?)(\w+)(\((.+?\s*(sizeof\s*\(\s*.*?\s*\)\s*)?,\s*)*.*?\s*(sizeof\s*\(\s*.*?\s*\)\s*)?\))\s*=\s*0\s*;[^\S\n]*', re.MULTILINE)
    optionalParamPattern = re.compile(r'\s*=\s*(sizeof\s*\(\s*.*?\s*\)\s*)?[^,)]+')
    versionPattern = re.compile(r'\s*static\s*const\s*char\s*\*\s*const\s*IVR\w+\s*=\s*\"IVR\w+\";')

    annoyingMacros = [match.group(1) for match in annoyingMacroPattern.finditer(header)]
  3. tunabrain created this gist Jun 7, 2016.
    62 changes: 62 additions & 0 deletions wrap_openvr.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #! python3

    import shlex
    import sys
    import re
    import io

    header = open('openvr.h', newline='\n').read()

    annoyingMacroPattern = re.compile(r'#define\s+(\w+).*VR_CLANG_ATTR.*')
    fullClassPattern = re.compile(r'^([^\S\n]*)class\s+(\w+).*?\{.*?\};', re.MULTILINE | re.DOTALL)
    virtualPattern = re.compile(r'([^\S\n]*)virtual\s(.*?)(\w+)(\((.+?,\s*)*.*?\s*\))\s*=\s*0\s*;[^\S\n]*', re.MULTILINE)
    optionalParamPattern = re.compile(r'\s*=\s*[^,)]+')
    versionPattern = re.compile(r'\s*static\s*const\s*char\s*\*\s*const\s*IVR\w+\s*=\s*\"IVR\w+\";')

    annoyingMacros = [match.group(1) for match in annoyingMacroPattern.finditer(header)]

    newHeader = header
    for match in versionPattern.finditer(newHeader):
    newHeader = newHeader.replace(match.group(0), match.group(0).replace('"IVR', '"FnTable:IVR'))

    for match in fullClassPattern.finditer(header):
    if match.group(0).find('virtual') == -1:
    continue
    fullClass = match.group(0)
    indent = match.group(1)
    className = match.group(2)

    newClass = fullClass

    declarations = []
    for function in virtualPattern.finditer(fullClass):
    returnType = function.group(2)
    functionName = function.group(3)
    params = function.group(4)
    paramsNoDefault = re.sub(optionalParamPattern, '', params)
    for macro in annoyingMacros:
    paramsNoDefault = re.sub(macro + r'\(.*?\)', '', paramsNoDefault)

    args = []
    paramTokens = list(shlex.shlex(io.StringIO(paramsNoDefault)))
    for i, token in enumerate(paramTokens):
    if token == ',' or (token == ')' and paramTokens[i - 1] not in ['(', 'void']):
    args.append(paramTokens[i - 1])

    declaration = indent + '\t{}(__stdcall *{}){};'.format(returnType, functionName, paramsNoDefault)
    definition = indent + '\t' + returnType + functionName + params + ' { '
    if returnType.strip() != 'void':
    definition = definition + 'return '
    definition = definition + '_table.' + functionName + '(' + ', '.join(args) + '); }'

    declarations.append(declaration)
    newClass = newClass.replace(function.group(0), definition)

    tableName = 'VR_{}_FnTable'.format(className)
    table = indent + 'struct ' + tableName + '\n' + indent + '{\n' + '\n'.join(declarations) + '\n' + indent + '};\n\n'

    newClass = newClass.replace('public:', '\t{} _table;\n{}public:'.format(tableName, indent))

    newHeader = newHeader.replace(fullClass, table + newClass)

    open('openvr_mingw.hpp', 'w', newline='\n').write(newHeader)