Skip to content

Commit e4a9d6b

Browse files
committed
Tentative fix for #3
Use std::filesystem instead of manually concatenating paths and using stat to test existence
1 parent 6b7785c commit e4a9d6b

File tree

6 files changed

+50
-47
lines changed

6 files changed

+50
-47
lines changed

source/add_install_dlg_derived.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void AddNewInstallDlg::InstallSelected(wxCommandEvent&){
175175
}
176176
else{
177177
// write the file to temp location
178-
auto outpath = fmt::format("{}{}{}.{}", cachedir,"UnityDownloadAssistant",data.hashcode,installerExt);
178+
auto outpath = fmt::format("{}{}{}.{}", cachedir.string(), "UnityDownloadAssistant", data.hashcode, installerExt);
179179
ofstream outfile(outpath, std::ios::binary);
180180
outfile.write(r.text.c_str(),r.text.size());
181181

source/create_dialog_derived.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ void CreateProjectDialogD::OnCreate(wxCommandEvent& event){
7070
if (message == ""){
7171
//assemble the command that will create the project described by the dialog
7272
editor& e = editors[unityVersionChoice->GetSelection()];
73-
string executablePath = e.path + dirsep + e.name + dirsep + executable;
74-
string executableTemplatesPath = e.path + dirsep + e.name + dirsep + templatesDir;
73+
auto executablePath = e.path / e.name / executable;
74+
auto executableTemplatesPath = e.path / e.name / templatesDir;
7575
string projName = projNameTxt->GetValue().ToStdString();
7676
string projPath = projLocTxt->GetValue().ToStdString();
7777

@@ -86,9 +86,9 @@ void CreateProjectDialogD::OnCreate(wxCommandEvent& event){
8686
#if defined __APPLE__
8787
string command = "\"" + executablePath + "\" -createproject \"" + projPath + dirsep + projName + "\" -cloneFromTemplate \"" + executableTemplatesPath + templatePrefix + "." + templateName + "\"";
8888
#elif defined _WIN32
89-
string fullProj = "\"" + projPath + dirsep + projName + "\"";
90-
string fullTemplate = "\"" + executableTemplatesPath + templatePrefix + "." + templateName + "\"";
91-
string command = "\"" + executablePath + "\" -createproject " + fullProj + " -cloneFromTemplate " + fullTemplate;
89+
auto fullProj = std::filesystem::path("\"") / projPath / projName / "\"";
90+
auto fullTemplate = std::filesystem::path("\"") / executableTemplatesPath / (templatePrefix + "." + templateName + "\"");
91+
string command = "\"" + executablePath.string() + "\" -createproject " + fullProj.string() + " -cloneFromTemplate " + fullTemplate.string();
9292
#elif defined __linux__
9393
string command = "\"" + executablePath + "\" -createproject \"" + projPath + dirsep + projName + "\" -cloneFromTemplate \"" + executableTemplatesPath + templatePrefix + "." + templateName + "\"";
9494
#endif
@@ -139,8 +139,8 @@ void CreateProjectDialogD::loadTemplates(const editor& e){
139139
templateCtrl->ClearAll();
140140

141141
//open the folder
142-
string templatesFolder = e.path + dirsep + e.name + dirsep + templatesDir;
143-
DIR* dir = opendir(templatesFolder.c_str());
142+
auto templatesFolder = e.path /e.name / templatesDir;
143+
DIR* dir = opendir(templatesFolder.string().c_str());
144144
struct dirent *entry = readdir(dir);
145145
//loop over the contents
146146
while (entry != NULL)

source/globals.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static const std::string AppVersion = "v1.4";
1919
//structure for representing an editor and for locating it
2020
struct editor{
2121
std::string name;
22-
std::string path;
22+
std::filesystem::path path;
2323
};
2424

2525
#if defined __APPLE__
@@ -46,19 +46,20 @@ struct editor{
4646
#define popen _popen
4747
#define pclose _pclose
4848
#define mkdir _mkdir
49-
#include <wx/wx.h>
50-
static const std::string datapath = getenv("HOMEPATH") + std::string("\\AppData\\Roaming\\UnityHubNative");
49+
#include <wx/wx.h>
50+
static const std::string homedir = getenv("HOMEPATH");
51+
static const std::filesystem::path datapath = std::filesystem::path(homedir) / std::filesystem::path("AppData\\Roaming\\UnityHubNative");
5152
static const char dirsep = '\\';
5253

53-
static const std::string cachedir = std::filesystem::temp_directory_path().string();
54+
static const std::filesystem::path cachedir = std::filesystem::temp_directory_path();
5455
static const std::string installerExt = "exe";
5556

5657
//where to find various Unity things on windows
57-
static const std::string executable = "Editor\\Unity.exe";
58-
static const std::vector<std::string> defaultInstall = {"\\Program Files\\Unity\\Hub\\Editor"};
58+
static const std::filesystem::path executable = "Editor\\Unity.exe";
59+
static const std::vector<std::filesystem::path> defaultInstall = {"\\Program Files\\Unity\\Hub\\Editor"};
5960

60-
static const std::string hubDefault = "\\Program Files\\Unity Hub\\Unity Hub.exe";
61-
static const std::string templatesDir = "Editor\\Data\\Resources\\PackageManager\\ProjectTemplates\\";
61+
static const std::filesystem::path hubDefault = "\\Program Files\\Unity Hub\\Unity Hub.exe";
62+
static const std::filesystem::path templatesDir = "Editor\\Data\\Resources\\PackageManager\\ProjectTemplates\\";
6263

6364
/**
6465
@returns the calculated display scale factor using GDI+
@@ -137,7 +138,7 @@ struct project{
137138
std::string name;
138139
std::string version;
139140
std::string modifiedDate;
140-
std::string path;
141+
std::filesystem::path path;
141142
};
142143

143144
/**

source/interface_derived.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#endif
1818

1919
#include <fmt/format.h>
20+
#include <filesystem>
2021
using namespace std;
22+
using namespace std::filesystem;
2123

2224
#define LEARN_TAB 2
2325
#define WEBVIEW 2000
@@ -73,7 +75,7 @@ MainFrameDerived::MainFrameDerived() : MainFrame(NULL){
7375
int status = mkdir(datapath.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
7476
menuReveal->SetItemLabel("Reveal In Finder\tCtrl-F");
7577
#elif defined _WIN32
76-
int status = mkdir(datapath.c_str());
78+
int status = mkdir(datapath.string().c_str());
7779
//on windows also make the main window background white
7880
this->SetBackgroundColour(*wxWHITE);
7981
//high DPI scaling fixes
@@ -106,8 +108,8 @@ void MainFrameDerived::ReloadData(){
106108
editors.clear();
107109

108110
//check that projects file exists in folder
109-
string p = string(datapath + dirsep + projectsFile);
110-
if (file_exists(p)){
111+
path p = datapath / projectsFile;
112+
if (filesystem::exists(p)){
111113
ifstream in;
112114
in.open(p);
113115
string line;
@@ -140,8 +142,8 @@ void MainFrameDerived::ReloadData(){
140142
}
141143

142144
//check that the installs path file exists in the folder
143-
p = string(datapath + dirsep + editorPathsFile);
144-
if (file_exists(p)){
145+
p = datapath / editorPathsFile;
146+
if (filesystem::exists(p)){
145147
//load the editors
146148
ifstream in; in.open(p); string line;
147149
while (getline(in, line)){
@@ -232,14 +234,14 @@ void MainFrameDerived::OnPageChanging(wxBookCtrlEvent& event){
232234
Loads an editor search path into the app, updating the UI and the vector
233235
@param path the string path to laod
234236
*/
235-
void MainFrameDerived::LoadEditorPath(const string& path){
237+
void MainFrameDerived::LoadEditorPath(const std::filesystem::path& path){
236238
//add to internal structure and to file
237239
installPaths.push_back(path);
238240
SaveEditorVersions();
239241

240242
//add to the UI
241243
wxArrayString a;
242-
a.Add(path);
244+
a.Add(path.string());
243245

244246
installsPathsList->Append(a);
245247
}
@@ -289,7 +291,7 @@ void MainFrameDerived::OnRevealProject(wxCommandEvent& event){
289291
long selectedIndex = wxListCtrl_get_selected(projectsList);
290292
if (selectedIndex > -1){
291293
project& p = projects[selectedIndex];
292-
reveal_in_explorer(p.path);
294+
reveal_in_explorer(p.path.string());
293295
}
294296
}
295297

@@ -318,13 +320,13 @@ void MainFrameDerived::OpenProject(const long& index){
318320
//get the project
319321
project p = projects[index];
320322

321-
for(string path : installPaths){
322-
string editorPath = path + dirsep + p.version + dirsep + executable;
323+
for(auto& path : installPaths){
324+
auto editorPath = path / p.version / executable;
323325

324326
//check that the unity editor exists at that location
325-
if (file_exists(editorPath)){
327+
if (filesystem::exists(editorPath)){
326328

327-
string cmd = "\"" + editorPath + "\" -projectpath \"" + p.path + "\"";
329+
string cmd = "\"" + editorPath.string() + "\" -projectpath \"" + p.path.string() + "\"";
328330

329331
//start the process
330332
launch_process(cmd);
@@ -343,7 +345,7 @@ void MainFrameDerived::OpenProject(const long& index){
343345
@param e the editor version to use when opening the project
344346
*/
345347
void MainFrameDerived::OpenProject(const project& p, const editor& e){
346-
string cmd = "\"" + e.path + dirsep + e.name + dirsep + executable + "\" -projectpath \"" + p.path + "\"";
348+
string cmd = "\"" + (e.path / e.name / executable).string() + "\" -projectpath \"" + p.path.string() + "\"";
347349
launch_process(cmd);
348350
}
349351

@@ -408,18 +410,18 @@ project MainFrameDerived::LoadProject(const string &path){
408410
*/
409411
void MainFrameDerived::SaveProjects(){
410412
ofstream file;
411-
file.open(datapath + dirsep + projectsFile);
413+
file.open(datapath / projectsFile);
412414
for (project& p : projects){
413-
file << p.path << endl;
415+
file << p.path.string() << endl;
414416
}
415417
file.close();
416418
}
417419

418420
void MainFrameDerived::SaveEditorVersions(){
419421
ofstream file;
420-
file.open(datapath + dirsep + editorPathsFile);
421-
for (string& p : installPaths){
422-
file << p << endl;
422+
file.open(datapath / editorPathsFile);
423+
for (auto& p : installPaths){
424+
file << p.string() << endl;
423425
}
424426
file.close();
425427
LoadEditorVersions();
@@ -454,7 +456,7 @@ void MainFrameDerived::AddProject(const project& p){
454456
projectsList->SetItem(i);
455457

456458
i.SetColumn(3);
457-
i.SetText(p.path);
459+
i.SetText(p.path.string());
458460
projectsList->SetItem(i);
459461

460462
//resize columns
@@ -473,9 +475,9 @@ void MainFrameDerived::LoadEditorVersions(){
473475
editors.clear();
474476

475477
//iterate over the search paths
476-
for (string& path : installPaths){
478+
for (auto& path : installPaths){
477479
//open the folder
478-
DIR* dir = opendir(path.c_str());
480+
DIR* dir = opendir(path.string().c_str());
479481
if(dir != nullptr){
480482
struct dirent *entry = readdir(dir);
481483
//loop over the contents
@@ -485,10 +487,10 @@ void MainFrameDerived::LoadEditorVersions(){
485487
//is this a folder?
486488
if (entry->d_type == DT_DIR){
487489
//does this folder have a valid executable inside?
488-
string p = string(path + dirsep + entry->d_name + dirsep + executable);
489-
if (file_exists(p)){
490+
auto p = path / entry->d_name / executable;
491+
if (filesystem::exists(p)){
490492
//add it to the list
491-
a.Add(string(entry->d_name) + " - " + path);
493+
a.Add(string(entry->d_name) + " - " + path.string());
492494

493495
//add it to the backing datastructure
494496
editor e = {entry->d_name, path};

source/interface_derived.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class MainFrameDerived : public MainFrame{
3737
void OpenProject(const long& index);
3838
void OpenProject(const project& p, const editor& e);
3939
void SaveEditorVersions();
40-
void LoadEditorPath(const std::string& path);
40+
void LoadEditorPath(const std::filesystem::path& path);
4141
void LoadEditorVersions();
4242
void ReloadData();
4343

4444
//will store the list of projects
4545
std::vector<project> projects;
46-
std::vector<std::string> installPaths;
46+
std::vector<std::filesystem::path> installPaths;
4747
std::vector<editor> editors;
4848
wxWebView* learnView = NULL;
4949
const std::string homeurl = "https://learn.unity.com";
@@ -141,8 +141,8 @@ class MainFrameDerived : public MainFrame{
141141
int id = installsList->GetSelection();
142142
if (id != wxNOT_FOUND){
143143
editor& e = editors[id];
144-
std::string path = e.path + dirsep + e.name;
145-
reveal_in_explorer(path);
144+
std::filesystem::path path = e.path / e.name;
145+
reveal_in_explorer(path.string());
146146
}
147147
}
148148
/**
@@ -152,7 +152,7 @@ class MainFrameDerived : public MainFrame{
152152
void OnRevealInstallLocation(wxCommandEvent& event){
153153
int id = installsPathsList->GetSelection();
154154
if (id != wxNOT_FOUND){
155-
reveal_in_explorer(installPaths[id]);
155+
reveal_in_explorer(installPaths[id].string());
156156
}
157157
}
158158
void OnOpenHub(wxCommandEvent& event);

source/open_with_dlg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ OpenWithDlg::OpenWithDlg(wxWindow* parent, const project& project, const vector<
1919
//populate list ctrl in reverse so ids match
2020
wxArrayString a;
2121
for (const editor& e : versions){
22-
a.Add(e.name + " - " + e.path);
22+
a.Add(e.name + " - " + e.path.string());
2323
}
2424
editorsListBox->InsertItems( a, 0);
2525

0 commit comments

Comments
 (0)