Imports System.Threading Imports System.Math Imports System.IO Imports Effector.Plugin.Lib Public Class MainWindowM #Region "FIELDS" ' massimo numero di istanze del programma ammesse Const MAX_INST As Integer = 1 Private m_objMutex As Mutex Private m_sDataRoot As String = String.Empty Friend ReadOnly Property sDataRoot As String Get Return m_sDataRoot End Get End Property Private m_sDataDir As String = String.Empty Friend ReadOnly Property sDataDir As String Get Return m_sDataDir End Get End Property Private m_sConfigDir As String = String.Empty Public ReadOnly Property sConfigDir As String Get Return m_sConfigDir End Get End Property Private m_sScriptDir As String = String.Empty Public ReadOnly Property sScriptDir As String Get Return m_sScriptDir End Get End Property Private m_nDebug As Integer = 0 Public ReadOnly Property DebugLevel As Integer Get Return m_nDebug End Get End Property Private m_bFirstInstance As Boolean = False Friend ReadOnly Property bFirstInstance As Boolean Get Return m_bFirstInstance End Get End Property Private m_nInstance As Integer = 0 Friend ReadOnly Property nInstance As Integer Get Return m_nInstance End Get End Property Private m_nUserLevel As Integer = 1 Friend ReadOnly Property nUserLevel As Integer Get Return m_nUserLevel End Get End Property Private m_nKeyLevel As Integer = 0 Friend ReadOnly Property nKeyLevel As Integer Get Return m_nKeyLevel End Get End Property Private m_nKeyOpt1 As UInteger = 0 Friend ReadOnly Property nKeyOpt1 As Integer Get Return m_nKeyOpt1 End Get End Property Private m_nKeyOpt2 As UInteger = 0 Friend ReadOnly Property nKeyOpt2 As Integer Get Return m_nKeyOpt2 End Get End Property Friend ReadOnly Property sVersion As String Get Return My.Application.Info.Version.Major.ToString() & "." & My.Application.Info.Version.Minor.ToString() & (ChrW(97 - 1 + My.Application.Info.Version.Build)).ToString() & My.Application.Info.Version.Revision.ToString() End Get End Property Private m_sTempDir As String Friend ReadOnly Property sTempDir As String Get Return m_sTempDir End Get End Property Private m_sResourcesRoot As String Friend ReadOnly Property sResourcesRoot As String Get Return m_sResourcesRoot End Get End Property Private m_sLogFile As String Friend ReadOnly Property sLogFile As String Get Return m_sLogFile End Get End Property #End Region ' FIELDS #Region "CONSTRUCTOR" Sub New() InitializeEgtEnvironment() End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Private Sub InitializeEgtEnvironment() ' Impostazione path radice per i dati m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory If GetPrivateProfileString(S_DATA, K_DATAROOT, "", m_sDataRoot, m_sDataRoot & "\" & DAT_FILE_NAME) = 0 Then m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory End If ' Impostazione direttorio di configurazione m_sConfigDir = m_sDataRoot & "\" & CONF_DIR ' Impostazione direttorio per file script m_sScriptDir = m_sDataRoot & "\" & SCRIPT_DIR ' Impostazione direttorio per file temporanei m_sTempDir = m_sDataRoot & "\" & TEMP_DIR ' Impostazione path Ini file IniFile.SetIniFile(m_sConfigDir & "\" & INI_FILE_NAME) ' Impostazione path resources dir m_sResourcesRoot = m_sDataRoot & "\" & RES_DIR ' Verifico indice di istanza ManageInstance() ' inizializzo log m_sLogFile = m_sTempDir & "\" & GENLOG_FILE_NAME.Replace("#", m_nInstance.ToString()) Lua_General.SetLogPath(m_sLogFile) If File.Exists(m_sLogFile) Then Try File.Copy(m_sLogFile, m_sLogFile & ".bak", True) File.Delete(m_sLogFile) Catch ex As Exception EgtOutLog("Old log delete failed!" & ex.Message) End Try End If Dim sLogMsg As String = "User " & Environment.MachineName & "\" & Environment.UserName & " (" & m_nInstance.ToString() & ")" & vbLf & My.Application.Info.Title.ToString() & " ver. " & My.Application.Info.Version.Major.ToString() & "." & My.Application.Info.Version.Minor.ToString() & (ChrW(97 - 1 + My.Application.Info.Version.Build)).ToString() & My.Application.Info.Version.Revision.ToString() EgtOutLog(sLogMsg) ' Leggo lingua corrente Dim sLanguage As String = String.Empty GetMainPrivateProfileString(S_GENERAL, K_MESSAGES, "", sLanguage) ' Recupero nome file dei messaggi della lingua corrente Dim sMsgName As String = "Eng.txt" Dim nIndex As Integer = 1 Dim ReadLanguage As Language = GetMainPrivateProfileLanguage(S_LANGUAGES, nIndex) While Not IsNothing(ReadLanguage) If String.Compare(ReadLanguage.Name, sLanguage, True) = 0 Then sMsgName = ReadLanguage.FilePath Exit While End If nIndex += 1 ReadLanguage = GetMainPrivateProfileLanguage(S_LANGUAGES, nIndex) End While ' recupero file messaggi Dim sMsgFilePath As String = m_sDataRoot & "\" & MSG_DIR & "\" & sMsgName ' verifico se c'e' un plugin Dim sPluginName As String = "" If GetMainPrivateProfileString(S_GENERAL, K_PLUGINNAME, "", sPluginName) > 0 AndAlso Not String.IsNullOrWhiteSpace(sPluginName) Then ' recupero nome del file messaggi del plugin Dim sPlugInMsgFilePath As String = m_sDataRoot & "\Plugin\" & sPluginName & "\Messages\" & sMsgName If File.Exists(sPlugInMsgFilePath) Then Dim OrigMsgFile As List(Of String) = File.ReadAllLines(sMsgFilePath).ToList() ' elimino eventuali righe vuote o commento di fine While String.IsNullOrWhiteSpace(OrigMsgFile.Last()) OrElse OrigMsgFile.Last().StartsWith("//") OrigMsgFile.RemoveAt(OrigMsgFile.Count - 1) End While Dim PluginMsgFile As List(Of String) = File.ReadAllLines(sPlugInMsgFilePath).ToList() ' elimino eventuale riga di inizio file If PluginMsgFile.Count > 1 AndAlso PluginMsgFile(1).StartsWith("//") AndAlso PluginMsgFile(0).StartsWith("//") Then PluginMsgFile.RemoveAt(0) End If OrigMsgFile.AddRange(PluginMsgFile) Dim sNewMsgFilePath As String = m_sDataRoot & "\Plugin\" & sPluginName & "\Messages\Complete" & sMsgName Dim bNewMsgFile As Boolean = False Try File.WriteAllLines(sNewMsgFilePath, OrigMsgFile) bNewMsgFile = True Catch ex As Exception End Try If bNewMsgFile Then sMsgFilePath = sNewMsgFilePath End If End If End If ' Leggo file messaggi If Not EgtLoadMessages(sMsgFilePath) Then EgtOutLog("Error in EgtLoadMessages") End If ' Gestione della chiave di protezione Dim sLicFileName As String = "" GetMainPrivateProfileString(S_GENERAL, K_LICENCE, LIC_FILE_NAME, sLicFileName) Dim sLicFile As String = sConfigDir & "\" & sLicFileName Dim sKey As String = "" GenInterface.GetPrivateProfileString(S_LICENCE, K_KEY, "", sKey, sLicFile) EgtSetKey(sKey) ' Recupero livello e opzioni della chiave Dim bKey As Boolean = EgtGetKeyLevel(7375, 2706, 1, m_nKeyLevel) And EgtGetKeyOptions(7375, 2706, 1, m_nKeyOpt1, m_nKeyOpt2) If Not bKey Then EgtOutLog("Key or Licence Problem (" & (-nKeyLevel).ToString() & ")") MessageBox.Show(String.Format(EgtMsg(201), (-nKeyLevel).ToString()), "Error", MessageBoxButton.OK, MessageBoxImage.Error) EgtOutLog("Exit") End End If 'EgtInit(m_nDebug, m_sLogFile, sLogMsg) 'EgtSetTempDir(m_sTempDir) 'EgtSetIniFile(IniFile.m_sIniFile) ' Leggo direttorio dei messaggi (se manca uso direttorio di configurazione) 'Dim sMsgDir As String = String.Empty 'If GetMainPrivateProfileString(S_GENERAL, K_MESSAGESDIR, "", sMsgDir) = 0 Then ' sMsgDir = m_sConfigDir 'End If '' Inizializzo OptionModule ''OptionModule.InitOptionModule() ' Leggo e imposto livello utilizzatore ' m_nUserLevel = Math.Min(m_nKeyLevel, GetMainPrivateProfileInt(S_GENERAL, K_USERLEVEL, 1)) ' Info su opzioni chiave 'EgtOutLog("KeyOptions : " & bKey.ToString() & " " & m_nKeyOptions.ToString()) ' inizializzo ambiente Lua LuaManager.Init() End Sub Private Sub ManageInstance() Dim bCreated As Boolean Try m_objMutex = New Mutex(False, "Global\Effector", bCreated) Catch bCreated = False End Try m_bFirstInstance = bCreated If bCreated Then ' Prima istanza m_nInstance = 1 ' Aggiorno stato istanze attive WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, m_nInstance.ToString()) Else ' Leggo il massimo numero di istanze ammesse Dim nMaxInst As Integer = GetMaxInstances() ' Cerco il primo indice di istanza libero Dim nTmp As Integer = GetMainPrivateProfileInt(S_GENERAL, K_INSTANCES, 0) m_nInstance = 1 Dim nMask As Integer = 1 While (nTmp And nMask) <> 0 And m_nInstance <= MAX_INST m_nInstance += 1 nMask *= 2 End While ' Se l'indice supera il massimo If m_nInstance > nMaxInst Then ' porto in primo piano la prima istanza Dim bFound As Boolean = False ' processi del programma a 32 bit Dim localProc As Process() = Process.GetProcessesByName("EffectorR32") For Each p As Process In localProc If p.Id <> Process.GetCurrentProcess().Id Then bFound = True ShowWindow(p.MainWindowHandle, 1) Exit For End If Next ' se non trovati processi a 32 bit provo a 64 bit If Not bFound Then localProc = Process.GetProcessesByName("EffectorR64") For Each p As Process In localProc If p.Id <> Process.GetCurrentProcess().Id Then bFound = True ShowWindow(p.MainWindowHandle, SW.RESTORE) Exit For End If Next End If ' esco dal programma End End If ' Aggiorno stato istanze attive nTmp += (1 << (m_nInstance - 1)) WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString()) End If End Sub Friend Function GetKeyOption1(nKeyOpt1 As Integer) As Boolean Return (m_nKeyOpt1 >= nKeyOpt1) End Function Friend Function GetKeyOption2(nKeyOpt2 As Integer) As Boolean Return (m_nKeyOpt2 = nKeyOpt2) End Function Friend Function GetMaxInstances() As Integer ' Leggo il massimo numero di istanze ammesse ' Dim nMaxInst As Integer = GetMainPrivateProfileInt(S_GENERAL, K_MAXINST, 1) Return 1 ' Max(1, Min(nMaxInst, MAX_INST)) End Function Friend Sub Close() '' Terminazione generale di EgtInterface 'EgtExit() ' Aggiorno istanze usate m_objMutex.WaitOne(1000) Dim nTmp As Integer = GetMainPrivateProfileInt(S_GENERAL, K_INSTANCES, 0) nTmp -= (1 << (m_nInstance - 1)) WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString()) m_objMutex.ReleaseMutex() ' Rilascio mutex If Not IsNothing(m_objMutex) Then m_objMutex.Close() End Sub #End Region ' METHODS End Class