require "sys" require "filesystem" local uni = require 'ffi.unicode' -- 根据ydwebase.dll的路径计算 fs.__ydwe_path = fs.get(fs.DIR_MODULE):remove_filename():remove_filename() log.debug('ydwe path ' .. fs.__ydwe_path:string()) function fs.ydwe_path() return fs.__ydwe_path end function fs.war3_path() return fs.__war3_path end local root = fs.ydwe_path():parent_path():remove_filename():remove_filename() / "Component" if not fs.exists(root) then root = fs.ydwe_path() end package.path = package.path .. ';' .. uni.u2a((root / 'plugin' / 'w3x2lni' / 'script' / '?.lua'):string()) -- 给路径末尾,扩展名前添加内容 -- 效果:("abc.w3x", "def") -> "abcdef.w3x" function fs.aux_filename(self, str) return self:parent_path() / (self:stem():string() .. str .. self:extension():string()) end io.__open = io.open function io.open(file_path, mode) local f, e = io.__open(__(file_path:string()), mode) if f then if not mode or (not mode:match('b') and mode:match('r')) then if f:read(3) ~= '\xEF\xBB\xBF' then f:seek('set', 0) end end end return f, e end io.__lines = io.lines function io.lines(file_path) return io.__lines(__(file_path:string())) end -- 载入一个文件的内容 -- file_path - 文件路径, 必须是fs.path类型 -- 返回文件内容, nil表示出错 function io.load(file_path) local f, e = io.open(file_path, "rb") if f then local content = f:read("*a") f:close() return content else return nil, e end end -- 保存一个文件的内容 -- file_path - 文件路径, 必须是fs.path类型 -- content - 文件内容,必须是字符串 -- 返回true表示成功,false表示失败 function io.save(file_path, content) local f, e = io.open(file_path, "wb") if f then f:write(content) f:close() return true else return false, e end end -- 比较2个浮点数是否相等 -- a, b - 要比较的2个浮点数 -- eps - 精确度阈值 -- 返回值:true表示2个数的差在精确阈值内(相等),false表明不在阈值内 function math.feq(a, b, eps) if not eps then eps = 1e-10 end return math.abs(a - b) < eps end function string.trim (self) return self:gsub("^%s*(.-)%s*$", "%1") end function string.from_objectid (id) return string.pack('