''' Created on Mar 12, 2013 Modified on Feb 12, 2026 @author: bli ''' import sys, os, argparse from os.path import exists, join, dirname, abspath ScriptDir = abspath(dirname(__file__)) g_PosixLinebreak = '\n' class WwiseIDConverter(object): '''Convert Wwise soundbank ID header from C++ to C#. Save it to Assets/Scripts/Generated.''' def __init__(self, inHeader): self.inHeader = abspath(inHeader) # --- 修改开始: 自动定位到 Assets/Scripts/Generated --- # 1. 初始化目标目录变量 outDir = None # 2. 从输入文件的目录开始向上查找 "Assets" 文件夹 current_dir = dirname(self.inHeader) # 简单的循环向上查找 while len(current_dir) > 4: # 防止死循环 (保留根路径长度) head, tail = os.path.split(current_dir) if tail == 'Assets': # 找到了 Assets 文件夹,构建目标路径 outDir = join(current_dir, 'Scripts', 'Generated') break if head == current_dir: # 已经到达磁盘根目录 break current_dir = head # 3. 如果没找到 Assets 文件夹 (比如 .h 文件在项目外部), # 则默认使用相对于当前工作目录的绝对路径 if outDir is None: outDir = abspath(join('Assets', 'Scripts', 'Generated')) self.outHeader = join(outDir, 'Wwise_IDs.cs') print('Target Output File: ' + self.outHeader) # --- 修改结束 --- def Convert(self): lines = self._ImportFile(self.inHeader) # Extract ID part IDStartKey = 'namespace' startLine = self._FindKeyLine(lines, IDStartKey) IDEndKey = '#endif' endLine = self._FindKeyLine(lines, IDEndKey) lines = lines[startLine : endLine] # Use C# class for namespace CType = 'namespace' CSType = 'public class' self._ReplaceLineByLine(lines, CType, CSType) # Replace "public class AK" with "namespace AK" CType = 'public class AK' CSType = 'namespace AK' self._ReplaceLineByLine(lines, CType, CSType) # Replace AK type with C# types CType = 'static const AkUniqueID' CSType = 'public static uint' self._ReplaceLineByLine(lines, CType, CSType) # 确保输出目录存在 (Convert 方法里已有的逻辑会处理新建文件夹) outDir = abspath(dirname(self.outHeader)) if not os.path.exists(outDir): try: os.makedirs(outDir) print('Created directory: ' + outDir) except OSError as e: print('Error creating directory: ' + str(e)) return self._ExportFile(self.outHeader, lines) def _ImportFile(self, inputFile): rawLines = [] with open(inputFile) as f: rawLines = f.readlines() f.close() return rawLines def _ExportFile(self, outputFile, outputLines): # append line separators if none for ll in range(len(outputLines)): hasNoLinebreak = outputLines[ll].find(os.linesep) == -1 and outputLines[ll].find(g_PosixLinebreak) == -1 if hasNoLinebreak: outputLines[ll] += g_PosixLinebreak with open(outputFile, 'w') as f: f.writelines(outputLines) f.close() def _FindKeyLine(self, lines, key): keyLineNumber = 0 for ll in range(len(lines)): foundKey = lines[ll].find(key) != -1 if foundKey: keyLineNumber = ll break return keyLineNumber def _ReplaceLineByLine(self, lines, inPattern, outPattern): for ll in range(len(lines)): namespaceStartCol = lines[ll].find(inPattern) foundNamespace = namespaceStartCol != -1 if foundNamespace: lines[ll] = lines[ll].replace(inPattern, outPattern) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Convert Wwise SoundBank ID C++ header into C# for Unity. Generated in Assets/Scripts/Generated.') parser.add_argument('WwiseIDHeader', action='store', default='UndefinedHeader', help='Full path to Wwise SoundBank ID C++ header, e.g., Wwise_IDs.h') args = parser.parse_args() inHeader = args.WwiseIDHeader if not exists(inHeader): raise RuntimeError('Input header file does not exist: {}'.format(inHeader)) converter = WwiseIDConverter(inHeader) converter.Convert()