Esempio n. 1
0
File: Form1.cs Progetto: Tilps/Stash
        private void button4_Click(object sender, EventArgs e)
        {
            #if DEBUG
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "CSV|*.csv";
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>();
                try
                {
                    Dictionary<string, List<int>> ids = new Dictionary<string, List<int>>();
                    List<string> fullPattern = new List<string>();
                    using (CSV csv = new CSV(dialog.FileName))
                    {
                        csv.GetRow();
                        while (true)
                        {
                            string[] row = csv.GetRow();
                            if (row == null)
                                break;
                            string path = row[5];
                            string fileName = Path.GetFileName(path);
                            if (!fileName.StartsWith("client_") || (!fileName.EndsWith(".dat") && !fileName.EndsWith(".datx")))
                                continue;
                            DatFile2 file;
                            if (!files.TryGetValue(path, out file))
                            {
                                try
                                {
                                    file = new DatFile2(path);
                                    files.Add(path, file);
                                    file.CheckJumpTables();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", path, ex.ToString()));
                                    return;
                                }
                                ids[fileName] = new List<int>();
                            }
                            string result = row[7];
                            long offset;
                            long length;
                            bool hitDisk;
                            if (ParseResult(result, out offset, out length, out hitDisk))
                            {
                                if (!hitDisk)
                                {
                                    int id;
                                    if (file.TryMapToId(offset, out id))
                                    {
                                        string check = fileName + ":" + id.ToString();
                                        ids[fileName].Add(id);
                                        fullPattern.Add(check);
                                    }
                                }
                            }
                        }
                    }
                    PatternFinder<string> patterns = new PatternFinder<string>(fullPattern, EqualityComparer<string>.Default);
                    PatternFinder<string>.PatternTree tree = patterns.FindPatterns();
                }
                finally
                {
                    foreach (DatFile2 file in files.Values)
                    {
                        file.Dispose();
                    }
                }
            }
            Viewer view = new Viewer();
            view.Show(this);
            #endif
        }
