public IEnumerable <VersionInfo> LoadVersionInfo() { MySqlResult result = SelectPreparedStatement(AuthenticationPreparedStatement.VersionSelect); for (int i = 0; i < result?.Count; i++) { yield return(new VersionInfo(result.Rows[i])); } }
public IEnumerable <RealmInfo> GetRealmList() { MySqlResult result = SelectPreparedStatement(DataCentrePreparedStatement.RealmListSelect); for (int i = 0; i < result?.Count; i++) { yield return(new RealmInfo(result.Rows[i])); } }
public IEnumerable <(byte CityStateId, WorldPosition Position)> GetCharacterSpawns() { MySqlResult result = SelectPreparedStatement(WorldPreparedStatement.CharacterSpawnSelect); for (uint i = 0; i < result?.Count; i++) { yield return(result.Read <byte>(i, "cityStateId"), new WorldPosition(result.Rows[(int)i])); } }
public async Task <uint> GetAccount(string sessionId) { if (sessionId == string.Empty) { return(0u); } MySqlResult result = await SelectPreparedStatementAsync(AuthenticationPreparedStatement.AccountSelect, sessionId); return(result.Count != 0u ? result.Read <uint>(0, "id") : 0u); }
public async Task <List <ServiceAccountInfo> > GetServiceAccounts(uint accountId) { var serviceAccountList = new List <ServiceAccountInfo>(); MySqlResult result = await SelectPreparedStatementAsync(AuthenticationPreparedStatement.AccountServiceSelect, accountId); for (int i = 0; i < result?.Count; i++) { serviceAccountList.Add(new ServiceAccountInfo(result.Rows[i])); } return(serviceAccountList); }
private static void AccountSelectCallback(MySqlResult result, Session session) { var connectResponse = new ServerPacket(0x0B, PacketHeaderFlags.ConnectRequest); connectResponse.Payload.Write(0u); connectResponse.Payload.Write(0u); connectResponse.Payload.Write(13626398284849559039ul); // some sort of check value? connectResponse.Payload.Write((ushort)0); connectResponse.Payload.Write((ushort)0); connectResponse.Payload.Write(ISAAC.ServerSeed); connectResponse.Payload.Write(ISAAC.ClientSeed); connectResponse.Payload.Write(0u); NetworkManager.SendPacket(ConnectionType.Login, connectResponse, session); if (result.Count == 0) { session.SendCharacterError(CharacterError.AccountDoesntExist); return; } uint accountId = result.Read <uint>(0, "id"); string account = result.Read <string>(0, "account"); if (WorldManager.Find(account) != null) { session.SendCharacterError(CharacterError.AccountInUse); return; } string digest = SHA2.Hash(SHA2Type.SHA256, result.Read <string>(0, "password") + result.Read <string>(0, "salt")); /*if (glsTicket != digest) * { * }*/ /*if (WorldManager.ServerIsFull()) * { * session.SendCharacterError(CharacterError.LogonServerFull); * return; * }*/ // TODO: check for account bans session.SetAccount(accountId, account); }
/// <summary> /// Executes the current associated database script. /// </summary> public void Execute() { _executed = false; _error = ""; if (_databasescript != null) { MySqlResult _result = MySql.Execute(SCMS.ServerConnection.ToString(), _databasescript.SqlStatement); _executed = _result.Succeeded; _error = _result.Error; if (!string.IsNullOrEmpty(_error.RLTrim())) { SCMS.LogError(this.GetType().Name, new Exception(_error)); } _result = null; Materia.RefreshAndManageCurrentProcess(); } }
public static void CharacterListSelectCallback(MySqlResult result, Session session) { var characterList = new ServerPacket(0x0B, PacketHeaderFlags.EncryptedChecksum); var characterFragment = new ServerPacketFragment(9, FragmentOpcode.CharacterList); characterFragment.Payload.Write(0u); characterFragment.Payload.Write(result.Count); session.CachedCharacters.Clear(); for (byte i = 0; i < result.Count; i++) { uint lowGuid = result.Read <uint>(i, "guid"); string name = result.Read <string>(i, "name"); characterFragment.Payload.Write(lowGuid); characterFragment.Payload.WriteString16L(name); ulong deleteTime = result.Read <ulong>(i, "deleteTime"); characterFragment.Payload.Write(deleteTime != 0ul ? (uint)(WorldManager.GetUnixTime() - deleteTime) : 0u); session.CachedCharacters.Add(new CachedCharacter(lowGuid, i, name)); } characterFragment.Payload.Write(0u); characterFragment.Payload.Write(11u /*slotCount*/); characterFragment.Payload.WriteString16L(session.Account); characterFragment.Payload.Write(0u /*useTurbineChat*/); characterFragment.Payload.Write(0u /*hasThroneOfDestiny*/); characterList.Fragments.Add(characterFragment); NetworkManager.SendPacket(ConnectionType.Login, characterList, session); var serverName = new ServerPacket(0x0B, PacketHeaderFlags.EncryptedChecksum); var serverNameFragment = new ServerPacketFragment(9, FragmentOpcode.ServerName); serverNameFragment.Payload.Write(0u); serverNameFragment.Payload.Write(0u); serverNameFragment.Payload.WriteString16L(ConfigManager.Config.Server.WorldName); serverName.Fragments.Add(serverNameFragment); NetworkManager.SendPacket(ConnectionType.Login, serverName, session); }
public async Task <List <CharacterInfo> > GetCharacters(uint serviceAccountId) { var characters = new List <CharacterInfo>(); MySqlResult result = await SelectPreparedStatementAsync(DataCentrePreparedStatement.CharacterSelect, serviceAccountId); for (int i = 0; i < result?.Count; i++) { var characterInfo = new CharacterInfo(result.Rows[i]); MySqlResult levelResult = await SelectPreparedStatementAsync(DataCentrePreparedStatement.CharacterClassSelect, characterInfo.Id); for (int j = 0; j < levelResult?.Count; j++) { characterInfo.AddClassInfo(levelResult.Rows[j]); } characters.Add(characterInfo); } return(characters); }
private void RunBackUp() { if (_isrunning) { return; } if (_succeeded) { _succeeded = false; } if (!Directory.Exists(Path.GetDirectoryName(lblPath.Text))) { MsgBoxEx.Shout("Please specify a valid database backup output directory.", "Database Backup"); return; } _isrunning = true; _cancelled = false; InitializeEventGrid(); btnBrowse.Enabled = false; btnBackup.Enabled = false; pctLoad.Show(); pctLoad.BringToFront(); btnCancel.BringToFront(); AddEvent(BackupEvent.Information, "Database backup operations started."); string _tempdir = Application.StartupPath + "\\dbtemp\\"; int _trycounter = 0; if (!Directory.Exists(_tempdir)) { try { Directory.CreateDirectory(_tempdir); } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Failed to create temporary output directory."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); _isrunning = false; btnCancel.SendToBack(); return; } } MySqlDumpParameterCollection _parameters = new MySqlDumpParameterCollection(); _parameters.Add(MySqlDumpParameters.CompleteInsert); _parameters.Add(MySqlDumpParameters.HexBlob); _parameters.Add(MySqlDumpParameters.Routines); _parameters.Add("--skip-extended-insert"); _parameters.Add(MySqlDumpParameters.Triggers); _parameters.Add(MySqlDumpParameters.Quick); string _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".sql"; Func <string, string, MySqlDumpParameterCollection, MySqlResult> _dumpdelegate = new Func <string, string, MySqlDumpParameterCollection, MySqlResult>(MySql.Dump); IAsyncResult _dumpresult = _dumpdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _filename, _parameters, null, _dumpdelegate); AddEvent(BackupEvent.Information, "Running database backup operations."); while (!_dumpresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (!_dumpresult.IsCompleted) { try { _dumpresult = null; } catch { } } AddEvent(BackupEvent.Warning, "Cancelling database backup..."); Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } AddEvent(BackupEvent.Information, "Database backup operations has been cancelled."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { MySqlResult _dumpexecresult = _dumpdelegate.EndInvoke(_dumpresult); if (_dumpexecresult.Succeeded) { AddEvent(BackupEvent.Information, "Database backup has been created."); AddEvent(BackupEvent.Information, "Archiving backup file..."); Func <string, ArchivingToolEnum, FileInfo> _archiverdelegate = new Func <string, ArchivingToolEnum, FileInfo>(Archiver.CompressInsert); IAsyncResult _archiverresult = _archiverdelegate.BeginInvoke(_filename, ArchivingToolEnum.SevenZip, null, _archiverdelegate); while (!_archiverresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database backup..."); Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } _trycounter = 0; _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z"; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(20); Application.DoEvents(); } AddEvent(BackupEvent.Information, "Database backup operations has been cancelled."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { FileInfo _archivefile = _archiverdelegate.EndInvoke(_archiverresult); if (_archivefile != null) { AddEvent(BackupEvent.Information, "Database backup file has been archived."); AddEvent(BackupEvent.Information, "Finalizing database backup operations..."); bool _copied = false; try { File.Copy(_archivefile.FullName, lblPath.Text, true); _copied = true; Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } _trycounter = 0; _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z"; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Failed to transfer archive file into output destinaton."); AddEvent(BackupEvent.Warning, "Cancelling database backup..."); Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } _trycounter = 0; _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z"; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } AddEvent(BackupEvent.Information, "Database backup operations has been cancelled."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } if (_copied) { IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.BackupDatabase, "Performed database backup into : " + lblPath.Text.ToSqlValidString().Replace("\\\\", "\\") + "."); _logresult.WaitToFinish(); AddEvent(BackupEvent.Success, "Database backup operations has been completed."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; if (!_succeeded) { _succeeded = true; } } } else { AddEvent(BackupEvent.Error, "Database backup file archiving failed."); AddEvent(BackupEvent.Warning, "Cancelling database backup..."); Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } _trycounter = 0; _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z"; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } AddEvent(BackupEvent.Information, "Database backup operations has been cancelled."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } } } else { SCMS.LogError(this.GetType().Name, new Exception(_dumpexecresult.Error)); AddEvent(BackupEvent.Error, "Database backup operations has been cancelled."); AddEvent(BackupEvent.Warning, "Cancelling database backup..."); Materia.RefreshAndManageCurrentProcess(); _trycounter = 0; while (File.Exists(_filename) && _trycounter < 30) { try { File.Delete(_filename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } AddEvent(BackupEvent.Information, "Database backup operations has been cancelled."); btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose; pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false; _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } } }
public ulong GetMaxCharacterId() { MySqlResult result = SelectPreparedStatement(DataCentrePreparedStatement.CharacterMaxSelect); return(result?.Read <ulong>(0, "max") ?? 0ul); }
public async Task <(uint ServiceAccountId, ulong CharacterId)> GetCharacterSession(uint actorId, string source) { MySqlResult result = await SelectPreparedStatementAsync(DataCentrePreparedStatement.CharacterSessionSelect, actorId, source); return(result?.Count > 0u ? (result.Read <uint>(0, "serviceAccountId"), result.Read <ulong>(0, "id")) : (0u, 0ul)); }
public async Task <bool> IsCharacterNameAvailable(string name) { MySqlResult result = await SelectPreparedStatementAsync(DataCentrePreparedStatement.CharacterNameCount, name); return(result?.Read <uint>(0, "count") == 0u); }
private void RunRestoration() { if (!File.Exists(_backupfilename)) { MsgBoxEx.Shout("Could not locate the specified database backup file / restore point.", "Database Restoration"); return; } btnBrowseDrive.Enabled = false; btnBrowseRestorePoint.Enabled = false; btnRestore.Enabled = false; btnCancel.BringToFront(); InitializeEventGrid(); chkCreateRestorePoint.Enabled = false; pctLoad.Show(); pctLoad.BringToFront(); _isrunning = true; _cancelled = false; AddEvent(BackupEvent.Information, "Starting database restoration routines..."); RestorePointInfo _restorepoint = null; if (chkCreateRestorePoint.Checked) { _restorepoint = MakeRestorePoint(); if (_restorepoint == null) { btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _filename = _backupfilename; int _trycounter = 0; if (Path.GetExtension(_filename).ToLower().Replace(".", "").RLTrim() == "scmsiv") { AddEvent(BackupEvent.Information, "Extracting database backup..."); _filename = ""; string _dbtempdir = Application.StartupPath + "\\dbtemp"; if (!Directory.Exists(_dbtempdir)) { try { Directory.CreateDirectory(_dbtempdir); } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Could not create temporary backup file extraction directory."); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _archivedir = _dbtempdir + "\\" + Path.GetFileNameWithoutExtension(_backupfilename); Func <string, string, bool> _extractordelegate = new Func <string, string, bool>(Archiver.Decompress); IAsyncResult _extractorresult = _extractordelegate.BeginInvoke(_backupfilename, _archivedir, null, _extractordelegate); while (!_extractorresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { bool _extracted = _extractordelegate.EndInvoke(_extractorresult); if (_extracted) { if (Directory.Exists(_archivedir)) { string[] _files = Directory.GetFiles(_archivedir); foreach (string _file in _files) { if (Path.GetExtension(_file).ToLower().Replace(".", "").RLTrim() == "sql") { _filename = _file; break; } } if (String.IsNullOrEmpty(_filename.RLTrim())) { AddEvent(BackupEvent.Error, "Could not find any supported database backup file."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } } if (!String.IsNullOrEmpty(_filename.RLTrim())) { FileInfo _backupfile = new FileInfo(_filename); Func <string, FileInfo, MySqlResult> _restoredelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restoreresult = _restoredelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _backupfile, null, _restoredelegate); while (!_restoreresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 30) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 30) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { MySqlResult _execresult = _restoredelegate.EndInvoke(_restoreresult); if (_execresult.Succeeded) { AddEvent(BackupEvent.Information, "Finalizing database restoration..."); IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.RestoreDatabase, "Restored database from file : " + lblPath.Text.ToSqlValidString().Replace("\\\\", "\\") + "."); IAsyncResult _queexresult = Que.BeginExecution(SCMS.ServerConnection.ToString(), "UPDATE `settings` SET `LastRestored` = NOW();"); while (!_logresult.IsCompleted && !_queexresult.IsCompleted) { Thread.Sleep(1); Application.DoEvents(); } QueResult _queresult = Que.EndExecution(_queexresult); _queresult.Dispose(QueResultDisposition.WithAssociatedQue); SCMS.CleanUp(); _isrunning = false; AddEvent(BackupEvent.Success, "Database backup restoration has been completed."); _trycounter = 0; while (_trycounter <= 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } MsgBoxEx.Inform("Application will restart for the restored values to fully take effect.", "Database Backup Restoration"); DialogResult = System.Windows.Forms.DialogResult.OK; Close(); } else { SCMS.LogError(this.GetType().Name, new Exception(_execresult.Error)); AddEvent(BackupEvent.Error, "Failed to complete database restoration from the specified database backup / restore point."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 15) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); } } } }
private RestorePointInfo MakeRestorePoint() { RestorePointInfo _restorepoint = null; AddEvent(BackupEvent.Information, "Creating database restore point..."); string _rpdir = Application.StartupPath + "\\Restore Points"; if (!Directory.Exists(_rpdir)) { try { Directory.CreateDirectory(_rpdir); } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Can't create database restore point directory."); } } if (Directory.Exists(_rpdir)) { Func <IDbConnection, DateTime> _serverdatedelegate = new Func <IDbConnection, DateTime>(MySql.GetServerDateAndTime); IAsyncResult _serverdateresult = _serverdatedelegate.BeginInvoke(SCMS.Connection, null, _serverdatedelegate); _serverdateresult.WaitToFinish(); DateTime _serverdatetime = _serverdatedelegate.EndInvoke(_serverdateresult); string _rpfilename = _rpdir + "\\" + SCMS.ServerConnection.Database.ToUpper() + "_" + SCMS.CurrentCompany.Company + "_RESTORE_POINT_" + VisualBasic.Format(_serverdatetime, "dd_MM_yyyy_HH_mm_ss") + ".sql"; int _trycounter = 0; MySqlDumpParameterCollection _parameters = new MySqlDumpParameterCollection(); _parameters.Add(MySqlDumpParameters.CompleteInsert); _parameters.Add(MySqlDumpParameters.HexBlob); _parameters.Add(MySqlDumpParameters.Routines); _parameters.Add("--skip-extended-insert"); _parameters.Add(MySqlDumpParameters.Triggers); _parameters.Add(MySqlDumpParameters.Quick); Func <string, string, MySqlDumpParameterCollection, MySqlResult> _dumpdelegate = new Func <string, string, MySqlDumpParameterCollection, MySqlResult>(MySql.Dump); IAsyncResult _dumpresult = _dumpdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _rpfilename, _parameters, null, _dumpdelegate); AddEvent(BackupEvent.Information, "Restore point creation started."); while (!_dumpresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling restore point creation..."); _trycounter = 0; while (File.Exists(_rpfilename) && _trycounter <= 30) { try { File.Delete(_rpfilename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Restore point creation cancelled."); } else { MySqlResult _result = _dumpdelegate.EndInvoke(_dumpresult); if (_result.Succeeded) { string _path = Application.StartupPath + "\\Xml\\restorepoints.xml"; DataTable _table = SCMS.XmlToTable(_path); if (_table != null) { bool _created = false; DataRow _newrow = null; object[] _values = new object[_table.Columns.Count]; DataColumnCollection _cols = _table.Columns; _values[_cols["DateAndTime"].Ordinal] = _serverdatetime; _values[_cols["Filename"].Ordinal] = _rpfilename; _values[_cols["Company"].Ordinal] = SCMS.CurrentCompany.Company; _values[_cols["Server"].Ordinal] = SCMS.ServerConnection.Server; _values[_cols["Database"].Ordinal] = SCMS.ServerConnection.Database; try { _newrow = _table.Rows.Add(_values); _table.AcceptChanges(); _table.WriteXml(_path, XmlWriteMode.WriteSchema); _created = true; } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point."); _trycounter = 0; while (File.Exists(_rpfilename) && _trycounter <= 30) { try { File.Delete(_rpfilename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } } if (_created) { _restorepoint = new RestorePointInfo(VisualBasic.CLng(_newrow["DetailId"])); } _table.Dispose(); _table = null; Materia.RefreshAndManageCurrentProcess(); } else { AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point."); _trycounter = 0; while (File.Exists(_rpfilename) && _trycounter <= 30) { try { File.Delete(_rpfilename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Materia.RefreshAndManageCurrentProcess(); } } else { SCMS.LogError(this.GetType().Name, new Exception(_result.Error)); AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point."); _trycounter = 0; while (File.Exists(_rpfilename) && _trycounter <= 30) { try { File.Delete(_rpfilename); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Materia.RefreshAndManageCurrentProcess(); } } } return(_restorepoint); }