Skip to content

How to read/write properties? #2

@MestreKarin

Description

@MestreKarin

The first thing I tried was to implement the property API of unity:

File: il2cpp.ts

    private _il2cpp_class_get_properties: NativeFunction;
    private _il2cpp_property_get_get_method: NativeFunction;
    private _il2cpp_property_get_name: NativeFunction;

    ...

    constructor() {
        ...
        this._il2cpp_class_get_properties = new NativeFunction(module.findExportByName("il2cpp_class_get_properties")!, 'pointer', ['pointer', 'pointer']);
        this._il2cpp_property_get_get_method = new NativeFunction(module.findExportByName("il2cpp_property_get_get_method")!, 'pointer', ['pointer']);
        this._il2cpp_property_get_name = new NativeFunction(module.findExportByName("il2cpp_property_get_name")!, 'pointer', ['pointer']);
    }

    ...

    public il2cpp_class_get_properties(clazz: Il2CppClass, iter: NativePointer): PropertyInfo {
        return this._il2cpp_class_get_properties(clazz, iter) as PropertyInfo;
    }

    // const MethodInfo* il2cpp_property_get_get_method(PropertyInfo * prop)
    public il2cpp_property_get_get_method(prop: PropertyInfo): MethodInfo {
        return this._il2cpp_property_get_get_method(prop) as MethodInfo;
    }

    // const char* il2cpp_property_get_name(PropertyInfo * prop)
    public il2cpp_property_get_name(prop: PropertyInfo): string | null {
        return (this._il2cpp_property_get_name(prop) as NativePointer).readCString();
    }

Then, on il2cpp_class.ts I added this method:

    protected get_property_value(fieldName: string): NativePointer {
        const prop = this._properties.get(fieldName);

        if (prop === undefined) {
            throw new Error(`Property ${fieldName} does not exists for class ${this._className}`);
        }

        const getAddr = il2cpp.il2cpp_property_get_get_method(prop);

        return (new NativeFunction(getAddr, 'pointer', []))() as NativePointer;
    }

Also, I added getAllClassProperties in il2cpp_utils class, it is exactly the same as getAllClassFields, but it uses the property methods above.

Then, on my main file:

class PlayerDataModel extends Il2CppClassWrapper {
	private _handle: NativePointer = il2cpp.il2cpp_object_get_class(il2cppUtils.findClass('', 'PlayerDataModel'));

	constructor() {
		super('', 'PlayerDataModel');
	}

	get money(): any {
		return this.get_property_value('money').readDouble();
	}
}

I always get an Access violation. If I use dump_methods, I can see the get_money method, but it returns NaN (I added double in IlTypes, see below): return this.invoke_instance_method(this._handle, 'get_money', 'double'); // = NaN

    protected unbox(...) {
        case "double":
            return il2cpp.il2cpp_object_unbox(obj).readDouble();
    }

This is for reading, for writing I have no idea (maybe using invoke_instance_method with set_money ?!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions