metaforce/hecl/blender/BlenderConnection.hpp

95 lines
2.5 KiB
C++
Raw Normal View History

2015-07-22 19:14:50 +00:00
#ifndef BLENDERCONNECTION_HPP
#define BLENDERCONNECTION_HPP
2015-05-24 04:51:16 +00:00
#if _WIN32
#define _WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#else
#include <unistd.h>
2015-05-24 04:51:16 +00:00
#endif
2015-07-22 19:14:50 +00:00
#include <stdint.h>
2015-05-24 22:19:28 +00:00
#include <string>
#include <functional>
2015-07-25 23:01:02 +00:00
#include <mutex>
2015-05-24 22:19:28 +00:00
2015-07-22 19:14:50 +00:00
class BlenderConnection
{
2015-07-25 23:01:02 +00:00
std::mutex m_lock;
2015-05-24 04:51:16 +00:00
#if _WIN32
HANDLE m_blenderProc;
2015-07-22 19:14:50 +00:00
HANDLE m_readpipe[2];
HANDLE m_writepipe[2];
2015-05-24 04:51:16 +00:00
#else
pid_t m_blenderProc;
2015-05-24 04:51:16 +00:00
int m_readpipe[2];
int m_writepipe[2];
#endif
2015-05-24 22:19:28 +00:00
std::string m_loadedBlend;
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:
2015-07-22 19:14:50 +00:00
BlenderConnection(bool silenceBlender=false);
~BlenderConnection();
2015-05-24 04:51:16 +00:00
2015-05-24 22:19:28 +00:00
bool openBlend(const std::string& path);
enum CookPlatform
{
CP_MODERN = 0,
CP_GX = 1,
};
2015-05-26 04:42:20 +00:00
bool cookBlend(std::function<char*(uint32_t)> bufGetter,
2015-05-24 22:19:28 +00:00
const std::string& expectedType,
const std::string& platform,
bool bigEndian=false);
2015-07-25 23:01:02 +00:00
class PyOutStream : public std::ostream
{
friend class BlenderConnection;
std::unique_lock<std::mutex> m_lk;
BlenderConnection* m_parent;
struct StreamBuf : std::streambuf
{
BlenderConnection* m_parent;
StreamBuf(BlenderConnection* parent) : m_parent(parent) {}
StreamBuf(const StreamBuf& other) = delete;
StreamBuf(StreamBuf&& other) = default;
int_type overflow(int_type ch)
{
if (ch != traits_type::eof())
{
char_type chi = ch;
m_parent->_writeBuf(&chi, 1);
}
return ch;
}
} m_sbuf;
PyOutStream(BlenderConnection* parent)
: m_lk(parent->m_lock), m_parent(parent), m_sbuf(parent), std::ostream(&m_sbuf)
{
m_parent->_writeBuf("\033PYBEGIN\n", 9);
}
public:
PyOutStream(const PyOutStream& other) = delete;
PyOutStream(PyOutStream&& other)
: m_lk(std::move(other.m_lk)), m_parent(other.m_parent), m_sbuf(std::move(other.m_sbuf))
{other.m_parent = nullptr;}
~PyOutStream()
{
if (m_parent)
m_parent->_writeBuf("\033PYEND\n", 7);
}
};
inline PyOutStream beginPythonOut()
{
return PyOutStream(this);
}
2015-05-24 04:51:16 +00:00
void quitBlender();
};
2015-07-22 19:14:50 +00:00
#endif // BLENDERCONNECTION_HPP