2020-04-11 12:42:49 +00:00
|
|
|
#ifndef PKMNLIB_FILEBYTECODESTREAM_HPP
|
|
|
|
#define PKMNLIB_FILEBYTECODESTREAM_HPP
|
|
|
|
#include <angelscript.h>
|
|
|
|
#include <cstdio>
|
2020-05-02 09:13:04 +00:00
|
|
|
#include "IPkmnBinaryStream.hpp"
|
2020-04-11 12:42:49 +00:00
|
|
|
|
2020-05-02 09:13:04 +00:00
|
|
|
class FileByteCodeStream : public IPkmnBinaryStream {
|
2020-04-11 12:42:49 +00:00
|
|
|
private:
|
2022-03-25 18:08:42 +00:00
|
|
|
FILE* non_null _file;
|
2020-05-02 09:13:04 +00:00
|
|
|
size_t _readPosition = 0;
|
2020-04-11 12:42:49 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-25 18:08:42 +00:00
|
|
|
explicit FileByteCodeStream(FILE* non_null file, size_t bound) : IPkmnBinaryStream(bound), _file(file) {}
|
2020-04-11 12:42:49 +00:00
|
|
|
|
2022-03-25 18:08:42 +00:00
|
|
|
int Write(const void* non_null ptr, asUINT size) override {
|
2020-04-11 12:42:49 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
return fwrite(ptr, size, 1, _file);
|
|
|
|
}
|
2022-03-25 18:08:42 +00:00
|
|
|
int Read(void* non_null ptr, asUINT size) override {
|
2020-04-11 12:42:49 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2020-05-02 09:13:04 +00:00
|
|
|
if (_readPosition + size >= _angelScriptBound) {
|
|
|
|
size = _angelScriptBound - _readPosition;
|
|
|
|
}
|
|
|
|
auto diff = fread(ptr, size, 1, _file);
|
|
|
|
_readPosition += diff;
|
|
|
|
return diff;
|
2020-04-11 12:42:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PKMNLIB_FILEBYTECODESTREAM_HPP
|