0b655aeb09
- aggiunto Dictionary plugin
302 lines
11 KiB
VB.net
302 lines
11 KiB
VB.net
Imports KeraLua
|
|
Imports StackExchange.Redis
|
|
Imports System.IO
|
|
|
|
Public Module Lua_General
|
|
|
|
' Lock per scrittura file di log
|
|
Private m_Lock_OutLog As New Object
|
|
|
|
Private m_sLogPath As String
|
|
Friend Sub SetLogPath(sPath As String)
|
|
m_sLogPath = sPath
|
|
End Sub
|
|
|
|
' da eliminare quando arriva funzione di libreria!!
|
|
Public Function EgtOutLog(sLogMsg As String) As Boolean
|
|
SyncLock (m_Lock_OutLog)
|
|
Using LogStreamWriter As StreamWriter = File.AppendText(m_sLogPath)
|
|
LogStreamWriter.WriteLine(DateTime.Now.ToLongTimeString() & " " & sLogMsg)
|
|
End Using
|
|
End SyncLock
|
|
Return True
|
|
End Function
|
|
|
|
Friend func_EntIs64bit As LuaFunction = AddressOf Lua_EntIs64bit
|
|
Friend func_EntOutLog As LuaFunction = AddressOf Lua_EntOutLog
|
|
Friend func_EntExistsDirectory As LuaFunction = AddressOf Lua_EntExistsDirectory
|
|
Friend func_EntCopyFile As LuaFunction = AddressOf Lua_EntCopyFile
|
|
Friend func_EntGetIniFile As LuaFunction = AddressOf Lua_EntGetIniFile
|
|
Friend func_EntGetNumberFromIni As LuaFunction = AddressOf Lua_EntGetNumberFromIni
|
|
Friend func_EntGetStringFromIni As LuaFunction = AddressOf Lua_EntGetStringFromIni
|
|
Friend func_EntWriteStringToIni As LuaFunction = AddressOf Lua_EntWriteStringToIni
|
|
Friend func_RdsStringGet As LuaFunction = AddressOf Lua_RdsStringGet
|
|
Friend func_RdsStringSetAsync As LuaFunction = AddressOf Lua_RdsStringSetAsync
|
|
Friend func_RdsPublishAsync As LuaFunction = AddressOf Lua_RdsPublishAsync
|
|
Friend func_ReadVariable As LuaFunction = AddressOf Lua_ReadVariable
|
|
Friend func_WriteVariable As LuaFunction = AddressOf Lua_WriteVariable
|
|
|
|
Private Function Lua_EntIs64bit(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
state.PushBoolean(Not IntPtr.Size = 4)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntOutLog(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 1 o 2 parametri : stringa da emettere [, DebugLevel = 0]
|
|
Dim sLogMsg As String = ""
|
|
Dim nDebugLevel As Integer = 0
|
|
LuaCheckParam(state, 1, sLogMsg)
|
|
LuaGetParam(state, 2, nDebugLevel)
|
|
LuaClearStack(state)
|
|
' accodo il messaggio nel file di log
|
|
EgtOutLog(sLogMsg)
|
|
' non c'è risultato
|
|
Return 0
|
|
End Function
|
|
|
|
Private Function Lua_EntExistsDirectory(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 1 parametro : sDir
|
|
Dim sDirectory As String = ""
|
|
LuaCheckParam(state, 1, sDirectory)
|
|
LuaClearStack(state)
|
|
Dim bResult As Boolean
|
|
Try
|
|
' verifico esistenza direttorio
|
|
bResult = Directory.Exists(sDirectory)
|
|
Catch ex As Exception
|
|
bResult = False
|
|
EgtOutLog("Lua_EntExistsDirectory on " & sDirectory & " failed! " & ex.Message)
|
|
End Try
|
|
' restituisco il risultato
|
|
LuaSetParam(state, bResult)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntCopyFile(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 2 parametri : sFile, sNewFile
|
|
Dim sSourcePath As String = ""
|
|
Dim sDestinationPath As String = ""
|
|
LuaCheckParam(state, 1, sSourcePath)
|
|
LuaCheckParam(state, 2, sDestinationPath)
|
|
LuaClearStack(state)
|
|
Dim bResult As Boolean
|
|
Try
|
|
' copio il file
|
|
File.Copy(sSourcePath, sDestinationPath, True)
|
|
bResult = True
|
|
Catch ex As Exception
|
|
bResult = False
|
|
EgtOutLog("Lua_EntCopyFile from " & sSourcePath & " to " & sDestinationPath & " failed! " & ex.Message)
|
|
End Try
|
|
' restituisco il risultato
|
|
LuaSetParam(state, bResult)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntGetIniFile(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' restituisco il risultato
|
|
LuaSetParam(state, IniFile.sPath)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntGetNumberFromIni(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 4 parametri: sSec, sKey, dDef, sIniFile
|
|
Dim sSec As String = ""
|
|
Dim sKey As String = ""
|
|
Dim dDef As Integer = 0
|
|
Dim sIniFile As String = ""
|
|
LuaCheckParam(state, 1, sSec)
|
|
LuaCheckParam(state, 2, sKey)
|
|
LuaCheckParam(state, 3, dDef)
|
|
LuaCheckParam(state, 4, sIniFile)
|
|
LuaClearStack(state)
|
|
Dim nResult As Integer = GetPrivateProfileInt(sSec, sKey, dDef, sIniFile)
|
|
' restituisco il risultato
|
|
LuaSetParam(state, nResult)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntGetStringFromIni(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 4 parametri: sSec, sKey, sDef, sIniFile
|
|
Dim sSec As String = ""
|
|
Dim sKey As String = ""
|
|
Dim sDef As String = 0
|
|
Dim sIniFile As String = ""
|
|
LuaCheckParam(state, 1, sSec)
|
|
LuaCheckParam(state, 2, sKey)
|
|
LuaCheckParam(state, 3, sDef)
|
|
LuaCheckParam(state, 4, sIniFile)
|
|
LuaClearStack(state)
|
|
Dim sResult As String = ""
|
|
GetPrivateProfileString(sSec, sKey, sDef, sResult, sIniFile)
|
|
' restituisco il risultato
|
|
LuaSetParam(state, sResult)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_EntWriteStringToIni(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 4 parametri: sSec, sKey, sVal, sIniFile
|
|
Dim sSec As String = ""
|
|
Dim sKey As String = ""
|
|
Dim sVal As Integer = 0
|
|
Dim sIniFile As String = ""
|
|
LuaCheckParam(state, 1, sSec)
|
|
LuaCheckParam(state, 2, sKey)
|
|
LuaCheckParam(state, 3, sVal)
|
|
LuaCheckParam(state, 4, sIniFile)
|
|
LuaClearStack(state)
|
|
Dim bResult As Boolean = WritePrivateProfileString(sSec, sKey, sVal, sIniFile)
|
|
' restituisco il risultato
|
|
LuaSetParam(state, bResult)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_RdsStringGet(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
If IsNothing(Map.refMainWindowVM.RedisManager) OrElse IsNothing(Map.refMainWindowVM.RedisManager.Subscriber) Then
|
|
EgtOutLog("Error! Trying to get string from a non existent redis!")
|
|
LuaSetParam(state)
|
|
Return 1
|
|
End If
|
|
' 1 parametro: sKey
|
|
Dim sKey As String = ""
|
|
LuaCheckParam(state, 1, sKey)
|
|
LuaClearStack(state)
|
|
Dim Result As RedisValue
|
|
Result = Map.refMainWindowVM.RedisManager.RedisDb.StringGet(sKey)
|
|
' restituisco il risultato
|
|
LuaSetParam(state, Result)
|
|
Return 1
|
|
End Function
|
|
|
|
Private Function Lua_RdsStringSetAsync(ByVal p As IntPtr) As Integer
|
|
If IsNothing(Map.refMainWindowVM.RedisManager) OrElse IsNothing(Map.refMainWindowVM.RedisManager.Subscriber) Then
|
|
EgtOutLog("Error! Trying to set string on a non existent redis!")
|
|
Return 0
|
|
End If
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 2 parametri: sKey, sVal
|
|
Dim sKey As String = ""
|
|
Dim sVal As String = 0
|
|
LuaCheckParam(state, 1, sKey)
|
|
LuaCheckParam(state, 2, sVal)
|
|
LuaClearStack(state)
|
|
Map.refMainWindowVM.RedisManager.RedisDb.StringSetAsync(sKey, sVal)
|
|
Return 0
|
|
End Function
|
|
|
|
Private Function Lua_RdsPublishAsync(ByVal p As IntPtr) As Integer
|
|
If IsNothing(Map.refMainWindowVM.RedisManager) OrElse IsNothing(Map.refMainWindowVM.RedisManager.Subscriber) Then
|
|
EgtOutLog("Error! Trying to publish on a non existent redis!")
|
|
Return 0
|
|
End If
|
|
Dim state = Lua.FromIntPtr(p)
|
|
' 2 parametri: sKey, sVal
|
|
Dim sChannel As String = ""
|
|
Dim sMessage As String = 0
|
|
LuaCheckParam(state, 1, sChannel)
|
|
LuaCheckParam(state, 2, sMessage)
|
|
LuaClearStack(state)
|
|
Map.refMainWindowVM.RedisManager.Subscriber.PublishAsync(sChannel, sMessage)
|
|
Return 0
|
|
End Function
|
|
|
|
Private Function Lua_ReadVariable(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
If state.Type(1) <> LuaType.String Then Return 0
|
|
Dim sVarAddr As String = state.ToString(1, False)
|
|
'state.Pop(1)
|
|
Dim dResult As Double = 0
|
|
If ReadVariable(sVarAddr, dResult) Then
|
|
state.PushNumber(dResult)
|
|
Return 1
|
|
Else
|
|
Return 0
|
|
End If
|
|
End Function
|
|
|
|
Private Function ReadVariable(sVarAddr As String, ByRef dResult As Double) As Boolean
|
|
Select Case sVarAddr
|
|
Case "a"
|
|
dResult = 256
|
|
Case "b"
|
|
dResult = 257
|
|
Case "c"
|
|
dResult = 258
|
|
Case "d"
|
|
dResult = 259
|
|
Case "e"
|
|
dResult = 260
|
|
Case "f"
|
|
dResult = 261
|
|
Case Else
|
|
Return False
|
|
End Select
|
|
Return True
|
|
End Function
|
|
|
|
Private Function Lua_WriteVariable(ByVal p As IntPtr) As Integer
|
|
Dim state = Lua.FromIntPtr(p)
|
|
If state.Type(1) <> LuaType.String OrElse state.Type(2) <> LuaType.String Then Return 0
|
|
Dim sVarAddr As String = state.ToString(1, False)
|
|
Dim sValue As String = state.ToString(2, False)
|
|
'state.Pop(2)
|
|
Dim bResult As Boolean = False
|
|
If WriteVariable(sVarAddr, sValue, bResult) Then
|
|
state.PushBoolean(bResult)
|
|
' state.PushInteger(If(bResult, 1, 0))
|
|
Return 1
|
|
Else
|
|
Return 0
|
|
End If
|
|
End Function
|
|
|
|
Private Function WriteVariable(sVarAddr As String, sValue As String, ByRef bResult As Boolean) As Boolean
|
|
Select Case sVarAddr
|
|
Case "a"
|
|
bResult = sValue = "abbecedario"
|
|
Case "b"
|
|
bResult = sValue = "bullone"
|
|
Case "c"
|
|
bResult = sValue = "cane"
|
|
Case "d"
|
|
bResult = sValue = "diario"
|
|
Case "e"
|
|
bResult = sValue = "elefante"
|
|
Case "f"
|
|
bResult = sValue = "foglia"
|
|
Case Else
|
|
Return False
|
|
End Select
|
|
Return True
|
|
End Function
|
|
|
|
Friend Function LuaInstallGeneral(state As Lua) As Boolean
|
|
If IsNothing(state) Then Return False
|
|
state.Register("EntIs64bit", func_EntIs64bit)
|
|
state.Register("EntOutLog", func_EntOutLog)
|
|
state.Register("EntExistsDirectory", func_EntExistsDirectory)
|
|
state.Register("EntCopyFile", func_EntCopyFile)
|
|
state.Register("EntGetIniFile", func_EntGetIniFile)
|
|
state.Register("EntGetNumberFromIni", func_EntGetNumberFromIni)
|
|
state.Register("EntGetStringFromIni", func_EntGetStringFromIni)
|
|
state.Register("EntWriteStringToIni", func_EntWriteStringToIni)
|
|
state.Register("RdsStringGet", func_RdsStringGet)
|
|
state.Register("RdsStringSetAsync", func_RdsStringSetAsync)
|
|
state.Register("RdsPublishAsync", func_RdsPublishAsync)
|
|
state.Register("ReadVariable", func_ReadVariable)
|
|
state.Register("WriteVariable", func_WriteVariable)
|
|
state.Register("print", func_print)
|
|
Return True
|
|
End Function
|
|
|
|
End Module
|