版本: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]);
}
});
}
}