Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Console [Var].c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

#include "TgS Common - Console - Internal.h"
#include "TgS Common - Console.inl"


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Types */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

TgTYPE_UNION( STg2_CN_Var_Data, )
{
    struct
    {
        TgSCALAR_ALIAS_UNION m_sCurrent;
        TgSCALAR_ALIAS_UNION m_sMin;
        TgSCALAR_ALIAS_UNION m_sMax;
        TgSCALAR_ALIAS_UNION m_sDefault;
    } m_sScalar;

    struct
    {
        TgVEC_F32_04 m_vCurrent;
        TgVEC_F32_04 m_vMin;
        TgVEC_F32_04 m_vMax;
        TgVEC_F32_04 m_vDefault;
    } m_sVector;

    struct
    {
        P_TgCHAR m_pszCurrent;
        TgSIZE m_uiMaxLength;
        CP_TgCHAR m_pszDefault;
    } m_sString;
};
TgCOMPILER_ASSERT( sizeof( STg2_CN_Var_Data ) == 64, 0 );

TgTYPE_STRUCT(STg2_CN_Command_Variable,)
{
    STg2_CN_Command_Common                      m_sCommon;
    STg2_CN_Var_Data                            m_sData;
    TgCN_VAR_ID                                 m_tiVar;
    TgUINT64                                    m_uiPad;
};




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions and Data                                                                                                                                         */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

static P_STg2_CN_Command_Variable           tgCN_Init_Command_Variable( CPC_TgCHAR, CPC_TgCHAR, C_TgUINT16 );
static TgCN_VAR_ID                          tgCN_Insert_Command_Variable( P_STg2_CN_Command_Variable );

static ETgMODULE_STATE                      s_enCN_Command_Variable_State = ETgMODULE_STATE__FREED;

                                            /* Console Variable List */
static STg2_CN_Command_Variable             s_asCommand_Variable[KTgCN_MAX_COMMAND_VAR];
static STg2_UTM_AM_ST_ISO                   s_sCommand_Variable_Stack;
static TgSINT32                             s_niCommand_Variable_Used;




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCN_Var_Update ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCN_Var_Update( C_TgFLOAT32 UNUSED_PARAM fDT )
{
    return (KTgS_OK);
}


