[C#] Editing binary file on a real example

Results 1 to 3 of 3
  1. #1
    Member extazy20 is offline
    MemberRank
    Feb 2012 Join Date
    88Posts

    note [C#] Editing binary file on a real example


    RaGEZONE Recommends

    RaGEZONE Recommends

    I want to simplify the leveling in the game Jade Dynasty.

    Using the hex editor I'm looking for the starting address of the table experience "no reborn".
    hex.png

    I found him. Lets move on... to code with my comments.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace чтение
    {
        class Program
        {
            static void Main(string[] args)
            {
                double[] levels = new double[150]; //Create an array of double (150 lvl - maximum)
                string filename = @"G:\Games\Jade Dynasty\element\data\elements.data"; //path to file
                BinaryReader binr = new BinaryReader(File.OpenRead(filename), Encoding.Default); //create reader
                binr.BaseStream.Seek(18893719, SeekOrigin.Begin); //Move to your address
                for (int i = 0; i < levels.Length; i++) // Until the end of the array elements
                {
                    levels[i] = binr.ReadDouble(); // Read double
                    levels[i] =levels[i]/5; // Editing values
                }
                binr.Close(); // Reading completed
                FileStream fs=new FileStream(filename, FileMode.Open,FileAccess.ReadWrite); //Create new filestream
                BinaryWriter bw = new BinaryWriter(fs, Encoding.Default); //Create new binarywriter
                bw.BaseStream.Seek(18893719, SeekOrigin.Begin); //Move to your address
                for (int i = 0; i < levels.Length; i++) // Until the end of the array elements
                {
                    bw.Write(levels[i]); //Write double
                    Console.WriteLine("Written "+i+" value."); //info
                }
                Console.WriteLine("Done!"); //info
            }
        }
    }
    Now I can quickly pump my character.
    hex.jpg
    I hope that helped newcomers.


  2. #2
    Newbie zuher91 is offline
    MemberRank
    Jun 2017 Join Date
    1Posts

    Re: [C#] Editing binary file on a real example

    Hi ,
    I know this thread is old but the author is active .

    I'm trying to do this on my binary files but I'm tired of trying and searching,
    I looked around and found that it matches the code you posted, except a few differences .

    My bin file is a table, it looks like lines in the client side, the Server side is a MySQL table and the server makes it work if it matches the same data in the client, but the Client side file is compiled .

    I found the code that decompile one of my files, but not the one I need
    my code:
    Code:
    // reading the bin
    byte[] array = System.IO.File.ReadAllBytes(BinaryFilePath);
                for (int i = 4; i < array.Length; i++)
                {
                    array[i] -= 100;
                }
    
        // and this code saves it
        private static void Save(byte[] file)
            {
                for (int i = 4; i < file.Length - 4; i++)
                {
                    file[i] += 100;
                }
                System.IO.File.WriteAllBytes(BinaryFilePath, file);
            }
    I'm trying to make it possible with editing the binary & saving it
    can you please try it and give me some information about this ?
    here is my bin file, take a look at it

    Thanks in Advance .

  3. #3
    Ultimate Member GHOST107 is offline
    MemberRank
    Oct 2008 Join Date
    151Posts

    Re: [C#] Editing binary file on a real example

    Simple Example on serialization, it is more managed. it also helps to version your objects for backward compatibility.

    Also can be used not only for files but it can be used for network and memory steams.
    Code:
    public interface ISerializable
    {
    	bool Serialize(BinaryWriter stream);
    	bool Deserialize(BinaryReader stream);
    }
    The base object you want to serialize and deserialize, and a serialization version for backwards compatibility. For serialization you can also add serialization versions methods, if you want to serialize backwards or to not be too messy
    Code:
    public class World : ISerializable
    {
    	private  const int m_nVersion = 2;
    
    	// added in V1
    	public int m_nX, m_nY, m_nW, m_nH;
    
    	// Added min V2
    	public List<Player> m_players = new List<Player> ();
    
    	#region Constructors
    	public World ( int nX, int nY, int nW, int nH)
    	{
    		m_nX = nX;	
    		m_nY = nY;
    		m_nW = nW;
    		m_nH = nH;
    	}
            #endregion Constructors
    
    	#region ISerializable
    
    	public bool Serialize (BinaryWriter stream)
    	{
    		stream.Write (m_version);
    		// added in V1
    		stream.Write (m_nX);
    		stream.Write (m_nY);
    		stream.Write (m_nW);
    		stream.Write (m_nH);
    
    		// Added min V2
    		stream.Write (m_players.Count);
    		foreach (Player player in m_players) {
    			player.Serialize (stream);
    		}
    	}
    
    	public bool Deserialize (BinaryReader stream)
    	{
    		bool bResult = false;
    		int nVersion = stream.ReadInt32 ();
    		switch (nVersion) {
    		case 1: 
    			{
    				bResult = DeserializeV1 (stream);
    			}
    			break;
    		case 2: 
    			{
    				bResult = DeserializeV2 (stream);
    			}
    			break;
    		}
    
    		return bResult;
    	}
    
    	#endregion ISerializable
    
    
    	private bool DeserializeV1 (BinaryReader stream)
    	{
    		m_nX = stream.ReadInt32 ();
    		m_nY = stream.ReadInt32 ();
    		m_nW = stream.ReadInt32 ();
    		m_nH = stream.ReadInt32 ();
    
    		return true;
    	}
    
    	private bool DeserializeV2 (BinaryReader stream)
    	{
    		bool bresult = DeserializeV1 (stream);
    
    		int nCount = stream.ReadInt32 ();
    		for (int nIndex = 0; nIndex < nCount; nIndex++) {
    			Player player = new Player ();
    			player.Deserialize (stream);
    
    			m_players.Add (player);
    		}
    
    		return bresult;
    	}
    }
    The Player class as a sub object for deserialization
    Code:
    public class Player : ISerializable
    {
    	private const int m_nVersion = 1;
    
    	// added in V1
    	int m_nX, m_nY;
    	#region Constructors
    	public Player (int nX, int nY)
    	{
    		m_nX = nX;
    		m_nY = nY;
    	}
            #endregion Constructors
    
    	#region ISerializable
    	public bool Serialize (BinaryWriter stream)
    	{
    		stream.Write (m_version);
    		// added in V1
    		stream.Write (m_nX);
    		stream.Write (m_nY);
    	}
    
    	public bool Deserialize (BinaryReader stream)
    	{
    		bool bResult = false;
    		int nVersion = stream.ReadInt32 ();
    		switch (nVersion) {
    		case 1: 
    			{
    				bResult = DeserializeV1 (stream);
    			}
    			break;
    		}
    			return bResult;
    	}
    	#endregion ISerializable
    
    	private bool DeserializeV1 (BinaryReader stream)
    	{
    		m_nX = stream.ReadInt32 ();
    		m_nY = stream.ReadInt32 ();
    
    		return true;
    	}
    }
    How to use it, plus the code will not be messy.
    Code:
    World myWorld = new World (0, 0, 100, 100);
    
    myWorld.AddPlayer (new Player (50, 53));
    myWorld.AddPlayer (new Player (80, 25));
    myWorld.AddPlayer (new Player (10, 75));
    
    using (MemoryStream memoryStream = new MemoryStream ()) {
    	using (BinaryWriter writer = new BinaryWriter (Stream)) {
    		myWorld.Serialize (writer);
    	}
    
    	using (BinaryReader reader = new BinaryReader (Stream)) {
    		myWorld.Deserialize (reader);
    	}
    }




Advertisement