Skip to content

Commit

Permalink
Launcher patch
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Jul 28, 2024
1 parent 40d86d5 commit 0a75046
Show file tree
Hide file tree
Showing 17 changed files with 819 additions and 56 deletions.
3 changes: 3 additions & 0 deletions apps/EraLauncher/src/imgui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include <imgui/imgui.h>
221 changes: 220 additions & 1 deletion apps/EraLauncher/src/launcher/build/project_builder.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,220 @@
#include "launcher/build/project_builder.h"
#include "launcher/build/project_builder.h"
#include "launcher/core/hub_project.h"

#include <cderr.h>

namespace era_engine::launcher
{
std::optional<std::string> builder::selectBuildFolder()
{
save_file_dialog dialog("Project location");
dialog.defineExtension("Project", "..");
dialog.show();
if (dialog.hasSucceeded())
{
std::string result = dialog.getSelectedFilePath();
result = std::string(result.data(), result.data() + result.size() - std::string("..").size()) + "\\";

if (!std::filesystem::exists(result))
return result;
else
{
//LOG_ERR("[Building] Folder already exists!");
return {};
}
}
else
{
return {};
}
}

void builder::build(hub_project& project, bool autoRun, bool tempFolder)
{
std::string destinationFolder;

if (tempFolder)
{
destinationFolder = std::string(std::filesystem::current_path().string()) + "\\Data\\Temp\\ProjectTemplate\\";
try
{
std::filesystem::remove_all(destinationFolder);
}
catch (std::filesystem::filesystem_error error)
{
//LOG_ERR("[Building] Temporary build failed!");
return;
}
}
else if (auto res = selectBuildFolder(); res.has_value())
destinationFolder = res.value();
else
return;

std::string buildPath(destinationFolder);

bool failed = false;

project.path = buildPath;

//LOG_INFO("Preparing to build at location: \"" + buildPath + "\"");

std::filesystem::remove_all(buildPath);
std::error_code err;

try
{
std::filesystem::copy("Data\\ProjectTemplate", buildPath, std::filesystem::copy_options::recursive, err);
}
catch (...)
{
//LOG_ERR("[Building] " + err.message());
}

//LOG_INFO("[Building] Builded successfuly!");
}

file_dialog::file_dialog(std::function<int(tagOFNA*)> callback_, const std::string& dialogTitle_)
: callback(callback_),
dialogTitle(dialogTitle_),
initialDirectory("")
{
}

void file_dialog::setInitialDirectory(const std::string& initialDirectory_)
{
initialDirectory = initialDirectory_;
}

void file_dialog::show(explorer_flags flags)
{
if (!initialDirectory.empty())
filepath = initialDirectory;

filepath.resize(MAX_PATH);

OPENFILENAMEA ofn;
ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = filter.c_str();
ofn.lpstrFile = filepath.data();
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = dialogTitle.c_str();

if (!initialDirectory.empty())
ofn.lpstrInitialDir = initialDirectory.c_str();

ofn.Flags = static_cast<DWORD>(flags);

succeeded = callback(&ofn);

if (!succeeded)
handleError();

filename.clear();

for (auto it = filepath.rbegin(); it != filepath.rend() && *it != '\\' && *it != '/'; ++it)
filename += *it;

std::reverse(filename.begin(), filename.end());
}

bool file_dialog::hasSucceeded() const
{
return succeeded;
}

std::string file_dialog::getSelectedFileName()
{
return filename;
}

std::string file_dialog::getSelectedFilePath()
{
return filepath;
}

std::string file_dialog::getErrorInfo()
{
return error;
}

bool file_dialog::isFileExisting() const
{
return std::filesystem::exists(filepath);
}

void file_dialog::handleError()
{
switch (CommDlgExtendedError())
{
case CDERR_DIALOGFAILURE: error = "CDERR_DIALOGFAILURE"; break;
case CDERR_FINDRESFAILURE: error = "CDERR_FINDRESFAILURE"; break;
case CDERR_INITIALIZATION: error = "CDERR_INITIALIZATION"; break;
case CDERR_LOADRESFAILURE: error = "CDERR_LOADRESFAILURE"; break;
case CDERR_LOADSTRFAILURE: error = "CDERR_LOADSTRFAILURE"; break;
case CDERR_LOCKRESFAILURE: error = "CDERR_LOCKRESFAILURE"; break;
case CDERR_MEMALLOCFAILURE: error = "CDERR_MEMALLOCFAILURE"; break;
case CDERR_MEMLOCKFAILURE: error = "CDERR_MEMLOCKFAILURE"; break;
case CDERR_NOHINSTANCE: error = "CDERR_NOHINSTANCE"; break;
case CDERR_NOHOOK: error = "CDERR_NOHOOK"; break;
case CDERR_NOTEMPLATE: error = "CDERR_NOTEMPLATE"; break;
case CDERR_STRUCTSIZE: error = "CDERR_STRUCTSIZE"; break;
case FNERR_BUFFERTOOSMALL: error = "FNERR_BUFFERTOOSMALL"; break;
case FNERR_INVALIDFILENAME: error = "FNERR_INVALIDFILENAME"; break;
case FNERR_SUBCLASSFAILURE: error = "FNERR_SUBCLASSFAILURE"; break;
default: error = "You cancelled.";
}
}

save_file_dialog::save_file_dialog(const std::string& dialogTitle_)
: file_dialog(GetSaveFileNameA, dialogTitle_)
{
}

void save_file_dialog::show(explorer_flags flags)
{
file_dialog::show(flags);

if (succeeded)
addExtensionToFilePathAndName();
}

void save_file_dialog::defineExtension(const std::string& label_, const std::string& extension_)
{
extension = extension_;
filter = label_ + '\0' + '*' + extension + '\0';
}

void save_file_dialog::addExtensionToFilePathAndName()
{
if (filename.size() >= extension.size())
{
std::string fileEnd(filename.data() + filename.size() - extension.size(), filename.data() + filename.size());

if (fileEnd != extension)
{
filepath += extension;
filename += extension;
}
}
else
{
filepath += extension;
filename += extension;
}
}

open_file_dialog::open_file_dialog(const std::string& dialogTitle_)
: file_dialog(GetOpenFileNameA, dialogTitle_)
{
}

void open_file_dialog::addFileType(const std::string& label_, const std::string& filter_)
{
filter += label_ + '\0' + filter_ + '\0';
}

}
111 changes: 111 additions & 0 deletions apps/EraLauncher/src/launcher/build/project_builder.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
#pragma once
#include <commdlg.h>

