//----------------------------------------------------------------// size_t USStream::WriteStream ( USStream& source, size_t size ) { if ( !( source.GetCaps () & CAN_READ )) return 0; if ( !( this->GetCaps () & CAN_WRITE )) return 0; u8 buffer [ LOCAL_BUFFER ]; size_t readSize = 0; size_t total = 0; do { if (( total + LOCAL_BUFFER ) > size ) { readSize = source.ReadBytes ( buffer, size - total ); } else { readSize = source.ReadBytes ( buffer, LOCAL_BUFFER ); } if ( readSize ) { total += this->WriteBytes ( buffer, readSize ); } } while ( readSize == LOCAL_BUFFER ); return total; }
//----------------------------------------------------------------// void USLuaSerializer::WriteDecls ( USStream& stream ) { stream.Print ( "--Declaring Objects\n" ); stream.Print ( "local objects = {\n\n" ); this->WriteTableDecls ( stream ); this->WriteInstanceDecls ( stream ); stream.Print ( "}\n\n" ); }
//----------------------------------------------------------------// void USLuaSerializer::WriteTableDecls ( USStream& stream ) { if ( !this->mTableMap.size ()) return; stream.Print ( "\t--Declaring Tables\n" ); TableMapIt tableIt = this->mTableMap.begin (); for ( ; tableIt != this->mTableMap.end (); ++tableIt ) { uintptr tableID = tableIt->first; stream.Print ( "\t[ 0x%08X ] = {},\n", tableID ); } stream.Print ( "\n" ); }
//----------------------------------------------------------------// u32 USLuaSerializer::WriteTableInitializer ( USStream& stream, USLuaState& state, int idx, cc8* prefix ) { u32 count = 0; u32 itr = state.PushTableItr ( idx ); while ( state.TableItrNext ( itr )) { switch ( lua_type ( state, -2 )) { case LUA_TSTRING: { stream.Print ( "\t%s [ \"%s\" ] = ", prefix, lua_tostring ( state, -2 )); break; } case LUA_TNUMBER: { stream.Print ( "\t%s [ %s ]\t= ", prefix, lua_tostring ( state, -2 )); break; } }; switch ( lua_type ( state, -1 )) { case LUA_TBOOLEAN: { int value = lua_toboolean ( state, -1 ); cc8* str = ( value ) ? "true": "false"; stream.Print ( "%s\n", str ); break; } case LUA_TTABLE: { uintptr tableID = ( uintptr )lua_topointer ( state, -1 ); if ( this->mTableMap.contains ( tableID )) { stream.Print ( "objects [ 0x%08X ]\n", tableID ); } break; } case LUA_TSTRING: { STLString str = _escapeString ( lua_tostring ( state, -1 )); stream.Print ( "\"%s\"\n", str.c_str ()); break; } case LUA_TNUMBER: { stream.Print ( "%s\n", lua_tostring ( state, -1 )); break; } case LUA_TUSERDATA: { USLuaObject* object = state.GetLuaObject < USLuaObject >( -1 ); u32 instanceID = this->GetID ( object ); stream.Print ( "objects [ 0x%08X ]\n", instanceID ); break; } case LUA_TLIGHTUSERDATA: { stream.Print ( "%p,\n", lua_touserdata ( state, -1 )); break; } }; ++count; } return count; }
//----------------------------------------------------------------// void MOAISerializer::WriteObjectInits ( USStream& stream ) { if ( !this->mPending.size ()) return; while ( this->mPending.size ()) { MOAILuaObject* object = this->mPending.front (); assert ( object ); this->mPending.pop_front (); u32 id = this->GetID ( object ); stream.Print ( "\t--%s\n", object->TypeName ()); stream.Print ( "\tserializer:initObject (\n" ); MOAILuaClass* type = object->GetLuaClass (); if ( type->IsSingleton ()) { stream.Print ( "\t\t%s.get (),\n", object->TypeName ()); } else { stream.Print ( "\t\tobjects [ 0x%08X ],\n", id ); } MOAILuaStateHandle state = MOAILuaRuntime::Get ().State (); object->PushMemberTable ( state ); stream.Print ( "\t\tobjects [ 0x%08X ],\n", this->AffirmMemberID ( state, -1 )); state.Pop ( 1 ); // this should fill the table for the class lua_newtable ( state ); object->SerializeOut ( state, *this ); stream.Print ( "\t\t{" ); if ( this->WriteTable ( stream, state, -1, 3 )) { stream.Print ( "\t\t}\n" ); } else { stream.Print ( "}\n" ); } state.Pop ( 1 ); stream.Print ( "\t)\n\n" ); } }
//----------------------------------------------------------------// void USLuaSerializer::WriteInstanceDecls ( USStream& stream ) { if ( !this->mInstanceMap.size ()) return; stream.Print ( "\t--Declaring Instances\n" ); InstanceMapIt instanceIt; instanceIt = this->mInstanceMap.begin (); for ( ; instanceIt != this->mInstanceMap.end (); ++instanceIt ) { USLuaObject* object = instanceIt->second; if ( !object ) continue; uintptr id = this->GetID ( object ); USLuaClass* type = object->GetLuaClass (); if ( !type->IsSingleton ()) { stream.Print ( "\t[ 0x%08X ] = serializer:register ( %s.new (), 0x%08X ),\n", id, object->TypeName (), id ); } } stream.Print ( "\n" ); }
//----------------------------------------------------------------// void MOAISerializer::WriteObjectDecls ( USStream& stream ) { if ( !this->mObjectMap.size ()) return; stream.Print ( "\t--Declaring Instances\n" ); ObjectMapIt objectIt; objectIt = this->mObjectMap.begin (); for ( ; objectIt != this->mObjectMap.end (); ++objectIt ) { MOAILuaObject* object = objectIt->second; if ( !object ) continue; uintptr id = this->GetID ( object ); MOAILuaClass* type = object->GetLuaClass (); if ( !type->IsSingleton ()) { stream.Print ( "\t[ 0x%08X ] = serializer:registerObjectID ( %s.new (), 0x%08X ),\n", id, object->TypeName (), id ); } } stream.Print ( "\n" ); }
UString DebugTimeStamp() { using namespace std::chrono; USStream ss; system_clock::time_point now_point = system_clock::now(); std::time_t now_time = system_clock::to_time_t(now_point); #ifdef VIX_SYS_WINDOWS tm pTime; localtime_s(&pTime, &now_time); char buffer[VIX_BUFSIZE]; asctime_s(buffer, &pTime); ss << buffer; #else ss << asctime(localtime(&now_time)); #endif return ss.str(); }
//----------------------------------------------------------------// void USLuaSerializer::WriteTableInits ( USStream& stream ) { if ( !this->mTableMap.size ()) return; stream.Print ( "\t--Initializing Tables\n" ); stream.Print ( "\tlocal table\n\n" ); USLuaStateHandle state = USLuaRuntime::Get ().State (); TableMapIt tableIt = this->mTableMap.begin (); for ( ; tableIt != this->mTableMap.end (); ++tableIt ) { uintptr tableID = tableIt->first; stream.Print ( "\ttable = objects [ 0x%08X ]\n", tableID ); USLuaRef& tableRef = tableIt->second; state.Push ( tableRef ); this->WriteTableInitializer ( stream, state, -1, "table" ); state.Pop ( 1 ); stream.Print ( "\n" ); } }
//----------------------------------------------------------------// size_t USStream::WriteStream ( USStream& source ) { if ( !( source.GetCaps () & CAN_READ )) return 0; if ( !( this->GetCaps () & CAN_WRITE )) return 0; u8 buffer [ LOCAL_BUFFER ]; size_t readSize = 0; size_t writeSize = 0; size_t total = 0; do { readSize = source.ReadBytes ( buffer, LOCAL_BUFFER ); if ( readSize ) { writeSize = this->WriteBytes ( buffer, readSize ); total += writeSize; if ( writeSize != readSize ) break; } } while ( readSize == LOCAL_BUFFER ); return total; }
//----------------------------------------------------------------// u32 USStream::Pipe ( USStream& source, u32 size ) { u8 buffer [ LOCAL_BUFFER ]; u32 readSize = 0; u32 total = 0; do { if (( total + LOCAL_BUFFER ) > size ) { readSize = source.ReadBytes ( buffer, size - total ); } else { readSize = source.ReadBytes ( buffer, LOCAL_BUFFER ); } if ( readSize ) { total += this->WriteBytes ( buffer, readSize ); } } while ( readSize == LOCAL_BUFFER ); return total; }
//----------------------------------------------------------------// u32 USStream::Pipe ( USStream& source ) { u8 buffer [ LOCAL_BUFFER ]; u32 readSize = 0; u32 writeSize = 0; u32 total = 0; do { readSize = source.ReadBytes ( buffer, LOCAL_BUFFER ); if ( readSize ) { writeSize = this->WriteBytes ( buffer, readSize ); total += writeSize; if ( writeSize != readSize ) break; } } while ( readSize == LOCAL_BUFFER ); return total; }
//----------------------------------------------------------------// void USCipherStream::OpenCipher ( USStream& stream, USCipher& cipher ) { assert ( &stream != this ); this->Clear (); this->mStream = &stream; this->mCipher = &cipher; this->mPlainBlockSize = cipher.GetPlainBlockSize (); this->mCryptBlockSize = cipher.GetCryptBlockSize (); this->mCryptBaseAddr = stream.GetCursor (); this->mPlaintext = ( u8* )calloc ( 1, this->mPlainBlockSize ); this->mCrypttext = ( u8* )calloc ( 1, this->mCryptBlockSize ); this->mIsDirty = false; this->mPlainCursor = 0; this->mBlockID = 0xffffffff; this->SyncBlock ( true ); }
//----------------------------------------------------------------// void MOAISerializer::WriteReturnList ( USStream& stream ) { if ( !this->mReturnList.size ()) return; stream.Print ( "\n" ); stream.Print ( "--Returning Tables\n" ); stream.Print ( "return " ); ReturnListIt returnListIt = this->mReturnList.begin (); for ( ; returnListIt != this->mReturnList.end (); ++returnListIt ) { if ( returnListIt != this->mReturnList.begin ()) { stream.Print ( ", " ); } u32 id = *returnListIt; stream.Print ( "objects [ 0x%08X ]", id ); } stream.Print ( "\n" ); }
//----------------------------------------------------------------// void USLuaSerializer::SerializeToStream ( USStream& stream ) { stream.Print ( "%s\n", this->GetFileMagic ()); stream.Print ( "serializer = ... or %s.new ()\n", this->TypeName ()); stream.Print ( "\n" ); // write the initializer first, in a function stream.Print ( "local function init ( objects )\n\n" ); this->WriteTableInits ( stream ); this->WriteInstanceInits ( stream ); stream.Print ( "end\n\n" ); // now write the decls this->WriteDecls ( stream ); // call the initializer stream.Print ( "init ( objects )\n" ); this->WriteReturnList ( stream ); }
//----------------------------------------------------------------// void USLuaSerializer::WriteInstanceInits ( USStream& stream ) { if ( !this->mPending.size ()) return; while ( this->mPending.size ()) { USLuaObject* object = this->mPending.front (); assert ( object ); this->mPending.pop_front (); u32 id = this->GetID ( object ); stream.Print ( "\t--%s\n", object->TypeName ()); USLuaClass* type = object->GetLuaClass (); if ( type->IsSingleton ()) { stream.Print ( "\tserializer:initInstance ( %s.get (), {", object->TypeName ()); } else { stream.Print ( "\tserializer:initInstance ( objects [ 0x%08X ], {", id ); } USLuaStateHandle state = USLuaRuntime::Get ().State (); lua_newtable ( state ); // this should push the table for the class object->SerializeOut ( state, *this ); if ( this->WriteTable ( stream, state, -1, 2 )) { stream.Print ( "\t})\n" ); } else { stream.Print ( "})\n" ); } stream.Print ( "\n" ); } }
//----------------------------------------------------------------// void USHexDump::DumpAsCPPHeader ( USStream& stream, cc8* name, const void* data, size_t size, u32 columns ) { size_t count; u8 buffer; stream.Print ( "#ifndef _%s_H\n", name ); stream.Print ( "#define _%s_H\n", name ); stream.Print ( "\n" ); stream.Print ( "#define %s_SIZE 0x%08X\n", name, ( uint )size ); stream.Print ( "\n" ); stream.Print ( "unsigned char %s [] = {\n\t", name ); for ( count = 0; count < size; count ++ ) { if ( count ) { if ( !( count % columns )) { stream.Print ( "\n\t" ); } else { stream.Print ( " " ); } } buffer = (( u8* )data )[ count ]; stream.Print ( "0x%02X,", buffer ); }; if ((( count - 1 ) % columns )) { stream.Print ( "\n" ); } stream.Print ( "};\n" ); stream.Print ( "\n" ); stream.Print ( "#endif\n" ); }
//----------------------------------------------------------------// static void _pngFlush ( png_structp png ) { USStream* stream = ( USStream* )png_get_io_ptr ( png ); stream->Flush (); }
//----------------------------------------------------------------// static void _pngWrite ( png_structp png, png_bytep buffer, png_size_t size ) { USStream* stream = ( USStream* )png_get_io_ptr ( png ); stream->WriteBytes ( buffer, ( u32 )size ); }
//----------------------------------------------------------------// u32 USLuaSerializer::WriteTable ( USStream& stream, USLuaState& state, int idx, u32 tab ) { STLString indent; for ( u32 i = 0; i < tab; ++i ) { indent.append ( "\t" ); } u32 count = 0; u32 itr = state.PushTableItr ( idx ); while ( state.TableItrNext ( itr )) { if ( count == 0 ) { stream.Print ( "\n" ); } switch ( lua_type ( state, -2 )) { case LUA_TSTRING: { stream.Print ( "%s[ \"%s\" ] = ", indent.c_str (), lua_tostring ( state, -2 )); break; } case LUA_TNUMBER: { stream.Print ( "%s[ %s ]\t= ", indent.c_str (), lua_tostring ( state, -2 )); break; } }; switch ( lua_type ( state, -1 )) { case LUA_TBOOLEAN: { int value = lua_toboolean ( state, -1 ); cc8* str = ( value ) ? "true": "false"; stream.Print ( "%s,\n", str ); break; } case LUA_TTABLE: { uintptr tableID = ( uintptr )lua_topointer ( state, -1 ); if ( this->mTableMap.contains ( tableID )) { stream.Print ( "objects [ 0x%08X ],\n", tableID ); } else { stream.Print ( "{" ); if ( this->WriteTable ( stream, state, -1, tab + 1 )) { stream.Print ( "%s},\n", indent.c_str ()); } else { stream.Print ( "},\n" ); } } break; } case LUA_TSTRING: { STLString str = _escapeString ( lua_tostring ( state, -1 )); stream.Print ( "\"%s\",\n", str.c_str ()); break; } case LUA_TNUMBER: { stream.Print ( "%s,\n", lua_tostring ( state, -1 )); break; } case LUA_TLIGHTUSERDATA: { stream.Print ( "%p,\n", lua_touserdata ( state, -1 )); break; } }; ++count; } return count; }