####################
C# Library Loader
####################

The C# API for HOOPS Exchange simplifies loading and initializing the required binaries, providing streamlined access to its functionality. 
This guide explains how to use the ``Library`` class to set up HOOPS Exchange for your applications.

.. tabs::

   .. tab:: C#

      .. code-block:: cs

         using TS3D.Exchange;
         
         static void Main(string[] args)
         {
             // Load and initialize the library
             Library.Initialize(HOOPS_LICENSE.KEY, PATH_TO_A3DLIBS_DIR);
         
             // HOOPS Exchange ready to use!
         
             // Terminate HOOPS Exchange
             Library.Free();
         }

   .. tab:: C

      .. code-block:: c

         #define INITIALIZE_A3D_API
         #include <A3DSDKIncludes.h>
         #include <hoops_license.h>
      
         int main(int argc, char* argv[])
         {
             // Load the library
             A3DSDKLoadLibraryA(PATH_TO_A3DLIBS_DIR);
      
             // Initialize HOOPS Exchange
             A3DLicPutUnifiedLicense(HOOPS_LICENSE);
             A3DDllInitialize(A3D_DLL_MAJORVERSION, A3D_DLL_MINORVERSION);
      
             // HOOPS Exchange ready to use!
      
             // Dispose HOOPS Exchange
             A3DDllTerminate();
      
             // Unload the library
             A3DSDKUnloadLibrary();
      
             return EXIT_SUCCESS;
         }

Initializing
============

The ``Initialize`` method of the ``Library`` class handles the entire process of loading and initializing the HOOPS Exchange binaries.
Here's its signature:

.. code-block:: cs

   static public void Initialize(string hoopsLicense, string binFolder);

The function performs the following:

1. **Load Binaries:**
   It dynamically loads the required binaries into memory. The path to these binaries is provided via the ``binFolder`` parameter.

2. **Bind Functions:**
   Once the binaries are loaded, all HOOPS Exchange API functions from the ``Direct.API`` namespace are mapped to C# delegates.

3. **License Verification and Initialization:**
   The ``Initialize`` method automatically invokes the following functions:
   - :cpp:func:`A3DLicPutUnifiedLicense` to verify the provided license.
   - :cpp:func:`A3DDllInitialize` to initialize the HOOPS Exchange library.

4. **Error Handling:**
   If any step fails, an ``InitializationException`` is thrown with a detailed error message.

Parameter Details
-----------------

.. rubric:: License Information

The ``hoopsLicense`` parameter expects a valid license string.
If you've followed the :doc:`environment setup tutorial </tutorials/cs/environment-setup>`, this license is available as the ``HOOPS_LICENSE.KEY`` constant in the *hoops_license.cs* file.

.. rubric:: Binary Folder

The ``binFolder`` parameter is a string specifying the directory containing the HOOPS Exchange binaries.
Ensure this folder contains the appropriate binary for your platform:

- Windows: ``A3DLIBS.dll``
- macOS: ``libA3DLIBS.dylib``
- Linux: ``libA3DLIBS.so``

Disposing
=========

To clean up and release resources, call the ``Library.Free()`` method when HOOPS Exchange is no longer needed.
This effectively calls :cpp:func:`A3DDllTerminate` before releasing HOOPS Exchange binary from memory.

Full Example Usage
------------------

.. code-block:: cs

   using TS3D.Exchange;
     
   static void Main(string[] args)
   {
       try
       {
           // Initialize the library
           Library.Initialize(HOOPS_LICENSE.KEY, PATH_TO_A3DLIBS_DIR);
     
           // Perform HOOPS Exchange operations here
       }
       catch (Library.InitializationException ex)
       {
           Console.WriteLine($"Initialization failed: {ex.Message}");
       }
       finally
       {
           // Ensure proper cleanup
           Library.Free();
       }
   }
