Imports KeraLua

Public Module Lua_Aux

   Private m_State As Lua
   Friend Sub SetState(state As Lua)
      m_State = state
   End Sub

   Friend Function LuaClearStack(state As Lua)
      Dim nIndex As Integer = state.GetTop()
      If nIndex > 0 Then
         state.Pop(nIndex)
      End If
      Return True
   End Function

   Friend Function LuaCheckParam(state As Lua, nIndex As Integer, ByRef bValue As Boolean)
      If Not LuaGetParam(state, nIndex, bValue) Then
         Return state.Error(" Invalid Parameter # " & nIndex)
      End If
      Return True
   End Function

   Friend Function LuaCheckParam(state As Lua, nIndex As Integer, ByRef nValue As Integer)
      If Not LuaGetParam(state, nIndex, nValue) Then
         Return state.Error(" Invalid Parameter # " & nIndex)
      End If
      Return True
   End Function

   Friend Function LuaCheckParam(state As Lua, nIndex As Integer, ByRef lValue As Long)
      If Not LuaGetParam(state, nIndex, lValue) Then
         Return state.Error(" Invalid Parameter # " & nIndex)
      End If
      Return True
   End Function

   Friend Function LuaCheckParam(state As Lua, nIndex As Integer, ByRef dValue As Double)
      If Not LuaGetParam(state, nIndex, dValue) Then
         Return state.Error(" Invalid Parameter # " & nIndex)
      End If
      Return True
   End Function

   Friend Function LuaCheckParam(state As Lua, nIndex As Integer, ByRef sValue As String)
      If Not LuaGetParam(state, nIndex, sValue) Then
         Return state.Error(" Invalid Parameter # " & nIndex)
      End If
      Return True
   End Function

   Friend Function LuaGetParam(state As Lua, nIndex As Integer, ByRef bValue As Boolean) As Boolean
      If Not state.IsBoolean(nIndex) Then Return False
      bValue = state.ToBoolean(nIndex)
      Return True
   End Function

   Friend Function LuaGetParam(state As Lua, nIndex As Integer, ByRef nValue As Integer) As Boolean
      If Not state.IsNumber(nIndex) Then Return False
      nValue = Math.Round(state.ToNumber(nIndex))
      Return True
   End Function

   Friend Function LuaGetParam(state As Lua, nIndex As Integer, ByRef lValue As Long) As Boolean
      If Not state.IsNumber(nIndex) Then Return False
      lValue = state.ToInteger(nIndex)
      Return True
   End Function

   Friend Function LuaGetParam(state As Lua, nIndex As Integer, ByRef dValue As Double) As Boolean
      If Not state.IsNumber(nIndex) Then Return False
      dValue = state.ToNumber(nIndex)
      Return True
   End Function

   Friend Function LuaGetParam(state As Lua, nIndex As Integer, ByRef sValue As String) As Boolean
      If Not state.IsString(nIndex) Then Return False
      sValue = state.ToString(nIndex)
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua) As Boolean
      Try
         state.PushNil()
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua, ByRef bValue As Boolean) As Boolean
      Try
         state.PushBoolean(bValue)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua, ByRef nValue As Integer) As Boolean
      Try
         state.PushInteger(nValue)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua, ByRef lValue As Long) As Boolean
      Try
         state.PushInteger(lValue)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua, ByRef dValue As Double) As Boolean
      Try
         state.PushNumber(dValue)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaSetParam(state As Lua, ByRef sValue As String) As Boolean
      Try
         state.PushString(sValue)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

   Friend Function LuaGetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef bValue As Boolean) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      state.GetField(nIndex, sVarName)
      Dim bResult As Boolean = LuaGetParam(state, -1, bValue)
      state.Pop(1)
      Return bResult
   End Function

   Friend Function LuaGetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef nValue As Integer) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      state.GetField(nIndex, sVarName)
      Dim bResult As Boolean = LuaGetParam(state, -1, nValue)
      state.Pop(1)
      Return bResult
   End Function

   Friend Function LuaGetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef lValue As Long) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      state.GetField(nIndex, sVarName)
      Dim bResult As Boolean = LuaGetParam(state, -1, lValue)
      state.Pop(1)
      Return bResult
   End Function

   Friend Function LuaGetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef dValue As Double) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      state.GetField(nIndex, sVarName)
      Dim bResult As Boolean = LuaGetParam(state, -1, dValue)
      state.Pop(1)
      Return bResult
   End Function

   Friend Function LuaGetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef sValue As String) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      state.GetField(nIndex, sVarName)
      Dim bResult As Boolean = LuaGetParam(state, -1, sValue)
      state.Pop(1)
      Return bResult
   End Function

   Friend Function LuaGetGlobVar(state As Lua, ByRef sName As String, ByRef bValue As Boolean) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         state.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(state, -1, bValue)
         state.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         state.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               state.RawGetInteger(-1, nTableIndex)
            Else
               state.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(state, -1, sVarName, bValue)
         state.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function
   Friend Function LuaGetGlobVar(ByRef sName As String, ByRef bValue As Boolean) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         m_State.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(m_State, -1, bValue)
         m_State.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         m_State.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               m_State.RawGetInteger(-1, nTableIndex)
            Else
               m_State.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(m_State, -1, sVarName, bValue)
         m_State.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function

   Friend Function LuaGetGlobVar(state As Lua, ByRef sName As String, ByRef nValue As Integer) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         state.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(state, -1, nValue)
         state.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         state.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               state.RawGetInteger(-1, nTableIndex)
            Else
               state.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(state, -1, sVarName, nValue)
         state.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function
   Friend Function LuaGetGlobVar(ByRef sName As String, ByRef nValue As Integer) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         m_State.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(m_State, -1, nValue)
         m_State.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         m_State.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               m_State.RawGetInteger(-1, nTableIndex)
            Else
               m_State.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(m_State, -1, sVarName, nValue)
         m_State.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function

   Friend Function LuaGetGlobVar(state As Lua, ByRef sName As String, ByRef lValue As Long) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         state.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(state, -1, lValue)
         state.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         state.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               state.RawGetInteger(-1, nTableIndex)
            Else
               state.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(state, -1, sVarName, lValue)
         state.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function
   Friend Function LuaGetGlobVar(ByRef sName As String, ByRef lValue As Long) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         m_State.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(m_State, -1, lValue)
         m_State.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         m_State.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               m_State.RawGetInteger(-1, nTableIndex)
            Else
               m_State.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(m_State, -1, sVarName, lValue)
         m_State.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function

   Friend Function LuaGetGlobVar(state As Lua, ByRef sName As String, ByRef dValue As Double) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         state.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(state, -1, dValue)
         state.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         state.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               state.RawGetInteger(-1, nTableIndex)
            Else
               state.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(state, -1, sVarName, dValue)
         state.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function
   Friend Function LuaGetGlobVar(ByRef sName As String, ByRef dValue As Double) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         m_State.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(m_State, -1, dValue)
         m_State.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         m_State.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               m_State.RawGetInteger(-1, nTableIndex)
            Else
               m_State.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(m_State, -1, sVarName, dValue)
         m_State.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function

   Friend Function LuaGetGlobVar(state As Lua, ByRef sName As String, ByRef sValue As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         state.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(state, -1, sValue)
         state.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         state.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               state.RawGetInteger(-1, nTableIndex)
            Else
               state.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(state, -1, sVarName, sValue)
         state.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function
   Friend Function LuaGetGlobVar(ByRef sName As String, ByRef sValue As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         m_State.GetGlobal(sName)
         Dim bResult As Boolean = LuaGetParam(m_State, -1, sValue)
         m_State.Pop(1)
         Return bResult
      Else
         Dim sTableName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         m_State.GetGlobal(sTableName)
         For nParamIndex As Integer = 1 To sParams.Count - 2
            Dim nTableIndex As Integer = -1
            If Integer.TryParse(sParams(nParamIndex), nTableIndex) Then
               m_State.RawGetInteger(-1, nTableIndex)
            Else
               m_State.GetField(-1, sParams(nParamIndex))
            End If
         Next
         Dim sVarName As String = sParams(sParams.Count - 1)
         Dim bResult As Boolean = LuaGetTabFieldParam(m_State, -1, sVarName, sValue)
         m_State.Pop(sParams.Count - 1)
         Return bResult
      End If
   End Function

   Friend Function LuaSetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      If Not LuaSetParam(state) Then Return False
      Dim nPos As Integer = (If(nIndex > 0, nIndex, nIndex - 1))
      state.SetField(nPos, sVarName)
      Return True
   End Function

   Friend Function LuaSetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef bValue As Boolean) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      If Not LuaSetParam(state, bValue) Then Return False
      Dim nPos As Integer = (If(nIndex > 0, nIndex, nIndex - 1))
      state.SetField(nPos, sVarName)
      Return True
   End Function

   Friend Function LuaSetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef nValue As Integer) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      If Not LuaSetParam(state, nValue) Then Return False
      Dim nPos As Integer = (If(nIndex > 0, nIndex, nIndex - 1))
      state.SetField(nPos, sVarName)
      Return True
   End Function

   Friend Function LuaSetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef dValue As Double) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      If Not LuaSetParam(state, dValue) Then Return False
      Dim nPos As Integer = (If(nIndex > 0, nIndex, nIndex - 1))
      state.SetField(nPos, sVarName)
      Return True
   End Function

   Friend Function LuaSetTabFieldParam(state As Lua, nIndex As Integer, sVarName As String, ByRef sValue As String) As Boolean
      If Not state.IsTable(nIndex) Then Return False
      If Not LuaSetParam(state, sValue) Then Return False
      Dim nPos As Integer = (If(nIndex > 0, nIndex, nIndex - 1))
      state.SetField(nPos, sVarName)
      Return True
   End Function

   Friend Function LuaSetGlobVar(state As Lua, ByRef sName As String, ByRef bValue As Boolean) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(state, bValue) Then Return False
         state.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         state.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(state, -1, sVarName, bValue)
         state.Pop(1)
         Return bResult
      End If
   End Function
   Friend Function LuaSetGlobVar(ByRef sName As String, ByRef bValue As Boolean) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(m_State, bValue) Then Return False
         m_State.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         m_State.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(m_State, -1, sVarName, bValue)
         m_State.Pop(1)
         Return bResult
      End If
   End Function

   Friend Function LuaSetGlobVar(state As Lua, ByRef sName As String, ByRef nValue As Integer) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(state, nValue) Then Return False
         state.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         state.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(state, -1, sVarName, nValue)
         state.Pop(1)
         Return bResult
      End If
   End Function
   Friend Function LuaSetGlobVar(ByRef sName As String, ByRef nValue As Integer) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(m_State, nValue) Then Return False
         m_State.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         m_State.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(m_State, -1, sVarName, nValue)
         m_State.Pop(1)
         Return bResult
      End If
   End Function

   Friend Function LuaSetGlobVar(state As Lua, ByRef sName As String, ByRef dValue As Double) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(state, dValue) Then Return False
         state.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         state.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(state, -1, sVarName, dValue)
         state.Pop(1)
         Return bResult
      End If
   End Function
   Friend Function LuaSetGlobVar(ByRef sName As String, ByRef dValue As Double) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(m_State, dValue) Then Return False
         m_State.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         m_State.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(m_State, -1, sVarName, dValue)
         m_State.Pop(1)
         Return bResult
      End If
   End Function

   Friend Function LuaSetGlobVar(state As Lua, ByRef sName As String, ByRef sValue As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(state, sValue) Then Return False
         state.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         state.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(state, -1, sVarName, sValue)
         state.Pop(1)
         Return bResult
      End If
   End Function
   Friend Function LuaSetGlobVar(ByRef sName As String, ByRef sValue As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(m_State, sValue) Then Return False
         m_State.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         m_State.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(m_State, -1, sVarName, sValue)
         m_State.Pop(1)
         Return bResult
      End If
   End Function

   Friend Function LuaResetGlobVar(state As Lua, ByRef sName As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(state) Then Return False
         state.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         state.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(state, -1, sVarName)
         state.Pop(1)
         Return bResult
      End If
   End Function
   Friend Function LuaResetGlobVar(ByRef sName As String) As Boolean
      ' se variabile standard
      If Not sName.Contains("."c) Then
         If Not LuaSetParam(m_State) Then Return False
         m_State.SetGlobal(sName)
         Return True
         ' altrimenti campo di tabella
      Else
         Dim sTableName As String = "", sVarName As String = ""
         Dim sParams() As String = sName.Split("."c)
         sTableName = sParams(0)
         sVarName = sParams(1)
         m_State.GetGlobal(sTableName)
         Dim bResult As Boolean = LuaSetTabFieldParam(m_State, -1, sVarName)
         m_State.Pop(1)
         Return bResult
      End If
   End Function

   Friend Function LuaCreateGlobTable(state As Lua, ByRef sName As String) As Boolean
      Try
         state.NewTable()
         state.SetGlobal(sName)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function
   Friend Function LuaCreateGlobTable(ByRef sName As String) As Boolean
      Try
         m_State.NewTable()
         m_State.SetGlobal(sName)
      Catch ex As Exception
         Return False
      End Try
      Return True
   End Function

End Module
