Skip to content

Commit

Permalink
Register iostreams.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Oct 7, 2024
1 parent bbd1ca7 commit 268d5c5
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ set(NGT_SOURCES
"SRC/scriptstdstring/scriptstdwstring.cpp"
"SRC/print_func/print_func.cpp"
"SRC/obfuscate.h"
"SRC/IOStreams.cpp"
)
if(WIN32)
set(NGT_SOURCES ${NGT_SOURCES} GUI/GUI.cpp)
Expand Down
5 changes: 5 additions & 0 deletions Release/Tests/cout.ngt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void main(){
string r;
cin>>r;
cout<<r;
}
6 changes: 6 additions & 0 deletions Release/Tests/file.ngt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
file f;
void main(){
f.open("Test", "w");
f<<"W";
f.close();
}
296 changes: 294 additions & 2 deletions SRC/IOStreams.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,301 @@
#include "IOStreams.h"
#include <angelscript.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <string>
#undef GetObject

using namespace std;


class CScriptIOStream {
public:
std::ostream* outputStream;
std::istream* inputStream;

CScriptIOStream() : ref(1), outputStream(nullptr), inputStream(nullptr) {}

void AddRef() {
ref += 1;
}

void Release() {
if (--ref < 1) {
delete this;
}
}

void write(const string& data) {
*outputStream << data;
}

string read() {
string data;
*inputStream >> data;
return data;
}

string getline() {
string data;
std::getline(*inputStream, data);
return data;
}

void clear() {
inputStream->clear();
outputStream->clear();
}

void setOutputStream(std::ostream* stream) {
outputStream = stream;
}

void setInputStream(std::istream* stream) {
inputStream = stream;
}


void seekg(size_t pos) {
inputStream->seekg(static_cast<std::streampos>(pos));
}

size_t tellg() {
return static_cast<size_t>(inputStream->tellg());
}

void seekp(size_t pos) {
outputStream->seekp(static_cast<std::streampos>(pos));
}

size_t tellp() {
return static_cast<size_t>(outputStream->tellp());
}


private:
mutable int ref;
};

void WriteGeneric(asIScriptGeneric* gen) {
CScriptIOStream* stream = (CScriptIOStream*)gen->GetObject();
void* value = gen->GetArgAddress(0);
int tid = gen->GetArgTypeId(0);
std::ostream* ostr = stream->outputStream;
switch (tid) {
case asTYPEID_BOOL:
{
bool val = *reinterpret_cast<const bool*>(value);
*ostr << val;
break;
}
case asTYPEID_FLOAT:
{
float val = *reinterpret_cast<const float*>(value);
*ostr << val;
break;
}
case asTYPEID_DOUBLE:
{
double val = *reinterpret_cast<const double*> (value);
*ostr << val;
break;
}
case asTYPEID_INT8:
{
int8_t val = *reinterpret_cast<const int8_t*>(value);
*ostr << val;
break;
}
case asTYPEID_INT16:
{
int16_t val = *reinterpret_cast<const int16_t*>(value);
*ostr << val;
break;
}
case asTYPEID_INT32:
{
int32_t val = *reinterpret_cast<const int32_t*>(value);
*ostr << val;
break;
}
case asTYPEID_INT64:
{
int64_t val = *reinterpret_cast<const int64_t*>(value);
*ostr << val;
break;
}
case asTYPEID_UINT8:
{
uint8_t val = *reinterpret_cast<const uint8_t*>(value);
*ostr << val;
break;
}
case asTYPEID_UINT16:
{
uint16_t val = *reinterpret_cast<const uint16_t*>(value);
*ostr << val;
break;
}
case asTYPEID_UINT32:
{
uint32_t val = *reinterpret_cast<const uint32_t*>(value);
*ostr << val;
break;
}
case asTYPEID_UINT64:
{
uint64_t val = *reinterpret_cast<const uint64_t*>(value);
*ostr << val;
break;
}
case 67108876:
{
const string& str = *static_cast<const string*>(value);
*ostr << str;
break;
}

default:
*ostr << value;
break;
}
gen->SetReturnObject(gen->GetObject());
}