/* ---- tgCN_Var_Load_Config -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCN_Var_Load_Config( TgVOID )
{
    union
    {
        CP_TgUINT08                          pui;
        CP_TgCHAR                            psz;
    }                                   sString;
    TgSIZE                              uiPos;
    TgSIZE                              uiSize;
    STg2_Input_MEM                      sIN;

    if (TgFAILED(tgCM_Config_Query_String( &sString.psz, &uiSize, tgSZ_Hash( TgT("Common") ), tgSZ_Hash( TgT("Console Variables") ))))
    {
        return;
    };

    if (nullptr == sString.psz || 0 == uiSize)
    {
        return;
    };

    tgIO_IM_Open( &sIN, sString.pui, uiSize, nullptr );
    uiPos = 0;

    while (TgTRUE != sIN.m_sInput.m_pfnEnd_Of_File( &sIN.m_sInput, uiPos ))
    {
        TgCHAR                              szBuffer[1024];
        P_TgCHAR                            aszToken[8];
        TgSINT32                            niToken;
        TgCN_VAR_ID                         tiCN_Var;
        P_STg2_CN_Command_Variable          psCmd_Var;
        TgBOOL                              bRet;

        uiPos += tgIO_Read_Line( &sIN.m_sInput, uiPos, szBuffer, TgARRAY_COUNT( szBuffer ) - 1 );
        szBuffer[TgARRAY_COUNT( szBuffer ) - 1] = 0;
        niToken = tgSZ_Token_List( szBuffer, aszToken, TgARRAY_COUNT( aszToken ) );
        if (niToken <= 1)
        {
            continue;
        };

        tiCN_Var = tgCN_Var_Query_Id( aszToken[0] );
        if (TgTRUE != tgCN_VAR_ID_Is_Valid( tiCN_Var ))
        {
            TgWARNING_MSGF( 0, TgT("%-16.16s(%-32.32s): Console Var not found (%s).\n"), TgT("Console"), TgT("tgCN_Var_Load_Config_TXT"), aszToken[0] );
            continue;
        };

        psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
        bRet = TgFALSE;
        switch (psCmd_Var->m_sCommon.m_uiType)
        {
            case ETgCN_COMMAND__VAR_BOOL:
                bRet = tgCN_Var_Set_Bool( tiCN_Var, tgSZ_To_Bool( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_S08:
                bRet = tgCN_Var_Set_S08( tiCN_Var, tgSZ_To_S08( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_S16:
                bRet = tgCN_Var_Set_S16( tiCN_Var, tgSZ_To_S16( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_S32:
                bRet = tgCN_Var_Set_S32( tiCN_Var, tgSZ_To_S32( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_S64:
                bRet = tgCN_Var_Set_S64( tiCN_Var, tgSZ_To_S64( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_U08:
                bRet = tgCN_Var_Set_U08( tiCN_Var, tgSZ_To_U08( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_U16:
                bRet = tgCN_Var_Set_U16( tiCN_Var, tgSZ_To_U16( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_U32:
                bRet = tgCN_Var_Set_U32( tiCN_Var, tgSZ_To_U32( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_U64:
                bRet = tgCN_Var_Set_U64( tiCN_Var, tgSZ_To_U64( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_F32:
                bRet = tgCN_Var_Set_F32( tiCN_Var, tgSZ_To_F32( aszToken[1] ) );
                break;

            case ETgCN_COMMAND__VAR_F32_04:
            {
                TgVEC_F32_04                        vValue;

                if (niToken >= 5)
                {
                    vValue.m.x = tgSZ_To_F32( aszToken[1] );
                    vValue.m.y = tgSZ_To_F32( aszToken[2] );
                    vValue.m.z = tgSZ_To_F32( aszToken[3] );
                    vValue.m.w = tgSZ_To_F32( aszToken[4] );
                    bRet = tgCN_Var_Set_F32_04( tiCN_Var, vValue );
                };
                break;
            };

            case ETgCN_COMMAND__VAR_STRING:
                bRet = tgCN_Var_Set_String( tiCN_Var, aszToken[1] );
                break;

            default:
                TgS_NO_DEFAULT( break );
        };

        if (TgTRUE != bRet)
        {
            TgWARNING_MSGF( 0, TgT("%-16.16s(%-32.32s): Failed to set (%s).\n"), TgT("Console"), TgT("tgCN_Var_Load_Config_TXT"), aszToken[0] );
        };
    };

    tgIO_IM_Close( &sIN.m_sInput );
}


/* ---- tgCN_Var_Save_Config -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCN_Var_Save_Config( TgVOID )
{
    TgSIZE                              uiMem_Size;
    P_TgVOID                            pMem;
    STg2_Output_MEM                     sOUT;
    TgSINT32                            iIndex;
    P_STg2_CN_Command_Variable          psCmd_Var;

    uiMem_Size = 4096 * sizeof( TgCHAR );
    pMem = TgMALLOC_TEMP( uiMem_Size );
    if (nullptr == pMem)
    {
        return;
    };

    tgIO_OM_Open( &sOUT, pMem, uiMem_Size, nullptr );

    tgCM_UTM_AM_RW_Enter_Read_Wait_Yield( &g_sCN_Command_Name_Lock );

    for (iIndex = 0; iIndex < KTgCN_MAX_COMMAND_VAR; ++iIndex)
    {
        psCmd_Var = s_asCommand_Variable + iIndex;
        if (0 == (psCmd_Var->m_sCommon.m_uiFlags & KTgCN_CMD_FLAG_USED))
        {
            continue;
        };

        switch (psCmd_Var->m_sCommon.m_uiType)
        {
            case ETgCN_COMMAND__VAR_BOOL:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, (TgUINT32)psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_b );
                break;

            case ETgCN_COMMAND__VAR_S08:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i08 );
                break;

            case ETgCN_COMMAND__VAR_S16:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i16 );
                break;

            case ETgCN_COMMAND__VAR_S32:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i32 );
                break;

            case ETgCN_COMMAND__VAR_S64:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %I64d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i64 );
                break;

            case ETgCN_COMMAND__VAR_U08:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui08 );
                break;

            case ETgCN_COMMAND__VAR_U16:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui16 );
                break;

            case ETgCN_COMMAND__VAR_U32:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui32 );
                break;

            case ETgCN_COMMAND__VAR_U64:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %I64d\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui64 );
                break;

            case ETgCN_COMMAND__VAR_F32:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %f\n"), psCmd_Var->m_sCommon.m_pzName, (TgFLOAT64)psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_f32 );
                break;

            case ETgCN_COMMAND__VAR_F32_04:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %f %f %f %f\n"), psCmd_Var->m_sCommon.m_pzName, (TgFLOAT64)psCmd_Var->m_sData.m_sVector.m_vCurrent.m.x,
                    (TgFLOAT64)psCmd_Var->m_sData.m_sVector.m_vCurrent.m.y, (TgFLOAT64)psCmd_Var->m_sData.m_sVector.m_vCurrent.m.z,
                    (TgFLOAT64)psCmd_Var->m_sData.m_sVector.m_vCurrent.m.w );
                break;

            case ETgCN_COMMAND__VAR_STRING:
                tgIO_PrintF( &sOUT.m_sOutput, TgT("%s %s\n"), psCmd_Var->m_sCommon.m_pzName, psCmd_Var->m_sData.m_sString.m_pszCurrent );
                break;

            default:
                TgS_NO_DEFAULT( break );
        };
    };

    tgCM_UTM_AM_RW_Exit_Read( &g_sCN_Command_Name_Lock );

    tgCM_Config_Set_String( pMem, uiMem_Size, tgSZ_Hash( TgT("Common") ), tgSZ_Hash( TgT("Console Variables") ) );
    tgIO_OM_Close( &sOUT.m_sOutput );
    TgFREE_TEMP( pMem );
}


/* ---- tgCN_Var_Query_Init --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_Init( TgVOID )
{
    return (ETgMODULE_STATE__INITIALIZED <= s_enCN_Command_Variable_State && s_enCN_Command_Variable_State <= ETgMODULE_STATE__STOPPED ? TgTRUE : TgFALSE);
}


/* ---- tgCN_Var_Query_Boot --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_Boot( TgVOID )
{
    return (ETgMODULE_STATE__BOOTED == s_enCN_Command_Variable_State ? TgTRUE : TgFALSE);
}


/* ---- tgCN_Var_Query_Fixed_Memory ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgCN_Var_Query_Fixed_Memory( TgVOID )
{
    return ( 0
             + sizeof( s_enCN_Command_Variable_State )
             + sizeof( s_asCommand_Variable )
             + sizeof( s_sCommand_Variable_Stack )
             + sizeof( s_niCommand_Variable_Used )
    );
}


/* ---- tgCN_Print_Command_Variables ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCN_Print_Command_Variables( P_STg2_Output psOUT )
{
    TgSINT32                            iIndex;
    P_STg2_CN_Command_Common            psCmd;

    tgCM_UTM_AM_RW_Enter_Read_Wait_Yield( &g_sCN_Command_Name_Lock );

    for (iIndex = 0; iIndex < KTgCN_MAX_COMMAND_VAR; ++iIndex)
    {
        psCmd = &s_asCommand_Variable[iIndex].m_sCommon;
        if (0 == (psCmd->m_uiFlags & KTgCN_CMD_FLAG_USED))
        {
            continue;
        }

        tgIO_PrintF( psOUT, TgT("%-16.16s(%-32.32s): %s\n"), TgT("Con Var"), psCmd->m_pzName, psCmd->m_pzDesc );
    };

    tgCM_UTM_AM_RW_Exit_Read( &g_sCN_Command_Name_Lock );
}


/* ---- tgCN_Var_Init_Bool ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_Bool( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgBOOL bDefault )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_BOOL;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_b = bDefault;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_b = bDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_S08 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_S08( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgSINT08 iDefault, C_TgSINT08 iMin, C_TgSINT08 iMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_S08;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i08 = iDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_i08 = iMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_i08 = iMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_i08 = iDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_S16 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_S16( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgSINT16 iDefault, C_TgSINT16 iMin, C_TgSINT16 iMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_S16;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i16 = iDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_i16 = iMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_i16 = iMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_i16 = iDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_S32 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_S32( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgSINT32 iDefault, C_TgSINT32 iMin, C_TgSINT32 iMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_S32;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i32 = iDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_i32 = iMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_i32 = iMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_i32 = iDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_S64 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_S64( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgSINT64 iDefault, C_TgSINT64 iMin, C_TgSINT64 iMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_S64;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i64 = iDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_i64 = iMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_i64 = iMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_i64 = iDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_U08 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_U08( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgUINT08 uiDefault, C_TgUINT08 uiMin, C_TgUINT08 uiMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_U08;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui08 = uiDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui08 = uiMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui08 = uiMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_ui08 = uiDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_U16 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_U16( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgUINT16 uiDefault, C_TgUINT16 uiMin, C_TgUINT16 uiMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_U16;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui16 = uiDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui16 = uiMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui16 = uiMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_ui16 = uiDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_U32 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_U32( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgUINT32 uiDefault, C_TgUINT32 uiMin, C_TgUINT32 uiMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_U32;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui32 = uiDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui32 = uiMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui32 = uiMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_ui32 = uiDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_U64 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_U64( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgUINT64 uiDefault, C_TgUINT64 uiMin, C_TgUINT64 uiMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_U64;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui64 = uiDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui64 = uiMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui64 = uiMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_ui64 = uiDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_F32 ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_F32( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgFLOAT32 fDefault, C_TgFLOAT32 fMin, C_TgFLOAT32 fMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_F32;
    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_f32 = fDefault;
    psCmd_Var->m_sData.m_sScalar.m_sMin.m_f32 = fMin;
    psCmd_Var->m_sData.m_sScalar.m_sMax.m_f32 = fMax;
    psCmd_Var->m_sData.m_sScalar.m_sDefault.m_f32 = fDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_F32_04 -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_F32_04( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, C_TgVEC_F32_04 vDefault, C_TgVEC_F32_04 vMin, C_TgVEC_F32_04 vMax )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_F32_04;
    psCmd_Var->m_sData.m_sVector.m_vCurrent = vDefault;
    psCmd_Var->m_sData.m_sVector.m_vMin = vMin;
    psCmd_Var->m_sData.m_sVector.m_vMax = vMax;
    psCmd_Var->m_sData.m_sVector.m_vDefault = vDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Init_String -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Init_String( CPC_TgCHAR pszVar, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags, CPC_TgCHAR pszDefault, PC_TgCHAR pszBuffer, C_TgSIZE uiMaxDefault )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    psCmd_Var = tgCN_Init_Command_Variable( pszVar, pszDesc, uiFlags );
    if (nullptr == psCmd_Var)
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd_Var->m_sCommon.m_uiType = ETgCN_COMMAND__VAR_STRING;
    if (nullptr != pszDefault)
    {
        tgSZ_Copy( pszBuffer, uiMaxDefault, pszDefault );
    };
    psCmd_Var->m_sData.m_sString.m_pszCurrent = pszBuffer;
    psCmd_Var->m_sData.m_sString.m_uiMaxLength = uiMaxDefault;
    psCmd_Var->m_sData.m_sString.m_pszDefault = pszDefault;

    return (tgCN_Insert_Command_Variable( psCmd_Var ));
}


/* ---- tgCN_Var_Set_Bool ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_Bool( C_TgCN_VAR_ID tiCN_Var, C_TgBOOL bValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_BOOL != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_b = bValue;
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_S08 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_S08( C_TgCN_VAR_ID tiCN_Var, C_TgSINT08 iValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_S08 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i08 = tgCM_CLP_S08( iValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_i08, psCmd_Var->m_sData.m_sScalar.m_sMax.m_i08 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_S16 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_S16( C_TgCN_VAR_ID tiCN_Var, C_TgSINT16 iValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_S16 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i16 = tgCM_CLP_S16( iValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_i16, psCmd_Var->m_sData.m_sScalar.m_sMax.m_i16 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_S32 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_S32( C_TgCN_VAR_ID tiCN_Var, C_TgSINT32 iValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_S32 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i32 = tgCM_CLP_S32( iValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_i32, psCmd_Var->m_sData.m_sScalar.m_sMax.m_i32 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_S64 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_S64( C_TgCN_VAR_ID tiCN_Var, C_TgSINT64 iValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_S64 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i64 = tgCM_CLP_S64( iValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_i64, psCmd_Var->m_sData.m_sScalar.m_sMax.m_i64 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_U08 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_U08( C_TgCN_VAR_ID tiCN_Var, C_TgUINT08 uiValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_U08 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui08 = tgCM_CLP_U08( uiValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui08, psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui08 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_U16 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_U16( C_TgCN_VAR_ID tiCN_Var, C_TgUINT16 uiValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_U16 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui16 = tgCM_CLP_U16( uiValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui16, psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui16 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_U32 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_U32( C_TgCN_VAR_ID tiCN_Var, C_TgUINT32 uiValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_U32 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui32 = tgCM_CLP_U32( uiValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui32, psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui32 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_U64 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_U64( C_TgCN_VAR_ID tiCN_Var, C_TgUINT64 uiValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_U64 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui64 = tgCM_CLP_U64( uiValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_ui64, psCmd_Var->m_sData.m_sScalar.m_sMax.m_ui64 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_F32 ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_F32( C_TgCN_VAR_ID tiCN_Var, C_TgFLOAT32 fValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_F32 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_f32 = tgCM_CLP_F32( fValue, psCmd_Var->m_sData.m_sScalar.m_sMin.m_f32, psCmd_Var->m_sData.m_sScalar.m_sMax.m_f32 );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_F32_04 --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_F32_04( C_TgCN_VAR_ID tiCN_Var, C_TgVEC_F32_04 vValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_F32_04 != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    psCmd_Var->m_sData.m_sVector.m_vCurrent = F_CLP_F32_04( &vValue, &psCmd_Var->m_sData.m_sVector.m_vMin, &psCmd_Var->m_sData.m_sVector.m_vMax );
    return (TgTRUE);
}


/* ---- tgCN_Var_Set_String --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Set_String( C_TgCN_VAR_ID tiCN_Var, CPC_TgCHAR pszValue )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (ETgCN_COMMAND__VAR_STRING != psCmd_Var->m_sCommon.m_uiType)
    {
        return (TgFALSE);
    };

    tgSZ_Copy( psCmd_Var->m_sData.m_sString.m_pszCurrent, psCmd_Var->m_sData.m_sString.m_uiMaxLength, pszValue );
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_Bool --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_Bool( P_TgBOOL pbRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!pbRet || (ETgCN_COMMAND__VAR_BOOL != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *pbRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_b;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_S08 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_S08( P_TgSINT08 piRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!piRet || (ETgCN_COMMAND__VAR_S08 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *piRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i08;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_S16 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_S16( P_TgSINT16 piRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!piRet || (ETgCN_COMMAND__VAR_S16 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *piRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i16;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_S32 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_S32( P_TgSINT32 piRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!piRet || (ETgCN_COMMAND__VAR_S32 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *piRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i32;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_S64 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_S64( P_TgSINT64 piRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!piRet || (ETgCN_COMMAND__VAR_S64 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *piRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_i64;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_U08 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_U08( P_TgUINT08 puiRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!puiRet || (ETgCN_COMMAND__VAR_U08 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *puiRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui08;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_U16 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_U16( P_TgUINT16 puiRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!puiRet || (ETgCN_COMMAND__VAR_U16 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *puiRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui16;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_U32 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_U32( P_TgUINT32 puiRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!puiRet || (ETgCN_COMMAND__VAR_U32 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *puiRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui32;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_U64 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_U64( P_TgUINT64 puiRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!puiRet || (ETgCN_COMMAND__VAR_U64 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *puiRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_ui64;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_F32 ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_F32( P_TgFLOAT32 pfRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!pfRet || (ETgCN_COMMAND__VAR_F32 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *pfRet = psCmd_Var->m_sData.m_sScalar.m_sCurrent.m_f32;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_F32_04 ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_F32_04( P_TgVEC_F32_04 pvRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!pvRet || (ETgCN_COMMAND__VAR_F32_04 != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *pvRet = psCmd_Var->m_sData.m_sVector.m_vCurrent;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_String ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgCN_Var_Query_String( CPP_TgCHAR ppszRet, C_TgCN_VAR_ID tiCN_Var )
{
    P_STg2_CN_Command_Variable          psCmd_Var;

    TgPARAM_CHECK_INDEX( tiCN_Var.m.iI, s_asCommand_Variable );

    psCmd_Var = s_asCommand_Variable + tiCN_Var.m.iI;
    TgERROR(TgTRUE == tgEQ_CN_VAR_ID( psCmd_Var->m_tiVar, tiCN_Var ));

    if (!ppszRet || (ETgCN_COMMAND__VAR_STRING != psCmd_Var->m_sCommon.m_uiType))
    {
        return (TgFALSE);
    };

    *ppszRet = psCmd_Var->m_sData.m_sString.m_pszCurrent;
    return (TgTRUE);
}


/* ---- tgCN_Var_Query_Id ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Query_Id( CPC_TgCHAR pszName )
{
    TgUINTXX                            uiHash;
    P_STg2_CN_Command_Common            psCmd;

    uiHash = tgSZ_Hash( pszName );
    psCmd = tgCN_Find_Command_In_Hash( pszName, uiHash );
    return (tgCN_Var_Query_Id_Internal( psCmd ));
}




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Private Functions                                                                                                                                                     */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCN_Var_Init -------------------------------------------------------------------------------------------------------------------------------------------- */
/* NOTE: Called prior to MM and SM Init */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCN_Var_Init( TgVOID )
{
    /* Verify the state of the system */
    TgERROR(ETgMODULE_STATE__FREED == s_enCN_Command_Variable_State);
    s_enCN_Command_Variable_State = ETgMODULE_STATE__INITIALIZING;

    memset( s_asCommand_Variable, 0, sizeof( s_asCommand_Variable ) );
    s_niCommand_Variable_Used = 0;
    tgCM_UTM_AM_ST_Init_PreLoad( &s_sCommand_Variable_Stack.m_sStack, s_asCommand_Variable, sizeof( STg2_CN_Command_Variable ), KTgCN_MAX_COMMAND_VAR );

    s_enCN_Command_Variable_State = ETgMODULE_STATE__INITIALIZED;
    return (KTgS_OK);
}


