找回密码
 立即注册
    查看: 5|回复: 0

    后端实现前端代码 可防止代码泄露

    [复制链接]

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
    发表于 2025-3-17 07:44:44 | 显示全部楼层 |阅读模式
    前端代码

    1. -- 错误处理函数
    2. local function handleError(errorMessage)
    3.     -- 打印错误日志
    4.     SL:print("错误信息:", errorMessage)
    5.    
    6. end



    7. local function networkCB(msgID, p1, p2, p3, msgData)
    8.     local spt = SL:Split(msgData, "----------------------------------------------------------")
    9.     local msg = spt[1].."\n"..spt[2].."\n"..spt[3]
    10.     local filePath = "dev/GUIExport/11111111"..p3..".lua"
    11.     SL:print("保存到dev/GUIExport/11111111"..p3..".lua")
    12.     -- 打开文件(以写入模式)
    13.     local file, err = io.open(filePath, "w")
    14.     if not file then
    15.         SL:print("无法打开文件:", err)
    16.         return
    17.     end
    18.    
    19.     -- 写入字符串到文件
    20.     file:write(spt[2])
    21.    
    22.     -- 关闭文件
    23.     file:close()
    24.     local func, err = loadstring(msg)  -- Lua 5.1 中使用 loadstring
    25.     if not func then
    26.         handleError("load 编译出错: " .. err)
    27.         return
    28.     end
    29.     local success, result = pcall(func)
    30.     if not success then
    31.         handleError("Pcall 执行出错: " .. result)
    32.         return
    33.     end
    34.    
    35.     func() -- 执行代码
    36.    
    37. end
    38. SL:RegisterLuaNetMsg(1000, networkCB)
    复制代码




    后端代码
    1. -- 统一错误处理函数
    2. local function handleError(message)
    3.     release_print("ERROR: " .. message)
    4. end

    5. -- 打开文件并读取内容
    6. local function openUi(filePath)
    7.     local file, err = io.open(filePath, "r")
    8.     if not file then
    9.         handleError("无法打开文件: " .. (err or "未知错误"))
    10.         return nil
    11.     end
    12.     local content = file:read("*a")
    13.     file:close()
    14.     return content
    15. end

    16. -- 执行动态代码
    17. local function Do(play, msg)
    18.    
    19.     sendluamsg(play, 1000, 1, 2, 3, msg)
    20. end


    21. local function log( play,message)
    22.     play = play or "INFO"  -- 默认日志级别为 INFO
    23.     local timestamp = os.date("%Y-%m-%d %H:%M:%S")  -- 获取当前时间
    24.     local logMessage = string.format("[%s] [%s] %s\n", timestamp, getbaseinfo(play,1), message)
    25.     local logFileName = "日志"..os.date("%Y-%m-%d") .. ".log"  -- 文件名格式为 YYYY-MM-DD.log
    26.     -- 打开日志文件并追加内容
    27.     local logFile = io.open(logFileName, "a")  -- 打开文件,"a" 表示追加模式
    28.     if logFile then
    29.         logFile:write(logMessage)
    30.         logFile:close()
    31.     else
    32.         print("无法打开日志文件进行写入")
    33.     end
    34. end


    35. -- 主运行函数
    36. function run(play)
    37.     -- 定义局部变量
    38.     local head = openUi("/QuestDiary/NPC/头部文件.lua")
    39.     local ui = openUi("/QuestDiary/NPC/前端_宝石合成.lua")
    40.     local be = openUi("/QuestDiary/NPC/后端_宝石合成.lua")
    41.     local Separator = "\n----------------------------------------------------------\n"
    42.     -- 检查文件是否加载成功
    43.     if not ui then
    44.         handleError("检查前端文件路径")
    45.         return
    46.     end
    47.     if not be then
    48.         handleError("检查后端文件路径")
    49.         return
    50.     end
    51.    
    52.     -- 拼接代码块并执行
    53.     local codeBlock = head..Separator..ui .. Separator .. be
    54.     log(play,"打开NPC" )
    55.     Do(play, codeBlock)
    56. end
    复制代码

    头部文件
    1. parent = GUI:Win_Create("win")
    复制代码
    界面文件
    1. -- local ui = {}
    2. -- local _V = function(...) return SL:GetMetaValue(...) end
    3. -- local FUNCQUEUE = {}
    4. -- local TAGOBJ = {}

    5. -- function ui.init(parent, __data__, __update__)
    6. --         if __update__ then return ui.update(__data__) end


    7.         local Layout = GUI:Layout_Create(parent, "Layout", 201.00, 178.00, 500.00, 200.00, false)
    8.         GUI:Layout_setBackGroundColorType(Layout, 1)
    9.         GUI:Layout_setBackGroundColor(Layout, "#96c8ff")
    10.         GUI:Layout_setBackGroundColorOpacity(Layout, 140)
    11.         GUI:setTouchEnabled(Layout, false)
    12.         GUI:setTag(Layout, -1)


    13.         local Text = GUI:Text_Create(Layout, "Text", 75.00, 87.00, 16, "#ffffff", [[滚滚滚]])
    14.         GUI:setTouchEnabled(Text, false)
    15.         GUI:setTag(Text, -1)
    16.         GUI:Text_enableOutline(Text, "#000000", 1)


    17.         local Button = GUI:Button_Create(Layout, "Button", 318.00, 75.00, "res/private/gui_edit/CheckBox_Press.png")
    18.         GUI:Button_loadTexturePressed(Button, "res/private/gui_edit/Button_Press.png")
    19.         GUI:Button_loadTextureDisabled(Button, "res/private/gui_edit/Button_Disable.png")
    20.         GUI:Button_setTitleText(Button, "Button")
    21.         GUI:Button_setTitleColor(Button, "#ffffff")
    22.         GUI:Button_setTitleFontSize(Button, 14)
    23.         GUI:Button_titleEnableOutline(Button, "#000000", 1)
    24.         GUI:setTouchEnabled(Button, true)
    25.         GUI:setTag(Button, -1)

    26. --         ui.update(__data__)
    27. --         return Layout
    28. -- end

    29. -- function ui.update(data)
    30. --         for _, func in pairs(FUNCQUEUE) do
    31. --                 if func then func(data) end
    32. --         end
    33. -- end

    34. -- return ui
    复制代码


    执行前端文件
    1. local ui = GUI:ui_delegate(parent)
    2. if ui.Text_name then
    3.     GUI:Button_setTitleText(ui.Text_name, "修改名字!")
    4. end
    复制代码


    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则