void ReadGeneric(asIScriptGeneric* gen) {
CScriptIOStream* stream = (CScriptIOStream*)gen->GetObject();
void* value = gen->GetArgAddress(0);
int tid = gen->GetArgTypeId(0);
std::istream* istr = stream->inputStream;
switch (tid) {
case asTYPEID_BOOL: {
bool val;
*istr >> val;
*reinterpret_cast<bool*>(value) = val;
break;
}
case asTYPEID_FLOAT: {
float val;
*istr >> val;
*reinterpret_cast<float*>(value) = val;
break;
}
case asTYPEID_DOUBLE: {
double val;
*istr >> val;
*reinterpret_cast<double*>(value) = val;
break;
}
case asTYPEID_INT8: {
int8_t val;
*istr >> val;
*reinterpret_cast<int8_t*>(value) = val;
break;
}
case asTYPEID_INT16: {
int16_t val;
*istr >> val;
*reinterpret_cast<int16_t*>(value) = val;
break;
}
case asTYPEID_INT32: {
int32_t val;
*istr >> val;
*reinterpret_cast<int32_t*>(value) = val;
break;
}
case asTYPEID_INT64: {
int64_t val;
*istr >> val;
*reinterpret_cast<int64_t*>(value) = val;
break;
}
case asTYPEID_UINT8: {
uint8_t val;
*istr >> val;
*reinterpret_cast<uint8_t*>(value) = val;
break;
}
case asTYPEID_UINT16: {
uint16_t val;
*istr >> val;
*reinterpret_cast<uint16_t*>(value) = val;
break;
}
case asTYPEID_UINT32: {
uint32_t val;
*istr >> val;
*reinterpret_cast<uint32_t*>(value) = val;
break;
}
case asTYPEID_UINT64: {
uint64_t val;
*istr >> val;
*reinterpret_cast<uint64_t*>(value) = val;
break;
}
case 67108876: { // Assuming this is the type ID for std::string
std::string str;
*istr >> str; // Read into a temporary string
*reinterpret_cast<std::string*>(value) = str; // Assign to the provided address
break;
}
default: {
break;
}
}

gen->SetReturnObject(gen->GetObject());
}



// Factory function to create a new CScriptIOStream for the specified streams
CScriptIOStream* createStream(std::ostream* outStream, std::istream* inStream) {
CScriptIOStream* strm = new CScriptIOStream();
strm->setOutputStream(outStream);
strm->setInputStream(inStream);
return strm;
}

CScriptIOStream* get_cout() {
return createStream(&std::cout, nullptr);
}

CScriptIOStream* get_cin() {
return createStream(nullptr, &std::cin);
}

CScriptIOStream* get_cerr() {
return createStream(&std::cerr, nullptr);
}

CScriptIOStream* IostreamFactory() {
return new CScriptIOStream;
}

void RegisterScriptIOStreams(asIScriptEngine* engine) {
engine->RegisterObjectType("iostream", sizeof(CScriptIOStream), asOBJ_REF);
engine->RegisterObjectBehaviour("iostream", asBEHAVE_FACTORY, "iostream@ io()", asFUNCTION(IostreamFactory), asCALL_CDECL);

engine->RegisterObjectBehaviour("iostream", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptIOStream, AddRef), asCALL_THISCALL);
engine->RegisterObjectBehaviour("iostream", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptIOStream, Release), asCALL_THISCALL);

engine->RegisterObjectMethod("iostream", "void write(const string &in)", asMETHOD(CScriptIOStream, write), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "string read()", asMETHOD(CScriptIOStream, read), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "string getline()", asMETHOD(CScriptIOStream, getline), asCALL_THISCALL);

engine->RegisterObjectMethod("iostream", "void clear()", asMETHOD(CScriptIOStream, clear), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "void seekg(size_t)", asMETHOD(CScriptIOStream, seekg), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "size_t tellg()", asMETHOD(CScriptIOStream, tellg), asCALL_THISCALL);

engine->RegisterObjectMethod("iostream", "void seekp(size_t)", asMETHOD(CScriptIOStream, seekp), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "size_t tellp()", asMETHOD(CScriptIOStream, tellp), asCALL_THISCALL);
engine->RegisterObjectMethod("iostream", "iostream& opShl(?&in)", asFUNCTION(WriteGeneric), asCALL_GENERIC);
engine->RegisterObjectMethod("iostream", "iostream& opShr(?&out)", asFUNCTION(ReadGeneric), asCALL_GENERIC);


}
engine->RegisterGlobalFunction("iostream@ get_cout()property", asFUNCTION(get_cout), asCALL_CDECL);
engine->RegisterGlobalFunction("iostream@ get_cin()property", asFUNCTION(get_cin), asCALL_CDECL);
engine->RegisterGlobalFunction("iostream@ get_cerr()property", asFUNCTION(get_cerr), asCALL_CDECL);
}
2 changes: 2 additions & 0 deletions SRC/ngtreg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "autowrapper/aswrappedcall.h"
#include "IOStreams.h"
#include "MemoryStream.h"
#include "ngtreg.h"
#include "obfuscate.h"
Expand Down Expand Up @@ -423,6 +424,7 @@ void RegisterFunctions(asIScriptEngine* engine)
engine->RegisterGlobalFunction("void unicode_convert(const wstring &in, string &out)", asFUNCTIONPR(Poco::UnicodeConverter::convert, (const std::wstring&, std::string&), void), asCALL_CDECL);
register_pack(engine);
register_sound(engine);
RegisterScriptIOStreams(engine);
RegisterMemstream(engine);
AS_BEGIN(engine, "scripting");
RegisterScripting(engine);
Expand Down

0 comments on commit 268d5c5

Please sign in to comment.