
import clr
import sys
import os
import System
from System import AppDomain
from System.Reflection import BindingFlags

def inspect_dll(dll_path):
    print(f"Inspecting: {dll_path}")
    try:
        clr.AddReference(dll_path)
        assembly = None
        for a in AppDomain.CurrentDomain.GetAssemblies():
            if "rapiplus" in a.FullName.lower():
                assembly = a
                break
        
        if not assembly:
            print("Assembly not found.")
            return

        target_name = "com.omnesys.rapi.AdmCallbacks"
        t = assembly.GetType(target_name)
        if not t:
            print(f"{target_name} not found.")
            return

        results = []
        results.append(f"Class: {t.FullName}")
        results.append(f"IsAbstract: {t.IsAbstract}")
        
        # 1. すべての抽象メンバー（メソッド、プロパティのアクセサ）
        results.append("\n--- ALL ABSTRACT MEMBERS ---")
        members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
        for m in members:
            is_abstract = False
            if hasattr(m, "IsAbstract"):
                is_abstract = m.IsAbstract
            elif m.MemberType == System.Reflection.MemberTypes.Method:
                is_abstract = m.IsAbstract
            
            if is_abstract:
                results.append(f"  [ABSTRACT] {m.MemberType} {m.Name}")
                if m.MemberType == System.Reflection.MemberTypes.Method:
                    params = [f"{p.ParameterType.Name} {p.Name}" for p in m.GetParameters()]
                    results.append(f"    Args: ({', '.join(params)}) returning {m.ReturnType.Name}")

        # 2. 継承しているインターフェース
        results.append("\n--- INTERFACES ---")
        for i in t.GetInterfaces():
            results.append(f"  Interface: {i.FullName}")
            for im in i.GetMethods():
                results.append(f"    Method in Interface: {im.Name}")

        # 3. 自身を継承している具象クラスがアセンブリ内にないか
        results.append("\n--- DERIVED CLASSES IN ASSEMBLY ---")
        for at in assembly.GetTypes():
            if at.IsSubclassOf(t) and not at.IsAbstract:
                results.append(f"  Derived Concrete Class: {at.FullName}")

        with open("inspect_adm_final.txt", "w") as f:
            f.write("\n".join(results))
        print("Done. Results in inspect_adm_final.txt")

    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    dll_path = os.path.join(os.getcwd(), "libs", "rapiplus.dll")
    inspect_dll(dll_path)
