Exemplo n.º 1
0
        private void updateFromFileList(string[] files)
        {
            // Set the timestamp for new / altered items.
            DateTime timestamp = DateTime.Now;

            // Make sure your UI will update
            this.BeginInvoke((MethodInvoker) delegate()
            {
                toolstrip.Enabled = false;
                progress.Maximum  = files.Length;
                progress.Value    = 0;
                status.Text       = "0 / " + files.Length.ToString();
            });

            // Create data directory if necessary
            if (!Directory.Exists(datafolder + "data"))
            {
                Directory.CreateDirectory(datafolder + "data");
            }

            // Loop through the files
            for (int i = 0; i < files.Length; i++)
            {
                // Thread sometimes likes to go out of sync... dirty fix
                if (i < (files.Length - 1))
                {
                    string output = "";

                    // Update UI
                    this.BeginInvoke((MethodInvoker) delegate()
                    {
                        if (i < files.Length)
                        {
                            progress.Value = i;
                            status.Text    = "Updating: " + (i + 1).ToString() + " / " + files.Length.ToString() + " - " + files[i].ToString();
                        }
                    });

                    // Read new file, and deserialize current file
                    GW2StringFile newFile = new GW2StringFile(files[i], timestamp);
                    GW2StringFile oldFile = new GW2StringFile();

                    // Make sure your new file actually has data
                    if (newFile.Items.Count > 0)
                    {
                        // Create language directory if necessary
                        output = datafolder + @"data\" + newFile.Language.ToString() + @"\";
                        if (!Directory.Exists(output))
                        {
                            Directory.CreateDirectory(output);
                        }

                        // Deserialize old data if availible
                        if (File.Exists(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2"))
                        {
                            using (FileStream filestream = new FileStream(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2", FileMode.Open))
                            {
                                BinaryFormatter serializer = new BinaryFormatter();
                                oldFile = (GW2StringFile)serializer.Deserialize(filestream);
                            }
                        }

                        // Make sure the language and filename are reset (it tends to forget this one)
                        oldFile.Language = newFile.Language;
                        oldFile.Filename = newFile.Filename;

                        // Loop through new items
                        foreach (GW2Entry item in newFile.Items)
                        {
                            // Check if row is already present
                            GW2Entry oldEntry = oldFile.Items.FirstOrDefault(a => a.row == item.row);

                            if (oldEntry != null)
                            {
                                // Check if row has changed
                                if (oldEntry.value != item.value)
                                {
                                    // Row has changed, update.
                                    oldEntry.value = item.value;
                                    oldEntry.stamp = item.stamp;
                                }
                            }
                            else
                            {
                                // No row found, add it.
                                oldFile.Items.Add(item);
                            }
                        }

                        // Serialize back to file.
                        using (FileStream filestream = new FileStream(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2", FileMode.Create))
                        {
                            BinaryFormatter serializer = new BinaryFormatter();
                            serializer.Serialize(filestream, oldFile);
                        }

                        oldFile = null;
                        newFile = null;
                    }
                }
            }

            // Restore UI to previous state and reload grid.
            this.BeginInvoke((MethodInvoker) delegate()
            {
                toolstrip.Enabled = true;
                progress.Value    = 0;
                status.Text       = "";
                refreshGridAsync();
            });
        }
Exemplo n.º 2
0
        private void updateFromFileList(string[] files)
        {
            // Set the timestamp for new / altered items.
            DateTime timestamp = DateTime.Now;

            // Make sure your UI will update
            this.BeginInvoke((MethodInvoker)delegate()
            {
                toolstrip.Enabled = false;
                progress.Maximum = files.Length;
                progress.Value = 0;
                status.Text = "0 / " + files.Length.ToString();
            });

            // Create data directory if necessary
            if (!Directory.Exists(datafolder + "data")) Directory.CreateDirectory(datafolder + "data");

            // Loop through the files
            for (int i = 0; i < files.Length; i++)
            {
                // Thread sometimes likes to go out of sync... dirty fix
                if (i < (files.Length - 1))
                {
                    string output = "";

                    // Update UI
                    this.BeginInvoke((MethodInvoker)delegate()
                    {
                        progress.Value = i;
                        status.Text = "Updating: " + (i + 1).ToString() + " / " + files.Length.ToString() + " - " + files[i].ToString();
                    });

                    // Read new file, and deserialize current file
                    GW2StringFile newFile = new GW2StringFile(files[i], timestamp);
                    GW2StringFile oldFile = new GW2StringFile();

                    // Make sure your new file actually has data
                    if (newFile.Items.Count > 0)
                    {
                        // Create language directory if necessary
                        output = datafolder + @"data\" + newFile.Language.ToString() + @"\";
                        if (!Directory.Exists(output)) Directory.CreateDirectory(output);

                        // Deserialize old data if availible
                        if (File.Exists(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2"))
                        {
                            using (FileStream filestream = new FileStream(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2", FileMode.Open))
                            {
                                BinaryFormatter serializer = new BinaryFormatter();
                                oldFile = (GW2StringFile)serializer.Deserialize(filestream);
                            }
                        }

                        // Make sure the language and filename are reset (it tends to forget this one)
                        oldFile.Language = newFile.Language;
                        oldFile.Filename = newFile.Filename;

                        // Loop through new items
                        foreach (GW2Entry item in newFile.Items)
                        {
                            // Check if row is already present
                            GW2Entry oldEntry = oldFile.Items.FirstOrDefault(a => a.row == item.row);

                            if (oldEntry != null)
                            {
                                // Check if row has changed
                                if (oldEntry.value != item.value)
                                {
                                    // Row has changed, update.
                                    oldEntry.value = item.value;
                                    oldEntry.stamp = item.stamp;
                                }
                            }
                            else
                            {
                                // No row found, add it.
                                oldFile.Items.Add(item);
                            }
                        }

                        // Serialize back to file.
                        using (FileStream filestream = new FileStream(output + Path.GetFileNameWithoutExtension(newFile.Filename) + ".gw2", FileMode.Create))
                        {
                            BinaryFormatter serializer = new BinaryFormatter();
                            serializer.Serialize(filestream, oldFile);
                        }

                        oldFile = null;
                        newFile = null;
                    }
                }
            }

            // Restore UI to previous state and reload grid.
            this.BeginInvoke((MethodInvoker)delegate()
                {
                    toolstrip.Enabled = true;
                    progress.Value = 0;
                    status.Text = "";
                    refreshGridAsync();
                });
        }
Exemplo n.º 3
0
        // Updates the gridview from serialized files
        private void refreshGrid()
        {
            // Clear previous data
            stringData = null;
            stringData = new DataSet();

            // Generate columns
            stringTable = stringData.Tables.Add();

            stringTable.Columns.Add("timestamp", typeof(DateTime));
            stringTable.Columns.Add("key");
            stringTable.Columns.Add("value");

            string input = datafolder + @"data\" + Language + @"\";

            this.BeginInvoke((MethodInvoker) delegate()
            {
                // Clear data grid
                grid.DataSource = null;

                // Set status text
                status.Text       = "Loading...";
                toolstrip.Enabled = false;
            });

            // Make sure the data directory actually exists
            if (Directory.Exists(input))
            {
                // Get file list
                string[] files = Directory.GetFiles(input, "*.gw2");

                // Deserialize files
                foreach (string file in files)
                {
                    using (FileStream fs = new FileStream(file, FileMode.Open))
                    {
                        BinaryFormatter serializer    = new BinaryFormatter();
                        GW2StringFile   gw2StringFile = (GW2StringFile)serializer.Deserialize(fs);

                        // Add row to data table
                        string key = Path.GetFileNameWithoutExtension(gw2StringFile.Filename) + "_";
                        foreach (GW2Entry entry in gw2StringFile.Items)
                        {
                            DataRow row = stringTable.NewRow();

                            // Timestamp (system format)
                            row["timestamp"] = entry.stamp;

                            // Key = filename_row
                            row["key"] = key + entry.row.ToString("0000");

                            // Make sure to replace UNIX \n with \r\n for copy paste function.
                            row["value"] = entry.value.Replace("\n", Environment.NewLine);

                            stringTable.Rows.Add(row);
                        }

                        gw2StringFile = null;
                    }
                }
            }

            this.BeginInvoke((MethodInvoker) delegate()
            {
                status.Text       = stringTable.Rows.Count.ToString() + " strings found.";
                toolstrip.Enabled = true;

                // Bind datasource to grid
                grid.DataSource = stringTable.DefaultView;

                // Set column style
                grid.Columns[0].Width = DeviceDpi + 20;
                grid.Columns[1].Width = DeviceDpi + 20;
                grid.Columns[2].Width = this.Width - 40 - (DeviceDpi * 3);
                grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
                grid.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

                filterGrid(string.Empty);
                filterButton.Checked = false;
            });
        }