mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 02:19:44 +00:00
Merge branch 'dev' into lights-fixes
This commit is contained in:
commit
3fc5a4322d
65 changed files with 1549 additions and 715 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"id": "base",
|
||||
"title": "Base",
|
||||
"version": "0.32",
|
||||
"version": "0.33",
|
||||
"description": "basic content package"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,12 +39,15 @@ local function bezier_interpolation(k0, k1, t)
|
|||
return bezier(k0.value, k0.ry + k0.value, k1.ly + k1.value, k1.value, u)
|
||||
end
|
||||
|
||||
local this = {
|
||||
local M = {
|
||||
CH_TRANSLATE = 1,
|
||||
CH_ROTATE = 2,
|
||||
CH_SCALE = 3,
|
||||
CH_ZOOM = 4,
|
||||
CH_TEXTURE = 5,
|
||||
CH_SHOW = 6,
|
||||
CH_MODEL = 7,
|
||||
CH_COLOR = 8,
|
||||
|
||||
INT_CONST = 1,
|
||||
INT_LINEAR = 2,
|
||||
|
|
@ -53,7 +56,7 @@ local this = {
|
|||
MAX_DURATION = 1e9,
|
||||
MAX_FRAMES = 1e9,
|
||||
|
||||
TRACE_CODEGEN = false,
|
||||
TRACE_CODEGEN = true,
|
||||
|
||||
maths = {
|
||||
bezier_interpolation = bezier_interpolation
|
||||
|
|
@ -67,14 +70,14 @@ function internals.store_animation(name, track)
|
|||
loaded_tracks[name] = track
|
||||
end
|
||||
|
||||
function this.get_track(identifier)
|
||||
function M.get_track(identifier)
|
||||
return loaded_tracks[identifier]
|
||||
end
|
||||
|
||||
local running_actions = {}
|
||||
local playing_tracks = {}
|
||||
|
||||
function this.action(func)
|
||||
function M.action(func)
|
||||
table.insert(running_actions, coroutine.create(func))
|
||||
end
|
||||
|
||||
|
|
@ -98,7 +101,7 @@ local PlayingTrack = {
|
|||
}
|
||||
}
|
||||
|
||||
function this.play(name, target)
|
||||
function M.play(name, target)
|
||||
if target and target.reset_pose then
|
||||
target:reset_pose()
|
||||
end
|
||||
|
|
@ -156,4 +159,4 @@ function internals.restore_animation_backup()
|
|||
loaded_tracks, backup_tracks = backup_tracks, {}
|
||||
end
|
||||
|
||||
return this
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ local exclude_patters = {
|
|||
}
|
||||
|
||||
-- TODO: replace with actual expression -> lua translator
|
||||
local function process_expression(src, memoised, mode)
|
||||
local function process_expression(src, mode)
|
||||
if mode == 'mul' then
|
||||
src = string.format("(%s) * intensity + (1.0 - intensity)", src)
|
||||
elseif mode == "add" then
|
||||
|
|
@ -30,7 +30,6 @@ local function process_expression(src, memoised, mode)
|
|||
for i, pattern in ipairs(patterns) do
|
||||
local pattern_safe = string.pattern_safe(pattern.pattern)
|
||||
if src:find(pattern_safe) then
|
||||
memoised[pattern.name] = pattern.pattern
|
||||
src = src:gsub(pattern_safe, pattern.name)
|
||||
end
|
||||
end
|
||||
|
|
@ -76,7 +75,7 @@ local env = {
|
|||
return keys[left].value
|
||||
end
|
||||
left = keys[left]
|
||||
if interp == INT_CONST then
|
||||
if not interp or interp == INT_CONST then
|
||||
return left.value
|
||||
end
|
||||
right = keys[right]
|
||||
|
|
@ -137,42 +136,48 @@ local is_multiplier = {
|
|||
[animation.CH_ZOOM] = true,
|
||||
}
|
||||
|
||||
local function codegen_track(raw_track, lineset, memoised, keysets, use_tsf)
|
||||
local function codegen_line(raw_track, lineset, line, index, keysets)
|
||||
if line.expression then
|
||||
return "(" .. process_expression(line.expression,
|
||||
is_multiplier[line.channel] and "mul" or "add") .. ")"
|
||||
elseif line.keys then
|
||||
local target_keysets = keysets[lineset.target_name]
|
||||
if not target_keysets then
|
||||
target_keysets = {}
|
||||
keysets[lineset.target_name] = target_keysets
|
||||
end
|
||||
target_keysets[index] = line.keys
|
||||
|
||||
if line.curve_func then
|
||||
local valueat = string.format("curves[%s]", string.escape(line.curve_func))
|
||||
return string.format("value_at_custom(keysets['%s'][%d], t * %s %% %s, %s)",
|
||||
lineset.target_name, index, raw_track.fps, line.period, valueat)
|
||||
elseif line.channel == animation.CH_TEXTURE then
|
||||
return string.format("string_at(keysets['%s'][%d], t * %s %% %s)",
|
||||
lineset.target_name, index, raw_track.fps, line.period)
|
||||
else
|
||||
return string.format("value_at(keysets['%s'][%d], t * %s %% %s, %s)",
|
||||
lineset.target_name, index, raw_track.fps, line.period, line.interp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function codegen_track(raw_track, lineset, keysets, use_tsf, target_type)
|
||||
if lineset.target_type ~= target_type then
|
||||
return ""
|
||||
end
|
||||
|
||||
local lines = lineset.lines
|
||||
local code = ""
|
||||
local has_tsf = false
|
||||
local translation = {false, false, false}
|
||||
local rotation = {false, false, false}
|
||||
local scale = {false, false, false}
|
||||
local color = {false, false, false, false}
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
if line.expression then
|
||||
code = code .. "\n local l" .. i .. " = (" ..
|
||||
process_expression(line.expression, memoised,
|
||||
is_multiplier[line.channel] and "mul" or "add") .. ")"
|
||||
elseif line.keys then
|
||||
local target_keysets = keysets[lineset.target_name]
|
||||
if not target_keysets then
|
||||
target_keysets = {}
|
||||
keysets[lineset.target_name] = target_keysets
|
||||
end
|
||||
target_keysets[i] = line.keys
|
||||
|
||||
if line.curve_func then
|
||||
local valueat = string.format("curves[%s]", string.escape(line.curve_func))
|
||||
|
||||
code = code .. string.format(
|
||||
"\n local l%d = value_at_custom(keysets['%s'][%d], t * %s %% %s, %s)",
|
||||
i, lineset.target_name, i, raw_track.fps, line.period, valueat)
|
||||
elseif line.channel == animation.CH_TEXTURE then
|
||||
code = code .. string.format(
|
||||
"\n local l%d = string_at(keysets['%s'][%d], t * %s %% %s)",
|
||||
i, lineset.target_name, i, raw_track.fps, line.period)
|
||||
else
|
||||
code = code .. string.format(
|
||||
"\n local l%d = value_at(keysets['%s'][%d], t * %s %% %s, %s)",
|
||||
i, lineset.target_name, i, raw_track.fps, line.period, line.interp)
|
||||
end
|
||||
end
|
||||
code = code .. string.format("\n local l%d = ", i)
|
||||
.. codegen_line(raw_track, lineset, line, i, keysets)
|
||||
|
||||
if line.channel == animation.CH_TRANSLATE then
|
||||
translation[line.axis] = i
|
||||
|
|
@ -186,7 +191,31 @@ local function codegen_track(raw_track, lineset, memoised, keysets, use_tsf)
|
|||
elseif line.channel == animation.CH_ZOOM then
|
||||
code = code .. "\n zoom = l" .. i
|
||||
elseif line.channel == animation.CH_TEXTURE then
|
||||
code = code .. string.format("\n target:set_texture(%s, %s)", lineset.target_name:escape(), "l"..i)
|
||||
code = code .. string.format("\n target:set_texture(%s, l%d)",
|
||||
lineset.target_name:escape(), i)
|
||||
elseif line.channel == animation.CH_MODEL then
|
||||
code = code .. string.format("\n target:set_model(bone_index, l%d)", i)
|
||||
elseif line.channel == animation.CH_SHOW then
|
||||
if target_type == "bone" then
|
||||
code = code .. string.format("\n target:set_visible(bone_index, l%d)", i)
|
||||
else
|
||||
code = code .. string.format("\n target:set_visible(l%d)", i)
|
||||
end
|
||||
elseif line.channel == animation.CH_COLOR then
|
||||
color[line.axis] = i
|
||||
end
|
||||
end
|
||||
|
||||
if color[1] or color[2] or color[3] or color[4] then
|
||||
local color_vector = "{" ..
|
||||
(color[1] and ("l" .. color[1]) or '1').. ", " ..
|
||||
(color[2] and ("l" .. color[2]) or '1').. ", " ..
|
||||
(color[3] and ("l" .. color[3]) or '1').. ", " ..
|
||||
(color[4] and ("l" .. color[4]) or '1').. "}"
|
||||
if target_type == "bone" then
|
||||
code = code .. "\n target:set_color(" .. color_vector .. ", bone_index)"
|
||||
else
|
||||
code = code .. "\n target:set_color(" .. color_vector .. ")"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -229,14 +258,15 @@ local function codegen_rig_target(raw_track, context)
|
|||
if lineset.target_type ~= "bone" and lineset.target_type ~= "texture" then
|
||||
goto continue
|
||||
end
|
||||
local lineset_code = codegen_track(
|
||||
raw_track, lineset, context.memoised, context.keysets, true)
|
||||
local lineset_code = codegen_track(raw_track, lineset, context.keysets, true, "bone")
|
||||
|
||||
code = code .. string.format("\n local bone_index = target:index(%s)", string.escape(bone))
|
||||
if context.has_tsf then
|
||||
code = code .. "\n local dst = target:get_matrix(bone_index)"
|
||||
end
|
||||
code = code
|
||||
.. string.format("\n local bone_index = target:index(%s)"
|
||||
.. "\n local dst = target:get_matrix(bone_index)", string.escape(bone))
|
||||
.. "\n do" .. lineset_code .. "\n end\n"
|
||||
if lineset.target_type == "bone" then
|
||||
if lineset.target_type == "bone" and context.has_tsf then
|
||||
code = code ..
|
||||
" target:set_matrix(bone_index, dst)\n"
|
||||
end
|
||||
|
|
@ -246,17 +276,18 @@ local function codegen_rig_target(raw_track, context)
|
|||
end
|
||||
|
||||
local function codegen_object_target(raw_track, context)
|
||||
local code = "\n if target.set_pos then\n"
|
||||
code = code .. " local dst = mat4.idt()\n"
|
||||
local code = " local dst = mat4.idt()\n"
|
||||
local lineset = raw_track.linesets[""]
|
||||
if not lineset then
|
||||
return ""
|
||||
end
|
||||
local lineset_code = codegen_track(
|
||||
raw_track, lineset, context.memoised, context.keysets, true)
|
||||
local lineset_code = codegen_track(raw_track, lineset, context.keysets, true)
|
||||
code = code .. "\n do" .. lineset_code .. "\n end\n"
|
||||
.. " set_matrix(target, dst)\n"
|
||||
return code .. " end"
|
||||
|
||||
if context.has_tsf then
|
||||
code = code .. " set_matrix(target, dst)\n"
|
||||
end
|
||||
return code
|
||||
end
|
||||
|
||||
local function codegen_camera_target(raw_track, context)
|
||||
|
|
@ -266,25 +297,37 @@ local function codegen_camera_target(raw_track, context)
|
|||
if not lineset then
|
||||
return ""
|
||||
end
|
||||
local lineset_code = codegen_track(
|
||||
raw_track, lineset, context.memoised, context.keysets, false)
|
||||
local lineset_code = codegen_track(raw_track, lineset, context.keysets, false, "camera")
|
||||
code = code .. "\n do" .. lineset_code .. "\n end\n"
|
||||
.. " target:set_zoom(zoom)\n"
|
||||
return code .. " end"
|
||||
end
|
||||
|
||||
|
||||
local function has_transform_directive(raw_track)
|
||||
for _, lineset in pairs(raw_track.linesets) do
|
||||
for _, line in ipairs(lineset.lines) do
|
||||
if line.channel == animation.CH_TRANSLATE or
|
||||
line.channel == animation.CH_ROTATE or
|
||||
line.channel == animation.CH_SCALE then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function internals.compile_animation_track(raw_track, track_name)
|
||||
local code = ""
|
||||
local context = {
|
||||
memoised = {},
|
||||
keysets = {},
|
||||
curves = {},
|
||||
has_tsf = has_transform_directive(raw_track),
|
||||
}
|
||||
for name, curve in pairs(raw_track.curves) do
|
||||
context.curves[name] = load(string.format(
|
||||
"return function(kl, kr, t) return %s end",
|
||||
process_expression(curve.func, context.memoised, "curve")
|
||||
process_expression(curve.func, "curve")
|
||||
), "<curve>", "t", env)()
|
||||
end
|
||||
|
||||
|
|
@ -292,17 +335,8 @@ function internals.compile_animation_track(raw_track, track_name)
|
|||
code = code .. codegen_object_target(raw_track, context)
|
||||
code = code .. codegen_camera_target(raw_track, context)
|
||||
|
||||
local memoised_code = ""
|
||||
for name, expression in pairs(context.memoised) do
|
||||
memoised_code = memoised_code .. "\n local " .. name .. " = "
|
||||
.. expression
|
||||
end
|
||||
|
||||
if #memoised_code > 0 then
|
||||
code = memoised_code .. "\n" .. code
|
||||
end
|
||||
|
||||
local src = "return function(target, t, intensity, m)\n m = m or 1\n intensity = intensity or 1.0\n"
|
||||
local src = "return function(target, t, intensity, m)\n"
|
||||
.. " m = m or 1\n intensity = intensity or 1.0\n"
|
||||
.. code .. "\nend"
|
||||
|
||||
if animation.TRACE_CODEGEN then
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ local action_to_channel = {
|
|||
scale = animation.CH_SCALE,
|
||||
zoom = animation.CH_ZOOM,
|
||||
texture = animation.CH_TEXTURE,
|
||||
show = animation.CH_SHOW,
|
||||
model = animation.CH_MODEL,
|
||||
color = animation.CH_COLOR,
|
||||
}
|
||||
|
||||
local curve_to_interp = {
|
||||
|
|
@ -16,14 +19,18 @@ local curve_to_interp = {
|
|||
bezier = animation.INT_BEZIER,
|
||||
}
|
||||
|
||||
local boolean_actions = {
|
||||
show = true
|
||||
}
|
||||
|
||||
local function parse_configure(raw_track, node)
|
||||
if node.fps then
|
||||
raw_track.fps = node.fps
|
||||
raw_track.fps = tonumber(node.fps)
|
||||
end
|
||||
if node.frames then
|
||||
raw_track.duration = node.frames / (node.fps or DEFAULT_FPS)
|
||||
raw_track.duration = tonumber(node.frames) / (tonumber(raw_track.fps) or DEFAULT_FPS)
|
||||
elseif node.duration then
|
||||
raw_track.duration = node.duration
|
||||
raw_track.duration = tonumber(node.duration)
|
||||
end
|
||||
raw_track.rotation_order = string.upper(node["rotation-order"] or "XYZ")
|
||||
end
|
||||
|
|
@ -62,6 +69,70 @@ local function parse_simple_frames(line, node)
|
|||
end
|
||||
end
|
||||
|
||||
local function parse_boolean_frames(line, node)
|
||||
line.keys = {}
|
||||
for j, key_node in ipairs(node) do
|
||||
local keyframe = {
|
||||
frame = tonumber(key_node.frame),
|
||||
value = key_node.value == "on",
|
||||
}
|
||||
table.insert(line.keys, keyframe)
|
||||
end
|
||||
end
|
||||
|
||||
local function parse_directive(node, raw_track)
|
||||
local linesets = raw_track.linesets
|
||||
local tag = node['#']
|
||||
if tag == "configure" then
|
||||
parse_configure(raw_track, node)
|
||||
return
|
||||
elseif tag == "curve" then
|
||||
raw_track.curves[node.name] = node
|
||||
return
|
||||
end
|
||||
|
||||
local target_type = nil
|
||||
if node.bone then
|
||||
target_type = "bone"
|
||||
elseif node.zoom then
|
||||
target_type = "camera"
|
||||
elseif tag == "texture" then
|
||||
target_type = "texture"
|
||||
end
|
||||
local target_name = node.bone or node.name or ""
|
||||
local lineset = linesets[target_name]
|
||||
if not lineset then
|
||||
lineset = {
|
||||
lines = {},
|
||||
target_type = target_type,
|
||||
target_name = target_name,
|
||||
flag = node.flag,
|
||||
}
|
||||
linesets[target_name] = lineset
|
||||
end
|
||||
|
||||
local channel = action_to_channel[tag]
|
||||
if not channel then
|
||||
error("unknown directive " .. tag:escape())
|
||||
end
|
||||
local line = {
|
||||
axis = node.by and (("xyz"):find(node.by) or ("rgba"):find(node.by)) or "",
|
||||
channel = channel,
|
||||
period = node.period or animation.MAX_FRAMES
|
||||
}
|
||||
if node.func then
|
||||
line.expression = node.func
|
||||
elseif node.curve then
|
||||
parse_curve(line, node)
|
||||
elseif boolean_actions[tag] then
|
||||
parse_boolean_frames(line, node)
|
||||
else
|
||||
parse_simple_frames(line, node)
|
||||
end
|
||||
|
||||
table.insert(lineset.lines, line)
|
||||
end
|
||||
|
||||
local function parse_track(root)
|
||||
local raw_track = {
|
||||
duration = math.huge,
|
||||
|
|
@ -69,56 +140,10 @@ local function parse_track(root)
|
|||
linesets = {},
|
||||
curves = {},
|
||||
}
|
||||
local linesets = raw_track.linesets
|
||||
for i, node in ipairs(root) do
|
||||
if type(node) == "string" then
|
||||
goto continue
|
||||
if type(node) == 'table' then
|
||||
parse_directive(node, raw_track)
|
||||
end
|
||||
local tag = node['#']
|
||||
if tag == "configure" then
|
||||
parse_configure(raw_track, node)
|
||||
goto continue
|
||||
elseif tag == "curve" then
|
||||
raw_track.curves[node.name] = node
|
||||
goto continue
|
||||
end
|
||||
|
||||
local target_type = nil
|
||||
if node.bone then
|
||||
target_type = "bone"
|
||||
elseif tag == "texture" then
|
||||
target_type = "texture"
|
||||
end
|
||||
local target_name = node.bone or node.name or ""
|
||||
local lineset = linesets[target_name]
|
||||
if not lineset then
|
||||
lineset = {
|
||||
lines = {},
|
||||
target_type = target_type,
|
||||
target_name = target_name
|
||||
}
|
||||
linesets[target_name] = lineset
|
||||
end
|
||||
|
||||
local channel = action_to_channel[tag]
|
||||
if not channel then
|
||||
error("unknown directive " .. tag:escape())
|
||||
end
|
||||
local line = {
|
||||
axis = node.by and ("xyz"):find(node.by) or "",
|
||||
channel = channel,
|
||||
period = node.period or animation.MAX_FRAMES
|
||||
}
|
||||
if node.func then
|
||||
line.expression = node.func
|
||||
elseif node.curve then
|
||||
parse_curve(line, node)
|
||||
else
|
||||
parse_simple_frames(line, node)
|
||||
end
|
||||
|
||||
table.insert(lineset.lines, line)
|
||||
::continue::
|
||||
end
|
||||
return raw_track
|
||||
end
|
||||
|
|
|
|||
72
res/modules/internal/stream/binary.lua
Normal file
72
res/modules/internal/stream/binary.lua
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
local binary = {}
|
||||
binary.__index = binary
|
||||
|
||||
local DEFAULT_CHUNK = 8192
|
||||
|
||||
function binary.new(stream)
|
||||
return setmetatable({ stream = stream }, binary)
|
||||
end
|
||||
|
||||
local function to_table(bytes)
|
||||
local t = {}
|
||||
for i = 1, #bytes do
|
||||
t[i] = bytes[i]
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function binary.read_all(stream)
|
||||
local result = Bytearray()
|
||||
local avail = stream:available()
|
||||
local chunk = avail > 0 and avail or DEFAULT_CHUNK
|
||||
|
||||
while true do
|
||||
local part = stream:read_some(chunk)
|
||||
if #part == 0 then break end
|
||||
result:append(part)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function binary:read(arg, as_table)
|
||||
local t = type(arg)
|
||||
|
||||
if t == "number" then
|
||||
local bytes = self.stream:read(arg)
|
||||
return as_table and to_table(bytes) or bytes
|
||||
elseif t == "string" then
|
||||
local size = byteutil.get_size(arg)
|
||||
local bytes = self.stream:read(size)
|
||||
if #bytes < size then
|
||||
error(string.format(
|
||||
"unexpected end of stream: format %q needs %d bytes, got %d",
|
||||
arg, size, #bytes), 2)
|
||||
end
|
||||
return byteutil.unpack(arg, bytes)
|
||||
elseif t == "nil" then
|
||||
error("in binary mode the first argument must be a byteutil format string"
|
||||
.. " or the number of bytes to read")
|
||||
else
|
||||
error("unknown argument type: " .. t)
|
||||
end
|
||||
end
|
||||
|
||||
function binary:write(arg, ...)
|
||||
local t = type(arg)
|
||||
|
||||
if t == "string" then
|
||||
self.stream:write(byteutil.pack(arg, ...))
|
||||
elseif t == "table" then
|
||||
self.stream:write(Bytearray(arg))
|
||||
else
|
||||
self.stream:write(arg)
|
||||
end
|
||||
end
|
||||
|
||||
function binary:read_fully(as_table)
|
||||
local bytes = binary.read_all(self.stream)
|
||||
return as_table and to_table(bytes) or bytes
|
||||
end
|
||||
|
||||
return binary
|
||||
19
res/modules/internal/stream/blocking.lua
Normal file
19
res/modules/internal/stream/blocking.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
local wrapper = require "internal/stream/wrapper"
|
||||
|
||||
local blocking = wrapper.extend({})
|
||||
|
||||
function blocking.new(inner)
|
||||
return setmetatable({ inner = inner }, blocking)
|
||||
end
|
||||
|
||||
function blocking:read(n)
|
||||
local result = self.inner:read(n)
|
||||
while #result < n do
|
||||
coroutine.yield()
|
||||
if not self.inner:is_alive() then break end
|
||||
result:append(self.inner:read(n - #result))
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
return blocking
|
||||
90
res/modules/internal/stream/buffered.lua
Normal file
90
res/modules/internal/stream/buffered.lua
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
local wrapper = require "internal/stream/wrapper"
|
||||
|
||||
local buffered = wrapper.extend({})
|
||||
|
||||
buffered.DEFAULT_SIZE = 8192
|
||||
|
||||
function buffered.new(inner, size, flush_inner)
|
||||
return setmetatable({
|
||||
inner = inner,
|
||||
size = size or buffered.DEFAULT_SIZE,
|
||||
flush_inner = flush_inner ~= false,
|
||||
rbuf = Bytearray(),
|
||||
wbuf = Bytearray(),
|
||||
}, buffered)
|
||||
end
|
||||
|
||||
function buffered:_take(n)
|
||||
local buf = self.rbuf
|
||||
if n >= #buf then
|
||||
self.rbuf = Bytearray()
|
||||
return buf
|
||||
end
|
||||
local part = buf:slice(1, n)
|
||||
buf:remove(1, n)
|
||||
return part
|
||||
end
|
||||
|
||||
function buffered:flush_buffer()
|
||||
if #self.wbuf > 0 then
|
||||
local data = self.wbuf
|
||||
self.wbuf = Bytearray()
|
||||
self.inner:write(data)
|
||||
end
|
||||
end
|
||||
|
||||
function buffered:read(n)
|
||||
local have = #self.rbuf
|
||||
if have < n then
|
||||
if have == 0 and n >= self.size then
|
||||
return self.inner:read(n)
|
||||
end
|
||||
self.rbuf:append(self.inner:read(self.size - have))
|
||||
end
|
||||
return self:_take(n)
|
||||
end
|
||||
|
||||
buffered.read_some = buffered.read
|
||||
|
||||
function buffered:write(data)
|
||||
if #self.wbuf + #data > self.size then
|
||||
self:flush_buffer()
|
||||
end
|
||||
if #data >= self.size then
|
||||
self.inner:write(data)
|
||||
else
|
||||
self.wbuf:append(data)
|
||||
end
|
||||
end
|
||||
|
||||
function buffered:flush()
|
||||
self:flush_buffer()
|
||||
if self.flush_inner then
|
||||
self.inner:flush()
|
||||
end
|
||||
end
|
||||
|
||||
function buffered:available()
|
||||
return self.inner:available() + #self.rbuf
|
||||
end
|
||||
|
||||
function buffered:seek(mode, offset)
|
||||
self:flush_buffer()
|
||||
if mode == "c" then
|
||||
offset = offset - #self.rbuf
|
||||
end
|
||||
self.rbuf = Bytearray()
|
||||
self.inner:seek(mode, offset)
|
||||
end
|
||||
|
||||
function buffered:tell()
|
||||
return self.inner:tell() - #self.rbuf + #self.wbuf
|
||||
end
|
||||
|
||||
function buffered:close()
|
||||
self:flush_buffer()
|
||||
self.rbuf = Bytearray()
|
||||
return self.inner:close()
|
||||
end
|
||||
|
||||
return buffered
|
||||
49
res/modules/internal/stream/raw.lua
Normal file
49
res/modules/internal/stream/raw.lua
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
local raw = {}
|
||||
raw.__index = raw
|
||||
|
||||
function raw.new(descriptor, io_lib)
|
||||
return setmetatable({ descriptor = descriptor, io_lib = io_lib }, raw)
|
||||
end
|
||||
|
||||
function raw:read(n)
|
||||
return self.io_lib.read(self.descriptor, n)
|
||||
end
|
||||
|
||||
raw.read_some = raw.read
|
||||
|
||||
function raw:write(data)
|
||||
self.io_lib.write(self.descriptor, data)
|
||||
end
|
||||
|
||||
function raw:flush()
|
||||
if self.io_lib.flush then
|
||||
self.io_lib.flush(self.descriptor)
|
||||
elseif not self:is_alive() then
|
||||
error("stream is closed")
|
||||
end
|
||||
end
|
||||
|
||||
function raw:available()
|
||||
local f = self.io_lib.available
|
||||
return f and f(self.descriptor) or 0
|
||||
end
|
||||
|
||||
function raw:seek(mode, offset)
|
||||
if not self.io_lib.seek then error("cannot seek this stream") end
|
||||
self.io_lib.seek(self.descriptor, mode, offset)
|
||||
end
|
||||
|
||||
function raw:tell()
|
||||
if not self.io_lib.tell then error("cannot tell this stream") end
|
||||
return self.io_lib.tell(self.descriptor)
|
||||
end
|
||||
|
||||
function raw:is_alive()
|
||||
return self.io_lib.is_alive(self.descriptor)
|
||||
end
|
||||
|
||||
function raw:close()
|
||||
return self.io_lib.close(self.descriptor)
|
||||
end
|
||||
|
||||
return raw
|
||||
102
res/modules/internal/stream/text.lua
Normal file
102
res/modules/internal/stream/text.lua
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
local binary = require "internal/stream/binary"
|
||||
|
||||
local text = {}
|
||||
text.__index = text
|
||||
|
||||
local CR = string.byte('\r')
|
||||
local LF = string.byte('\n')
|
||||
|
||||
function text.new(stream)
|
||||
return setmetatable({stream = stream}, text)
|
||||
end
|
||||
|
||||
function text:_read_byte()
|
||||
return self.stream:read(1)[1]
|
||||
end
|
||||
|
||||
function text:read_line()
|
||||
local line = Bytearray()
|
||||
local got_any = false
|
||||
|
||||
while true do
|
||||
local b = self:_read_byte()
|
||||
if b == nil then break end
|
||||
got_any = true
|
||||
|
||||
if b == LF then
|
||||
break
|
||||
elseif b == CR then
|
||||
local nb = self:_read_byte()
|
||||
if nb == LF then break end
|
||||
line:append(CR)
|
||||
if nb == nil then break end
|
||||
line:append(nb)
|
||||
else
|
||||
line:append(b)
|
||||
end
|
||||
end
|
||||
|
||||
if not got_any then return nil end
|
||||
return utf8.tostring(line)
|
||||
end
|
||||
|
||||
local function trim_empty(lines)
|
||||
while #lines > 0 and lines[#lines] == "" do
|
||||
lines[#lines] = nil
|
||||
end
|
||||
while #lines > 0 and lines[1] == "" do
|
||||
table.remove(lines, 1)
|
||||
end
|
||||
end
|
||||
|
||||
function text:read_lines(count, trim)
|
||||
if count < 0 then error("count of lines to read must be positive") end
|
||||
|
||||
local lines = {}
|
||||
for i = 1, count do
|
||||
local line = self:read_line()
|
||||
if line == nil then break end
|
||||
lines[i] = line
|
||||
end
|
||||
|
||||
if trim ~= false then trim_empty(lines) end
|
||||
return lines
|
||||
end
|
||||
|
||||
function text:read(count, trim)
|
||||
if count == nil then
|
||||
return self:read_line()
|
||||
end
|
||||
return self:read_lines(count, trim)
|
||||
end
|
||||
|
||||
function text:read_fully(as_lines)
|
||||
if as_lines then
|
||||
local lines = {}
|
||||
for line in self.read_line, self do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
return lines
|
||||
end
|
||||
return utf8.tostring(binary.read_all(self.stream))
|
||||
end
|
||||
|
||||
function text:write_line(str)
|
||||
self.stream:write(utf8.tobytes(str .. "\n"))
|
||||
end
|
||||
|
||||
-- write(string) | write({ lines... })
|
||||
function text:write(arg)
|
||||
local t = type(arg)
|
||||
if t == "string" then
|
||||
self:write_line(arg)
|
||||
elseif t == "table" then
|
||||
for i = 1, #arg do
|
||||
self:write_line(arg[i])
|
||||
end
|
||||
else
|
||||
error("unknown argument type: " .. t)
|
||||
end
|
||||
end
|
||||
|
||||
return text
|
||||
26
res/modules/internal/stream/wrapper.lua
Normal file
26
res/modules/internal/stream/wrapper.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
local wrapper = {}
|
||||
wrapper.__index = wrapper
|
||||
|
||||
function wrapper.extend(cls)
|
||||
cls.__index = cls
|
||||
return setmetatable(cls, { __index = wrapper })
|
||||
end
|
||||
function wrapper:read(n) return self.inner:read(n) end
|
||||
|
||||
function wrapper:read_some(n) return self.inner:read_some(n) end
|
||||
|
||||
function wrapper:write(data) return self.inner:write(data) end
|
||||
|
||||
function wrapper:flush() return self.inner:flush() end
|
||||
|
||||
function wrapper:available() return self.inner:available() end
|
||||
|
||||
function wrapper:seek(mode, off) return self.inner:seek(mode, off) end
|
||||
|
||||
function wrapper:tell() return self.inner:tell() end
|
||||
|
||||
function wrapper:is_alive() return self.inner:is_alive() end
|
||||
|
||||
function wrapper:close() return self.inner:close() end
|
||||
|
||||
return wrapper
|
||||
|
|
@ -1,444 +1,108 @@
|
|||
local io_stream = { }
|
||||
local raw = require "internal/stream/raw"
|
||||
local buffered = require "internal/stream/buffered"
|
||||
local blocking = require "internal/stream/blocking"
|
||||
local binary = require "internal/stream/binary"
|
||||
local text = require "internal/stream/text"
|
||||
|
||||
local io_stream = {}
|
||||
io_stream.__index = io_stream
|
||||
|
||||
local MAX_BUFFER_SIZE = 8192
|
||||
|
||||
local DEFAULT_MODE = "default"
|
||||
local BUFFERED_MODE = "buffered"
|
||||
local YIELD_MODE = "yield"
|
||||
|
||||
local ALL_MODES = {
|
||||
DEFAULT_MODE,
|
||||
BUFFERED_MODE,
|
||||
YIELD_MODE
|
||||
}
|
||||
|
||||
local FLUSH_MODE_ALL = "all"
|
||||
local FLUSH_MODE_ONLY_BUFFER = "buffer"
|
||||
|
||||
local ALL_FLUSH_MODES = {
|
||||
FLUSH_MODE_ALL,
|
||||
FLUSH_MODE_ONLY_BUFFER
|
||||
}
|
||||
|
||||
local CR = string.byte('\r')
|
||||
local LF = string.byte('\n')
|
||||
|
||||
local function read_fully(result, read_func)
|
||||
local isTable = type(result) == "table"
|
||||
|
||||
local buf
|
||||
|
||||
repeat
|
||||
buf = read_func(MAX_BUFFER_SIZE)
|
||||
|
||||
if isTable then
|
||||
for i = 1, #buf do
|
||||
result[#result + 1] = buf[i]
|
||||
end
|
||||
else result:append(buf) end
|
||||
until #buf == 0
|
||||
end
|
||||
|
||||
--[[
|
||||
descriptor - descriptor of stream for provided I/O library
|
||||
binaryMode - if enabled, most methods will expect bytes instead of strings
|
||||
ioLib - I/O library. Should include the following functions:
|
||||
read(descriptor: int, length: int) -> Bytearray
|
||||
May return bytearray with a smaller size if bytes have not arrived yet or have run out
|
||||
May throw error if descriptor isn't readable
|
||||
write(descriptor: int, data: Bytearray)
|
||||
May throw error if descriptor isn't writeable
|
||||
[optional] seek(descriptor: int, mode: string, offset: int)
|
||||
Mode may be 'b' (relative begin), 'c' (relative current), 'e' (relative end + 1)
|
||||
[optional] tell(descriptor: int) -> int
|
||||
[optional] flush(descriptor: int)
|
||||
[optional] available(descriptor: int) -> int
|
||||
is_alive(descriptor: int) -> bool
|
||||
close(descriptor: int)
|
||||
--]]
|
||||
local MODES = { default = true, buffered = true, yield = true }
|
||||
local FLUSH_MODES = { all = true, buffer = true }
|
||||
|
||||
function io_stream.new(descriptor, binary_mode, io_lib, mode, flush_mode)
|
||||
mode = mode or DEFAULT_MODE
|
||||
flush_mode = flush_mode or FLUSH_MODE_ALL
|
||||
|
||||
local self = setmetatable({}, io_stream)
|
||||
|
||||
self.descriptor = descriptor
|
||||
self.binary_mode = binary_mode
|
||||
self.max_buffer_size = MAX_BUFFER_SIZE
|
||||
self.io_lib = io_lib
|
||||
|
||||
self:set_mode(mode)
|
||||
self:set_flush_mode(flush_mode)
|
||||
local self = setmetatable({
|
||||
descriptor = descriptor,
|
||||
io_lib = io_lib,
|
||||
binary_mode = binary_mode and true or false,
|
||||
max_buffer_size = buffered.DEFAULT_SIZE,
|
||||
}, io_stream)
|
||||
|
||||
self:set_flush_mode(flush_mode or "all")
|
||||
self:set_mode(mode or "default")
|
||||
return self
|
||||
end
|
||||
|
||||
function io_stream:is_binary_mode()
|
||||
return self.binary_mode
|
||||
function io_stream:_rebuild()
|
||||
local old = self.stream
|
||||
if old and old.flush_buffer and old:is_alive() then
|
||||
old:flush_buffer()
|
||||
end
|
||||
|
||||
local s = raw.new(self.descriptor, self.io_lib)
|
||||
if self.mode == "buffered" then
|
||||
s = buffered.new(s, self.max_buffer_size, self.flush_mode == "all")
|
||||
elseif self.mode == "yield" then
|
||||
s = blocking.new(s)
|
||||
end
|
||||
|
||||
self.stream = s
|
||||
self.text = text.new(s)
|
||||
self.binary = binary.new(s)
|
||||
end
|
||||
|
||||
function io_stream:set_binary_mode(binary_mode)
|
||||
self.binary_mode = binary_mode ~= nil
|
||||
function io_stream:_format()
|
||||
return self.binary_mode and self.binary or self.text
|
||||
end
|
||||
|
||||
function io_stream:get_mode()
|
||||
return self.mode
|
||||
end
|
||||
function io_stream:is_binary_mode() return self.binary_mode end
|
||||
function io_stream:set_binary_mode(v) self.binary_mode = v and true or false end
|
||||
|
||||
function io_stream:get_mode() return self.mode end
|
||||
|
||||
function io_stream:set_mode(mode)
|
||||
if not table.has(ALL_MODES, mode) then
|
||||
error("invalid stream mode: "..mode)
|
||||
end
|
||||
|
||||
if self.write_buffer then
|
||||
self.write_buffer:clear()
|
||||
self.write_buffer = nil
|
||||
end
|
||||
|
||||
if self.read_buffer then
|
||||
self.read_buffer:clear()
|
||||
self.read_buffer = nil
|
||||
end
|
||||
|
||||
if mode == BUFFERED_MODE then
|
||||
self.write_buffer = Bytearray(self.max_buffer_size)
|
||||
self.read_buffer = Bytearray(self.max_buffer_size)
|
||||
end
|
||||
|
||||
if not MODES[mode] then error("invalid stream mode: " .. tostring(mode)) end
|
||||
self.mode = mode
|
||||
self:_rebuild()
|
||||
end
|
||||
|
||||
function io_stream:get_flush_mode()
|
||||
return self.flush_mode
|
||||
end
|
||||
function io_stream:get_flush_mode() return self.flush_mode end
|
||||
|
||||
function io_stream:set_flush_mode(flush_mode)
|
||||
if not table.has(ALL_FLUSH_MODES, flush_mode) then
|
||||
error("invalid flush mode: " .. flush_mode)
|
||||
if not FLUSH_MODES[flush_mode] then
|
||||
error("invalid flush mode: " .. tostring(flush_mode))
|
||||
end
|
||||
|
||||
self.flush_mode = flush_mode
|
||||
if self.mode == "buffered" then
|
||||
self.stream.flush_inner = flush_mode == "all"
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:get_max_buffer_size()
|
||||
return self.max_buffer_size
|
||||
function io_stream:get_max_buffer_size() return self.max_buffer_size end
|
||||
|
||||
function io_stream:set_max_buffer_size(size)
|
||||
self.max_buffer_size = size
|
||||
if self.mode == "buffered" then self:_rebuild() end
|
||||
end
|
||||
|
||||
function io_stream:set_max_buffer_size(max_buffer_size)
|
||||
self.max_buffer_size = max_buffer_size
|
||||
function io_stream:read(arg, use_table) return self:_format():read(arg, use_table) end
|
||||
|
||||
self.write_buffer = Bytearray(self.max_buffer_size)
|
||||
self.read_buffer = Bytearray(self.max_buffer_size)
|
||||
end
|
||||
function io_stream:write(arg, ...) return self:_format():write(arg, ...) end
|
||||
|
||||
function io_stream:read_fully(use_table) return self:_format():read_fully(use_table) end
|
||||
|
||||
function io_stream:read_line() return self.text:read_line() end
|
||||
|
||||
function io_stream:write_line(str) return self.text:write_line(str) end
|
||||
|
||||
function io_stream:available(length)
|
||||
local available = self.io_lib.available and self.io_lib.available(self.descriptor) or 0
|
||||
|
||||
if self.mode == BUFFERED_MODE then
|
||||
available = available + #self.read_buffer
|
||||
end
|
||||
|
||||
if not length then
|
||||
return available
|
||||
else
|
||||
return available >= length
|
||||
end
|
||||
local n = self.stream:available()
|
||||
if length then return n >= length end
|
||||
return n
|
||||
end
|
||||
|
||||
function io_stream:__read(length, from_read_fully)
|
||||
if self.mode == YIELD_MODE then
|
||||
if from_read_fully then
|
||||
return self.io_lib.read(self.descriptor, length)
|
||||
end
|
||||
function io_stream:seek(mode, offset) return self.stream:seek(mode, offset) end
|
||||
|
||||
local buffer = Bytearray()
|
||||
function io_stream:tell() return self.stream:tell() end
|
||||
|
||||
while #buffer < length do
|
||||
buffer:append(self.io_lib.read(self.descriptor, length - #buffer))
|
||||
function io_stream:is_alive() return self.stream:is_alive() end
|
||||
|
||||
if #buffer < length then coroutine.yield() end
|
||||
end
|
||||
function io_stream:is_closed() return not self.stream:is_alive() end
|
||||
|
||||
return buffer
|
||||
elseif self.mode == BUFFERED_MODE then
|
||||
local buf_len = #self.read_buffer
|
||||
|
||||
if buf_len < length then
|
||||
self.read_buffer:append(
|
||||
self.io_lib.read(self.descriptor, self.max_buffer_size - buf_len)
|
||||
)
|
||||
end
|
||||
|
||||
buf_len = #self.read_buffer
|
||||
|
||||
length = math.min(buf_len, length)
|
||||
|
||||
local copy = self.read_buffer:slice(1, length)
|
||||
|
||||
if buf_len == length then
|
||||
self.read_buffer:clear()
|
||||
else
|
||||
self.read_buffer:remove(1, length)
|
||||
end
|
||||
|
||||
return copy
|
||||
elseif self.mode == DEFAULT_MODE then
|
||||
return self.io_lib.read(self.descriptor, length)
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:__write(data)
|
||||
if self.mode == BUFFERED_MODE then
|
||||
local data_length = #data
|
||||
|
||||
if #self.write_buffer + data_length > self.max_buffer_size then
|
||||
self:flush()
|
||||
end
|
||||
|
||||
if data_length > self.max_buffer_size then
|
||||
local to_write = math.floor(data_length / self.max_buffer_size) * self.max_buffer_size
|
||||
local to_save = data_length - to_write
|
||||
|
||||
self.io_lib.write(self.descriptor, data:slice(1, to_write))
|
||||
|
||||
self:flush()
|
||||
|
||||
self.write_buffer = data:slice(to_write + 1, to_save)
|
||||
else self.write_buffer:append(data) end
|
||||
elseif self.mode == DEFAULT_MODE or self.mode == YIELD_MODE then
|
||||
return self.io_lib.write(self.descriptor, data)
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:read_fully(use_table)
|
||||
if self.binary_mode then
|
||||
local result = use_table and { } or Bytearray()
|
||||
|
||||
local avail = self:available()
|
||||
|
||||
if avail == 0 then
|
||||
avail = self.max_buffer_size
|
||||
end
|
||||
|
||||
read_fully(result, function() return self:__read(avail, true) end)
|
||||
|
||||
return result
|
||||
else
|
||||
if use_table then
|
||||
local lines = { }
|
||||
|
||||
local line
|
||||
|
||||
repeat
|
||||
line = self:read_line()
|
||||
|
||||
lines[#lines + 1] = line
|
||||
until not line
|
||||
|
||||
return lines
|
||||
else
|
||||
local result = Bytearray()
|
||||
|
||||
read_fully(result, function() return self:__read(self.max_buffer_size) end)
|
||||
|
||||
return utf8.tostring(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:read_line()
|
||||
local result = Bytearray()
|
||||
|
||||
local first = true
|
||||
|
||||
while true do
|
||||
local char = self:__read(1)
|
||||
|
||||
if #char == 0 then
|
||||
if first then return else break end
|
||||
end
|
||||
|
||||
char = char[1]
|
||||
|
||||
if char == LF then break
|
||||
elseif char == CR then
|
||||
char = self:__read(1)
|
||||
|
||||
if char[1] == LF then break
|
||||
else
|
||||
result:append(CR)
|
||||
result:append(char[1])
|
||||
end
|
||||
else result:append(char) end
|
||||
|
||||
first = false
|
||||
end
|
||||
|
||||
return utf8.tostring(result)
|
||||
end
|
||||
|
||||
function io_stream:write_line(str)
|
||||
self:__write(utf8.tobytes(str .. "\n"))
|
||||
end
|
||||
|
||||
function io_stream:read(arg, use_table)
|
||||
local arg_type = type(arg)
|
||||
|
||||
if self.binary_mode then
|
||||
local byte_arr
|
||||
|
||||
if arg_type == "number" then
|
||||
-- using 'arg' as length
|
||||
|
||||
byte_arr = self:__read(arg)
|
||||
|
||||
if use_table == true then
|
||||
local t = { }
|
||||
|
||||
for i = 1, #byte_arr do
|
||||
t[i] = byte_arr[i]
|
||||
end
|
||||
|
||||
return t
|
||||
else
|
||||
return byte_arr
|
||||
end
|
||||
elseif arg_type == "string" then
|
||||
return byteutil.unpack(
|
||||
arg,
|
||||
self:__read(byteutil.get_size(arg))
|
||||
)
|
||||
elseif arg_type == "nil" then
|
||||
error(
|
||||
"in binary mode the first argument must be a string data format"..
|
||||
" for the library \"byteutil\" or the number of bytes to read"
|
||||
)
|
||||
else
|
||||
error("unknown argument type: "..arg_type)
|
||||
end
|
||||
else
|
||||
if not arg then
|
||||
return self:read_line()
|
||||
else
|
||||
local lines_count = arg
|
||||
local trim_last_empty_lines = use_table
|
||||
|
||||
if use_table == nil then
|
||||
trim_last_empty_lines = true
|
||||
end
|
||||
|
||||
if lines_count < 0 then error "count of lines to read must be positive" end
|
||||
|
||||
local result = { }
|
||||
|
||||
for i = 1, lines_count do
|
||||
result[i] = self:read_line()
|
||||
end
|
||||
|
||||
if trim_last_empty_lines then
|
||||
local i = #result
|
||||
|
||||
while i >= 0 do
|
||||
local length = utf8.length(result[i])
|
||||
|
||||
if length > 0 then break
|
||||
else result[i] = nil end
|
||||
|
||||
i = i - 1
|
||||
end
|
||||
|
||||
local i = 1
|
||||
|
||||
while #result > 0 do
|
||||
local length = utf8.length(result[i])
|
||||
|
||||
if length > 0 then break
|
||||
else table.remove(result, i) end
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:write(arg, ...)
|
||||
local arg_type = type(arg)
|
||||
|
||||
if self.binary_mode then
|
||||
local byte_arr
|
||||
|
||||
if arg_type ~= "string" then
|
||||
-- using arg as bytes table/bytearray
|
||||
|
||||
if arg_type == "table" then
|
||||
byte_arr = Bytearray(arg)
|
||||
else
|
||||
byte_arr = arg
|
||||
end
|
||||
else
|
||||
byte_arr = byteutil.pack(arg, ...)
|
||||
end
|
||||
|
||||
self:__write(byte_arr)
|
||||
else
|
||||
if arg_type == "string" then
|
||||
self:write_line(arg)
|
||||
elseif arg_type == "table" then
|
||||
for i = 1, #arg do
|
||||
self:write_line(arg[i])
|
||||
end
|
||||
else error("unknown argument type: "..arg_type) end
|
||||
end
|
||||
end
|
||||
|
||||
function io_stream:seek(mode, offset)
|
||||
if not self.io_lib.seek then
|
||||
error("cannot seek this stream")
|
||||
end
|
||||
|
||||
self.io_lib.seek(self.descriptor, mode, offset)
|
||||
end
|
||||
|
||||
function io_stream:tell()
|
||||
if not self.io_lib.tell then
|
||||
error("cannot tell this stream")
|
||||
end
|
||||
|
||||
return self.io_lib.tell(self.descriptor)
|
||||
end
|
||||
|
||||
function io_stream:is_alive()
|
||||
return self.io_lib.is_alive(self.descriptor)
|
||||
end
|
||||
|
||||
function io_stream:is_closed()
|
||||
return not self:is_alive()
|
||||
end
|
||||
|
||||
function io_stream:close()
|
||||
if self.mode == BUFFERED_MODE then
|
||||
self.read_buffer:clear()
|
||||
self.write_buffer:clear()
|
||||
end
|
||||
|
||||
return self.io_lib.close(self.descriptor)
|
||||
end
|
||||
function io_stream:close() return self.stream:close() end
|
||||
|
||||
function io_stream:flush()
|
||||
if self.mode == BUFFERED_MODE and #self.write_buffer > 0 then
|
||||
self.io_lib.write(self.descriptor, self.write_buffer)
|
||||
self.write_buffer:clear()
|
||||
end
|
||||
|
||||
if self.flush_mode ~= FLUSH_MODE_ONLY_BUFFER then
|
||||
if self.io_lib.flush then
|
||||
self.io_lib.flush(self.descriptor)
|
||||
elseif self:is_closed() then error("stream is closed") end
|
||||
end
|
||||
if self.mode ~= "buffered" and self.flush_mode == "buffer" then return end
|
||||
self.stream:flush()
|
||||
end
|
||||
|
||||
return io_stream
|
||||
return io_stream
|
||||
|
|
|
|||
|
|
@ -183,6 +183,12 @@ function on_deprecated_call(name, alternatives)
|
|||
end
|
||||
end
|
||||
|
||||
local __pack_envs = __vc__pack_envs
|
||||
function __vc_internals.get_pack_env(packid)
|
||||
return __pack_envs[packid]
|
||||
end
|
||||
__vc__pack_envs = nil
|
||||
|
||||
function reload_module(name)
|
||||
local prefix, name = parse_path(name)
|
||||
local path = prefix..":modules/"..name..".lua"
|
||||
|
|
@ -196,6 +202,10 @@ function reload_module(name)
|
|||
if script == nil then
|
||||
error(err)
|
||||
end
|
||||
local env = __pack_envs[prefix]
|
||||
if env then
|
||||
script = setfenv(script, env)
|
||||
end
|
||||
local result = script()
|
||||
if not result then
|
||||
return
|
||||
|
|
@ -270,12 +280,6 @@ function __vc_lock_internal_modules()
|
|||
__internal_locked = true
|
||||
end
|
||||
|
||||
local __pack_envs = __vc__pack_envs
|
||||
function __vc_internals.get_pack_env(packid)
|
||||
return __pack_envs[packid]
|
||||
end
|
||||
__vc__pack_envs = nil
|
||||
|
||||
function require(path)
|
||||
if not string.find(path, ':') then
|
||||
local prefix, _ = parse_path(_debug_getinfo(2).source)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue