mono 加密/热更

路径 ./mono/metadata/image.c 找到mono_image_open_from_data_with_name 增加相应的代码,然后dll也做相应的加密处理

static FILE* OpenFileWithPath( const char* path )
{
    const char* fileMode ="rb";
    return fopen(path, fileMode );
}

static char* ReadStringFromFile(const char* pathName,int* size)
{
    FILE* file = OpenFileWithPath( pathName);

    if (file == NULL)
    {
        return 0;
    }

    fseek(file, 0, SEEK_END);
    int length = ftell(file);
    fseek(file, 0, SEEK_SET);
    if (length < 0)
    {
       fclose( file );
       return 0;
    }
    *size = length;
    char* outData = g_try_malloc (length);
    int readLength = fread(outData, 1, length, file);
    fclose(file);
   
    if (readLength != length)
    {
       g_free (outData);
       return 0;
    }
    return outData;
}


MonoImage *
mono_image_open_from_data_with_name (char *data, guint32 data_len, gboolean need_copy, MonoImageOpenStatus *status, gboolean refonly, const char *name)
{
    int datasize=0;
    if(strstr(name,"Assembly-CSharp.dll"))
    {
        const char* _pack=strstr(name,"com.");
        const char* _pfie=strstr(name,"-");
        char _name[512];
        memset(_name,0,512);
        int _len0=(int)(_pfie-_pack);
        memcpy(_name,"/sdcard/Android/data/",21);
        memcpy(_name+21,_pack,_len0);
        memcpy(_name+21+_len0,"/files/Assembly-CSharp.dll",26);     
        char* bytes=ReadStringFromFile(_name,&datasize);
        g_message("bytes===%d\n",datasize);
        if(datasize>0)
        {
            data=bytes;
            data_len=datasize;  
        }
        data[0]-=1; 
        g_message("!ctrl+z:fuck you man!");
    }


    MonoCLIImageInfo *iinfo;
    MonoImage *image;
    char *datac;

    if (!data || !data_len) {
        if (status)
            *status = MONO_IMAGE_IMAGE_INVALID;
        return NULL;
    }
    datac = data;
    if (need_copy) {
        datac = g_try_malloc (data_len);
        if (!datac) {
            if (status)
                *status = MONO_IMAGE_ERROR_ERRNO;
            return NULL;
        }
        memcpy (datac, data, data_len);
    }

    image = g_new0 (MonoImage, 1);
    image->raw_data = datac;
    image->raw_data_len = data_len;
    image->raw_data_allocated = need_copy;
    image->name = (name == NULL) ? g_strdup_printf ("data-%p", datac) : g_strdup(name);
    iinfo = g_new0 (MonoCLIImageInfo, 1);
    image->image_info = iinfo;
    image->ref_only = refonly;
    image->ref_count = 1;

    image = do_mono_image_load (image, status, TRUE, TRUE);
    if (image == NULL)
        return NULL;

    return register_image (image);
}