public static void GetProgressBar(ProgressBarInfo pbInfo) { StartBox(); GUILayout.Label("Progressbar :", (GUIStyle)"BoldLabel"); pbInfo.ProgressbarWindow = (MgsProgressWindow)EditorGUILayout.ObjectField( new GUIContent("ProgressbarWindow"), pbInfo.ProgressbarWindow, typeof(MgsProgressWindow), true); if (pbInfo.ProgressbarWindow != null) { EditorTools.Instance.LanguageField(TargetNode, "Progressbar Message", ref pbInfo.ProgressbarMessage); GUILayout.BeginHorizontal(); { GUILayout.Label("Display method:"); pbInfo.ProgressbarShow = GUILayout.Toggle(pbInfo.ProgressbarShow, "Show"); pbInfo.ProgressbarHide = GUILayout.Toggle(pbInfo.ProgressbarHide, "Hide"); } GUILayout.EndHorizontal(); } EndBox(); }
public void ShowProgressBar(ProgressBarInfo progressBarInfo) { Console.Write(progressBarInfo.Message + " "); progressBarInfo.ProgressChanged += (sender, d) => { if (progressBarInfo.IsClosed) { return; } var progressBlockCount = (int)(d * Bars); var percent = (int)(d * 100); var text = string.Format("[{0}{1}] {2,3}%", progressBlockCount == 0 ? "" : new string('=', progressBlockCount - 1) + ">", new string(' ', Bars - progressBlockCount), percent); UpdateText(text); }; progressBarInfo.Closed += (sender, args) => { UpdateText(string.Empty); Console.WriteLine("Done"); }; }
public void ShowProgressBar(ProgressBarInfo progressBarInfo) { _mainForm.ShowProgressBar(progressBarInfo.Message); progressBarInfo.ProgressChanged += (sender, d) => { _mainForm.ChangeProgress(d); }; progressBarInfo.Closed += (sender, args) => { _mainForm.HideProgressBar(); }; }
public void DownloadDatabase(DirectoryInfo directory, string emailAddress, string password) { Logger.Info("Geo-IP database not found, preparing download..."); if (directory.Exists) { directory.Delete(true); } Logger.Debug("Create directory"); directory.Create(); FileInfo csvFile = null; if (File.Exists("IP2LOCATION-LITE-DB11.CSV")) { csvFile = new FileInfo("IP2LOCATION-LITE-DB11.CSV"); Logger.Info("Local CSV file found"); } else { var zipFile = new FileInfo(Path.Combine(directory.FullName, "temp.zip")); var progressInfo = new ProgressBarInfo( $"{DateTime.Now:dd-MM-yyyy HH:mm:ss.ffff}\t[PROGRESS] Download IP2Location database"); UiManager.UiImplementation.ShowProgressBar(progressInfo); try { using (var webClient = new WebClient { Proxy = null }) { var url = $"http://www.ip2location.com/download?login={Uri.EscapeDataString(emailAddress)}&password={Uri.EscapeDataString(password)}&productcode={DatabaseProduct}"; webClient.DownloadProgressChanged += (sender, args) => progressInfo.ReportProgress(args.ProgressPercentage / 100d); using (var autoResetEvent = new AutoResetEvent(false)) { webClient.DownloadFileCompleted += (sender, args) => autoResetEvent.Set(); webClient.DownloadFileAsync(new Uri(url), zipFile.FullName); autoResetEvent.WaitOne(); } } } finally { progressInfo.Close(); } if (zipFile.Length < 10 * 1024) //noob tactic, checking the file size { var errorText = File.ReadAllText(zipFile.FullName); directory.Delete(true); throw new InvalidOperationException(errorText); } Logger.Info("Download finished successfully"); using (var fileStream = new FileStream(zipFile.FullName, FileMode.Open, FileAccess.Read)) { var zf = new ZipFile(fileStream); foreach (ZipEntry zipEntry in zf) { if (zipEntry.IsFile && zipEntry.Name.EndsWith(".csv", StringComparison.OrdinalIgnoreCase)) { var zipProgressInfo = new ProgressBarInfo($"{DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss.ffff")}\t[PROGRESS] Extracting archive"); UiManager.UiImplementation.ShowProgressBar(zipProgressInfo); try { using (var csvFileStream = new FileStream("IP2LOCATION-LITE-DB11.CSV", FileMode.CreateNew, FileAccess.Write)) using (var inputStream = zf.GetInputStream(zipEntry)) { byte[] buffer = new byte[16 * 1024]; int read; while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0) { csvFileStream.Write(buffer, 0, read); zipProgressInfo.ReportProgress(csvFileStream.Position / (double)zipEntry.Size); } } } finally { zipProgressInfo.Close(); } csvFile = new FileInfo("IP2LOCATION-LITE-DB11.CSV"); break; } } } if (csvFile == null) { throw new FileNotFoundException("Could not find CSV file in zip archive"); } zipFile.Delete(); } Logger.Info("Building database"); var databaseFile = new FileInfo(Path.Combine(directory.FullName, "geoipDatabase.sqlite")); SqliteConnection.CreateFile(databaseFile.FullName); using (var sqliteConnection = new SqliteConnection($"Data Source={databaseFile.FullName};Version=3;")) { sqliteConnection.Open(); using ( var command = new SqliteCommand( "CREATE TABLE `IpLocation` (IpFrom INTEGER UNIQUE, IpTo INTEGER PRIMARY KEY, Country TEXT, CountryName TEXT, Region TEXT, City TEXT, Latitude REAL, Longitude REAL, ZipCode TEXT, Timezone TEXT)", sqliteConnection)) command.ExecuteNonQuery(); using ( var command = new SqliteCommand( "CREATE TABLE `Properties` (Name TEXT, Value TEXT)", sqliteConnection)) command.ExecuteNonQuery(); using ( var command = new SqliteCommand( $"INSERT INTO `Properties` (Name, Value) VALUES ('CreationDate', '{DateTime.Now.ToString(CultureInfo.InvariantCulture)}')", sqliteConnection)) command.ExecuteNonQuery(); using (var csvReader = new CsvReader(csvFile.FullName)) using (var transaction = sqliteConnection.BeginTransaction()) { using (var command = sqliteConnection.CreateCommand()) { command.Transaction = transaction; while (csvReader.ReadNextRecord()) { command.CommandText = $"INSERT INTO `IpLocation` (IpFrom, IpTo, Country, CountryName, Region, City, Latitude, Longitude, ZipCode, Timezone) VALUES ({csvReader.Fields[0]}, {csvReader.Fields[1]}, '{csvReader.Fields[2]}', @countryName, @region, @city, {csvReader.Fields[6]}, {csvReader.Fields[7]}, '{csvReader.Fields[8]}', '{csvReader.Fields[9]}')"; command.Parameters.AddWithValue("@countryName", csvReader.Fields[3]); command.Parameters.AddWithValue("@region", csvReader.Fields[4]); command.Parameters.AddWithValue("@city", csvReader.Fields[5]); command.ExecuteNonQuery(); } transaction.Commit(); } } Logger.Info("Database created"); } }