2010-09-07 61 views

回答

3

是的,這是如果你知道你在做什麼。字符串以int的大小爲前綴。該int的大小和字節順序是平臺相關的。但爲什麼你必須編輯字節碼?你失去了來源?

+0

存儲的尺寸在哪裏?是否有任何應用程序在編輯後自動更改長度?我沒有提供資料。字節碼可供公衆免費使用,我將做的更改將用於個人使用。 – lesderid 2010-09-08 16:44:39

+1

就像我說過的,字符串以它們的大小爲前綴。嘗試你有的字節碼文件的hexdump。 – lhf 2010-09-08 20:04:54

+0

所以如果你只是改變前綴,它應該工作?沒有錯誤調用這些字符串或什麼(因爲改變的位置)? – lesderid 2010-09-09 15:25:55

1

經過一番跳水throught Lua的源代碼,我發現這樣的解決方案:

#include "lua.h" 
#include "lauxlib.h" 

#include "lopcodes.h" 
#include "lobject.h" 
#include "lundump.h" 

/* Definition from luac.c: */ 
#define toproto(L,i) (clvalue(L->top+(i))->l.p) 

writer_function(lua_State* L, const void* p, size_t size, void* u) 
{ 
    UNUSED(L); 
    return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); 
} 

static void 
lua_bytecode_change_const(lua_State *l, Proto *f_proto, 
        int const_index, const char *new_const) 
{ 
    TValue *tmp_tv = NULL; 
    const TString *tmp_ts = NULL; 

    tmp_ts = luaS_newlstr(l, new_const, strlen(new_const)); 
    tmp_tv = &f_proto->k[INDEXK(const_index)]; 
    setsvalue(l, tmp_tv, tmp_ts); 

    return; 
} 

int main(void) 
{ 
    lua_State *l = NULL; 
    Proto *lua_function_prototype = NULL; 
    FILE *output_file_hnd = NULL; 

    l = lua_open(); 
    luaL_loadfile(l, "some_input_file.lua"); 
    lua_proto = toproto(l, -1); 
    output_file_hnd = fopen("some_output_file.luac", "w"); 

    lua_bytecode_change_const(l, lua_function_prototype, some_const_index, "some_new_const"); 
    lua_lock(l); 
    luaU_dump(l, lua_function_prototype, writer_function, output_file_hnd, 0); 
    lua_unlock(l); 

    return 0; 
} 

首先,我們必須開始Lua的VM,並加載我們要修改劇本。編譯與否,無所謂。然後構建一個Lua函數原型,解析並更改它的常量表。將原型轉儲到文件。

我希望你明白了基本的想法。

相關問題