• Hello
Search Results for

    Show / Hide Table of Contents

    Protected DataChain & BlockChain (Pro)

    A data chain is an ordered list that notices if someone rewrites an entry or changes the order. That is useful for:

    • Digital assets: Store in-game assets such as weapons, skins, or cosmetics so that players have real ownership rights and secure trading opportunities.
    • Achievements: Ensure the immutability and transparency of achievements, increasing trust and credibility within the community.
    • Virtual currency: Manage game currencies to ensure transparency and protect against cheating or manipulation.

    Datachain

    A data chain is a line of linked items. You can add to the end, or remove from the end. Each add or remove checks the chain. If the check fails, PrimitiveCheatingDetector is told.

    That check costs time. Do not use a data chain for a huge list.

    /// <summary>
    /// Represents a generic data chain that implements the <see cref="IDataChain{T}"/> interface and is observed by the primitive cheating detector. 
    /// Using this class, you can ensure that a chain of data is not modified without your knowledge. Each time you append or remove an item from the
    /// chain, the class verifies its integrity and notifies the primitive cheating detector if a change is detected. This can be performance costly,
    /// so do not use it for large amounts of data.
    /// Only primitive types are supported.
    /// </summary>
    /// <typeparam name="T">The type of elements stored in the data chain, must be a nullable value type.</typeparam>
    public class DataChain<T> : IDataChain<T>, IWatchedSubject where T: struct
    

    How to use

    DataChain is in GUPS.AntiCheat.Protected.Collection.Chain. A small example:

    // Create a new data chain.
    DataChain<Int32> datachain = new DataChain<Int32>();
    
    // Append data and get directly informed about any failed integrity.
    if(datachain.Append(1))
    {
       // Still has its integrity.
       // ... TODO: Put your code here.
    }
    else
    {
       // Has lost its integrity, probably caused by memory manipulation.
       // ... TODO: Put your code here.
    }
    
    // Or verify the integrity manually.
    if(!datachain.CheckIntegrity())
    {
       // Has lost its integrity, probably caused by memory manipulation.
       // ... TODO: Put your code here.
    }
    

    Items must be value types (structs), not classes, unless you allow classes yourself. A class could be changed from outside the chain.

    BlockChain

    A blockchain is a data chain made of blocks. Each block holds the real data. A data chain stays in memory. A blockchain can also sync with a file or a server, and still notice a rewrite.

    Two built-in sync classes share one interface. You can write your own.

    • FileSynchronizer: Syncs the chain with a file on disk.
    • WebSynchronizer: Syncs the chain with a web server. The server needs a read address and a write address.

    Before a sync, the last block is checked. If it was changed, you and PrimitiveCheatingDetector are told.

    /// <summary>
    /// Represents an implementation of the <see cref="IDataChain{Block{T}}"/> interface, storing linked blocks to form a blockchain. The 
    /// blockchain can be synchronized with a remote source to retrieve and upload data while maintaining its integrity. Valid use cases
    /// could be to store game data, such as scores, achievements, or player progress and synchronize it with a remote server. This 
    /// implementation is not designed to store large amounts of data, but rather to store small amounts of data that require integrity 
    /// and synchronization.
    /// </summary>
    /// <typeparam name="T">The value type of the content stored in the block transactions.</typeparam>
    public class BlockChain<T> : IDataChain<Block<T>>, IWatchedSubject
    

    How to use

    BlockChain is in GUPS.AntiCheat.Protected.Collection.Chain. A small example:

    // Create a temporary file location.
    String filePath = System.IO.Path.GetTempFileName();
    
    // Create a file synchronizer using the temporary file.
    FileSynchronizer<Int32> fileSynchronizer = new FileSynchronizer<Int32>(filePath);
    
    // Create a blockchain with a block size of 10 (amount of possible transactions per block) using the file synchronizer.
    BlockChain<Int32> blockChain = new BlockChain<Int32>(10, fileSynchronizer);
    
    // Append example data asynchronously; if data already exists at the file location, it will be synchronized automatically.
    await blockChain.AppendAsync(1);
    await blockChain.AppendAsync(2);
    await blockChain.AppendAsync(3);
    await blockChain.AppendAsync(4);
    
    // If you wish to manually synchronize data with the remote source, you can do so as follows.
    await blockChain.SynchronizeAsync();
    
    // Validate the integrity of the last block in the blockchain manually.
    if(!blockChain.CheckIntegrityOfLastBlock())
    {
       // Has lost its integrity, probably caused by memory manipulation.
       // ... TODO: Put your code here.
    }
    
    // Validate the integrity of the whole blockchain manually.
    if(!blockChain.CheckIntegrity())
    {
       // Has lost its integrity, probably caused by memory manipulation.
       // ... TODO: Put your code here.
    }
    

    Items must be value types (structs), not classes, unless you allow classes yourself.

    In This Article
    Back to top GuardingPearSoftware documentation