Added scripting quality of life features #299

Merged
Pycorax merged 10 commits from SP3-6-c-scripting into main 2023-01-01 12:37:10 +08:00
3 changed files with 58 additions and 25 deletions
Showing only changes of commit 88e89a226a - Show all commits

View File

@ -120,6 +120,11 @@ namespace SHADE
auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
scriptEngine->GenerateScriptsCsProjFile();
}
if (ImGui::Selectable("Open Visual Studio Project"))
{
auto* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
scriptEngine->OpenSolution();
}
ImGui::BeginDisabled(SHSystemManager::GetSystem<SHEditor>()->editorState != SHEditor::State::STOP);
if (ImGui::Selectable("Build Scripts - Debug"))
{

View File

@ -306,6 +306,22 @@ namespace SHADE
file.close();
}
void SHScriptEngine::OpenSolution()
{
// Generate csproj file if it doesn't exist
if (!std::filesystem::exists(CSPROJ_PATH))
{
GenerateScriptsCsProjFile(CSPROJ_PATH);
}
// Open it
execProcessNoBlock
(
L"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe",
L"/Edit " + SHStringUtilities::StrToWstr(CSPROJ_PATH)
);
}
/*-----------------------------------------------------------------------------------*/
/* Event Handler Functions */
/*-----------------------------------------------------------------------------------*/
@ -611,6 +627,34 @@ namespace SHADE
}
DWORD SHScriptEngine::execProcess(const std::wstring& path, const std::wstring& args)
{
PROCESS_INFORMATION procInfo = execProcessNoBlock(path, args);
// Wait for execution to end
DWORD status;
while (true)
{
const auto EXEC_SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status);
if (!EXEC_SUCCESS)
{
auto err = GetLastError();
std::ostringstream oss;
oss << "[ScriptEngine] Failed to query process. Error code: " << std::hex << err
<< " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")";
throw std::runtime_error(oss.str());
}
// Break only if process ends
if (status != STILL_ACTIVE)
{
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
return status;
}
}
}
PROCESS_INFORMATION SHScriptEngine::execProcessNoBlock(const std::wstring& path, const std::wstring& args)
{
STARTUPINFOW startInfo;
PROCESS_INFORMATION procInfo;
@ -638,28 +682,7 @@ namespace SHADE
throw std::runtime_error(oss.str());
}
// Wait for execution to end
DWORD status;
while (true)
{
const auto EXEC_SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status);
if (!EXEC_SUCCESS)
{
auto err = GetLastError();
std::ostringstream oss;
oss << "[ScriptEngine] Failed to query process. Error code: " << std::hex << err
<< " (" << SHStringUtilities::GetWin32ErrorMessage(err) << ")";
throw std::runtime_error(oss.str());
}
// Break only if process ends
if (status != STILL_ACTIVE)
{
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
return status;
}
}
return procInfo;
}
std::wstring SHScriptEngine::generateBuildCommand(bool debug)

View File

@ -217,6 +217,10 @@ namespace SHADE
/// </summary>
/// <param name="path">File path to the generated file.</param>
void GenerateScriptsCsProjFile(const std::filesystem::path& path = CSPROJ_PATH) const;
/// <summary>
/// Opens the script solution in Visual Studio 2019.
/// </summary>
void OpenSolution();
private:
/*-----------------------------------------------------------------------------*/
@ -321,6 +325,7 @@ namespace SHADE
static bool fileExists(const std::filesystem::path& filePath);
static bool copyFile(const std::filesystem::path& from, const std::filesystem::path& to, const std::filesystem::copy_options options) noexcept;
static DWORD execProcess(const std::wstring& path, const std::wstring& args);
static PROCESS_INFORMATION execProcessNoBlock(const std::wstring& path, const std::wstring& args);
static std::wstring generateBuildCommand(bool debug);
};
}