namespace era_engine::launcher
{
struct hub_project;

class builder
{
public:
std::optional<std::string> selectBuildFolder();

void build(hub_project& project, bool autoRun = false, bool tempFolder = false);
};

enum class explorer_flags
{
READONLY = 0x00000001,
OVERWRITEPROMPT = 0x00000002,
HIDEREADONLY = 0x00000004,
NOCHANGEDIR = 0x00000008,
SHOWHELP = 0x00000010,
ENABLEHOOK = 0x00000020,
ENABLETEMPLATE = 0x00000040,
ENABLETEMPLATEHANDLE = 0x00000080,
NOVALIDATE = 0x00000100,
ALLOWMULTISELECT = 0x00000200,
EXTENSIONDIFFERENT = 0x00000400,
PATHMUSTEXIST = 0x00000800,
FILEMUSTEXIST = 0x00001000,
CREATEPROMPT = 0x00002000,
SHAREAWARE = 0x00004000,
NOREADONLYRETURN = 0x00008000,
NOTESTFILECREATE = 0x00010000,
NONETWORKBUTTON = 0x00020000,
NOLONGNAMES = 0x00040000, // Force no long names for 4.x modules
EXPLORER = 0x00080000, // New look commdlg
NODEREFERENCELINKS = 0x00100000,
LONGNAMES = 0x00200000, // Force long names for 3.x modules
ENABLEINCLUDENOTIFY = 0x00400000, // Send include message to callback
ENABLESIZING = 0x00800000,
DONTADDTORECENT = 0x02000000,
FORCESHOWHIDDEN = 0x10000000 // Show All files including System and hidden files
};

inline explorer_flags operator~ (explorer_flags a) { return (explorer_flags)~(int)a; }
inline explorer_flags operator| (explorer_flags a, explorer_flags b) { return (explorer_flags)((int)a | (int)b); }
inline explorer_flags operator& (explorer_flags a, explorer_flags b) { return (explorer_flags)((int)a & (int)b); }
inline explorer_flags operator^ (explorer_flags a, explorer_flags b) { return (explorer_flags)((int)a ^ (int)b); }
inline explorer_flags& operator|= (explorer_flags& a, explorer_flags b) { return (explorer_flags&)((int&)a |= (int)b); }
inline explorer_flags& operator&= (explorer_flags& a, explorer_flags b) { return (explorer_flags&)((int&)a &= (int)b); }
inline explorer_flags& operator^= (explorer_flags& a, explorer_flags b) { return (explorer_flags&)((int&)a ^= (int)b); }

class file_dialog
{
public:
file_dialog(std::function<int(tagOFNA*)> callback_, const std::string& dialogTitle_);

void setInitialDirectory(const std::string& initialDirectory_);

virtual void show(explorer_flags flags = explorer_flags::DONTADDTORECENT | explorer_flags::FILEMUSTEXIST | explorer_flags::HIDEREADONLY | explorer_flags::NOCHANGEDIR);

bool hasSucceeded() const;

std::string getSelectedFileName();

std::string getSelectedFilePath();

std::string getErrorInfo();

bool isFileExisting() const;

private:
void handleError();

protected:
std::function<int(tagOFNA*)> callback;

const std::string dialogTitle;

std::string initialDirectory;
std::string filter;
std::string error;
std::string filename;
std::string filepath;

bool succeeded;
};

class save_file_dialog : public file_dialog
{
public:
save_file_dialog(const std::string& dialogTitle_);

virtual void show(explorer_flags flags = explorer_flags::DONTADDTORECENT | explorer_flags::FILEMUSTEXIST | explorer_flags::HIDEREADONLY | explorer_flags::NOCHANGEDIR) override;

void defineExtension(const std::string& label_, const std::string& extension_);

private:
void addExtensionToFilePathAndName();

private:
std::string extension;
};

class open_file_dialog : public file_dialog
{
public:
open_file_dialog(const std::string& dialogTitle_);

void addFileType(const std::string& label_, const std::string& filter_);
};
}
Loading

0 comments on commit 0a75046

Please sign in to comment.