Repository navigation
Expand file tree
/
Copy pathfile.lua
More file actions
106 lines (85 loc) · 3.6 KB
/
Copy pathfile.lua
File metadata and controls
106 lines (85 loc) · 3.6 KB
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
101
102
103
104
105
106
File = { }
File.Addend_Path = 'config\\rsvp'
File.Filename = 'timers'
File.Delimter = '|||'
File.Pattern = string.format("([^%s]+)", File.Delimter)
File.NO_GROUP = '@!@!@!@!@'
-- ------------------------------------------------------------------------------------------------------
-- Create the base file path.
-- ------------------------------------------------------------------------------------------------------
local path = function()
local directory = string.format("%s%s", AshitaCore:GetInstallPath(), File.Addend_Path)
---@diagnostic disable-next-line: undefined-field
return ('%s/'):fmt(directory)
end
-- ------------------------------------------------------------------------------------------------------
-- Check if the base directory exists. If it doesn't, create it.
-- ------------------------------------------------------------------------------------------------------
---@param filePath string
-- ------------------------------------------------------------------------------------------------------
local fileExists = function(filePath)
if not ashita.fs.exists(filePath) then
ashita.fs.create_dir(filePath)
end
---@diagnostic disable-next-line: undefined-field
local file = io.open(('%s/%s'):fmt(filePath, File.Filename), 'r')
if not file then
---@diagnostic disable-next-line: undefined-field
file = io.open(('%s/%s'):fmt(filePath, File.Filename), 'w')
if file then
file:close()
end
end
end
-- ------------------------------------------------------------------------------------------------------
-- Write to file.
-- ------------------------------------------------------------------------------------------------------
File.Save = function()
local filePath = path()
fileExists(filePath)
---@diagnostic disable-next-line: undefined-field
local file = io.open(('%s/%s'):fmt(filePath, File.Filename), 'w')
if file ~= nil then
for timerName, timerData in pairs(Timers.Timers) do
local timerStart = timerData.Start
local timerEnd = timerData.End
local timerGroup = timerData.Group
if not timerGroup then
timerGroup = Timers.Groups.NO_GROUP
end
local saveString = string.format(
"%s%s%s%s%s%s%s",
timerName, File.Delimter,
timerStart, File.Delimter,
timerEnd, File.Delimter,
timerGroup
)
file:write(string.format("%s\n", saveString))
end
file:close()
end
end
-- ------------------------------------------------------------------------------------------------------
-- Load data from file.
-- ------------------------------------------------------------------------------------------------------
File.Load = function()
local filePath = path()
fileExists(filePath)
Timers.Timers = { }
---@diagnostic disable-next-line: undefined-field
for line in io.lines(('%s/%s'):fmt(filePath, File.Filename)) do
local pieces = {}
local pieceIndex = 1
for piece in string.gmatch(line, File.Pattern) do
pieces[pieceIndex] = piece
pieceIndex = pieceIndex + 1
end
if pieces then
local timerName = pieces[1]
local timerStart = tonumber(pieces[2]) or os.time()
local timerEnd = tonumber(pieces[3]) or os.time()
local timerGroup = tostring(pieces[4]) or Timers.Groups.NO_GROUP
Timers.Create(timerName, timerStart, timerEnd, timerGroup)
end
end
end