代码之家  ›  专栏  ›  技术社区  ›  Rule

删除混淆名称的最有效方法是什么(Minecraft)?

  •  0
  • Rule  · 技术社区  · 12 年前

    我为《我的世界》制作了一个mod,它允许Lua进行mod。一切都很好,但通常情况下,方法的名称在MC中是模糊的。让正常的函数名称重定向到模糊的名称的最佳方法是什么?

    例如,用户为右键单击时打印hello world的块制作脚本:

    function onActivated(world, x, y, z, player) player:addChatMessage("hello world") end

    addChatMessage应调用Java方法EntityPlayer.func_71035_c(字符串文本)

    1 回复  |  直到 12 年前
        1
  •  1
  •   Egor Skriptunoff    12 年前
    -- translation file (translation.txt)
    func_70912_b,setTameSkin,2,
    func_70913_u,getTameSkin,2,
    func_70915_j,getShadingWhileShaking,2,Used when calculating the amount of shading to apply while the wolf is shaking.
    func_70916_h,setAngry,2,Sets whether this wolf is angry.
    
    
    -- obfuscated program (script.lua)
    x:func_70913_u(y, z)
    x:func_70915_j(y, z)
    
    
    -- your preprocessor (preprocessor.lua)
    local transl = {}
    for line in io.lines'translation.txt' do
       local obf, orig = line:match'^(.-),(.-),'
       transl[obf] = orig
    end
    local script = assert(io.open('script.lua','rb')):read'*a'
    local output = assert(io.open('script2.lua','wb'))
    output:write((script:gsub('[%w_]+',transl)))
    output:close()
    
    
    -- preprocessor output (script2.lua)
    x:getTameSkin(y, z)
    x:getShadingWhileShaking(y, z)
    

    编辑:

    local obfuscations = {}
    for line in io.lines'translation.txt' do
       local obf, orig = line:match'^(.-),(.-),'
       obfuscations[orig] = obf
    end
    
    local function get_obf_key_value(t, k, __index)
       local value = __index and __index(t, k)
       if value == nil and obfuscations[k] then
          value = t[obfuscations[k]]
       end
       return value
    end
    
    local cache = {get_obf_key_value = true}
    
    local function __index_constructor(__index)
       if not __index then
          return get_obf_key_value
       end
       local old__index = cache[__index]
       if old__index then
          return old__index == true and __index or old__index
       else
          local function new__index(t, k)
             return get_obf_key_value(t, k, __index)
          end
          cache[__index] = new__index
          cache[new__index] = true
          return new__index
       end
    end
    
    local obf_mt = {__index = get_obf_key_value}
    
    local function correct_metatable(object)
       local mt = getmetatable(object)
       if mt == nil then
          setmetatable(object, obf_mt)
       else
          local __index = mt.__index
          if __index == nil or type(__index) == 'function' then
              mt.__index = __index_constructor(__index)
          else
             correct_metatable(__index)
          end
       end
    end
    
    -- you should call correct_metatable(class_or_object_of_that_class)
    -- at least once for every class
    correct_metatable(wolf)
    correct_metatable(goat)
    correct_metatable(cabbage)
    ...