summaryrefslogtreecommitdiff
path: root/server/src/files/files.lua
blob: 4f5b064e6c883b29fe92a2adc951568619ca0ce5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local file = require 'files.file'

---@class files
local mt = {}
mt.__index = mt
mt.type = 'files'
mt._fileCount = 0
---@type table<uri, file>
mt._files = nil

---@param uri uri
---@param text string
function mt:save(uri, text, version)
    local f = self._files[uri]
    if not f then
        f = file(uri)
        self._files[uri] = f
        self._fileCount = self._fileCount + 1
    end
    f:setText(text)
    f:setVersion(version)
end

---@param uri uri
function mt:remove(uri)
    local f = self._files[uri]
    if not f then
        return
    end

    f:remove()
    self._files[uri] = nil
    self._fileCount = self._fileCount - 1
end

---@param uri uri
function mt:open(uri)
    self._open[uri] = true
end

---@param uri uri
function mt:close(uri)
    self._open[uri] = nil
end

---@param uri uri
---@return boolean
function mt:isOpen(uri)
    return self._open[uri] == true
end

---@param uri uri
function mt:isDead(uri)
    local f = self._files[uri]
    if not f then
        return true
    end
    if f:isRemoved() then
        return true
    end
    return f:getVersion() == -1
end

---@param uri uri
---@return file
function mt:get(uri)
    return self._files[uri]
end

---@param uri uri
---@param vm VM
function mt:saveVM(uri, vm)
    local f = self._files[uri]
    if not f then
        return
    end
    f:saveVM(vm)
end

function mt:clear()
    for _, f in pairs(self._files) do
        f:remove()
    end
end

function mt:eachFile()
    return pairs(self._files)
end

function mt:eachOpened()
    return pairs(self._open)
end

return function ()
    local self = setmetatable({
        _files = {},
        _open = {},
    }, mt)
    return self
end