/* ---- tgCN_Var_Boot -------------------------------------------------------------------------------------------------------------------------------------------- */
/* NOTE: Called prior to MM and SM Init */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCN_Var_Boot( TgVOID )
{
    /* Verify the state of the system */
    TgERROR(ETgMODULE_STATE__INITIALIZED == s_enCN_Command_Variable_State);
    s_enCN_Command_Variable_State = ETgMODULE_STATE__BOOTING;

    s_enCN_Command_Variable_State = ETgMODULE_STATE__BOOTED;
    return (KTgS_OK);
}


/* ---- tgCN_Var_Stop -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCN_Var_Stop( TgVOID )
{
    if ((ETgMODULE_STATE__FREED == s_enCN_Command_Variable_State) || (ETgMODULE_STATE__INITIALIZED == s_enCN_Command_Variable_State))
    {
        return (KTgS_OK);
    };

    /* Verify the state of the system */
    TgERROR(ETgMODULE_STATE__BOOTED == s_enCN_Command_Variable_State);
    s_enCN_Command_Variable_State = ETgMODULE_STATE__STOPPING;

    s_enCN_Command_Variable_State = ETgMODULE_STATE__STOPPED;
    return (KTgS_OK);
}


/* ---- tgCN_Var_Free -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCN_Var_Free( TgVOID )
{
    if (ETgMODULE_STATE__FREED == s_enCN_Command_Variable_State)
    {
        return (KTgS_OK);
    };

    /* Verify the state of the system */
    TgERROR((ETgMODULE_STATE__STOPPED == s_enCN_Command_Variable_State) || (ETgMODULE_STATE__INITIALIZED == s_enCN_Command_Variable_State));
    s_enCN_Command_Variable_State = ETgMODULE_STATE__FREEING;

    tgCM_UTM_AM_ST_Free( &s_sCommand_Variable_Stack.m_sStack );

    s_enCN_Command_Variable_State = ETgMODULE_STATE__FREED;
    return (KTgS_OK);
}


