### Get Method Pointers and Interop Information in C++ Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Shows how to obtain method pointers and interop-related information using the InterpreterModule. This includes getting native-callable method pointers, adjustor thunks for value types, method invokers for reflection, managed-to-native call methods, and checking interpreter implementation status. ```cpp #include "hybridclr/interpreter/InterpreterModule.h" // Get method pointers for interpreted methods void GetMethodBridges(const MethodInfo* method) { // Get the native-callable method pointer Il2CppMethodPointer methodPtr = hybridclr::interpreter::InterpreterModule::GetMethodPointer(method); // Get adjustor thunk for value type instance methods Il2CppMethodPointer adjustorThunk = hybridclr::interpreter::InterpreterModule::GetAdjustThunkMethodPointer(method); // Get method invoker for reflection InvokerMethod invoker = hybridclr::interpreter::InterpreterModule::GetMethodInvoker(method); // Get managed-to-native call method hybridclr::interpreter::Managed2NativeCallMethod m2n = hybridclr::interpreter::InterpreterModule::GetManaged2NativeMethodPointer( method, false // forceStatic ); // Check if method is implemented by interpreter bool isInterpreted = hybridclr::interpreter::InterpreterModule::IsImplementsByInterpreter(method); // Check native call support bool hasNative2Managed = hybridclr::interpreter::InterpreterModule::HasImplementCallNative2Managed(method); // Get reverse P/Invoke wrapper for callbacks Il2CppMethodPointer wrapper = hybridclr::interpreter::InterpreterModule::GetReversePInvokeWrapper( image, method, IL2CPP_CALL_DEFAULT); } ``` -------------------------------- ### Access Metadata Module API Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Provides examples for retrieving interpreter images, resolving metadata handles from encoded indices, and inspecting method or field information within the HybridCLR runtime. ```cpp #include "hybridclr/metadata/MetadataModule.h" void AccessMetadata() { hybridclr::metadata::InterpreterImage* image = hybridclr::metadata::MetadataModule::GetImage(imageIndex); image = hybridclr::metadata::MetadataModule::GetImage(il2cppImage); image = hybridclr::metadata::MetadataModule::GetImage(klass); image = hybridclr::metadata::MetadataModule::GetImage(methodInfo); const char* str = hybridclr::metadata::MetadataModule::GetStringFromEncodeIndex(index); Il2CppMetadataTypeHandle handle = hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromEncodeIndex(index); const Il2CppMethodDefinition* methodDef = hybridclr::metadata::MetadataModule::GetMethodDefinitionFromIndex(methodIndex); const MethodInfo* method = hybridclr::metadata::MetadataModule::GetMethodInfoFromVTableSlot(klass, vtableSlot); uint32_t offset = hybridclr::metadata::MetadataModule::GetFieldOffset(klass, fieldIndexInType, fieldInfo); bool isInterpreted = hybridclr::metadata::MetadataModule::IsImplementedByInterpreter(method); } ``` -------------------------------- ### Unity Hot Update Manager with HybridCLR Source: https://context7.com/focus-creative-games/hybridclr/llms.txt This C# script demonstrates a complete hot update integration for Unity using HybridCLR. It handles loading AOT metadata, the hot update assembly, pre-JITing critical code, and starting the hot update entry point. It relies on Unity's MonoBehaviour and Coroutines for asynchronous loading and HybridCLR's RuntimeApi for metadata and assembly loading. ```csharp // Complete Unity integration example using UnityEngine; using HybridCLR; using System; using System.Reflection; using System.IO; public class HotUpdateManager : MonoBehaviour { [SerializeField] private string[] aotMetadataAssemblies = { "mscorlib", "System", "System.Core" }; [SerializeField] private string hotUpdateAssemblyName = "HotUpdate"; void Start() { StartCoroutine(LoadHotUpdate()); } IEnumerator LoadHotUpdate() { // Step 1: Load supplemental AOT metadata for generic support yield return LoadAOTMetadata(); // Step 2: Load the hot update assembly yield return LoadHotUpdateAssembly(); // Step 3: Pre-JIT critical paths (optional optimization) yield return PreJitCriticalCode(); // Step 4: Start hot update entry point StartHotUpdateGame(); } IEnumerator LoadAOTMetadata() { foreach (string assemblyName in aotMetadataAssemblies) { byte[] dllBytes = LoadFromStreamingAssets($"{assemblyName}.dll.bytes"); if (dllBytes != null) { var result = RuntimeApi.LoadMetadataForAOTAssembly( dllBytes, HomologousImageMode.SuperSet ); Debug.Log($"Load AOT {assemblyName}: {result}"); } yield return null; } } IEnumerator LoadHotUpdateAssembly() { byte[] dllBytes = LoadFromStreamingAssets($"{hotUpdateAssemblyName}.dll.bytes"); byte[] pdbBytes = LoadFromStreamingAssets($"{hotUpdateAssemblyName}.pdb.bytes"); Assembly assembly = Assembly.Load(dllBytes, pdbBytes); Debug.Log($"Loaded hot update assembly: {assembly.FullName}"); yield return null; } IEnumerator PreJitCriticalCode() { Type entryType = Type.GetType($"GameEntry, {hotUpdateAssemblyName}"); if (entryType != null) { RuntimeApi.PreJitClass(entryType); } yield return null; } void StartHotUpdateGame() { Type entryType = Type.GetType($"GameEntry, {hotUpdateAssemblyName}"); MethodInfo entryMethod = entryType?.GetMethod("Start"); entryMethod?.Invoke(null, null); } byte[] LoadFromStreamingAssets(string fileName) { string path = Path.Combine(Application.streamingAssetsPath, fileName); if (File.Exists(path)) { return File.ReadAllBytes(path); } return null; } } ``` -------------------------------- ### Load and Initialize Interpreter Image in C++ Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Demonstrates loading and initializing an interpreter image using the InterpreterImage class. It covers allocating an image index, creating an InterpreterImage instance, loading DLL data, and registering the image. It also shows how to access loaded metadata like the Il2CppImage, index, and initialization status. ```cpp #include "hybridclr/metadata/InterpreterImage.h" // Load and initialize an interpreter image void LoadInterpreterImage(const void* dllData, size_t length) { // Allocate image index uint32_t imageIndex = hybridclr::metadata::InterpreterImage::AllocImageIndex(length); // Create new interpreter image hybridclr::metadata::InterpreterImage* image = new hybridclr::metadata::InterpreterImage(imageIndex); // Load the DLL data hybridclr::metadata::LoadImageErrorCode result = image->Load(dllData, length); if (result == hybridclr::metadata::LoadImageErrorCode::OK) { // Register the image hybridclr::metadata::InterpreterImage::RegisterImage(image); // Access loaded data const Il2CppImage* il2cppImage = image->GetIl2CppImage(); uint32_t index = image->GetIndex(); bool isInit = image->IsInitialized(); } } ``` -------------------------------- ### Pre-JIT Compilation for Hot Update Code Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Uses PreJitClass and PreJitMethod to compile IL to machine instructions ahead of time, preventing runtime stutters. This is typically performed during loading screens or initialization. ```csharp using HybridCLR; using System; using System.Reflection; public class PreJitOptimization : MonoBehaviour { void PrecompileHotUpdateCode() { Type hotUpdateType = Type.GetType("MyHotUpdateClass, HotUpdate"); RuntimeApi.PreJitClass(hotUpdateType); MethodInfo method = hotUpdateType.GetMethod("FrequentlyCalledMethod"); RuntimeApi.PreJitMethod(method); } IEnumerator PrecompileAllHotUpdateAssemblies() { Assembly hotUpdateAssembly = Assembly.Load("HotUpdate"); foreach (Type type in hotUpdateAssembly.GetTypes()) { RuntimeApi.PreJitClass(type); yield return null; } } } ``` -------------------------------- ### Initialize HybridCLR Runtime Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Initializes the HybridCLR runtime environment, including metadata, interpreter, and transform modules. This function must be invoked during the il2cpp initialization phase before any other HybridCLR features are used. ```cpp #include "hybridclr/Runtime.h" // Initialize HybridCLR runtime during Unity's il2cpp initialization void InitializeHybridCLR() { // This sets up: // - RuntimeApi internal calls for C# interop // - MetadataModule for assembly metadata handling // - InterpreterModule for IL interpretation // - TransformModule for IL-to-register instruction conversion hybridclr::Runtime::Initialize(); } ``` -------------------------------- ### Pre-JIT Compilation API Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Functions to pre-compile interpreted methods or classes to improve execution performance and prevent runtime stutters. ```APIDOC ## POST RuntimeApi.PreJitClass ### Description Pre-compiles all methods within a specified class to improve first-call performance. ### Method POST ### Endpoint RuntimeApi.PreJitClass(Type type) ### Parameters #### Request Body - **type** (Type) - Required - The C# Type object representing the class to be pre-compiled. ### Response #### Success Response (200) - **success** (bool) - Returns true if the class was successfully pre-compiled. ## POST RuntimeApi.PreJitMethod ### Description Pre-compiles a specific method to convert IL to register instructions ahead of time. ### Method POST ### Endpoint RuntimeApi.PreJitMethod(MethodInfo method) ### Parameters #### Request Body - **method** (MethodInfo) - Required - The MethodInfo object of the specific method to pre-compile. ### Response #### Success Response (200) - **success** (bool) - Returns true if the method was successfully pre-compiled. ``` -------------------------------- ### Runtime Configuration API Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Methods to adjust interpreter stack sizes, cache limits, and inlining behavior for performance tuning. ```APIDOC ## SET RuntimeApi.SetRuntimeOption ### Description Configures specific runtime parameters for the HybridCLR interpreter, such as stack sizes and cache limits. ### Method SET ### Endpoint RuntimeApi.SetRuntimeOption(RuntimeOptionId optionId, int value) ### Parameters #### Request Body - **optionId** (RuntimeOptionId) - Required - The identifier for the configuration option (e.g., InterpreterThreadObjectStackSize). - **value** (int) - Required - The integer value to assign to the configuration option. ### Request Example { "optionId": "InterpreterThreadObjectStackSize", "value": 262144 } ## GET RuntimeApi.GetRuntimeOption ### Description Retrieves the current value of a specific runtime configuration option. ### Method GET ### Endpoint RuntimeApi.GetRuntimeOption(RuntimeOptionId optionId) ### Parameters #### Query Parameters - **optionId** (RuntimeOptionId) - Required - The identifier for the configuration option to retrieve. ### Response #### Success Response (200) - **value** (int) - The current integer value of the requested configuration. ``` -------------------------------- ### Access Metadata from Loaded Interpreter Image in C++ Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Illustrates how to access various metadata elements from a loaded InterpreterImage. This includes retrieving type definitions, Il2CppTypes, method definitions, field definitions, generic containers, method bodies for interpretation, and class layout information. ```cpp #include "hybridclr/metadata/InterpreterImage.h" // Access type and method information from loaded image void AccessImageMetadata(hybridclr::metadata::InterpreterImage* image) { // Get type definition by index const Il2CppTypeDefinition* typeDef = image->GetTypeFromRawIndex(typeIndex); // Get Il2CppType from index const Il2CppType* type = image->GetIl2CppTypeFromRawIndex(typeIndex); // Get method definition const Il2CppMethodDefinition* methodDef = image->GetMethodDefinitionFromRawIndex(methodIndex); // Get field definition const Il2CppFieldDefinition* fieldDef = image->GetFieldDefinitionFromRawIndex(fieldIndex); // Get generic container Il2CppGenericContainer* container = image->GetGenericContainerByRawIndex(containerIndex); // Get method body for interpretation hybridclr::metadata::MethodBody* body = image->GetMethodBody(methodToken); // Get class layout information hybridclr::metadata::TbClassLayout layout = image->GetClassLayout(typeDef); int32_t packingSize = image->GetPackingSize(typeDef); } ``` -------------------------------- ### Retrieve Interpreted Method Information in C++ Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Demonstrates how to retrieve interpreter-specific method information, including transformed register instructions, using the InterpreterModule. This is useful for understanding the internal representation of interpreted methods. ```cpp #include "hybridclr/interpreter/InterpreterModule.h" // Get interpreted method info void GetInterpMethodInfo(const MethodInfo* method) { // Get the interpreter-specific method info // This contains the transformed register instructions hybridclr::interpreter::InterpMethodInfo* interpInfo = hybridclr::interpreter::InterpreterModule::GetInterpMethodInfo(method); } ``` -------------------------------- ### Metadata Module API Source: https://context7.com/focus-creative-games/hybridclr/llms.txt The MetadataModule provides access to loaded interpreter images and type metadata. It allows retrieving various metadata elements like images, strings, type handles, method definitions, and field offsets. ```APIDOC ## Metadata Module API ### Description The `MetadataModule` provides access to loaded interpreter images and type metadata. ### Method N/A (Class methods) ### Endpoint N/A (Class methods) ### Parameters N/A ### Request Example N/A ### Response N/A #### Accessing Interpreter Images ```cpp #include "hybridclr/metadata/MetadataModule.h" // Get image by index hybridclr::metadata::InterpreterImage* image = hybridclr::metadata::MetadataModule::GetImage(imageIndex); // Get image from Il2CppImage image = hybridclr::metadata::MetadataModule::GetImage(il2cppImage); // Get image from class image = hybridclr::metadata::MetadataModule::GetImage(klass); // Get image from method image = hybridclr::metadata::MetadataModule::GetImage(methodInfo); ``` #### Retrieving Metadata Elements ```cpp #include "hybridclr/metadata/MetadataModule.h" // Get string from encoded index const char* str = hybridclr::metadata::MetadataModule::GetStringFromEncodeIndex(index); // Get type handle from encoded index Il2CppMetadataTypeHandle handle = hybridclr::metadata::MetadataModule::GetAssemblyTypeHandleFromEncodeIndex(index); // Get method definition const Il2CppMethodDefinition* methodDef = hybridclr::metadata::MetadataModule::GetMethodDefinitionFromIndex(methodIndex); // Get method info from vtable slot const MethodInfo* method = hybridclr::metadata::MetadataModule::GetMethodInfoFromVTableSlot(klass, vtableSlot); // Get field offset uint32_t offset = hybridclr::metadata::MetadataModule::GetFieldOffset(klass, fieldIndexInType, fieldInfo); // Check if method is implemented by interpreter bool isInterpreted = hybridclr::metadata::MetadataModule::IsImplementedByInterpreter(method); ``` ``` -------------------------------- ### Manage Interpreter Machine State Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Demonstrates how to access the current thread's MachineState to query stack and frame information, allocate method arguments, and manage call frames during execution. ```cpp #include "hybridclr/interpreter/InterpreterModule.h" #include "hybridclr/interpreter/Engine.h" void AccessMachineState() { hybridclr::interpreter::MachineState& state = hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(); int32_t stackTop = state.GetStackTop(); hybridclr::interpreter::StackObject* stackBase = state.GetStackBasePtr(); uint32_t frameTop = state.GetFrameTopIdx(); hybridclr::interpreter::InterpFrame* topFrame = state.GetTopFrame(); hybridclr::interpreter::StackObject* args = state.AllocArgments(4); } void ManageInterpFrames() { hybridclr::interpreter::MachineState& state = hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(); hybridclr::interpreter::InterpFrame* frame = state.PushFrame(); state.PopFrame(); } ``` -------------------------------- ### Configure Runtime Performance Options Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Adjusts interpreter memory allocation, stack sizes, and method inlining parameters to optimize performance for hot update code. Supports both C# and C++ interfaces for fine-tuning the execution environment. ```csharp using HybridCLR; public class RuntimeConfiguration : MonoBehaviour { void ConfigureRuntime() { RuntimeApi.SetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize, 1024 * 256); RuntimeApi.SetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize, 1024 * 4); RuntimeApi.SetRuntimeOption(RuntimeOptionId.InterpreterThreadExceptionFlowSize, 1024); RuntimeApi.SetRuntimeOption(RuntimeOptionId.MaxMethodBodyCacheSize, 2048); RuntimeApi.SetRuntimeOption(RuntimeOptionId.MaxMethodInlineDepth, 5); RuntimeApi.SetRuntimeOption(RuntimeOptionId.MaxInlineableMethodBodySize, 64); int stackSize = RuntimeApi.GetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize); Debug.Log($"Current stack size: {stackSize}"); } } ``` ```cpp #include "hybridclr/RuntimeConfig.h" void ConfigureInterpreter() { hybridclr::RuntimeConfig::SetRuntimeOption( hybridclr::RuntimeOptionId::InterpreterThreadObjectStackSize, 1024 * 256 ); int32_t currentSize = hybridclr::RuntimeConfig::GetRuntimeOption( hybridclr::RuntimeOptionId::InterpreterThreadObjectStackSize ); } ``` -------------------------------- ### Load Hot Update Assemblies Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Loads .NET assemblies dynamically from raw byte data at runtime. Supports optional PDB symbol loading to enable stack traces for hot-updated code. ```cpp #include "hybridclr/metadata/Assembly.h" // Load a hot update assembly from bytes // assemblyData: raw DLL bytes // length: size of the DLL data // rawSymbolStoreBytes: optional PDB bytes for debugging (can be nullptr) // rawSymbolStoreLength: size of PDB data Il2CppAssembly* LoadHotUpdateAssembly(const void* assemblyData, uint64_t length) { // Load assembly without debug symbols Il2CppAssembly* assembly = hybridclr::metadata::Assembly::LoadFromBytes( assemblyData, length, nullptr, // No PDB 0 ); // The assembly is now registered and can be used // Types and methods from this assembly will be interpreted return assembly; } // Load assembly with debug symbols for stack traces Il2CppAssembly* LoadHotUpdateAssemblyWithPDB( const void* dllData, uint64_t dllLength, const void* pdbData, uint64_t pdbLength) { return hybridclr::metadata::Assembly::LoadFromBytes( dllData, dllLength, pdbData, pdbLength ); } ``` -------------------------------- ### Load Supplemental AOT Metadata Source: https://context7.com/focus-creative-games/hybridclr/llms.txt Loads supplemental metadata for AOT assemblies to support generic instantiation of hot-updated types. This is critical for resolving errors related to missing AOT generic method instantiations. ```csharp using HybridCLR; public class AOTMetadataLoader : MonoBehaviour { void Start() { // Load supplemental metadata for System.Core to support // generic types like List, Dictionary with hot update types byte[] aotDllBytes = LoadDllFromAssetBundle("System.Core.dll"); // HomologousImageMode.SuperSet: The loaded DLL is a superset of AOT DLL // HomologousImageMode.Consistent: The loaded DLL matches AOT DLL exactly LoadImageErrorCode result = RuntimeApi.LoadMetadataForAOTAssembly( aotDllBytes, HomologousImageMode.SuperSet ); if (result == LoadImageErrorCode.OK) { Debug.Log("AOT metadata loaded successfully"); } else { Debug.LogError($"Failed to load AOT metadata: {result}"); } } } ``` ```cpp #include "hybridclr/RuntimeApi.h" #include "hybridclr/metadata/AOTHomologousImage.h" // Load AOT metadata from C++ // mode: CONSISTENT (exact match) or SUPERSET (superset of AOT assembly) int32_t LoadAOTMetadata(const void* dllBytes, uint32_t dllSize, int32_t mode) { return (int32_t)hybridclr::metadata::Assembly::LoadMetadataForAOTAssembly( dllBytes, dllSize, (hybridclr::metadata::HomologousImageMode)mode ); } ``` -------------------------------- ### Interpreter Machine State API Source: https://context7.com/focus-creative-games/hybridclr/llms.txt The MachineState class manages per-thread interpreter state, including the evaluation stack, call frames, and exception handling. It provides methods to access and manipulate these states. ```APIDOC ## Interpreter Machine State API ### Description The `MachineState` class manages per-thread interpreter state including evaluation stack, call frames, and exception handling. ### Method N/A (Class methods) ### Endpoint N/A (Class methods) ### Parameters N/A ### Request Example N/A ### Response N/A #### Accessing Machine State ```cpp #include "hybridclr/interpreter/InterpreterModule.h" #include "hybridclr/interpreter/Engine.h" // Get the current thread's machine state hybridclr::interpreter::MachineState& state = hybridclr::interpreter::InterpreterModule::GetCurrentThreadMachineState(); // Query stack information int32_t stackTop = state.GetStackTop(); hybridclr::interpreter::StackObject* stackBase = state.GetStackBasePtr(); // Query frame information uint32_t frameTop = state.GetFrameTopIdx(); hybridclr::interpreter::InterpFrame* topFrame = state.GetTopFrame(); // Allocate stack slots for method arguments hybridclr::interpreter::StackObject* args = state.AllocArgments(4); ``` #### Frame Management ```cpp #include "hybridclr/interpreter/InterpreterModule.h" // Push a new frame for a method call hybridclr::interpreter::InterpFrame* frame = state.PushFrame(); // ... execute method ... // Pop frame when method returns state.PopFrame(); ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.