Support for saving compiled AngelScript to either file or RAM, so we can reuse it.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#ifndef PKMNLIB_FILEBYTECODESTREAM_HPP
|
||||
#define PKMNLIB_FILEBYTECODESTREAM_HPP
|
||||
#include <angelscript.h>
|
||||
#include <cstdio>
|
||||
|
||||
class FileByteCodeStream : public asIBinaryStream {
|
||||
private:
|
||||
FILE* _file;
|
||||
|
||||
public:
|
||||
explicit FileByteCodeStream(FILE* file) : _file(file) {}
|
||||
|
||||
int Write(const void* ptr, asUINT size) {
|
||||
if (size == 0)
|
||||
return 0;
|
||||
return fwrite(ptr, size, 1, _file);
|
||||
}
|
||||
int Read(void* ptr, asUINT size) {
|
||||
if (size == 0)
|
||||
return 0;
|
||||
return fread(ptr, size, 1, _file);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // PKMNLIB_FILEBYTECODESTREAM_HPP
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef PKMNLIB_MEMORYBYTECODESTREAM_HPP
|
||||
#define PKMNLIB_MEMORYBYTECODESTREAM_HPP
|
||||
#include <angelscript.h>
|
||||
#include <vector>
|
||||
|
||||
class MemoryByteCodeStream : public asIBinaryStream {
|
||||
private:
|
||||
uint8_t* _out;
|
||||
size_t _index = 0;
|
||||
size_t _size = 0;
|
||||
size_t _capacity = 0;
|
||||
|
||||
#define MEM_STEPS 256
|
||||
|
||||
public:
|
||||
MemoryByteCodeStream() : _out((uint8_t*)malloc(MEM_STEPS * sizeof(uint8_t) )), _capacity(MEM_STEPS){};
|
||||
MemoryByteCodeStream(uint8_t* in, size_t size) : _out(in), _size(size) {}
|
||||
|
||||
uint8_t* GetOut() const { return _out; }
|
||||
size_t GetWrittenSize() const { return _size; }
|
||||
|
||||
int Write(const void* ptr, asUINT size) final {
|
||||
if (size == 0)
|
||||
return 0;
|
||||
auto initialSize = _size;
|
||||
if (_size + size >= _capacity) {
|
||||
_capacity += MEM_STEPS;
|
||||
auto newLoc = realloc(_out, _capacity * sizeof(uint8_t));
|
||||
if (newLoc == nullptr) {
|
||||
throw CreatureException("Out of memory.");
|
||||
}
|
||||
_out = (uint8_t*)newLoc;
|
||||
}
|
||||
auto start = reinterpret_cast<const uint8_t*>(ptr);
|
||||
for (auto index = 0; index < size; index++) {
|
||||
_out[initialSize + index] = *(start + index);
|
||||
}
|
||||
_size += size;
|
||||
return size;
|
||||
}
|
||||
int Read(void* ptr, asUINT size) final {
|
||||
if (size == 0)
|
||||
return 0;
|
||||
auto start = reinterpret_cast<uint8_t*>(ptr);
|
||||
auto toRead = size;
|
||||
if (_index + toRead > _size) {
|
||||
toRead = _size - _index;
|
||||
}
|
||||
for (auto index = 0; index < toRead; index++) {
|
||||
*(start + index) = _out[_index + index];
|
||||
}
|
||||
_index += toRead;
|
||||
return toRead;
|
||||
}
|
||||
};
|
||||
#undef MEM_STEPS
|
||||
#endif // PKMNLIB_MEMORYBYTECODESTREAM_HPP
|
||||
Reference in New Issue
Block a user