aboutsummaryrefslogtreecommitdiffstats
path: root/Files.lua
blob: 8fc19fe67fb0b6b1b93a563dd6b20b250a47e2a1 (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
101
102
local Files = {}

function Files.findSongs(folder, fileTree)
	if folder == nil then folder = "" end
	if fileTree == nil then fileTree = {} end
	local filesTable = love.filesystem.getDirectoryItems(folder)
	for i,v in ipairs(filesTable) do
		local file = folder.."/"..v
		if love.filesystem.isFile(file) and v:sub(-3) == ".sm" then
			table.insert(fileTree, file)
		elseif love.filesystem.isDirectory(file) then
			fileTree = Files.findSongs(file, fileTree)
		end
	end
	return fileTree
end

function Files.readNotesInfos(smInfo)

	local pos = { mesure = 1, beat = 1, note = 1}
	local state = 0
	local numNotes = 0

	for line in love.filesystem.lines(smInfo.file) do
		line = line:gsub("//.*", "")

		local info, text= line:match("^#(%u+):(.-);?%s*$")

		if info == "NOTES" then
			pos = { mesure = 1, beat = 1, note = 1}
			state = 1
			numNotes = numNotes + 1
			if numNotes > 1 then
				smInfo["notes"][numNotes] = Utils.copyTable(SMFileSchema.FileSchema["notes"][1])
			end

		elseif state > 0 and state < SMFileSchema.NotesInfos.noteData then

			if state <= SMFileSchema.NotesInfos.difficultyMeter then
				tmp = line:match("^%s*(.+):.*$")
			elseif state == SMFileSchema.NotesInfos.radarValues then
				tmp = {}
				for i in line:gmatch("%d+%.%d+") do
					table.insert(tmp,i)
				end
			end

			if tmp then
				smInfo["notes"][numNotes][state][2] = tmp
				state = state + 1
			end
		elseif state == SMFileSchema.NotesInfos.noteData then
			for c in line:gmatch"." do
				if c == "," then
					pos.beat = 1
					pos.mesure = pos.mesure + 1
				elseif c == ";" then
					pos = { mesure = 1, beat = 1, note = 1}
				elseif c:find("%w") then
					if not smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure] then
						smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure] = {}
					end
					if not smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure][pos.beat] then
						smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure][pos.beat] = {}
					end

					table.insert(smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure][pos.beat], c)

					if #smInfo["notes"][numNotes][SMFileSchema.NotesInfos.noteData][2][pos.mesure][pos.beat] >= SMFileSchema.NotesType[smInfo["notes"][numNotes][SMFileSchema.NotesInfos.notesType][2]] then
						pos.beat = pos.beat + 1
					end
				end
			end
		end 
	end
	
	return smInfo
end

function Files.getSMInfo(file)
	local smInfo = Utils.copyTable(SMFileSchema.FileSchema)
	smInfo.file = file
	for line in love.filesystem.lines(smInfo.file) do
		line = line:gsub("//.*", "")

		local info, text= line:match("^#(%u+):(.-);?%s*$")

		if info and info ~= "NOTES" then
			if type(smInfo[info:lower()]) == "table" then
				for i in text:gmatch("%d+%.%d+=%d+%.%d+") do
					local beat, bpm = i:match("(%d+%.%d+)=(%d+%.%d+)")
					smInfo[info:lower()][beat] = bpm
				end
			else
				smInfo[info:lower()] = text
			end
		end
	end
	return smInfo
end

return Files