-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReplayProvider.cpp
178 lines (150 loc) · 4.25 KB
/
ReplayProvider.cpp
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
169
170
171
172
173
174
175
176
177
178
/*
** RLReplayManager
**
** Copyright (C) 2015 Tobias Taschner <[email protected]>
**
** Licensed under GPL v3 or later
*/
#include <wx/wxprec.h>
#include "ReplayProvider.h"
#include "TransferManager.h"
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/config.h>
#include <wx/zipstrm.h>
#include <wx/wfstream.h>
wxDEFINE_EVENT(wxEVT_REPLAY_REMOVED, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_REPLAY_ADDED, wxCommandEvent);
//
// ReplayProvider
//
ReplayProvider::ReplayProvider(ReplayProvider* parent, const wxString& path, const wxString& description):
m_parent(parent),
m_description(description)
{
if (!path.empty())
FindFilesInFolder(path);
}
void ReplayProvider::FindFilesInFolder(const wxString& path)
{
m_localPath = path;
replay.clear();
wxArrayString replayFiles;
wxDir::GetAllFiles(path, &replayFiles, "*.replay", wxDIR_FILES);
for (auto filename = replayFiles.begin(); filename != replayFiles.end(); ++filename)
{
Replay::Ptr ri(new Replay(*filename));
replay.push_back(ri);
}
m_fsWatcher.Bind(wxEVT_FSWATCHER, &ReplayProvider::OnFileSystemChange, this);
m_fsWatcher.Add(path, wxFSW_EVENT_CREATE | wxFSW_EVENT_DELETE | wxFSW_EVENT_RENAME);
}
Replay* ReplayProvider::FindReplay(const wxString& filename) const
{
for (auto it = replay.begin(); it != replay.end(); ++it)
{
if ((*it)->GetFileName().IsSameAs(filename, false))
return it->get();
}
return NULL;
}
void ReplayProvider::OnFileSystemChange(wxFileSystemWatcherEvent& event)
{
wxLogDebug(event.ToString());
if (!event.GetPath().GetExt().IsSameAs("replay", false))
return;
if (event.GetChangeType() & wxFSW_EVENT_CREATE)
{
Replay* existingReplay = FindReplay(event.GetPath().GetFullPath());
if (existingReplay) // Replay is already in list
return;
// Give rocket league some time to write the file
wxMilliSleep(500);
// Add new file
Replay::Ptr ri(new Replay(event.GetPath().GetFullPath()));
replay.push_back(ri);
if (wxConfig::Get()->ReadBool("AutoUpload", false))
{
TransferManager::Get().Upload(ri);
}
wxCommandEvent evt(wxEVT_REPLAY_ADDED);
evt.SetInt(replay.size() - 1);
evt.SetEventObject(this);
GetRoot()->ProcessEvent(evt);
}
else if (event.GetChangeType() & wxFSW_EVENT_DELETE)
{
// Find replay and remove it
size_t index = 0;
wxString changePath = event.GetPath().GetFullPath();
for (auto replayIT = replay.begin(); replayIT != replay.end(); ++replayIT, ++index)
{
if ((*replayIT)->GetFileName().IsSameAs(changePath, false))
{
replay.erase(replayIT);
break;
}
}
wxCommandEvent evt(wxEVT_REPLAY_REMOVED);
evt.SetInt(index);
evt.SetEventObject(this);
GetRoot()->ProcessEvent(evt);
}
else if (event.GetChangeType() & wxFSW_EVENT_RENAME)
{
wxString changePath = event.GetPath().GetFullPath();
// Find replay and update filename
for (auto replayIT = replay.begin(); replayIT != replay.end(); ++replayIT)
{
if ((*replayIT)->GetFileName().IsSameAs(changePath, false))
{
(*replayIT)->SetFileName(event.GetNewPath().GetFullPath());
break;
}
}
}
event.Skip();
}
ReplayProvider* ReplayProvider::GetRoot() const
{
ReplayProvider* provider = const_cast<ReplayProvider*>(this);
while (provider->GetParent() != NULL)
provider = provider->GetParent();
return provider;
}
bool ReplayProvider::Import(const wxString& filename, bool move)
{
wxFileName fn(filename);
if (fn.GetExt().IsSameAs("zip", false))
{
// TODO: handle zip files
wxFileInputStream fstr(filename);
wxZipInputStream zip(fstr);
while (wxZipEntry* zipEntry = zip.GetNextEntry())
{
wxFileName entryFN(zipEntry->GetName());
if (entryFN.GetExt().IsSameAs("replay", false))
{
wxFileName targetFN(m_localPath, entryFN.GetFullName());
wxFileOutputStream targetFile(targetFN.GetFullPath());
zip.Read(targetFile);
targetFile.Close();
targetFN.SetTimes(NULL, &zipEntry->GetDateTime(), &zipEntry->GetDateTime());
Replay::Ptr ri(new Replay(targetFN.GetFullPath()));
replay.push_back(ri);
}
}
}
else if (fn.GetExt().IsSameAs("replay", false))
{
wxFileName targetFN(m_localPath, fn.GetFullName());
bool copyRes = wxCopyFile(filename, targetFN.GetFullPath(), false);
if (copyRes)
{
Replay::Ptr ri(new Replay(targetFN.GetFullPath()));
replay.push_back(ri);
}
return copyRes;
}
return false;
}