# lua_5.4.2_example **Repository Path**: flame_cyclone/lua_5.4.2_example ## Basic Information - **Project Name**: lua_5.4.2_example - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-06 - **Last Updated**: 2024-11-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 动态库 lua 版本 使用 5.1.5 编译 # 引用方式 ```lua json = require("JSON") fclib = require("FCLIB") lib = nil -- 判断当前运行位数 function is64bit() arc = os.getenv("PROCESSOR_ARCHITECTURE") if arc == "x86" then return false end return true end -- 加载动态库 if is64bit() then lib = require("lua_library_x64") else lib = require("lua_library_x86") end lib.print(lib) --[[ 输出 { "json": { "decode": (function) "encode": (function) } "debug": { "print": (function) } "network": { "post": (function) "download": (function) "get": (function) } } --]] ``` # 接口 ## 常用 ### 睡眠等待 ```lua -- 睡眠等待 -- @param int Milliseconds 等待时长 -- @return void lib.sleep(int Milliseconds) -> void ``` ```lua -- 等待1秒 lib.sleep(1000) ``` ### 打印内容 ```lua -- 打印内容 -- @param ... 任意参数 -- @return 无 lib.print(...) -> void ``` ```lua lib.print(1,2,3,4,5,6,7,8,9,0) --[[ 输出: 1,2,3,4,5,6,7,8,9,0 lib.print({1,2,3,4,5,6,7,8,9,0}) --[[ 输出: { 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 0 } --]] lib.print({['name']='FlameCyclone',['age']=30}) --[[ 输出: { "name": "FlameCyclone" "age": 30 } --]] ``` ## JSON ### 将JSON字符串解码为表 ```lua -- 将JSON字符串解码为表 -- @param string json JSON 字符串 -- @return table 解码后的表 lib.json.decode(string json) -> table ``` ```lua -- 示例 data = '{"name":"FlameCyclone","age":30}' res = lib.json.decode('{"name":"FlameCyclone","age":30}') -- 此时 res = {['name']='FlameCyclone',['age']=30} ``` ### 将表编码为JSON字符串 ```lua -- 将表编码为JSON字符串 -- @param table tab 表 -- @return table 编码后的 JSON 字符串 lib.json.encode(table tab) -> string ``` ```lua data = {['name']='FlameCyclone',['age']=30} res = lib.json.encode(tab) -- 此时 res = "{"name":"FlameCyclone","age":30}" ``` ## 网络 ### 发送 Get 请求 ```lua -- 发送 Get 请求 -- @param string url 链接 -- @param table param 参数 -- @param table header 请求头 -- @param function(table) 进度回调函数 -- @return table 请求结果 lib.network.get(string url, table param, table header, function(table)->void cb) -> table ``` ```lua -- 请求链接 url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=4412054' -- 参数 param = { } -- 请求头 header = { ['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0', ['Referer']= 'https://www.amd.com/' } -- 进度回调 function network_callback(info) info = string.format('%.2f%% %.14g/%.14g', info['Progress'] * 100, info['Cur'], progress['Total']) print(info) end -- 发送请求 res = lib.network.get(url, param, header, network_callback) --[[ 输出: { "result": "{"code":0,"data":{"admin":[],"room":[]},"message":"","msg":""}" "code": 200 } --]] ``` ### 发送 Post 请求 ```lua -- 发送 Post 请求 -- @param string url 链接 -- @param table param 参数 -- @param table header 请求头 -- @param function(table) 进度回调函数 -- @return table 请求结果 lib.network.post(string url, table param, table header, function(table)->void cb) -> table ``` ```lua -- 请求链接 url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=4412054' -- 参数 param = { } -- 请求头 header = { ['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0', ['Referer']= 'https://www.amd.com/' } -- 进度回调 function network_callback(info) info = string.format('%.2f%% %.14g/%.14g', info['Progress'] * 100, info['Cur'], progress['Total']) print(info) end -- 发送请求 res = lib.network.post(url, param, header, network_callback) ``` ### 下载文件 ```lua -- 下载文件 -- @param string url 链接 -- @param string path 文件保存路径 -- @param table header 请求头 -- @param function(table) 进度回调函数 -- @return table 请求结果 lib.network.download(string url, string path, table header, function(table)->void cb) -> table ``` ```lua -- 下载链接 download_url = "https://drivers.amd.com/drivers/whql-amd-software-adrenalin-edition-24.8.1-win10-win11-aug-rdna.exe" -- 存放路径 file_path = 'whql-amd-software-adrenalin-edition-24.8.1-win10-win11-aug-rdna.exe' -- 请求头 header = { ['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0', ['Referer']= 'https://www.amd.com/' } -- 进度回调 function network_callback(info) info = string.format('%.2f%% %.14g/%.14g', info['Progress'] * 100, info['Cur'], progress['Total']) print(info) end -- 下载文件 res = lib.network.download(download_url, file_path, header, network_callback) ``` ## 共享数据 ### 创建共享数据 ```lua -- 创建共享数据 -- @param string name 共享数据名 -- @param int size 数据大小(字节) -- @return bool 操作结果 lib.share.create(string name, int size) -> bool ``` ### 读取共享数据 ```lua -- 读取共享数据 -- @param string name 共享数据名 -- @return string 读取到的内容 lib.share.read(string name) -> string ``` ### 写入共享数据 ```lua -- 写入共享数据 -- @param string name 共享数据名 -- @param string data 写入数据内容 -- @return string 读取到的内容 lib.share.write(string name, string data) -> bool ``` ### ## 路径 ### 获取文件路径列表 ```lua -- 获取文件路径列表 -- @param string dir 文件夹 -- @param int depth 子路径深度 -- @param int exclude 排除项属性 -- @param int contain 包含项属性 -- @return table 文件路径列表 lib.path.get_path_list(string dir, int depth, int exclude, int contain) -> table 属性项 1: 只读 2: 隐藏 4: 系统 16: 文件夹 32 : 文件 ``` ```lua -- 获取文件夹列表 info = lib.path.get_path_list('D:\\gitee\\lua_example', 260, 0, 16) -- 获取文件夹列表, 不含隐藏项 info = lib.path.get_path_list('D:\\gitee\\lua_example', 260, 2, 16) -- 获取文件列表, 不含文件夹 info = lib.path.get_path_list('D:\\gitee\\lua_example', 260, 16, 0) -- 获取文件列表, 不含隐藏项 info = lib.path.get_path_list('D:\\gitee\\lua_example', 260, 2, 0) ``` ### 获取文件路径信息 ```lua -- 获取文件信息 -- @param string path 文件路径 -- @return table 文件信息表 lib.path.get_path_info(string path) -> table ``` ```lua -- 获取路径属性 info = lib.path.get_path_info('E:\\gitee\\lua_example\\Bin\\x64\\lua_demo.exe') lib.print(info) --[[ { "attr": 32 "driver": "E" "name": "lua_demo" "dir": "E:\gitee\lua_example\Bin\x64" "ext": "exe" "size": 111616 } --]] ``` ## 位操作 ### 位与 ```lua -- 位与 -- @param 位与的整数 -- @return int 位运算结果 lib.bitop.And(...) -> int ``` ```lua val = lib.bitop.Or(1,2,4,8,16,32,64,128,256) lib.print(val) -- 打印: 511 ``` ### 位或 ```lua -- 位或 -- @param 位或的整数 -- @return int 位运算结果 lib.bitop.Or(...) -> int ``` ```lua val = lib.bitop.And(255, 128) lib.print(val) -- 打印: 128 ``` ### 位异或 ```lua -- 位异或 -- @param 位异或的整数 -- @return int 位运算结果 lib.bitop.Xor(...) -> int ``` ```lua val = lib.bitop.Xor(1024, 512) lib.print(val) -- 打印: 1536 ``` ### 位开关组合 ```lua -- 返回一个设置指定位的整数 -- @param 要设置的位 -- @return int 位开关组合结果 lib.bitop.Bit(...) -> int ``` ```lua val = lib.bitop.Bit(0,1,2,3) lib.print(val) -- 打印: 15 ```