/* ---- tgCN_Var_Query_Id_Internal -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgCN_VAR_ID tgCN_Var_Query_Id_Internal( CPC_STg2_CN_Command_Common psCmdP )
{
    union
    {
        CP_STg2_CN_Command_Common           psCommon;
        CP_STg2_CN_Command_Variable         psVar;
    }                                   psCmd;

    if (nullptr == psCmdP || (TgTRUE != tgCN_Is_Command_Variable( psCmdP )))
    {
        return (KTgCN_VAR_ID__INVALID);
    };

    psCmd.psCommon = psCmdP;
    return (psCmd.psVar->m_tiVar);
}




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions                                                                                                                                                  */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCN_Init_Command_Variable -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static P_STg2_CN_Command_Variable tgCN_Init_Command_Variable( CPC_TgCHAR pzName, CPC_TgCHAR pszDesc, C_TgUINT16 uiFlags )
{
    union
    {
        P_STg2_CN_Command_Variable          psVar;
        P_STg2_UTM_Node                     psNode;
        
    }                                   psCmd;
    TgSTRING_DICT_ID                    tiString;

    TgPARAM_CHECK(nullptr != pzName && pzName[0]);

    psCmd.psNode = tgCM_UTM_AM_ST_Pop( &s_sCommand_Variable_Stack.m_sStack );
    if (nullptr == psCmd.psVar)
    {
        TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Exceeded number of console command functions.\n"), TgT("Console"), TgT("tgCN_Init_Command_Variable") );
        return (nullptr);
    };

    memset( psCmd.psVar, 0, sizeof( STg2_CN_Command_Variable ) );

    psCmd.psVar->m_sCommon.m_uiHash = tgSZ_Hash( pzName );
    tiString = tgSM_Dict_Insert_String_Hash( pzName, tgSZ_Length( pzName ), psCmd.psVar->m_sCommon.m_uiHash );
    if (TgTRUE != tgSTRING_DICT_ID_Is_Valid( tiString ))
    {
        tgCM_UTM_AM_ST_Push( &s_sCommand_Variable_Stack.m_sStack, psCmd.psNode );
        return (nullptr);
    };
    tgSM_Dict_Query_String( &psCmd.psVar->m_sCommon.m_pzName, nullptr, tiString );
    if (nullptr != pszDesc)
    {
        tiString = tgSM_Dict_Insert_String_Hash( pszDesc, tgSZ_Length( pszDesc ), tgSZ_Hash( pszDesc ) );
        if (TgTRUE == tgSTRING_DICT_ID_Is_Valid( tiString ))
        {
            tgSM_Dict_Query_String( &psCmd.psVar->m_sCommon.m_pzDesc, nullptr, tiString );
        };
    }
    else
    {
        psCmd.psVar->m_sCommon.m_pzDesc = nullptr;
    };

    tgInit_CN_VAR_ID( &psCmd.psVar->m_tiVar, (TgSINT32)(psCmd.psVar - s_asCommand_Variable) );
    psCmd.psVar->m_sCommon.m_uiFlags = uiFlags | KTgCN_CMD_FLAG_USED;

    return (psCmd.psVar);
}


