Skip to content

Commit

Permalink
Improve behavior of Launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
durswd committed Mar 23, 2024
1 parent 02da29b commit 44bc175
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Tool/EffekseerLauncher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ if(APPLE)
elseif(WIN32)
cmake_policy(SET CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /SUBSYSTEM:WINDOWS")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /SUBSYSTEM:WINDOWS")

add_executable(EffekseerLauncher WIN32 src/main.cpp resources.rc)
add_executable(EffekseerLauncher src/main.cpp resources.rc)
endif()
90 changes: 63 additions & 27 deletions Tool/EffekseerLauncher/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,70 @@ void SetCurrentDir(const char* path)
#endif
}

FILE* Popen(const char* path, const char* mode)
{
#ifdef _WIN32
return _popen(path, mode);
#else
return popen(path, mode);
#endif
}

void Pclose(FILE* file)
struct Platform
{
#ifdef _WIN32
_pclose(file);
PROCESS_INFORMATION pi;
STARTUPINFO si;
Platform()
{
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
}

~Platform()
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

bool Execute(const char* cmd)
{
return CreateProcess(nullptr, (LPSTR)cmd, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi) == TRUE;
}

void Wait()
{
WaitForSingleObject(pi.hProcess, INFINITE);
}
};
#else
pclose(file);
struct Platform
{
FILE* fp = nullptr;
Platform() = default;
~Platform()
{
if (fp != nullptr)
{
pclose(fp);
}
}

bool Execute(const char* cmd)
{
fp = popen(cmd, "r");
if (fp == nullptr)
{
return false;
}
return true;
}

void Wait()
{
std::array<char, 256> buffer;
while (!feof(fp))
{
fgets(buffer.data(), buffer.size(), fp);
std::cout << buffer.data();
}

pclose(fp);
}
};
#endif
}

int mainLoop(int argc, char* argv[])
{
Expand All @@ -92,29 +139,18 @@ int mainLoop(int argc, char* argv[])
cmd = cmd + " \"" + argv[i] + "\"";
}

auto fp = Popen(cmd.c_str(), "r");
if (fp == nullptr)
Platform platform;
if (!platform.Execute(cmd.c_str()))
{
std::cout << "Failed to call " << cmd << std::endl;
return 1;
}

std::array<char, 256> buffer;

while (!feof(fp))
{
fgets(buffer.data(), buffer.size(), fp);
std::cout << buffer.data();
}

Pclose(fp);

std::cout << "Finished " << cmd << std::endl;

return 0;
}

#if defined(_WIN32)
#if defined(NDEBUG) && defined(_WIN32)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nShowCmd)
{
return mainLoop(__argc, __argv);
Expand Down

0 comments on commit 44bc175

Please sign in to comment.