34using namespace AngelScript;
43 r = engine->RegisterObjectType(
"file", 0, asOBJ_REF);
45 r = engine->RegisterObjectBehaviour(
"file", asBEHAVE_FACTORY,
"file @f()", asFUNCTION(
ScriptFile_Factory),
48 r = engine->RegisterObjectBehaviour(
"file", asBEHAVE_ADDREF,
"void f()", asMETHOD(
ScriptFileSafe, AddRef),
51 r = engine->RegisterObjectBehaviour(
"file", asBEHAVE_RELEASE,
"void f()", asMETHOD(
ScriptFileSafe, Release),
55 r = engine->RegisterObjectMethod(
"file",
"int open(const string &in, const string &in)",
58 r = engine->RegisterObjectMethod(
"file",
"int close()", asMETHOD(
ScriptFileSafe, Close), asCALL_THISCALL);
60 r = engine->RegisterObjectMethod(
"file",
"int getSize() const", asMETHOD(
ScriptFileSafe, GetSize), asCALL_THISCALL);
62 r = engine->RegisterObjectMethod(
"file",
"bool isEndOfFile() const", asMETHOD(
ScriptFileSafe, IsEOF),
65 r = engine->RegisterObjectMethod(
"file",
"int readString(uint, string &out)", asMETHOD(
ScriptFileSafe, ReadString),
68 r = engine->RegisterObjectMethod(
"file",
"int readLine(string &out)", asMETHOD(
ScriptFileSafe, ReadLine),
71 r = engine->RegisterObjectMethod(
"file",
"int64 readInt(uint)", asMETHOD(
ScriptFileSafe, ReadInt), asCALL_THISCALL);
73 r = engine->RegisterObjectMethod(
"file",
"uint64 readUInt(uint)", asMETHOD(
ScriptFileSafe, ReadUInt),
76 r = engine->RegisterObjectMethod(
"file",
"float readFloat()", asMETHOD(
ScriptFileSafe, ReadFloat), asCALL_THISCALL);
78 r = engine->RegisterObjectMethod(
"file",
"double readDouble()", asMETHOD(
ScriptFileSafe, ReadDouble),
82 r = engine->RegisterObjectMethod(
"file",
"int writeString(const string &in)", asMETHOD(
ScriptFileSafe, WriteString),
85 r = engine->RegisterObjectMethod(
"file",
"int writeInt(int64, uint)", asMETHOD(
ScriptFileSafe, WriteInt),
88 r = engine->RegisterObjectMethod(
"file",
"int writeUInt(uint64, uint)", asMETHOD(
ScriptFileSafe, WriteUInt),
91 r = engine->RegisterObjectMethod(
"file",
"int writeFloat(float)", asMETHOD(
ScriptFileSafe, WriteFloat),
94 r = engine->RegisterObjectMethod(
"file",
"int writeDouble(double)", asMETHOD(
ScriptFileSafe, WriteDouble),
98 r = engine->RegisterObjectMethod(
"file",
"int getPos() const", asMETHOD(
ScriptFileSafe, GetPos), asCALL_THISCALL);
100 r = engine->RegisterObjectMethod(
"file",
"int setPos(int)", asMETHOD(
ScriptFileSafe, SetPos), asCALL_THISCALL);
102 r = engine->RegisterObjectMethod(
"file",
"int movePos(int)", asMETHOD(
ScriptFileSafe, MovePos), asCALL_THISCALL);
134 if (mode !=
"r" && mode !=
"w" && mode !=
"a")
145 std::string myFilename = filename;
148 if (myFilename.length() > 7 && myFilename.substr(myFilename.length() - 7, 7) ==
".asdata")
149 myFilename = myFilename.substr(0, myFilename.length() - 7);
152 std::string allowedChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
153 for (std::string::iterator it = myFilename.begin(); it < myFilename.end(); ++it) {
154 if (allowedChars.find(*it) == std::string::npos)
218 size_t size = (int)
m_stream->read(&str[0], length);
221 return static_cast<int>(size);
229 size_t count =
m_stream->readLine(buf, 2000);
230 str.assign(buf, count);
232 return static_cast<int>(count);
239 if (bytes > 8) bytes = 8;
240 if (bytes == 0)
return 0;
242 unsigned char buf[8];
243 size_t r =
m_stream->read(buf, bytes);
244 if (r == 0)
return 0;
249 for (; n < bytes; n++)
250 val |= asQWORD(buf[n]) << (n * 8);
253 val |= asQWORD(0xFF) << (n * 8);
263 if (bytes > 8) bytes = 8;
264 if (bytes == 0)
return 0;
266 unsigned char buf[8];
267 size_t r =
m_stream->read(buf, bytes);
268 if (r == 0)
return 0;
273 for (; n < bytes; n++)
274 val |= asQWORD(buf[n]) << (n * 8);
284 unsigned char buf[4];
286 if (r == 0)
return 0;
292 val |= asUINT(buf[n]) << (n * 8);
295 return *
reinterpret_cast<float *
>(&val);
302 unsigned char buf[8];
304 if (r == 0)
return 0;
310 val |= asQWORD(buf[n]) << (n * 8);
313 return *
reinterpret_cast<double *
>(&val);
328 size_t r =
m_stream->write(&str[0], str.length());
336 unsigned char buf[8];
338 for (
unsigned int n = 0; n < bytes; n++)
339 buf[n] = (val >> (n * 8)) & 0xFF;
342 size_t r =
m_stream->write(&buf, bytes);
350 unsigned char buf[8];
352 for (
unsigned int n = 0; n < bytes; n++)
353 buf[n] = (val >> (n * 8)) & 0xFF;
356 size_t r =
m_stream->write(&buf, bytes);
364 unsigned char buf[4];
365 asUINT val = *
reinterpret_cast<asUINT *
>(&f);
367 for (
unsigned int n = 0; n < 4; n++)
368 buf[n] = (val >> (n * 8)) & 0xFF;
371 size_t r =
m_stream->write(&buf, 4);
379 unsigned char buf[8];
380 asQWORD val = *
reinterpret_cast<asQWORD *
>(&d);
382 for (
unsigned int n = 0; n < 8; n++)
383 buf[n] = (val >> (n * 8)) & 0xFF;
386 size_t r =
m_stream->write(&buf, 8);
#define RGN_SERVER_SCRIPTS
ScriptFileSafe * ScriptFile_Factory()
Ogre::DataStreamPtr m_stream
int WriteInt(AngelScript::asINT64 v, AngelScript::asUINT bytes)
AngelScript::asQWORD ReadUInt(AngelScript::asUINT bytes)
int WriteUInt(AngelScript::asQWORD v, AngelScript::asUINT bytes)
int Open(const std::string &filename, const std::string &mode)
int WriteString(const std::string &str)
int WriteDouble(double v)
int ReadLine(std::string &str)
AngelScript::asINT64 ReadInt(AngelScript::asUINT bytes)
int ReadString(unsigned int length, std::string &str)
void HandleGenericException(const std::string &from, BitMask_t flags)
void RegisterScriptFile_Native(AngelScript::asIScriptEngine *engine)