Skip to content

Commit 4316121

Browse files
committed
Fix #21, cleanup
Interface_derived ~ if an editor is findable via multiple search paths, only add the first instance found globals.h, globals.cpp ~ make some definitions constexpr ~ move a definition to own TU (will move more here eventually)
1 parent d3512bc commit 4316121

File tree

6 files changed

+57
-31
lines changed

6 files changed

+57
-31
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (APPLE)
2525
set(OBJCPP "source/AppleUtilities.h" "source/AppleUtilities.mm")
2626
endif()
2727
add_executable("${PROJECT_NAME}" WIN32 ${source} "source/wxmac.icns" "source/windows.rc" ${OBJCPP})
28-
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
28+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
2929

3030
#wxwidgets
3131
set(wxBUILD_SHARED OFF CACHE INTERNAL "")

source/create_dialog_derived.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <dirent.h>
1212
#endif
1313
#include <wx/msgdlg.h>
14+
#include <fmt/format.h>
1415

1516
using namespace std;
1617

@@ -85,7 +86,13 @@ void CreateProjectDialogD::OnCreate(wxCommandEvent& event){
8586

8687
//create the command string
8788
#if defined __APPLE__
88-
string command = "\"" + executablePath.string() + "\" -createproject \"" + (filesystem::path(projPath) / projName).string() + "\" -cloneFromTemplate \"" + executableTemplatesPath.string() + templatePrefix + "." + templateName + "\"";
89+
string command = fmt::format("\"{}\" -createProject \"{}\" -cloneFromTemplate \"{}{}.{}\"",
90+
executablePath.string(),
91+
(filesystem::path(projPath) / projName).string(),
92+
executableTemplatesPath.string(),
93+
templatePrefix,
94+
templateName
95+
);
8996
#elif defined _WIN32
9097
auto fullProj = std::filesystem::path("\"") / projPath / projName / "\"";
9198
auto fullTemplate = std::filesystem::path("\"") / executableTemplatesPath / (templatePrefix + "." + templateName + "\"");

source/globals.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "globals.h"
2+
#include <fmt/format.h>
3+
4+
void launch_process(const std::string& command, int flags) {
5+
#if defined __APPLE__ || defined __linux__
6+
//the '&' runs the command nonblocking, and >/dev/null 2>&1 destroys output
7+
auto fullcmd = fmt::format("{}{} &", command,null_device);
8+
FILE* stream = popen(fullcmd.c_str(), "r");
9+
pclose(stream);
10+
11+
#elif _WIN32
12+
//call wxExecute with the Async flag
13+
wxExecute(wxString(command),flags);
14+
15+
#endif
16+
}

source/globals.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
#include <filesystem>
1212

1313
//data file names
14-
static const std::string projectsFile = "projects.txt";
15-
static const std::string editorPathsFile = "editorPaths.txt";
16-
static const std::string templatePrefix = "com.unity.template";
17-
static const std::string AppVersion = "v1.5";
14+
static constexpr std::string_view projectsFile = "projects.txt";
15+
static constexpr std::string_view editorPathsFile = "editorPaths.txt";
16+
static constexpr std::string_view templatePrefix = "com.unity.template";
17+
static constexpr std::string_view AppVersion = "v1.51";
1818

1919
#if defined __APPLE__
2020
#include <pwd.h>
2121
//the location to store application data
2222
static const std::filesystem::path datapath = std::filesystem::path(getpwuid(getuid())->pw_dir) / "Library/Application Support/UnityHubNative";
23-
static const char dirsep = '/';
2423

2524
static const std::filesystem::path cachedir = std::filesystem::path(getpwuid(getuid())->pw_dir) / "/Library/Caches/com.ravbug.UnityHubNative/";
26-
static const std::string installerExt = "dmg";
25+
static constexpr std::string_view installerExt = "dmg";
2726

2827
//where to find various Unity things on macOS
2928
static const std::filesystem::path executable = "Unity.app/Contents/MacOS/Unity";
@@ -33,7 +32,7 @@ static const std::string AppVersion = "v1.5";
3332
static const std::filesystem::path templatesDir = "Unity.app/Contents/Resources/PackageManager/ProjectTemplates/";
3433

3534
//for stream redirecting to dev/null
36-
static const std::string null_device = ">/dev/null 2>&1";
35+
static constexpr std::string_view null_device = ">/dev/null 2>&1";
3736

3837
#elif defined _WIN32
3938
//naming conflicts
@@ -46,7 +45,6 @@ static const std::string AppVersion = "v1.5";
4645
static const std::filesystem::path homedir = homedrive / homepath;
4746

4847
static const std::filesystem::path datapath = homedir / std::filesystem::path("AppData\\Roaming\\UnityHubNative");
49-
static const char dirsep = '\\';
5048

5149
static const std::filesystem::path cachedir = std::filesystem::temp_directory_path();
5250
static const std::string installerExt = "exe";
@@ -117,7 +115,6 @@ static const std::string AppVersion = "v1.5";
117115
#include <pwd.h>
118116
static const std::filesystem::path datapath = std::filesystem::path(getpwuid(getuid())->pw_dir) / "UnityHubNative";
119117
static const std::string null_device = ">/dev/null 2>&1";
120-
static const char dirsep = '/';
121118

122119
static const std::filesystem::path executable = "Editor/Unity";
123120
static const std::vector<std::filesystem::path> defaultInstall = {std::filesystem::path(getpwuid(getuid())->pw_dir) / "Unity/Hub/Editor"};
@@ -136,6 +133,9 @@ struct project{
136133
std::string version;
137134
std::string modifiedDate;
138135
std::filesystem::path path;
136+
bool operator==(const project& other) const{
137+
return this->path == other.path;
138+
}
139139
};
140140

141141
/**
@@ -144,18 +144,7 @@ struct project{
144144
@param command the shell command to run on the system
145145
@note The command passed to this function must be correct for the system it is running on. If it is not correct, the function will appear to do nothing.
146146
*/
147-
inline void launch_process(const std::string& command, int flags = 0) {
148-
#if defined __APPLE__ || defined __linux__
149-
//the '&' runs the command nonblocking, and >/dev/null 2>&1 destroys output
150-
FILE* stream = popen(std::string(command + null_device + " &").c_str(), "r");
151-
pclose(stream);
152-
153-
#elif _WIN32
154-
//call wxExecute with the Async flag
155-
wxExecute(wxString(command),flags);
156-
157-
#endif
158-
}
147+
void launch_process(const std::string& command, int flags = 0);
159148

160149
inline void reveal_in_explorer(const std::string& path){
161150
#if defined __APPLE__
@@ -205,4 +194,8 @@ struct editor {
205194
decltype(path) executablePath() const{
206195
return path / name / executable;
207196
}
197+
198+
bool operator==(const editor& other){
199+
return this->name == other.name; // many editors can share a root path
200+
}
208201
};

source/interface_derived.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ MainFrameDerived::MainFrameDerived() : MainFrame(NULL){
102102
//if no projects to load, the interface will be blank
103103

104104
//show current version in titlebar
105-
this->SetLabel("Unity Hub Native " + AppVersion);
105+
this->SetLabel(fmt::format("Unity Hub Native {}",AppVersion));
106106
projSearchCtrl->Bind(wxEVT_KEY_UP, &MainFrameDerived::Filter, this);
107107
projSearchCtrl->SetFocus();
108108
}
@@ -303,6 +303,9 @@ void MainFrameDerived::OnPageChanging(wxBookCtrlEvent& event){
303303
*/
304304
void MainFrameDerived::LoadEditorPath(const std::filesystem::path& path){
305305
//add to internal structure and to file
306+
if (std::find(installPaths.begin(),installPaths.end(),path) != installPaths.end()){
307+
return;
308+
}
306309
installPaths.push_back(path);
307310
SaveEditorVersions();
308311

@@ -517,6 +520,11 @@ void MainFrameDerived::SaveEditorVersions(){
517520
*/
518521
void MainFrameDerived::AddProject(const project& p, const std::string& filter, bool select){
519522
//add to the vector backing the UI
523+
if (std::find_if(projects.begin(),projects.end(),[&](const auto& item){
524+
return p == item;
525+
}) != projects.end()){
526+
return;
527+
}
520528
projects.insert(projects.begin(),p);
521529

522530
//save to file
@@ -597,21 +605,23 @@ void MainFrameDerived::LoadEditorVersions(){
597605
char buffer[16];
598606
getCFBundleVersionFromPlist(infopath.string().c_str(), buffer, sizeof(buffer));
599607

600-
a.Add(string(buffer) + " - " + path.string());
601608
//add it to the backing datastructure
602609
editor e = {buffer, path};
603-
604-
editors.push_back(e);
610+
if (std::find(editors.begin(), editors.end(), e) == editors.end()){
611+
a.Add(e.name + " - " + e.path.string());
612+
editors.push_back(e);
613+
}
605614
}
606615
}
607616
else
608617
#endif
609618
{
610-
a.Add(string(entry->d_name) + " - " + path.string());
611619
//add it to the backing datastructure
612620
editor e = {entry->d_name, path};
613-
614-
editors.push_back(e);
621+
if (std::find(editors.begin(), editors.end(), e) == editors.end()){
622+
a.Add(e.name + " - " + e.path.string());
623+
editors.push_back(e);
624+
}
615625
}
616626
}
617627
}

source/interface_derived.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class MainFrameDerived : public MainFrame{
7575
};
7676

7777
//will store the list of projects
78-
std::vector<project> projects;
78+
std::deque<project> projects;
7979
std::vector<std::filesystem::path> installPaths;
8080
std::vector<editor> editors;
8181

0 commit comments

Comments
 (0)