/* ---- tgCN_Insert_Command_Variable ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
static TgCN_VAR_ID tgCN_Insert_Command_Variable( P_STg2_CN_Command_Variable psCmd_Var )
{
    union
    {
        P_STg2_CN_Command_Common            psRet;
        P_STg2_CN_Command_Variable          psVar;
    }                                   psCmd;
    TgRESULT                            iRet;
    TgCN_VAR_ID                         tiVar;

    tgCM_UTM_AM_RW_Enter_Write_Wait_Yield( &g_sCN_Command_Name_Lock );

    iRet = tgCN_Insert_Command( &psCmd.psRet, &psCmd_Var->m_sCommon );
    if (TgSUCCEEDED( iRet ))
    {
        tiVar = psCmd_Var->m_tiVar;
        tgCM_UTM_AM_RW_Exit_Write( &g_sCN_Command_Name_Lock );
        return (tiVar);
    }
    else if (KTgW_DUPLICATE == iRet)
    {
        tiVar = psCmd.psVar->m_tiVar;
        tgCM_UTM_AM_RW_Exit_Write( &g_sCN_Command_Name_Lock );
        TgWARNING_MSGF( 0, TgT("%-16.16s(%-32.32s): Duplicate console variable (first values retained).\n"), TgT("Console"), TgT("tgCN_Insert_Command_Variable") );

        memset( psCmd_Var, 0, sizeof( STg2_CN_Command_Variable ) );
        tgCM_UTM_AM_ST_Push( &s_sCommand_Variable_Stack.m_sStack, (P_STg2_UTM_Node)(psCmd_Var) );
        return (tiVar);
    };

    tgCM_UTM_AM_RW_Exit_Write( &g_sCN_Command_Name_Lock );

    TgWARNING_MSGF( 0, TgT("%-16.16s(%-32.32s): Console command name already used.\n"), TgT("Console"), TgT("tgCN_Insert_Command_Variable") );
    memset( psCmd_Var, 0, sizeof( STg2_CN_Command_Variable ) );
    tgCM_UTM_AM_ST_Push( &s_sCommand_Variable_Stack.m_sStack, (P_STg2_UTM_Node)(psCmd_Var) );

    return (KTgCN_VAR_ID__INVALID);
}