mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-07-11 06:05:52 +00:00
Merge pull request #14 from lioncash/string
Connection: Replace std::string _writeStr overload with std::string_view
This commit is contained in:
commit
120ed20bfa
@ -710,8 +710,7 @@ class Connection {
|
||||
uint32_t _readStr(char* buf, uint32_t bufSz);
|
||||
uint32_t _writeStr(const char* str, uint32_t len, int wpipe);
|
||||
uint32_t _writeStr(const char* str, uint32_t len) { return _writeStr(str, len, m_writepipe[1]); }
|
||||
uint32_t _writeStr(const char* str) { return _writeStr(str, strlen(str)); }
|
||||
uint32_t _writeStr(const std::string& str) { return _writeStr(str.c_str(), str.size()); }
|
||||
uint32_t _writeStr(std::string_view view) { return _writeStr(view.data(), view.size()); }
|
||||
size_t _readBuf(void* buf, size_t len);
|
||||
size_t _writeBuf(const void* buf, size_t len);
|
||||
void _closePipe();
|
||||
|
@ -107,7 +107,7 @@ static int Write(int fd, const void* buf, size_t size) {
|
||||
|
||||
uint32_t Connection::_readStr(char* buf, uint32_t bufSz) {
|
||||
uint32_t readLen;
|
||||
int ret = Read(m_readpipe[0], &readLen, 4);
|
||||
int ret = Read(m_readpipe[0], &readLen, sizeof(readLen));
|
||||
if (ret < 4) {
|
||||
BlenderLog.report(logvisor::Error, fmt("Pipe error {} {}"), ret, strerror(errno));
|
||||
_blenderDied();
|
||||
@ -124,8 +124,11 @@ uint32_t Connection::_readStr(char* buf, uint32_t bufSz) {
|
||||
if (ret < 0) {
|
||||
BlenderLog.report(logvisor::Fatal, fmt("{}"), strerror(errno));
|
||||
return 0;
|
||||
} else if (readLen >= 9) {
|
||||
if (!memcmp(buf, "EXCEPTION", std::min(readLen, uint32_t(9)))) {
|
||||
}
|
||||
|
||||
constexpr std::string_view exception_str{"EXCEPTION"};
|
||||
if (readLen >= exception_str.size()) {
|
||||
if (exception_str.compare(0, exception_str.size(), buf) == 0) {
|
||||
_blenderDied();
|
||||
return 0;
|
||||
}
|
||||
@ -136,54 +139,75 @@ uint32_t Connection::_readStr(char* buf, uint32_t bufSz) {
|
||||
}
|
||||
|
||||
uint32_t Connection::_writeStr(const char* buf, uint32_t len, int wpipe) {
|
||||
int ret, nlerr;
|
||||
nlerr = Write(wpipe, &len, 4);
|
||||
if (nlerr < 4)
|
||||
goto err;
|
||||
ret = Write(wpipe, buf, len);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
return (uint32_t)ret;
|
||||
err:
|
||||
const auto error = [this] {
|
||||
_blenderDied();
|
||||
return 0;
|
||||
return 0U;
|
||||
};
|
||||
|
||||
const int nlerr = Write(wpipe, &len, 4);
|
||||
if (nlerr < 4) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const int ret = Write(wpipe, buf, len);
|
||||
if (ret < 0) {
|
||||
return error();
|
||||
}
|
||||
|
||||
return static_cast<uint32_t>(ret);
|
||||
}
|
||||
|
||||
size_t Connection::_readBuf(void* buf, size_t len) {
|
||||
uint8_t* cBuf = reinterpret_cast<uint8_t*>(buf);
|
||||
size_t readLen = 0;
|
||||
do {
|
||||
int ret = Read(m_readpipe[0], cBuf, len);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
if (len >= 9)
|
||||
if (!memcmp((char*)cBuf, "EXCEPTION", std::min(len, size_t(9))))
|
||||
const auto error = [this] {
|
||||
_blenderDied();
|
||||
return 0U;
|
||||
};
|
||||
|
||||
auto* cBuf = static_cast<uint8_t*>(buf);
|
||||
size_t readLen = 0;
|
||||
|
||||
do {
|
||||
const int ret = Read(m_readpipe[0], cBuf, len);
|
||||
if (ret < 0) {
|
||||
return error();
|
||||
}
|
||||
|
||||
constexpr std::string_view exception_str{"EXCEPTION"};
|
||||
if (len >= exception_str.size()) {
|
||||
if (exception_str.compare(0, exception_str.size(), static_cast<char*>(buf)) == 0) {
|
||||
_blenderDied();
|
||||
}
|
||||
}
|
||||
|
||||
readLen += ret;
|
||||
cBuf += ret;
|
||||
len -= ret;
|
||||
} while (len);
|
||||
} while (len != 0);
|
||||
|
||||
return readLen;
|
||||
err:
|
||||
_blenderDied();
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t Connection::_writeBuf(const void* buf, size_t len) {
|
||||
const uint8_t* cBuf = reinterpret_cast<const uint8_t*>(buf);
|
||||
const auto error = [this] {
|
||||
_blenderDied();
|
||||
return 0U;
|
||||
};
|
||||
|
||||
const auto* cBuf = static_cast<const uint8_t*>(buf);
|
||||
size_t writeLen = 0;
|
||||
|
||||
do {
|
||||
int ret = Write(m_writepipe[1], cBuf, len);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
const int ret = Write(m_writepipe[1], cBuf, len);
|
||||
if (ret < 0) {
|
||||
return error();
|
||||
}
|
||||
|
||||
writeLen += ret;
|
||||
cBuf += ret;
|
||||
len -= ret;
|
||||
} while (len);
|
||||
} while (len != 0);
|
||||
|
||||
return writeLen;
|
||||
err:
|
||||
_blenderDied();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Connection::_closePipe() {
|
||||
@ -315,8 +339,8 @@ Connection::Connection(int verbosityLevel) {
|
||||
}
|
||||
}
|
||||
|
||||
std::wstring cmdLine = fmt::format(fmt(L" --background -P \"{}\" -- {} {} {} \"{}\""),
|
||||
blenderShellPath, uintptr_t(writehandle), uintptr_t(readhandle), verbosityLevel, blenderAddonPath);
|
||||
std::wstring cmdLine = fmt::format(fmt(L" --background -P \"{}\" -- {} {} {} \"{}\""), blenderShellPath,
|
||||
uintptr_t(writehandle), uintptr_t(readhandle), verbosityLevel, blenderAddonPath);
|
||||
|
||||
STARTUPINFO sinfo = {sizeof(STARTUPINFO)};
|
||||
HANDLE nulHandle = CreateFileW(L"nul", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sattrs, OPEN_EXISTING,
|
||||
@ -331,8 +355,8 @@ Connection::Connection(int verbosityLevel) {
|
||||
sinfo.hStdOutput = consoleOutWrite;
|
||||
}
|
||||
|
||||
if (!CreateProcessW(blenderBin, const_cast<wchar_t*>(cmdLine.c_str()), NULL, NULL, TRUE,
|
||||
NORMAL_PRIORITY_CLASS, NULL, NULL, &sinfo, &m_pinfo)) {
|
||||
if (!CreateProcessW(blenderBin, const_cast<wchar_t*>(cmdLine.c_str()), NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
|
||||
NULL, NULL, &sinfo, &m_pinfo)) {
|
||||
LPWSTR messageBuffer = nullptr;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
|
||||
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, NULL);
|
||||
@ -396,8 +420,8 @@ Connection::Connection(int verbosityLevel) {
|
||||
|
||||
/* Try user-specified blender first */
|
||||
if (blenderBin) {
|
||||
execlp(blenderBin, blenderBin, "--background", "-P", blenderShellPath.c_str(), "--",
|
||||
readfds.c_str(), writefds.c_str(), vLevel.c_str(), blenderAddonPath.c_str(), NULL);
|
||||
execlp(blenderBin, blenderBin, "--background", "-P", blenderShellPath.c_str(), "--", readfds.c_str(),
|
||||
writefds.c_str(), vLevel.c_str(), blenderAddonPath.c_str(), NULL);
|
||||
if (errno != ENOENT) {
|
||||
errbuf = fmt::format(fmt("NOLAUNCH {}"), strerror(errno));
|
||||
_writeStr(errbuf.c_str(), errbuf.size(), m_readpipe[1]);
|
||||
@ -414,8 +438,8 @@ Connection::Connection(int verbosityLevel) {
|
||||
steamBlender += "/blender";
|
||||
#endif
|
||||
blenderBin = steamBlender.c_str();
|
||||
execlp(blenderBin, blenderBin, "--background", "-P", blenderShellPath.c_str(), "--",
|
||||
readfds.c_str(), writefds.c_str(), vLevel.c_str(), blenderAddonPath.c_str(), NULL);
|
||||
execlp(blenderBin, blenderBin, "--background", "-P", blenderShellPath.c_str(), "--", readfds.c_str(),
|
||||
writefds.c_str(), vLevel.c_str(), blenderAddonPath.c_str(), NULL);
|
||||
if (errno != ENOENT) {
|
||||
errbuf = fmt::format(fmt("NOLAUNCH {}"), strerror(errno));
|
||||
_writeStr(errbuf.c_str(), errbuf.size(), m_readpipe[1]);
|
||||
@ -446,8 +470,8 @@ Connection::Connection(int verbosityLevel) {
|
||||
m_errPath = hecl::SystemString(TMPDIR) +
|
||||
fmt::format(fmt(_SYS_STR("/hecl_{:016X}.derp")), (unsigned long long)m_pinfo.dwProcessId);
|
||||
#else
|
||||
m_errPath =
|
||||
hecl::SystemString(TMPDIR) + fmt::format(fmt(_SYS_STR("/hecl_{:016X}.derp")), (unsigned long long)m_blenderProc);
|
||||
m_errPath = hecl::SystemString(TMPDIR) +
|
||||
fmt::format(fmt(_SYS_STR("/hecl_{:016X}.derp")), (unsigned long long)m_blenderProc);
|
||||
#endif
|
||||
hecl::Unlink(m_errPath.c_str());
|
||||
|
||||
@ -628,8 +652,7 @@ void PyOutStream::close() {
|
||||
}
|
||||
|
||||
void PyOutStream::linkBlend(const char* target, const char* objName, bool link) {
|
||||
format(fmt(
|
||||
"if '{}' not in bpy.data.scenes:\n"
|
||||
format(fmt("if '{}' not in bpy.data.scenes:\n"
|
||||
" with bpy.data.libraries.load('''{}''', link={}, relative=True) as (data_from, data_to):\n"
|
||||
" data_to.scenes = data_from.scenes\n"
|
||||
" obj_scene = None\n"
|
||||
@ -651,8 +674,7 @@ void PyOutStream::linkBlend(const char* target, const char* objName, bool link)
|
||||
|
||||
void PyOutStream::linkBackground(const char* target, const char* sceneName) {
|
||||
if (!sceneName) {
|
||||
format(fmt(
|
||||
"with bpy.data.libraries.load('''{}''', link=True, relative=True) as (data_from, data_to):\n"
|
||||
format(fmt("with bpy.data.libraries.load('''{}''', link=True, relative=True) as (data_from, data_to):\n"
|
||||
" data_to.scenes = data_from.scenes\n"
|
||||
"obj_scene = None\n"
|
||||
"for scene in data_to.scenes:\n"
|
||||
@ -664,8 +686,7 @@ void PyOutStream::linkBackground(const char* target, const char* sceneName) {
|
||||
"bpy.context.scene.background_set = obj_scene\n"),
|
||||
target, target);
|
||||
} else {
|
||||
format(fmt(
|
||||
"if '{}' not in bpy.data.scenes:\n"
|
||||
format(fmt("if '{}' not in bpy.data.scenes:\n"
|
||||
" with bpy.data.libraries.load('''{}''', link=True, relative=True) as (data_from, data_to):\n"
|
||||
" data_to.scenes = data_from.scenes\n"
|
||||
" obj_scene = None\n"
|
||||
@ -684,8 +705,7 @@ void PyOutStream::linkBackground(const char* target, const char* sceneName) {
|
||||
void PyOutStream::AABBToBMesh(const atVec3f& min, const atVec3f& max) {
|
||||
athena::simd_floats minf(min.simd);
|
||||
athena::simd_floats maxf(max.simd);
|
||||
format(fmt(
|
||||
"bm = bmesh.new()\n"
|
||||
format(fmt("bm = bmesh.new()\n"
|
||||
"bm.verts.new(({},{},{}))\n"
|
||||
"bm.verts.new(({},{},{}))\n"
|
||||
"bm.verts.new(({},{},{}))\n"
|
||||
@ -1434,9 +1454,10 @@ Action::Action(Connection& conn) {
|
||||
subtypeAABBs.emplace_back();
|
||||
subtypeAABBs.back().first.read(conn);
|
||||
subtypeAABBs.back().second.read(conn);
|
||||
//printf("AABB %s %d (%f %f %f) (%f %f %f)\n", name.c_str(), i,
|
||||
// float(subtypeAABBs.back().first.val.simd[0]), float(subtypeAABBs.back().first.val.simd[1]), float(subtypeAABBs.back().first.val.simd[2]),
|
||||
// float(subtypeAABBs.back().second.val.simd[0]), float(subtypeAABBs.back().second.val.simd[1]), float(subtypeAABBs.back().second.val.simd[2]));
|
||||
// printf("AABB %s %d (%f %f %f) (%f %f %f)\n", name.c_str(), i,
|
||||
// float(subtypeAABBs.back().first.val.simd[0]), float(subtypeAABBs.back().first.val.simd[1]),
|
||||
// float(subtypeAABBs.back().first.val.simd[2]), float(subtypeAABBs.back().second.val.simd[0]),
|
||||
// float(subtypeAABBs.back().second.val.simd[1]), float(subtypeAABBs.back().second.val.simd[2]));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user