CocosCreator 防內存修改


版本:2.4.3

參考:

如何防止工具(八門神器)進行內存修改

 

修改內存工具,類似以前玩仙劍奇俠傳的修改器金手指之類,查找金幣1000,然后金幣改變到1200,再查找1200。

根據多次查找鎖定金幣的內存位置,然后修改為99999.

 

可以將保存的關鍵數據進行異或后保存,取出來時才經過異或獲取。或者和參考中一樣使用md5等方式將數據改變后再保存,避開內存修改器直接查找數值來進行修改。

 

@ccclass
export default class test_setShader extends cc.Component {

    public num:number = 999;
    public key:number = 0;

    onLoad() {
        this.key = this.num*Math.random();
        this.num = this.num^this.key;
        console.log(this.num);           //隨機值
        console.log(this.num^this.key);  //999
    }
}

  

做成一個工具類EncodeNumber,將需要防內存修改的屬性傳入。

const { ccclass, property } = cc._decorator;

@ccclass
export default class Test extends cc.Component {

    public gold:number = 999;    //金幣

    onLoad() {
        //防內存修改屬性
        EncodeNumber.encode(this, "gold");

        this.node.on(cc.Node.EventType.TOUCH_END,()=>{
            let rand = Math.random()*1000;
            this.gold = rand;
            console.log("隨機數:",rand, "擁有金幣:", this.gold);
        },this);
    }
}

class EncodeNumber{
    private static num = {};        //存放原始值
    private static key = {};        //存放異或的key
    private static key_num = {};    //存放異或后的值
    
    public static encode(thisObj:any, propertyKey:string){
        Object.defineProperty(thisObj, propertyKey, {
            get:()=>{
                if(this.key_num[propertyKey] != null && this.key[propertyKey] != null){
                    console.log("get",this.key[propertyKey], this.key_num[propertyKey]);
                    if((this.num[propertyKey]^this.key[propertyKey]) != this.key_num[propertyKey]){
                        console.log("值被修改");
                        return 0;
                    }else{
                        console.log("值正常");
                        return this.key_num[propertyKey] ^this.key[propertyKey];
                    }
                }else{
                    console.log("沒有初始值");
                    return 0;
                }
            },
            set:(value)=>{
                this.num[propertyKey] = value;
                this.key[propertyKey] = Math.random()*1000;
                this.key_num[propertyKey] = this.key[propertyKey]^value;
                console.log("set",this.key[propertyKey], this.key_num[propertyKey]);
            }
        });
    }
}

  

 

用一個自定義類的方式。 新創建一個自定義number類來進行異或。

const { ccclass, property } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    public gold:EncodeNumber = new EncodeNumber();

    start() {
        console.log(this.gold.getValue());     //0

        this.gold.setValue(99);                
        console.log(this.gold.getValue());     //99

        this.gold.setValue(123);
        console.log(this.gold.getValue());     //123
    }
}


class EncodeNumber{
    private num:number = 0;
    private key:number = 0;

    /**
     * 設置值
     * @param value 
     */
    public setValue(value){
        this.key = value*Math.random();
        this.num = value^this.key;
        console.log("異或值:", this.key, this.num);  //保存的num和key都是隨機值
    }

    /**
     * 獲取值
     * @returns
     */
    public getValue(){
        return this.num^this.key;
    }   
   
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM