mirror of https://github.com/AxioDL/metaforce.git
259 lines
8.4 KiB
C++
259 lines
8.4 KiB
C++
#ifndef BLENDERCONNECTION_HPP
|
|
#define BLENDERCONNECTION_HPP
|
|
|
|
#if _WIN32
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
#endif
|
|
#include <windows.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string>
|
|
#include <functional>
|
|
|
|
#include "HECL/HECL.hpp"
|
|
|
|
namespace HECL
|
|
{
|
|
|
|
extern LogVisor::LogModule BlenderLog;
|
|
extern class BlenderConnection* SharedBlenderConnection;
|
|
|
|
class BlenderConnection
|
|
{
|
|
bool m_lock = false;
|
|
#if _WIN32
|
|
HANDLE m_blenderProc;
|
|
#else
|
|
pid_t m_blenderProc;
|
|
#endif
|
|
int m_readpipe[2];
|
|
int m_writepipe[2];
|
|
SystemString m_loadedBlend;
|
|
std::string m_startupBlend;
|
|
size_t _readLine(char* buf, size_t bufSz);
|
|
size_t _writeLine(const char* buf);
|
|
size_t _readBuf(char* buf, size_t len);
|
|
size_t _writeBuf(const char* buf, size_t len);
|
|
void _closePipe();
|
|
public:
|
|
BlenderConnection(bool silenceBlender=false);
|
|
~BlenderConnection();
|
|
|
|
bool createBlend(const SystemString& path);
|
|
bool openBlend(const SystemString& path);
|
|
bool saveBlend();
|
|
void deleteBlend();
|
|
enum CookPlatform
|
|
{
|
|
CP_MODERN = 0,
|
|
CP_GX = 1,
|
|
};
|
|
bool cookBlend(std::function<char*(uint32_t)> bufGetter,
|
|
const std::string& expectedType,
|
|
const std::string& platform,
|
|
bool bigEndian=false);
|
|
|
|
class PyOutStream : public std::ostream
|
|
{
|
|
friend class BlenderConnection;
|
|
BlenderConnection* m_parent;
|
|
bool m_deleteOnError;
|
|
struct StreamBuf : std::streambuf
|
|
{
|
|
PyOutStream& m_parent;
|
|
std::string m_lineBuf;
|
|
bool m_deleteOnError;
|
|
StreamBuf(PyOutStream& parent, bool deleteOnError)
|
|
: m_parent(parent), m_deleteOnError(deleteOnError) {}
|
|
StreamBuf(const StreamBuf& other) = delete;
|
|
StreamBuf(StreamBuf&& other) = default;
|
|
int_type overflow(int_type ch)
|
|
{
|
|
if (!m_parent.m_parent || !m_parent.m_parent->m_lock)
|
|
BlenderLog.report(LogVisor::FatalError, "lock not held for PyOutStream writing");
|
|
if (ch != traits_type::eof() && ch != '\n' && ch != '\0')
|
|
{
|
|
m_lineBuf += char_type(ch);
|
|
return ch;
|
|
}
|
|
//printf("FLUSHING %s\n", m_lineBuf.c_str());
|
|
m_parent.m_parent->_writeLine(m_lineBuf.c_str());
|
|
char readBuf[16];
|
|
m_parent.m_parent->_readLine(readBuf, 16);
|
|
if (strcmp(readBuf, "OK"))
|
|
{
|
|
if (m_deleteOnError)
|
|
m_parent.m_parent->deleteBlend();
|
|
BlenderLog.report(LogVisor::FatalError, "error sending '%s' to blender", m_lineBuf.c_str());
|
|
}
|
|
m_lineBuf.clear();
|
|
return ch;
|
|
}
|
|
} m_sbuf;
|
|
PyOutStream(BlenderConnection* parent, bool deleteOnError)
|
|
: std::ostream(&m_sbuf),
|
|
m_parent(parent),
|
|
m_deleteOnError(deleteOnError),
|
|
m_sbuf(*this, deleteOnError)
|
|
{
|
|
m_parent->m_lock = true;
|
|
m_parent->_writeLine("PYBEGIN");
|
|
char readBuf[16];
|
|
m_parent->_readLine(readBuf, 16);
|
|
if (strcmp(readBuf, "READY"))
|
|
BlenderLog.report(LogVisor::FatalError, "unable to open PyOutStream with blender");
|
|
}
|
|
public:
|
|
PyOutStream(const PyOutStream& other) = delete;
|
|
PyOutStream(PyOutStream&& other)
|
|
: std::ostream(&m_sbuf), m_parent(other.m_parent), m_sbuf(std::move(other.m_sbuf))
|
|
{other.m_parent = nullptr;}
|
|
~PyOutStream() {close();}
|
|
void close()
|
|
{
|
|
if (m_parent && m_parent->m_lock)
|
|
{
|
|
m_parent->_writeLine("PYEND");
|
|
char readBuf[16];
|
|
m_parent->_readLine(readBuf, 16);
|
|
if (strcmp(readBuf, "DONE"))
|
|
BlenderLog.report(LogVisor::FatalError, "unable to close PyOutStream with blender");
|
|
m_parent->m_lock = false;
|
|
}
|
|
}
|
|
void format(const char* fmt, ...)
|
|
{
|
|
if (!m_parent || !m_parent->m_lock)
|
|
BlenderLog.report(LogVisor::FatalError, "lock not held for PyOutStream::format()");
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
char* result = nullptr;
|
|
#ifdef _WIN32
|
|
int length = _vscprintf(fmt, ap);
|
|
result = (char*)malloc(length);
|
|
vsnprintf(result, length, fmt, ap);
|
|
#else
|
|
int length = vasprintf(&result, fmt, ap);
|
|
#endif
|
|
va_end(ap);
|
|
if (length > 0)
|
|
this->write(result, length);
|
|
free(result);
|
|
}
|
|
void linkBlend(const std::string& target, const std::string& objName, bool link=true);
|
|
|
|
class ANIMOutStream
|
|
{
|
|
BlenderConnection* m_parent;
|
|
unsigned m_curCount = 0;
|
|
unsigned m_totalCount = 0;
|
|
bool m_inCurve = false;
|
|
public:
|
|
enum CurveType
|
|
{
|
|
CurveRotate,
|
|
CurveTranslate,
|
|
CurveScale
|
|
};
|
|
ANIMOutStream(BlenderConnection* parent)
|
|
: m_parent(parent)
|
|
{
|
|
m_parent->_writeLine("PYANIM");
|
|
char readBuf[16];
|
|
m_parent->_readLine(readBuf, 16);
|
|
if (strcmp(readBuf, "ANIMREADY"))
|
|
BlenderLog.report(LogVisor::FatalError, "unable to open ANIMOutStream");
|
|
}
|
|
~ANIMOutStream()
|
|
{
|
|
char tp = -1;
|
|
m_parent->_writeBuf(&tp, 1);
|
|
char readBuf[16];
|
|
m_parent->_readLine(readBuf, 16);
|
|
if (strcmp(readBuf, "ANIMDONE"))
|
|
BlenderLog.report(LogVisor::FatalError, "unable to close ANIMOutStream");
|
|
}
|
|
void changeCurve(CurveType type, unsigned crvIdx, unsigned keyCount)
|
|
{
|
|
if (m_curCount != m_totalCount)
|
|
BlenderLog.report(LogVisor::FatalError, "incomplete ANIMOutStream for change");
|
|
m_curCount = 0;
|
|
m_totalCount = keyCount;
|
|
char tp = char(type);
|
|
m_parent->_writeBuf(&tp, 1);
|
|
struct
|
|
{
|
|
uint32_t ci;
|
|
uint32_t kc;
|
|
} info = {uint32_t(crvIdx), uint32_t(keyCount)};
|
|
m_parent->_writeBuf(reinterpret_cast<const char*>(&info), 8);
|
|
m_inCurve = true;
|
|
}
|
|
void write(unsigned frame, float val)
|
|
{
|
|
if (!m_inCurve)
|
|
BlenderLog.report(LogVisor::FatalError, "changeCurve not called before write");
|
|
if (m_curCount < m_totalCount)
|
|
{
|
|
struct
|
|
{
|
|
uint32_t frm;
|
|
float val;
|
|
} key = {uint32_t(frame), val};
|
|
m_parent->_writeBuf(reinterpret_cast<const char*>(&key), 8);
|
|
++m_curCount;
|
|
}
|
|
else
|
|
BlenderLog.report(LogVisor::FatalError, "ANIMOutStream keyCount overflow");
|
|
}
|
|
};
|
|
ANIMOutStream beginANIMCurve()
|
|
{
|
|
return ANIMOutStream(m_parent);
|
|
}
|
|
};
|
|
inline PyOutStream beginPythonOut(bool deleteOnError=false)
|
|
{
|
|
if (m_lock)
|
|
BlenderLog.report(LogVisor::FatalError, "lock already held for BlenderConnection::beginPythonOut()");
|
|
return PyOutStream(this, deleteOnError);
|
|
}
|
|
|
|
void quitBlender();
|
|
|
|
static inline BlenderConnection& SharedConnection()
|
|
{
|
|
if (!SharedBlenderConnection)
|
|
SharedBlenderConnection = new BlenderConnection(HECL::VerbosityLevel?false:true);
|
|
return *SharedBlenderConnection;
|
|
}
|
|
|
|
inline void closeStream()
|
|
{
|
|
if (m_lock)
|
|
deleteBlend();
|
|
}
|
|
|
|
static inline void Shutdown()
|
|
{
|
|
if (SharedBlenderConnection)
|
|
{
|
|
SharedBlenderConnection->closeStream();
|
|
SharedBlenderConnection->quitBlender();
|
|
delete SharedBlenderConnection;
|
|
SharedBlenderConnection = nullptr;
|
|
BlenderLog.report(LogVisor::Info, "BlenderConnection Shutdown Successful");
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif // BLENDERCONNECTION_HPP
|