Esempio n. 2
0
File: Form1.cs Progetto: Tilps/Stash
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            KeyValuePair<string, int> arg = (KeyValuePair<string, int>)e.Argument;
            int option = arg.Value;
            string inputFileName = arg.Key;
            if (option == 1)
            {
                Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>();
                try
                {
                    Dictionary<string, List<int>> ids = new Dictionary<string, List<int>>();
                    Dictionary<string, bool> seen = new Dictionary<string, bool>();
                    using (CSV csv = new CSV(inputFileName))
                    {
                        csv.GetRow();
                        while (true)
                        {
                            string[] row = csv.GetRow();
                            if (row == null)
                                break;
                            string path = row[5];
                            string fileName = Path.GetFileName(path);
                            if (!fileName.StartsWith("client_") || (!fileName.EndsWith(".dat") && !fileName.EndsWith(".datx")))
                                continue;
                            string collapsedFile = Collapse(fileName);
                            DatFile2 file;
                            if (!files.TryGetValue(path, out file))
                            {
                                try
                                {
                                    file = new DatFile2(path);
                                    files.Add(path, file);
                                    file.CheckJumpTables();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", path, ex.ToString()));
                                    return;
                                }
                                ids[collapsedFile] = new List<int>();
                            }
                            string result = row[7];
                            long offset;
                            long length;
                            bool hitDisk;
                            if (ParseResult(result, out offset, out length, out hitDisk))
                            {
                                if (!hitDisk)
                                {
                                    int id;
                                    if (file.TryMapToId(offset, out id))
                                    {
                                        string check = fileName + ":" + id.ToString();
                                        if (!seen.ContainsKey(check))
                                        {
                                            ids[collapsedFile].Add(id);
                                            seen.Add(check, true);
                                        }
                                    }
                                }
                            }
                        }
                        e.Result = ids;
                    }
                }
                catch
                {
                    MessageBox.Show("An unexpected error occured while creating a profile.");
                    return;
                }
                finally
                {
                    foreach (DatFile2 file in files.Values)
                    {
                        file.Dispose();
                    }
                }
            }
            if (option == 2)
            {
                Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>();
                Dictionary<string, DatFile2.DatFileRewriter> writers = new Dictionary<string, DatFile2.DatFileRewriter>();
                bool abort = false;
                try
                {
                    string[] args = inputFileName.Split('|');
                    string basePath = args[0];
                    for (int i = 1; i < args.Length; i++)
                    {
                        string datProfileFile = args[i];
                        using (StreamReader reader = File.OpenText(datProfileFile))
                        {
                            // Skip the description section.
                            string version = reader.ReadLine();
                            if (string.IsNullOrEmpty(version) || version != "1")
                            {
                                MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile));
                                abort = true;
                                return;
                            }
                            string description = reader.ReadLine();
                            if (description == null)
                            {
                                MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile));
                                abort = true;
                                return;
                            }
                            string graphicsLevel = reader.ReadLine();
                            if (graphicsLevel == null)
                            {
                                MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile));
                                abort = true;
                                return;
                            }

                            string currentFile = null;
                            while (true)
                            {
                                string next = reader.ReadLine();
                                if (string.IsNullOrEmpty(next))
                                    break;
                                if (char.IsLetter(next[0]))
                                {
                                    currentFile = next;
                                    continue;
                                }
                                if (currentFile == null)
                                {
                                    MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile));
                                    abort = true;
                                    return;
                                }
                                int value;
                                if (!int.TryParse(next, out value))
                                {
                                    MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile));
                                    abort = true;
                                    return;
                                }
                                int level = 0;
                                while (true)
                                {
                                    string fullPath = Path.Combine(basePath, currentFile);
                                    if (level > 0)
                                    {
                                        fullPath =  Path.Combine(basePath, Path.GetFileNameWithoutExtension(fullPath) + "_aux_" + level + ".datx");
                                    }
                                    if (!File.Exists(fullPath))
                                        break;
                                    DatFile2 file;
                                    if (!files.TryGetValue(fullPath, out file))
                                    {
                                        try
                                        {
                                            file = new DatFile2(fullPath);
                                            files.Add(fullPath, file);
                                            file.CheckJumpTables();
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fullPath, ex.ToString()));
                                            abort = true;
                                            return;
                                        }
                                    }
                                    DatFile2.DatFileRewriter datWriter;
                                    if (!writers.TryGetValue(fullPath, out datWriter))
                                    {
                                        try
                                        {
                                            datWriter = file.GetRewriter(fullPath + ".optimized");
                                            writers.Add(fullPath, datWriter);
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(string.Format("Error creating optimized output file {0}, Exception: {1}", fullPath + ".optimized", ex.ToString()));
                                            abort = true;
                                            return;
                                        }
                                    }
                                    try
                                    {
                                        if (datWriter.ForceById(value))
                                            break;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(string.Format("Error writing to optimized output file {0}, Exception: {1}", datWriter.Filename, ex.ToString()));
                                        abort = true;
                                        return;
                                    }
                                    level++;
                                }
                            }
                            string[] alldats = GetAllDats(basePath);
                            foreach (string fullPath in alldats)
                            {
                                DatFile2 file;
                                if (!files.TryGetValue(fullPath, out file))
                                {
                                    try
                                    {
                                        file = new DatFile2(fullPath);
                                        files.Add(fullPath, file);
                                        file.CheckJumpTables();
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fullPath, ex.ToString()));
                                        abort = true;
                                        return;
                                    }
                                }
                                DatFile2.DatFileRewriter datWriter;
                                if (!writers.TryGetValue(fullPath, out datWriter))
                                {
                                    try
                                    {
                                        datWriter = file.GetRewriter(fullPath + ".optimized");
                                        writers.Add(fullPath, datWriter);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(string.Format("Error creating optimized output file {0}, Exception: {1}", fullPath + ".optimized", ex.ToString()));
                                        abort = true;
                                        return;
                                    }
                                }
                            }
                        }
                    }
                    foreach (DatFile2.DatFileRewriter rewriter in writers.Values)
                    {
                        try
                        {
                            rewriter.Finish();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("Error writing to optimized output file {0}, Exception: {1}", rewriter.Filename, ex.ToString()));
                            abort = true;
                            return;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Unexpected error while optimizing.");
                    abort = true;
                    return;
                }
                finally
                {
                    foreach (DatFile2.DatFileRewriter rewriter in writers.Values)
                    {
                        try
                        {
                            if (abort)
                                rewriter.Cancel();
                            else
                                rewriter.CancelIfNotFinished();
                        }
                        catch
                        {
                        }
                    }
                    foreach (DatFile2 file in files.Values)
                    {
                        file.Dispose();
                    }
                }
            }
            if (option == 3)
            {
                foreach (string fileName in inputFileName.Split('|'))
                {
                    using (DatFile2 file = new DatFile2(fileName))
                    {
                        try
                        {
                            file.CheckJumpTables();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fileName, ex.ToString()));
                            return;
                        }
                        using (DatFile2.DatFileRewriter writer = file.GetRewriter(fileName + ".optimized"))
                        {

                        }
                    }
                }
            }
            if (option == 4)
            {
                foreach (string fileName in inputFileName.Split('|'))
                {
                    File.Copy(fileName, fileName + ".compress");
                    using (DatFile2 file = new DatFile2(fileName + ".compress"))
                    {
                        try
                        {
                            file.CheckJumpTables(false);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fileName, ex.ToString()));
                            return;
                        }
                        file.DebugFileContent();
                    }
                }
            }
        }