Register for your free account! | Forgot your password?

You last visited: Today at 06:05

  • Please register to post and access all features, it's quick, easy and FREE!

 

Read RDB files

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Oct 2014
Posts: 13
Received Thanks: 3
Question Read RDB files

hi

i was wondering how can i import and export rdb files using c#

tried StreamReader doesn't work

thnx in advance



GrimReaper307 is offline  
Old   #2
 
elite*gold: 0
Join Date: Jul 2015
Posts: 222
Received Thanks: 251
Example of reading string.rdb:

String class
Code:
    public class Strings
    {
        static MainWindow MainWindow_Instance = MainWindow.Instance;

        static Stopwatch FileLoadTimer = new Stopwatch();

        static Header FileHeader = null;
        public static byte[] RawBytes = null;
        public static StringResource[] Data = null;
        static int DataType = 0;

        static int lastProgressValue = 0;

        public static void ReadFile(string filePath)
        {
            string fileName = Path.GetFileNameWithoutExtension(filePath).ToLower();

            using (BackgroundWorker loadWorker = new BackgroundWorker())
            {
                loadWorker.DoWork += (o, x) =>
                {
                    FileLoadTimer.Start();

                    byte[] fieldBuffer = new byte[0];
                    int curRow = 0;
                    RawBytes = File.ReadAllBytes(filePath);

                    using (var ms = new MemoryStream(RawBytes))
                    {
                        FileHeader = new Header();

                        fieldBuffer = new byte[27];
                        ms.Read(fieldBuffer, 0, fieldBuffer.Length);
                        FileHeader.HeaderBytes = fieldBuffer;

                        ms.Position += 101;

                        fieldBuffer = new byte[4];
                        ms.Read(fieldBuffer, 0, 4);
                        FileHeader.FileRows = BitConverter.ToInt32(fieldBuffer, 0);

                        Data = new StringResource[FileHeader.FileRows];

                        loadWorker.ReportProgress(0);

                        int nameLen, valueLen;

                        while (true) 
                        {
                            if (curRow == FileHeader.FileRows) { break; }

                            fieldBuffer = new byte[4];
                            ms.Read(fieldBuffer, 0, 4);
                            nameLen = BitConverter.ToInt32(fieldBuffer, 0);

                            fieldBuffer = new byte[4];
                            ms.Read(fieldBuffer, 0, 4);
                            valueLen = BitConverter.ToInt32(fieldBuffer, 0);

                            fieldBuffer = new byte[nameLen];
                            ms.Read(fieldBuffer, 0, fieldBuffer.Length);
                            Data[curRow].name = Encoding.Default.GetString(fieldBuffer);

                            fieldBuffer = new byte[valueLen];
                            ms.Read(fieldBuffer, 0, fieldBuffer.Length);
                            Data[curRow].value = Encoding.Default.GetString(fieldBuffer);

                            fieldBuffer = new byte[4];
                            ms.Read(fieldBuffer, 0, 4);
                            Data[curRow].code = BitConverter.ToInt32(fieldBuffer, 0);

                            fieldBuffer = new byte[4];
                            ms.Read(fieldBuffer, 0, 4);
                            Data[curRow].group_id = BitConverter.ToInt32(fieldBuffer, 0);

                            ms.Position += 16;

                            if (Math.Abs(curRow - lastProgressValue) > 2500) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                            else if ((curRow + 1) == FileHeader.FileRows) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                            curRow++;
                        }
                    }
                };
                loadWorker.WorkerReportsProgress = true;
                loadWorker.ProgressChanged += (o, x) =>
                {
                    if (x.ProgressPercentage == 0) { MainWindow_Instance.Progress.Maximum = FileHeader.FileRows; }
                    else { MainWindow_Instance.Progress.Value = x.ProgressPercentage; }
                };
                loadWorker.RunWorkerCompleted += (o, x) =>
                {
                    FileLoadTimer.Stop();
                    MainWindow_Instance.Status.Text = string.Format("{0} Rows Loaded from {1} in: {2} ms", FileHeader.FileRows, fileName, FileLoadTimer.ElapsedMilliseconds);
                    GenerateTab(fileName);
                };

                MainWindow_Instance.Status.Text = string.Format("Loading: {0}", fileName);
                loadWorker.RunWorkerAsync();
            }
        }

        public static void ReadDB()
        {
            string connectionString = Settings.ConnectionString;

            string selectStatement = "SELECT ";
            int selectStatement_idx = 0;

            PropertyInfo[] properties = typeof(StringResource).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (selectStatement_idx == properties.Length - 1) { selectStatement += string.Format("[{0}] ", property.Name); }
                else { selectStatement += string.Format("[{0}], ", property.Name); }

                selectStatement_idx++;
            }

            selectStatement += "FROM dbo.StringResource";

            int rowCount = 0;
            int curRow = 0;

            using (BackgroundWorker loadWorker = new BackgroundWorker())
            {
                loadWorker.DoWork += (o, x) =>
                {
                    FileLoadTimer = new Stopwatch();
                    FileLoadTimer.Start();

                    SqlConnection sqlCon = new SqlConnection(Settings.ConnectionString);
                    SqlCommand sqlCmd = new SqlCommand("SELECT COUNT(code) FROM dbo.StringResource", sqlCon);
                    SqlDataReader sqlRdr = null;

                    try
                    {
                        sqlCon.Open();

                        rowCount = (int)sqlCmd.ExecuteScalar();
                        Data = new StringResource[rowCount];
                        DataType = 1;
                        MainWindow_Instance.InitProgress(0, rowCount);

                        if (rowCount > 0)
                        {
                            sqlCmd = new SqlCommand(selectStatement, sqlCon);
                            sqlRdr = sqlCmd.ExecuteReader();

                            while (sqlRdr.Read())
                            {
                                Data[curRow].name = Convert.ToString(sqlRdr[0]);
                                Data[curRow].group_id = Convert.ToInt32(sqlRdr[1]);
                                Data[curRow].code = Convert.ToInt32(sqlRdr[2]);
                                Data[curRow].value = Convert.ToString(sqlRdr[3]);

                                curRow++;

                                if (Math.Abs(curRow - lastProgressValue) > 100) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                                else if ((curRow + 1) == rowCount) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                            }
                        }
                    }
                    catch (SqlException sqlEx)
                    {
                        MessageBox.Show(sqlEx.Message, "SQL Exception Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Exception Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        sqlCon.Close();
                    }
                };
                loadWorker.WorkerReportsProgress = true;
                loadWorker.ProgressChanged += (o, x) =>
                {
                    MainWindow_Instance.Progress.Value = x.ProgressPercentage;
                };
                loadWorker.RunWorkerCompleted += (o, x) =>
                {
                    FileLoadTimer.Stop();
                    MainWindow_Instance.Status.Text = string.Format("{0} Rows loaded from {1} in: {2} ms", Data.Length, "db_string", FileLoadTimer.ElapsedMilliseconds);
                    GenerateTab("db_string");
                };
                loadWorker.RunWorkerAsync();
            }
        }
        
        public static void SaveData()
        {
            string rdbName = "db_string";

            string saveDir = Properties.Settings.Default.outputDirectory;
            string saveName = rdbName;
            if (Properties.Settings.Default.appendAscii) { saveName += "(ascii).rdb"; }
            else { saveName += ".rdb"; }
            if (Properties.Settings.Default.saveHashed) { saveName = FileNameCipher.Encode(saveName); }
            string savePath = Path.Combine(saveDir, saveName);

            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }

            try
            {
                using (BackgroundWorker saveWorker = new BackgroundWorker())
                {
                    saveWorker.DoWork += (o, x) =>
                    {
                        FileLoadTimer = new Stopwatch();
                        FileLoadTimer.Start();

                        using (BinaryWriter bw = new BinaryWriter(File.Create(savePath)))
                        {
                            if (DataType != 0)
                            {
                                FileHeader = new Header();
                                FileHeader.HeaderBytes = new byte[128];
                                FileHeader.FileRows = Data.Length;
                            }

                            byte[] HeaderBytes = Encoding.Default.GetBytes("Teal.Sky.Project.2015.rEDIT");

                            bw.Write(HeaderBytes);
                            bw.Write(new byte[128 - HeaderBytes.Length]);
                            bw.Write(FileHeader.FileRows);

                            for (int curRow = 0; curRow < FileHeader.FileRows; curRow++)
                            {
                                bw.Write(Convert.ToInt32(Data[curRow].name.Length));
                                bw.Write(Convert.ToInt32(Data[curRow].value.Length));
                                bw.Write(Encoding.Default.GetBytes(Data[curRow].name));
                                bw.Write(Encoding.Default.GetBytes(Data[curRow].value));
                                bw.Write(Data[curRow].code);
                                bw.Write(Data[curRow].group_id);
                                bw.Write(new byte[16]);

                                if (Math.Abs(curRow - lastProgressValue) > 2500) { saveWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                                else if ((curRow + 1) == FileHeader.FileRows) { saveWorker.ReportProgress(curRow); lastProgressValue = curRow; }
                            }
                        }
                    };
                    saveWorker.WorkerReportsProgress = true;
                    saveWorker.ProgressChanged += (o, x) =>
                    {
                        if (x.ProgressPercentage == 0) { MainWindow_Instance.Progress.Maximum = FileHeader.FileRows; }
                        else { MainWindow_Instance.Progress.Value = x.ProgressPercentage; }

                    };
                    saveWorker.RunWorkerCompleted += (o, x) =>
                    {
                        FileLoadTimer.Stop();
                        MainWindow_Instance.Status.Text = string.Format("{0} Rows saved to {1} in: {2} ms", Data.Length, saveName, FileLoadTimer.ElapsedMilliseconds);
                    };
                    saveWorker.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {

            }
        }

        public static void GenerateTab(string title)
        {
            if (MainWindow_Instance.TabControl.TabPages[title] == null)
            {
                TabPage newPage = new TabPage
                {
                    Name = title,
                    Text = title
                };

                MainWindow_Instance.TabControl.TabPages.Add(newPage);
                MainWindow_Instance.TabControl.TabPages[title].Controls.Add(new DataGridView { Name = string.Format("{0}_dgv", title), Dock = DockStyle.Fill });
                DataGridView tmpDGV = MainWindow_Instance.TabControl.TabPages[title].Controls[string.Format("{0}_dgv", title)] as DataGridView;
                tmpDGV.DataSource = Data;
            }
        }

        public static void ClearAssets()
        {
            FileHeader = null;
            Data = null;
            DataType = 0;
            lastProgressValue = 0;
        }
    }
StringResource structure class
Code:
    public struct StringResource
    {
        public String name { get; set; }
        public Int32 group_id { get; set; }
        public Int32 code { get; set; }
        public String value { get; set; }
    }
p.s. you may have to remove some calls to the gui (MainWindow) and next time use help thread


SilentWisdom is offline  
Reply



« Previous Thread | Next Thread »

Similar Threads
How to read bin/dat files?
hey guys, because the bin/dat converter is offline, does anyone have any idea how I'm able to read florensia bin/dat files? I guess it's binary...
0 Replies - Florensia
read this for who got the vsro files
well watch this : Comet Elenin, Nibiru, Pole Shift, 2011 - YouTube they can enjoy on that money only untill 16 september / 11 octomber , neah...
11 Replies - SRO PServer - Discussions / Questions
[Question] How to read .dll files?
Ohh gosh i really failed... Im sorry ^^ you can close this thread i found a solution to join the file again. Sorry if i made you lost time :$ :/...
2 Replies - S4 League
[READ] Scanning Files
There are more and more threads being posted here everyday of files which could be .exe's or .rar's before you do anything with them or execute them....
1 Replies - Soldier Front
help read this (real TQ files)
heey im furax does somone know what client you need to have for the real TQ files and what patch. if you can help me send an mail or add me...
2 Replies - CO2 PServer - Discussions / Questions



All times are GMT +2. The time now is 06:05.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Abuse
Copyright ©2017 elitepvpers All Rights Reserved.