-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.h
169 lines (140 loc) · 3.87 KB
/
import.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Created by Dima on 11.11.2023.
//
#include "vector"
#include "MusicBlock.h"
#include "iostream"
#include "SDL3_mixer/SDL_mixer.h"
#include "ForWindows/json.hpp"
#include "fstream"
#include <filesystem>
#pragma once
class MusicImporter
{
public:
MusicImporter(const std::filesystem::path& workingDirectory, const std::filesystem::path& JSONDirectory): workDirectory(workingDirectory)
{
if(!std::filesystem::exists(workDirectory))
{
std::filesystem::create_directory(workDirectory);
}
for(auto& directoryEntry : std::filesystem::directory_iterator(workDirectory))
{
Collector.emplace_back(directoryEntry.path());
}
if (!std::filesystem::exists(JSONDirectory))
{
return;
}
std::ifstream ifs(JSONDirectory, std::ios::in);
if (!ifs) {
throw std::runtime_error("Cyka, cannot open file!");
}
nlohmann::json j;
ifs >> j;
ifs.close();
for(auto& mus: Collector)
{
for (const auto& obj : j)
{
if (obj.at("name") == mus.GetName())
{
mus.SetIDFromJSON(obj.at("id").get<uint64_t>());
}
}
} //честно не нравится эта идея, но другое вообще не приходит
}
void Import(const std::filesystem::path& path)
{
SaveMusicIfNotSaved(path);
}
MusicBlock& GetMusic(const std::string& Name)
{
for(auto & i : Collector)
{
if (Name == i.GetName())
{
return i;
}
}
throw std::runtime_error("Could not find music in Importer");
}
MusicBlock& GetMusic(MusicPlayer::UUID id)
{
for(auto & i : Collector)
{
if (id == i.GetUUID())
{
return i;
}
}
throw std::runtime_error("Could not find music in Importer");
}
std::vector<std::string> GetMusicList()
{
std::vector<std::string> tempList;
for(int i=0;i<Collector.size();i++)
{
tempList.push_back(Collector[i].GetName());
}
return tempList;
}
std::vector<MusicPlayer::UUID> GetMusicUUIDList()
{
std::vector<MusicPlayer::UUID> tempList;
for(const auto & i : Collector)
{
tempList.push_back(i.GetUUID());
}
return tempList;
}
std::vector<MusicBlock>& GetCollection()
{
return Collector;
}
void DeleteMusic(const std::string& Name)
{
for(int i=0;i<Collector.size();i++)
{
if (Name == Collector[i].GetName())
{
auto temp_path = Collector[i].GetPath();
Collector.erase(Collector.begin() + i);
std::filesystem::remove_all(temp_path);
return;
}
}
}
private:
std::filesystem::path workDirectory;
std::vector<MusicBlock> Collector;
void SaveMusicIfNotSaved(const std::filesystem::path& path)
{
auto name = std::filesystem::path(path).filename();
auto internalPath = workDirectory / name;
if(std::filesystem::exists(internalPath))
{
return;
}
std::filesystem::copy_file(path,internalPath);
Collector.emplace_back(internalPath);
}
};
void Play(Mix_Music* music)
{
if(Mix_PausedMusic())
{
Mix_ResumeMusic();
return;
}
if(Mix_PlayingMusic())
{
Mix_PauseMusic();
return;
}
Mix_PlayMusic(music, 1); // -1 означает бесконечное воспроизведение
// Ожидание, пока музыка играет
//SDL_Delay(5000); // 5 секунд
// Очистка
//Mix_FreeMusic(music);
}